///////////////
// Constants //
///////////////
var failed_color = 'yellow';
var regular_color = '';
var first_failed_field = null;
var numericRegex = /\d/;

////////////////
// Prototypes //
////////////////
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}

///////////////
// Functions //
///////////////
function returnObject (id) {
	if (document.all) {
		return document.all[id];
	} else if (document.getElementById) {
		return document.getElementById(id)
	} else if (document.layers) {
		return document.layers[id];
	}
	return null;
}

function returnRadioValue (form,radio) {
	var radios = document.forms[form].elements[radio].length;
	
	if (radios == undefined) {
		if (document.forms[form].elements[radio])
			return document.forms[form].elements[radio].value;
	} else {
		for (var i = 0; i < radios; i++)
			if (document.forms[form].elements[radio][i].checked)
				return document.forms[form].elements[radio][i].value;
	}
	return null;
}

function showPopUp(url, title, options) {
	return window.open(url, title, options);
}

function documentWidth () {
	if(typeof(window.innerWidth) == 'number') {
		//Non-IE
	    return parseInt(window.innerWidth);
	} else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
	    //IE 6+ in 'standards compliant mode'
    	return parseInt(document.documentElement.clientWidth);
	} else if(document.body && (document.body.clientWidth || document.body.clientHeight)) {
		//IE 4 compatible
		return parseInt(document.body.clientWidth);
	}
	return 0;
}
	
function documentHeight () {
	if(typeof(window.innerWidth) == 'number') {
		//Non-IE
	    return parseInt(window.innerHeight);
	} else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
	    //IE 6+ in 'standards compliant mode'
	    return parseInt(document.documentElement.clientHeight);
	} else if(document.body && (document.body.clientWidth || document.body.clientHeight)) {
		//IE 4 compatible
		return parseInt(document.body.clientHeight);
	}
	return 0;
}

function validEmail (field) {
	var emailRegex = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	return emailRegex.test(field.value.trim());
}

function updateFirstFailedField (field) {
	if (first_failed_field == null)
		first_failed_field = field;
}

function validateTextField (field, name) {
	if (field.disabled)
		return '';

	var error = '';
	if (field.value.trim().length == 0) {
		field.style.background = failed_color;
		error = '\n' + name + ' field is incomplete.';
		updateFirstFailedField(field);
	} else {
		field.style.background = regular_color;
	}
	return error;
}

function validateNumericTextField (field, name, minimumLength) {
	if (field.disabled)
		return '';
	
	var error = '';

	if (field.value.trim().length < minimumLength) {
		field.style.background = failed_color;
		error = '\n' + name + ' field is incomplete.';
		updateFirstFailedField(field);
	} else if (!numericRegex.test(field.value)) {
		field.style.background = failed_color;
		error = '\n' + name + ' field contains invalid characters.';
		updateFirstFailedField(field);
	} else {
		field.style.background = regular_color;
	}
	
	return error;
}

function validateSelectField (field, name) {
	if (field.disabled)
		return '';
	
	var error = ''
	if (field.options[field.selectedIndex].value.length == 0) {
		field.style.background = failed_color;
		error = '\n' + name + ' field is incomplete.';
		updateFirstFailedField(field);
	} else {
		field.style.background = regular_color;
	}
	
	return error;
}

function validateEmail (field) {
	if (field.disabled)
		return '';

	var error = '';
	if (field.value.trim().length == 0) {
		field.style.background = failed_color;
		error = '\nEmail address is incomplete';
		updateFirstFailedField(field);
	} else if (!validEmail(field)) {
		field.style.background = failed_color;
		error = '\nEmail address is invalid.';
		updateFirstFailedField(field);
	} else {
		field.style.background = regular_color;
	}
	return error;
}

function validatePhone (field, section) {
	if (field.disabled)
		return '';

	var error = '';
	
	if (field.value.trim().length < 10) {
		field.style.background = failed_color;
		error = '\n' + section + 'Telephone number field is incomplete.';
		updateFirstFailedField(field);
	} else {
		field.style.background = regular_color;
	}

	return error;
}

function checkTextField(field) {
	if (field.disabled)
		return false;
	
	if (validateTextField(field).length > 0)
		field.style.background = failed_color;
	else
		field.style.background = regular_color;

	return true;
}

function checkNumericTextField(field, miniumumLength) {
	if (field.disabled)
		return false;

	if (validateNumericTextField(field, '', miniumumLength).length > 0)
		field.style.background = failed_color;
	else
		field.style.background = regular_color;

	return true;
}

function checkSelectField(field) {
	if (field.disabled)
		return false;

	if (validateSelectField(field).length > 0)
		field.style.background = failed_color;
	else
		field.style.background = regular_color;

	return true;
}

function checkEmailField(field) {
	if (field.disabled)
		return false;

	if (validateEmail(field).length > 0)
		field.style.background = failed_color;
	else
		field.style.background = regular_color;

	return true;
}

function checkPhoneField(field) {
	if (field.disabled)
		return false;

	if (validatePhone(field).length > 0)
		field.style.background = failed_color;
	else
		field.style.background = regular_color;

	return true;
}