var InvalidPhoneMail = 'The information provided by you does not appear to be correct.\r\nPlease verify your information and resubmit the form to proceed.\r\n\r\nWe apologize for the inconvenience!'; 
var msgBlank= " cannot be less than 5 char\'s or blank!";
var msgB= " can not be blank!";
var msgInvalidChar= " entry has some invalid characters!";
var msgReqChar= " requires chars only!";
var msgTotal = " Total characters in Firstname and Lastname should be minimum 5 and maximum 40!"
var msgDigit = " should be in digits only!"
var msgChar = " should be in char!"
var msgEmailTotal = " Total characters in email  should be minimum 7 and maximum 64!"
var msgCommentTotal = " should be at least 5 characters!"
var msgEmailInvalid = " missing some valid characters or it is in upper case!"
var msgSelect = " please select"
var msgLength = " number can not be blank and make sure that it is not more than 20 digits!"
var msgPhone=" entry's first character should be numeric!"

function FrontPage_Form1_Validator(theForm)
{
var flen=0;
var llen=0;
var qstate=false


	var offensive = ValidateOffensive(theForm.email.value);
	if(offensive !=true)
	{
    	alert(InvalidPhoneMail);
		theForm.email.focus();
		theForm.email.select();
       	return false;
	}


//---------for loop  start for validations---------------------

	//---------validation start for checking blank values---------

for(j=0;j<theForm.elements.length;j++)
{ 
  	if (theForm.elements[j].type=="text")
	{

		if(theForm.elements[j].name == "company")
		{
	     	var sVal=trim(theForm.elements[j].value)
			flen=sVal.length;			
			if(flen==0){
				alert('Please fill in the company field.')
				theForm.elements[j].focus()
				theForm.elements[j].select()
				return false;
			}
			else
			{
			   	if(checkSQLServerChar(sVal)==false)
				{
					alert('Invalid Input, Single/double quotes not allowed!');
					theForm.elements[j].focus()
					theForm.elements[j].select()
		        	return false;
		    	}
			}
		}

		
		//------------validation start for blank values for other fields-----------
		if(theForm.elements[j].name == "address1")
		{
	     	var sVal=trim(theForm.elements[j].value)
			flen=sVal.length;			
			if(flen==0){
				alert('Please fill in the address1 field.')
				theForm.elements[j].focus()
				theForm.elements[j].select()
				return false;
			}
			else
			{
			   	if(checkSQLServerChar(sVal)==false)
					{
						alert('Invalid Input, Single/double quotes not allowed!');
						theForm.elements[j].focus()
						theForm.elements[j].select()
			        	return false;
			    	}
			}
		}

		//-----Address2 and Address3 For all Form accept NCP
		
		if(theForm.elements[j].name=="address2"){
	     	var sVal=trim(theForm.elements[j].value)
			flen=sVal.length;
			if(checkSQLServerChar(sVal)==false)
				{
					alert('Invalid Input, Single/double quotes not allowed!');
					theForm.elements[j].focus()
					theForm.elements[j].select()
			        return false;
			    }
		}

		//------------------------------ City validation starts here--

		if((theForm.elements[j].name== "city") || (theForm.elements[j].name== "city1"))
		{
			var city=trim(theForm.elements[j].value)
			clen=city.length;
			if (clen==0)
			{
				alert('Please fill in the city field.')
				theForm.elements[j].focus()
				theForm.elements[j].select()
				return false;
			}
			else
			{
			   	if(checkSQLServerChar(city)==false)
				{
					alert('Invalid Input, Single/double quotes not allowed!');
					theForm.elements[j].focus()
					theForm.elements[j].select()
			        return false;
			    }
			}
		} 

		//------------------------------ State validation starts here--

//		if(theForm.elements[j].name== "state")
//		{
//			var sText=trim(theForm.elements[j].value)
//			slen=sText.length;
//			if (slen==0)
//			{
//				alert('Please fill in the city field.')
//				theForm.elements[j].focus()
//				theForm.elements[j].select()
//				return false;
//			}
//			else
//			{
//			   	if(checkSQLServerChar(sText)==false)
//				{
//					alert('Invalid Input, Single/double quotes not allowed!');
//					theForm.elements[j].focus()
//					theForm.elements[j].select()
//			        return false;
//			    }
//			}
//		}  

		//---------------Zip validation starts here------------------------------------

		if(theForm.elements[j].name == "zip")
		{
	     	var sVal=trim(theForm.elements[j].value)
			flen=sVal.length;			
			if(flen==0){
				alert('Please fill in the zip field.')
				theForm.elements[j].focus()
				theForm.elements[j].select()
				return false;
			}
			else
			{
				if (checkChars_numbers(theForm.elements[j].value,theForm.elements[j])==false)
					return false;
			}
		}		


		//------------------------------ country validation starts here--
		if(theForm.elements[j].name== "country")
		{
	     	var country=trim(theForm.elements[j].value)
			clen=country.length;
			if (clen==0)
			{
		             	message(msgB,theForm.elements[j])
				return false;
			}
			else
			{
				if(checkcharacters(country)==false)
				{
					message(msgInvalidChar,theForm.elements[j])
					return false;
				}
			 }
        }  


		if(theForm.elements[j].name == "firstname")
		{
	     	var fname=trim(theForm.elements[j].value)
			flen=fname.length;
			if (flen==0){
				alert('Please fill in the primary contact field.')
				theForm.elements[j].focus()
				theForm.elements[j].select()
				return false;
			}
			else
			{
				if(checkcharacters(fname)==false){
					alert('Invalid Input, First Name has some invalid characters!')
					theForm.elements[j].focus()
					theForm.elements[j].select()						
					return false;
				}
			}
		}
		if(theForm.elements[j].name == "title11")
		{
	     	var fname=trim(theForm.elements[j].value)
			flen=fname.length;
			if (flen==0){
				alert('Please fill in the title field.')
				theForm.elements[j].focus()
				theForm.elements[j].select()
				return false;
			}
			else
			{
				if(checkcharactersTitle(fname)==false){
					alert('Invalid Input, Title has some invalid characters!')
					theForm.elements[j].focus()
					theForm.elements[j].select()						
					return false;
				}
			}
		}
		//-- Validate Country Canada and US with State Region -------------------------------------
		var test=0;
		if(theForm.elements[j].name == "address2")
		{
			test=j;
		}
		
		if(test==j)
		{ 
			if(document.getElementById("country").options[document.getElementById("country").selectedIndex].value == "United States")
			{
				if(document.getElementById("USState_id").options[document.getElementById("USState_id").selectedIndex].value == "SELECT")
				{
					alert("Please select state of United State.");
					document.getElementById("USState_id").focus();
					return false;
				}
			}
			else if(document.getElementById("country").options[document.getElementById("country").selectedIndex].value == "Canada")
			{
				if(document.getElementById("CanadaState_id").options[document.getElementById("CanadaState_id").selectedIndex].value == "SELECT")
				{
					alert("Please select state of Canada.");
					document.getElementById("CanadaState_id").focus();
					return false;
				}
			}
		}


		if(theForm.elements[j].name == "name")
		{
	     	var fname=trim(theForm.elements[j].value)
			flen=fname.length;
			if (flen==0){
				alert('Invalid Input, Primary Contact can not be blank!')
				theForm.elements[j].focus()
				theForm.elements[j].select()
				return false;
			}
			else
			{
				if(checkcharacters(fname)==false){
					alert('Invalid Input, Primary Contact has some invalid characters!')
					theForm.elements[j].focus()
					theForm.elements[j].select()						
					return false;
				}
			}
		}		
		//if(theForm.elements[j].name == "title" || theForm.elements[j].name == "title1")
		//{
	    // 	var fname=trim(theForm.elements[j].value)
		//	flen=fname.length;
		//	if (flen==0){
		//		alert('Please fill in the title field.')
		//		theForm.elements[j].focus()
		//		theForm.elements[j].select()
		//		return false;
		//	}
		//	else
		//	{
		//		if(checkcharactersTitle(fname)==false){
		//			alert('Invalid Input, Title has some invalid characters!')
		//			theForm.elements[j].focus()
		//			theForm.elements[j].select()						
		//			return false;
		//		}
		//	}
		//}		
		//--------validation start for email--------------------------------------
		if(theForm.elements[j].name == "email")
		{
			var status=checkChars_Email(trim(theForm.elements[j].value));			
			if(status !=true)
			{
		        alert(status);
				theForm.elements[j].focus();
				theForm.elements[j].select();
				return false;
			}
		}
	 	if(theForm.elements[j].name == "email")
		{
			if(validate(trim(theForm.elements[j].value))==false)
			{
				alert('Email has some invalid characters!');
				theForm.elements[j].focus();
				theForm.elements[j].select();
				return false;					
			}
		}

		if(theForm.elements[j].name == "email1")
		{
			var semail=trim(theForm.elements[j].value);
			flen=semail.length;
			if(flen!=0)
			{
				if(validate(trim(theForm.elements[j].value))==false)
				{
					alert('Email has some invalid characters!');
					theForm.elements[j].focus();
					theForm.elements[j].select();
					return false;					
				}
			}
		}

		if(theForm.elements[j].name== "folder_name")
		{	
			if(theForm.elements[j].value=='')
			{
				alert('Invalid Input, please make sure that foldername has been entered!')
				theForm.elements[j].focus()
				theForm.elements[j].select()
				return false;
			}
		}



		if(theForm.elements[j].name== "lastname")
		{
	   		var lname=trim(theForm.elements[j].value)
			llen=lname.length;
			if (llen==0)
			{
				alert('Invalid Input, Last Name can not be blank!')
				theForm.elements[j].focus()
				theForm.elements[j].select()
				return false;
			}
			else
			{
				if(checkcharacters(lname)==false)
				{
					alert('Invalid Input, Last Name has some invalid characters!')
					theForm.elements[j].focus()
					theForm.elements[j].select()						
					return false;
				}
			}
		}  

		//-------------------validation for lastname ends here

		//-- Validating firstname+lastname total length ----------------
		if((flen+llen)>40 ){
			alert("Invalid Input, First and Last Name together cannot exceed more than 40 char\'s.")
			theForm.elements[j].focus()
			theForm.elements[j].select()				
			return false;
		}
		//------------validation end for lastname----------------------------------

		//------------validation start for blank values for other fields-----------
			//-- serialno --------------------------------------------------
			if(theForm.elements[j].name == "serialno"){
	     		var sVal=trim(theForm.elements[j].value)
				flen=sVal.length;			
				if(flen==0){			
					alert('Invalid Input, Serial Number can not be blank!')
					theForm.elements[j].focus()
					theForm.elements[j].select()
					return false;
				}
				else
				{
			   		if(checkSQLServerChar(sVal)==false)
						{
							alert('Invalid Input, Single/double quotes not allowed!');
							theForm.elements[j].focus()
							theForm.elements[j].select()
			        		return false;
			    		}
				}				
			}

			//-- subscriptionno ---------------------------------------------
			if(theForm.elements[j].name == "subscription_no"){
	     		var sVal=trim(theForm.elements[j].value)
				flen=sVal.length;			
				if(flen==0){
					alert('Invalid Input, Subscription Number can not be blank!')
					theForm.elements[j].focus()
					theForm.elements[j].select()
					return false;
				}
				else
				{
			   		if(checkSQLServerChar(sVal)==false)
						{
							alert('Invalid Input, Single/double quotes not allowed!');
							theForm.elements[j].focus()
							theForm.elements[j].select()
			        		return false;
			    		}
				}				
			}

			//-- products ---------------------------------------------------
			if(theForm.elements[j].name == "products"){
	     		var sVal=trim(theForm.elements[j].value)
				flen=sVal.length;			
				if(flen==0){
					alert('Invalid Input, Products can not be blank!')
					theForm.elements[j].focus()
					theForm.elements[j].select()
					return false;
				}
				else
				{
					if (checkChars_numbers(theForm.elements[j].value,theForm.elements[j])==false)
						return false;
				}
			}

			//-- version ----------------------------------------------------
			if(theForm.elements[j].name == "version"){
	     		var sVal=trim(theForm.elements[j].value)
				flen=sVal.length;			
				if(flen==0){
					alert('Invalid Input, Version can not be blank!')
					theForm.elements[j].focus()
					theForm.elements[j].select()
					return false;
				}
				else
				{
					if (checkChars_numbers(theForm.elements[j].value,theForm.elements[j])==false)
						return false;
				}
			}

			//-- hostapp ----------------------------------------------------
			if(theForm.elements[j].name == "hostapp"){
	     		var sVal=trim(theForm.elements[j].value)
				flen=sVal.length;			
				if(flen==0){
					alert('Invalid Input, Host Application can not be blank!')
					theForm.elements[j].focus()
					theForm.elements[j].select()
					return false;
				}
				else
				{
			   		if(checkSQLServerChar(sVal)==false)
						{
							alert('Invalid Input, Single/double quotes not allowed!');
							theForm.elements[j].focus()
							theForm.elements[j].select()
			        		return false;
			    		}
				}
			}

			//-- pctype -----------------------------------------------------
			if(theForm.elements[j].name == "pctype"){
	     		var sVal=trim(theForm.elements[j].value)
				flen=sVal.length;			
				if(flen==0){
					alert('Invalid Input, PC type can not be blank!')
					theForm.elements[j].focus()
					theForm.elements[j].select()
					return false;
				}
				else
				{
					if (checkChars_numbers(theForm.elements[j].value,theForm.elements[j])==false)
						return false;
				}
			}
		//--------validation start for telephone---------------------------------
		if(theForm.elements[j].name == "phone")
		{
			if (checkChars_Phone(trim(theForm.elements[j].value),theForm.elements[j])==false)
			return false;
		}

		//------------validation end -----------------------------------	

	}//-------validation end for blank values------------------------------	


	
//--------validation start for date-----------------------
	if(theForm.elements[j].name == "heard_from")
		{

			if(theForm.elements[j].options.selectedIndex==0)
			{
			alert("Invalid Input, please select " + theForm.elements[j].name + "!"); 			
			theForm.elements[j].focus();
				return false;

			}
			
		}
	

//--------validation start for comments---------------------------------
	if(theForm.elements[j].name == "comment" || theForm.elements[j].name == "comments")
	{
     		var sVal=trim(theForm.elements[j].value)
		flen=sVal.length;			
		if(flen != 0)
		{
	   		if(checkSQLServerChar(sVal)==false)
			{
				alert('Invalid Input, Single/double quotes not allowed!');
				theForm.elements[j].focus();
				theForm.elements[j].select();
	        		return false;
	    		}
			if(flen>1024){
				alert('Invalid Input, Comments cannot be more than 1024 Characters!')
				theForm.elements[j].focus();
				theForm.elements[j].select();
				return false;
			}
			if(flen<5){
				alert("Invalid Input, Total characters in comment should be more than 5 characters!");
				theForm.elements[j].focus();
				theForm.elements[j].select();
				return false;
			}
		}
	}


		//-- NULL Validation for country drop down ------------------------------
		/*if(theForm.elements[j].name == "country"){
			if(theForm.elements[j].options.selectedIndex==0){
				alert("Invalid Input, please select " + theForm.elements[j].name + "!"); 			
				theForm.elements[j].focus();
				return false;
			}
		}*/


//------------validation end--------------------------------------------

}
//------------End of for loop---------------------------		
SetCookies(theForm);
return true;
}

