var _obj = null;
var sql = "";
var start = 0;
var zoek = "";
var order = "";
var order_mode = "DESC";
function toon_paswoord()
{
	
	var toon_obj = document.getElementById("toon_btn");
	_obj = document.getElementById("paswoord");
	
	var new_type = "";
	if(_obj.type=="password")
	{
		
		new_type == "text";
		toon_obj.value = "verberg paswoord";
	
	}
	else
	{
		new_type = "password";
		toon_obj.value = "toon paswoord";
	}
	
	changeInputType(_obj,new_type);
}
var last_color = null;
function changeInputType(oldObject, oType) {
  var newObject = document.createElement('input');
  newObject.type = oType;
  if(oldObject.size) newObject.size = oldObject.size;
  if(oldObject.value) newObject.value = oldObject.value;
  if(oldObject.name) newObject.name = oldObject.name;
  if(oldObject.id) newObject.id = oldObject.id;
  if(oldObject.className) newObject.className = oldObject.className;
  oldObject.parentNode.replaceChild(newObject,oldObject);
  return newObject;
}
	function openMac()
	{
		var _obj = document.getElementById("mac");
		if(_obj.style.visibility == "hidden")
		{
			_obj.style.visibility = "visible";
			_obj.style.height = "100%";
		}
		else
		{
			_obj.style.visibility = "hidden";
			_obj.style.height = "0px";
		}
	}
	
function hashPass()
{
	var elm = document.getElementById("password");
	elm.value = MD5(elm.value);
	return true;
}
function uncompressCode(code)
{
	if (compressing == "1") {
		code = str_replace("3E%3C%2Ftd%3E%3Ctd%2Bclass%3D%27grid_fiche", ":ZT", code);

		code = str_replace("%3C%2Ftd%3E%3Ctd%2Bclass%3D%27grid_fiche%27%3E:ZL", ":ZF", code);


		code = str_replace("%29%3C%2Ftd%3E%3Ctd%2Bclass%3D%27grid_fiche%27%2Bstyle%3D%27width%3A100%25%3B%27%3E:ZS%3C%2Ftd%3E%0A%09%3C", ":ZH2", code);
		code = str_replace("3Ctd%2Bclass%3D%27grid_header%27%2Bstyle%3D%27cursor%3Apointer%3B", ":ZH", code);
		code = str_replace("%27%2Bonmouseover%3D%27fiche_rollover%28this%29%3B%27%2Bonmouseout%3D%27fiche_rollout%28this%29%3B%27%3E%3Ctd%2Bclass%3D%27grid_fiche%27%3E", ":ZM", code);
		code = str_replace("%3Ca%2Bhref%3D%27index.php%3Faction%3D", ":ZL", code);
	
		code = str_replace("%3C%2Ftd%3E%3Ctd%2Bclass%3D%27grid_fiche%27%3E", ":ZO", code);
		code = str_replace("%3Ctable%2Bcellpadding%3D%270%27%2Bcellspacing%3D%270%27", ":ZC", code);
		code = str_replace(":ZSbackground-color%3A%23", ":ZB", code);
		code = str_replace("%3B%27%2Bsrc%3D%27..%2F..%2Fcore%2Fimages%2Fspacer.gif%27%2B%2F%3E%3C", ":ZI", code);
		code = str_replace("width%3A300px%3B%27%3E%3Ctr%3E%3Ctd%3E%3Cspan%3E", ":ZW", code);
		
		code = str_replace("%3Ctd%2Bclass%3D%27grid_fiche%27", ":Z4", code);
		code = str_replace("%27%2Bclass%3D%27button_variable_size%27%2Bvalue%3D%27", ":ZE", code);
		code = str_replace("%27%2Bonmouseover%3D%27fiche_rollover%28this%29%3B%27%2Bonmouseout%3D%27fiche_rollout%28this%29%3B%27%3E", ":ZZ", code);
		code = str_replace("%27onclick%3D%27javascript%3A", ":ZN", code);
		code = str_replace("%26amp%3B", ":ZA", code);
		code = str_replace(":ZPwidth%3A100%25%3B%27%3E:ZS%3C%2Ftd%3E%0A", ":ZJ", code);
		code = str_replace("%2Bstyle%3D%27", ":ZP", code);
		code = str_replace("%2Bclass%3D%27", ":ZR", code);
		code = str_replace("padding", ":ZV", code);
	
		code = str_replace("%26nbsp%3B", ":ZS", code);
	}
	
	return code;
}

