//common fuctions used with forms

//takes a value, checks if it's an integer, and returns true or false
function isInteger(value) {
	return (value==parseInt(value));
}
	
//takes a form and an array of element names; verifies that each has a value
function requireValues (form, requiredValues) {
	temp="";
	for (var i=0; i<requiredValues.length; i++) {
		element=requiredText[i];
		if (form[element].value=="") {
		temp+=form[element].name+"\n";
		}		
	}
	if (temp){
	alert("Please enter a value for each of following fields:\n\n"+temp);
	return false;
	}
	return true;
}

//takes a form and an array of element names; verifies that each has a option selected 
//(other than the first; assume that the first option in each select menu contains instruction)
function requireSelects (form, requiredSelect) {
	temp="";
	for (var i=0; i<requiredSelect.length; i++) {
		element=requiredSelect[i];
		if (form[element].selectedIndex<=0) {
		temp+=form[element].name+"\n";
		}		
	}
	if (temp){
	alert("Please select a choice for each of following fields:\n\n"+temp);
	return false;
	}
	return true;
}


//takes a form and an array of element names; verifies that each has a value checked
function requireRadios (form, requiredRadio) {
	for (var i=0; i<requiredRadio.length; i++) {
		element=requiredRadio[i];
		isChecked=false;
		for (j=0; j<form[element].length; j++) {
			if (form[element][j].checked) {
				isChecked=true;
			}
		}
		if (! isChecked) {
			alert ("Please choose a " + form[element][0].name + ".");
				return false;
			}
		}
		return true;
}

//verifies that the zip code element has ##### or #####-#### format

function checkZip(element) {
var valid="0123456789-"
var f=element;

	for (var i=0; i < f.length; i++) {
	while (""+f.charAt(i)==" ")f=(f.substring(0,i)+f.substring(i+1,f.length));
	temp = "" + f.substring(i,i+1);
	if (valid.indexOf(temp) == "-1") {
		alert ("Numbers and hyphen only, please.")
		return false;
		}
	}

	if ((f.length!=5 && f.length!=10)
		||(f.length==10&&(f.charAt(5)!=="-"||f.charAt(0)=="-"||f.charAt(6)=="-"))
		||(f.length==5&&(!isInteger(f.substring(0,5))||f.charAt(0)=="-"))
		||(f.length==10&&(!isInteger(f.substring(0,5))||!isInteger(f.substring(6,10))))) {
			alert("Please input your 5-digit or 5-hyphen-4 digit zip code\n"+
			"in the format like: '12345' or '12345-6789'.");
		return false;
		}	
	
	return true;

}
//  End checkZip-->

//verifies that email address provided is in correct format--think about Perl or C++

<!-- Begin
function checkEmail (emailStr) {

// To check if e-mail fits the user@domain format and to separate the username from the domain 
var emailPat=/^(.+)@(.+)$/;

// Matching all special characters not allowed, including ( ) < > @ , ; : \ " . [ ]       
var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";

// The range of characters allowed in a username or domainname, actually, it's !specialChars 
var validChars="\[^\\s" + specialChars + "\]";

// The pattern applies if the "user" is a quoted string which is legal 
var quotedUser="(\"[^\"]*\")";

// The pattern applies for domains that are IP addresses rather than symbolic names. Note the square brackets are required
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;

// The following string represents an atom (basically a series of non-special characters.) 
var atom=validChars + '+';

// The string represents one word in the typical username, e.g., in john.doe@somewhere.com, john and doe are words. 
var word="(" + atom + "|" + quotedUser + ")";

// The pattern describes the structure of the user
var userPat=new RegExp("^" + word + "(\\." + word + ")*$");

// The pattern describes the structure of a normal symbolic domain, as opposed to ipDomainPat 
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");


// Finally, let's start trying to figure out if the supplied address is valid. 

// Begin with the coarse pattern to simply break up user@domain into different pieces that are easy to analyze. 

var matchArray=emailStr.match(emailPat);
if (matchArray==null) {
  // Too many/few @'s or something; basically, it doesn't even fit the general mould of a valid e-mail address. 
	alert("Email address seems incorrect (check @ and .'s)");
	return false;
}
var user=matchArray[1];
var domain=matchArray[2];

// See if "user" is valid 
if (user.match(userPat)==null) {
    // user is not valid
    alert("The username doesn't seem to be valid.");
    return false;
}

// if the e-mail address is at an IP address (as opposed to a symbolic host name) make sure the IP address is valid. 
var IPArray=domain.match(ipDomainPat);
if (IPArray!=null) {
    // this is an IP address
	  for (var i=1;i<=4;i++) {
	    if (IPArray[i]>255) {
	        alert("Destination IP address is invalid!");
		return false;
	    }
    }
    return true;
}

// Domain is symbolic name
var domainArray=domain.match(domainPat);
if (domainArray==null) {
	alert("The domain name doesn't seem to be valid.");
    return false;
}

// now make sure that it ends in a three-letter word (like com, edu, gov) or a two-letter word 
// we need to break up the domain to get a count of how many atoms it consists of. 
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) {
   // the address must end in a two letter or three letter word.
   alert("The address must end in a three-letter domain, or two letter country.");
   return false;
}

// Make sure there's a host name preceding the domain.
if (len<2) {
   var errStr="This address is missing a hostname!";
   alert(errStr);
   return false;
}

// If we've gotten this far, everything's valid!
return true;
}

//  End checkEmail-->

//phone number validation

<!-- Begin validatePhone(field)

function checkPhone(field) {
var valid="()0123456789-"
var f=field;
	//check if all characters are valid. Any combination of digits, spaces, "-", "(", and ")" are OK
	for (var i=0; i < f.length; i++) {
	while (""+f.charAt(i)==" ")f=(f.substring(0,i)+f.substring(i+1,f.length));
	temp = "" + f.substring(i,i+1);
	if (valid.indexOf(temp) == "-1") {
		alert ("Invalid characters. Please try again")
		return false;
		}
	}	
	//eliminate spaces, hyphen, brakets, then check if the phone is a 10-digit format	
	var temp = "";
	f=""+f;
	splitstring = f.split(" ");
	for(i = 0; i < splitstring.length; i++)
	temp += splitstring[i];
	var hy="";
	splithy = temp.split("-");
	for(i = 0; i < splithy.length; i++)
	hy += splithy[i];
	var bb="";
	splitbb = hy.split("(");
	for(i = 0; i < splitbb.length; i++)
	bb += splitbb[i];
	var cb="";
	splitcb = bb.split(")");
	for(i = 0; i < splitcb.length; i++)
	cb += splitcb[i];	
	if (cb.length!=10){
		alert ("Please enter your 10-digit phone number including area code.")
		return false;
	}	
}

//  End checkPhone(field)-->

