function setStatus(sStatus){
	getEl('status').innerHTML = sStatus; 		
}

function loadMod(sMod, sActie, iId){
	if(!handleChange(true))
		return;
	ID = iId;
	MOD = sMod;
	ACTIE = sActie;
	NAAM = 'default';
	//setStatus('Bezig met het laden van ' + ucFirst(sMod));
	setHash(LANG + "/" + MOD + "/" + escape(NAAM) + "/" + escape(ACTIE) + "/" + escape(ID));
	if(typeof x_load_mod == "function")
		x_load_mod(sMod, sActie, iId, cb_load_mod);
	return;
}

var sHash = "";

function setHash(sH){
	sHash = "#" + sH
	if(browser != 'FF')
		window.location.hash = sH; 
}

function detectHashChange(){
	if(window.location.hash != sHash){
		if("http://" + window.location.host + "/" + window.location.hash.substr(1) != "http://" + window.location.host + "/"){
			window.location.href = BASE_HTTP + "/" + window.location.hash.substr(1);
		}else{
			refresh();
		}
	}
}
detectHashChange();

function destroyObjects(){
	var oEls = document.getElementsByTagName('object');
	var oR = new Array();
	var y = 0;
	for(i=0; i < oEls.length; i++) {
		oEls[i].parentNode.removeChild(oEls[i]);
	}
}

function reloadMod(x){
	loadMod(MOD, ACTIE, ID);
}

function cb_load_mod(x){
	setKruimelpad(x['kruimelpad']);
	
	getEl('content').innerHTML = x['content'];
	getEl('content_outer').scrollTop = 0;
	getEl('coll_left').innerHTML = x['coll_left_menu'];
	//getEl('zoeken_scherm').innerHTML = x['coll_left_top'];
	getEl('menu_sub').innerHTML = x['menu_sub'];
	getEl('menu_sub2').style.display = 'none';
	getEl('menu_sub2').innerHTML = '';
	
	if(getEl('zoeken_adv'))
		getEl('zoeken_adv').innerHTML = '';
	handleSubmenu();
	resizePage();
}

function setKruimelpad(x){
	var temp = false;
	if(getEl("kruimel_actie"))
		temp = getEl("kruimel_actie").innerHTML

	getEl('kruimelpad').innerHTML = x;
	
	if(temp !== undefined && temp !== false)
		getEl("kruimel_actie").innerHTML = temp
	
	updateTitle()
}

function setKruimelpadActie(x){
	getEl("kruimel_actie").innerHTML = x;
	updateTitle();
}

function updateTitle(){
	var sT = '';
	if(getEl("kruimel_naam"))
		if(getEl('kruimel_naam').title != '')
			sT += getEl('kruimel_naam').title + ' - ';
	if(getEl("kruimel_actie"))
		if(getEl('kruimel_actie').title != '')
			sT += getEl('kruimel_actie').title + ' - ';
	if(getEl("kruimel_mod"))
		if(getEl('kruimel_mod').title != '')
			sT += getEl('kruimel_mod').title;
	setTitle(sT);
}

var sTPID;
var sTPMOD;
var sTPACTIE;

function popupMod(sMod, sActie, iId, wc_cb, cb){
	var sWc = false;
	getEl("print_css").href = VIEW_HTTP + "/css/print_popup.css"
	
	if(cb_popupClicked == undefined)
		cb_popupClicked = false;
	
	if(cb == undefined)
		cb = wc_cb;
	else
		sWc = wc_cb;
	
	cb_popupClicked = cb;
	
	sTPID = ID;
	sTPMOD = MOD;
	sTPACTIE = ACTIE;
	
	ID = iId;
	MOD = sMod;
	ACTIE = sActie;
	
	//setStatus('Bezig met het laden van ' + ucFirst(sMod));
	x_popup_mod(sMod, sActie, iId, sWc, cb_popup_mod);
}