function str_replace(sReplace, sFind, code)
{
	code = code.split(sFind).join(sReplace);
	return code;	
}

function deleteItem(evt)
{
	if(confirm('ben je zeker dat je dit bestand wil verwijderen?')) {
		del_file(evt.id);
	}
}
	
function uploadImage(evt) {
	return true;
}
function word_alert()
{
	alert("lees de help onderaan/bovenaan op deze pagina voor meer uitleg");	
}

var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};

BrowserDetect.init();

function getQueryString() 
{  
	var query =  window.location.search.substring(1);  
	query = query.split("&tiny_mce=1").join("");
	query = query.split("&tiny_mce_old=1").join("");
	return query;
}

function refresh_mce(editor)
{
	var query = getQueryString();
	if(editor=="tiny_mce")
	{
		window.location = "index.php?" + query + "&tiny_mce_old=1";
	}
	else
	{
		window.location = "index.php?" + query + "&tiny_mce=1";
	}
	//$_SESSION[$session_prefix]["tiny_mce_old"]	
}

function go_back(type,url)
{
	var goBack = false;
	if (taal_id) {
		url += "&taal_id=" + taal_id;	
	}
	if(type=="edit" || type=="add")
	{
		if(document.getElementById("button"))
		{
			var answer = confirm("ben je zeker dat je wil terug gaan? je hebt de gegevens nog niet opgeslagen!")
			if (answer){
				//window.location = url;
				goBack = true;
			}
		}
		else
		{
			//window.location = url;	
			goBack = true;
		}
	}
	else
	{
		//window.location = url;	
		goBack = true;
	}
	
	if (goBack) {
		//history.go(-1);	
		window.location = url;
	}
	/*if (type=="edit" || type=="add") {
		var answer = confirm("ben je zeker dat je wil terug gaan? je hebt de gegevens nog niet opgeslagen!")
		if (answer){
			history.go(-1);
		}
	} else {
		history.go(-1);
	}*/
}

function check_safari()
{
	if(BrowserDetect.OS=="Mac")
	{
		if(BrowserDetect.browser=="Safari")
		{
			alert("om deze applicatie optimaal te laten werken raden we aan om de firefox browser te gebruiken i.p.v. safari (hier werken de texteditors niet)");
		}
	}
}
function clear_color(id)
{
	_obj = document.getElementById("cp" + id + "_Hex");
	update_color_and_field(id,"#" + _obj.value);
}

function open_colorpicker(id)
{
	open_colorpicker_simple(id);
}

function init_colors(id)
{
	var kleur = document.getElementById(id).value;
	
	_obj = document.getElementById("div_" + id);
	_obj.style.visibility = "hidden";
	_obj.style.height = "0px";
	
	_obj = document.getElementById("div_colorpicker_" + id);
	_obj.style.visibility = "visible";
	_obj.style.height = "100%";
	
	return kleur;
}

var hex_array = new Array("00","33","66","99","CC","FF");

function build_color_array(r,g,b)
{
	var what = 1;
	var kleuren_array = new Array();
	
	var kleur = "";
	
	for(var i = 0;i<hex_array.length;i++)
	{
		if(what==0)
		{
			r = hex_array[i];	
		}
		else if(what==1)
		{
			g = hex_array[i];	
		}
		else if(what==2)
		{
			b = hex_array[i];	
		}
		
		kleur = r+g+b;
		kleuren_array[kleuren_array.length] = kleur;
	}
	
	return kleuren_array;
}

function build_color_box_array(k1,k2,k3)
{
	var kleuren_array = new Array();
	
	for(var i = 0;i<hex_array.length;i++)
	{
		kleuren_array = kleuren_array.concat(build_color_array(k1,"00",hex_array[i]));
		kleuren_array = kleuren_array.concat(build_color_array(k2,"00",hex_array[i]));
		kleuren_array = kleuren_array.concat(build_color_array(k3,"00",hex_array[i]));
	}
	
	return kleuren_array;
}

function reset_color(id)
{
	_obj = document.getElementById(id);
	update_color_and_field(id,_obj.value);
}

