



/*
* Name : theValidator
* Purpose : To add client side validations on form fields
*
* The parameters
* 0) theform - The instance of the form , use 'this' rather than name
*
* 1) thecontrols - The name of the controls separated by comma, only add name of controls which you want to be validated.
*
* 2) themessages - The Message i.e if you write Name, then the msg would be 'Name cannot be blank.'
*
* 3) theNumeric - If you want any control to be validated for numeric value, then specify 'Y' 
*    exactly in the same order if the 1st parameter has values Input1,Input2,temp,.. 
*	 if you want to validate the Input2 control for numeric value the ,Y,,....
*	 The input value must be less than or equal to 0.
*
* 4) theEmail - For validating correct form of email address
*
* 5) theURL - For validating correct form of URL i.e. http://www.domain.com/
*
* 6) theConfirmPassword - If the form contains a password and a confirm password field 
*	 then add a 'Y' against the confirm password control, the script will match its value with the
*	 password field and if they doesn't match will return false.
*
* 7) thebadstuff - To check for the special characters, if present then it returns false
*	 It works for three conditions means - 
*
*	 if we specify 1 against the control in the thebadstuff variable e.g. thebadstuff=",,,,1,,,";
*	 then the script will check for these special characters "~`!@#$%^&*()=+|<>?;:/\"\\"
*
*	 if we specify 2 against the control in the thebadstuff variable e.g. thebadstuff=",,,,2,,,";
*	 then the script will check for these special characters "~`!@$%^&*()=+|<>?;:/\"\\" (no hash sign present)
*
*	 if we specify 3 against the control in the thebadstuff variable e.g. thebadstuff=",,,,3,,,";
*	 then the script will check for these special characters "~`!@$%^&* ()=+|<>?;\"" in this the script
*	 checks for the (space as well)
*
* 8) theinteger - Same as numeric except it check that the value entered must be greater than 0
*
* 9) thepercent - It checks for the percentile value which must be between 0 to 100
*
* 10) thesize - If you want to have a maxlength check on any input type then specify the maxlength value
*	  against that control for e.g say we have
*	  thecontrols=text1,text1,textarea1,textarea2;
*	  and we want to have a size control on textareas then we will enter sizes as below
*	  thesize = ,,500,1000;
*	  NOTE: But it has a drawback it lets user enter the number of chars he wants and when user submits
*	  the form then it checks the size which creates problem, you should use function textCounter(field,maxlimit)
*	  instead (specified later).
*
* 11) thedecimal - If you does not want that user should enter decimals in a particular field
*	  then specify 'Y' against that control.
*	  NOTE: Make sure you are using this check for fields in which you are entering numeric values
*	  else it will not allow you enter "." i.e. fullstop 
*
* 12) theimage - This parameter is used to add checks on file uploading control i.e. input type="file"
*	  This javscript check enables the system to check on client side that which type of file is getting
*	  uploaded. 4 types of variations has been implemented till now means 4 types of file type checks has
*	  been implemented. They are as follows:
*
*	  If we want to add a check for simple images then write Y against the controls i.e. It will allow files of types
*	  .gif, .jpg, .jpeg
*	  theimage=,,Y,,Y;
*
*
*	  If we want to add a check for text files then write C against the controls i.e. It will allow files of types
*	  .txt, .doc, .pdf
*	  theimage=,,C,,C;
*
*
*	  If we want to add a check for both images and text files then write YC against the controls i.e.
*	  It will allow files of types  .gif, .jpg, .jpeg and .txt, .doc, .pdf as well
*	  theimage=,,YC,,YC;
*
*
*	  If we want to add a check for movie files then write M against the controls i.e.
*	  It will allow files of types  .mpg, .mpeg, .avi, .mov, .rm, .wmv, .asf
*	  theimage=,,M,,M;
*
* 13) theempty - In this parameter Y against the specified controls will not allow user to leave those
*	  fields empty/blank, it works for textbox, textarea, selectbox, file upload.
*
* 14) themin - If you don't want that a user should enter value less than some specified value then you
*	  must enter that value against that control in the "themin" variable list. Say we are implementing
*	  a check on user's bidding for some product and his bidding must be greater than 100 then you must
*	  specify 100 against that control e.g.
*	  themin=,,100,,,;
*
* 15) themax - If you don't want that a user should enter value greater than some specified value then you
*	  must enter that value against that control in the "themax" variable list. Say we are implementing
*	  a check on user's age that it must not be greater than 120 then you must
*	  specify 120 against that control e.g.
*	  themax=,,120,,,;
*
* 16) thecheckboxlength - You can implement this functionality where you have a group of checkbox/radio
*	  with same name and you can specify the number of the checkboxes or radio buttons and you want that 
*	  user must select any of these checkboxes. Then you can implement it like this e.g.
*	  theControl="vchMemFirstName,vchMemLastName,vchMemEmail,vchMemDoc,chkIndustry[]";
*	  thecheckboxlength=",,,,<?=fnCountRow('iaba_IndustryMaster','1');?>";
*	  OR
*	  thecheckboxlength=",,,,7";
*	  
*
* 17) thephoneno - This is the most commonly used parameter, if you specify 'Y' against any control in this
*	  parameter then the script will allow user to enter phone numbers in following formats:
*	  xxx-xxx-xxxx , (xxx)xxx-xxxx , xxxxxxxxxx (10 digited Mobile no), xxxxxx (6 digits no), xxxxxxx (7 digits no)
*	  and xxxxxxxx (8 digits no)

*/

