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);
			bAddSubmitHandler = true; // always force the handler ( ASX specific because of Omniture scripts )
			var aInput = this._form[ i ].getElementsByTagName( "input" );
			for ( var j = 0 ; j < aInput.length; ++j )
			{
				if ( aInput[ j ].nodeType == 1 && aInput[ j ].getAttribute( "name" ) == "interfaceprocessor" )
				{
					if ( aInput[ j ].getAttribute( "value" ) == "force" )
						bAddSubmitHandler = true;
					if ( aInput[ 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":
						if ( !aInput[ i ].checked )
							oVariable[ aInput[ i ].name ] = "";
					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;
				}
			}
			else
			{
				switch( aInput[ i ].type )
				{
					case "submit":
					case "button":
					case "image":
						if ( klib3.style.get( aInput[ i ], "display" ) == "none" )
							return false;
						break;
					default:
						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 );
			window[ "$_Status" ] = oStatus;
			if ( this._parent._busyclass )
				this._form.parentNode.parentNode.className = this._form.parentNode.parentNode.className.replace( new RegExp( this._parent._busyclass, "gi" ), "" );

			if ( oStatus.message != "false" )
			{
				if ( !this._form._feedback )
				{
					this._form._feedback = document.createElement( "div" );
					this._form.parentNode.insertBefore( this._form._feedback, this._form );
					this._form._feedback.setAttribute( "id", this._parent._feedbackelementid + this._form.id );
				}
				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.omniture )
			{
				oStatus.content.omniture.division = omniture.division;
				oStatus.content.omniture.section = omniture.section;
				oStatus.content.omniture.country = omniture.country;
				oStatus.content.omniture.language = omniture.language;
				oStatus.content.omniture.pagename = s.pageName;
				if( omniture.repsuiteadd && omniture.repsuiteadd != "" )
					oStatus.content.omniture.repsuiteadd = omniture.repsuiteadd;
				if( oStatus.content.omniture.eVar3 )
				{
					if( oStatus.content.omniture.eVar3 != " " )
						oStatus.content.omniture.eVar3 = s.pageName + ":" + oStatus.content.omniture.eVar3;
					else
						oStatus.content.omniture.eVar3 = s.pageName;
				}

				if( oStatus.content.omniture.setevent && typeof window.$Omniture == "object" && typeof window.$Omniture.event == "function" )
				{
					var sLinkTrackVars = "";
					var sLinkTrackEvents = "";
					var mEvents = "";
					var oEvar = {};
					var sPageAddition = "";
					if( oStatus.content.omniture.events )
					{
						sLinkTrackVars = "events";
						sLinkTrackEvents = mEvents = oStatus.content.omniture.events;
					}
					if( oStatus.content.omniture.eVar )
					{
						for( var i in oStatus.content.omniture.eVar )
						{
							if( i != "child" )
							{
								sLinkTrackVars += ( sLinkTrackVars.length > 0 ? "," : "" ) + i;
								oEvar[ i ] = oStatus.content.omniture.eVar[ i ];
							}
							
							// We're getting s.pageName as a String, but we need the value of the object variable
							if( i == "eVar3" && oStatus.content.omniture.eVar[ i ].indexOf("s.pageName") >= 0 )
								oEvar[ i ] = oStatus.content.omniture.eVar[ i ].replace("s.pageName", s.pageName);
							
						}
					}
					if( oStatus.content.omniture.extrapagename )
						sPageAddition += oStatus.content.omniture.extrapagename;
					window.$Omniture.event( sLinkTrackVars, sLinkTrackEvents, mEvents, oEvar, null, null, sPageAddition );
				}
				else
				{
					if( oStatus.content.omniture.extrapagename )
						oStatus.content.omniture.pagename += oStatus.content.omniture.extrapagename;
					_page.metrics.trackFlash( oStatus.content.omniture );
				}
			}

			var bErratic = true;
			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 );
				bErratic = false;
			}
			
			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 );
				bErratic = false;
			}

			if( bErratic )
			{
				// create a hashtable so we can refer to erratic 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 aFieldset = this._form.getElementsByTagName( "fieldset" );
				var sPattern = this._form.id + "_";
				for ( var i = 0; i < aFieldset.length; ++i )
				{
					aFieldset[ i ].className = aFieldset[ i ].className.replace( /error/gi, "" );
					aFieldset[ i ].className = aFieldset[ i ].className.replace( /validated/gi, "" );

					var oRegExp = new RegExp( sPattern, 'gi' );
					var sEratic = aFieldset[ i ].id.replace( oRegExp, "" );

					aFieldset[ i ].className += typeof oErratic[ sEratic ] == "string" ? " error" : " validated";
					aDiv = aFieldset[ i ].getElementsByTagName( "div" );
					for ( var j = 0; j < aDiv.length; ++j )
						if ( aDiv[ j ].className.match( /feedback/gi ) )
							aDiv[ j ].innerHTML = typeof oErratic[ sEratic ] == "string" ? oErratic[ sEratic ] : "";

				}
			}

		};
	}
	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 submitsendconfirm( obj )
{
	while( obj.nodeName.toLowerCase() != "form" && obj.parentNode )
		obj = obj.parentNode;

	if( obj.nodeName.toLowerCase() == "form" )
	{
		if ( typeof obj.onsubmit != "undefined" )
			obj.onsubmit();
		else
			obj.submit();
	}
}
