function isZipCode (s,message){
	// s is a form element
	// message is a string
	// Does NOT change the value of form element, s
	//Uses loops instead of slice or substr because IE3 doesn't
	//support those methods.  -- Steve
	var strZip;
	
	strZip = s.value;
	
	//Convert 5 digit zip to 9 digit by adding "0000"
	if (strZip.length == 5){
		if (isInteger(strZip,message)){
			return true;
		}
		else{
			s.focus();
			return false;
		}
	}
	
	// If length ten (XXXXX-YYYY), remove non-numeric then check length 9
	if(strZip.length == 10){
		strZip=numericize(strZip);
	}
	
	//Check length 9
	if (strZip.length == 9){
		if (isInteger(strZip,message)){
			return true;
		}
		else{
			s.focus();
			return false;
		}
		
	}
	alert(message);
	s.focus();
	return false;
}

function formatZipCode(s){
	// s is a string
	var strZip;
	strZip = numericize(s);
	if (strZip.length == 5){ 
		return strZip+"-0000";
	}
	else if(strZip.length==9){
			strZip = strZip.substring(0,5)+"-"+strZip.substring(5,9);
			return strZip;
	}
	else{
		return s;
	}
}

function isMoney(s,message) {
	// s is a string
	// message is a string
	// accepts blank as good -- check that separately
	// does not accept $ or ,
	// does not modify s
	if ((s.length == 0) || (s == "") || (s == null)) {
		return true;
	}
	    
	// skip over any leading blanks added for right justification

	var j=0;
	for(var i=0; i<s.length; i++) {
		if (s.substring(i, i+1) != ' ') {
			j=i;
			break;
		}
	}
	for (i=j; i<s.length; i++) {
		var ch = s.substring(i, i +1)
		if ((ch < "0" || "9" < ch) && (ch != '.')) {
			alert(message);
			return false;
		}
	}
	return true;
} 


function isInteger (s,message){
	// s is a string
	// message is a string
	var i;
	for (i = 0; i < s.length; i++){   
	    // Check that current character is number.
	    var c = s.charAt(i);
	    if (!isDigit(c)){
	 	 	alert(message);			
			return false;
		}
	}
	return true;
}

function isDigit (c){
	// c is a character
	return ((c >= "0") && (c <= "9"))
}

function numericize(s){
	// s is a string
	// message is a string
	var i,j;
	j="";
	for (i=0;i<s.length;i++){
		if (isDigit(s.charAt(i))){
			 j=j+s.charAt(i);
		}
	}
	return j;
}

function isBlank(s,message){
	// s is a form element
	// message is a string
	if ((s.value.length > 0) && (s.value != null) && (s.value != "")){
		return false;
	}
	if ((message != null) && (message.length > 0) && (message != "")){
		alert(message);
		s.focus();
	}
	return true;
}

function isSSN (s,message){
	// s is a form element
	// CHANGES the value of form element, s
	// message is a string
	//Uses loops instead of slice or substr because IE3 doesn't
	//support those methods.  -- Steve
	var x,i;

	// Check blank
	if (isBlank(s,message)){
		s.focus();
		return false;
	}

	// Check 11 digit with dashes (strip out dashes, check if valid)
	if (s.value.length == 11){
		s.value=numericize(s.value);
	}
	
	// Check 9 digit integer
	if (s.value.length == 9){
		if (isInteger(s.value,message)){
			return true;
		}
		else{
			s.focus();
			return false;
		}
	}

	alert(message);
	s.focus();
	return false;
}

function isPhone (s,message){
	// s is a form element
	// message is a string
	// Does NOT change the value of form element, s

	var strPhone;
	strPhone = s.value;

	// If non-numeric characters, strip them, then check for length
	if (strPhone.length > 10){
		strPhone = numericize(strPhone);
	}
	
	//Check if first digit is 0 or 1. Invalid phone number.
	if ((strPhone.charAt(0) == '0') || (strPhone.charAt(0) == '1')){
		alert(message);
		s.focus();
		return false;
	}
	
	//Should be 3 digit area code + 7 digit phone number, as a 10 digit string
	if (strPhone.length == 10){
		if (isInteger(strPhone,message)){
			return true;
		}
		else{
			s.focus();
			return false;
		}
	}
	else{ 
		alert(message);
		s.focus();
		return false;
	}
	return true;
}