function open_colorpicker_simple(id)
{
	if(last_color!=null)
	{
		reset_color(last_color);
	}
	
	var kleuren_array = new Array();
	kleuren_array = kleuren_array.concat(new Array("000000"));
	kleuren_array = kleuren_array.concat(new Array("000000"));
	kleuren_array = kleuren_array.concat(new Array("000000"));
	kleuren_array = kleuren_array.concat(new Array("000000"));
	kleuren_array = kleuren_array.concat(new Array("000000"));
	kleuren_array = kleuren_array.concat(new Array("000000"));
	kleuren_array = kleuren_array.concat(new Array("000000"));
	kleuren_array = kleuren_array.concat(new Array("333333"));
	kleuren_array = kleuren_array.concat(new Array("666666"));
	kleuren_array = kleuren_array.concat(new Array("999999"));
	kleuren_array = kleuren_array.concat(new Array("CCCCCC"));
	kleuren_array = kleuren_array.concat(new Array("FFFFFF"));
	kleuren_array = kleuren_array.concat(new Array("FF0000"));
	kleuren_array = kleuren_array.concat(new Array("00FF00"));
	kleuren_array = kleuren_array.concat(new Array("0000FF"));
	kleuren_array = kleuren_array.concat(new Array("FFFF00"));
	kleuren_array = kleuren_array.concat(new Array("00FFFF"));
	kleuren_array = kleuren_array.concat(new Array("FF00FF"));
	
	kleuren_array = kleuren_array.concat(new Array("000000"));
	kleuren_array = kleuren_array.concat(new Array("000000"));
	kleuren_array = kleuren_array.concat(new Array("000000"));
	kleuren_array = kleuren_array.concat(new Array("000000"));
	kleuren_array = kleuren_array.concat(new Array("000000"));
	kleuren_array = kleuren_array.concat(new Array("000000"));
	kleuren_array = kleuren_array.concat(new Array("000000"));
	kleuren_array = kleuren_array.concat(new Array("000000"));
	kleuren_array = kleuren_array.concat(new Array("000000"));
	kleuren_array = kleuren_array.concat(new Array("000000"));
	kleuren_array = kleuren_array.concat(new Array("000000"));
	kleuren_array = kleuren_array.concat(new Array("000000"));
	kleuren_array = kleuren_array.concat(new Array("000000"));
	kleuren_array = kleuren_array.concat(new Array("000000"));
	kleuren_array = kleuren_array.concat(new Array("000000"));
	kleuren_array = kleuren_array.concat(new Array("000000"));
	kleuren_array = kleuren_array.concat(new Array("000000"));
	kleuren_array = kleuren_array.concat(new Array("000000"));

	kleuren_array = kleuren_array.concat(build_color_box_array("00","33","66"));
	kleuren_array = kleuren_array.concat(build_color_box_array("99","CC","FF"));
	
	var kleuren_code = "<table><tr><td class='colorpicker'><table cellpadding='0' cellspacing='0'>";
	
	var kleur = init_colors(id);
	
	var max_row = 18;
	var diameter = 12;
	
	var aant = 0;
	var css = "";
	for(var i = 0;i<kleuren_array.length;i++)
	{
		if(aant==0)
		{
			kleuren_code += "<tr>";	
		}
		css = "color_pick";
		
		if(kleur=="#" + kleuren_array[i])
		{
			css="color_selected";
		}
		kleuren_code += "<td class='" + css + "'><div style='cursor:pointer;background:#" + kleuren_array[i] + "' style='width:" + diameter + "px;height:" + diameter + "px;' onclick='update_color_and_field(\"" + id + "\",\"#" + kleuren_array[i] + "\")'><img src='core/images/spacer.gif' style='width:" + diameter + "px;height:" + diameter + "px;' alt='#" + kleuren_array[i] + "'/></div></td>";	
		aant++;
		if(max_row==aant)
		{
			aant = 0;
			kleuren_code += "</tr>";
		}
	}
	
	if(aant!=0)
	{
		while(aant<max_row)
		{
			kleuren_code += "<td></td>";
			aant++;
		}
		kleuren_code += "</tr>";
	}
	
	kleuren_code += "</table></td><td><input type='button' value='terug' class='button_variable_size' onclick='reset_color(\"" + id + "\")' /></td></tr></table>";

	_obj.innerHTML = kleuren_code;
	
	last_color = id;
}

function update_color_and_field(id,color)
{
	_obj = document.getElementById(id);
	_obj.value = color;
	
	_obj = document.getElementById("div_" + id);
	_obj.style.visibility = "visible";
	_obj.style.height = "100%";
	
	_obj = document.getElementById("div_colorpicker_" + id);
	_obj.style.visibility = "hidden";
	_obj.style.height = "0px";
	
	update_color(id);
}