//-----------End of function----------------------------		
//-----------------------------------------------------		

function ltrim ( s )
{
	return s.replace( /^\s*/, "" )
}

//Function to remove Right space
function rtrim ( s )
{
	return s.replace( /\s*$/, "" );
}

//using both  Functions to trim both sides.

function trim ( s )
{
	return rtrim(ltrim(s));

}

function checkcharactersTitle(sText) 
	{	
	var validDigits = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.().";
	var cch = sText.length;
		
	for (var i = 0; i < cch; i++)
	{
		ch = sText.charAt(i);
		if (validDigits.indexOf(ch)<0)
			return false;
	}	
	return true;	
}

function checkcharacters(sText) 
	{	
	var validDigits = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ";
	var cch = sText.length;
		
	for (var i = 0; i < cch; i++)
	{
		ch = sText.charAt(i);
		if (validDigits.indexOf(ch)<0)
			return false;
	}	
	return true;	
}


function checkChars_Phone(sText,ele)
	 {	

	//var validChars = ")(-0123456789";
	//var validChars = ")(+#&*[]-.,abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
	var validChars = ")(+#[]-.,0123456789";		
	var validChars = validChars +" ";
		var cch = sText.length;
		var state=true;	
		if(cch<5)
			{
			alert('Please fill in the telephone field and it cannot be less than 5 digits.');
			ele.focus();
			return false;
			}
		if (sText=="" || cch>22)
			{
			alert("Please fill in the telephone field and it is not more than 20 digits!")
			ele.focus();
			return false;
			}

		if(ValidatePhone(sText)==false)
		{
			alert(InvalidPhoneMail);
			ele.focus();
			return false;
		}

		for (var i = 0; i < cch; i++)
		{
			ch = sText.charAt(i);
			//alert(ch);
						
			if (validChars.indexOf(ch)==-1)
				{
				state=false;
				break		
				}
		}
		if (state==false)
			{
			 alert("Telephone field has some invalid characters.")
			 ele.focus();
			 return false;
			}
		else
			return true;

	 } // function ends here

