// function to trim leading & trailing spaces
function trim(strText) { 
    // this will get rid of leading spaces 
    while (strText.substring(0,1) == ' ') 
        strText = strText.substring(1, strText.length);

    // this will get rid of trailing spaces 
    while (strText.substring(strText.length-1,strText.length) == ' ')
        strText = strText.substring(0, strText.length-1);

   return strText;
} 

/*
16. isValidEntry

Usage:
	Element  name of the control like frm.password.
	Message  Field Name that we want to display in alert message.

if(!isValidEntry(frm.name, "Name"))
return;
*/
function isValidEntry(element,msg) 
{
	if(element.value.length == 0)
	{
 		return false;
	}
	return true;
} // closing the function isValidEntry()

/*
2. FUNTION SELECTALL CHECK BOXES

Usage:
frm  name of the form
SelectAll(frm);
*/

function SelectAll(frm) {
	 //alert(frm.selectall.checked);
	   if(frm.selectall.checked == true) {
	   
		 for(sel=0;sel<frm.elements.length;sel++) {
		   if((frm.elements[sel].type == "checkbox") && (frm.elements[sel].name != "selectall")) {
			 frm.elements[sel].checked = true;
		   } // if statement
		 } // for loop
	   }
	   else if(frm.selectall.checked == false) {
		
		  for(sel=0;sel<frm.elements.length;sel++) {
			 if((frm.elements[sel].type == "checkbox") && (frm.elements[sel].name != "selectall")) {
			   frm.elements[sel].checked = false;
			 } // if statement
		  } // for loop
	   } // if - else - if condition
	} // closing the function SelectAll()
	
/*

/*
3. SELETC VALIDATION

Usage:
Element  name of the control, like frm.firstname
Message  Field Name that we want to display in alert message.

if(!isValidSelect(frm.country,'Country'))
return;
*/
function isValidSelect(element,msg) 
{
	if(element.value == "0" || element.value == "") 
	{
		return false;
	}
	return true;
}

/*

7. URL VALIDATION

Usage:
Element  name of the control, like frm.url
Message  Field Name that we want to display in alert message.
Required  Set this to yes if the field is mandatory, otherwise no.

	 if(!isValidURL(frm.url, "URL", "yes")) 
	 return;
*/
function isValidURL(element, msg, required)
{
	if(element.value == "")
	{
		var rval = trim(required);
		if (rval.toLowerCase() == "yes" || rval == 1)
		{
			alert("Please enter "+msg);
			element.focus();
			return false;
		}
	}
	if(element.value != "")
	{
		var oRegExp = /[^:]+:\/\/[^:\/]+(:[0-9]+)?\/?.*/;
		if (!oRegExp.test(element.value))
		{
			alert('\r\n The URL you have entered is invalid.\n Please check it for accuracy.');
			element.focus();
			element.select();
			return false;
		}
	}
	return true;
}

/*

4. PHONE NUMBER VALIDATION
Usage: 
Element  name of the control, like frm.phone
Message  Field Name that we want to display in alert message.
Required  Set this to yes if the field is mandatory, otherwise no.

 if(!isValidPhone(frm.phone,'Phone Number','yes'))
 return;
*/
function isValidPhone(element, msg, required)
{	
	var VarPhone = element.value;
	if (VarPhone== "")
	{	
		var rval = trim(required);
		if (rval.toLowerCase() == "yes" || rval == 1)
		{
			return false;
		}
	}
	if (VarPhone != "")
	{
		var Phno;
		Phno=VarPhone;
		var valid = "-0123456789()";
		var hyphencount = 0;
		for (var i=0; i < Phno.length; i++) 
		{
			temp = "" + Phno.substring(i, i+1);
			if (valid.indexOf(temp) == "-1")
			{
				return false;
			}
		}
     } 
	 return true;      
}

/*
 EMAIL ADDRESS VALIDATION

Usage: 
Element  name of the control, like frm.email
Required  Set this to yes if the field is mandatory, otherwise no.

	 if(!isValidEmail(frm.email,'yes'))
	 return;
*/

function isValidEmail(element, required)
{
	var VarEmail = element.value;
	if(VarEmail == "")
	{
		var rval = trim(required);
		if (rval.toLowerCase() == "yes" || rval == 1)
		{
 			return false;
		}
	}	
	if(VarEmail != "")
	{
		var emailStr = VarEmail;
		 
		var emailPat=/^(.+)@(.+)$/
		var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
		var validChars="\[^\\s" + specialChars + "\]"
		var firstChars=validChars
		var quotedUser="(\"[^\"]*\")"
		var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
		var atom="(" + firstChars + validChars + "*" + ")"
		var word="(" + atom + "|" + quotedUser + ")"
		var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
		var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
		var matchArray=emailStr.match(emailPat)
		if (matchArray==null) 
		{
 			 return false;
		}
		var user=matchArray[1]
		var domain=matchArray[2]
		if (user.match(userPat)==null) 
		{
  			return false;
		}
		var IPArray=domain.match(ipDomainPat)
		if (IPArray!=null) 
		{
			for (var i=1;i<=4;i++) 
			{
				if (IPArray[i]>255) 
				{
 					 return false;
				}
			}
		}
		var domainArray=domain.match(domainPat)
		if (domainArray==null) 
		{
 			return false;
		}
		var atomPat=new RegExp(atom,"g");
		var domArr=domain.match(atomPat);
		var len=domArr.length;
		if (domArr[domArr.length-1].length<2 || domArr[domArr.length-1].length>3) 
		{
 		   return false;
		}
		if (domArr[domArr.length-1].length==2 && len<3) 
		{
  			return false;
		}
		if (domArr[domArr.length-1].length==3 && len<2) 
		{
			 var errStr="This address is missing a hostname!";
 			 return false;
		}
	}
	return true;
}

function isValidConfirmPassword(element1,element2) 
{
	if(element1.value != element2.value)
	{
		alert("Confirm Password doesn't match");
		element2.focus();
		return false;
	}
	else
		return true;
} // closing the function PassValidation()


