/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	FormValidation.js - library contains code to validate HTML form-field data.
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	The bulk of this file was derived from Eric Krock's FormCheck.js at developer.netscape.com/docs/examples/javascript/formval/overview.html
	(Many thanks, Eric!) Enjoy ... and please maintain this header. Also thanks to Rick Scott who further developed this library
	(c) 1997 Netscape Communications Corporation
	(c) Copyright 2000, Rick Scott.
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	(c) Copyright 2002, 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
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	- added "namespace" FV for easier integration for other scripts
	- removed some string-munching functions from Rick's version
	- some functions renamed to shorter names (like StripLeadingTrailingBlanks to Trim)
	- removed all checks for letters since it works only with English language. Language independent functions needed. Time (for it) needed.
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	stripped-down version.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/

var FV_sDigits = "0123456789";
var FV_sBlanks = " \t\n\r";	// aka whitespace chars
var FV_sDecimalPointDelimiter = "."; // decimal point character differs by language and culture
/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	Returns true if string s is empty
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
function FV_IsEmpty(s){
	return ((s == null) || (s.length == 0));
}


/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	Returns true if string s is empty or all blank chars
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
function FV_IsBlank(s){
	var i;
	var c;

	// Is s empty?
	if (FV_IsEmpty(s))
		return true;

	// Search through string's chars one by one until we find first non-blank char, then return false; if we don't, return true
	for (i=0; i<s.length; i++){	 
		// Check that current character isn't blank
		c = s.charAt(i);
		if (FV_sBlanks.indexOf(c) == -1) 
			return false;
	}

	// All characters are blank
	return true;
}


/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	Removes all characters which do NOT appear in string bag from string s
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
function FV_StripCharsNotInBag(s, bag){
	var i;
	var c;
	var returnString = "";

	// Search through string's characters one by one; if character is in bag, append to returnString
	for (i = 0; i < s.length; i++){	 
		// Check that current character isn't blank
		c = s.charAt(i);
		if (bag.indexOf(c) != -1) 
			returnString += c;
	}

	return returnString;
}


/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	Removes leading blank chars (as defined by FV_sBlanks) from s
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
function FV_LTrim(s){ 
	var i = 0;
	while ((i < s.length) && (FV_sBlanks.indexOf(s.charAt(i)) != -1))
		 i++;
	return s.substring(i, s.length);
}


/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	Removes trailing blank chars (as defined by FV_sBlanks) from s
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
function FV_RTrim(s){ 
	var i = s.length - 1;
	while ((i >= 0) && (FV_sBlanks.indexOf(s.charAt(i)) != -1))
		 i--;
	return s.substring(0, i+1);
}


/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	Removes leading+trailing blank chars (as defined by FV_sBlanks) from s
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
function FV_Trim(s){ 
	s = FV_LTrim(s);
	s = FV_RTrim(s);
	return s;
}


/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	Returns true if character c is a digit (0 .. 9)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
function FV_IsDigit(c){
	return ((c >= "0") && (c <= "9"));
}


/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	Returns true if all chars in string s are numbers;
	first character is allowed to be + or -; does not 
	accept floating point, exponential notation, etc.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
function FV_IsInteger(s){
	if (FV_IsBlank(s))
		return false;

	// skip leading + or -
	if ((s.charAt(0) == "-") || (s.charAt(0) == "+"))
		var i = 1;
	else
		var i = 0;

	// Search through string's chars one by one until we find a non-numeric char, then return false; if we don't, return true
	var c;
	for (i; i<s.length; i++){	 
		// Check that current character is number
		c = s.charAt(i);
		if (!FV_IsDigit(c)) 
			return false;
	}

	// All characters are numbers
	return true;
}


/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	Returns true if string s is an integer such that a <= s <= b
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
function FV_IsIntegerInRange(s, a, b){ 
	if (FV_IsBlank(s)) 
		return false;
	if (!FV_IsInteger(s)) 
		return false;
	var num = parseInt(s, 10);
	return ((num >= a) && (num <= b));
}


/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	True if string s is an unsigned floating point (real) number; first character is allowed to be + or -; no exponential notation.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
function FV_IsFloat(s){ 
	var seenDecimalPoint = false;

	if (FV_IsBlank(s)) 
		return false;
	if (s == FV_sDecimalPointDelimiter) 
		return false;

	// skip leading + or -
	if ((s.charAt(0) == "-") || (s.charAt(0) == "+"))
		var i = 1;
	else
		var i = 0;

	// Search through string's chars one by one until we find a non-numeric char, then return false; if we don't, return true
	var c;
	for (i; i<s.length; i++){	 
		// Check that current character is number
		c = s.charAt(i);

		if ((c == FV_sDecimalPointDelimiter) && !seenDecimalPoint) 
			seenDecimalPoint = true;
		else if (!FV_IsDigit(c)) 
			return false;
	}

	// All characters are numbers
	return seenDecimalPoint;
}


/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	Returns true if string is a valid email address: @ and . required, at least one char before @, at least one char before and after .
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
function FV_IsEmail(strEmail) {
	if (strEmail.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)
		return true;
	else
		return false;
}

/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	Returns year in	4-digit format (e.g., 2000, not 0 or 100) 
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
function FV_GetYear2k(date){
	var year = date.getYear();
	if (year < 1000)
		year += 1900;
	return year;
}

/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	check if this is a valid date (no Feb 31st and similar)	- argument nJan can mi omitted, when it defaults to 0
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
function FV_IsValidDate(sYear, sMonth, sDay, nJan) {
	if (nJan != 1) nJan = 0;
	var nDec = nJan + 11;
	if ( !FV_IsInteger(sYear) || !FV_IsIntegerInRange(sMonth, nJan, nDec) || !FV_IsIntegerInRange(sDay, 1, 31) )
		 return false;	

	var nDay = parseInt(sDay);
	var nMonth = parseInt(sMonth) - ((nJan == 1) ? 1 : 0);
	var nYear = parseInt(sYear);
	var oDate = new Date(nYear, nMonth, nDay);

	if ( nDay == oDate.getDate() && nMonth == oDate.getMonth() && nYear == FV_GetYear2k(oDate) )
		return true;

	return false;
}
/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	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("form");




	
}


/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	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;
}




