<!--

var alphabet="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

var numerals="1234567890"

var invalidChar;
	
function checklist(theForm, theField, displayName)
{

	if (theForm[theField].selectedIndex == 0) 
		{
			message= displayName + " is required."
			alert(message);
			theForm[theField].focus();
			return (false);
		}

	if (LTrim(theForm[theField][theForm[theField].selectedIndex].value) == "") 
		{
			message= displayName + " is required."
			alert(message);
			theForm[theField].focus();
			return (false);
		}
	
	if (theForm[theField][theForm[theField].selectedIndex].value == " ") 
		{
			message= displayName + " is required."
			alert(message);
			theForm[theField].focus();
			return (false);
		}
	if (theForm[theField][theForm[theField].selectedIndex].value == "-1") 
		{
			message= displayName + " is required."
			alert(message);
			theForm[theField].focus();
			return (false);
		}
  return(true);
}

function LTrim(VALUE){
var w_space = String.fromCharCode(32);
if(v_length < 1){
return"";
}
var v_length = VALUE.length;
var strTemp = "";

var iTemp = 0;

while(iTemp < v_length){
if(VALUE.charAt(iTemp) == w_space){
}
else{
strTemp = VALUE.substring(iTemp,v_length);
break;
}
iTemp = iTemp + 1;
} //End While
return strTemp;
} //End Function


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
	
	if(total==1){
		if (theForm[theField].checked)
			{	temp++;
				theForm[hiddenField].value += theForm[theField].value;
				theForm[hiddenField].value += ",";	// delimiter used is comma		
			}			
	}
	else
	{
	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(total==1)
{
	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].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].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].focus();
			theForm[hiddenField].value=""  // destroy the values stored in the hidden field
			return (false);
		}	
	else if(temp >= min)
			return(true)
}			
 else
 {
		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)
{
	/* 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
				'date' for date format varification
		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
	*/

		
		/* 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 (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. Current length is " + theForm[theField].value.length
	
			alert(message);
			theForm[theField].focus();
			return (false);
		}
	
	
		if(!checkdatatype(theForm[theField].value, type, special))
		{
			if(type=='digits')
			message=displayName + " does not appear to be valid. Please validate the format."
			else
			message=displayName + " does not appear to be valid. The character " + invalidChar + " is not allowed. Please remove the character."
			
			alert(message);
			theForm[theField].focus();
			return (false);
		}
		
		if(type=="date")
			{
			
			if(!CheckDate(theForm[theField].value))
			{
				message=displayName + " does not appear to be valid. Please validate format i.e.(MM/DD/YYYY)."
				alert(message);
				theForm[theField].focus();
				return (false);
			}
			else
				return(true)				
			}
			
		if(type=="time")
			{
			
			if(!CheckTime(theForm[theField].value))
			{
				message=displayName + " does not appear to be valid. Please validate format i.e.(HH:MM:SS AM/PM). You can use 00 for Seconds (SS)."
				alert(message);
				theForm[theField].focus();
				return (false);
			}
			else
				return(true)				
			}			
				
		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 CheckDate(str_datetime) {
	var re_date = /^(\d{1,2})\/(\d{1,2})\/(\d{2,4})$/;
	if (!re_date.exec(str_datetime))
		return (false)
	else
		return (true);
}

function CheckTime(str_time) {
	var re_time = /^\d{1,2}:\d{2}:\d{2} ([AP]M)?$/;
	if (!re_time.exec(str_time))
		return (false)
	else
		return (true);
}


function checkdatatype(string, type, special) {
		invalidChar ="";
		if(type=='none')
				return(true)		
		
		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) 
						{
							invalidChar = ch;
							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) {

	emailStr = emailStr.toLowerCase();
	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;
		
}



function isAnyCard(cc)
{
  if (!isCreditCard(cc)){
    alert("You have entered an invalid credit card number. Please check the number for errors.");
    return false;
    }
  if (!isMasterCard(cc) && !isVisa(cc) && !isAmericanExpress(cc) && !isDinersClub(cc) && !isDiscover(cc)) {
    alert("You have entered an invalid credit card number. Please check the number for errors.");
    return false;
  }
  return true;

} // END FUNCTION isAnyCard()


/*  ================================================================
    FUNCTION:  isMasterCard()
 
    INPUT:     cc - a string representing a credit card number

    RETURNS:  true, if the credit card number is a valid MasterCard
		    number.
		    
	      false, otherwise

    Sample number: 5500 0000 0000 0004 (16 digits)
    ================================================================ */

function IsMasterCard (cc)  {
  return isMasterCard(cc);
}

function isMasterCard(cc)
{
  firstdig = cc.substring(0,1);
  seconddig = cc.substring(1,2);
  if ((cc.length == 16) && (firstdig == 5) &&
      ((seconddig >= 1) && (seconddig <= 5)))
    return isCreditCard(cc);
  return false;

} // END FUNCTION isMasterCard()


/*  ================================================================
    FUNCTION:  isVisa()
 
    INPUT:     cc - a string representing a credit card number

    RETURNS:  true, if the credit card number is a valid VISA number.
		    
	      false, otherwise

    Sample number: 4111 1111 1111 1111 (16 digits)
    ================================================================ */

function IsVisa (cc)  {
  return isVisa(cc);
}

function isVisa(cc)
{
  if (((cc.length == 16) || (cc.length == 13)) &&
      (cc.substring(0,1) == 4))
    return isCreditCard(cc);
  return false;
}  // END FUNCTION isVisa()



/*  ================================================================
    FUNCTION:  isAmericanExpress()
 
    INPUT:     cc - a string representing a credit card number

    RETURNS:  true, if the credit card number is a valid American
		    Express number.
		    
	      false, otherwise

    Sample number: 340000000000009 (15 digits)
    ================================================================ */

function IsAmericanExpress (cc)  {
  return isAmericanExpress(cc);
}

function isAmericanExpress(cc)
{
  firstdig = cc.substring(0,1);
  seconddig = cc.substring(1,2);
  if ((cc.length == 15) && (firstdig == 3) &&
      ((seconddig == 4) || (seconddig == 7)))
    return isCreditCard(cc);
  return false;

} // END FUNCTION isAmericanExpress()



/*  ================================================================
    FUNCTION:  isDinersClub()
 
    INPUT:     cc - a string representing a credit card number

    RETURNS:  true, if the credit card number is a valid Diner's
		    Club number.
		    
	      false, otherwise

    Sample number: 30000000000004 (14 digits)
    ================================================================ */

function IsDinersClub (cc)  {
  return isDinersClub(cc);
}

function isDinersClub(cc)
{
  firstdig = cc.substring(0,1);
  seconddig = cc.substring(1,2);
  if ((cc.length == 14) && (firstdig == 3) &&
      ((seconddig == 0) || (seconddig == 6) || (seconddig == 8)))
    return isCreditCard(cc);
  return false;
}


/*  ================================================================
    FUNCTION:  isDiscover()
 
    INPUT:     cc - a string representing a credit card number

    RETURNS:  true, if the credit card number is a valid Discover
		    card number.
		    
	      false, otherwise

    Sample number: 6011000000000004 (16 digits)
    ================================================================ */


function IsDiscover (cc)  {
  return isDiscover(cc);
}

function isDiscover(cc)
{
  first4digs = cc.substring(0,4);
  if ((cc.length == 16) && (first4digs == "6011"))
    return isCreditCard(cc);
  return false;

} // END FUNCTION isDiscover()


/*  ================================================================
    FUNCTION:  isCreditCard(st)
 
    INPUT:     st - a string representing a credit card number

    RETURNS:  true, if the credit card number passes the Luhn Mod-10
		    test.
	      false, otherwise
    ================================================================ */

function isCreditCard(st) {
  // Encoding only works on cards with less than 19 digits
  if (st.length > 19)
    return (false);

  sum = 0; mul = 1; l = st.length;
  for (i = 0; i < l; i++) {
    digit = st.substring(l-i-1,l-i);
    tproduct = parseInt(digit ,10)*mul;
    if (tproduct >= 10)
      sum += (tproduct % 10) + 1;
    else
      sum += tproduct;
    if (mul == 1)
      mul++;
    else
      mul--;
  }
// Uncomment the following line to help create credit card numbers
// 1. Create a dummy number with a 0 as the last digit
// 2. Examine the sum written out
// 3. Replace the last digit with the difference between the sum and
//    the next multiple of 10.

//  document.writeln("<BR>Sum      = ",sum,"<BR>");
//  alert("Sum      = " + sum);

  if ((sum % 10) == 0)
    return (true);
  else
    return (false);

} // END FUNCTION isCreditCard()


//function isExpDate(st)
//{

//	var gotNumber,GotVal;
//	sum = 0; mul = 1; l = st.length;
//	if (l!=5)
//	{
//		return (false);
//	}
	
//	for (i = 0; i < l; i++)
//	{
//		GotVal = st.substring(l-i-1,l-i);
//		if (GotVal=="/")
//			//alert("asdf/")
//			//check for year
//		else
//			//alert(GotVal)
//			
//			//gotNumber = gotNumber + GotVal
//			
//		alert(gotNumber);
//	}
	
	//alert(l);
//}


function isExpiryDate(ccYear,ccMonth) {


if (!checkdatatype(ccYear, 'digits', ''))
return false;

if (!checkdatatype(ccMonth, 'digits', ''))
return false;

today = new Date();

expiry = new Date(ccYear, ccMonth);

if (today.getTime() > expiry.getTime())
return false;
else
return true;
}


-->
