function FormScrobbler()
{
	window[(this._self = "FormScrobbler")] = this;
	//this._form = document.getElementById("theatertour");
	this._form = document.getElementsByTagName("form");
};
FormScrobbler.prototype = new klib3.dynamic;
FormScrobbler.prototype.initialize = function()
{
	if (this._form)
	{
		for (var i = 0; i < this._form.length; i++)
		{
			if (this._form[i].id == 'SearchForm')
				continue;

			this._form[i]._parent = this;
			this._form[i]._error = document.getElementById("error");
			this._form[i].onsubmit = this.submitHandler;
		}
	}
}
FormScrobbler.prototype.submitHandler = function()
{
	this._parent.onSubmit(this);
	return false;
}
FormScrobbler.prototype.onSubmit = function(oForm)
{
	this.scrobble(oForm);
	this.validate(oForm, true);
}
FormScrobbler.prototype.scrobble = function(oForm)
{
	this._aInput = oForm.getElementsByTagName("INPUT");
	this._aArea = oForm.getElementsByTagName("TEXTAREA");
	this._aSelect = oForm.getElementsByTagName("SELECT");
}

FormScrobbler.prototype.validate = function(oForm, bPost) {
    this._interface = oForm.getAttribute("action") || "/FormInterface.aspx";
    this._error = oForm._error;
    this._errors = Array();
    this._radios = Array();
    this._radiosOK = Array();
    this._oVariables = new Object();

    this._isFocussed = false;

    for (var i = 0; i < this._aInput.length; i++) {
        this.validateObject(this._aInput[i]);
    }

    for (var i = 0; i < this._aSelect.length; i++) {
        this.validateObject(this._aSelect[i]);
    }

    for (var i = 0; i < this._aArea.length; i++) {
        this.validateObject(this._aArea[i]);
    }

if (this._errors.length > 0) {
    //alert("error: " + this._errors.length); 
   // alert(this._errors[0].name);
        this._error.style.display = "block";
        this._error.innerHTML = "De onderstaande gekleurde velden zijn niet of onjuist ingevuld";
        document.getElementById("submitbutton").disabled = false;
        document.getElementById("submitbutton").value = "Verstuur";
        alert("De gekleurde velden zijn niet of onjuist ingevuld.");
        return;
    }
    else if (this._error) {
        try {
            this._error.style.display = "none";
            this._error.innerHtml = "";
        }
        catch (err) {
        }
    }

    if (bPost) {
        var oXML = new klib3.xml();
        oXML._form = oForm;
        oXML._parent = this;

        oXML.onload = function() {
            var oStatus = new Status(this);

            this._form._error.className = (oStatus.status == "OK" ? "info" : "error");
            this._form._error.innerHTML = oStatus.message;
            this._form._error.style.display = (oStatus.status != "OK" ? "block" : "none");

            if (document.getElementById("aanmelden2_2"))
                document.getElementById("aanmelden2_2").style.display = "none";

            if (document.getElementById("BackButton"))
                document.getElementById("BackButton").style.display = "none";


            if (oStatus.status == "OK") {
                if (document.getElementById("verstuurd_bedankt"))
                    document.getElementById("verstuurd_bedankt").style.display = "block";
                if (document.getElementById("message"))
                    document.getElementById("message").innerHTML = oStatus.message;
            }
        }

        oXML.post(this._interface, true, this._oVariables);
    }
}