function cb_popup_mod(x){	
	getEl('popup_holder').style.display = 'block';
	getEl('popup_holder').innerHTML = x;
}

var cb_popupClicked;

function popupClicked(iId){	
	
	hidePopup(false, true);
	
	if(cb_popupClicked != false && cb_popupClicked != undefined){
		var funcCall = cb_popupClicked + "('" + iId + "');"; 
		return eval(funcCall);
	}
	return false;
}

function popupKoppelMod(sMod, sActie, iId, wc_cb, cb){

	var sWc = false;
	
	if(cb_popupKoppelClicked == undefined)
		cb_popupKoppelClicked = false;
	
	if(cb == undefined)
		cb = wc_cb;
	else
		sWc = wc_cb;
	
	sTPID = ID;
	sTPMOD = MOD;
	sTPACTIE = ACTIE;
	
	ID = iId;
	MOD = sMod;
	ACTIE = sActie;
	
	cb_popupKoppelClicked = cb;
		
	//setStatus('Bezig met het laden van ' + ucFirst(sMod));
	x_popup_mod(sMod, sActie, iId, sWc, cb_popup_koppel_mod);
}

function cb_popup_koppel_mod(x){
	getEl('popup_koppel_holder').style.display = 'block';
	getEl('popup_koppel_holder').innerHTML = x;
}

var cb_popupKoppelClicked;

function popupKoppelClicked(iId){	
	hidePopupKoppel(false, true);
	if(cb_popupKoppelClicked != false && cb_popupKoppelClicked != undefined){
		var funcCall = cb_popupKoppelClicked + "('" + iId + "');"; 
		return eval(funcCall);
	}
	return false;
}

function cb_set_content(x){
	getEl('content').innerHTML = x;
}

function loadEntiteit(sMod, iId, sActie){
	ID = iId;
	MOD = sMod;
	ACTIE = sActie;
	NAAM = 'default';
	
	setHash(LANG + "/" + MOD + "/" + escape(NAAM) + "/" + escape(ACTIE) + "/" + escape(ID));
	setStatus('Bezig met het laden van ' + ucFirst(sActie) + ' voor ' + ucFirst(sMod));
	x_load_sub_mod(sMod, iId, sActie, cb_load_sub_mod);
}

function loadSubMod(sMod, iId, sNaam){
	ID = iId;
	MOD = sMod;
	NAAM = sNaam;
		
	setStatus('Bezig met het laden van ' + ucFirst(sNaam) + ' voor ' + ucFirst(sMod));

	if(iId <= 0){
		//if (iId.length <= 1){
			alert("U dient eerst op te slaan voordat u verder kunt.");
			return;
		//}
	}
	showBekijken();
	//alert(sMod +  ", " +  iId +  ", " +  sNaam)
	setHash(LANG + "/" + MOD + "/" + escape(NAAM) + "/" + escape(ACTIE) + "/" + escape(ID));
	x_load_sub_mod(sMod, iId, sNaam, cb_load_sub_mod);
}

function reloadSubMod(x){
	loadSubMod(MOD, ID, NAAM);
}

function cb_load_sub_mod(x){
	//if(getEl("content_container")){
		//getEl("content_container").innerHTML = x["content"];
	//}else{

	getEl("content").innerHTML = x["content"];
	getEl('content_outer').scrollTop = 0;
	
	handleSubmenu();
	//}
	setKruimelpad(x['kruimelpad']);
	
	resizePage();
}

function handleSubmenu(){
	if(getEl("menu_sub_temp")){
		setMenuSub(getEl("menu_sub_temp").innerHTML);
		getEl("menu_sub_temp").parentNode.removeChild(getEl("menu_sub_temp"));
	}
	if(getEl("menu_sub2_temp")){
		setMenuSub2(getEl("menu_sub2_temp").innerHTML);
		getEl("menu_sub2_temp").parentNode.removeChild(getEl("menu_sub2_temp"));
	}
}

