<!--

var alphabet="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
var numerals="1234567890"
	
function trim(s) {
  while (s.substring(0,1) == ' ') {
    s = s.substring(1,s.length);
  }
  while (s.substring(s.length-1,s.length) == ' ') {
    s = s.substring(0,s.length-1);
  }
  return s;
}
	
function checklist(theForm, theField, displayName)
{

	if (theField=='StateProv' && document.getElementById("Country").value!='USA')
		{
			return(true);
		}
	else 
		if (theForm[theField].selectedIndex == 0) 
			{
				message= displayName + " is required."
				alert(message);
				theForm[theField].focus();
				return (false);
			}
	
  return(true);
}

function checkbox(theForm, theField, displayName, min, max, total){

	/* COMMENTS ON HOW TO USE THIS FUNCTION
	   ====================================
	   theForm = Name Of Form
		theField = Name of field (checkboxes group ) used in the HTML form
		displayName = Name of field to be displayed in message box
		min = minimum selections required  
		max = maximum selections required
		total = total number of checkboxes 
	
	NOTE : 1. if there is no restriction over how many selections should be made, then 
				'min' should be equal to 0 and 'max' equal to 'total'.
				
			2. there should be a hidden field in the form with the name in the following format ....
				 
				fieldName_all // where fieldName is the Name of the field (checkboxes group)
								// e.g if the checkboxes group name is "selection" , the corresponding
								// hidden field should be named "selection_all"
								
			3. If there is only one checkbox to be validated, all of 'min','max' and 'total'
				should be equal to 1.
	*/
	
	var temp=0							//to hold the number of selected options
	hiddenField=theField + "_all"	// general convention for a hidden field ( fieldname_all) in form
										// that will take the concatinated value of all the marked checkboxes
	
	theForm[hiddenField].value=""  //make sure hidden field value is 0, it's important.
	
	otherField=theField + "_other" //textbox field for an option not present in the list
	
	for(i=0; i<total; i++){
		if (theForm[theField][i].checked)
			{	temp++;
				theForm[hiddenField].value += theForm[theField][i].value;
				theForm[hiddenField].value += ",";	// delimiter used is comma		
			}
		}
		
	if(theForm[otherField] != null) // if the 'other' field exists then concatinate its value
			theForm[hiddenField].value += theForm[otherField].value; 	
	
	if(temp==0) // user has made no selection at all
		{
			message= displayName + " is required."
			alert(message);
			theForm[theField][0].focus();
			return (false);
		}
		
	else if(temp < min) // user has made selections less than required
		{
			message="Please select at least " + min + " from the options given in " + displayName + ".";
			alert(message);
			theForm[theField][0].focus();
			theForm[hiddenField].value=""  // destroy the values stored in the hidden field
			return (false);
		}
	
	else if(temp > max) // user has made selections more than required
		{
			message= "Please select no more than " + max + " from the options given in " + displayName + ".";
			alert(message);
			theForm[theField][0].focus();
			theForm[hiddenField].value=""  // destroy the values stored in the hidden field
			return (false);
		}	
	
	else if(temp >= min)
			return(true)

}



