//based on a validate at http://developer.apple.com/internet/webcontent/validation.html

//search form for all names beginning with an "_" and separate their "type" into an array
function getForm (formObj){//formName comes in from the form onSubmit
	//alert(formObj);
	/*if (document.formObj != null){
		xForm=document.formName;//variable to access the form
		alert ("y");
	}else{
	alert ("n");*/
		xForm=formObj;//variable to access the form
		//alert(xForm.name);
	//}
	reqFields = new Array();
	for (i = 0; i < xForm.elements.length; i++){//loop through the form elements
		if (xForm.elements[i].name != null){
			var currElement = xForm.elements[i].name;
			if (currElement.match("^_")){//look for required fields which all begin with "_"	and keep track of the type
				if (currElement.match("_emai")){
					reqFields[i] = "email";
				}else if(currElement.match("_name")){
					reqFields[i] = "name";
				}else if(currElement.match("_tel")) {
					reqFields[i] = "telephone";
				}else if (currElement.match("_passw")) {
					reqFields[i] = "password";
				}else {
					reqFields[i] = "misc";
				}
			}
		}

	}	
	return checkForm();//call checkForm to begin the validation of fields
	
}
//------this lot may work, but I can't get it to activate
//check form fields and output any errors
function checkForm() {
var why = "There are some errors in the form:\n";
//loop through form and run various checks based on types set above
for (i = 0; i < xForm.elements.length; i++){
	if (reqFields [i] != ""){//limit the processing to specified fields
		if (reqFields [i] == "email"){
			why += checkEmail(xForm[i].value);//pass the form field, chack it and add any errors to the "why" alert
		}else if (reqFields [i] == "telephone"){
			why += checkPhone(xForm[i].value);
		}else if (reqFields [i] == "password"){
			why += checkPassword(xForm[i].value);
		}else if (reqFields [i] == "name"){
			why += checkname(xForm[i].value);	
		}else if (reqFields [i] == "misc"){
			var strngName = xForm[i].name;
			strngName = strngName.replace("_","");
			why += isEmpty(xForm[i].value, strngName);//check if empty if none of the above
		}
	}
}
if (why != "There are some errors in the form:\n"){
alert (why);
  return false;
}else{
	/*--------------------------------------------------------------------------------------
	|			the next bit takes the place of nameem to make it OOP						|
	---------------------------------------------------------------------------------------*/
	xForm.Sender.value=xForm._emailaddress.value;
	//alert (xForm.Sender.value);
	//--------- end
}
}
//IS THE FIELD EMPTY?
function isEmpty (strng, strngName){
var error = "";
 if (strng == "") {
 	var errorMsg = " is empty and is a required field.\n"
   	 error = strngName + errorMsg;
 }
 return error;
}

//CHECK THE NAME
function checkname (strng) {
 var error = "";
 if (strng == "") {
    error += "You didn't enter a name.\n";
 }
//make sure name has more than 1 character and less than 30
 if (strng.length > 30){
	error += "Please use a shortened version of your name.\n";
}

//look for illegal characters in the field
//var illegalChars = /[\'W_]/;   // to allow only letters, numbers, + underscores
//    if (illegalChars.test(strng)) {
//       error += "The name contains strange characters.\n";
//    } 
	return error;
}

//EMAIL ADDRESS CHECK
function checkEmail(strng){
	var error = "";
	var emailFilter=/^.+@.+\..{2,3}$/;
if (!(emailFilter.test(strng))) { 
       error = "Please enter a valid email address.\n";
	  
}
/*The carat (^) means the start of the string. the dollar sign ($) means the end of the string. By 
surrounding the rest with these characters, we evaluate the entire string.  .+ means "at least one character" — 
the dot (.) means "any character" and the plus sign (+) means "one or more." The .{2,3} portion means "two 
or three characters (but no more)." */

//forbid the following: ( ) < > [ ] , ; : \ / "
var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/ 
if (strng.match(illegalChars)) {
   error = "The email address contains illegal characters.\n";
  
}
return error
}


//TELEPHONE NUMBERS (FAX ETC)------ NOTE: the name must start with "_tel", so "_telfax" would work)
function checkPhone(strng){
	var error = "";
	var stripped = strng.replace(/[\(\)\.\-\ ]/g, '');
//strip out acceptable non-numeric characters and check the result is a number

if (isNaN(parseInt(stripped))) {
   error = "Please enter a valid phone number.\n";
   
}else if (stripped.length <= 10) {
	error = "The phone number is the wrong length. Make sure you have included an area code.\n";
	
}
	return error;
}

//PASSWORD validation
function checkPassword (strng) {
 var error = "";
 if (strng == "") {
    error = "You didn't enter a password.\n";
	
 }
 
 //--CONTROL PASSWORD LENGTH
 	if ((strng.length < 6) || (strng.length > 15)) {
       error = "The password is the too short.\n";
	   
 	}else if (strng.length > 20){
		error = "The password is the too long.\n";
		
	}
//look for illegal characters in the field
/*    var illegalChars = /[\W_]/;      // to allow only letters and numbers uncomment this phrase
    else if (illegalChars.test(strng)) {  
      error = "The password contains illegal characters.\n";
    }
	
//to Force the password to be SECURE --- comment out if unnecessary
//------ regular expressions, a-z, A-Z, and 0-9, each followed by  + , which means “one or more,” and we use the search() method to make sure they’re all there
	else if (!((strng.search(/(a-z)+/))  && (strng.search(/(A-Z)+/))  && (strng.search(/(0-9)+/)))) {
  error = "The password must contain at least one uppercase letter, one lowercase letter, and one numeral.\n";
  }*/
  return error;
 }

  
/*//RADIO BUTTONS --To make sure that a radio button has been chosen from a selection

function checkRadio(){
  for (i=0, n=xForm.radios.length; i<n; i++) {
   if (xForm.radios[i].checked) {
      var checkvalue = xForm.radios[i].value;
      break;
   }
}
why += checkRadio(checkvalue);
 
function checkRadio(checkvalue) {
var error = "";
   if (!(checkvalue)) {
       error = "Please check a radio button.\n";
    }
return error;    
}
}

//DROPDOWNS -- to make sure that the user has selected an option from a drop-down a (and not the first option, which is just a "Choose one" header)
function checkDropdown(choice) {
    var error = "";
    if (choice == 0) {
       error = "You didn't choose an option from the drop-down list.\n";
    }    
return error;
}*/