var iEntTemp = 0;
var sEntModTemp = '';
var sEntSubTemp = '';

function koppel_entiteit(iEnt1, sEnt1Type, iEnt2, sEnt2Type){
	iEntTemp = iEnt1;
	sEntModTemp = sEnt1Type;
	sEntSubTemp = sEnt2Type;
	//alert(iEnt1 + ", " + sEnt1Type + ", " + iEnt2 + ", " +  sEnt2Type)

	if(iEnt2 !== false && iEnt2 > 0)
		x_koppel_entiteit(iEnt1, sEnt1Type, iEnt2, sEnt2Type, cb_koppel_entiteit);
}

function cb_koppel_entiteit(x){
	if(x['id'] > 0 ){
		//windowOpener(x['url'], x['id'], x['scherm1']);
		windowModalDialogShower(x['url'], "popup", x['scherm2'])
	}
	//if(sEntModTemp == sEntSubTemp)
	sEntSubTemp = sEntSubTemp + 'en';
	//alert(sEntModTemp + ", " + iEntTemp+ ", " +  sEntSubTemp)
	loadSubMod(sEntModTemp, iEntTemp, sEntSubTemp);
}

function del_koppel_entiteit(iEnt1, sEnt1Type, iEnt2, sEnt2Type){
	iEntTemp = iEnt1;
	sEntModTemp = sEnt1Type;
	sEntSubTemp = sEnt2Type;
	x_del_koppel_entiteit(iEnt1, sEnt1Type, iEnt2, sEnt2Type, cb_koppel_entiteit);
}

function new_account(){
	x_load_new_entiteit("account", cb_cb_new_account);
}

function cb_cb_new_account(x){
	windowModalDialogShower(x['url'], "popup", x['scherm2'])
	cb_new_account(x['id']);
}

function new_contact(){
	x_load_new_entiteit("contact", cb_cb_new_contact);
}

function cb_cb_new_contact(x){
	windowModalDialogShower(x['url'], "popup", x['scherm2'])
	cb_new_contact(x['id']);
}

var sTempMod;
var sTempSubMod;

function delete_entiteit(iId, sType, sClose){
	sTempMod = sType;
	sTempSubMod = sClose;
	
	if(sClose == 'sub')
		x_delete_entiteit(iId, sType, cb_delete_sub_entiteit);
	else
		x_delete_entiteit(iId, sType, cb_delete_entiteit);
}

function cb_delete_entiteit(x){
	refreshPrullebak();
	loadMod(sTempMod);
}

function cb_delete_sub_entiteit(x){
	refreshPrullebak();
	reloadSubMod();
}

var bChanged = false;
function setChange(){
	bChanged = true
}

function handleChange(bConfirm){
	if(bChanged == false)
		return true;
	if(bConfirm !== false)
		if(confirm("U heeft wijzigingen gemaakt. Als u door gaat gaan deze verloren. Klik op \"Ok\" om door te gaan of \"Annuleren\" om op de pagina te blijven.")){
			bChanged = false;
			return true;
		}
	return false;
}

var x;
var bClose = false;
function cb_save_mod(x){
	x = x; 
	var bValue = x['id'];
	save_call = false;
	bChanged = false;
	if (typeof x.error != 'undefined') { 
		for (i in x['error']){
			if(typeof(x['error'][i]) == 'string')
				alert(x['error'][i]);
		}
		return;
	}
	
	//refreshOpener();
	if (typeof x.header != 'undefined') { 
		if(getEl('refresh_form')){
			getEl('refresh_form').action = x['header'];
			getEl('refresh_form').submit();
			return;
		}//else
			//header(x['header']);
		//return;
	}

	if (typeof x.save != 'undefined') { 
		loadMod(MOD, 'bekijken', x['id']);
		return;
	}
	
	if (typeof x.message != 'undefined') { 
		alert(x['message']);
		return
	}
}

