
//-- -------------------------------------------
//-- FUNCTION TO CLEAR THE SEARCH BOX TEXT
//-- -------------------------------------------
function clearField(Field,fieldValue) {
	if (Field.value	== fieldValue) {
		Field.value = '';
	}
}





//-- -------------------------------------------
//-- FUNCTION TO RESET THE SEARCH BOX TEXT
//-- -------------------------------------------
function resetField(Field,fieldValue) {
	if (Field.value	== '') {
		Field.value = fieldValue;
	}
}





//-- -------------------------------------------
//-- FUNCTION TO PRINT PAGE
//-- -------------------------------------------
function printpage() {
	if (window.print) { window.print(); }
	else {
		alert ("To print this page please select the 'Print' option from your browser's 'File' menu.")
	}
	return;
}





//-- -------------------------------------------
//-- ADD ON LOAD EVENT
//-- -------------------------------------------
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	}
	else {
		window.onload = function() {
			if (oldonload) {
				oldonload();
			}
		func();
		}
	}
}





//-- -------------------------------------------
//-- CLASS FUNCTIONS
//-- -------------------------------------------
function hasClass(target, classValue) {
	var pattern = new RegExp("(^| )" + classValue + "( |$)");
	if (target.className.match(pattern)) {
		return true;
	}
	return false;
}

function addClass(target, classValue) {
	if (!hasClass(target, classValue)) {
		if (target.className == "") {
			target.className = classValue;
		} else {
			target.className += " " + classValue;
		}
	}
	return true;
};

function removeClass(target, classValue) {
	var removedClass = target.className;
	var pattern = new RegExp("(^| )" + classValue + "( |$)");
	removedClass = removedClass.replace(pattern, "$1");
	removedClass = removedClass.replace(/ $/, "");
	target.className = removedClass;
	return true;
};





//-- -------------------------------------------
//-- GENERIC ACCESSIBLE POPUP SCRIPT
//-- -------------------------------------------
var _POPUP_FEATURES = 'width=790,height=525,scrollbars=no';

function raw_popup(url, target, features) {
	if (isUndefined(features)) features = _POPUP_FEATURES;
	if (isUndefined(target)) target   = '_blank';
	var theWindow = window.open(url, target, features);
	theWindow.focus();
	return theWindow;
}

function link_popup(src, features) {
	return raw_popup(src.getAttribute('href'), src.getAttribute('target') || '_blank', features);
}


function isUndefined(v) {
	var undef;
	return v===undef;
}




//-- -------------------------------------------
//-- AUTOMATICALLY UPDATE THE END DATE
//-- -------------------------------------------
function syncDate(sourceObj,target) {
	var sourceValue = sourceObj.options[sourceObj.selectedIndex].value;
	var targetObj = document.getElementById(target);
	targetObj.value = sourceValue;
}





//-- -------------------------------------------
//-- VALIDATE FORM
//-- -------------------------------------------
epValidateForms = function() {
	if (!dom) return;
	var allForms = document.getElementsByTagName('form');
	for (var f=0; f<allForms.length; f++) {
		if (hasClass(allForms[f], "ep-validate")) {
			// Update form's onsubmit action
			var thisForm = allForms[f];
			var oldOnsubmit = thisForm.onsubmit;
			if (typeof oldOnsubmit!= "function") {
				thisForm.onsubmit = function() {
					return epCheckFields(thisForm);
				}
			} else {
				thisForm.onsubmit = function() {
					epCheckFields(thisForm);
					return oldOnsubmit();
				}
			}
			// Update fields onfocus and onblue actions
			var allInputs = allForms[f].getElementsByTagName("input");
			for (var i=0; i<allInputs.length; i++) {
				if (hasClass(allInputs[i], "txt")) {
					if (allInputs[i].value == "") {
						allInputs[i].value = allInputs[i].alt;
					}
					allInputs[i].onfocus = function() {
						clearField(this, this.alt);
					}
					allInputs[i].onblur = function() {
						resetField(this, this.alt);
					}
				}
			}
		}
	}
	return true;
};


epCheckFields = function(theForm) {

	var hasError = false;
	var errObj;
	var errorMessage = "";

	// Loop through all the fields looking for required fields
	var allInputs = theForm.elements;

	for (var i=0; i<allInputs.length; i++) {
		if (hasClass(allInputs[i], "ep-required")) {
			// Radio inputs
			if (allInputs[i].type == "radio") {
				var radioName = allInputs[i].name;
				var radioChecked = false;
				var allRadio = theForm.getElementsByTagName("input");
				for (var r=0; r<allRadio.length; r++) {
					if (allRadio[r].type == "radio" && allRadio[r].name == radioName) {
						if (allRadio[r].checked) radioChecked = true;
					}
				}
				if (radioChecked == false) {
					hasError = true;
					errObj = allInputs[i].parentNode;
					errorMessage = "Please " + allInputs[i].alt.toLowerCase();
					break;
				} else {
					removeClass(allInputs[i].parentNode, "err");
				}
			// Checkboxes
			} else if (allInputs[i].type == "checkbox") {
				if(!allInputs[i].checked) {
					hasError = true;
					errObj = allInputs[i].parentNode;
					errorMessage = "Please " + allInputs[i].alt.toLowerCase();
					break;
				} else {
					removeClass(allInputs[i].parentNode, "err");
				}
			// Textareas
			} else if (allInputs[i].type == "textarea") {
				if (allInputs[i].value == "") {
					hasError = true;
					errObj = allInputs[i];
					errorMessage = "Please enter " + allInputs[i].title.toLowerCase();
					break;
				} else {
					removeClass(allInputs[i], "err");
				}
			// Text inputs
			} else {
				// If the field is empty of has the default value
				if (allInputs[i].value == "" || allInputs[i].value == allInputs[i].alt) {
					hasError = true;
					errObj = allInputs[i];
					errorMessage = "Please enter " + allInputs[i].alt.toLowerCase();
					break;
				// If the field has a value and it is an email field - check it is valid
				} else {
					if (hasClass(allInputs[i], "email") && !validateEmail(allInputs[i].value)) {
						hasError = true;
						errObj = allInputs[i];
						errorMessage = "Please enter a valid email address";
						break;
					} else {
						removeClass(allInputs[i], "err");
					}
				}
			}
		}
	}



	// If we have an error return false
	if(hasError) {
		addClass(errObj,"err");
		if (document.getElementById("errorHolder")) {
			document.getElementById("errorHolder").innerHTML = errorMessage;
		} else {
			var errorHolder = document.createElement("p");
			errorHolder.id = "errorHolder";
			addClass(errorHolder,"error");
			errorHolder.appendChild(document.createTextNode(errorMessage));
			theForm.parentNode.insertBefore(errorHolder, theForm);
		}
		// Move to the correct part of the page
		window.location = String(window.location).replace(/\#.*$/, "") + "#errorHolder";
		return false;
	} else {
		var theAction = theForm.action;
		var theNewAction;
		if (theAction.indexOf("?") != -1) {
			theNewAction = theAction + "&javascript=true";
		} else {
			theNewAction = theAction + "?javascript=true";
		}

		theForm.action = theNewAction;
		return true;
	}
};


addLoadEvent(epValidateForms);