//implementation
/*
function check(theform)
{
	thecontrols="vchAttorneyName,vchFirmName,vchAddress1,vchAddress2,vchStateName,vchCity,vchZipCode,vchPhone,vchFax,vchEmailAddress,intFormatID";
	themessages="Attorney name,Firm name,Address,Address,State,City,Zipcode,Phone,Fax,Email address,Format";
	theNumeric=",,,,,,,,,,";
	theEmail=",,,,,,,,,Y,";
	theURL=","
	theConfirmPassword=",";
	thebadstuff="1,1,,,,,,,,,";
	theinteger=",,,,,,,,,,";
	thepercent=",";
	thesize=",,,,,,,,,,";
	thedecimal=",,,,,,,,,,";
	theimage=",";
	theempty="Y,Y,Y,,Y,Y,Y,Y,,Y,Y";
	themin=",";
	themax=",";
	thecheckboxlength=",,,,,,,,,,";
	thephoneno=",,,,,,,Y,Y,,";
	
	if(!theValidator(theForm,theControl,theMessage,theNumeric,theEMail,theURL,theConfirmPassword,thebadstuff,theinteger,thepercent,thesize,thedecimal,theimage,theempty,themin,themax,thecheckboxlength,thephoneno))
		return false;

	return true;
}
*/

//	*************************************  theValidator **********************************************************

