
function validateContactForm()
{	

	
	var selectd=document.getElementById("submissionType");
	
	clearErrorMsgs();
	trimAttributes();
	//openURL();
	//document.getElementById('sample')
	
   if(validateAttributes())
	{
	
	document.captcha.submit();
	//document.getElementById('emailConfirm').disabled=true;
//	document.getElementById('emailForm').disabled=false;
	}
}

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;
    var selectd=document.getElementById("submissionType");
	if(selectd==null)
	{
   	if(document.getElementById('submission')!=null){
		 isAttrValid = false;
		document.getElementById('submission').innerHTML="No Submission Found";
	}
	}
	if(isEmptyString(firstName))
	 {
		
		isAttrValid = false;
		document.getElementById('firstNameError').innerHTML="Please enter your First Name";
	 }
	 if(!isEmptyString(firstName)){
	 	
		document.getElementById('firstNameError').innerHTML="";
	 }
	 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(email)&&validateEMailFormat(email))
	 {
		document.getElementById('emailError').innerHTML="";
	 }
   
	 //if(validateEMailFormat(email) == true )
	 //{
			
	//		document.getElementById('emailError').innerHTML="";
	 //}
	 if(isEmptyString(lastname))
	 {
		isAttrValid = false;
	document.getElementById('lastNameError').innerHTML="Please enter your Last Name";
	 }

     if(!isEmptyString(lastname))
	 {
			document.getElementById('lastNameError').innerHTML="";
	 }

	  if(isEmptyString(message))
	 {
		isAttrValid = false;
		document.getElementById('messageError').innerHTML="Please enter your Message";
	 }


	  if(!isEmptyString(message))
	 {
		document.getElementById('messageError').innerHTML="";
	 }
	 if(isEmptyString(captchaText))
	 {
		isAttrValid = false;
		document.getElementById('captchaError').innerHTML="Please enter Captcha characters";
	 }
	  if(!isEmptyString(captchaText))
	 {
		  document.getElementById('captchaError').innerHTML="";
	 }
	 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 ='';
}

function includeSubmissionTypeList(id, url) 
{
   var req = false;
  // For Safari, Firefox, and other non-MS browsers
  if (window.XMLHttpRequest) {
  try {
   req = new XMLHttpRequest();
  } catch (e) {
   req = false;
  }
  } else if (window.ActiveXObject) {
    // For Internet Explorer on Windows
    try {
	req = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
	try {
	 req = new ActiveXObject("Microsoft.XMLHTTP");
	} catch (e) {
	req = false;
	  }
  	}
 }
							  
	var element = document.getElementById(id);
	if (!element) {
	return;
	}
	if (req) {
	// Synchronous request, wait till we have it all
	req.open('GET', url, false);
	req.send(null);
	element.innerHTML = req.responseText;
	} else {
	element.innerHTML =
		"Sorry, your browser does not support " +
		"XMLHTTPRequest objects. This page requires " +
		"Internet Explorer 5 or better for Windows, " +
		"or Firefox for any system, or Safari. Other " +
		"compatible browsers may also exist.";
  }
}
