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!'; 
function validator(theForm){
    var serial=  trim(theForm.Subscription_Number.value)
    var name= trim(theForm.name.value)
    var email=trim(theForm.email.value)
    var iName=""
    var iValue=""
    var iReplacement=""
    var state

	for(var i=0;i<theForm.elements.length;i++)
	{
		
		//--------validation start for comments---------------------------------
		if(theForm.elements[i].name == "mycomments"){
			var sVal=trim(theForm.elements[i].value)
			flen=sVal.length;			

			if(flen != 0){
		  	    if(checkSQLServerChar(sVal)==false){
					alert('Invalid Input, Single/double quotes not allowed!');
					theForm.elements[i].focus();
					theForm.elements[i].select();
		   		    return false;
		  	    }

				if(flen>1024){
					alert('Invalid Input, Comments cannot be more than 1024 Characters!')
					theForm.elements[i].focus();
					theForm.elements[i].select();
					return false;
				}

				if(flen<5){
					alert("Invalid Input, Total characters in comment should be more than 5 characters!");
					theForm.elements[i].focus();
					theForm.elements[i].select();
					return false;
				}
			}
		}

		if(theForm.elements[i].type=="text"){
			iName=trim(theForm.elements[i].name)
			iValue=trim(theForm.elements[i].value)

			if (iName != "company")	{
				if(checkChars_name_company(iValue)==false){
					if(iName=="fn2")
						iName="folder location"
					else if(iName=="phone")
						iName="Phone Number"
					else if(iName=="name")
						iName="Name"

					alert("Please enter "+iName+"!");
					theForm.elements[i].focus()
					theForm.elements[i].select()
					return false;
				}
			}													
		}
	}

	//-----Validate same value string entered in name, lastname, company then restrict
//	if ( (theForm.name.value==theForm.lastname.value) && (theForm.name.value==theForm.company.value) && (theForm.lastname.value==theForm.company.value) )
//	{
//		alert(InvalidPhoneMail );
//		theForm.lastname.focus();
//		return false;
//	}													

	if ( (trim(serial).length != 20) && (trim(serial).length != 25) && (trim(serial).length != 29))
	{
		alert("Invalid subscription number, Please enter a valid subscription number!");
		theForm.Subscription_Number.focus();
		theForm.Subscription_Number.select();
		return false;
	}
			
	//--------validation start for email--------------------------------------
	var status=checkChars_Email(theForm.email.value);			
	if(status !=true)
	{
	    	alert(status);
		theForm.email.focus();
		theForm.email.select();
        	return false;
	}

	//var offensive = ValidateOffensive(theForm.email.value);
	//if(offensive !=true)
	//{
	//    	alert(InvalidPhoneMail);
	//	theForm.email.focus();
	//	theForm.email.select();
        //	return false;
	//}
	

	state=checkChars_Phone(trim(theForm.phone.value),theForm.phone)
	if (state==false)
		return false

	state = verifymail (theForm,theForm.email.value, theForm.verifyemail.value)
	if (state==false)
		return false

	return true;
}

//------ Compare email and veryfy email----------------------------------------
function verifymail(theForm,email1, email2)
{
	if (trim(email1.toLowerCase()) != trim(email2.toLowerCase()))
	{
		alert("Invalid Input, Please verify your email address and submit again!");
		theForm.verifyemail.focus();
		theForm.verifyemail.select();
		return false;
	}
}


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 to validate Phone characters 
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<6)
	{
		alert('Invalid Input, Phone Number cannot be less than 6 characters!');
		ele.focus();
		return false;
	}
	if (sText=="" || cch>22)
	{
		alert('Invalid Input, Phone 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 entry has some invalid characters!')
		ele.focus();
		return false;
	}
	else
		return true;
} // function ends here


//-- Function to validate email charachers
function checkChars_Email(sText) 
{	
	sText=trim(sText.toUpperCase());
	var inValidChars = "`,~#$%^&*()=|!\\//'?<>;:"
	var x;
    

	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."
	}

	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(".") ==(sText.length-1))
		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 company name
function checkChars_name_company(sText) 
{	
	var ValidChars = /[a-zA-Z0-9]/
	if (ValidChars.test(sText))
		return true;
	else
		return false;
}

function SetDownloadFolder(folder)
	{
	document.Subscription_Number_Universal.folder_name.value=folder;
	}

function setfolder(theFormControl)
	{
//	document.Subscription_Number_Universal.NZFolder.value=theFormControl.name;
	}


function setfoldereqserver(theFormControl)
	{
//	document.Subscription_Number_Universal.NZFolder.value=theFormControl.name;
	}


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;}
}

function popup_validatesno(url)
{

var theForm = document.Subscription_Number_Universal;
var sno = theForm.Subscription_Number.value;

  if(trim(theForm.Subscription_Number.value)=="")
	{
	alert("Please enter your subscription number in the subscription number box and click on 'Check Expiration Date' again.")
	theForm.Subscription_Number.focus();
	}
   else
	window.open(url+"?sno="+sno,'popWin2','resizeable=no,scrollbars=yes,width=600,height=130,top=120,left=200');

}

//-- 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;
}