function theValidator(theForm,theControl,theMessage,theNumeric,theEMail,theURL,theConfirmPassword,thebadstuff,theinteger,thepercent,thesize,thedecimal,theimage,theempty,themin,themax,thecheckboxlength,thephoneno)
{

	//Split the controls + Messages + Numerics by comma ,

	var theMessages = new Array();
	var theNumerics = new Array();
	var theEMails = new Array();
	var theURLs = new Array();
	var theConfirmPasswords = new Array();

	theControls=theControl.split(",");
	theMessages=theMessage.split(",");
	theNumerics=theNumeric.split(",");
	theEMails=theEMail.split(",");
	theURLs=theURL.split(",");
	thebadstuffs=thebadstuff.split(",");
	theintegers=theinteger.split(",");
	thepercents=thepercent.split(",");
	thesizes=thesize.split(",");
	thedecimals=thedecimal.split(",");
	theimages=theimage.split(",");
	theemptys=theempty.split(",");
	themins=themin.split(",");
	themaxs=themax.split(",");
	thecheckboxlengths=thecheckboxlength.split(",");
	theConfirmPasswords=theConfirmPassword.split(",");
	thephonenos=thephoneno.split(",");

	for (var i=0; i < theForm.elements.length; i++)
	{
		for(var counter=0;counter < theControls.length; counter++)
		{
			if (theForm.elements[i].name == theControls[counter])
			{
				if (theForm.elements[i].type == 'file')
				{
					//Blank value check
					if (trim(theForm.elements[i].value) == '' && theemptys[counter] == "Y" )
					{
						alert("Please enter the "+theMessages[counter].toLowerCase()+".");
						theForm.elements[i].focus();
						return false;	
					}
					else
					{
						if (theimages[counter] == "Y")
						{
							var s=theForm.elements[i].value;
							if (s!="")
							{
								array=s.split("\\");
								len=array.length;
								filename=array[len-1];
								array1=filename.split(".");
								len1 = array1.length;
								len1 = len1-1;
								if (array1.length == 1)
								{
									alert("Files with extensions .gif, .jpg, .jpeg can only be uploaded.");
									theForm.elements[i].focus();
									return false;									
								}
								if(array1[len1].toLowerCase()!="jpg" && array1[len1].toLowerCase()!="jpeg" && array1[len1].toLowerCase()!="gif")
								{
									alert("Files with extensions .gif, .jpg, .jpeg can only be uploaded.");
									theForm.elements[i].focus();
									return false;
								}
							}
						}
						if (theimages[counter] == "C")
						{
							var s=theForm.elements[i].value;
							if (s!="")
							{
								array=s.split("\\");
								len=array.length;
								filename=array[len-1];
								array1=filename.split(".");
								len1 = array1.length;
								len1 = len1-1;
								if (array1.length == 1)
								{
									alert("Files with extensions .txt, .doc, .pdf can only be uploaded.");
									theForm.elements[i].focus();
									return false;									
								}
								if(array1[len1].toLowerCase()!="txt" && array1[len1].toLowerCase()!="doc" && array1[len1].toLowerCase()!="pdf")
								{
									alert("Files with extensions .txt, .doc, .pdf can only be uploaded.");
									theForm.elements[i].focus();
									return false;
								}
							}
						}
						if (theimages[counter] == "YC")
						{
							var s=theForm.elements[i].value;
							if (s!="")
							{
								array=s.split("\\");
								len=array.length;
								filename=array[len-1];
								array1=filename.split(".");
								len1 = array1.length;
								len1 = len1-1;
								if (array1.length == 1)
								{
									alert("Files with extensions .gif, .jpg, .jpeg, .txt, .doc, .pdf can only be uploaded.");
									theForm.elements[i].focus();
									return false;									
								}
								if(array1[len1].toLowerCase()!="jpg" && array1[len1].toLowerCase()!="jpeg" && array1[len1].toLowerCase()!="gif" && array1[len1].toLowerCase()!="txt" && array1[len1].toLowerCase()!="doc" && array1[len1].toLowerCase()!="pdf")
								{
									alert("Files with extensions .gif, .jpg, .jpeg, .txt, .doc, .pdf can only be uploaded.");
									theForm.elements[i].focus();
									return false;
								}
							}
						}
						if (theimages[counter] == "M")
						{
							var s=theForm.elements[i].value;
							if (s!="")
							{
								array=s.split("\\");
								len=array.length;
								filename=array[len-1];
								array1=filename.split(".");
								len1 = array1.length;
								len1 = len1-1;
								if (array1.length == 1)
								{
									alert("Files with extensions .mpg, .mpeg, .avi, .mov, .rm, .wmv, .asf can only be uploaded.");
									theForm.elements[i].focus();
									return false;									
								}
								if(array1[len1].toLowerCase()!="mpg" && array1[len1].toLowerCase()!="mpeg" && array1[len1].toLowerCase()!="avi" && array1[len1].toLowerCase()!="mov" && array1[len1].toLowerCase()!="rm" && array1[len1].toLowerCase()!="wmv" && array1[len1].toLowerCase()!="asf")
								{
									alert("Files with extensions .mpg, .mpeg, .avi, .mov, .rm, .wmv, .asf can only be uploaded.");
									theForm.elements[i].focus();
									return false;
								}
							}
						}

					}

				}

				if (theForm.elements[i].type == 'text')
				{
					//Blank value check
					if (trim(theForm.elements[i].value) == '' && theemptys[counter] == "Y")
					{
						alert("Please enter the "+theMessages[counter].toLowerCase()+".");
						theForm.elements[i].focus();
						return false;	
					}

					// Numeric value check
					if (trim(theForm.elements[i].value) != '' && theNumerics[counter] == "Y")
					{
						if(isNaN(trim(theForm.elements[i].value)))
						{
							alert("Please enter numeric values for "+theMessages[counter].toLowerCase()+".");
							theForm.elements[i].focus();
							return false;	
						}
						if(trim(theForm.elements[i].value)<= 0 )
						{
							alert(""+theMessages[counter].toLowerCase()+" should be greater than  0 (zero).");
							theForm.elements[i].focus();
							return false;	
						}					
					}

					if  (trim(theForm.elements[i].value) != '' && theintegers[counter] == "Y")
					{
							if(isNaN(trim(theForm.elements[i].value)))
								{
									alert("Please enter numeric values for "+theMessages[counter].toLowerCase()+".");
									theForm.elements[i].focus();
									return false;	
								}
							if(parseInt(trim(theForm.elements[i].value))< 0 )
								{
								alert("Please enter "+theMessages[counter].toLowerCase()+" greater than or equal to 0 (zero).");
								theForm.elements[i].focus();
								return false;
								}
					}
					
					if ((trim(theForm.elements[i].value) != '' ) && (thebadstuffs[counter] != ""))
					{
							if (thebadstuffs[counter]==1)
								badStuff = ".`~#!@$%^&* '()= +|<>?;:/\"\\[]{},";//for page url only -_
							else if(thebadstuffs[counter]==2)
								badStuff="~`!@$%^&* ()=+|<>?;.':/\"\\-_[]{},";//for txt sequenceonly #
							else if(thebadstuffs[counter]==3)//price sequence only deci
								badStuff="~`!@$%^&* ()=+|<>?;':/\"\\-_[]{},";
							else if(thebadstuffs[counter]==4)
								badStuff=".~`#!@$%^&* ()=+|<>?;':/\"\\-_[]{},";
							else if(thebadstuffs[counter]==5)//productname
								badStuff=".`-~#!@$%^&*()=+|<>?;:/\"\\[]{},";
							else if(thebadstuffs[counter]==6)//User Name only under score come
								badStuff=".`-' ~#!@$%^&*()=+|<>?;:/\"\\[]{},";
							else if(thebadstuffs[counter]==7)//First/Last Name only ' come
								badStuff=".`-~#!@$%^&*_()=+|<>?;:/\"\\[]{},";
							else

								badStuff='"';

							chkstring = trim(theForm.elements[i].value);
							for (x=0; x<badStuff.length; x++)
							{
								badCheck = badStuff.charAt(x);
								if (chkstring.indexOf(badCheck,0) != -1)
								{							
									if(badStuff=='"')
										alert("Please replace double quotes with single quotes for "+theMessages[counter].toLowerCase()+".");
									else
									{
										abc=theMessages[counter].replace(theMessages[counter],theMessages[counter].charAt(0).toUpperCase());
										alert(abc+theMessages[counter].substring(1,theMessages[counter].length)+" can not have special characters.");
									}
									theForm.elements[i].focus();
									return false;
								}
							}
					}

					if (trim(theForm.elements[i].value) != '' && thepercents[counter] == 'Y')
					{			
						if (parseInt(theForm.elements[i].value)<0 || parseInt(theForm.elements[i].value)>100)
						{							
							alert(theMessages[counter] +" should have value between 0 to 100.");
							theForm.elements[i].focus();
							return false;
						}
					}

					
					if (trim(theForm.elements[i].value) != '' && thesizes[counter] != "")
					{
								if (trim(theForm.elements[i].value).length>thesizes[counter])
								{							
									alert("Maximum characters allowed are "+ thesizes[counter] +". Entered are "+ theForm.elements[i].value.length +" characters.");	
									theForm.elements[i].focus();
									return false;
								}
					}
					
					if (trim(theForm.elements[i].value) != '' && thedecimals[counter] != "")
					{
								var str=theForm.elements[i].value;
									 if (str.indexOf('.')!=-1)
										{
										   alert("No decimals allowed for "+theMessages[counter].toLowerCase()+".");
										    theForm.elements[i].focus();
											return false;
										}
					}

					if (trim(theForm.elements[i].value) != '' && thephonenos[counter] != "")
					{
						if(thephonenos[counter]=='Y')
						{
							var regexp=/^(\d{2}-\d{3}-\d{3}-\d{4}|\d{3}-\d{3}-\d{4}|\d{10}|\(\d{2}\)\d{3}-\d{3}-\d{4}|\(\d{3}\)\d{3}-\d{4}|\d{6}|\d{7}|\d{8})$/;
							x = trim(theForm.elements[i].value);
							if(!(regexp.test(x)))
							{
								var msg = "";
								msg = "Please enter the correct "+theMessages[counter].toLowerCase();
								msg = msg +  "\n The correct forms are : ";
								msg = msg + "\n xxx-xxx-xxxx";
								msg = msg + "\n (xxx)xxx-xxxx";
								msg = msg + "\n xx-xxx-xxx-xxxx";
								msg = msg + "\n (xx)xxx-xxx-xxxx";
								msg = msg + "\n xxxxxxxxxx (10 digited Mobile no)";
								msg = msg + "\n xxxxxxxxxxxx (12 digited Mobile no)";
								msg = msg + "\n xxxxxx (6 digits no)";
								msg = msg + "\n xxxxxxx (7 digits no)";
								msg = msg + "\n xxxxxxxx (8 digits no)";

								alert(msg);
								theForm.elements[i].focus();
								return false;
							}
						}
						
						//code for mobile number check
						else if(thephonenos[counter]=='M')
						{
							/*var regexp=/^(\d{12})$/;
							x = trim(theForm.elements[i].value);
							if(!(regexp.test(x)))
							{
								var msg = "";
								msg = "Please enter the 12 Digit "+theMessages[counter].toLowerCase();
								alert(msg);
								theForm.elements[i].focus();
								return false;
							}*/
						}
					}
					
					
					
					
					
					// Email value check
					if (trim(theForm.elements[i].value) != '' && theEMails[counter] == "Y")
					{
						if(!emailcheck(theForm.elements[i],"Please enter valid email address. eg: abc@xyz.com"))  return false;
					}
					// URL value check
					if (trim(theForm.elements[i].value) != '' && theURLs[counter] == "Y")
					{
						if(!validateURL(trim(theForm.elements[i].value)))
						{
							alert("Please enter valid url. \n http://www.domain.com");
							theForm.elements[i].focus();
							return false;	
						}
					}
					
					if (trim(theForm.elements[i].value) !='' && themins[counter]!='' && themaxs[counter]!='')
					{
						if(parseInt(theForm.elements[i].value)<themins[counter] || parseInt(theForm.elements[i].value)>themaxs[counter])
						{
							alert("Please enter value for "+theMessages[counter].toLowerCase()+" between "+themins[counter]+" and "+themaxs[counter]+".");
							theForm.elements[i].focus();
							return false;
						}
					}
					if (trim(theForm.elements[i].value) !='' && themins[counter]!='' && themaxs[counter]=='')
					{
						if((theForm.elements[i].value.length)<themins[counter])
						{
							alert("Please enter valid "+theMessages[counter].toLowerCase()+".");
							theForm.elements[i].focus();
							return false;
						}
					}
					if (trim(theForm.elements[i].value) !='' && themaxs[counter]!='')
					{
						if( parseInt(theForm.elements[i].value)>themaxs[counter])
						{
							alert("Please enter value for "+theMessages[counter].toLowerCase()+" less than "+themaxs[counter]+".");
							theForm.elements[i].focus();
							return false;
						}
					}
				}
				// Password Validation
				if (theForm.elements[i].type == 'password' && theConfirmPasswords[counter]!="Y" && theemptys[counter] == "Y")
				{
					//Blank value check
					if (trim(theForm.elements[i].value) == '')
					{
						alert("Please enter the "+theMessages[counter].toLowerCase()+".");
						theForm.elements[i].focus();
						return false;	
					}
					

						if(theForm.elements[i].value.length > 14)
					{
						alert("Password cannot have more than 14 characters.");	
						theForm.elements[i].focus();
						return false;
					}


					/*	if(theForm.elements[i].value.length < 6)
					{
						alert("Password cannot be less than 6 characters.");	
						theForm.elements[i].focus();
						return false;
					}*/


				}	

				// Confirm Password Validation
				if (theForm.elements[i].type=='password' && theConfirmPasswords[counter]=="Y")
				{
					//Blank value check
					if (trim(theForm.elements[i].value) == '')
					{
						alert("Please retype the "+theMessages[counter].toLowerCase()+".");
						theForm.elements[i].focus();
						return false;	
					}
					

						if(theForm.elements[i].value.length > 14)
					{
						alert("Password cannot have more than 14 characters.");	
						theForm.elements[i].focus();
						return false;
					}


					/*	if(theForm.elements[i].value.length < 6)
					{
						alert("Password cannot be less than 6 characters.");	
						theForm.elements[i].focus();
						return false;
					}*/

					if (theForm.elements[i].value!=theForm.elements[i-1].value)
					{
						alert("Passwords do not match.");
						theForm.elements[i].focus();
						return false;	
					}

				}	
				
			//Select box validation
				if ((theForm.elements[i].type == "select-one") || (theForm.elements[i].type == "select-multiple") )
				{
					var selIndex,selValue;
					selIndex=theForm.elements[i].selectedIndex;
					var theObject=theForm.elements[i];
					selValue = theObject[selIndex].value;
					if( trim(selValue) == "" || trim(selValue) == "0")
					{
						alert("Please select the " + theMessages[counter].toLowerCase() + ".");
						theForm.elements[i].focus();
						return false;	
					}
				}
			//Check box validation

				if (theForm.elements[i].type == "checkbox" && theemptys[counter] == "Y" )
				{ 
					var flagchecked="0";	
					if (parseInt(thecheckboxlengths[counter]) > 1)
					{				
						for (j=i;j<i+parseInt(thecheckboxlengths[counter]);j++)

						{	
							checkbox1=eval(theForm.elements[j]);				
							if(checkbox1.checked==true)
							{	
								flagchecked="1";
								break;
							}
						}
					}
					else
					{
						if(theForm.checkboxname.checked==true)
						{	
							flagchecked="1";
						}				
					}
					if (flagchecked=="0")
					{
						alert("Please select the " + theMessages[counter].toLowerCase() + ".");
						theForm.elements[i].focus();
						return false;	
					}
					i=i+parseInt(thecheckboxlengths[counter])-1;
				}
			
			// Text Area Validation
				if (theForm.elements[i].type == "textarea")
				{
					var txtAreaValue;
					txtAreaValue=theForm.elements[i].value;

					if( trim(txtAreaValue) == "" && theemptys[counter] == "Y")
					{
						alert("Please enter the "+theMessages[counter].toLowerCase()+".");
						theForm.elements[i].focus();
						return false;	
					}
					if (trim(theForm.elements[i].value) != '' && thesizes[counter] != "")
					{
						if (trim(theForm.elements[i].value).length>thesizes[counter])
						{							
							alert("Maximum characters allowed are "+ thesizes[counter] +". Entered are "+ theForm.elements[i].value.length +" characters.");	
							theForm.elements[i].focus();
							return false;
						}
					}
				}
			}
		}
	}
	
	return true;
}