function check(theForm, theField, displayName, min, max, type, special,importance)
{
	/* COMMENTS ON HOW TO USE THIS FUNCTION
	   ====================================
	    theForm = Name OF Form
		theField = Name of field used in the HTML form
		displayName = Name of field to be displayed in message box
		min = minimum charactres required
		max = maximum characters required
		type = type of field .... 
				'characters' for string ( one line text box )
				'digits' for numeric ( one line text box )
				'dropdown' for combo box
				'checkbox' for a group of checkboxes
				'email' for email address
				'ccnumber' for credit card number
				'none' for all types of data
		special = variable used for various purposes in diff. cases like ...
					
					for textbox, special = special characters allowed in the field 
											  in addition to its actual datatype
										
			for checkbox group, special = total number of check boxes in the group
		importance = indicates whether field is optional or mandatory
					'optional' for optioanl fields
					'mandatory' for mandatory fields
		
	*/

		/* check whether field is optional and empty
		 then let it go empty ... */
						
		if (importance=='optional' && theForm[theField].value.length==0)
			return(true);
		
		/* Since Dropdown and checkboxes are the datatypes which do not need to be checked for minimun,
		   maximum or invalid characters, hence they have separate functions for them */ 
		
		if(type=="dropdown")
			{ if(!checklist(theForm, theField, displayName))
				 return(false);
			  else 
			    return(true);
			}
			
		if(type=="checkbox")
			{  
			 if(!checkbox(theForm, theField, displayName, min, max, special))
 				 return(false);
			  else 
			    return(true);
			}
		
		if (theForm[theField].value.length =="") 
		{
			message=displayName + " is required."
			alert(message);
			theForm[theField].focus();
			return (false);
		}
		
		//If the control is here, it means the field is a text box or text area.
		//Remove any TRAILING SPACES		
		theForm[theField].value = trim(theForm[theField].value)
		
		if (min!="null" && theForm[theField].value.length < min) 
		{
			
			if(type=="digits" || type=='ccnumber')
				message=displayName + " must contain at least " + min + " digits."
			else
				message=displayName + " must contain at least " + min + " characters."	
			
			alert(message);
			theForm[theField].focus();
			return (false);
		}
		
		if (max!="null" && theForm[theField].value.length > max) 
		{
			if(type=="digits" || type=='ccnumber')
				message=displayName + " must contain at most " + max + " digits."
			else
				message=displayName + " must contain at most " + max + " characters."
	
			alert(message);
			theForm[theField].focus();
			return (false);
		}
	
	
		if(!checkdatatype(theForm[theField].value, type, special))
		{
			message=displayName + " does not appear to be valid. Please check your format."
			alert(message);
			theForm[theField].focus();
			return (false);
		}

				
		if(type=="email")
			{
			
			if(!checkEmail(theForm[theField].value))
				{
				theForm[theField].focus()
				return(false)
				}
			else
				return(true)				

			}
		
		
		if(type=="ccnumber")
			{
			
			
			if(!checkCardnumber(theForm[theField].value))
				{
				theForm[theField].focus()
				return(false)
				}
			else
				return(true)
								

			}


		if(theField=="CCDateM") {
			if( theForm[theField].value>12 || theForm[theField].value<1 )
				{
				alert('Invalid Month specified.');
				theForm[theField].focus();
				return(false);
				}
		}
		
		if(theField=="CCDateY") {
			if( theForm[theField].value < 2003 )
				{
				alert('Year must be greater then or equal to the current year.');
				theForm[theField].focus();
				return(false);
				}
		}
	
	return(true);
}

function checkdatatype(string, type, special) {
		
		if(type=='none')
				return(true)
		
		if(special==null)
			special=''
		
		else if(type=='characters')
				special = special + alphabet
		
		else if(type=='digits')
				special = special + numerals
		
		else if(type=='email')
				special = special + numerals + alphabet + "@."
				
		else if(type=='ccnumber')
				special = special + numerals
		
		
				
		var result = true;
			
		for (i = 0;  i < string.length;  i++) 
			{
			ch = string.charAt(i);
			for (j = 0;  j < special.length;  j++)
				{
				 if (ch==special.charAt(j))
						{ 
						  break;
						}
				 if (j == special.length-1) 
						{
							result = false;
							break;
						}
				}
	
			}
		
		return(result);
			
}



function match(theForm, theField1, theField2, displayName)
{
	/* COMMENTS ON HOW TO USE THIS FUNCTION
	   ====================================
	   theForm = Name OF Form
		theField1 = Name of first field used in the HTML form
		theField2 = Name of second field used in the HTML form
		displayName = Name of field to be displayed in message box
	*/

		var string1 = theForm[theField1].value
		var string2 = theForm[theField2].value
		
		if (string1.length != string2.length) 
		{
			alert( "Given " + displayName + " do not match. Please re-type your " + displayName + " and then try again.");
			theForm[theField1].focus();
			return (false);
		}
		
		
		for( i=0; i<string1.length; i++)
		{
		if (string1.charAt(i) != string2.charAt(i))
			{
			alert( "Given " + displayName + " do not match. Please re-type your " + displayName + " and then try again.");
			theForm[theField1].focus();
			return (false);
			}
		}
			
	return(true);
}


