/* 
	Validation.js
	A collection of functions for form validation.
	
		validateReqFields(form) -  A function for validating filling of all the required fields in 
				the given form. It assumes that all the	requied fields have labels with the "required" 
				css class, and that these fields are not disabled. Every such field should also have a 
				div for the error message with id="e-<fieldname>-req" . It also validates the date if the
				class of the input is "date". Returns whether the validation succeeded. NOTE: Every field
				in the form must have an id identical to its name (and to the value of the "for" field of
				the its label) for this function to work!
		
		validateRegExp(value, regexp, errordiv) - Gets a text input object, and returns whether its 
				value has the form of the regular expression, and if not, turns on the error.
		
		validateNumber(field, integer) - Gets a text input object, and returns whether it contains a 
				number. The second argument is a flag indicating whether the number has to be an integer 
				(not contain a decimal point). If the field does not contain a number, and has a div 
				with an error message with id="e-<fieldname>-num", the div will be displayed.
				
		validatePhone(field) - Gets a text input object and returns whether it contains a valid phone 
				number. The required form is a 3-digit area code (possibly surrounded by paranthesis),
				then a 7-digit number, possibly sepapated from the area code by a space or a dash, and
				possibly including a similar separation (not necessarily consistant with the first one)
				after the first 3 digits.
				
		validatePhone(field) - Gets a text input object and returns whether it contains a valid email 
				address. The required form is a string of the form name@domain.
*/

function validateReqFields(form) {
	var formOK = true;
	var e, df;
	var foc = null;
	
	// get the array of labels:
	var labels = document.getElementsByTagName("label");
	
	for (var i=0; i<labels.length; i++) {
		// if the label is bold then the field is required:
		var l = labels[i];
		if (l.htmlFor == "")
			// a label with no field:
			continue;
			
		var f = document.getElementById(l.htmlFor);
		
		if (f.form == form) {
			// if the field is in the relevant form: 
			
			if ((f.type == "text") || (f.type == "password"))
				while ((f.value.length > 0) && (f.value.charAt(0) == " ")) 
					f.value = f.value.substring(1, f.value.length);
			
			if ((l.className == "required") && (!f.disabled)) {
				// if the field is a text/password field, we trim spaces from the beginning:
				
				e = document.getElementById("e-" + f.name + "-req");
				if (f.value.length == 0) {
					// field is empty, color the label red and display the required field error:
					formOK = false;
					l.style.color = "red";
					if (e != null) 
						e.className = "show-error";
					if (foc == null) 
						foc = f;
				}
				else {
					// field has value, make sure the label is black and required field error is hidden:
					l.style.color = "black";
					e.style.visibility="hidden";
				}
			}			
			// focus on the first problematic field
			if (foc != null)
				foc.focus();
		}
	}
	return formOK;
}



function validateRegExp(field, regexp, errordiv) {
	var formOK = true;
	
	if (field.value.length == 0)
		// field is empty, we don't check this:
		return true;
		
	while ((field.value.length > 0) && (field.value.charAt(0) == " ")) 
		field.value = field.value.substring(1, field.value.length);
	
	formOK =  (field.value.match(regexp) != null);
	
	if (errordiv != null)
		errordiv.className = formOK ? "hide-error" : "show-error";
		
	return formOK;
}



function validatePhone(field) {	
	var phoneRE = new RegExp("\^(\\d{3}|\\(\\d{3}\\))[\\s\\-]?\\d{3}[\\s\\-]?\\d{4}\\s*$");
	var phoneError = document.getElementById("e-" + field.name + "-phone");	
	
	return validateRegExp(field, phoneRE, phoneError);
}



function validateNumber(field, integer) {
	var numRE = integer ? new RegExp("\^\\d*\\s*$") : new RegExp("\^\\d+(.\d+)?\\s*$");
	var emailError = document.getElementById("e-" + field.name + "-num");	
	
	return validateRegExp(field, emailRE, emailError);
}



function validateEmail(field) {
	var emailRE = new RegExp("\^[\\w\\.]+@[\\w\\.]+\\.\\w+\\s*$");
	var emailError = document.getElementById("e-" + field.name + "-email");	
	
	return validateRegExp(field, emailRE, emailError);
}
