// 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)
	{
		alert("Please enter the "+ msg);
		element.focus();
		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 == "") 
	{
		alert("Please select "+msg+" from the list");
		element.focus();
		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)
		{
			alert("Please enter "+msg);
			element.focus();
			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")
			{
				alert("Invalid characters in your "+msg+". Please try again.");
				element.focus();
				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)
		{
			alert("Please enter Email Address");
			element.focus();
			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) 
		{
			 alert("Email address seems to be incorrect (check @ and .'s)");
			 element.focus();
			 return false;
		}
		var user=matchArray[1]
		var domain=matchArray[2]
		if (user.match(userPat)==null) 
		{
			alert("The username doesn't seem to be valid.");
			element.focus();
			return false;
		}
		var IPArray=domain.match(ipDomainPat)
		if (IPArray!=null) 
		{
			for (var i=1;i<=4;i++) 
			{
				if (IPArray[i]>255) 
				{
					 alert("Destination IP address is invalid!");
					 element.focus();
					 return false;
				}
			}
		}
		var domainArray=domain.match(domainPat)
		if (domainArray==null) 
		{
			alert("The domain name doesn't seem to be valid.");
			element.focus();
			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) 
		{
		   alert("The address must end in a three-letter domain, or two letter country.");
		   element.focus();
		   return false;
		}
		if (domArr[domArr.length-1].length==2 && len<3) 
		{
			var errStr = "This address ends in two characters, which is a country";
			errStr    += " code.  Country codes must be preceded by ";
			errStr	  += "a hostname and category (like com, co, pub, pu, etc.)";
			alert(errStr);
			element.focus();
			return false;
		}
		if (domArr[domArr.length-1].length==3 && len<2) 
		{
			 var errStr="This address is missing a hostname!";
			 alert(errStr);
			 element.focus();
			 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()
function isValidAlphabet(element, msg, required)
{
	var i=0;
	var ValidData="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz._- ";
	var Data=element.value;
	if(element.value == "")
	{
		var rval = trim(required);
		if (rval.toLowerCase() == "yes" || rval == 1)
		{
			alert("Please enter "+msg);
			element.focus();
			return false;
		}
	}	
	if(element.value != "")
	{
		for(i=0;i<Data.length;i++)
		{
			if(ValidData.indexOf(Data.charAt(i))==-1)
			{
				alert("PLease enter Non-Numeric Data Only");
				element.focus();
				return false;
			}
		}
	}
    return true;
}
/*
17.  FUNCTION SPLCHARACTERS(element)
No speaicl Characters
This function will not accept any special characters.
Valid entries for this function are [a-z][A-Z][0-9][ _ ].
Usage:
	Element  name of the control like frm.password.

if(!noSplChars(frm.name))
return;
*/
function noSplChars(element) 
{
	var alp = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_ ";
	for (var i=0;i<element.value.length;i++)
	{
		temp=element.value.substring(i,i+1);
		if (alp.indexOf(temp)==-1)
		{
			alert("No special characters\r\nValid entries are [a-z][A-Z][0-9][ _ ][ space ]");
			element.focus();
			return false;
		}
	} // closing the for loop
	 return true;
}

/*
2. NUMBER VALIDATION
Usage:
Element  name of the control, like frm.number
Message  Field Name that we want to display in alert message.
Required  Set this to yes if the field is mandatory, otherwise no.

 if(!isValidNumber(frm.num,'Roll Number','yes'))
 return;
*/
function isValidNumber(element, msg, required)
{  
	var VarNumber = element.value;
	if(VarNumber == "")
	{
		var rval = trim(required);
		if (rval.toLowerCase() == "yes" || rval == 1)
		{
			alert("Please enter "+msg);
			element.focus();
			return false;
		}
	}
	if (VarNumber != "")
	{
		var Num;
		Num=VarNumber;
		var valid = "0123456789";
		var hyphencount = 0;
		
		for (var i=0; i < Num.length; i++) 
		{
			temp = "" + Num.substring(i, i+1);
			if (valid.indexOf(temp) == "-1")
			{
			  alert("Invalid characters in your "+msg+".  Please try again.");
			  element.focus();
			  return false;
			}
	   } // end for loop
	   
		if(VarNumber < 1)
		{
			alert(msg+" is not a valid number");
			element.focus();
			return false;
		}
    }   // end if
    return true; 
}  // end function