/*function checkChars_Email(sText,ele)
	 {	
		var validChars = ")(-0123456789_.@abcdefghijklmnopqrstuvwxyz";
		var cch = sText.length;
		var state=true;	
		if (sText=="" || cch<7)
			{
			message(msgEmailTotal,ele)
			return false;
			}
		for (var i = 0; i < cch; i++)
		{
			ch = sText.charAt(i);
			if (validChars.indexOf(ch)==-1)
				{
				
				state=false;
				break		
				}
		}
		if (state==false)
			{
			 message(msgEmailInvalid,ele)
			  return false;
			}
		else
			return true;

	 } // function ends here*/


//-------------------------------------------------------------------------------------
//-- Function to check alphanumeric values --------------------------------------------
//-------------------------------------------------------------------------------------
function checkChars_numbers(sText,ele){	
	var validChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.- ";
	var cch = sText.length;
	var state=true;	

	for (var i = 0; i < cch; i++){
		ch = sText.charAt(i);
		if (validChars.indexOf(ch)==-1){
			state=false;
			break		
		}				
	}
	if (state==false){
		  message(msgInvalidChar,ele)
		  return false;
	}
	else{
		return true;
	}
}
//-- Function Ends - checkChars_numbers(sText,ele) -----------------------------------



	function message(msg,ele)
	{
	alert( "Invalid Input, " + ele.name + msg);
	ele.focus();
	ele.select();
	
	}


	function checkChars_Email(sText) 
		{	
		var inValidChars = "`,~@#$%^&*()_-+=|!\\//'.?<>;:"
		var x;
		if (sText == "")
			return "Please fill in the email field.";
		if (sText.length < 6)
			return "E-mail Address cannot cantain less than 6 characters! Please re-enter.";
		if (sText.indexOf(" ") != -1)
			return "E-mail Address cannot contain space! Please re-enter."
		if (sText.indexOf(".") == -1 || sText.indexOf("@") == -1)
			return "Invalid E-mail Address! Please re-enter."
		if (sText.indexOf("@.") >0)
			return "Invalid E-mail Address! Please re-enter."
		if (sText.indexOf("..") >0)
			return "Invalid E-mail Address! Please re-enter."
	if (sText.indexOf("@") <2)
		return "Invalid Input, E-mail Address requires atleast two char\'s before \'@\'!"
	if (sText.search("nobody") >=0 || sText.search("anyone") >=0)
		return "Invalid Input, E-mail Address cannot contain \'anyone\',\'nobody\'!"
		for(var i=0;i<inValidChars.length-1;i++)
		    {
			if (sText.indexOf(inValidChars.substring(i,i+1)) ==0)
				return "Invalid E-mail Address! Please re-enter."
		   }
		var cntr=0;
		for(var i=0;i<sText.length;i++)
		    {
			if (sText.substring(i,i+1)=="@")
				cntr++;
		   }
			if(cntr>1)
			    return "Invalid E-mail Address! Please re-enter."	
		return true;

		}
		