function cb_save_koppel(x){
	x = x; 
	save_call = false;
	var bValue = x['id'];
	bChanged = false;
	popupClicked(x['id']);
}

function refreshOpener(){
	if(window.opener != undefined)
		if(!window.opener.closed)
			window.opener.timerRefreshMod();
}

function refreshPrullebak(){
	x_reload_entiteit_verwijderd(cb_refreshPrullebak);
}

function cb_refreshPrullebak(x){
	getEl('coll_links_prullebak').innerHTML = x;
}

function setSubTabSelect(oD){
	var li = getEl("subTabs").childNodes;
	for(i in li)
		li[i].className = "menu_sub_item";
	oD.className = "menu_sub_item selected";
}

function printContent(){
	getEl("print_css").href = VIEW_HTTP + "/css/print_content.css"
	window.print();
}

function edit_koppel_opmerking(iEnt1, iEnt2){
	getEl("opm_koppel_" + iEnt1 + "_" + iEnt2).innerHTML = "<textarea style=\'width:200px;height:50px;\'>" + getEl("opm_koppel_" + iEnt1 + "_" + iEnt2).innerHTML + "</textarea>";
	getEl("img_edit_koppel_" + iEnt1 + "_" + iEnt2).style.display = "none"
	getEl("img_save_koppel_" + iEnt1 + "_" + iEnt2).style.display = "block"
}

function save_koppel_opmerking(iEnt1, iEnt2){
	var opm =  getEl("opm_koppel_" + iEnt1 + "_" + iEnt2).childNodes(0).innerHTML;
	getEl("opm_koppel_" + iEnt1 + "_" + iEnt2).innerHTML = opm;
	getEl("img_save_koppel_" + iEnt1 + "_" + iEnt2).style.display = "none"
	getEl("img_edit_koppel_" + iEnt1 + "_" + iEnt2).style.display = "block"
	x_save_koppel_entiteit_opmerking(iEnt1, iEnt2, opm, reloadSubMod);
}

function showBekijken(){
	if(getEl("edit_button"))
		getEl("edit_button").style.display="inline";
	if(getEl("view_button"))
		getEl("view_button").style.display="none";
	if(getEl("save_button"))
		getEl("save_button").style.display="none";
	if(getEl("save_send_button"))
		getEl("save_send_button").style.display="none";
	if(getEl("delete_button"))
		getEl("delete_button").style.display="inline";
}

function showBewerken(){
	if(getEl("edit_button"))
		getEl("edit_button").style.display="none";
	if(getEl("view_button"))
		getEl("view_button").style.display="inline";
	if(getEl("save_button"))
		getEl("save_button").style.display="inline";
	if(getEl("save_send_button"))
		getEl("save_send_button").style.display="inline";
	if(getEl("delete_button"))
		getEl("delete_button").style.display="none";
}

function setMenuSub(s){
	getEl("menu_sub").innerHTML = s;
}

function setMenuSub2(s){
	getEl("menu_sub2").innerHTML = s;
	getEl("menu_sub2").style.display = "block";
}		

function editLanguage(oSpan, sClass, sKey){
	oSpan.innerHTML = "<img src='" + VIEW_HTTP + "/img/icon/save_16.png' " + 
								"alt='save' " + 
								"onclick='saveLanguage(\"" + sClass + "\", \"" + sKey + "\")'/>" + 
						"<input id='" + sClass + "_" + sKey + "' "+
								"type='text' " +
								"value='" + oSpan.innerHTML + "' />";
	oSpan.onclick = function(){};
}

function saveLanguage(sClass, sKey){
	x_save_language(sClass, sKey, getEl(sClass + "_" + sKey).value, cb_save_language);
}

function cb_save_language(x){
	getEl(x['class'] + "_" + x['pointer']).parentNode.onclick = function(){editLanguage(this, x['class'], x['pointer'])};
	getEl(x['class'] + "_" + x['pointer']).parentNode.innerHTML = getEl(x['class'] + "_" + x['pointer']).value;
}