function checkEmail (emailStr) {

	var checkTLD=1;
	var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
	var emailPat=/^(.+)@(.+)$/;
	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
	var validChars="\[^\\s" + specialChars + "\]";
	var quotedUser="(\"[^\"]*\")";
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom=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("Invalid Email address ! Please check @ and .'s");
		return false;
	}

	var user=matchArray[1];
	var domain=matchArray[2];


	for (i=0; i<user.length; i++) {
		if (user.charCodeAt(i)>127) {
			alert("Invalid Email address ! The username contains invalid characters.");
			return false;
   			}
		}

	for (i=0; i<domain.length; i++) {
		if (domain.charCodeAt(i)>127) {
			alert("Invalid Email address ! The domain name contains invalid characters.");
			return false;
   		}
	}

	if (user.match(userPat)==null) {
			alert("Invalid Email address ! The username doesn't seem to be valid.");
			return false;
	}

	var IPArray=domain.match(ipDomainPat);
	
	if (IPArray!=null) {
			for (var i=1;i<=4;i++) {
				if (IPArray[i]>255) {
						alert("Invalid Email address ! Destination IP address is invalid!");
						return false;
   						}
			}
		return true;
	}

 
	var atomPat=new RegExp("^" + atom + "$");
	var domArr=domain.split(".");
	var len=domArr.length;
	
	for (i=0;i<len;i++) {
		if (domArr[i].search(atomPat)==-1) {
			alert("Invalid Email address ! The domain name does not seem to be valid.");
			return false;
   			}
		}

	if (checkTLD && domArr[domArr.length-1].length!=2 && domArr[domArr.length-1].search(knownDomsPat)==-1) {
			alert("Invalid Email address ! The address must end in a well-known domain or two letter " + "country.");
			return false;
		}

	if (len<2) {
		alert("Invalid Email address ! This address is missing a hostname!");
		return false;
	}

	return true;
}


function checkCardnumber(number)
{
		var checkStr = strip(number) + '';
		var checksum=0;
		var ddigit=0;
		var kdig = 0;
	
			
		for (i = checkStr.length-1;  i >= 0;  i--) 
		{
			kdig++;
			ch = checkStr.charAt(i);
			if ((kdig % 2) != 0) 
				{
					checksum=checksum+parseInt(ch)
				}
			else
				{
				ddigit=parseInt(ch)*2;
				if (ddigit >= 10)
					checksum=checksum+1+(ddigit-10)
				else
					checksum=checksum+ddigit;
				}
			
		}

		if ((checksum % 10) != 0) {
			alert('You have entered an invalid credit card number. Please check the number for errors.');
			return (false);
		}

	return(true);
}

function strip(number) {
			var sOut = '';
			mask = '1234567890';
			for(count = 0; count <= number.length; count++) {
 				if(mask.indexOf(number.substring(count, count+1),0) != -1 )
 						sOut += number.substring(count,count+1);
 			}
			return sOut;
		
}



//Start of checkdate function
<!-- Begin
function checkdate(objName) 
{
//	alert(objName);
	var datefield = objName;
	if (chkdate(objName) == false) 
	{
		datefield.select();
		alert("That date is invalid.  Please try again.");
//		datefield.focus();
		return false;
	}
	else
	{
		return true;
	}
}

