/* 
	This function scans all the fields of a form and, depending on the value of their 'id' (that can be, for
	example, 'mandatory' or 'email') calls the appopriate function for checking that the field is correct
	Last modified: 05/09/2005
	By: Silvio Porcellana
*/

	function check_form(form) {
		var return_false = false;
		var checks = new Array;				// Hash where keys are all the checks that we need to perform
		var j = 0;
		for (var i = 0; i < form.elements.length; i++) {
			var ids_array = form.elements[i].id.split("&");
			for (var x = 0; x < ids_array.length; x++) {
				if (ids_array[x] != "") {
					if (! checks[ids_array[x]]) {
						eval("var " + ids_array[x] + " = new Array");
						checks[ids_array[x]] = 1;		// ...and here all the checks ('mandatory', etc.)
					}
					eval(ids_array[x] + "[j++] = form.elements[i].name");		// here we get all the fields names...
				}
			}
		}
		for (var c in checks) {
			eval("var length = " + c + ".length");		// the length of the 'checks' array
			for (var i = 0; i < length; i++) {
				eval("var field = " + c + "[i]");
				if (field == null) {
					continue;
				}
				var args_array = c.split("Z");
				// here we call the appopriate function
				eval("var failed_msg = " + args_array[0] + "_failed(form.elements['" + field + "'], " + args_array[1] + ")");	
				if (failed_msg) {		// if function returns true we focus on the form field
					// eval("alert(form." + field + ".className)");
					eval("form.elements['" + field + "'].focus()");
					eval("form.elements['" + field + "'].className = form.elements['" + field + "'].className + ' input_red'");
					return_false = true;
					return false;
				} else {
					eval("form.elements['" + field + "'].className = form.elements['" + field + "'].className.replace(/input_red/, '')");
				}
			}
		}

		for (var i = 0; i < form.elements.length; i++) {
			if (form.elements[i].type == 'submit') {
				form.elements[i].disabled = true;
				form.elements[i].value = '...wait...';
			}
		}

//		if (return_false) {
//			return false;
//		}
	}

	function mandatory_failed(field) {
		if (field.value == "") {
			if (field.title) {
				alert(alert_missing_mandatory_field + " " + field.title);
			} else {
				alert(alert_missing_mandatory_field + " " + field.name.toUpperCase());
			}
			return true;
		}
		return false;
	}

	function email_failed(field) {
		if (! emailCheck(field.value)) {
			return true;
		}
		return false;
	}

	function equal_failed(field, id_other_field) {
		return false;
	}

	function decimal_failed(field) {
		var allowed = '0123456789.';
		for (var i = 0; i < field.value.length; i++) {
			if (allowed.indexOf(field.value.charAt(i)) == -1) {
				if (field.title) {
					alert(alert_invalid_decimal_field + " " + field.title);
				} else {
					alert(alert_invalid_decimal_field + " " + field.name.toUpperCase());
				}
				return true;
			}
		}
		return false;
	}

	function number_failed(field) {
		var allowed = '0123456789-.,';
		for (var i = 0; i < field.value.length; i++) {
			if (allowed.indexOf(field.value.charAt(i)) == -1) {
				if (field.title != '') {
					alert(alert_invalid_number_field + " " + field.title);
				} else {
					alert(alert_invalid_number_field + " " + field.name.toUpperCase());
				}
				return true;
			}
		}
		return false;
	}

	function only_numbers_failed(field) {
		var allowed = '0123456789';
		for (var i = 0; i < field.value.length; i++) {
			if (allowed.indexOf(field.value.charAt(i)) == -1) {
				if (field.title != '') {
					alert(alert_invalid_number_field + " " + field.title);
				} else {
					alert(alert_invalid_number_field + " " + field.name.toUpperCase());
				}
				return true;
			}
		}
		return false;
	}

	function mobile_failed(field) {
		var allowed = '0123456789';
		for (var i = 0; i < field.value.length; i++) {
			if (allowed.indexOf(field.value.charAt(i)) == -1) {
				if (field.title != '') {
					alert(alert_invalid_mobile_field + " " + field.title);
				} else {
					alert(alert_invalid_mobile_field + " " + field.name.toUpperCase());
				}
				return true;
			}
		}
		return false;
	}

	function min_failed(field, min_value) {
		if (field.value < min_value) {
			if (field.title != '') {
				alert(alert_wrong_min_field + " " + field.title + " (min " + min_value + ") ");
			} else {
				alert(alert_wrong_min_field + " " + field.name.toUpperCase() + " (min " + min_value + ") ");
			}
			return true;
		}
		return false;
	}

	function max_failed(field, max_value) {
		if (field.value > max_value) {
			if (field.title != '') {
				alert(alert_wrong_max_field + " " + field.title + " (max " + (max_value - 1) + ") ");
			} else {
				alert(alert_wrong_max_field + " " + field.name.toUpperCase() + " (max " + (max_value - 1) + ") ");
			}
			return true;
		}
		return false;
	}

	function maxlen_failed(field, max_len) {
		if (field.value.length > max_len) {
			if (field.title != '') {
				alert(alert_wrong_maxlen_field + " " + field.title + " (max " + max_len + ") ");
			} else {
				alert(alert_wrong_maxlen_field + " " + field.name.toUpperCase() + " (max " + max_len + ") ");
			}
			return true;
		}
		return false;
	}

	function integer_failed(field) {
		var allowed = '0123456789-';
		for (var i = 0; i < field.value.length; i++) {
			if (allowed.indexOf(field.value.charAt(i)) == -1) {
				if (field.title != '') {
					alert(alert_invalid_integer_field + " " + field.title);
				} else {
					alert(alert_invalid_integer_field + " " + field.name.toUpperCase());
				}
				return true;
			}
		}
		return false;
	}

	function onblur_username_failed(field) {
		request = loadXMLDoc('ajax/check_username.php?username=' + field.value, 'ajax_return');
	}
	function username_failed(field) {
		if (document.getElementById('ajax_return').innerHTML != "") {
			alert(document.getElementById('ajax_return').innerHTML);
			return true;
		}
		return false;
	}


	function no_profanity_failed(field) {
		return false;
	}


	function onblur_captcha_failed(captcha_input, captcha_string) {
		request = loadXMLDoc('ajax/check_captcha.php?captcha_input=' + captcha_input.value + '&captcha_string=' + captcha_string, 'ajax_return');
	}
	function captcha_failed(field) {
		if (document.getElementById('ajax_return').innerHTML != "") {
			alert(document.getElementById('ajax_return').innerHTML);
			return true;
		}
		return false;
	}