/* -[ Information ]----------------------------------------------------------------------------
	Form validator
	Version: 1.2
	Last changes:
	- Firefox compatibility
	- Set focus to formfield when clicking the erroricon
	- Compare functionality added
	--------------------------------------------------------------------------------------------*/
	// mighty constructor
	function formValidator(formName, formFields)
	{
		this.formName = formName;
		this.arrElements = new Array(formFields);
		this.arrCompareElements = new Array(formFields);
		this.total = 0;
		this.totalCompare = 0;
		this.errorContainer = '';
		this.errorIcon = 'themes/default/images/error.gif';
	}
	formValidator.prototype.SetErrorIcon = function(iconurl)
	{
		this.errorIcon = iconurl;
	}
	// add new check
	formValidator.prototype.add = function(element_id, element_name, element_data, min_length, max_length, required)
	{
		// temporary array for storing all element information
		var tmpArray = new Array(5);
		tmpArray[0] = element_id;
		tmpArray[1] = element_name;
		tmpArray[2] = element_data;
		tmpArray[3] = min_length;
		tmpArray[4] = max_length;
		tmpArray[5] = required;
		// add element to element array
		this.arrElements[this.total] = tmpArray;
		this.total++;
	}
	formValidator.prototype.addCompare = function(element_id1, element_name1, element_id2, element_name2)
	{
		var tmpArray = new Array(3);
		tmpArray[0] = element_id1;
		tmpArray[1] = element_name1;
		tmpArray[2] = element_id2;
		tmpArray[3] = element_name2;
		this.arrCompareElements[this.totalCompare] = tmpArray;
		this.totalCompare++;
	}
	// validate all elements on required, contenttype and length
	formValidator.prototype.validate = function()
	{
		blResult = false;
		this.clearErrors();

		var blRequired = this.checkRequired();
		var blContenttype = this.checkContenttype();
		var blLength = this.checkLength();
		var blCompare = this.checkCompare();

		if (blRequired == true && blContenttype == true && blLength == true && blCompare == true)
		{
			return true;
		}
		else
		{
			tmp = this.arrElements[0];
			if (document.getElementById(tmp[0]))
			{
				if (document.getElementById(tmp[0]).type == "text")
				{
					document.getElementById(tmp[0]).focus()
				}
			}
			return false;
		}
	}
	// Compares two fields
	formValidator.prototype.checkCompare = function()
	{
		var blResult = true;
		var tmp, i;
		for (i = 0; i < this.totalCompare; i++)
		{
			tmp = this.arrCompareElements[i];
			if (document.getElementById(tmp[0]) && document.getElementById(tmp[2]))
			{
				if (document.getElementById(tmp[0]).value != document.getElementById(tmp[2]).value)
				{
					blResult = false;
					this.setError(tmp[0], 'Dit veld komt niet overeen met ' + tmp[3]);
					this.setError(tmp[2], 'Dit veld komt niet overeen met ' + tmp[1]);
					document.getElementById(tmp[0]).value = document.getElementById(tmp[0]).value.trim();
					document.getElementById(tmp[2]).value = document.getElementById(tmp[2]).value.trim();
				}
			}
		}
		return blResult;
	}
	// checks each field which is required whether it's blank or not.
	formValidator.prototype.checkRequired = function()
	{
		var blResult = true;
		var tmp, i;
		for (i = 0; i < this.total; i++)
		{
			tmp = this.arrElements[i];
			if (tmp[2] != 'checkbox')
			{
				if (document.getElementById(tmp[0]) && tmp[5] == true)
				{
					if (document.getElementById(tmp[0]).value.trim() == "")
					{
						blResult = false;
						this.setError(tmp[0], 'Dit veld is verplicht.');
						document.getElementById(tmp[0]).value = document.getElementById(tmp[0]).value.trim();
					}
				}
			}
			else
			{
				if (!document.getElementById(tmp[0]).checked)
				{
					blResult = false;
					this.setError(tmp[0], 'U dient dit veld te selecteren.');
				}
			}
		}
		return blResult;
	}
	// checks each field on it's length
	formValidator.prototype.checkLength = function()
	{
		var blResult = true;
		var tmp, i;
		for (i = 0; i < this.total; i++)
		{
			tmp = this.arrElements[i];
			if (document.getElementById(tmp[0]))
			{
				if (document.getElementById(tmp[0]).value.trim() != "" && tmp[5] == true)
				{
					var tmpStr = document.getElementById(tmp[0]).value.trim();
					// too small
					if (tmpStr.length < tmp[3])
					{
						blResult = false;
						this.setError(tmp[0],'De invoer is te kort (' + tmpStr.length + ' teken(s)) . Deze moet minimaal ' + tmp[3] + ' tekens lang zijn.');
					}
					// too large
					else if (tmpStr.length > tmp[4])
					{
						blResult = false;
						this.setError(tmp[0], 'De invoer is te lang (' + tmpStr.length + ' teken(s)) . Deze mag maximaal ' + tmp[4] + ' tekens lang zijn.');
					}
				}
			}
		}
		return blResult;
	}
	// checks each field for valid content (email, url etc etc)
	formValidator.prototype.checkContenttype = function()
	{
		var blResult = true;
		var tmp, i;
		for (i = 0; i < this.total; i++)
		{
			tmp = this.arrElements[i];

			if (tmp[2] != 'checkbox')
			{
				if (document.getElementById(tmp[0]))
				{
					if (document.getElementById(tmp[0]).value.trim() != "")
					{
						switch (tmp[2])
						{
							case 'email':
							case 'e-mail':
								if (!this.isValidEmail(document.getElementById(tmp[0]).value.trim()))
								{
									blResult = false;
									this.setError(tmp[0], 'Voer een geldig emailadres in.');
								}
								break;
							case 'url':
							case 'uri':
							case 'website':
								if (!this.isValidUrl(document.getElementById(tmp[0]).value.trim()))
								{
									blResult = false;
									this.setError(tmp[0], 'Voer een geldige URL in.');
								}
								break;
							case 'date':
							case 'datum':
								if (!this.isValidDate(document.getElementById(tmp[0]).value.trim()))
								{
									blResult = false;
									this.setError(tmp[0], 'De ingevoerde datum is ongeldig. Voer de datum alsvolgt in: \'dd-mm-jjjj\'');
								}
								break;
							case 'nummer':
							case 'number':
							case 'nr':
								if (!this.isNumeric(document.getElementById(tmp[0]).value.trim()))
								{
									blResult = false;
									this.setError(tmp[0], 'Dit veld moet numeriek zijn.');
								}
								break;
						}
					}
				}
			}
		}
		return blResult;
	}
	formValidator.prototype.setErrorContainer = function(obj_id)
	{
		if (document.getElementById(obj_id)) { this.errorContainer = document.getElementById(obj_id); }
	}
	formValidator.prototype.isValidEmail = function(s)
	{
		return (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/).test(s);
	}
	formValidator.prototype.isValidUrl = function(s)
	{
		var urlPattern = (/^(?:(?:ftp|https?):\/\/)?(?:[a-z0-9](?:[-a-z0-9]*[a-z0-9])?\.)+(?:com|edu|biz|org|gov|int|info|mil|net|name|museum|coop|aero|[a-z][a-z])\b(?:\d+)?(?:\/[^;"'<>()\[\]{}\s\x7f-\xff]*(?:[.,?]+[^;"'<>()\[\]{}\s\x7f-\xff]+)*)?/);
		return urlPattern.test(s.toLowerCase());
	}
	formValidator.prototype.isNumeric = function(s) { return !isNaN(s); }
	formValidator.prototype.isValidDate = function(s)
	{
		var blResult = false;
		var temp = s.split("-");
		if (temp.length == 3)
		{
			blResult = true;
			var iDay = temp[0];
			var iMonth = temp[1];
			var iYear = temp[2];
			if (iYear.length != 2 && iYear.length != 4) { blResult = false; }
			if (iMonth.length != 1 && iMonth.length != 2) { blResult = false; }
			if (iDay.length != 1 && iDay.length != 2) { blResult = false; }
			if (iMonth < 1 || iMonth > 12) { blResult = false; }
			var iDayFeb = (((iYear % 4 == 0) && ( (!(iYear % 100 == 0)) || (iYear % 400 == 0))) ? 29 : 28 );
			if ((iMonth == 2 && iDay > iDayFeb) || iDay > 31) { blResult =  false; }
			if (!iYear.isNumeric(iYear)) { blResult = false; }
		}
		return blResult;
	}
	formValidator.prototype.isEmpty = function(s) { return (s.trim() == ""); }
	formValidator.prototype.IsText = function(s) { return typeof s == 'string'; }
	String.prototype.isNumeric = function(s)
	{
		var ValidChars = "0123456789.";
   	var IsNumber=true;
   	var Char;
		for (i = 0; i < s.length && IsNumber == true; i++)
		{
      Char = s.charAt(i);
			if (ValidChars.indexOf(Char) == -1)
			{
				IsNumber = false;
			}
		}
		return IsNumber;
	}
	String.prototype.trim = function()
	{
		var i = 0, j = this.length;
  		while (i < j && this.charAt(i) <= ' ') i++;
  		while (j > i && this.charAt(j - 1) <= ' ') j--;
  		return this.substring(i, j);
	}

	formValidator.prototype.setError = function (obj_id, errormessage)
	{
		// remove previous error if exists...
		if (document.getElementById(obj_id + '_error'))
		{
			document.getElementById(obj_id).parentNode.removeChild(document.getElementById(obj_id + '_error'));
		}
		var errormsg = document.createElement('span');
		errormsg.id = obj_id + '_error';
		errormsg.style.font = 'normal 12px arial';
		errormsg.style.color = '#ff0000';

		var erroricon = document.createElement('img');
		erroricon.id = obj_id + '_errorimg';
		erroricon.style.cursor = 'help';
		erroricon.src = this.errorIcon;
		erroricon.style.width = '16px';
		erroricon.style.height = '16px';
		erroricon.alt = errormessage;
		erroricon.title = errormessage;
		erroricon.onclick = function()
		{
			if (document.getElementById(obj_id).type == "text" || 
					document.getElementById(obj_id).type == "password") { document.getElementById(obj_id).focus() }
		}

		errormsg.appendChild(erroricon);
		var obj = document.getElementById(obj_id);
   	obj.parentNode.insertBefore(errormsg, obj.nextSibling);
   	//obj.focus();
	}
	formValidator.prototype.clearErrors = function()
	{
		for (i = 0; i < this.total ;i++)
		{
			tmp = this.arrElements[i];

			if (document.getElementById(tmp[0] + '_error'))
			{
				document.getElementById(tmp[0]).parentNode.removeChild(document.getElementById(tmp[0] + '_error'));
			}
		}
	}
