/* FormValidation.js */
/* Version 1.1       */
var ctlService, ctlName, ctlCompany, ctlEmail, ctlPhone, ctlDate, ctlComments;

function isEmpty(s) {
	if (s == null || s.length == 0)
		return true;
	else
		return !/\S/.test(s);
}

function validateService(field) {
	var str = field.value;

	if (str == 0) {
		alert("Please select an item.");
		field.focus();
		return false;
	}
	return true;
}

function validateName(field) {
	var str = field.value;

	if (isEmpty(str)) {
		alert("Name may not be empty.");
		field.focus();
		return false;
	}
	else
		return true;
}

function validateEmail(field) {
	var str = field.value;
	var strEmailPattern = /[^@]+@\w+\.(com|org|edu|net|info|gov|biz)/;
	
	if (isEmpty(str)) {
		alert("Email may not be empty.");
		field.focus();
		return false;
	}
	else {
		if (strEmailPattern.test(str))
		 	return true;
		else {
			alert("Email is not in an accepted format.\nPlease try again.");
			field.focus();
			return false;
		}
	}
}

function validatePhone(field) {
	var str = field.value;
	var strPhonePattern = /^\(?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{4})$/;
	
	if (!isEmpty(str)) {
		if (strPhonePattern.test(str)) {
			return true;
		}
		else {
			alert("Phone number is not in an accepted format.\nPlease try again.");
			field.focus();
			return false;
		}
	}
	else {
		return true;
	}
}

function validateDate(field) {
	var str = field.value;
	var strDatePattern = /((?:0[1-9]|1[0-2])\/?:0[1-9]|[12][0-9]|3[01])\/(?:19|20\d{2})/;

	if (!isEmpty(str)) {
		if (strDatePattern.test(str)) {
			return true;
		}
		else {
			alert("Please select a date.");
			field.focus();
			return false;
		}
	}
	else {
		return true;
	}
}

/* * * * * * * * * * * * * * * * * * * * * * * * */
/* Validates frmBooking.                         */
/* * * * * * * * * * * * * * * * * * * * * * * * */
function validateFrmBooking() {
	ctlService = document.frmBooking.cboService;
	ctlName = document.frmBooking.txtName;
	ctlCompany = document.frmBooking.txtCompany;
	ctlPhone = document.frmBooking.txtPhone;
	ctlEmail = document.frmBooking.txtEmail;
	ctlDate = document.frmBooking.txtDate;
	ctlComments = document.frmBooking.txtMessage;
	
	if (!validateService(ctlService)) {
		return false;
	}

	if (!validateName(ctlName)) {
		return false;
	}
	else {
		document.frmBooking.txtName.value = filterText(ctlName.value);
	}
	
	if (!isEmpty(ctlCompany.value)) {
		document.frmBooking.txtCompany.value = filterText(ctlCompany.value);
	}
	
	if (!validateEmail(ctlEmail)) {
		return false;
	}
	else {
		document.frmBooking.txtEmail.value = filterText(ctlEmail.value);
	}
	
	if (!validatePhone(ctlPhone)) {
		return false;
	}
	else {
		document.frmBooking.txtPhone.value = filterText(ctlPhone.value);
		if (!validatePhone(ctlPhone)) {
			return false;
		}			
	}

	if (!validateDate(ctlDate)) {
		return false;
	}
	else {
		document.frmBooking.txtDate.value = filterText(ctlDate.value);
	}

	if (!isEmpty(ctlComments.value)) {
		document.frmBooking.txtMessage.value = filterText(ctlComments.value);
	}	

	return true;
}

/* * * * * * * * * * * * * * * * * * * * * * * * */
/* Validates frmMailingList.                     */
/* * * * * * * * * * * * * * * * * * * * * * * * */
function validateFrmMailingList() {
	ctlName = document.frmMailingList.txtName;
	ctlEmail = document.frmMailingList.txtEmail;

	if (!validateName(ctlName)) {
		return false;
	}
	else {
		document.frmMailingList.txtName.value = filterText(ctlName.value);
	}

	if (!validateEmail(ctlEmail)) {
		return false;
	}
	else {
		document.frmMailingList.txtEmail.value = filterText(ctlEmail.value);
	}

	return true;
}

/* * * * * * * * * * * * * * * * * * * * * * * * */
/* Validates frmMailingListHome.                 */
/* * * * * * * * * * * * * * * * * * * * * * * * */
function validateFrmMailingListHome() {
	ctlEmail = document.frmMailingListHome.txtEmail;

	if (!validateEmail(ctlEmail)) {
		return false;
	}
	else {
		document.frmMailingListHome.txtEmail.value = filterText(ctlEmail.value);
	}

	return true;
}

/* * * * * * * * * * * * * * * * * * * * * * * * */
/* Limits a text field to the specified amount.  */
/* * * * * * * * * * * * * * * * * * * * * * * * */
function limitText(ctlLimitField, ctlCount, intMax) {
	if (ctlLimitField.value.length > intMax) {
		ctlLimitField.value = ctlLimitField.value.substring(0, intMax);
	}
	else {
		ctlCount.value = intMax - ctlLimitField.value.length;
	}
}

/* * * * * * * * * * * * * * * * * * * * * * * * */
/* Removes possible malicious characters.        */
/* * * * * * * * * * * * * * * * * * * * * * * * */
function filterText(strValue) {
	var strTemp = strValue;

	strTemp = strTemp.replace(/\<|\>|\"|\'|\%|\;|\(|\)|\&|\+|\-/g,"");
	
	return strTemp;
}
