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._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.getElementById( this._busyclass ).style.display = 'block';
		
	document.getElementById( 'fade1' ).style.color = 'rgb( 240,240,240 )' ; 
	document.getElementById( 'fade2' ).style.color = 'rgb( 240,240,240 )' ; 
	document.getElementById( 'fade3' ).style.color = 'rgb( 240,240,240 )' ; 



	if ( typeof oForm._feedback != "undefined" )
	{
		oForm._feedback.className = "";
		oForm._feedback.innerHTML = "&#160;";
	}

	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._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.success )
 			{
 				document.getElementById('formpart').style.display = 'none' ;
	 			document.getElementById('afterform').style.display = 'block' ;
			}
			document.getElementById( 'progress' ).style.display = 'none';
			document.getElementById( 'fade1' ).style.color = 'black' ; 
			document.getElementById( 'fade2' ).style.color = 'black' ; 
			document.getElementById( 'fade3' ).style.color = 'black' ; 
		};
	}
	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;
}

function Status( oXML )
{
	if ( typeof oXML != "undefined" )
		this.init( oXML );
}
Status.prototype.init = function( oXML )
{
	this.parseStatusResult( oXML.getData() );
};
Status.prototype.getNodeValue = function( oNode )
{
	if ( oNode )
	{
		//   textnode               cdata
		if ( oNode.nodeType == 3 || oNode.nodeType == 4 )
			return oNode.nodeValue;
		return arguments.callee( oNode.firstChild );
	}
	return false;
};
Status.prototype.getSiblingByName = function ( oNode, sName )
{
	if ( oNode )
	{
		while ( ( oNode.nodeType != 1 || ( oNode.nodeName && oNode.nodeName.toLowerCase() != sName.toLowerCase() ) ) && oNode.nextSibling )
			oNode = oNode.nextSibling;

		if ( oNode.nodeType == 1 && ( oNode.nodeName && oNode.nodeName.toLowerCase() == sName.toLowerCase() ) )
			return oNode;
	}
	return false;
};
Status.prototype.parseStatusResult = function( oDataXML )
{
	if ( oDataXML )
	{
		var oReply = this.getSiblingByName( oDataXML.firstChild, "reply" );
		if ( oReply )
		{
			var sStatus = "error";
			if ( typeof oReply.attributes[ 0 ] != "undefined" && oReply.attributes[ 0 ].name.toLowerCase() == "status" )
				sStatus = oReply.attributes[ 0 ].value;
			this.success = sStatus == "OK";
			this.status  = sStatus;
			this.message = unescape( this.trim( this.getNodeValue( this.getSiblingByName( oReply.firstChild, "message" ) ) ) );
			this.content = this.xmlToObject( this.getSiblingByName( oReply.firstChild, "content" ) );
			return true;
		}
	}	
	return false;
};
Status.prototype.trim = function( s )
{
	if ( typeof s == "string" )
	{
		for ( var i = 0; i < s.length; ++i )
			if ( s.charCodeAt( i ) > 32 )
				break;
		for ( var j = s.length - 1; j >= 0 ; --j )
			if ( s.charCodeAt( j ) > 32 )
				break;
		return s.substr( i, ( j + 1 ) - i );
	}
	return s;
};
Status.prototype.xmlToObject = function( oXML )
{
	var oReturn = new Object();
	if ( oXML.childNodes && oXML.childNodes.length > 0 )
		for ( var i = 0; i < oXML.childNodes.length; ++i )
			if ( oXML.childNodes[ i ].nodeType == 3 || oXML.childNodes[ i ].nodeType == 4 )
				return unescape( oXML.childNodes[ i ].nodeValue );
			else
				oReturn[ oXML.childNodes[ i ].nodeName ] = arguments.callee( oXML.childNodes[ i ] );
	return oReturn;
};