/* Creates a nifty validator object */
function validator(form){
	//Properties
	this.frm = form;
	this.errorMessage = "The following Errors occured with your submission\n\n";
	this.isValid = true;
	this.haveFee = false; // this is for confreg.php form 
	this.haveDonation = false; // this is for donate.php
	//Methods	
	this.validateIt = validateIt; //validater the form, any fields that arent optional and are empty produce an error
	this.validate = validate;
	this.alertUser = alertUser;
	this.addToErrorMessage = addToErrorMessage;
	this.getErrorMessage = getErrorMessage; 
	this.checkEmail = checkEmail; //(email)check an email element is valid
	this.fillform = fillform; //fill the form with test data
	this.displayNameNice = displayNameNice;//make the name look good
}


//Code for Methods ...
function addToErrorMessage(message){
	if(this.errorMessage.indexOf(message)==-1)
	this.errorMessage += message +"\n";
}
function getErrorMessage(){
	return this.errorMessage;	
}
function alertUser(){
	alert(this.errorMessage);
}
function validate(){
	if(this.validateIt()){
		return true;
	}
	this.alertUser();
	return false;
}
function displayNameNice(str){
	return str.replace('[]','');
}

function validateIt(){
	for(var x=0; x<this.frm.length; x++) {
		if (this.frm.elements[x].className == 'jsRequired'){
			if (this.frm.elements[x].value==''){
				this.isValid = false;
				this.addToErrorMessage("- the field " + this.displayNameNice(this.frm.elements[x].name) + " is required.");
			}
		}
		if (this.frm.elements[x].className == 'jsEmail'){
			this.isValid = this.checkEmail(this.frm.elements[x].value)
		
			if(this.frm.elements['EmailAddress'].value != this.frm.elements['_EmailAddress2_'].value ){
				this.isValid = false;
				if(this.errorMessage.indexOf("- Email addresses don't match.")==-1)	
					this.addToErrorMessage("- Email addresses don't match.");
			}
		}
	
	}
		
	return this.isValid;
	
}
function checkEmail(email){
	if(email.indexOf('@')==-1||email.indexOf('.')==-1){
		this.addToErrorMessage("- You have not supplied a valid email address.");
		return false;
	}
return true;}

function fillform(){
	var els = this.form.elements;
	//alert("els" + els);
	for(var i=0; i<els.length; i++) {
		switch(els[i].type){
			case "select-one" :
			els[i].options[els[i].selectedIndex].value == ""
			els[i].selectedIndex = 1;
			break;
			case "text":
			els[i].value= "AT"+ els[i].id;
			break;
			case "textarea":
			els[i].value= "AdminTest for" + els[i].id;
			break;
			case "checkbox":
			els[i].checked = true;
			break;
			case "radio":
			els[i].checked = true;
			break;
		}
	}
}
