// FastSearch functionaliteit:

var fs_req = null;
var fs_timer = null;
var fs_timerId = null;
var fs_status = 0;
var fs_IE = document.all?true:false;
var fs_xmlhttp;

function xmlhttpChange() {
	if(fs_xmlhttp.readyState == 4) {
		if (fs_xmlhttp.status == 200) {
			responseText = fs_xmlhttp.responseText;

			// reset object (firefox gets recursive xmlhttprequests!)
			fs_xmlhttp.onreadystatechange = new Function ('return true;');
			fs_xmlhttp.abort();
			fs_xmlhttp = null;

			eval(responseText);
			fs_status = 0;
			hideWait();
		} else {
			window.status = "There was a problem retrieving data from the server:<BR>\n" + fs_xmlhttp.statusText;
		}
	}
}

var fs_outstandingRequests = Array();

function doSearch(name,value){
	var s = Array();
	s[0] = name;
	s[1] = value;
	
	fs_outstandingRequests[fs_outstandingRequests.length] = s;
	
	if (fs_status < 2){
		postAjax();
	}

}

function postAjax(){
	// handle queue:
	if (fs_status > 0){
		setTimeout('postAjax()',100);
		fs_status = 2;
		return;
	}
	
	fs_status = 1;	
	var uri = './?ajax=1';
	for (var teller =0;teller < fs_outstandingRequests.length;teller++){
		uri = uri + '&search_' + fs_outstandingRequests[teller][0] + '=' + fs_outstandingRequests[teller][1];
	}
	//alert(uri);
	fs_outstandingRequests = Array();

	loadXMLDoc(uri);
	showWait();
}

function showWait(){
	var el = document.getElementById('search_wait');
	if(el) {
		el.style.display = 'block';
	}
}

function hideWait(){
	var el = document.getElementById('search_wait');
	if(el) {
		el.style.display = 'none';
	}
}

function postFull(){
	document.forms['fsearch'].submit();
}

function loadXMLDoc(url) {

	fs_status = 1;

	// code for Mozilla, etc.
	if (window.XMLHttpRequest) {
		fs_xmlhttp=new XMLHttpRequest();
		fs_xmlhttp.onreadystatechange=xmlhttpChange;
		fs_xmlhttp.open("GET", url, true);
		fs_xmlhttp.send(null);
	} else if (window.ActiveXObject)  {
		fs_xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		if (fs_xmlhttp) {
			fs_xmlhttp.onreadystatechange=xmlhttpChange;
			fs_status =1;
			fs_xmlhttp.open("GET",url,true);
			fs_xmlhttp.send();
		}
	}
}

function getXMLHTTP(){
  var A=null;
  try{
	A=new ActiveXObject("Msxml2.XMLHTTP")
  }catch(e){
	try{
	  A=new ActiveXObject("Microsoft.XMLHTTP")
	} catch(oc){
	  A=null
	}
  }
  if(!A && typeof XMLHttpRequest != "undefined") {
	A=new XMLHttpRequest();
	
	}
	return A;  
}


// function fsearchCheckResultAmount() kijkt of er resultaten worden teruggegeven

function fsearchCheckResultAmount(){
	if (!fs_resultAmount == undefined){
		if (fs_resultAmount == 0){
			return confirm('Er zijn geen voertuigen die aan de voorwaarden voldoen');
		}
	}	
	return true;
}


// update functies

var fs_currentFieldName;
var fs_mode = 'create';
var fs_currentNode = 0;

// fieldName: field to update, nrValues: amount of times updateField will be called
function initializeUpdateField(fieldName,nrValues,fieldType){
	
	if (fieldType == 'select' && (document.forms['fsearch'][fieldName] != undefined)){
		fs_currentFieldName = fieldName;
		fs_currentNode = 0;

		if(document.forms['fsearch'][fieldName].options.length == nrValues){
			fs_mode = 'update';
		} else {
			fs_mode = 'create';
			document.forms['fsearch'][fieldName].options.length = 0;
		}
	} else {
		if (fieldType == 'text' && (document.forms['fsearch'][fieldName] != undefined)){
			if( nrValues == 0){
				document.forms['fsearch'][fieldName].value = '';
				document.forms['fsearch'][fieldName].disabled = true;
				document.forms['fsearch'][fieldName].className = 'disabledInputField';
			} else {
				document.forms['fsearch'][fieldName].disabled = false;
				document.forms['fsearch'][fieldName].className = 'enabledInputField';
			}
		}

		fs_mode = 'skip';
	}
	
	
}

function updateField(_value,_label,_selected,_enabled){

	if(fs_mode == 'skip'){
		return;
	}
	
	if(fs_mode == 'update'){
			document.forms['fsearch'][fs_currentFieldName].options[fs_currentNode].value = _value;
			document.forms['fsearch'][fs_currentFieldName].options[fs_currentNode].innerHTML = _label;
			if (!_enabled){
				document.forms['fsearch'][fs_currentFieldName].options[fs_currentNode].style.color = '#aaaaaa';
			} else {
				document.forms['fsearch'][fs_currentFieldName].options[fs_currentNode].style.color = '#000000';
			}
		if (_selected){
			document.forms['fsearch'][fs_currentFieldName].options[fs_currentNode].selected = true;			
		}
	} else {
		var oOption = document.createElement('option');
		 oOption.innerHTML = _label;
		 oOption.value = _value;
		 if(!_enabled){
		 	oOption.style.color = '#aaaaaa';
		 }
		 if(_selected){
		 	oOption.selected = true;
		 }
		 document.forms['fsearch'][fs_currentFieldName].appendChild(oOption);

	}
	fs_currentNode++;
}

function setResultAmount(amount,divName){
	var el = document.getElementById(divName);
	if(el) {
		el.innerHTML = amount;
	}
}

