//This file creates the client side validation objects, and validates the client input


// Construct the validation object
var objValidation = new Object;
objValidation = validation_init();

// validation object
function validation_init() {
	this.nonEmpty = Check_NonEmpty;
	this.itemSelected = Check_itemSelected;
	this.firstitemSelected = vs_firstitem_selected;
	this.validZip = Check_zipCode;
	this.checkEmail = Check_Email;
	this.badChar = Check_characters;
	this.validNumber = vs_valid_number;
	this.validPhone = Check_Phone;
	this.checkPassword = Check_Password;
	this.verifyOldPassword = Verify_OldPassword;
	return this;
}

// Extend the string object to include a trim function
String.prototype.Trim = trim_string;
// Extend the date object to include a simple form string conversion
Date.prototype.toSimpleForm = date_toSimpleForm;


function display_name(field) {
	var strDisplay = field.getAttribute("DisplayName");
	if (strDisplay==null || strDisplay=="")
		strDisplay="Field";
	return strDisplay;
}

function Check_Password (Password1, Password2)
{
	var strErrorMsg = "Your confirmed password is inconsist of the password you typed in, please reenter your password and confirm it.";
	
	if (Password1.value == Password2.value)
		return true;
	else {
		alert (strErrorMsg);
		return false;
	}
}

function Verify_OldPassword (Password1, Password2)
{
	var strErrorMsg = "The old password you entered is inconsist of the one in our system,please reenter it.";
	
	if (Password1.value == Password2.value)
		return true;
	else {
		alert (strErrorMsg);
		return false;
	}
}
	
function Check_Phone(field, fieldName) {
var strErrorMsg = "The " + fieldName + " field may not contain any of the following characters: & \+ ' \; % \> \< ' \x22";
	var badChar = new Array("&","\+", "'","\;","%","\>","\<","[,]",'"');
	var strItem = field.value.Trim();
	for (var i = 0; i < badChar.length; i++) {
		if (strItem.indexOf(badChar[i]) != -1){
		field.focus();
		alert(strErrorMsg);
		return false;
		}
	}
	return true;
}

function default_value(field) {
	var strDefault = field.defaultValue;
	if (strDefault==null || strDefault=="")
		strDefault="";
	return strDefault;
}

function trim_string() {
	var ichar, icount;
	var strValue = this;
	ichar = strValue.length - 1;
	icount = -1;
	while (strValue.charAt(ichar)==' ' && ichar > icount)
		--ichar;
	if (ichar!=(strValue.length-1))
		strValue = strValue.slice(0,ichar+1);
	ichar = 0;
	icount = strValue.length - 1;
	while (strValue.charAt(ichar)==' ' && ichar < icount)
		++ichar;
	if (ichar!=0)
		strValue = strValue.slice(ichar,strValue.length);
	return strValue;
}

function date_toSimpleForm() {
	var toSimpleForm = new String;
	toSimpleForm = this.toLocaleString();
	toSimpleForm = toSimpleForm.substring(0,toSimpleForm.indexOf(' '));
	return toSimpleForm;
}


function Check_NonEmpty(field, fieldName) {
	var strErrorMsg = "Please complete the " + fieldName + " field.";
	field.value=field.value.Trim();
	if (field.value.length==0) {
		field.focus();
		alert(strErrorMsg);
		return false;
	}
	return true;
}

function Check_itemSelected(field, fieldName) {
	var strErrorMsg = "Please select the " + fieldName + ".";
	if (field.selectedIndex==0) {
		field.focus();
		alert(strErrorMsg);
		return false;
	}
	return true;
}
//special case where value of first select item is -1
function vs_firstitem_selected(field, fieldName) {
	var strErrorMsg = "Please choose either UK or EIRE as your " + fieldName + ".";
	if (field.value==-1) {
		field.focus();
		alert(strErrorMsg);
		return false;
	}
	return true;
}

function Check_zipCode(field, fieldName) {
	var strErrorMsg = "Please enter a valid ZIP Code of the form 99999 for the " + fieldName + " field.";
	field.value=field.value.Trim();
	if (!(/^\d{5}$/.test(field.value) || /^\d{5}-\d{4}$/.test(field.value))) {
		field.focus();
		alert(strErrorMsg);
		return false;
	}
	return true;
}

function Check_Email(field, fieldName) {
	var strErrorMsg = "Please enter a valid address in the " + fieldName + " field.";
	if (field.value.length > 0 )
	{
		field.value=field.value.Trim();
		if (!(/^[\w-\.]+@[\w-\.]+$/.test(field.value))) {
			field.focus();
			alert(strErrorMsg);
			return false;
		}
	}
	return true;
}

//two fields must have identical values
//**THIS FUNCTION IS NOT LIKE THE OTHERS**
//only pass the two element names(in any order)
function vs_password_confirm(fieldA, fieldB) {
	var strErrorMsg = "Please confirm your password.";
	if (fieldA.value!=fieldB.value) {
		fieldB.focus();
		alert(strErrorMsg);
		return false;
	}
	return true;
}

//prevents troublesome characters from being entered
function Check_characters(field, fieldName) {
var strErrorMsg = "The " + fieldName + " field may not contain any of the following characters: & \+ \( \) ' \; % \> \< ' \x22";
	var badChar = new Array("&","\+","\(","\)","'","\;","%","\>","\<","[,]",'"');
	var strItem = field.value.Trim();
	for (var i = 0; i < badChar.length; i++) {
		if (strItem.indexOf(badChar[i]) != -1){
		field.focus();
		alert(strErrorMsg);
		return false;
		}
	}
	return true;
}


function vs_valid_number(field, fieldName) {
	var strErrorMsg = "Please enter a valid non-negative number in the " + fieldName + " field.";
	var strDefault = default_value(field);
	if (strDefault.length==0) {
		strDefault="0";
	}
	field.value=field.value.Trim();
	if (field.value.length==0)
		field.value=strDefault;
	var num = ".0123456789";
	for (var intLoop = 0; intLoop < field.value.length; intLoop++) {
		if (num.indexOf(field.value.charAt(intLoop)) == -1) {
			field.focus();
			alert(strErrorMsg);
			return false;
		}
	}
	if (field.value.indexOf(".")!=field.value.lastIndexOf(".")) {
		field.focus();
		alert(strErrorMsg);
		return false;
	}
	return true;
}


