var oXML;
// variabele om dubbele zoek-click te voorkomen
var busy = false;

function initDirectFormFeedback()
{
	var oSearch = document.getElementById( "searchform" );
	if ( oSearch )
	{
		oSearch.onsubmit = function()
		{
			// check om dubbele zoek-click te voorkomen
			if (busy!=true) {
				var busy = true;

				var bSearch = search( this.q.value, this.destination.value );
				return bSearch;
			}
		}
	}
}

/* XML helper functions */
function getSiblingByName( oNode, sName )
{
	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;
}

function getNodeValue( oNode )
{
	if ( oNode )
	{
		if ( oNode.nodeType == 3 )
			return oNode.nodeValue;
		return arguments.callee( oNode.firstChild );
	}
	return false;
}

/* Status class result parser */
function parseStatusResult( oDataXML )
{
	if ( oDataXML )
	{
		var oReply = 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;
			var sMessage = getNodeValue( getSiblingByName( oReply.firstChild, "message" ) );
			var sContent = getNodeValue( getSiblingByName( oReply.firstChild, "content" ) );
			var bStatus  = sStatus == "OK";
			return {
				success:bStatus,
				status:sStatus,
				message:sMessage,
				content:sContent
//				message:unescape( sMessage ),
			};
		}
	}	
	return false;
}

/* get max pages */
function getMaxPages( oDataXML )
{
	pages = 0;
	var oMax = getSiblingByName( oDataXML.firstChild, "blocks" );
	if ( oMax ){
		var oMax = getSiblingByName( oMax.firstChild, "metadata" );
		if ( oMax ){
			pages = getNodeValue( getSiblingByName( oMax.firstChild, "pages" ) );
		}
	}
	return pages;
}

/* e-mail address escaping */
function escapeEmailAddress( sEmail )
{
	return sEmail.replace( /\+/g, "%2B" );
}

/*
 *  Login functions
 */
function search( sQ, sRedirectURL )
{
	oXML        = new klib3.xml();
	oXML.onload = function()
	{
		onSearch( oXML.getData() );
	}
	oXML.post( "/2005/interface.asp", true, {
		command:"searchblocks",
		extra:sQ
	} );
	// bugfix: zodat safari een submit doet
	if (navigator.appName != 'Microsoft Internet Explorer'&&navigator.platform=='MacPPC') {
		return true;
	} else {
		return false;
	}
}

function onSearch( oDataXML )
{
	var oResult = document.getElementById( "searchresult" );
	if ( oDataXML )
	{
		if ( oResult )
		{
			var sXSL = "/2005/resources/home/xslt/searchblocks.xslt";
			// code for IE
			if (window.ActiveXObject)
			{
				var xsl = new ActiveXObject("Microsoft.XMLDOM")
				xsl.async = false
				xsl.load(sXSL)
				var output = oDataXML.transformNode(xsl);
			}
			// code for Mozilla, Firefox, Opera, etc.
			else
			{
                xsl = document.implementation.createDocument("", "", null);
                xsl.async = false;
                xsl.load(sXSL);

                var processor = new XSLTProcessor(); 
                processor.importStylesheet(xsl); 
				var result = processor.transformToDocument(oDataXML); 
				var xmls = new XMLSerializer(); 
				var output = xmls.serializeToString(result); 
			}
//			alert(oDataXML.xml);
//			alert(output);
			oResult.innerHTML = output;
			var max = getMaxPages(oDataXML);
//			alert(max);
			showsearch( max );
//			alert('ready');
			busy = false;
		}
	}
}

/*
 *  Searchform submit
 */
function submitSearch()
{
	oForm = document.searchform;
					
	if( oForm )
	{
		sError = "";
		if( oForm.q.value == "" )
		{
			sError = "Vul een zoekwoord in.";
			oForm.q.focus();
		}

		if( sError != "" )
		{
			oError = document.getElementById( "searcherror" );
			oError.innerHTML = sError;
			oError.className = "error";
		}
		else
		{
			oForm.submit();	
		}
	}
}

/*
 *  Form focus first field with an error
 */
function focusFirstEraticField()
{
	var aInput = document.getElementsByTagName( "input" ); // alleen input velden; radio's, checkboxen en select velden krijg visueel geen focus
	for ( var i = 0; i < aInput.length; ++i )
		if ( aInput[ i ].className.indexOf( "error" ) >= 0 )
		{
			aInput[ i ].focus();
			break;
		}
}
