function AjaxConnection(url) 
{
	url = "core/ajax/" + url + "&rand=" + Math.floor(Math.random()*9999);

	if(debug_id)
	{
		window.open(url);
	}
	if(gebruiker_id == 1)
	{
		//alert(url);
		}
	
	this.connect=connect;
	this.uri= url;
} 


function start_preloading(div)
{
	if(advanced_preloading==1)
	{
		_obj = document.getElementById("preloader");
		_obj.innerHTML = "<img src='" + images_dir + "preloader.gif' />";
		_obj.style.visibility = "visible";

		if((BrowserDetect.OS=="Windows")&&(BrowserDetect.browser=="Explorer")&&(BrowserDetect.version<7))
		{
			scroll(0,0);
			_obj.style.height = "100%"
			_obj.style.position = "fixed";
		}
	}
}

function end_preloading(div)
{
	_obj = document.getElementById("preloader");
	
	if (_obj) {
	_obj.style.visibility = "hidden";

		if((BrowserDetect.OS=="Windows")&&(BrowserDetect.browser=="Explorer")&&(BrowserDetect.version<7))
		{
			_obj.style.position = "absolute";
			_obj.style.height = "0%"
			_obj.innerHTML = "";
		}
	}
}

function connect(return_func)
{
	this.x=init_object();
    this.x.open("GET", this.uri,true);
    var self = this;
    
	this.x.onreadystatechange = function() 
	{
		if (self.x.readyState != 4)
		{
        	return;
		}
        eval(return_func + '(self.x.responseText)');
        delete self.x;
    }
	
	this.x.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	this.x.send('');
}

$(document).ready(function(){
	animateMeters();		   
	if (theme == "zap_2010") {
		Cufon.replace('span.title');
		Cufon.replace('.grid-header', {hover: true});
	//	Cufon.replace('.navigation_main', {hover: true});	
		Cufon.replace('.formmaker_title');	
		Cufon.replace('span.title_sub');
	}
});

function init_object() 
{
	var x;
	try 
	{
		x=new ActiveXObject("Msxml2.XMLHTTP");
	} 
	catch (e) 
	{
		try 
		{
			x=new ActiveXObject("Microsoft.XMLHTTP");
        } 
		catch (oc) 
		{
			x=null;
        }
	}      
    
	if(!x && typeof XMLHttpRequest != "undefined")
	{
       x = new XMLHttpRequest();
	}
    
	if (x)
	{
		return x;
	}
}

var request_array = new Array();
var listing_fiches = false;

function list_fiches(div,table,action,gebruiker_id,extra_id,del_action,klant_id,beheren,master_action,type,extra_param1)
{
	listing_fiches = true;
	start_preloading(div);
	order = "";
	order_mode = "ASC";
	sql = "get_fiches.php?taal_id=" + taal_id + "&div=" + div + "&table=" + table + "&action=" + action + "&gebruiker_id=" + gebruiker_id + "&extra_id=" + extra_id + "&del_action=" + del_action + "&beheren=" + beheren + "&klant_id=" + klant_id+ "&master_action=" + master_action + "&type=" + type + "&extra_param1=" + extra_param1;	
	request_array[div] = sql;
	update_fiches();
}

function change_zoek(div,new_zoek)
{
	_obj = document.getElementById("zoek_txt");
	_obj.value = new_zoek;	
	update_by_div(div);
}

function change_order(div,new_order)
{
	if(order==new_order)
	{
		if(order_mode=="ASC")
		{
			order_mode = "DESC";
		}
		else
		{
			order_mode = "ASC";	
		}
	}
	else
	{
		order_mode = "DESC";	
	}
	
	order = new_order;
	update_by_div(div);
}

function update_by_div(div)
{
	start_preloading(div)
	sql = request_array[div];
	
	update_fiches();
}

function change_start(div,new_start)
{
	start = new_start;	
	update_by_div(div);
}

function trigger_update_fiches()
{
	_obj = document.getElementById("application");
	if(_obj.style.visibility!="hidden")
	{
		var update = true;
		_obj = document.getElementById("zoek_txt");
		
		if(_obj)
		{
			if(_obj.value!="")
			{
				update = false;	
			}
		}
		
		_obj = document.getElementById("div_xml_stats_searchbox");
		
		if(_obj)
		{
			//alert("test");
			if(_obj.value!="")
			{
				update = false;	
			}
			//alert(_obj.value);
		}
		
		if(update)
		{
			update_fiches();	
		}
	}
}

function update_fiches()
{
	var extra = "";
	
	_obj = document.getElementById("zoek_txt");
	
	if(_obj)
	{
		if(zoek!=_obj.value)
		{
			start = 0;
		}
		zoek = _obj.value;
		extra += "&zoek=" + zoek;	
	}
	
	if(order!="")
	{
		extra += "&order=" + order.split("xx.").join("");
		extra += "&order_mode=" + order_mode;
	}
	
	
	if (sql != "undefined") {
		connection = new AjaxConnection(sql + extra + "&start=" + start);
		connection.connect("build_fiches");
	}
}

function get_calendar(div,datum)
{
	_obj = document.getElementById("datum");
	_obj.value = datum;
	connection = new AjaxConnection("dynamic.php?div=" + div + "&page=main/ajax/calendar.php&datum=" + datum);
	connection.connect("build_calendar");
	
	if(document.getElementById("date_written"))
	{
		_obj = document.getElementById("date_written");
		datum = datum.split(":");
		datum = datum[0];
		datum = datum.split(" ").join(" om ");
		datum += "u";
		_obj.innerHTML = datum;
		
		//can_send();
	}
}


