function trim(s) {
  while (s.substring(0,1) == ' ') {
    s = s.substring(1,s.length);
  }
  while (s.substring(s.length-1,s.length) == ' ') {
    s = s.substring(0,s.length-1);
  }
  return s;
}

function check_passwords(pw1,pw2)
{
	var password1 = document.getElementById(pw1).value;
	var password2 = document.getElementById(pw2).value;
	if (password1 != password2)
	{
		alert("You must enter the same password twice. Please try again");
		return false;
	}
	return true;
}

/*********************************
 * Define a formValidator class.
 *********************************/
/**
 * formValidator constructor.
 * @param string Form_name the name of the form to validate.
 * @param document The current document.
 * @return A new formValidator object.
 */
function formValidator(form_name,currentDoc) {
	//Define member values
	this.names = new Array();
	this.fields = new Array();
	this.types = new Array();
	this.myDoc = currentDoc;
	this.element_types = new Array();
	this.my_form_name = form_name;
	this.i = 0;
	//Define member methods
	/*this.addFormElement = addFormElement;
	this.validate_form = validate_form;
	this.radioButtonIsValid = radioButtonIsValid;
	this.checkListIsValid = checkListIsValid;
	this.textFieldIsValid = textFieldIsValid;*/
}
	//Declare the member methods. Declare them inside the constructor function (formValidator) so they have access
	//to "private" members.
	
/**
 * Adds a form element to the validator.
 * @param string field The name of the HTML element.
 * @param string name The name of the form element that will be displayed.
 * @param string type The type of the data (String, Integer).
 * @param string element_type The formElement type (Radio, Checkbox, Text).
 */
formValidator.prototype.addFormElement = function addFormElement(field,name,type,element_type)
{
	//alert(form_name);
	this.names[this.i]=name;
	this.fields[this.i]=field;
	this.types[this.i]=type;
	this.element_types[this.i]=element_type;
	this.i++;
}

/**
 * Check to see if a checklist is valid.
 * @param string name The name of the item
 * @param string field_name The name of the HTML form element
 * @return  Returns "true" if something has been checked. "false", otherwise.
 */
formValidator.prototype.checkListIsValid = function checkListIsValid(name,field_name)
{
	elements = this.myDoc.getElementsByName(field_name);
	for (var j=0; j<elements.length; j++)
	{
		if (elements[j].checked==true)
			{return true;}
	}
	alert('You did not select a value for \n"'+name+'".\nPlease select a value before submitting this form.');
	return false;
}

/**
 * Check to see if a Radio button group is valid.
 * @param string name The name of the item
 * @param string field_name The name of the HTML form element
 * @return  Returns "true" if something has been selected. "false", otherwise.
 */
formValidator.prototype.radioButtonIsValid = function radioButtonIsValid(name,field_name)
{
	elements = this.myDoc.getElementsByName(field_name);
	for (var j=0; j<elements.length; j++)
	{
		if (elements[j].checked==true)
			{return true;}
	}
	alert('You did not select a value for '+name+'\nPlease select a value before submitting this form.');
	return false;
}

/**
 * Check to see if a checklist is valid.
 * @param string name The name of the item
 * @param string field_name The name of the HTML form element
 * @param string type The type that the field is suppsoed to be (String, Integer)
 * @return  Returns "true" if a text field has been filled out and is the correct type, "false" otherwise.
 */
formValidator.prototype.textFieldIsValid = function textFieldIsValid(name,field_name,type)
{
	var tmp_val = this.myDoc.getElementsByName(field_name)[0].value;
	//alert(field_name+' '+tmp_val);
	fieldType = typeof tmp_val;
	fieldVal = trim(tmp_val);
	var valid=true;
	//alert(typeof document.getElementById(fields[i]).value);
	//alert(Number(fieldVal));
	//If there is no value in a field, report it to the user.
	if (tmp_val==null || fieldVal=='')
	{
		alert('You did not fill out the field: '+name+'\nPlease fill out this field before submitting.');
		valid=false;
	}
	//If the user did not enter the correct TYPE of value, let them know!!
	//NOTE: if you want the types to be ignored (maybe you don't know or care) you can fill in 'N/A'
	else 
	{
		if (name.toLowerCase()=='email' || name.toLowerCase()=='email address')
		{
			valid = email_address_validator(fieldVal.toLowerCase());
			if (valid==false) alert('You did not enter a valid email address. Please try again.');
		}
		else if (type=='Integer')
		{
			if (isNaN(Number(fieldVal)))
			{
				alert('The field \''+name+'\' was filled in incorrectly as: \''+fieldVal+'\'\nPlease fill it in as: \''+type+'\'');
				valid=false;
			}
		}
	}
	return valid;
}

/**
 * Validates a form. You should call this function from an "onClick" function of a button.
 * It will attempt to validate all of the fields in the form. 
 * @return  Returns "false" if the form has errors, will call the "submit" routine of the form if validation succeeds.
 */
formValidator.prototype.validate_form = function validate_form()
{
	var k=0;
	var valid=true;
	//alert (fields.length);
	var fieldType;
	var fieldVal;
	while(valid && k<this.fields.length)
	{
		/*Check for checkboxes*/
		if (this.element_types[k]=='Checkbox')
		{
			valid = this.checkListIsValid(this.names[k], this.fields[k]);
		}
		
		/*Check for radio buttons*/
		else if (this.element_types[k]=='Radio')
		{
			valid = this.radioButtonIsValid(this.names[k], this.fields[k]);
		}

		/*Check for text fields*/
		else if (this.element_types[k]=='Text')
		{
			valid = this.textFieldIsValid(this.names[k], this.fields[k]);
		}
		
		/*Check for validity of form*/
		if (valid==true)
		{
			k++;
		}
		else
		{
			return valid;
		}
	}
	//If valid is still true, it means the the above loop exited because all of the fields were OK.
	//In this case, submit the form!
	if (valid==true)
	{
		//alert(form_name);
		//document.getElementById(form_name).submit();
		//this.myForm.submit();
		//alert(this.my_form_name);
		this.myDoc.getElementsByName(this.my_form_name)[0].submit();
	}
}
	



function email_address_validator(emailaddress)
{
	/*regular expression for matching email addresses found at: http://www.regular-expressions.info/email.html*/
	/*could replace the ([A-Z]{2}|com|org|net|biz|info|name|aero|biz|info|jobs|museum|name) with an actual list of ALL valid TLD's,
	and then just reference it here (if you can do that with regexp's in javascript)*/
	var email_pattern = /^[a-z0-9._%-]+@[a-z0-9-.]+\.([a-z]{2}|com|org|net|biz|info|name|aero|biz|info|jobs|museum|name)$/
	emailaddress = emailaddress.toLowerCase();
	if (emailaddress.match(email_pattern))
	{
		return true;
	}
	return false;
}