function update_color(id)
{
	if(document.getElementById("color_" + id))
	{
		_obj = document.getElementById("color_" + id);
		var kleur =  document.getElementById(id).value;
		if(kleur.length==7)
		{
			_obj.style.background = kleur;
		}
	}
}

function update_afbeelding(folder,query,bestand,large_width,large_height)
{
	bestand = bestand.split(" ").join("_");
	bestand = bestand.split("&").join("_en_");
	var url = "index.php?large_width=" + large_width +"&large_height=" + large_height + "&action=main_cropper&basic=1&query=" + query + "&bestand=" + bestand + "&folder=" + folder;

	open_cropper();
	
	/*myRef = window.open(url,'mywin',
'left=0,top=0,width=' + screen.width + ',height=' + screen.height + ',toolbar=0,resizable=0');*/

	var pic_url = website + folder + bestand;
	var folder = "../../../" + folder;
	
	var so = new SWFObject('core/assets/zapcropper_v2/cropperholder_v2.swf', 'holder', '100%', '100%', flashplayer , '#000000');

	so.addVariable('pic_url', pic_url)
	so.addVariable('save_url', folder);
	so.addVariable('cropfilename',bestand);
	so.addVariable('domain',website);
	so.addVariable('goto', query);
	so.addVariable('large_width', large_width);
	so.addVariable('large_height', large_height);
	so.addVariable('thumb_width', 100);
	so.addVariable('thumb_height', 100);
	so.addVariable('make_thumb', "false");
	so.addVariable('change_width', "true");
	so.addVariable('change_height', "true");
	so.addVariable('quality', cropper_quality);
	so.setAttribute('redirectUrl', 'index.php?action=main_noflash');
	so.addParam('scale', 'noscale');
	
	so.write('holder');	
}

function update_search(div)
{
	_obj = document.getElementById(div + "_searchbox");
	trigger_search(div,_obj.value);
}

function trigger_search(div,val)
{
	_obj = document.getElementById("zoek_txt");
	_obj.value = val;
	update_by_div(div);
}

function open_cropper()
{
	_obj = document.getElementById("holder");
	_obj.style.width = "100%";
	_obj.style.height = "100%";
	
	_obj = document.getElementById("application");
	_obj.style.visibility = "hidden";
	
	$("div#flash_afbeelding").hide();
	$("div#flash_afbeelding_bottom").hide();
	
	if(BrowserDetect.OS!="Mac")
	{
		_obj.style.position = "absolute";
		_obj.style.top = "0px";
	}

	//location.href="#holder";
}

function close_cropper(saved,path,file)
{
	
	$("div#flash_afbeelding").show();
	$("div#flash_afbeelding_bottom").show();
	
	_obj = document.getElementById("holder");
	_obj.innerHTML = "";
	_obj.style.height = "0px";
	_obj.style.width = "0px";
	
	_obj = document.getElementById("application");
	_obj.style.visibility = "visible";
	_obj.style.position = "static";
	if(saved=="0")
	{
		del_file(path + file);
	}
	else
	{
		if (path.indexOf("/inhoud/") != -1) {
			hideUpload();	
		}
		
		if (path.indexOf("/templates/") != -1) {
			update_by_div("div_xml");
			update_by_div("div_xml_bottom");
		}

		update_fiches();
	}
	location.href="#application";
}

function del_file(file)
{
	AjaxConnection();
	connection = new AjaxConnection("dynamic.php?page=main/ajax/delete.php&file=" + file);
	connection.connect("confirm_del");
}

function confirm_del(oResponse)
{
	var code = get_node(oResponse,"code");
	
	$("div#flash_afbeelding").show();
	$("div#flash_afbeelding_bottom").show();

	if (document.getElementById("help_afbeelding")) {
		document.getElementById("help_afbeelding").innerHTML = "";
		document.getElementById("flash_afbeelding").style.visibility = "visible";	
	}
	
	if (document.getElementById("div_xml")) {
		update_by_div("div_xml");
	}
	
	if (document.getElementById("div_xml_bottom")) {
		update_by_div("div_xml_bottom");
	}
	update_fiches();
}

function start_preload(id)
{
	_obj = document.getElementById(id);
	_obj.innerHTML = "bezig met verwerken";
}