//	************************************	End theValidator *****************************************************

/*
 * Name : checkUncheckAll
 * Purpose : This function will Select/Deselect the list of all checkboxes whose name has been passed
 *	
 *	The parameters
 *	1)	theElement - The Select All named checkbox
 *	2)	checkBoxName - The checbox group name on which this function needs to be performed
 *
 */
function checkUncheckAll(theElement,checkBoxName)
{
	var theForm = theElement.form, z = 0;
	for(var i=0;i<theForm.elements.length;i++)
	{
		if(theForm.elements[i].type=='checkbox' && theForm.elements[i].name == checkBoxName)
			theForm.elements[i].checked = theElement.checked;
	}
}

//	*************************************************************************************************


//	Use to remove the unwanted spaces on the left side of the string passed as parameter 
function ltrim ( s )
{
	return s.replace( /^\s*/, "" )
}

//	Use to remove the unwanted spaces on the right side of the string passed as parameter
function rtrim ( s )
{
	return s.replace( /\s*$/, "" );
}

//	Use to remove the unwanted spaces on the left and right side of the string passed as parameter
function trim(str)
{
//	return rtrim(ltrim(str));
	return( (""+str).replace(/^\s*([\s\S]*\S+)\s*$|^\s*$/,'$1') );
}
//**************************************************************

