// ******************  GENERIC FORM VALIDATOR  ***************
// 	INSTRUCTIONS:
//  1) Include the following tag at the top of your HTML page: <script language="javascript" src="/includes/form_validation.js"></script>
//  2) Make a javascript array in the HTML page like the following: var formArray = new Array();
//  3) Create new formElement objects for each field in your form you want to validate as below:
//	   formArray[0] = new formElement("email","email1","You have not entered a valid email address");
//	   ....the first parameter should be either "text", "email","radio" or "checkbox"
//	   ....the second parameter should be the name of the form element as in the HTML
//	   ....the the third parameter should be a string to represent the error displayed for this field
//  4) in the onsumbit section of the form call: "return validate(formArray)"
// ***********************************************************
function formElement(type,element,error)
{
	this.type   = new String (type);
	this.element = element;
	this.error   = new String (error);
}

function validate(formArray)
{
	for(i=0;i<formArray.length;i++)
	{
		thisformElement =  	document.forms[0].elements[formArray[i].element];
		thisformObject	=	formArray[i];
		
		if(thisformObject.type=="text"){
			if(thisformElement.value == "" || thisformElement.value == null){
				alert(thisformObject.error);
				return false;
				break;
			}
		}
		
		else if(thisformObject.type=="email"){
			if (thisformElement.value.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) == -1){
				alert(thisformObject.error);
				return false;
				break;
			}
		}
		
		else if(thisformObject.type=="radio"){
			radioSelected = 0;
			
			for (a=0; a < thisformElement.length; a++) {
				if (thisformElement[a].checked == true) {
					radioSelected++;
				} 
			}
			if(radioSelected<1){
				alert(thisformObject.error);
				return false;
				break;
			}
		}
		
		else if(thisformObject.type=="checkbox"){
			if(!thisformElement.checked){
				alert(thisformObject.error);
				return false;
				break;
			}
		}

	}
	return true;	
}

function IsNumeric(sText)
{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;
 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
}

function validateFormPhone()
{
	radioSelected = 0;
	for (a=0; a < compForm.answer1.length; a++) {
		if (compForm.answer1[a].checked == true) {
			radioSelected++;
		} 
	}
	if(radioSelected<1){
		alert("Please select an answer");
		return false;
	} else if ((compForm.email.value == null || compForm.email.value == "")) {
		if ((compForm.answer5.value == null || compForm.answer5.value == "")) {
			alert("You must enter either your email address or home telephone number");
			return false;
		} else if(!IsNumeric(compForm.answer5.value)){
			alert("Your telephone number shouldn't contain any letters");
			return false;
		} else if(!compForm.terms.checked){
			alert("You must check the terms box if you wish to enter");
			return false;
		} else {
			compForm.email.value = "-";
			return true;
		}
	} else if (compForm.email.value.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) == -1) {
		alert("You have entered an invaild email address");
		return false;
	} else if(!IsNumeric(compForm.answer5.value)){
		alert("Your telephone number shouldn't contain any letters");
		return false;
	} else if (!compForm.terms.checked) {
		alert("You must check the terms box if you wish to enter");
		return false;
	} else {
		return true;
	}
}