function import_csv(extra_id)
{
	//start_preload("flashcontent");
	window.location = "index.php?action=adressen_import&type=add&fire=1&extra_id=" + extra_id;
}

function submit_form()
{
	document.check.submit();
}

function is_email(email)
{
   	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	
   	if(reg.test(email) == false) {
     	return false;
   	}
   	else
   	{
		return true;   
	}
}

function change_klant(id)
{
	_obj = document.getElementById(id);
	var url = "index.php?action=overzicht_index&klant_id=" + _obj.value;
	window.location = url;
}

function del_all(id,ids,table,extra_id,del_action,master_action,type,div)
{
	var id_array = ids.split("_");
	var checked = "";
	for(var i = 0;i<id_array.length;i++)
	{
		_obj = document.getElementById(id + "_" + id_array[i]);
		
		if(_obj.checked)
		{
			if(checked!="")
			{
				checked+="_";
			}
			checked+=id_array[i];
		}
	}
	
	if(checked!="")
	{
		window.location = "index.php?div=" + div + "&action=main_del&master_action=" + master_action + "&extra_id=" + extra_id + "&del_action=" + del_action + "&table=" + table + "&id=" + checked + "&type=" + type;		
	}
}

function check_all(id,ids)
{
	var id_array = ids.split("_");
	var checked = document.getElementById(id).checked;
	
	for(var i = 0;i<id_array.length;i++)
	{
		_obj = document.getElementById(id + "_" + id_array[i]);
			
		if(_obj)
		{
			_obj.checked = checked;
		}
		else
		{
			_obj = document.getElementById(id_array[i]);
			if(_obj)
			{
				_obj.checked = checked;
			}
		}
	}
}

function show_hide(id)
{
	_obj = document.getElementById(id);
	if(_obj.style.visibility=="hidden")
	{
		_obj.style.visibility = "visible";
		_obj.style.height = "100%";
	}
	else
	{
		_obj.style.visibility = "hidden";
		_obj.style.height = "0px";
	}
}

function get_window_height() {
	var windowHeight = 0;
	if (typeof(window.innerHeight) == 'number') {
		windowHeight = window.innerHeight;
	}
	else {
		if (document.documentElement && document.documentElement.clientHeight) {
			windowHeight = document.documentElement.clientHeight;
		}
		else {
			if (document.body && document.body.clientHeight) {
				windowHeight = document.body.clientHeight;
			}
		}
	}
	return windowHeight;
}
		
function resize() {
	if (document.getElementById) {
		var windowHeight = get_window_height();
		if (windowHeight > 0) {
			var footerElement = document.getElementById('footer');
			var footerHeight  = footerElement.offsetHeight;
			
			footerElement.style.top = (windowHeight - footerHeight) + 'px';
			footerElement.style.visibility = "visible";
		}
	}
}

function fiche_rollover(e)
{
	var id = e.id.split("_sub").join("");
	
	change_css(id,"selected");
	change_css(id + "_sub","selected");
	$("#" + id + "_sub input, #" + id + "_sub div").show();
}
	
function fiche_rollout(e)
{
	var id = e.id.split("_sub").join("");
	
	var number = id.substr(3);
	var css = "uneven";
	
	if(number % 2)
	{
		css = "even";
	}
	
	change_css(id,css);
	change_css(id + "_sub",css);
	$("#" + id + "_sub input, #" + id + "_sub div").hide();
}
		
function focus_textfield(id)
{
	_obj = document.getElementById(id);
	_obj.select();
	var css = _obj.className;
	if(css!="textfield_error")
	{
		change_css(id,"textfield_focus");
	}	
}

document.onkeypress = key_press; 

function key_press(evt) {
	var evt = (evt) ? evt : ((event) ? event : null);
	var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
 	
  	if((node.id=="zoek_txt")&&(evt.keyCode==13))
	{
		update_fiches();
	}
	return true;
}


function change_textfield(id)
{
	change_css(id,"textfield_focus");	
}

function change_css(id,css)
{
	_obj = document.getElementById(id);
	if (_obj) {
		_obj.className = css;
	}
}

function blur_textfield(id)
{
	_obj = document.getElementById(id);
	var css = _obj.className;

	change_css(id,"textfield");
}

function select_input(id,css)
{	
	_obj = document.getElementById(id);
	if(_obj)
	{
		_obj.focus();
		change_css(id,css);	
	}
}

