function InputControl()
{
	this._monitor   = false;
	this._feedback  = false;
	this._timer     = null
	this._timeout   = 250;
	this._maxlength = 200;
	window[ ( this._self = "IC_Object" ) ] = this;
};
InputControl.prototype.monitor = function( oElement, nLength, oFeedback )
{
	this._monitor   = oElement;
	this._maxlength = nLength || this._maxlength;
	this._feedback  = oFeedback;
	this._timer     = setTimeout( this._self + "._check()", this._timeout );
};
InputControl.prototype.cancel = function()
{
	this._monitor  = false;
	this._feedback = false;
	clearTimeout( this._timer );
};
InputControl.prototype._check = function()
{
	clearTimeout( this._timer );
	if ( this._feedback )
	{
		if ( this._maxlength - this._monitor.value.length < 0 )
			this._monitor.value = this._monitor.value.substring( 0, this._maxlength );
		this._feedback.innerHTML = ( this._maxlength - this._monitor.value.length ) + " letters over.";
		this._timer = setTimeout( this._self + "._check();", this._timeout );
	}
};
// var oIC = new InputControl();


function inputMonitor( oElement, nLength, oFeedback )
{
	if ( typeof oIC == "object" )
		oIC.monitor( oElement, nLength, oFeedback );
}
function cancelMonitor( oElement, nLength )
{
	if ( typeof oIC == "object" )
		oIC.cancel();
}


function defaultFormValue( oElement, bRestore )
{
	if ( bRestore && typeof oElement._default == "string" )
	{
		if ( oElement.value.replace( / \t\n\r/g, "" ) == "" )
			oElement.value = oElement._default;
	}
	else if ( !bRestore )
	{
		if ( typeof oElement._default != "string" )
			oElement._default = oElement.value;			
		if ( oElement._default == oElement.value )
			oElement.value = "";
	}
}

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

			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, "" );
			}

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

function showTafPreview()
{
	document.getElementById('tafnamepreview').innerHTML		= document.getElementById('tafname').value;
	document.getElementById('tafmessagepreview').innerHTML	= document.getElementById('tafmessage').value.replace(/\n/g,'<br />');
	document.getElementById('tafsenderpreview').innerHTML	= document.getElementById('tafsender').value;
	
	document.getElementById('tafform').style.display	= 'none'; 
	document.getElementById('tafpreview').style.display = 'block';
}

function hideTafPreview()
{
	document.getElementById('tafpreview').style.display = 'none';
	document.getElementById('tafform').style.display	= 'block'; 
}