FormScrobbler.prototype.setError = function(o)
{
	this._errors[this._errors.length] = o;
	if (o.className.indexOf("error") < 0)
		o.className += " error";
}
FormScrobbler.prototype.removeError = function(o)
{
	var i = 0;
	while (i < this._errors.length)
	{
		if (this._errors[i].name == o.name)
			this._errors.splice(i, 1);
		else
			i++;
	}
	o.className = o.className.replace(new RegExp("error\\b"), "");
}
FormScrobbler.prototype.validateObject = function(o)
{
	//if(o.className.indexOf("required") >= 0)
	if ((o.className.match(/\brequired/i)) || (!o.className.match(/\bdependentrequired/i) && o.attributes.validation))
	{

		switch (o.type)
		{
			case "text":
			case "textarea":
				if (o.value == "")
				{
					if (o.className.match(/\brequired/i))
					{
						this.focus(o);
						this.setError(o);
					}
				}
				else if (!this.validateValue(o))
				{
					this.focus(o);
					this.setError(o);
				}
				else
					this.removeError(o);
				break;
			case "select-one":
				if (o[o.selectedIndex].value < 1)
				{
					this.focus(o);
					this.setError(o);
				}
				else if (!this.validateValue(o))
				{
					this.focus(o);
					this.setError(o);
				}
				else
					this.removeError(o);
				break;
			case "radio":
				if (!o.checked && this._radiosOK.find(o.name) < 0)
				{
					this.focus(o);
					this._radios[this._radios.length] = o;
					this.setError(o.parentNode);
				}
				else
				{
					this._radiosOK[this._radiosOK.length] = o.name;
					this.removeError(o.parentNode);
					var i = 0;
					while (i < this._radios.length)
					{
						if (this._radios[i].name == o.name)
						{
							this.removeError(this._radios[i].parentNode);
							this._radios.splice(i, 1);
						}
						else
							i++;
					}
				}
				break;
			case "checkbox":
				if (!o.checked)
				{
					this.focus(o);
					this.setError(o.parentNode);
				}
				else
					this.removeError(o.parentNode);
				break;
		}
	}

	switch (o.type)
	{
		case "checkbox":
		case "radio":
			if (o.checked)
				this._oVariables[o.name] = o.value || "on";
			break;
		case "submit":
		case "button":
		case "image":
			break;
		default:
			this._oVariables[o.name] = o.value;
			break;
	}
}
FormScrobbler.prototype.focus = function(o)
{
	/**
	 **  LET OP: check of de functie focus bestaat
	 **/
	if (!this._isFocussed && this.canFocus(o) && typeof o.focus == "function" )
	{
		/*  Symptoombestrijdings noodscenario:
 		try
		{
			o.focus();
			this._isFocussed = true;
		}
		catch( e )
		{
		}
		*/

		o.focus();
		this._isFocussed = true;
	}
}
FormScrobbler.prototype.canFocus = function(o)
{
	/**
	 **  LET OP: hier is !this.isVisible( o ) aan toegevoegd, extra controle of het element niet onzichtbaar is door gedrag van een parent element
	 **/
	if (o.style.display == "none" || o.style.visibility == "hidden" || o.disabled == true || !this.isVisible( o ) )
		return false;
	return true;
};
/**
 **  LET OP: nieuwe functie isVisible, welke kijkt of het element in een display:'none', visibility:'hidden' zit (zo ja, geen focus geven!)
 **/
