/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	Page.js - intrinsic form validation setup 
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	(c) Copyright 2004, Aleksandar Vacic, aleck@sezampro.yu, www.aplus.co.yu 
	## This work is licensed under the Creative Commons Attribution-ShareAlike License.
	## To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/1.0/ or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/

window.onload = function () {
	FV_SetupForm("avonform");
}


/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	setup mandatory form elements for proper validation 
	XField = do not process
	sField = process as string
	nField = process as number
	bField = process as boolean (checkbox and radio state)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
function FV_SetupForm(vForm){
	if (typeof(vForm) == "string")
		oForm = document.forms[vForm];
	else
		oForm = vForm;

	//	ditch non-supporting browsers
	if (!oForm.parentNode) return;

	//	set onsubmit validation function
	oForm.onsubmit = function() {
		return FV_FormVal(this);
	};

	//	flag. if true, alert box will be shown on bad input 
	oForm.alertuser = false;

	//	flag to prevent multiple submits
	oForm.submitted = false; 

	var oElems = oForm.elements; 
	var nLen = oElems.length;
	var sTmp, oTmp, sTmp2;
	for (var i=0; i<nLen; i++) {
		oTmp = oElems[i];
		//	skip elements that don't have names (usually fieldset)
		if (typeof(oTmp.name) == "undefined") continue;

		sTmp2 = oTmp.name;
		sTmp = sTmp2.charAt(0).toLowerCase();
		//	process elements with names starting with b, s and n 
		if (sTmp != "b" && sTmp != "s" && sTmp != "n") continue;

		switch(oTmp.type) {
			case "text": case "select-one": case "password": case "textarea": 
				oLabels = oTmp.parentNode.getElementsByTagName("label");
				for (var j=0;j<oLabels.length;j++) {
					if (oLabels[j].htmlFor == sTmp2) {
						oTmp.valme = function() {
							var oDiv = this.parentNode;

							if (this.type == "text" || this.type == "password" || this.type == "textarea")
								this.value = FV_Trim(this.value);

							bOk = FV_FieldVal(this);

							if (bOk)
								oDiv.className += " goodinput";
							else if (oDiv.className.indexOf("badinput") == -1) {
								oDiv.className += " badinput";
							}
							return bOk;
						};
						oTmp.onblur = oTmp.valme;
						break;			
					} 		
				}
				break; 		

			case "checkbox":				
				oLabel = oTmp.parentNode;
				if (oLabel.className.indexOf("mandat") != -1) {
	  				oTmp.valme = function() {
	  					var oDiv = this.parentNode.parentNode;
	
	  					var bOk = this.checked;
	
	  					if (bOk)
	  						oDiv.className = oDiv.className.replace("badinput", "goodinput");
	  					else if (oDiv.className.indexOf("badinput") == -1) {
	  						oDiv.className += " badinput";
	  						if (this.form.alertuser) FV_ShowMessage("This checkbox is mandatory.");
	  						this.form.alertuser = false;
	  						this.focus();
	  					}
	  					return bOk;
	  				};
					oTmp.onblur = oTmp.valme;
				}
				break; 		

			case "radio":
				oLabel = oTmp.parentNode;
				if (oLabel.className.indexOf("mandat") != -1) {
					oTmp.valme = function() {
						var oDiv = this.parentNode.parentNode;

						var oRadios = this.form.elements[this.name];
						var bOk = false;
						for (var r=0;r<oRadios.length;r++) {
							if (oRadios[r].checked) {
								bOk = true; 
								break; 
							}
						}

						if (bOk)
							oDiv.className = oDiv.className.replace("badinput", "goodinput");
						else if (oDiv.className.indexOf("badinput") == -1) {
							oDiv.className += " badinput";
							if (this.form.alertuser) FV_ShowMessage("Please choose one of the options.");
							this.form.alertuser = false;
							this.focus();
						}
						return bOk;
					};
					oTmp.onblur = oTmp.valme;
				} 		
				break; 		
				
		}//switch oTmp.type
	}
}

/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	validation handler		
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
function FV_FieldVal(oField) {
	var bOk = true;

	var sFldName = oField.name;
	var oForm = oField.form;
	var FldName = sFldName.substring(0,0)+sFldName.substring(1);
	
	var sType = sFldName.charAt(0).toLowerCase();
	switch(sType) {
		case "s":
			switch(sFldName) {
				case "sEmail":
					bOk = FV_IsEmail(oField.value);
					if (!bOk) {
						if (oForm.alertuser) {
							FV_ShowMessage("Please enter correct e-mail address.");
							oForm.alertuser = false;
							oField.focus();
						}
						break;
					}
					break;

				default:
					bOk = !FV_IsBlank(oField.value);
					if (!bOk) {
						if (oForm.alertuser) {
							FV_ShowMessage(FldName + " is mandatory information. Please enter.");
							oForm.alertuser = false;
							oField.focus();
						}
						break;
					}
			}
			break;

		case "n":
			switch(sFldName) {
				case "nDobday": case "nDobmonth": case "nDobyear":
					var oDay = oForm.elements["nDobday"];
					var oMonth = oForm.elements["nDobmonth"];
					var oYear = oForm.elements["nDobyear"];
					var sDay = FV_Trim(oDay.value);
					var sMonth = FV_Trim(oMonth.value);
					var sYear = FV_Trim(oYear.value);
					//	check for all parts
					if (sDay == "" || sYear == "") {
						//	if this is form submit, then inform user about the missing field 
						if (oForm.alertuser) {
							bOk = false;
							FV_ShowMessage("Please fill-in all date fields.");
							oForm.alertuser = false;
							(sDay == "") ? oDay.focus() : oYear.focus();
							break;
						//	user is in process of entering data, so just return without validating date until all is entered, but keep the marker about invalid date  
						} else {
							bOk = false;
							break;
						}
					}
					bOk = FV_IsValidDate(sYear, sMonth, sDay, 1);
					if (!bOk) {
						if (oForm.alertuser) {
							FV_ShowMessage("Please enter correct date.");
							oForm.alertuser = false;
							oField.focus();
						}
						break;
					}
					break;

				default:
					bOk = (FV_IsFloat(oField.value) || FV_IsInteger(oField.value));
					if (!bOk) {
						if (oForm.alertuser) {
							FV_ShowMessage("Please enter correct numeric value in the field " + FldName);
							oForm.alertuser = false;
							oField.focus();
						}
					}
			}
			break;
	}

	return bOk;
}

/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	message display		
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
function FV_ShowMessage(sMes) {
	if (sMes != "")
		alert(sMes);
}

/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	global validation function	
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
function FV_FormVal(oForm){
	var sTmp, oTmp, bOk = true;

	//	prevent multiple clicks on submit
	if (oForm.submitted)
		return false;

	//	inform user about mistakes
	oForm.alertuser = true;

	var oElems = oForm.elements; 
	var nLen = oElems.length;

	for (var i=0; i<nLen; i++) {
		oTmp = oElems[i]; 
		if (typeof(oTmp.valme) == "undefined") continue;
		bOk = oTmp.valme();
		if (!bOk) break; 
	}

	if (bOk) {
		oForm.submitted = true;
		oForm.elements["xSubmit"].disabled = true; 
	} 

	return bOk;
}


