function AjaxForm( sInterfaceURL, sFeedbackElementID, sBodyBusyClass )
{
	this.init( sInterfaceURL, sFeedbackElementID, sBodyBusyClass );

	window[ ( this._self = "$_AjaxFormObject" ) ] = this;
};
AjaxForm.prototype.init = function( sInterfaceURL, sFeedbackElementID, sBodyBusyClass )
{
	this._busyclass       = sBodyBusyClass || false;
	this._interface       = sInterfaceURL || "/interface.php";
	this._centralfeedback = typeof sFeedbackElementID == "string" && sFeedbackElementID != "";
	if ( this._centralfeedback )
		this._feedbackelement = document.getElementById( sFeedbackElementID );
	this.initAjaxForm();
};

AjaxForm.prototype.initAjaxForm = function()
{
	this._form = document.getElementsByTagName( "form" );
	if ( this._form )
		for ( var i = 0; i < this._form.length; ++i )
		{
			oParsedURL = parseURL( this._form[ i ].getAttribute( "action" ) );
			bAddSubmitHandler = !( oParsedURL.hostname != "" && oParsedURL.hostname != document.domain);
			for ( var j = 0 ; j < this._form[ i ].childNodes.length; ++j )
			{
				if ( this._form[ i ].childNodes[ j ].nodeType == 1 && this._form[ i ].childNodes[ j ].nodeName.toLowerCase() == "input" && this._form[ i ].childNodes[ j ].getAttribute( "name" ) == "interfaceprocessor" )
				{
					if ( this._form[ i ].childNodes[ j ].getAttribute( "value" ) == "force" )
						bAddSubmitHandler = true;
					if ( this._form[ i ].childNodes[ j ].getAttribute( "value" ) == "ignore" )
						bAddSubmitHandler = false;
				}
			}
			
			if ( bAddSubmitHandler )
			{
				this._form[ i ]._parent  = this;
				this._form[ i ].onsubmit = this._submitHandler;
			}
		}
};
AjaxForm.prototype._submitHandler = function()
{
	this._parent.submitHandler( this );
	return false;
};
AjaxForm.prototype.submitHandler = function( oForm )
{
	if ( this._busyclass )
		document.body.className += ( " " + this._busyclass );

	if ( oForm._feedback )
	{
		oForm._feedback.className = "";
		oForm._feedback.innerHTML = "";
	}

	var oVariable = new Object();
	var aInput    = oForm.getElementsByTagName( "input" );
	var aSelect   = oForm.getElementsByTagName( "select" );
	var aTextarea = oForm.getElementsByTagName( "textarea" );

	if ( aInput )
		for ( var i = 0;i < aInput.length; ++i )
			if ( aInput[ i ].name != "" )
				switch( aInput[ i ].type )
				{
					case "checkbox":
					case "radio":
						if ( aInput[ i ].checked )
							oVariable[ aInput[ i ].name ] = aInput[ i ].value || "on";
						break;
					case "submit":
					case "button":
					case "image":
						break;
					default:
						oVariable[ aInput[ i ].name ] = aInput[ i ].value;
						break;
				}

	if ( aSelect )
		for ( var i = 0;i < aSelect.length; ++i )
			if ( aSelect[ i ].name != "" )
				oVariable[ aSelect[ i ].name ] = aSelect[ i ][ aSelect[ i ].selectedIndex ].value;

	if ( aTextarea )
		for ( var i = 0;i < aTextarea.length; ++i )
			if ( aTextarea[ i ].name != "" )
				oVariable[ aTextarea[ i ].name ] = aTextarea[ i ].value;

	if ( typeof oVariable.command == "undefined" || oVariable.command == "" )
		oVariable.command = oForm.id || ( typeof oForm.name == "string" ? oForm.name : "" );

	if ( oVariable.command == "" )
	{
		alert( "Implementatiefout: Geen input.name=command of form.id gezet!" );
		return false;
	}

	var oXML     = new klib3.xml();
	oXML._form   = oForm;
	oXML._parent = this;
	if ( typeof this.onload == "function" )
	{
		oXML.onload = this.onload;
	}
	else
	{
		if ( this._centralfeedback )
			oXML._form._feedback = this._feedbackelement;

		oXML.onload  = function()
		{
			var oStatus = new Status( this );
			if ( this._parent._busyclass )
				document.body.className = document.body.className.replace( new RegExp( this._parent._busyclass, "gi" ), "" );

			if ( !this._form._feedback )
			{
				this._form._feedback = document.createElement( "div" );
				this._form.parentNode.insertBefore( this._form._feedback, this._form );
			}
			this._form._feedback.className     = "feedback " + ( oStatus.status == "OK" ? "info" : "error" );
			this._form._feedback.innerHTML     = oStatus.message;
			this._form._feedback.style.display = "block";

			if ( oStatus.content.redirect && oStatus.content.redirect.url && oStatus.content.redirect.url != "" )
			{
				var nTimeout = oStatus.content.redirect.timeout ? parseInt( oStatus.content.redirect.timeout ) : 1500;
				if ( nTimeout <= 0 )
					document.location = oStatus.content.redirect.url;
				else
					setTimeout( "document.location='" + oStatus.content.redirect.url + "';", nTimeout );
			}

			if ( scrollIntoView )
				scrollIntoView( 0, 150, 10 );

			if ( oStatus.content.error )
			{
				// create a hashtable so we can refer to eratic fields faster
				var oErratic = new Object();
				for ( var p in oStatus.content.error )
					oErratic[ oStatus.content.error[ p ].fieldname ] = oStatus.content.error[ p ].message || "";

				//  walk through the various form fields (the current form only) and mark eratic fields as... well eratic
				var aInput = this._form.getElementsByTagName( "input" );
				for ( var i = 0; i < aInput.length; ++i )
					switch( aInput[ i ].type )
					{
						case "checkbox":
						case "radio":
							if ( typeof oErratic[ aInput[ i ].name ] == "string" )
								aInput[ i ].parentNode.className = ( aInput[ i ].parentNode.className != "" ? aInput[ i ].parentNode.className + " " : "" ) + "formerror";
							else
								aInput[ i ].parentNode.className = aInput[ i ].parentNode.className.replace( /formerror/gi, "" );
							break;
						default:
							if ( typeof oErratic[ aInput[ i ].name ] == "string" )
								aInput[ i ].className = ( aInput[ i ].className != "" ? aInput[ i ].className + " " : "" ) + "formerror";
							else
								aInput[ i ].className = aInput[ i ].className.replace( /formerror/gi, "" );
							break;
					}
				var aInput = this._form.getElementsByTagName( "select" );
				for ( var i = 0; i < aInput.length; ++i )	
					if ( typeof oErratic[ aInput[ i ].name ] == "string" )
						aInput[ i ].parentNode.className = ( aInput[ i ].parentNode.className != "" ? aInput[ i ].parentNode.className + " " : "" ) + "formerror";
					else
						aInput[ i ].parentNode.className = aInput[ i ].parentNode.className.replace( /formerror/gi, "" );
				
				var aInput = this._form.getElementsByTagName( "textarea" );
				for ( var i = 0; i < aInput.length; ++i )
					if ( typeof oErratic[ aInput[ i ].name ] == "string" )
						aInput[ i ].className = ( aInput[ i ].className != "" ? aInput[ i ].className + " " : "" ) + "formerror";
					else
						aInput[ i ].className = aInput[ i ].className.replace( /formerror/gi, "" );
			}

		};
	}

	oXML.post( this._interface, true, oVariable );
};



