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= " 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 = " Total characters in comment  should be more than 4 characters!"
var msgEmailInvalid = " missing some valid characters or it is in upper case!"
var msgSelect = " please select"
var msgLength = " can 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 offensive = ValidateOffensive(theForm.email.value);
	if(offensive !=true)
	{
    	alert(InvalidPhoneMail);
		theForm.email.focus();
		theForm.email.select();
       	return false;
	}

//---------for loop  start for validations---------------------

	for(j=0;j<theForm.elements.length;j++)
	{ 
//---------validation start for checking blank values---------

	if (theForm.elements[j].type=="text")
	{
			//-- firstname  - Validating values for 5 char ---
			if(theForm.elements[j].name == "fname"){
	     		var fname=trim(theForm.elements[j].value)
				flen=fname.length;
				if (flen==0){
					alert('Invalid Input, Name can not be blank!')
					theForm.elements[j].focus()
					theForm.elements[j].select()
					return false;
				}
				else{
					if(checkcharacters(fname)==false){
						alert('Invalid Input, Name has some invalid characters!')
						theForm.elements[j].focus()
						theForm.elements[j].select()						
						return false;
					}
				 }
			}  
	
			
			//------------validation end for Title----------------------------------
			//-- firstname  - Validating values for 5 char ---
			if(theForm.elements[j].name == "Title"){
	     		var fname=trim(theForm.elements[j].value)
				flen=fname.length;
				if (flen==0){
					alert('Invalid Input, Title can not be blank!')
					theForm.elements[j].focus()
					theForm.elements[j].select()
					return false;
				}
				else{
					if(checkcharacters(fname)==false){
						alert('Invalid Input, Title has some invalid characters!')
						theForm.elements[j].focus()
						theForm.elements[j].select()						
						return false;
					}
				 }
			}  
	


		//------------validation end for title----------------------------------


		if(theForm.elements[j].name == "company")
		{
	     	var sVal=trim(theForm.elements[j].value)
			flen=sVal.length;			
			//if(flen==0){
				//alert('Invalid Input, Company Name 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;
		    	}
			}
		}

		
		//-- Title ----------------------------------------------------
			if(theForm.elements[j].name == "title"){
	     		var sVal=trim(theForm.elements[j].value)
				flen=sVal.length;			
				if(flen==0){
					alert('Invalid Input, Title 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 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;
			    }
		}
//------------validation end -----------------------------------	

	}

	if(theForm.elements[j].name == "year_used")
		{

			if(theForm.elements[j].options.selectedIndex==0)
			{
			alert("Invalid Input, please select How long have you been using NetZoom?!"); 			
			theForm.elements[j].focus();
				return false;

			}
			
		}

	if(theForm.elements[j].name == "helping")
		{

			if(theForm.elements[j].options.selectedIndex==0)
			{
			alert("Invalid Input, please select your Option!"); 			
			theForm.elements[j].focus();
				return false;

			}
			
		}

	
	
//--------validation start for comments---------------------------------
	if(theForm.elements[j].name == "storycomments")
	{
		if(trim(theForm.elements[j].value) == "")
		{
			alert("Invalid Input, Your NetZoom Story should not be blank!"); 			
			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));

}

//-----------------------------------------------------------
//-- Funtion - to Validate characters only ------------------
//-----------------------------------------------------------
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_Email(sText) 
		{	
		var inValidChars = "`,~@#$%^&*()_-+=|!\\//'.?<>;:"
		var x;
		if (sText == "")
			return "Invalid Input, email cannot be blank!";
		if (sText.length < 6)
			return "Invalid Input, E-mail Address cannot cantain less than 6 characters!";
		if (sText.indexOf(" ") != -1)
			return "Invalid Input, E-mail Address cannot contain space!"
		if (sText.indexOf(".") == -1 || sText.indexOf("@") == -1)
			return "Invalid Input, Invalid E-mail Address!"
		if (sText.indexOf("@.") >0)
			return "Invalid E-mail Address! Please re-enter."
		if (sText.indexOf("..") >0)
			return "Invalid E-mail Address! Please re-enter."
		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 checkChars_Digits(sText) 
{	
	var validDigits = "0123456789";
	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 (ele.name=='phone')
		{
			if(cch<5)
				{
				alert('Invalid Input, Phone/Fax Number cannot be less than 5 characters!');
				ele.focus();
				return false;
				}
			if (sText=="" || cch>22)
				{
				alert("Invalid Input, Phone/Fax Number can not be blank and make sure that 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("Invalid Input, Phone/Fax Number has some invalid characters!")
			 ele.focus();
			 return false;
			}
		else
			return true;

	 } // function ends here


	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 message(msg,ele)
	{
	alert("Invalid Input, " + ele.name + msg);
	ele.focus();
	ele.select();
	
	}
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;
}