FormScrobbler.prototype.isVisible = function( o )
{
	while ( o.parentNode && o.nodeName.toLowerCase() != "form" )
	{
		if (o.style.display == "none" || o.style.visibility == "hidden" || o.disabled == true)
			return false;
		o = o.parentNode;
	}
	return true;
};
FormScrobbler.prototype.validateValue = function(o)
{
	if (o.attributes.validation)
	{
		switch (o.attributes.validation.value)
		{
			case 'string':
				if (o.attributes.inputlength && o.attributes.inputlength.value > 0)
					return eval("/^[a-zA-Z]{" + o.attributes.inputlength.value + "}$/").test(o.value);
				else
					return /^[A-Za-z]+$/.test(o.value);
			case 'email':
				return /^[\'0-9A-Za-z_-]+(\.[\'0-9A-Za-z_-]+)*[\.]?@([0-9A-Za-z_-]+\.)+[A-Za-z]{2,6}$/.test(o.value);
			case 'alphanum':
				if (o.attributes.inputlength && o.attributes.inputlength.value > 0)
					return eval("/^[a-zA-Z0-9]{" + o.attributes.inputlength.value + "}$/").test(o.value);
				else
					return /^[A-Za-z0-9]+$/.test(o.value);
			case 'integer':
				if (o.attributes.inputlength && o.attributes.inputlength.value > 0)
					return eval("/^-?[0-9]{" + o.attributes.inputlength.value + "}$/").test(o.value);
				else
					return /^-?[0-9]+$/.test(o.value);
			case 'posint':
				if (o.attributes.inputlength && o.attributes.inputlength.value > 0)
					return eval("/^[0-9]{" + o.attributes.inputlength.value + "}$/").test(o.value);
				else
					return /^[0-9]+$/.test(o.value);
			case 'negint':
				if (o.attributes.inputlength && o.attributes.inputlength.value > 0)
					return eval("/^-[0-9]{" + o.attributes.inputlength.value + "}$/").test(o.value);
				else
					return /^-[0-9]+$/.test(o.value);
			case 'NLphone':
				//return /^0[0-9]{9}$/.test(o.value);
			    //return /^([0|31|+31])[0-9]{1,2}([ ,-]){0,1}[0-9 ]{4,14}$/.test(o.value);
			    return /^([0]{1}[6]{1}[-\s]*[1-9]{1}[\s]*([0-9]{1}[\s]*){7})|([0]{1}[1-9]{1}[0-9]{1}[0-9]{1}[-\s]*[1-9]{1}[\s]*([0-9]{1}[\s]*){5})|([0]{1}[1-9]{1}[0-9]{1}[-\s]*[1-9]{1}[\s]*([0-9]{1}[\s]*){6})$/.test(o.value);
			case 'NLphone06':
				return /^06[0-9]{8}$/.test(o.value);
			case 'NLzip':
				return /^[1-9]{1}[0-9]{3}( ){0,1}[A-Za-z]{2}$/.test(o.value);
			case 'InternetUrl':
				return /^(http(s)?:\/\/)?([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?$/.test(o.value);
			case 'date':
				//check for valid date on day-field
				var str = o.id;
				var idMonth = str.replace(/_DAY/, "_MONTH");
				idMonth = idMonth.replace(/datumdag/, "datummaand");

				var idYear = str.replace(/_DAY/, "_YEAR");
				idYear = idYear.replace(/datumdag/, "datumjaar");

				var daysInMonth = DaysArray(12);
				var strMonth = document.getElementById(idMonth).value;
				var strDay = o.value;
				var strYear = document.getElementById(idYear).value;

				month = parseInt(strMonth);
				day = parseInt(strDay);
				year = parseInt(strYear);

				//if (strMonth.length<1 || month<1 || month>12){
				//    return false;
				//}
				if (strDay.length < 1 || day < 1 || day > 31 || (month == 2 && day > daysInFebruary(year)) || day > daysInMonth[month])
					return false;
					
				//if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
				//	return false;
				//}
				return true;
			case 'futuredate':
				//check for valid date later or equal to current date on day-field

				var str = o.id;
				var idMonth = str.replace(/_DAY/, "_MONTH");
				idMonth = idMonth.replace(/datumdag/, "datummaand");

				var idYear = str.replace(/_DAY/, "_YEAR");
				idYear = idYear.replace(/datumdag/, "datumjaar");

				var daysInMonth = DaysArray(12);
				var strMonth = document.getElementById(idMonth).value;
				var strDay = o.value;
				var strYear = document.getElementById(idYear).value;

				month = parseInt(strMonth);
				day = parseInt(strDay);
				year = parseInt(strYear);

				if (strDay.length < 1 || day < 1 || day > 31 || (month == 2 && day > daysInFebruary(year)) || day > daysInMonth[month])
					return false;

				var dateString = strYear + "/" + strMonth + "/" + strDay;
				var dt1 = new Date(dateString);
				//var dt2 = new Date();
				var dt2 = new Date()
				dt2.setDate(dt2.getDate() - 1)

				if ((dt1 - dt2) < 0)
					return false;

				return true;
			case 'twomonthsdate':
				//check for valid date later or equal to current date on day-field

				var str = o.id;
				var idMonth = str.replace(/_DAY/, "_MONTH");
				idMonth = idMonth.replace(/datumdag/, "datummaand");

				var idYear = str.replace(/_DAY/, "_YEAR");
				idYear = idYear.replace(/datumdag/, "datumjaar");

				var daysInMonth = DaysArray(12);
				var strMonth = document.getElementById(idMonth).value;
				var strDay = o.value;
				var strYear = document.getElementById(idYear).value;

				month = parseInt(strMonth);
				day = parseInt(strDay);
				year = parseInt(strYear);

				if (strDay.length < 1 || day < 1 || day > 31 || (month == 2 && day > daysInFebruary(year)) || day > daysInMonth[month])
					return false;

				var dateString = strYear + "/" + strMonth + "/" + strDay;
				var dt1 = new Date(dateString);
				//var dt2 = new Date();
				var dt2 = new Date()
				dt2.setMonth(dt2.getMonth() - 2);
				dt2.setDate(dt2.getDate() - 1);

				if ((dt1 - dt2) < 0)
					return false;

				return true;
			case 'enddate':
				//check for valid date later or equal to start date on day-field

				var str = o.id;
				var idMonth = str.replace(/_DAY/, "_MONTH");
				idMonth = idMonth.replace(/datumdatumdag/, "datummaand");
				idMonth = idMonth.replace(/datumdag/, "datummaand");

				var idYear = str.replace(/_DAY/, "_YEAR");
				idYear = idYear.replace(/datumdatumdag/, "datumjaar");
				idYear = idYear.replace(/datumdag/, "datumjaar");

				var daysInMonth = DaysArray(12);
				var strMonth = document.getElementById(idMonth).value;
				var strDay = o.value;
				var strYear = document.getElementById(idYear).value;

				month = parseInt(strMonth);
				day = parseInt(strDay);
				year = parseInt(strYear);

				if (strDay.length < 1 || day < 1 || day > 31 || (month == 2 && day > daysInFebruary(year)) || day > daysInMonth[month])
					return false;

				var dateString1 = strYear + "/" + strMonth + "/" + strDay;
				var dt1 = new Date(dateString1);

				var idDay2 = str.replace(/logoeinddatum/, "logobegindatum");
				idDay2 = idDay2.replace(/einddatumdatum/, "startdatum");
				idDay2 = idDay2.replace(/einddatum/, "startdatum");
				var idMonth2 = idMonth.replace(/logoeinddatum/, "logobegindatum");
				idMonth2 = idMonth2.replace(/einddatum/, "startdatum");
				var idYear2 = idYear.replace(/logoeinddatum/, "logobegindatum");
				idYear2 = idYear2.replace(/einddatum/, "startdatum");

				var strDay2 = document.getElementById(idDay2).value;
				var strMonth2 = document.getElementById(idMonth2).value;
				var strYear2 = document.getElementById(idYear2).value;
				var dateString2 = strYear2 + "/" + strMonth2 + "/" + strDay2;

				var dt2 = new Date(dateString2);

				if ((dt1 - dt2) < 0)
					return false;

				return true;
			case 'dutchbankaccount':
				//check for "elfproef / bankproef"
				// verwijder alle tekens die geen cijfers zijn
				var bankrekeningnummer = o.value;
				bankrekeningnummer = bankrekeningnummer.replace(/\D/, "");
				aantal_tekens = bankrekeningnummer.length;
				var som = 0;

				// loop door de 9 cijfers met de 11 proef formule
				if (bankrekeningnummer.length == 9)
				{
					for (i = 1; i < 10; i++)
					{
						getal = bankrekeningnummer.charAt(i - 1);
						som += getal * (10 - i);
					}
					// geef resultaat van check terug
					if (som % 11 == 0 && aantal_tekens == 9)
						return true;
					else
						return false;
				}
				else if (bankrekeningnummer.length == 7)
					return /^[0-9]+$/.test(o.value);
				else
					return false;
			case 'eighteen':
				//check date => age > 18; on year-field;
				var idYear = o.id;
				var str = idYear.replace(/_YEAR/, "_DAY"); ;
				var idMonth = idYear.replace(/_YEAR/, "_MONTH");

				var daysInMonth = DaysArray(12);
				var strMonth = document.getElementById(idMonth).value;
				var strDay = document.getElementById(str).value;
				var strYear = o.value;

				month = parseInt(strMonth);
				day = parseInt(strDay);
				year = parseInt(strYear);

				if (strDay.length < 1 || day < 1 || day > 31 || (month == 2 && day > daysInFebruary(year)) || day > daysInMonth[month])
					return false;
				var dateString = strYear + "/" + strMonth + "/" + strDay;
				var dt1 = new Date(dateString);
				var dt2 = new Date();

				var overflow = (dt2 - dt1);

				var days = Math.round((overflow) / 864e5);
				var years = (days - 2) / 365.25;

				if (years < 18)
					return false;
				else
					return true;
		}
	}
	return true;
}


//start; validate date
function daysInFebruary(year)
{
	// February has 29 days in any year evenly divisible by four,
	// EXCEPT for centurial years which are not also divisible by 400.
	return (((year % 4 == 0) && ((!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28);
}

function DaysArray(n)
{
	for (var i = 1; i <= n; i++)
	{
		this[i] = 31
		if (i == 4 || i == 6 || i == 9 || i == 11) { this[i] = 30 }
		if (i == 2) { this[i] = 29 }
	}
	return this
}
//end; vaidatedate

Array.prototype.find = function(element)
{
	for (var keys in this)
	{
		if (this[keys] == element)
		{
			return keys;
			break;
		}
	}
	return -1;
};