function checkage( oWrapper )
{
	var aInput    = oWrapper.getElementsByTagName( "input" );
	var oField    = {};
	var bVerify   = true;
	var oFeedback = document.getElementById( "agefeedback" );

	for ( var i = 0; i < aInput.length; ++i )
	{
		oField[ aInput[ i ].name ] = aInput[ i ];
		if ( aInput[ i ].value == "" )
			bVerify = false;
	}

	if ( bVerify )
	{
		var oNow       = new Date();
		var oCompare16 = new Date( oNow.getYear() - 16, oNow.getMonth(), oNow.getDate(), oNow.getHours(), oNow.getMinutes(), oNow.getSeconds() );
		var oCompare12 = new Date( oNow.getYear() - 12, oNow.getMonth(), oNow.getDate(), oNow.getHours(), oNow.getMinutes(), oNow.getSeconds() );
		var oGiven     = new Date( parseInt( oField.year.value ), parseInt( oField.month.value ) - 1, parseInt( oField.day.value ), oNow.getHours(), oNow.getMinutes(), oNow.getSeconds() );

		if ( Math.floor( ( oCompare16.getTime() - oGiven.getTime() ) / 1000 ) >= 0 )
			oFeedback.innerHTML = "";
		else if ( Math.floor( ( oCompare12.getTime() - oGiven.getTime() ) / 1000 ) >= 0 )
			oFeedback.innerHTML = "Vraag toestemming aan je ouders voordat je verder gaat met registreren voor Verjaardagsalarm";
		else
			oFeedback.innerHTML = "Om deel te mogen nemen aan verjaardagsalarm moet je 12 jaar of ouder zijn!";
	}
	else
	{
		oFeedback.innerHTML = "";
	}
}