/*Function to validate email address
 * The parameters
 * 1)	object - The element on which validation is to be implemented
 * 2)	str - The message to be shown if email addres is not valid
 */
/*function emailcheck(object,str)
{
var email=object.value;
var matcharray=email.match(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) 
	if(matcharray==null){
	alert(str)
	 	object.focus();
		object.select();
	return false;
	}
	else return true
}*/
function emailcheck(object,str)
{
var email=object.value;
var matcharray=email.match(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) 
 if(matcharray==null){
 alert(str);
   object.focus();
  object.select();
 return false;
 }
 //var strEmail=new String(email);
 var emailLen=email.length;
 var extPos=email.lastIndexOf(".",emailLen);
 var emailPos=email.indexOf("@",0);
 var extStr=email.substr(extPos+1,emailLen);
 var emailStr=email.substr(0,emailPos);
 
 if(!isNaN(emailStr) || !isNaN(extStr))
 {
  alert(str);
  object.focus();
  object.select();
  return false; 
 }
 

 return true;
}
 
//**************************************************************

// Function to validate URL address for the proper syntax
// The Parameter 
// myobject - The element object on which validation is to be implemented
function validateURL(myobject)
{
	var re;
	var checkend=/\w\.[a-zA-Z]{2,3}/;
	url=myobject;
	
	if(url.search(checkend) == -1)
	{
		return false;
	}
	return true;
}
//**************************************************************

/* Function usually used to open pages in popups
 *	The parameters
 *	1) theURL - Path and name of the file to be open
 *	2) winName - Window name
 *	3) features - e.g features="scrollbars=yes,resizable=yes,width=500,height=300,left=0,top=0";
 */
function MM_openBrWindow(theURL,winName,features) { //v2.0

  window.open(theURL,winName,features);
}
//**************************************************************

/*	Function to validate the maximum length of characters in a field
 *	The Parameters
 *	1) field - The field's object of the form
 *	2) maxlen - The restricted length
 *	3) str - Field Label to be shown with error msg
 */
function validate_max(field, maxlen, str)
{	
	if (field.value.length > maxlen)
	{
		alert(str+" cannot have more than "+maxlen+" characters.");	
		field.focus();
		return  false;
	}
	return true;
}
//**************************************************************

/*	Function to validate the minimum length of characters in a field
 *	The Parameters
 *	1) field - The field's object of the form
 *	2) minlen - The restricted length
 *	3) str - Field Label to be shown with error msg
 */
function validate_min_len (field, minlen, str)
{	
	if (field.value.length < minlen)
	{

		alert(str+" cannot be less than "+minlen+" characters.");	
		field.focus();
		return  false;
	}
	return true;
}
//**************************************************************

/*	Function to validate the type of file to be uploaded
 *	Here it is for image files, can be modified according to the requirement or use function checkform for this
 *	The Parameters
 *	1) object - The file type object of the form
 *	2) str - Field Label to be shown with error msg
 */
function fileextension(object,str)
{
	var s=object.value;
	if (s!="")
	{
		array=s.split("\\");
		len=array.length;
		filename=array[len-1];
		array1=filename.split(".");
		if(array1[1]!="jpg" && array1[1]!="jpeg" && array1[1]!="gif")
		{
			alert(str);
			object.focus();
			return false;
		}
	}
	
	return true;
}
//**************************************************************