function chkdate(objName) 
{
	var strDatestyle = "US"; //United States date style
	//var strDatestyle = "EU";  //European date style
	var strDate;
	var strDateArray;
	var strDay;
	var strMonth;
	var strYear;
	var intday;
	var intMonth;
	var intYear;
	var booFound = false;
	var datefield = objName;
	var strSeparatorArray = new Array("-"," ","/",".");
	var intElementNr;
	var err = 0;
	var strMonthArray = new Array(12);
	strMonthArray[0] = "Jan";
	strMonthArray[1] = "Feb";
	strMonthArray[2] = "Mar";
	strMonthArray[3] = "Apr";
	strMonthArray[4] = "May";
	strMonthArray[5] = "Jun";
	strMonthArray[6] = "Jul";
	strMonthArray[7] = "Aug";
	strMonthArray[8] = "Sep";
	strMonthArray[9] = "Oct";
	strMonthArray[10] = "Nov";
	strMonthArray[11] = "Dec";
	strDate = datefield.value;

	if (strDate.length < 1) 
	{
		return true;
	}
	
	for (intElementNr = 0; intElementNr < strSeparatorArray.length; intElementNr++) 
	{
		if (strDate.indexOf(strSeparatorArray[intElementNr]) != -1) 
		{
			strDateArray = strDate.split(strSeparatorArray[intElementNr]);
			if (strDateArray.length != 3) 
			{
				err = 1;
				return false;
			}
			else 
			{
			strDay = strDateArray[0];
			strMonth = strDateArray[1];
			strYear = strDateArray[2];
			}
			booFound = true;
		}
	}
	if (booFound == false) 
	{
		if (strDate.length>5) 
		{
			strDay = strDate.substr(0, 2);
			strMonth = strDate.substr(2, 2);
			strYear = strDate.substr(4);
		}
	}
	if (strYear.length == 2) 
	{
		strYear = '20' + strYear;
	}
	
	// US style
	if (strDatestyle == "US") 
	{
		strTemp = strDay;
		strDay = strMonth;
		strMonth = strTemp;
	}
	intday = parseInt(strDay, 10);
	if (isNaN(intday)) 
	{
		err = 2;
		return false;
	}
	intMonth = parseInt(strMonth, 10);
	if (isNaN(intMonth)) 
	{
		for (i = 0;i<12;i++) 
		{
			if (strMonth.toUpperCase() == strMonthArray[i].toUpperCase()) 
			{
				intMonth = i+1;
				strMonth = strMonthArray[i];
				i = 12;
			}
		}
		if (isNaN(intMonth)) 
		{
			err = 3;
			return false;
		}
	}
	intYear = parseInt(strYear, 10);
	if (isNaN(intYear)) 
	{
		err = 4;
		return false;
	}
	if (intMonth>12 || intMonth<1) 
	{
		err = 5;
		return false;
	}
	if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intday > 31 || intday < 1)) 
	{
		err = 6;
		return false;
	}
	if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1)) 
	{
		err = 7;
		return false;
	}
	if (intMonth == 2) 
	{
		if (intday < 1) 
		{
			err = 8;
			return false;
		}
		if (LeapYear(intYear) == true) 
		{
			if (intday > 29) 
			{
				err = 9;
				return false;
			}
		}
	else 
		{
			if (intday > 28) 
			{
				err = 10;
				return false;
			}
		}
	}
	
	if (strDatestyle == "US") 
	{
		datefield.value = strMonthArray[intMonth-1] + " " + intday+" " + strYear;
	}
	else 
	{
		datefield.value = intday + " " + strMonthArray[intMonth-1] + " " + strYear;
	}
return true;
}

function LeapYear(intYear) 
{
	if (intYear % 100 == 0) 
	{
		if (intYear % 400 == 0) { return true; }
	}
	else 
	{
		if ((intYear % 4) == 0) { return true; }
	}
return false;
}

function IsNumeric(str,Theform)
   //  check for valid numeric strings	
   {
   
   var strValidChars = "0123456789.-";
   var strChar;
   var blnResult = true;
   var strString = "";
   
   
   
   
   strString = Theform.User_id.value;
   
   
   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
	{
		strChar = strString.charAt(i);
		if (strValidChars.indexOf(strChar) == -1)
			blnResult = false;		
	}
	
	if(blnResult == true)
		{
			alert("Only numeric value is not allowed in User ID field, please enter correct value");
			Theform.User_id.focus();
			return false;
		}
	else
			return true;
  }
//  End -->

//End of checkdate function
