function validateForm()
{	
    clearErrorMsgs();
	trimAttributes();
    if(validateAttributes())
	{
		document.captcha.submit();
	}
}

function trimAttributes()
{
    document.captcha.firstName.value =trimAll(document.captcha.firstName.value);
    document.captcha.email.value =trimAll(document.captcha.email.value);
    document.captcha.lastName.value =trimAll(document.captcha.lastName.value);
    document.captcha.message.value =trimAll(document.captcha.message.value);
}

function trimAll(stringToTrim) 
{
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function clearErrorMsgs()
{
	document.getElementById('firstNameError').innerHTML="";
	document.getElementById('emailError').innerHTML="";
	document.getElementById('lastNameError').innerHTML="";
	document.getElementById('messageError').innerHTML="";
	document.getElementById('captchaError').innerHTML="";
}
function validateAttributes()
{
	var firstName = document.captcha.firstName.value;
	var email = document.captcha.email.value;
	var lastname = document.captcha.lastName.value;
	var message=document.captcha.message.value;
	var captchaText = document.captcha.recaptcha_response_field.value;
	var isAttrValid = true;

	if(isEmptyString(firstName))
	 {
		isAttrValid = false;
		document.getElementById('firstNameError').innerHTML="Please enter your First Name";
	 }
	 if(isEmptyString(email))
	 {
		isAttrValid = false;
		document.getElementById('emailError').innerHTML="Please enter your Email";
	 }
	 else if(validateEMailFormat(email) == false )
	 {
		isAttrValid = false;
		document.getElementById('emailError').innerHTML="Please enter a valid Email"; 
	 }
	 if(isEmptyString(lastname))
	 {
		isAttrValid = false;
		document.getElementById('lastNameError').innerHTML="Please enter your Last Name";
	 }
	  if(isEmptyString(message))
	 {
		isAttrValid = false;
		document.getElementById('messageError').innerHTML="Please enter your Message";
	 }
	 if(isEmptyString(captchaText))
	 {
		isAttrValid = false;
		document.getElementById('captchaError').innerHTML="Please enter Captcha characters";
	 }
	 return isAttrValid;
}
   
function isEmptyString(text)
{

	if( text == null || text == '')
		return true;
		
	var empty = true;
	
	for(var k=0; k< text.length; k++)
	{
		if( text.charAt(k) != ' ' )
			return false;
	}
	
	return empty;
}

function validateEMailFormat(email)
{
	var atIndex = email.lastIndexOf('@');
	var dotIndex = email.lastIndexOf('.');	
	var stringBeforeAt =  email.substring(0, atIndex);     
	var stringafterAt =  email.substring(atIndex+1 , email.length);   
    if(email.indexOf('.')==0)
	{
	   return false;
	}
    
	if((stringafterAt == null  || stringafterAt =='') || (stringBeforeAt == null || stringBeforeAt =='') )
	{
	   return false;
	}
	
	if(!/^[\,\.0-9A-Za-z-_-]+.$/.exec( stringafterAt) || !/^[`~\"'"+?^~!#$%&*=\/|®\.0-9A-Za-z-_-]+$/.exec( stringBeforeAt))
	{
	   return false;
	}
	
	if (email.indexOf('@') != email.lastIndexOf('@') || atIndex == -1 || atIndex == 0 ||
		dotIndex == -1 || dotIndex == 0 || atIndex == dotIndex +1 || atIndex +1 == dotIndex || 
		stringBeforeAt.lastIndexOf(".") + 1 == atIndex  || email.length == email.lastIndexOf(".") + 1 ||stringafterAt.indexOf(".") == 0)
	{
		return false;
	}
	else
	{
	
		dotIndex = stringafterAt.lastIndexOf('.');	
		stringAfterDot = stringafterAt.substring(dotIndex, stringafterAt.length); 
		if (stringAfterDot.indexOf('.') == -1 )
		{
			return false;
		}  
		while (dotIndex !=-1 )
		{
			email = email.replace('.', '');
			if (dotIndex == email.indexOf('.'))
			{
				return false;
			}
			else
			{
				dotIndex = email.indexOf('.');
			}
		} 
	}
	return true;
}
function switchDivs(divToHide,divToDisplay)
{
	document.getElementById(divToHide).style.display ='none';
	document.getElementById(divToDisplay).style.display ='';
}