/*	Function to check whether the entered date is valid or not
 *	This function is made for the instance where we need to check the date values in two separate
 *	parts like Date From and Date To (in form of three select boxes mm,dd,yyyy). And hence there is function
 *	to compare those two dates as well. But If there is only one select date form element then you can modify
 *	this funcation by removing the last three parameters in the function and the part where 2nd date is 
 *	getting calculated.
 *	The Parameters
 *	1) theform - The form type object
 *	2) dd1 - Value of the date select box
 *	3) mm1 - Value of the month select box
 *	3) yy1 - Value of the year select box
 *	3) dd2 - Value of the second date select box
 *	3) mm2 - Value of the second month select box
 *	3) yy2 - Value of the second year select box
    */

	// for single
function chkdate1(theform,dd1,mm1,yy1)
{
	var myDayStr = dd1;
	var myMonthStr = mm1;
	var myYearStr = yy1;
	var MonthStr;
	switch(myMonthStr)
	{
		case '1':		MonthStr='Jan';
					break;
		case '2':		MonthStr='Feb';
					break;
		case '3':		MonthStr='Mar';
					break;
		case '4':		MonthStr='Apr';
					break;
		case '5':		MonthStr='May';
					break;
		case '6':		MonthStr='Jun';
					break;
		case '7':		MonthStr='Jul';
					break;
		case '8':		MonthStr='Aug';
					break;
		case '9':		MonthStr='Sep';
					break;
		case '10':		MonthStr='Oct';
					break;
		case '11':		MonthStr='Nov';
					break;
		case '12':		MonthStr='Dec';
					break;
		default:		MonthStr='';
					break;
	}			
	
	var myDateStr = myDayStr + ' ' + MonthStr + ' ' + myYearStr;
	
	/* Using form values, create a new date object
	which looks like "Wed Jan 1 00:00:00 EST 1975". */
	var myDate = new Date( myDateStr );

	// Convert the date to a string so we can parse it.
	var myDate_string = myDate.toString();

	/* Split the string at every space and put the values into an array so,
	using the previous example, the first element in the array is "Wed", the
	second element is "Jan", the third element is "1", etc. */
	var myDate_array = myDate_string.split( ' ' );

	if ( myDate_array[1] != MonthStr ) {
	  alert( 'I\'m sorry, but "' + myDateStr + '" is NOT a valid date.' );
		return false;
	}
	
}








/*function for 2 select date from to */

function chkdate(theform,dd1,mm1,yy1,dd2,mm2,yy2)
{ 
	var myDayStr = dd1;
	var myMonthStr = mm1;
	var myYearStr = yy1;
	var MonthStr;
	switch(myMonthStr)
	{
		case '1':		MonthStr='Jan';
					break;
		case '2':		MonthStr='Feb';
					break;
		case '3':		MonthStr='Mar';
					break;
		case '4':		MonthStr='Apr';
					break;
		case '5':		MonthStr='May';
					break;
		case '6':		MonthStr='Jun';
					break;
		case '7':		MonthStr='Jul';
					break;
		case '8':		MonthStr='Aug';
					break;
		case '9':		MonthStr='Sep';
					break;
		case '10':		MonthStr='Oct';
					break;
		case '11':		MonthStr='Nov';
					break;
		case '12':		MonthStr='Dec';
					break;
		default:		MonthStr='';
					break;
	}			
	
	var myDateStr = myDayStr + ' ' + MonthStr + ' ' + myYearStr;
	
	/* Using form values, create a new date object
	which looks like "Wed Jan 1 00:00:00 EST 1975". */
	var myDate = new Date( myDateStr );

	// Convert the date to a string so we can parse it.
	var myDate_string = myDate.toString();

	/* Split the string at every space and put the values into an array so,
	using the previous example, the first element in the array is "Wed", the
	second element is "Jan", the third element is "1", etc. */
	var myDate_array = myDate_string.split( ' ' );

	if ( myDate_array[1] != MonthStr ) {
	  alert( 'I\'m sorry, but "' + myDateStr + '" is NOT a valid date.' );
		return false;
	}


	var myDayStr2 = dd2;
	var myMonthStr2 = mm2;
	var myYearStr2 = yy2;
	var MonthStr2;
	switch(myMonthStr2)
	{
		case '1':		MonthStr2='Jan';
					break;
		case '2':		MonthStr2='Feb';
					break;
		case '3':		MonthStr2='Mar';
					break;
		case '4':		MonthStr2='Apr';
					break;
		case '5':		MonthStr2='May';
					break;
		case '6':		MonthStr2='Jun';
					break;
		case '7':		MonthStr2='Jul';
					break;
		case '8':		MonthStr2='Aug';
					break;
		case '9':		MonthStr2='Sep';
					break;
		case '10':		MonthStr2='Oct';
					break;
		case '11':		MonthStr2='Nov';
					break;
		case '12':		MonthStr2='Dec';
					break;
		default:		MonthStr='';
					break;
	}			
	
	var myDateStr2 = myDayStr2 + ' ' + MonthStr2 + ' ' + myYearStr2;

	/* Using form values, create a new date object
	which looks like "Wed Jan 1 00:00:00 EST 1975". */
	var myDate2 = new Date( myDateStr2 );

	// Convert the date to a string so we can parse it.
	var myDate_string2 = myDate2.toString();

	/* Split the string at every space and put the values into an array so,
	using the previous example, the first element in the array is "Wed", the
	second element is "Jan", the third element is "1", etc. */
	var myDate_array2 = myDate_string2.split( ' ' );

	if ( myDate_array2[1] != MonthStr2 ) {
	  alert( 'I\'m sorry, but "' + myDateStr2 + '" is NOT a valid date.' );
		return false;
	}

	if(myDayStr=='0' || MonthStr=='undefined' || myYearStr=='0' || myDayStr2=='0' || MonthStr2=='undefined' || myYearStr2=='0')
	{
		alert('Please select valid date range before pressing GO button.');
		return false;
	}
	
	return comparedate(theform);
}
//function to compare both dates enetered 
function comparedate(theform,dd1,mm1,yy1,dd2,mm2,yy2)
{
	var fromdate=new Date( yy1,  (mm1-1), dd1);
	var todate=new Date(yy2, (mm2-1), dd1);		
	if(fromdate > todate)
	{
		alert("Please enter valid date range.");
		return false;
	}
	return true;
}
//**************************************************************