function handleBeforeUnload(){
	if(handleChange(false) === false)
		return "U heeft wijzigingen gemaakt.";
	return;
}

function handleBeforePrinting(){
	if (browser != "IE")
		return;
	var oIF = document.getElementsByTagName("INPUT")
	for(i = 0; i < oIF.length; i++) {
		if(oIF[i].type == "text")
			oIF[i].parentNode.innerHTML = oIF[i].parentNode.innerHTML + "<pre class='pre_print'>" + oIF[i].value + "</pre>";
	}
	
	var oSB = document.getElementsByTagName("SELECT")
	for(i = 0; i < oSB.length; i++)
		oSB[i].parentNode.innerHTML = oSB[i].parentNode.innerHTML + "<pre class='pre_print'>" + oSB[i].value + "</pre>";
	
	var oTA = document.getElementsByTagName("TEXTAREA")
	for(i = 0; i < oTA.length; i++) 
		oTA[i].parentNode.innerHTML = oTA[i].parentNode.innerHTML + "<pre class='pre_print'>" + oTA[i].value + "</pre>";
	getEl("content_outer").style.top = "20px";
}

function handleAfterPrinting(){
	if (browser != "IE")
		return;
	var oIF = document.getElementsByTagName("PRE")
	for(i = 0; i < oIF.length; i++){
		if(oIF[i].className == "span_print")
			oIF[i].innerHTML ="";
	}
	resizePage();
}

function isNumberKey(evt) {
	var eInput = evt || window.event;
	var charCode= eInput.keyCode || eInput.which;	
	
	//var charCode = (evt.which) ? evt.which : event.keyCode;
	//alert(charCode)
	// A
	if (charCode == 1){loadMod("page"); setTitle("Pagina"); return false;};
	// B
	if (charCode == 2){loadMod("account"); setTitle("<?php echo ucfirst(A_ACCOUNT); ?>"); return false;};
	// C
	if (charCode == 3){loadMod("contact"); setTitle("<?php echo ucfirst(A_CONTACT); ?>"); return false;};
	// D
	if (charCode == 4){loadMod("document"); setTitle("<?php echo ucfirst(A_DOCUMENT); ?>"); return false;};
	// E
	if (charCode == 5){loadMod("email_template"); setTitle("Email template"); return false;};
	// G
	if (charCode == 7){loadMod("agendaitem"); setTitle("Agenda"); return false;};
	// M === return (13)
	return true;
}

function cb_type_editor(x){
	getEl(x['cb']).innerHTML = x['content'];
	//alert(getEl('type_table').style.top)
	//getEl('type_table').offsetTop = "50px";
	getEl('type_table').style.zIndex = "+10";
}

function cb_entiteit_info_editor(x){
	getEl('entiteit_info_editor').innerHTML = x['content'];
	//getEl('type_table').style.zIndex = "+10";
}

function save_order_types(li_id, sEntNaam){

	var aOrder = new Object();
	var list_item = getEl(li_id);
	var lg = list_item.getElementsByTagName('li');
	var lgl = lg.length;
	for(i=0;i < lgl;i++){
		var lgiId = lg[i].id;
		aOrder[i] = new Object();
		aOrder[i]['new_val'] = getEl(lgiId + '_ty').value;
		aOrder[i]['old_val'] = getEl(lgiId + '_hi').value;
		
		if(getEl(lgiId + '_va'))
			aOrder[i]['value'] = getEl(lgiId + '_va').value;
	}
	x_save_order_types(aOrder, sEntNaam, cb_save_order_types);
}

function cb_save_order_types(x){
	getEl('type_table').parentNode.removeChild(getEl('type_table'));
	alert('Uw wijzigingen zijn aangepast. Na het herladen van de pagina zal de nieuwe configuratie aangepast zijn.');
}

