///////////////
// Constants //
///////////////
var failed_color = 'yellow';
var regular_color = 'white';
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) {
	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) {
	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) {
	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) {
	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) {
	var error = '';
	var phoneStripped = '';
	
	for ($i = 0; $i < field.value.length; ++$i)
		if (field.value[$i].match(/(\d)/))
			phoneStripped += field.value[$i];
	
	if (field.value.trim().length == 0) {
		field.style.background = failed_color;
		error = '\nTelephone number field is incomplete.';
		updateFirstFailedField(field);
	} else if (isNaN(parseInt(phoneStripped))) {
		field.style.background = failed_color;
		error = '\nTelephone number contains invalid characters.';
		updateFirstFailedField(field);
	} else if (!(phoneStripped.length == 10)) {
		field.style.background = failed_color;
		error = '\nTelephone number is the wrong length.';
		updateFirstFailedField(field);
	} else {
		field.style.background = regular_color;
		field.value = phoneStripped.substring(0, 3) + '-' + phoneStripped.substring(3, 6) + '-' + phoneStripped.substring(6, 10);
	}
	return error;
}

function checkTextField(field) {
	if (validateTextField(field, '').length > 0)
		field.style.background = failed_color;
	else
		field.style.background = regular_color;
	return true;
}

function checkNumericTextField(field, miniumumLength) {
	if (validateNumericTextField(field, '', miniumumLength).length > 0)
		field.style.background = failed_color;
	else
		field.style.background = regular_color;
	return true;
}

function checkSelectField(field) {
	if (validateSelectField(field, '').length > 0)
		field.style.background = failed_color;
	else
		field.style.background = regular_color;
	return true;
}

function checkEmailField(field) {
	if (validateEmail(field, '').length > 0)
		field.style.background = failed_color;
	else
		field.style.background = regular_color;
	return true;
}

function checkPhoneField(field) {
	if (validatePhone(field, '').length > 0)
		field.style.background = failed_color;
	else
		field.style.background = regular_color;
	return true;
}

function showMoreInfo() { 
     var url = 'moreinfo.php'; 
     var title = 'More Info'; 
     var options = 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=400,height=200'; 
 
     showPopUp(url, title, options); 
} 