function can_send()
{
	_obj = document.getElementById("button");
	_obj.style.visibility = "hidden";
	_obj = document.getElementById("datum");
	var datum = _obj.value;
		
	connection = new AjaxConnection("dynamic.php?page=mails/ajax/can_send.php&id=" + id + "&datum=" + datum);		
	connection.connect("build_can_send");
		


}
/*
if(action=="mails_verzend")
{
	window.setTimeout("can_send();", 1);
}*/

function build_can_send(oResponse)
{
	_obj = document.getElementById("button");
	_obj.style.visibility = "visible";
	
	var datum = get_node(oResponse,"code");
	if(datum!="")
	{
		datum = datum.split(" ").join(" om ");
	
		if(confirm("De verzending van uw nieuwsbrief is in wachtrij geplaatst. Voorziene start van de verzending: " + datum)) {
			document.getElementById("datum").value = get_node(oResponse,"code");
			document.getElementById("check").submit();
		} 
	} else {
		document.getElementById("check").submit();	
	}
	
}
	
function update_calendar_year(div,month,uur,id)
{
	_obj = document.getElementById(id);
	get_calendar(div,_obj.value + "-" + month + "-01 " + uur + ":55:00");
}

function update_calendar_month(div,year,uur,id)
{
	_obj = document.getElementById(id);
	get_calendar(div,year + "-" + _obj.value + "-01 " + uur + ":55:00");
}
function update_calendar_hour(div,datum,id)
{
	_obj = document.getElementById(id);
	get_calendar(div,datum + " " + _obj.value + ":55:00");
}

function build_calendar(oResponse)
{
	var code = get_node(oResponse,"code");
	var div = get_node(oResponse,"div");
	_obj = document.getElementById(div);
	if(_obj)
	{
		_obj.innerHTML = code;	
	}
}

function get_tekst(titel,div,role)
{
	/*
	
		request om help tekst op te halen indien er op 'help tonen' geklikt worden
		
	*/
	/*if((titel == "help_templates_index")&&(role==2))
	{
		titel = "help_templates_surfer_index";
	}*/

	
	_obj = document.getElementById(div);
	if(_obj.innerHTML=="")
	{		
		connection = new AjaxConnection("dynamic.php?div=" + div + "&action=teksten_index&page=teksten/ajax/tekst.php&titel=help_" + titel);
		
		connection.connect("build_tekst");
	}
	else
	{
		_obj.innerHTML = "";	
	}
}

function build_tekst(oResponse)
{
	/*
	
		tekst tonen indien er op 'open help' geklikt werd
		
	*/
	
	var code = get_node(oResponse,"code");
	var div = get_node(oResponse,"div");
	_obj = document.getElementById(div);
	if(_obj)
	{
		code = "<div class='span_bg_help' style='padding-left:5px;'><p>" + code + "<p></div>";
		
		code = code.split("&apos;").join("'");

		_obj.innerHTML = code;	
	}
}

function animateMeters()
{
	$("span.meter img.mails_verzonden_left").css("width", 0);	
	$("span.meter img.mails_verzonden_right").css("width", 150);
	
	$("span.meter img").each(function(){
		$(this).animate({ width: $(this).attr("rel")  }, 1500 );
	});
}

function build_fiches(oResponse)
{
	/*
	
		fiches die met een request opgevraagt werden nu tonen. en indien de parameter update aanwezig is
		om de 10 seconden het grid refreshen
	
	*/
	
	var code = get_node(oResponse,"code");
	var div = get_node(oResponse,"div");
	
	end_preloading(div);
	
	var update = get_node(oResponse,"update");

	_obj = document.getElementById(div);

	if (_obj) {
		_obj.innerHTML = code;
	}
	

	if (theme == "zap_2010") {

		
		
		
		var $tr = $("div#" + div + " table.buttons").parent().parent().parent();

		var code = $("div#" + div + " table.buttons").html();
		
		
		
		if (code) {
			code = "<table style='float:left;width:300px;'>" + code + "</table>";
			if (action == "inhoud_index") {
				//$("div#" + div + "").find(".grid_header").append(code);
			} else if (action == "templates_index") {
				$("div#" + div + " table.buttons").hide();
				$tr.prev().find('td').find("table").remove();
				$tr.prev().find('td').append(code);
			} else {
				$("div#" + div + " table.buttons").hide();
				$("body").find("span.title").parent().find("table").remove();
				$("body").find("span.title").parent().append(code);
			}
		}
		
		
		if (action == "mails_index") {
			$(".buttons_mails").hide();	
			$(".grid_fiche").addClass("grid_fiche_border");
			$("table.state input").show();
		}
	}
	
	animateMeters();

	if(div=="div_klanten")
	{
		var ids = get_node(oResponse,"ids");
		get_klanten(ids);	
	} else if(div=="div_groepen")
	{
		get_groepen();	
	}
	select_input("zoek_txt","textfield");
		
	if(update==1)
	{
		setTimeout("trigger_update_fiches()", 10000);
	}
}

function get_node(xml,node,decode)	
{
	/*
	
		een bepaald deel uit de xml halen (vb. de content -> <content>ok</content>, value = 'ok');
	
	*/
	
	var oNode_array = xml.split("</" + node + ">");
	oNode_array = oNode_array[0];
	oNode_array = oNode_array.split("<" + node + ">");
	
	var node_value = uncompressCode(oNode_array[1]);
	var node_value =  decode_xml(node_value);
	
	return node_value;
}

function decode_xml(xml)
{
	//xml = xml.split("&apos;").join("'");
	xml = xml.replace(/%2B/g," ");
	xml = xml.replace(/%80/g,"�");

	xml = unescape(xml);
	return xml;
}

