/* global http request object */
var xmlhttp;

	
function toggleIcon(strId) {
	try {
		objIcon = document.getElementById(strId)
		if (objIcon.src.indexOf('plus')>0) {
			setTimeout("objIcon.src='images/default/buttons/min.gif'",100);
		}else {
			setTimeout("objIcon.src='images/default/buttons/plus.gif'",100);
		}
	} catch(e) {
	}
}


/* deserializer class and methods */
function objDeSerializerElement(strContent) {
	// properties
	this.strFieldDelimiter="!!field!!";
	this.strFieldValueSeperator="!!equals!!";
	this.arrFieldValue=new Array();
	this.serialized_content=strContent;

	// methods
	this.parse=_dseParse;
	this.getValue=_dseGetValue;
}

function _dseParse() {	
	arrFields=this.serialized_content.split(this.strFieldDelimiter);
	for(i=0;i<arrFields.length;i++) {
		strFieldValue=arrFields[i];
		arrSep=strFieldValue.split(this.strFieldValueSeperator);
		strField=arrSep[0];		
		strValue=arrSep[1];
		this.arrFieldValue[strField]=strValue;
	}
}

function _dseGetValue(strField) {
	return this.arrFieldValue[strField];
}

function objDeSerializer(strContent) {
	// properties
	this.strRowDelimiter="!!row!!";
	this.objs=new Array();
	this.serialized_content=strContent;
		
	// methods
	this.parse=_dsParse;
	this.getObj=_dsGetObj;
	this.numberOfObjs=_dsNumberOfObjs;
}

function _dsParse() {
	//alert(this.serialized_content);
	arrLines=this.serialized_content.split(this.strRowDelimiter);	
	//alert(arrLines.length);
	for(j=0;j<arrLines.length;j++) {		
		objElement = new objDeSerializerElement(arrLines[j]);
		objElement.parse();
		this.objs[this.objs.length]=objElement;
	}
}

function _dsGetObj(i) {
	return this.objs[i];
}

function _dsNumberOfObjs() {
	return this.objs.length;
}


/* request and div manipulation functions */
function isOff(strDivId) {
	if (document.getElementById(strDivId).innerHTML=='') {
		return true;
	} else {
		return false;
	}
}

function setOn(strDivId) {
	document.getElementById(strDivId).style.display='block';
	document.getElementById(strDivId).style.visibility='visible';
}

function setOff(strDivId) {
	document.getElementById(strDivId).innerHTML='';		
	document.getElementById(strDivId).style.display='none';
	document.getElementById(strDivId).style.visibility='hidden';
}

function simpleRequest(myUrl, callBackFunc, strParams)  {	
	var strResult="";
	var xmlhttp;

	
	if(window.XMLHttpRequest) {
		xmlhttp = new XMLHttpRequest(); // Gecko (Firefox, Moz), KHTML (Konqueror, Safari), Opera
	} else if(window.ActiveXObject) {
		xmlhttp = new ActiveXObject("MSXML2.XMLHTTP"); // Internet Explorer
	} else {
		return false;
	}
	xmlhttp.open("GET", myUrl, true); // Open a connection. Replace GET with HEAD in order to do a HEAD request.

	xmlhttp.onreadystatechange=function () {
		 if (xmlhttp.readyState==4) {
			if (xmlhttp.status==200) {
				callBackFunc(xmlhttp,strParams)
			}
		}
	}
	
	xmlhttp.send(null); // send() is used to initiate the transfer. No actual data have to be sent in this case.
}

function clearSelectBox(objBox) {
	for(i=0; objBox.options.length>0;i++) {
		objBox.options[0]=null;
	}
}

function addOptionToSelectBox(objBox, strOption, strValue) {
	var objOption = new Option(strValue, strOption);
	objBox.options[objBox.options.length] = objOption
}

function zetEenMomentAub(booModus) {	
	try {
		if (booModus) {
			document.getElementById('een_moment_aub').style.visibility='visible';
			document.getElementById('een_moment_aub').style.display='block';			
		} else {
			document.getElementById('een_moment_aub').style.visibility='hidden';
			document.getElementById('een_moment_aub').style.display='none';
		}
	} catch(e) {
		// Niet altijd is er een "een moment aub" div aanwezig. Bijvoorbeeld bij pane ajax requests.
	}
}

function writeInId(xmlhttp, strDivId) {
	//alert(xmlhttp.responseText);			
	document.getElementById(strDivId).innerHTML = xmlhttp.responseText;
	zetEenMomentAub(false);
}

function writeOpsomming(xmlhttp, strDivId) {
	//alert(xmlhttp.responseText);			
	document.getElementById(strDivId).innerHTML = xmlhttp.responseText;
	zetEenMomentAub(false);
	// init sortable tables:
	sortables_init();
}

function displayOpsomming(intObjTypeId, strZoekWoord, strDivId) {
	zetEenMomentAub(true);	
	simpleRequest('main.php?zoekwoord='+strZoekWoord+'&for_obj_type_id='+intObjTypeId+'&mode=opsomming&action=tree_aja&aja_action=get_opsomming', writeOpsomming,  strDivId);	
}

