// JavaScript Document

function empty(x)
{
	return x == '';
}

function numeric(x)
{
   var ValidChars = "0123456789.";
   var numeric=true;
   var Char; 
   for (i = 0; i < x.length && numeric == true; i++) 
   { 
     Char = x.charAt(i); 
     if (ValidChars.indexOf(Char) == -1)	 
		 	numeric = false;		 
		}
   return numeric;   
}

function email(x)
{
  email_regx = /^[^@]+@[^@]+.[a-z]{2,}$/i;
	return !(x.search(email_regx) == -1); 
}

	// Trim all is to check for the validation of leading spaces in the text boxes--By Boopathi
	function trimAll( strValue ) 
	{
		var objRegExp = /^(\s*)$/;

		//check for all spaces
		if(objRegExp.test(strValue)) 
		{
			strValue = strValue.replace(objRegExp, '');
			if(strValue.length == 0)
				return strValue;
		}

		//check for leading & trailing spaces
		objRegExp = /^(\s*)([\W\0]*)(\b\s*$)/;
		if(objRegExp.test(strValue)) 
		{
			//remove leading and trailing whitespace characters
			strValue = strValue.replace(objRegExp, '$2');
		}

		return strValue;
	}


	function validateContactForm()
	{
		$error_count = 0;
		
		/* validation start */	
		if(empty(trimAll(document.getElementById('name').value)))
		{		
			document.getElementById('contact_name_label').className = 'label_error';
			$error_count++;		
		}
		
		else if(trimAll(document.getElementById('name').value == ' '))
		{		
			document.getElementById('contact_name_label').className = 'label_error';
			$error_count++;		
		}		
		else
		{		
			document.getElementById('contact_name_label').className = 'label';
		}		

		if(empty(trimAll(document.getElementById('emailaddress').value)))
		{		
			document.getElementById('contact_email_label').className = 'label_error';
			$error_count++;		
		}	
		
		else if(!email(document.getElementById('emailaddress').value))
		{
			document.getElementById('contact_email_label').className = 'label_error';
			$error_count++;
		}
		else
		{		
			document.getElementById('contact_email_label').className = 'label';
		}	
		
		if(empty(trimAll(document.getElementById('comments').value)))
		{
			document.getElementById('contact_comments_label').className = 'label_error';
			$error_count++;
		}
		else
		{		
			document.getElementById('contact_comments_label').className = 'label';
		}		

		if($error_count > 0)
		{
			alert('You have left one or more of the fields in the form blank.  Please make the necessary changes.');	

		}
		else
		{
			document.getElementById('contactForm').submit();	
		}
	}

	//Enetr key validation for contact us form--By Boopathi
	function enterkeyforcontactus(e,formname)
	{
		if(e.keyCode==13)
		{
			if (navigator.appName=="Netscape")
			{
				e.preventDefault();
			}
			else
				e.keyCode=0;
			validateContactForm();
		}
	}