/*
* This function will not return until (at least)
* the specified number of milliseconds have passed.
* It does a busy-wait loop.
*/
function pause(numberMillis)
{
	var now = new Date();
	var exitTime = now.getTime() + numberMillis;
	while (true)
	{
		now = new Date();
		if (now.getTime() > exitTime)
			return;
	}
}
//**************************************************************

/*	Function to restrict the characters in a field (commonly used for textareas)
 *	The Parameters
 *	1) field - The element as object
 *	2) maxlimit - Size in number to restrict e.g 500
 */
function textCounter(field,maxlimit) 
{
	if (field.value.length > maxlimit) 
		field.value = field.value.substring(0, maxlimit);
}
//**************************************************************

/*	Function to add a url to favorites
 *	The Parameters
 *	1) url - The URL of the site
 *	2) title - The name by which it is to be stored in the browser
 */
function addfav(url, title)
{

	if ((navigator.appName == "Microsoft Internet Explorer") && (parseInt(navigator.appVersion) >= 4))
	{
		window.external.AddFavorite(url,title);
	}
	else
	{
		var msg = "Don't forget to bookmark us!";
		
		if(navigator.appName == "Netscape")
			msg += "  (CTRL-D)";
		
		document.write(msg);
	}
}
//**************************************************************

/*	Function to check whether any checkbox is selected (mainly to be used where user can select and delete
 *	or deactivate multiple records by selecting the checkboxes placed in front of those records)
 *	The Parameters
 *	1) theform - The form's object
 *	2) startElement - The sequence number at which the first checkbox appears in the form
 *	3) endElement - If no other such group/checkbox exists in form then it will be equal to 0
 *	4) message - delete or deactivate or activate
 *	returns true or false
 */
function confirmSelection(theform,startElement,endElement,message)
{alert ("java");
	var flag=0;
	for(var i=startElement;i<(theform.elements.length-endElement);i+=1)		  
	{
		
		if(theform.elements[i].checked==true)
		{
			 flag=1;
			 if (confirm("Are you sure you want to "+message+" the selected record(s)?"))
				return true;
			else
				return false;
		}	
	}	
	if(flag==0)	
	{
		alert("Please select the checkbox before pressing the "+message+" button.");
		return false;
	}
}
//**************************************************************

//**************************************************************

/*	Function to check whether any checkbox is selected (mainly to be used where user can select and delete
 *	or deactivate multiple records by selecting the checkboxes placed in front of those records)
 *	The Parameters
 *	1) theform - The form's object
 *	2) startElement - The sequence number at which the first checkbox appears in the form
 *	3) endElement - If no other such group/checkbox exists in form then it will be equal to 0
 *	4) message - delete or deactivate or activate
 *	returns true or false
 */
function confirmSequence(theform,startElement,endElement,message)
{
	var flag=0;
	for(var i=startElement;i<(theform.elements.length-endElement);i+=1)		  
	{
		
		if(theform.elements[i].value!='')
		{
			 flag=1;
			 if (confirm("Are you sure you want to "+message+" numbers?"))
				return true;
			else
				return false;
		}	
	}	
	if(flag==0)	
	{
		alert("Please fill sequence number before pressing "+message+" button.");
		return false;
	}
}
//**************************************************************


/*'--- ---------------------------------------------------------
'Purpose		:	This will check the valid date
'Input Parameter							
	'	theform	:	Name of the form
		dd		:	Name of the day form element
		mm		:	Name of the month form element
		yy		:	Name of the year form element
'-------------------------------------------------------------*/
function CheckDate(theform,dd,mm,yy)
{ 

	day1=parseInt(eval("theform."+dd+".options[theform."+dd+".selectedIndex].value"));
	month1=eval("theform."+mm+".options[theform."+mm+".selectedIndex].value");
	month1=parseInt(month1-1);
	year1=parseInt(eval("theform."+yy+".options[theform."+yy+".selectedIndex].value"));
	var test = new Date(year1,month1,day1);
	if (!((yearcheck(test.getYear()) == year1) && (month1 == test.getMonth()) && (day1 == test.getDate())) )
		{
			alert("Invalid date");
			return false;
		}
	return true;	
 }
 //**************************************************************

/*
 * Name : empty
 * Purpose : This function checks whether the given form element contains any value or is blank/empy
 *	
 *	The parameters
 *	1)	object - The object of the form element e.g for a textbox, document.form.textboxname
 *	2)	str - The error message to be displayed on returning blank value
 *
 */
function empty(object,str)
{
	var value=trim(object.value);
	if (value=="")
	 {
		alert(str);
		object.focus();
		return false;
	 }
	str1=trim(object.value);
	x=0;
	for(var i=0;i<str1.length;i++)
	{	
		if (str1.charAt(i)!=" ") { x=1 }
	
	}
	if (x==0)
	{
		alert(str);
		object.value="";
		object.focus();
		return false;
	}
	str1=trim(object.value);
	x=0;
	y=0;
	len=str1.length;
	for(var i=0;i<str1.length;i++)
	{	
		if ((str1.charAt(i)=="\r") && (str1.charAt(i+1)=="\n")) { y++; }
	
	}
	if (y==(len/2))
	{
		alert(str);
		object.value="";
		object.focus();
		return false;
	}
	
	 return true;
}
//**************************************************************