function displaySubsites(strMode, strDivId) {
	if (isOff(strDivId)) {
		simpleRequest('main.php?mode='+strMode+'&action=tree_aja&aja_action=get_subsites', writeInId,  strDivId);
		setOn(strDivId);
	} else {
		setOff(strDivId);	
	}
}

function displayObjTypes(strMode, strSubsiteId,strObjTypes, strDivId) {
	if (isOff(strDivId)) {
		simpleRequest('main.php?mode='+strMode+'&action=tree_aja&aja_action=get_objtypes&obj_types='+strObjTypes + '&subsite_id=' + strSubsiteId, writeInId,  strDivId);
		setOn(strDivId);
	} else {
		setOff(strDivId);	
	}
}

function displayConfigs(strMode, strDivId) {
	if (isOff(strDivId)) {
		simpleRequest('main.php?mode='+strMode+'&action=tree_aja&aja_action=get_configs', writeInId,  strDivId);
		setOn(strDivId);
	} else {
		setOff(strDivId);	
	}
}

function displayContentObjType(strMode, strSubsiteId, strObjType, strDivId) {			
	if (isOff(strDivId)) {
		zetEenMomentAub(true);
		//alert('main.php?mode=".$strMode."&action=tree_aja&aja_action=get_objtype&obj_type='+strObjType+'&subsite_id=' + strSubsiteId);
		simpleRequest('main.php?mode='+strMode+'&action=tree_aja&aja_action=get_objtype&obj_type='+strObjType+'&subsite_id=' + strSubsiteId, writeInId,  strDivId);
		setOn(strDivId);
	} else {
		setOff(strDivId);	
	}
}

function displayFolder(strDirOffset, strDivId, strMode) {
	
	if (isOff(strDivId)) {		
		zetEenMomentAub(true);			
		simpleRequest('main.php?action=tree_aja&mode='+strMode+'&aja_action=get_folder&folder_offset='+strDirOffset, writeInId, strDivId);		
		setOn(strDivId);
	} else {		
		setOff(strDivId);	
	}
}


function displaySubmenu(strMode, strId, strDivId, strFormName) {
	// alert(strFormName);
	if (isOff(strDivId)) {
		zetEenMomentAub(true);
		//alert('main.php?mode=".$strMode."&action=tree_aja&aja_action=get_objtype&obj_type='+strObjType+'&subsite_id=' + strSubsiteId);
		simpleRequest('main.php?mode='+strMode+'&form_name='+strFormName+'&action=tree_aja&aja_action=get_submenu&menu_id='+strId, writeInId,  strDivId);
		setOn(strDivId);
	} else {
		setOff(strDivId);	
	}
}

function displayIppContent(strUrl) {	
	simpleRequest(strUrl, writeInId,  'ipp_content');
}

function displayNieuweMedew(offSetId) {	
	simpleRequest('main.php?action=tree_aja&aja_action=get_nieuwe_medew&offset='+offSetId, setNieuweMedew);
}

function displayPrikbord(offSetId) {	
	simpleRequest('main.php?action=tree_aja&aja_action=get_prikbord&offset='+offSetId, setPrikbord);
}

function displayLogs(strMode, strDivId) {
	if (isOff(strDivId)) {
		zetEenMomentAub(true);		
		simpleRequest('main.php?action=tree_aja&aja_action=get_logs&mode='+strMode, writeInId, strDivId);
		setOn(strDivId);
	} else {
		setOff(strDivId);	
	}	
}

function displayMove(intObjId, intCurrentMenuId, intMenuId, strDivId) {			
	document.getElementById(strDivId).style.visibility='visible';
	document.getElementById(strDivId).style.display='block';
	if (intMenuId==0) {
		document.getElementById(strDivId).innerHTML='Een ogenblik, de menuboom wordt opgebouwd.';
		simpleRequest('main.php?name=move_obj&obj_id='+intObjId+'&current_menu_id='+intCurrentMenuId+'&menu_id=' + intMenuId, writeInId , strDivId);
	} else {
		document.getElementById(strDivId).innerHTML='Een ogenblik, het menu-item wordt verplaatst.';
		//simpleRequest('main.php?name=move_obj&obj_id='+intObjId+'&current_menu_id='+intCurrentMenuId+'&menu_id=' + intMenuId, writeInId , strDivId);
		simpleRequest('main.php?name=move_obj&obj_id='+intObjId+'&current_menu_id='+intCurrentMenuId+'&menu_id=' + intMenuId, function() {window.location.href='main.php?obj_id='+intObjId} , strDivId);
	}	
}

function displaySearchResults(strZoekWoord,strDivId) {
	// urlencode??		
	zetEenMomentAub(true);	
	simpleRequest('main.php?name=search_select_obj&zoekwoord='+strZoekWoord, writeInId , strDivId);
	setOn(strDivId);
}