function save_order_types_translate(li_id, aLangs, sEntNaam, sLangClass){

	var aOrder = new Object();
	var list_item = getEl(li_id);
	var lg = list_item.getElementsByTagName('li');
	var lgl = lg.length;
	for(i=0;i < lgl;i++){
		var lgiId = lg[i].id;
		aOrder[i] = new Object();
		for(x in aLangs){
			if(typeof aLangs[x] == 'string'){
				aOrder[i][aLangs[x]] = new Object();
				aOrder[i][aLangs[x]]['vertaling'] = getEl(lgiId + '_'+ aLangs[x]).value;
				aOrder[i][aLangs[x]]['key_type'] = getEl(lgiId + '_hi').value;
			}
		}
	}
	x_save_order_types_translate(aOrder, sEntNaam, sLangClass, cb_save_order_types);
}

function checkMultiSelect(checked){
	var oMS = getElsByName('multi_select', 'input');
	for(x in oMS)
		oMS[x].checked = checked;
	if(checked == 'checked'){
		getEl('check_input').style.display = 'none';
		getEl('uncheck_input').style.display = 'inline';
	}else{
		getEl('uncheck_input').style.display = 'none';
		getEl('check_input').style.display = 'inline';
	}
}

function switch_multi_select(iID){
	switch_select("multi_select_" + iID);
}

function switch_select(iID){
	if(getEl(iID).checked){
		getEl(iID).checked = false
	}else{
		getEl(iID).checked = true
	}
}

function save_order_entiteit_info(li_id, sEntNaam){

	var aOrder = new Object();
	var list_item = getEl(li_id);
	var lg = list_item.getElementsByTagName('li');
	var lgl = lg.length;
	
	for(i=0;i < lgl;i++){
		var lgiId = lg[i].id;
		if(getEl(lgiId + '_se').checked == true){
			aOrder[i] = new Object();
			aOrder[i]['limit'] = getEl(lgiId + '_li').value;
			aOrder[i]['infoent'] = getEl(lgiId + '_id').value;
		}
	}
	x_save_order_entiteit_info(aOrder, sEntNaam, reloadMod);
}

function save_default_order_entiteit_info(li_id, sEntNaam){

	var aOrder = new Object();
	var list_item = getEl(li_id);
	var lg = list_item.getElementsByTagName('li');
	var lgl = lg.length;
	
	for(i=0;i < lgl;i++){
		var lgiId = lg[i].id;
		if(getEl(lgiId + '_se').checked == true){
			aOrder[i] = new Object();
			aOrder[i]['limit'] = getEl(lgiId + '_li').value;
			aOrder[i]['infoent'] = getEl(lgiId + '_id').value;
		}
	}
	x_save_default_order_entiteit_info(aOrder, sEntNaam, reloadMod);
}

var aDates = new Array();

function timer_entiteit_herinnering() { 
	var currentTime = new Date()
	var month = currentTime.getMonth() + 1
	var day = currentTime.getDate()
	var year = currentTime.getFullYear()
	
	var hours = currentTime.getHours()
	var minutes = currentTime.getMinutes()
	
	if (minutes < 10)
		minutes = "0" + minutes
	if (hours < 10)
		hours = "0" + hours
	if (day < 10)
		day = "0" + day
	if (month < 10)
		month = "0" + month
	for (var i=0;i<aDates.length;i++){
		if(aDates[i])
			if(aDates[i].date <= year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":00"){
				if(ehSound != null)
					ehSound.play();
				alert('Herinnering: ' + aDates[i].melding);
				delete aDates[i];
				if(ehSound != null)
					ehSound.stop();
			}
	}
}
var timerId = setInterval(timer_entiteit_herinnering, 30000);

function cb_popup_reload_entiteit_herinnering(x){
	x_load_dates_entiteit_herinnering(cb_empty);
}