/*
 *	Name : my_group
 *	Purpose : (Modified form of previously used functions add_group and remove_group as those functions sometimes do not send the selected values)
 *	This function can be used where we need to switch values to and fro between two list boxes. This function will be used on
 *	the click event of the 'ADD' and 'REMOVE' buttons given with the list boxes. For e.g
 *	On ADD button <a href="javascript:my_group('product_id_src','product_id_dest');"><img src="images/arrowr.gif" border="0" ></a>
 *	On remove button <a href="javascript:my_group('product_id_dest','product_id_src');"><img src="images/arrowl.gif" border="0" ></a>
 *	The parameters
 *	1)	srcID - The Select box name
 *	2)	destID - The destingation select box name
 *
 */
function my_group(srcID,destID)
{
	var ns4 = (window.document.layers);
	var ie4 = (window.document.all && !window.document.getElementById);
	var ie5 = (window.document.all && window.document.getElementById);
	var ns6 = (!window.document.all && window.document.getElementById);
	if(ns4)
	{
		objSrc = window.document.layers[srcID];
		objDest = window.document.layers[destID];
	}
	// Explorer 4
	else if(ie4)
	{
		objSrc = window.document.all[srcID];
		objDest= window.document.all[destID];
	}
	// W3C - Explorer 5+ and Netscape 6+
	else if(ie5 || ns6)
	{

		objSrc = window.document.getElementById(srcID);
		objDest = window.document.getElementById(destID);
	}
	var doc=objSrc.document;
	var theform=objSrc.form;
	/*for(var j=0;j<objDest.options.length;j++)
	{
		objDest.options[j]=null;
	}*/
	var srcLeng=objSrc.options.length
	var remEle=new Array();
	var count=0;
	for(var j=0;j<srcLeng;j++)
	{
		if(objSrc.options[j].selected)
		{
			if(ie5 || ie4) 
			{
						var leng=objDest.options.length;
						var opt = doc.createElement('OPTION');
						opt.value=objSrc.options[j].value;
						opt.text=objSrc.options[j].text;
						objDest.options.add(opt,leng);
			}
			else if((ns4 || ns6))
			{
					var leng=objDest.options.length;
					objDest.options[leng]=new Option(objSrc.options[j].text,objSrc.options[j].value,false,false);
			}
			remEle[count]=objSrc.options[j].value;
			count++;
				
			}
	
	}
	sel=true;
	while(sel)
	{
		var sel=false;
		for(var j=0;j<objSrc.options.length;j++)
		{
			if(objSrc.options[j].selected)
			{
				
				sel=true;
				objSrc.options[j]=null;
			}
		}
	}
}


/* ************************* Date Validation *********************** */


 
 function LeapYear(year) 
 {
  if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) return true;
  else return false;
 }
 
 function okForm(monthField,myday,mymonth,myyear)
 {
	  if(mymonth=="Jan")
	  {
	   mymonth=1;
	  }
	  if(mymonth=="Feb")
	  {
	   mymonth=2;
	  }
	  if(mymonth=="Mar")
	  {
	   mymonth=3;
	  }
	  if(mymonth=="Apr")
	  {
	   mymonth=4;
	  }
	  if(mymonth=="May")
	  {
	   mymonth=5;
	  }
	  if(mymonth=="Jun")
	  {
	   mymonth=6;
	  }
	  if(mymonth=="Jul")
	  {
	   mymonth=7;
	  }
	  if(mymonth=="Aug")
	  {
	   mymonth=8;
	  }
	  if(mymonth=="Sep")
	  {
	   mymonth=9;
	  }
	  if(mymonth=="Oct")
	  {
	   mymonth=10;
	  }
	  if(mymonth=="Nov")
	  {
	   mymonth=11;
	  }
	  if(mymonth=="Dec")
	  {
	   mymonth=12;
	  }
	 
   if(myday > 31) {
    alert("The maximum number of days in a month is 31.");
    if(!monthField=='')
    monthField.focus();
	return false;
   }
   if((mymonth==4) || (mymonth==6) || (mymonth==9) || (mymonth==11)) {
    if(myday>30) {
    alert("There are only 30 days in the month that you have selected");
	if(!monthField=='')
		 monthField.focus();
    return false;
    }
   }
   if(!LeapYear(myyear) && (mymonth == 2) && (myday > 28)) {
    alert("There are only 28 days in February, "+myyear);
	if(monthField!='')
		monthField.focus();
    return false;
   }
  
   if((mymonth==2) && (myday > 29)) {
    alert("The maximum number of days in February is 29.");
	if(!monthField=='')
    monthField.focus();
    return false;
   }   
  return true;
 }

/*to check only aplhabets only*/

function Zipcheck(object,str)
{
 var zip=object.value;
 var allvalid=0;
 var valid="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
 for (var i=0; i<object.value.length; i++)
 {
  if(valid.indexOf(object.value.charAt(i)) < 0) 
  {
   allvalid=1;
  }
 }
 if (allvalid==0)
 {
  alert(str);
  object.focus();
  object.select();
  return false;
 }
 else
   return true;
}
 
function CheckState(country,state,stateText)
{	
	if(country.value!=1)
	{
		state.disabled=true;
		state.value='';
		stateText.disabled=false;
					
	}
	else
	{
		state.disabled=false;
		stateText.disabled=true;
		stateText.value='';
	}
}