var msgBlank= " cannot be less than 5 char\'s or blank!";
var msgB= " can not be blank ";
var msgB20= " can not be blank and can not more than 20 characters!";
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 = " 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!"
var msgEmailCompare = " Please verify your email address and submit again!";

function FrontPage_Form1_Validator(theForm)
{

var flen=0;
var llen=0;

	//---------for loop  start for validations---------------------
	for(j=0;j<theForm.elements.length;j++)
	{ 
		if (theForm.elements[j].type=="text")
		{
			if(theForm.elements[j].name == "Name")
			{
	    		var sText=trim(theForm.elements[j].value)
				flen=sText.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(sText)==false)
					{
						alert('Invalid Input, Name has some invalid characters!')
						theForm.elements[j].focus()
						theForm.elements[j].select()						
						return false;
					}
				}
			}  
			if(theForm.elements[j].name == "FirstName")
			{
	    		var sText=trim(theForm.elements[j].value)
				flen=sText.length;
				if (flen==0)
				{
					alert('Invalid Input, First Name can not be blank!')
					theForm.elements[j].focus()
					theForm.elements[j].select()
					return false;
				}
				else
				{
					if(checkcharacters(sText)==false)
					{
						alert('Invalid Input, First Name has some invalid characters!')
						theForm.elements[j].focus()
						theForm.elements[j].select()						
						return false;
					}
				}
			}  
		
			//-- lasttname --------------------------------------------
			if(theForm.elements[j].name== "LastName")
			{
				var sText=trim(theForm.elements[j].value)
				llen=sText.length;
				if(checkcharacters(sText)==false)
				{
					alert('Invalid Input, Last Name has some invalid characters!')
					theForm.elements[j].focus()
					theForm.elements[j].select()						
					return false;
				}
			}
				
			//-- company ----------------------------------------------------
			/*if(theForm.elements[j].name == "Company"){
	    		var sText=trim(theForm.elements[j].value)
				flen=sText.length;			
				if(flen==0){
					alert('Invalid Input, Company Name can not be blank!')
					theForm.elements[j].focus()
					theForm.elements[j].select()
					return false;
				}
			}			
			*/
			//------------validation start for blank values for other fields-----------
			if(theForm.elements[j].name == "SNumber")
			{
	    		var sText=trim(theForm.elements[j].value)
				flen=sText.length;			
				if((flen == 0) || (theForm.elements[j].value.length>29))
				{
					alert('Invalid Input, Subscription Number can not be blank and not more than 29 characters!');
					theForm.elements[j].focus()
					theForm.elements[j].select()
					return false;
				}
			}

			//--------validation start for telephone---------------------------------
			if(theForm.elements[j].name == "Phone" )
			{
				if (theForm.elements[j].value.replace(/^\s+/, '').replace(/\s+$/, '')=="")
				{
					alert('Please enter your phone number.');
					theForm.elements[j].focus();
					return false;
				}
				if (checkChars_Phone(theForm.elements[j].value,theForm.elements[j])==false)
				return false;
			}

			//-- email ------------------------------------------------------
			if(theForm.elements[j].name == "Email" ||theForm.elements[j].name == "Confirm_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;
				}
			}
					
			//-- Manufacturer ----------------------------------------------------
			if(theForm.elements[j].name == "Manufacturer"){
	    		var sText=trim(theForm.elements[j].value)
				flen=sText.length;			
				if(flen==0){
					alert('Invalid Input, Manufacturer Name can not be blank!')
					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;
					}
				}
			}
			
			//-- Manufacturer ----------------------------------------------------
			if((theForm.elements[j].name == "ProductNum")||(theForm.elements[j].name == "ProdNumber")){
	    		var sText=trim(theForm.elements[j].value)
				flen=sText.length;			
				if(flen==0){
					alert('Invalid Input, Product Number/Product Description can not be blank!')
					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;
					}
				}
			}		
			
			//-- URL/Addition Information --------------------------------------------
			//if(theForm.elements[j].name== "Url"){
			//	var sText=trim(theForm.elements[j].value)
			//	llen=sText.length;
			//	if(checkSQLServerChar(sText)==false){
				//	alert('Invalid Input, Single/double quotes not allowed!')
				//	theForm.elements[j].focus()
				//	theForm.elements[j].select()						
				//	return false;
				//}
			//}
			
			//-- Compare email and veryfy email -------------------------------------
			if(theForm.elements[j].name == "Confirm_Email")
			{
				if (trim(theForm.Email.value.toLowerCase()) !== trim(theForm.Confirm_Email.value.toLowerCase())){
					alert(msgEmailCompare);
					theForm.Confirm_Email.focus();
					theForm.Confirm_Email.select();
					return false;
				}
			}		
			//------------validation end -----------------------------------	

		}//-------validation end for blank values------------------------------	
		
		//-------validation start to check textarea values------------------------------	
		if(theForm.elements[j].type=="textarea")
		{
	 		var sVal=trim(theForm.elements[j].value)
			flen=sVal.length;			
			if(flen == 0)
			{
				alert("Invalid Input, TextBox should not be blank!");
				theForm.elements[j].focus();
				theForm.elements[j].select();
				return false;			
			}
			else
			{
				if(flen>1024){
					alert('Invalid Input, Total characters should not be more than 1024 characters!')
					theForm.elements[j].focus();
					theForm.elements[j].select();
					return false;
				}
			}
		}	
	}
	
	//Validate Country/State
	if ( (theForm.country.value=="United States") && (theForm.USState_id.value=="SELECT") )
	{
		alert("Please select state");
		theForm.USState_id.focus();
		return false;
	}
	if ( (theForm.country.value=="Canada") && (theForm.CanadaState_id.value=="SELECT") )
	{
		alert("Please select state");
		theForm.CanadaState_id.focus();
		return false;
	}
	//------------End of for loop---------------------------		
	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 for validation of Phone
//---------------------------------------

	function checkChars_Phone(sText,ele)
	 {	
		var validChars = ")(+#[]-.,0123456789";
		var validChars = validChars +" ";
		var cch = sText.length;
		var state=true;	

		if(cch==0)
			{
			alert("Invalid Input, " + ele.name + ' number cannot be blank!');
			ele.focus();
			ele.select();
			return false;
			}
		if(cch<5)
			{
			alert("Invalid Input, " + ele.name + ' number cannot be less than 5 characters!');
			ele.focus();
			ele.select();
			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, " + ele.name + ' has some invalid characters!');
			ele.focus();
			ele.select();
 		    return false;
			}
		else
			return true;

	 } 

// function ends here



//----------End Function------------------
//--------------------------------------

//--------------------------------------
//----------Old Phone Function------------------
//--------------------------------------


//function checkChars_Phone(sText,ele)
//{	
//	var validChars = ")(+-0123456789";
//	var validChars = ")(-.abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+";
//	var validChars = validChars +" ";
//	var cch = sText.length;
//	var state=true;	
//	if (sText=="" || cch>22)
//		{
//		message((msgB+"and"+msgLength),ele)
//		return false;
//		}
//	if(isNaN(sText.charAt(0)))
//		{
//		message(msgPhone,ele)
//		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)
//		{
//		message(msgInvalidChar,ele)
//		return false;
//		}
//	else
//		return true;
//	}

 // function ends here

//----------End Function------------------
//--------------------------------------

	
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 "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 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 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;}
}
