////////////////
// Constants //
//////////////
var failed_color = 'yellow';
var regular_color = 'white';
var numericRegex = /\d/;

///////////////
// Variables //
//////////////
var first_failed_field = null;

////////////////
// 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.getElementById) {
		return document.getElementById(id);
	} else if (document.all) {
		return document.all[id];
	} else if (document.layers) {
		return document.layers[id];
	}
	return null;
}

function showPopUp(url, title, options) {
	return window.open(url, title, options);
}

function validateEmail (field) {
	var emailRegex = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	return emailRegex.test(field.value.trim());
}

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.backgroundColor = 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.backgroundColor = 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.backgroundColor = regular_color;
	}
	
	return error;
}

function validateEmailField (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.backgroundColor = regular_color;
	}
	return error;
}

function validatePhoneField (field) {
	var error = '';
	var phoneStripped = '';
	
	phoneStripped = field.value.trim().replace(/[^\d]/g, '');
	
	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.backgroundColor = failed_color;
		error = '\nTelephone number contains invalid characters.';
		updateFirstFailedField(field);
	} else if (!(phoneStripped.length == 10)) {
		field.style.backgroundColor = 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 validateZipCodeField (field, name) {
	var error = '';
	var zipcodeStripped = '';
	
	//zipcodeStripped = field.value.trim().replace(/[^\d]/g, '');
	zipcodeStripped = field.value.trim().replace(' ', '');
	if (field.value.trim().length == 0) {
		field.style.backgroundColor = failed_color;
		error = '\n' + name + ' is incomplete.';
	} else if ((zipcodeStripped.length != 5) && (zipcodeStripped.length != 9) && (zipcodeStripped.length != 6)) {
		field.style.backgroundColor = failed_color;
		error = '\n' + name + ' is the wrong length.';
	} else {
		field.style.backgroundColor = regular_color;
		if (zipcodeStripped.length == 9) {
			field.value = zipcodeStripped.substring(0, 5) + '-' + zipcodeStripped.substring(5, 9);
		} else {
			field.value = zipcodeStripped.substring(0, 6);
		}
	}
	return error;
}

function checkTextField(field) {
	if (field.disabled)
		return false;
	
	if (validateTextField(field).length > 0)
		field.style.backgroundColor = failed_color;
	else
		field.style.backgroundColor = regular_color;

	return true;
}

function checkNumericTextField(field, miniumumLength) {
	if (field.disabled)
		return false;

	if (validateNumericTextField(field, '', miniumumLength).length > 0)
		field.style.backgroundColor = failed_color;
	else
		field.style.backgroundColor = regular_color;

	return true;
}

function checkSelectField(field) {
	if (field.disabled)
		return false;

	if (validateSelectField(field).length > 0)
		field.style.backgroundColor = failed_color;
	else
		field.style.backgroundColor = regular_color;

	return true;
}

function checkEmailField(field) {
	if (field.disabled)
		return false;

	if (validateEmailField(field).length > 0)
		field.style.backgroundColor = failed_color;
	else
		field.style.backgroundColor = regular_color;

	return true;
}

function checkPhoneField(field) {
	if (field.disabled)
		return false;

	if (validatePhoneField(field).length > 0)
		field.style.backgroundColor = failed_color;
	else
		field.style.backgroundColor = regular_color;

	return true;
}

function checkZipCodeField (field) {
	if (field.disabled)
		return false;
	
	if (validateZipCodeField(field).length > 0)
		field.style.backgroundColor = failed_color;
	else
		field.style.backgroundColor = regular_color;
	
	return true;
}