//-------------------------------------------------------------------------------------
//-- Function to validate email address -------------------------------------------
//-------------------------------------------------------------------------------------
function validate(strEmail)
{
	validRegExp = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
	if (strEmail.search(validRegExp) == -1)
	{	
		return false;
	}
	return true;
} 
function checkSQLServerChar(sText)
	{	
	var validChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789=-~!@#$%^&*`()_+|:;<>?][}{,./ \\";
	var cch = sText.length;
	var state=true;	
	for (var i = 0; i < cch; i++)
	{
		ch = trim(sText.charAt(i));

		if (validChars.indexOf(ch)==-1)
			{
			state=false;
			break		
			}				
	}
	if (state==false)
		{
			return false;
		}
	else{return true;}
}		


//-- Validate Offensive words in email -------------------------------------
function ValidateOffensive(userinput)
{
	var forbidden="sex spam "; 

	 //babes shit fuck damn porno cum cunt prick "
	 //+"pecker asshole pedophile man-boy man/boy dong twat pussy "
	 //+"monica spam ";

	/* As you add or subtract words from the forbidden list above,
	   be sure to change the nr_words variable below to reflect
	   the actual number of words in your forbidden string. */

	var nr_words=2;
	var bgn=0;
	var element_nr=0;
	var pos=0;
	var txt="";
	var wrd="";
	var state=true;	

	txt=userinput.toLowerCase();
	for (var i=0;i<nr_words;i++)
	{
		bgn=(i==0?0:bgn)
		pos=forbidden.indexOf(" ",bgn);
		wrd=forbidden.substring(bgn,pos);
		bgn=pos+1;
		while (txt.indexOf(wrd)>-1)
		{
			pos=txt.indexOf(wrd);
			state=false;
			break;
		}
	}

	if (state==false)
		return false;
	else
		return true;
}	

//--  Validate Function for Phone Numbers -------------------------------------
function ValidatePhone(txtphone)
{
	var phoneno="5555555|5551212|555-1212|555.1212|555 1212|";

	/* As you add or subtract words from the phoneno list above,
	   be sure to change the nr_phone variable below to reflect
	   the actual number of words in your phoneno string. */

	var nr_phone= 5;
	var bgn=0;
	var pos=0;
	var txt="";
	var wrd="";
	var state=true;
	txt=txtphone;
	for (var i=0; i<nr_phone; i++)
	{
		bgn=(i==0?0:bgn);
		pos=phoneno.indexOf("|",bgn);
		wrd=phoneno.substring(bgn,pos);
		bgn=pos+1;
		while(txt.indexOf(wrd)>-1)
		{
			pos=txt.indexOf(wrd);
			state=false;
			break;
		}
	}
	if (state==false)
	{
		return false;
	}
	else
		return true;
}
