//**********************************************************************************************************
// SubmitIt - Form Validation
//**********************************************************************************************************
function SubmitIt(theform)
{
		var cmtsLen = document.theform.comments.value.length;
							
		if (theform.fName.value == "") {
			alert("Enter first name")
			theform.fName.focus();
			return false;
		}
		if (theform.lName.value == "") {
			alert("Enter last name")
			theform.lName.focus();
			return false;
		}
		if (!validName(theform.fName.value)) {
			alert("No special characters allowed in the first name field.")
			theform.fName.focus();
			return false;
		}
		if (!validName(theform.lName.value)) {
			alert("No special characters allowed in the last name field.")
			theform.lName.focus();
			return false;
		}
		if (theform.addr.value == "") {
			alert("Enter street")
			theform.addr.focus();
			return false;
		}
		if (theform.city.value == "") {
			alert("Enter city")
			theform.city.focus();
			return false;
		}
		if (theform.state.selectedIndex == 0) {
			alert("Select state name")
			theform.state.focus();
			return false;
		}
		if (theform.zip.value == "") {
			alert("Enter zip code")
			theform.zip.focus();
			return false;
		}
		if (!validateZIP(theform.zip.value)) 
		{
			theform.zip.focus();
			return false;
		}
		if (theform.email.value == "") {
			alert("Enter email address")
			theform.email.focus();
			return false;
		}
		if (!validEmail(theform.email.value)) {
			alert("Invalid email address, Please enter a valid email address")
			theform.email.focus();
			return false;
		}
		
		if (cmtsLen == 0) {
			alert("Enter your comments")
			theform.comments.focus()
			return false
		}	
		
		if (cmtsLen > 1000) {
			var prompt = "The comments you entered has " + cmtsLen + " characters.\n";
			prompt = prompt + "The contents of this field should be less than 1000 characters.";
			alert(prompt);
			theform.comments.focus()
			return false
		}
}
//**********************************************************************************************************
// validName - Special character validation
//**********************************************************************************************************
function validName(name) {
	invalidChars = "!@#$%^&*()+=_~`:;\/|[]{},<>?\""

	for (i=0; i<invalidChars.length; i++) {
		// does it contain any invalid characters?
		badChar = invalidChars.charAt(i)
		if (name.indexOf(badChar,0) > -1) {
			return false
		}
	}
	return true
}
//**********************************************************************************************************
// validEmail - Email Validation
//**********************************************************************************************************
function validEmail(email) {
			invalidChars = "~`!#$%^&*()=+[]{}:;\/\"'?<>, "
				
			for (i=0; i<invalidChars.length; i++) {
				// does it contain any invalid characters?
				badChar = invalidChars.charAt(i)
				if (email.indexOf(badChar,0) > -1) {
					return false
				}
			}
			atPos = email.indexOf("@",1)
				// there must be one "@" symbol
			if (atPos == -1) {
				return false
			}
			if (email.indexOf("@",atPos+1) != -1) {
				// and only one "@" symbol
				return false
			}

			aftat = (email.indexOf("@",atPos))
			aftat = aftat + 1
		    if ((aftat == email.indexOf(".")))
			{
			  return false
			}
			tot_length = email.indexOf("@")+2;
//----------------------------------------------------------
			gan_mailvalue = document.forms[0].elements[3].value;
			gan_periodPos = email.indexOf(".",atPos)
			
//----------------------------------------------------------
//			if (tot_length > (email.indexOf(".")))
			if (tot_length > gan_periodPos)
			{
				//there must be atleast 1 characters after @ and before .
				return false
			}

			periodPos = email.indexOf(".",atPos)
			if (periodPos == -1) {
				// and atleast one "." after the "@"
				return false
			}

			if (periodPos+3 > email.length)	{
				// must be atleast 2 characters of category after "."
				return false
			}
			
			return true
		}
//**********************************************************************************************************
// validateZIP - Zip Code Validation
//**********************************************************************************************************
function validateZIP(field) 
{
	var valid = "0123456789-";
	var hyphencount = 0;
	if (field.length!=5 && field.length!=10) 
	{
		alert("Please enter your 5 digit or 5 digit+4 zip code.");
		return false;
	}

	for (var i=0; i < field.length; i++) 
	{
		temp = "" + field.substring(i, i+1);
		if (temp == "-") hyphencount++;
		if (valid.indexOf(temp) == "-1") 
		{
			alert("Invalid characters in your zip code.  Please try again.");
			return false;
		}

		if ((hyphencount > 1) || ((field.length==10) && ""+field.charAt(5)!="-")) 
		{
			alert("The hyphen character should be used with a properly formatted 5 digit+four zip code, like '12345-6789'.   Please try again.");
			return false;
		}
	}
	return true;
}
//**********************************************************************************************************
// retVal - Used to retrieve the querystring parameter value
//**********************************************************************************************************
function retVal(sName)
{
  var sURL = new String(window.location);
  var iQMark= sURL.lastIndexOf('?');
  var iLensName=sName.length;
  
  //retrieve loc. of sName
  var iStart = sURL.indexOf('?' + sName +'=') //limitation 1
  if (iStart==-1)
        {//not found at start
        iStart = sURL.indexOf('&' + sName +'=')//limitation 1
        if (iStart==-1)
           {//not found at end
            return 0; //not found
           }   
        }
        
  iStart = iStart + + iLensName + 2;
  var iTemp= sURL.indexOf('&',iStart); //next pair start
  if (iTemp ==-1)
        {//EOF
        iTemp=sURL.length;
        }  
  return sURL.slice(iStart,iTemp ) ;
  sURL=null;//destroy String
}
//**********************************************************************************************************
// validatePage - Check whether the page loads for Emailing or Re-directed after Successful Emailing.
//**********************************************************************************************************
function validatePage()
{
	var strQryStringVal = retVal('msg')
	//alert(strQryStringVal); 
	if(strQryStringVal == 1)
	{
		alert("Emai sent successfully, You'll be shortly contacted by our staff. Thank you for contacting Experts !");
	}
}
//**********************************************************************************************************

