//Formats to validate the form
var TEXTONLY_FORMAT=0,TEXT_FORMAT=1,EMAIL_FORMAT=2;

var formats=new Array();
formats[TEXTONLY_FORMAT]=new Array("checkText", "must have only letters and spaces");
formats[TEXT_FORMAT]=new Array("checkField", "");
formats[EMAIL_FORMAT]=new Array("checkEmail", "must be a properly formatted email address");

//This function returns whether all fields in the form are valid or not and
//also outputs a message to the user if one or more of the fields are incorrect
function IsValidForm(form, fields){
var msg="";
for(cnt=0;cnt<fields.length;cnt++){
if(fields[cnt][3]==true){
if(form.elements[fields[cnt][0]].value==""){
msg+="- The field \"" + fields[cnt][1] + "\" cannot be blank\n";
}
}
var isvalid=eval(formats[fields[cnt][2]][0] + "('" + form.elements[fields[cnt][0]].value + "');");
if(!isvalid){
msg+="- The field \"" + fields[cnt][1] + "\" " + formats[fields[cnt][2]][1] + "\n";
}
}
if(msg!=""){
alert("The following errors were found in the information you entered: \n\n" + msg + "\nPlease correct those errors in order to submit your information correctly.");
return false;
}
return true;
}


//Text only validation function
function checkText(txt){
var valid="abcdefghijklmnopqrstuvwxyzABCDEFGIJKLMNOPQRSTUVWXYZ ";
for(i=0;i<txt.length;i++) if(valid.indexOf(txt.charAt(i))==-1) return false;
return true;
}

//Text validation function
function checkField(str){
return true;
}

//Email validation function
function checkEmail(email){
var valid="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._@";
for(i=0;i<email.length;i++){
if(valid.indexOf(email.charAt(i))==-1) return false;
}
if(email!=""){
if(email.indexOf("@")==-1 || email.indexOf(".")==-1) return false;
}
return true;
}
