var cleanValidator = 
{
	init: function (settings) 
	{
		this.settings 	= settings;
		this.form	= document.getElementById(this.settings["formId"]);
		formInputs	= this.form.getElementsByTagName("input");
		
		// change color of inputs on focus
		for(i=0;i < formInputs.length; i++)
		{
			if(formInputs[i].getAttribute("type") != "submit") 
			{
				input 			= formInputs[i];
				input.style.background 	= settings["inputColors"][0];
				input.onblur 		= function () 
					{
						this.style.background = settings["inputColors"][0];
					}
				input.onfocus = function () 
					{
						this.style.background = settings["inputColors"][1];
					}
				if(formInputs[i].getAttribute("type") != "radio") 
				{
					input.style.border = '1px solid #CCCCCC';
				}
			}
		};
		this.form.onsubmit = function () 
		{
			error = cleanValidator.validate();
			if(error.length < 1) 
			{
				return true;
			} 
			else 
			{
//				cleanValidator.printError("Vult u aub alle verplichte velden in.\nDeze zijn rood gemarkeerd.");
				cleanValidator.printError(error);
				return false;
			}
		};
	},
	validate: function () {
		
		error = '';
		validationTypes = new Array("isRequired", "isEmail", "isNumeric", "isDatum", "checkBedragen", "Zipcode");

		for(n=0; n<validationTypes.length; n++) 
		{	
			var x = this.settings[validationTypes[n]];
			if(x != null) {
				for(i=0; i<x.length; i++) 
				{
					inputField = document.getElementById(x[i]);
					switch (validationTypes[n]) 
					{
						case "isRequired" :
							valid = !isRequired(inputField.value);
							errorMsg = "Verplicht veld.";
						break;
						case "isEmail" :
							valid = isEmail(inputField.value);
							errorMsg = "Geen geldig e-mail adres.";
						break;
						case "isNumeric" :
							valid = isNumeric(inputField.value);
							errorMsg = "Numeriek.";
						break;
						case "isDatum" :
							valid = isDatum(inputField.value);
							errorMsg = "Datum.";
						break;
						case "checkBedragen" :
							valid = checkBedragen(inputField.value);
							errorMsg = "Garantiebedrag mag niet groter zijn dan transactiebedrag.";
						break;
						
						case "Zipcode" :
							test1 = document.getElementById('ZipcodeNr');
							valid = !isRequired(test1.value);
							if (document.getElementById('Land').value == "0520")
							{
								test1 = document.getElementById('ZipcodeLetters');
								testis = !isRequired(test1.value);
								if (!testis)
								{
									test1.style.background = this.settings["errorColors"][0];
									test1.style.border = "2px solid "+this.settings["errorColors"][1];
									valid	= false;
								}
								else
								{
									test1.style.background = this.settings["inputColors"][0];
									test1.style.border = '1px solid #CCCCCC';
								}
							}
							errorMsg = "Zipcode niet in orde.";
						break;
						
					}
					if(!valid) 
					{
						error += errorMsg+"\n";
						inputField.style.background = this.settings["errorColors"][0];
						inputField.style.border = "2px solid "+this.settings["errorColors"][1];
					} 
					else 
					{
						inputField.style.background = this.settings["inputColors"][0];
						inputField.style.border = '1px solid #CCCCCC';
					}
				}
			}
		}
		return error;
	},
	printError: function (error) 
	{
		alert(error);
	}
};

// returns true if the string is not empty
function isRequired(str)
{
	return (str == null) || (str.length == 0);
}
// returns true if the string is a valid email
function isEmail(str)
{
	if(isRequired(str)) return false;
	var re = /^[^\s()<>@,;:\/]+@\w[\w\.-]+\.[a-z]{2,}$/i
	return re.test(str);
}
// returns true if the string only contains characters 0-9 and is not null
function isNumeric(str)
{
	if(isRequired(str)) return false;
	var re = /[\D]/g
	if (re.test(str)) return false;
	return true;
}
function isDatum(dtStr)
{
	if(dtStr.length > 0)
	{
		var dtCh		= "-";
		var pos1		= dtStr.indexOf(dtCh)
		var pos2		= dtStr.indexOf(dtCh,pos1+1)
		var strDay		= dtStr.substring(0,pos1)
		var strMonth		= dtStr.substring(pos1+1,pos2)
		var strYear		= dtStr.substring(pos2+1)
		var dateSel		= strYear + strMonth + strDay;
			
		var right_now		= new Date();
		var M 			= "" + (right_now.getMonth()+1); 
		var MM 			= "0" + M; 
		MM 			= MM.substring(MM.length-2, MM.length); 
		var D 			= "" + (right_now.getDate()); 
		var DD 			= "0" + D; 
		DD 			= DD.substring(DD.length-2, DD.length); 
		var YYYY 		= "" + (right_now.getFullYear());
		var Nu			= YYYY+MM+DD-6;
		if (dateSel <= Nu)
		{
			alert('Datum moet in de toekomst liggen');
			return false;
		}
		else
		{
			return true;
		}
	}
	return false;
}
function checkBedragen(grade)
{
	if (parseFloat(document.getElementById('Garantiebedrag').value) > grade)
	{
		gradeFormat	=  "€ " + number_format( grade, 2, ',', '.' );
		alert('Garantiebedrag te hoog.\nMaximaal toegestaan: ' + gradeFormat);
		document.getElementById('Garantiebedrag').value = "";
		
		return false;
	}
	if (parseFloat(document.getElementById('Garantiebedrag').value) > parseFloat(document.getElementById('Transactiebedrag').value))
	{
		alert('Garantiebedrag mag niet hoger zijn dan het transactiebedrag');
		document.getElementById('Garantiebedrag').value = "";
		
		return false;
	}
	return true;
}
function number_format( number, decimals, dec_point, thousands_sep ) 
{
	// *     example 1: number_format(1234.5678, 2, '.', '');
	// *     returns 1: 1234.57     
	
	var n = number, c = isNaN(decimals = Math.abs(decimals)) ? 2 : decimals;
	var d = dec_point == undefined ? "." : dec_point;
	var t = thousands_sep == undefined ? "," : thousands_sep, s = n < 0 ? "-" : "";
	var i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;
	
	return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
}