function formatPhone(s){
	// s is a string
	var strPhone;
	strPhone = numericize(s);
	if (strPhone.length != 10){ 
		return s;
	}
	else{
		strPhone = strPhone.substring(0,3)+"-"+strPhone.substring(3,6)+"-"+strPhone.substring(6,10);
		return strPhone;
	}
}

function isEmail (s,message){
	// s is a form element
	// message is a string
	// Does NOT change the value of form element, s
	var i,ii;
	var j;
	var k,kk;
    var jj;
    var len;
    
    // Check blank
	if (isBlank(s,message)){
		s.focus();
		return false;
	}

    // Check valid email
    // Must have a "@" and a "." to be valid.
    // Must have at least 1 character before "@"
    // Must have at least 1 character after "@" and before "."
    // Must have at least 2 characters after "."
    if (s.value.length >0){
		i=s.value.indexOf("@");
		ii=s.value.indexOf("@",i+1);
		j=s.value.indexOf(".",i);
		k=s.value.indexOf(",");
		kk=s.value.indexOf(" ");
		jj=s.value.lastIndexOf(".")+1;
		len=s.value.length;
		if ((i>0) && (j>(1+1)) && (k==-1) && (ii==-1) && (kk==-1) &&
			(len-jj >=2) && (len-jj<=3)) {}
		else {
	 		 	alert(message);
	 		 	s.focus();			
				return false;
		}
	}
    return true;
}

function isSelected (s,message){
	// s is a select form element
	// message is a string
	// Does NOT change the value of form element, s
	// Checks to see if a selectbox has been changed from its first value
	// Should be used on drop-boxes that have "Select an Item" or something
	// similar as their first OPTION
	if (s.selectedIndex==0){
		if ((message != null) && (message.length > 0) && (message != "")){
			alert(message);
			s.focus();
		}
		return false;
	}
	return true;
}

function isRadioSelected(s,message){
	// s is a select form element
	// message is a string
	// Does NOT change the value of form element, s
	// Checks to see if a group of radio buttons has a selected item
	for (var i=0;i<s.length;i++){
		if (s[i].checked==true){
			return true;
		}
	}
	alert (message);
	return false;
}

function isCreditCard(s,card_type,message){
	// s is a textbox
	// card_type is a string, either "visa", "mastercard", or "discover"
	// message is the message to show if the format is not valid
	
	// check length is 16
	if (s.value.length != 16){
		alert(message);
		s.focus();
		return false;
	}
	
	// Check is integer
	if (!isInteger(s.value,message)){
		s.focus();
		return false;
	}
	
	// Check format by card type
	switch (card_type){
		case 'visa':
		//check visa format
		if (s.value.length.charAt(0) != '4'){
			alert(message);
			s.focus();
			return false;	
		}
		break;
		case 'mastercard':
		//check mastercard format
		if (s.value.length.charAt(0) != '5'){
			alert(message);
			s.focus();
			return false;	
		}
		break;
		case 'discover':
		// check discover format
		if (s.value.length.charAt(0) != '6'){
			alert(message);
			s.focus();
			return false;	
		}
		break;
		default:
		// do nothing
		break;
	}
		
	return true;
}

function isTime (s,message){
	// s is a form element
	// message is a string
	// Does NOT change the value of form element, s

	var strTime;
	var strHour;
	var strMinute;
	strTime = s.value;
	alert('strTime:' + strTime);
	// If non-numeric characters, strip them, then check for length
	if (strTime.length > 4){
		strTime = numericize(strTime);
	}
	strTime = '0' + strTime;
	strTime = strTime.substring(strTime.length - 4, strTime.length);
	//Should be 4 digit 
	if (strTime.length == 4){
		if (isInteger(strTime,message)){
			strHour = strTime.substring(0,2);
			strMinute = strTime.substring(2,2)
			if ((strHour >=0) && (strHour <= 23)){
				if ((strMinute >=0) && (strMinute <= 59)){
					return true;
				}
			}
		}
		else{
			s.focus();
			return false;
		}
	}
	else{ 
		alert(message);
		s.focus();
		return false;
	}
	return true;
}

function formatTime(s){
	// s is a string
	var strTime;
	strTime = numericize(s);
	strTime = '0' + strTime;
	strTime = strTime.substring(strTime.length - 4, strTime.length);
	if (strTime.length != 4){ 
		return s;
	}
	else{
		strTime = strTime.substring(0,2)+":"+strTime.substring(2,strTime.length);
		return strTime;
	}
}
