/*
Simple JS for validating different fields
Jukka Paasonen

All the functions return true if the value of the given element is valid.

	checkRunner(element, type) is the main runner to change the background color

	checkEmpty(element)
	checkEmail(element)
	checkAlphanum(element)
	checkNumber(element)
	checkDate(element, separator)
	checkFileSuffix(element)

*/

var allowedSuffix = new Array('doc', 'pdf', 'odt', 'rtf', 'txt');

function checkRunner(element, kind) {
	//alert(element.name + ", " + kind);
	var res;

	// By default just set the "kind" empty.
	if (!kind)
		kind = "";

	switch (kind) {
		case 'email'	: res = checkEmail(element);		break;
		case 'date'		: res = checkDate(element, ".");	break;
		case 'number'	: res = checkNumber(element);		break;
		case 'alpha'	: res = checkAlphanum(element);		break;
		case 'file'		: res = checkFileSuffix(element);	break;
		default			: res = checkEmpty(element);		break;
	}

	// Those fields which are required, should have value filled and correct format.
	// Those which are not required, should have correct format only if the value is not empty.
	
	if (res) {
		element.style.backgroundColor = "#FFFFFF";
	}
	else {
		if (element.getAttribute('required') == '1' || element.value != '') {
			element.style.backgroundColor = "#CC3333";
		}
	}
}

// --------------------------

function checkEmpty(element) {
	if (element.value != "") {
		return true;
	}
	else {
		return false;
	}		
}
function checkEmail(element) {
	if (element.value.match(/^[a-zA-Z_0-9._-]+[@]{1}[a-zA-Z_0-9._-]+[.]+[a-zA-Z_0-9]{2,5}$/)) {
		return true;
	}
	return false;
}
function checkAlphanum(element) {
	if (element.value.search(/[a-zA-Z_0-9._-]/) > -1) {
		return true;
	}
	return false;
}
function checkNumber(element) {
	if (element.value.search(/0-9/) > -1) {
		return true;
	}
	return false;
}
function checkDate(element, separator) {
	var monthLength = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	var yearRange = new Array(1900, 2100);
	// Default separator
	if (!separator)
		separator = "/";

	if (element.type == "text" || element.type == "textarea") {
		var pizza = element.value.split(separator);
		if (pizza.length < 3)
			return false;
		var day = pizza[0];
		var month = pizza[1];
		// Do not include hours etc in year...
		var year = pizza[2];
		if (year.length > 4) {
			var temp = year.split(" ");
			var year = temp[0];
		}
	}
	else {
		return false;
	}

	// First check that all variables are set
	if (!day || !month || !year)
		return false;

	// Check if we are on a Running year
	if (year%4 == 0)
		monthLength[1] = 29;

	// Is the month correct?
	if (month < 1 || month > 12)
		return false;

	// Is the day correct?
	if (day > monthLength[month-1])
		return false;

	// Is the year correct?
	if (year.length != 4)
		return false;
	if (year < yearRange[0])
		return false;
	if (year > yearRange[1])
		return false;

	// So far seems ok...
	return true;
}
function checkFileSuffix(element) {
	var ext = element.value.split(".").pop();
	for (var i in allowedSuffix) {
		if (allowedSuffix[i] == ext) {
			return true;
		}
	}
	return false;
}
// Form submitter. element must be form
function submitForm() {
	var element = document.forms["formula"];
	// Before submitting, check all required fields.
	var submitOk = true;
	var inputs = element.getElementsByTagName('input');
	for (var i = 0; i < inputs.length; i++) {
		if (inputs[i].getAttribute('type') == 'text' && inputs[i].getAttribute('required') == '1') {
			if (!checkEmpty(inputs[i])) {
				submitOk = false;
				inputs[i].style.backgroundColor = "#CC3333";
			}
		}
	}
	var textareas = element.getElementsByTagName('textarea');
	for (var i = 0; i < textareas.length; i++) {
		if (textareas[i].getAttribute('required') == '1') {
			if (!checkEmpty(textareas[i])) {
				submitOk = false;
				textareas[i].style.backgroundColor = "#CC3333";
			}
		}
	}

	if (submitOk) {
		// Finally submit the form
		element.submit();
	}
	else {
		alert("Please fill in all the required fields");
	}
}

function enableEmploymentAdditional(element) {
	if (element.checked) {
		if (element.value == "2") {
			element.form.employmentadditional2.disabled = false;
			element.form.employmentadditional6.disabled = true;
		}
		else if (element.value == "6") {
			element.form.employmentadditional6.disabled = false;
			element.form.employmentadditional2.disabled = true;
		}
		else {
			element.form.employmentadditional6.disabled = true;
			element.form.employmentadditional2.disabled = true;
		}
	}
}