/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	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;
}

/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	check if this is a valid postcode using RPC
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
function FV_IsPostcode(postcode) {
	var REQ = SITE_PATH + "validate.asp?field=postcode&entity="+escape(postcode);
	var dom = new ActiveXObject("Microsoft.XMLDOM");
	dom.async=false;
	var http = new ActiveXObject("Microsoft.XMLHTTP");
	http.open("GET",REQ,false);
	http.send();
	var xml = http.ResponseText;
	if (xml.indexOf('0') > -1) {
		//alert('goed');
		return true; // no errors
	} else {
		//alert('niet goed');
		return false;
	}
}



/*
// formcheck
function checkform(formId) { 

	var formOK = 1;
	var msg = '';
	var res = true;
	
	var els = document.getElementsByTagName('input');
	for (var e=0; e<els.length; e++)
	{
		var curChild = els[e];

		var curChildType = curChild.getAttribute("alt");
		if (!curChildType) {
			curChildType = curChild.className;
		}
		if (curChildType) {
			if (curChildType.toLowerCase() == "required")
			{
				var curChildTagName = curChild.tagName;
				curChildTagName = curChildTagName.toLowerCase();
		
				switch (curChildTagName)
				{
					case 'input' || 'textarea' || 'text': 
						if (!curChild.value) {
							// var lbl = document.getElementById('lbl'+curChild.id);							
							// alert(lbl.innerHTML);
							// curChild.focus();							
							// lbl.className = "error_field";
							// break;
							curChild.className = "error_field";
							res = false;
							return false;
						} else {
							// var lbl = document.getElementById('lbl'+curChild.id);							
							// lbl.className = "normal";
							curChild.className = "normal";
						}

						var curChildName = curChild.getAttribute("name");
						if (res) {			
							if (curChildName.indexOf('mail') > -1) {
								// validate email
								res = FV_IsEmail(curChild.value);
								if (!res) {
									// var lbl = document.getElementById('lbl'+curChild.id);							
									// lbl.className = "error_field";
									// break;
									// alert(LABEL_BAD_EMAIL);
									curChild.className = "error_field";
									res = false;
									return false;
								} else {
									// var lbl = document.getElementById('lbl'+curChild.id);							
									// lbl.className = "normal";
									curChild.className = "normal";
								}
							} else if (curChildName.indexOf('ostcode') > -1) {
								// validate postcode
								res = FV_IsPostcode(curChild.value);
								if (!res) {
									// var lbl = document.getElementById('lbl'+curChild.id);							
									// lbl.className = "error_field";
									// break;
									// alert(LABEL_BAD_EMAIL);
									curChild.className = "error_field";
									res = false;
									return false;
								} else {
									// var lbl = document.getElementById('lbl'+curChild.id);							
									// lbl.className = "normal";
									curChild.className = "normal";
								}
							}
						}


					break;
					case 'select':						
					break;
				}
			}
		}


	}

	if (res) {
		document.getElementById(formId).submit();
	} else {
		res = false;
	}
	return res;
}
*/
