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);
	
			aFormelements = this._form[ i ].getElementsByTagName("input");
			for ( var j = 0 ; j < aFormelements.length; ++j )
			{
				if (aFormelements[j].name == "interfaceprocessor" )
				{
					if ( aFormelements[j].value == "force" )
						bAddSubmitHandler = true;
					if ( aFormelements[j].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 != "" )
			{
				/*header("Location: http://industrial-tyres.nl/templates/site/page/contact_thanks.html");*/
				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.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 )
			{
				if ( typeof oStatus.content.call == "string" )
				{
					if ( typeof window[ oStatus.content.call ] != "undefined" )
						return window[ oStatus.content.call ]();
				}

				// 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, "" );
			}
			
			if (oStatus.status != "OK")
                document.getElementById('captcha').src = '/captcha.php?sid=' + Math.random();
			

		};
	}
	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;
};
