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 || "/rpc.php";
	this._centralfeedback = typeof sFeedbackElementID == "string" && sFeedbackElementID != "";
	if ( this._centralfeedback )
		this._feedbackelementid = 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 )
		oForm.parentNode.parentNode.className += ( " " + this._busyclass );

	if ( oForm._feedback )
	{
		oForm._feedback.className = "feedback";
		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 = document.getElementById( this._feedbackelementid + oForm.id );

		oXML.onload  = function()
		{
			var oStatus = new Status( this );

			if ( this._parent._busyclass )
				this._form.parentNode.parentNode.className = this._form.parentNode.parentNode.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 && 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 );
			}
			else if ( oStatus.content && oStatus.content.callback && oStatus.content.callback.action && oStatus.content.callback.action != "" )
			{
				var nTimeout = oStatus.content.callback.timeout ? parseInt( oStatus.content.callback.timeout ) : 1500;
				if ( nTimeout <= 0 )
					eval(oStatus.content.callback.action);
				else
					setTimeout( oStatus.content.callback.action, nTimeout );
			}
			else if ( oStatus.content )
			{
				// create a hashtable so we can refer to eratic fields faster
				var oErratic = new Object();
				for ( var p in oStatus.content )
				{
					oErratic[ oStatus.content[ p ].fieldname ] = oStatus.content[ 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( "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, "" );
				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, "" );
			}

		};
	}
	oVariable._format = "xml";
	oXML.post( this._interface, true, oVariable );
};

// Synopsis: [object] = parseURL( string URL );
function parseURL( sURL )
{
	/^((\w+):\/{2,3})?((\w+\.)?\w+\.\w+)(:(\d+))?(\/[^\?#]*)?(\?[^#]+)?(#.+)?/.exec( sURL );
	var nPort     = ( RegExp.$6 || 80 );
	var oReturn   = {
			url:sURL,
			protocol:( RegExp.$2 || "http" ),
			port:nPort,
			hostname:RegExp.$3,
			host:( RegExp.$3 + ( nPort != 80 ? ":" + nPort : "" ) ),
			pathname:RegExp.$7,
			query:RegExp.$8,
			anchor:RegExp.$9
	}
	if ( typeof RegExp.$8 == "string" && RegExp.$8 != "" )
	{
			var aQuery    = RegExp.$8.split( /[?&]/ );
			oReturn.query = {};
			if ( aQuery.length > 0 )
			{
					for ( var i = 0; i < aQuery.length; ++i )
							if ( aQuery[ i ].indexOf( "=" ) >= 0 )
							{
									aQuery[ i ] = aQuery[ i ].split( /=/ );
									oReturn.query[ aQuery[ i ][ 0 ] ] = aQuery[ i ][ 1 ];
							}
			}
	}
	return oReturn;
}

// show an error
function showError(sError)
{
	document.getElementById("message").style.display = "block";
	document.getElementById("error").innerHTML = sError;
}

// show a message
function showMessage(sMessage)
{
	document.getElementById("message").className     	= "feedback info";
	document.getElementById("message").style.display 	= "block";
	document.getElementById("message").innerHTML 		= sMessage;
}


// show a message
function showRemoveMessage( iId, sName)
{
	document.getElementById("message").className     	= "feedback error";
	document.getElementById("message").style.display 	= "block";
	var sMessage = "Weet je zeker dat je "+sName+" wilt verwijderen? <a href='/removeitem.php?id="+iId+"'>Ja</a> / <a href='#' onclick='hideMessage()'>Nee</a>";
	document.getElementById("message").innerHTML 		= sMessage;
	return true;
}

function hideMessage()
{
	document.getElementById("message").innerHTML 	= "";
}


// handle button states. introduced for wordikrijk
function ButtonState( oInput, bPreserve )
{
	this._input = oInput;
	this._backgroundImage = this._input.style.backgroundImage.substr( 4, ( this._input.style.backgroundImage.length - 5  ) );
	this._preserve = typeof bPreserve != "undefined" && bPreserve;
	
	if ( this._backgroundImage != "" )
	{
		this._input._parent = this;		
		this._input.onmouseover = this.__mouseover;
		this._input.onmousedown = this.__mousedown;
		this._input.onmouseout  = this.__mouseout;
		this._input.onclick     = this.__mouseclick;
	}
};

ButtonState.prototype.__mouseover = function()
{
	this._parent._input.style.backgroundPosition = "0px " + ( this._parent._input.offsetHeight * -1 ) + "px";
};

ButtonState.prototype.__mouseout = function()
{
	this._parent._input.style.backgroundPosition = "0px " + ( this._parent._input.offsetHeight * -2 ) + "px";
};

ButtonState.prototype.__mousedown = function()
{
	this._parent._input.style.backgroundPosition = "0px 0px";
};

ButtonState.prototype.__mouseclick = function()
{
	this._parent._input.style.backgroundPosition = "0px " + ( this._parent._input.offsetHeight * -2 ) + "px";
};

function initButtonState()
{
	var aButton = document.getElementsByTagName( "input" );
	for ( var i = 0; i < aButton.length; ++i )
		if ( aButton[ i ].className.match( /button/ ) )
			new ButtonState( aButton[ i ] );
}

function optoutCheck()
{
	oOptoutform = document.getElementById("optoutform");
	aCheckboxes = oOptoutform.getElementsByTagName("input");
	bShowreason = false;
	for(var i=0; i<aCheckboxes.length; i++ )
	{
		if(aCheckboxes[i].name == "optout" || aCheckboxes[i].name == "partner" || aCheckboxes[i].name == "enquete" )
		{
			if(!aCheckboxes[i].checked)
				bShowreason = true;
		}
	}
	if( bShowreason )
		document.getElementById("reason").style.display = 'block';
	else
		document.getElementById("reason").style.display = 'none';
}