function clearText(field){
	if (field.defaultValue == field.value)
		field.value = ""
}

function setText(field, text) {
	if (field.defaultValue == field.value || field.value == '')
		field.value = text;
}

function disablePage(onclickHandler, zindex, color) {
	// LET OP onclickHandler meegeven ZONDER quotes. HET IS GEEN STRING

	if (!color) {
		color = "#222222";
	}

	if (!zindex) {
		zindex = 3;
	}

	var div = document.createElement('div');
	div.setAttribute("id", "disabled_page_div");
	div.setAttribute("style", "-moz-opacity: .60; opacity: .60;");
	document.body.appendChild(div);

	div.style.display = "block";
	div.style.position = "absolute";
	div.style.zIndex = zindex-0;
	div.style.top = 0;
	div.style.left = 0;
	div.style.backgroundColor = color;
	div.style.filter = "alpha(opacity=60)";

	if (navigator.appName == "Microsoft Internet Explorer") {
		div.style.width = document.documentElement.clientWidth + document.documentElement.scrollLeft + "px";
		div.style.height = document.documentElement.clientHeight + document.documentElement.scrollTop + "px";
		
	} else {
		div.style.height = window.innerHeight + window.scrollMaxY + "px";
		div.style.width = window.innerWidth + window.scrollMaxX + "px";
	}

	if (onclickHandler) {
		div.onclick = function () {
			onclickHandler.call();
		};
	}
}

function enablePage() {
	var div = document.getElementById("disabled_page_div");
	document.body.removeChild(div);
}

function insertProductData(params) {
	if(params["response"] != null) {
		var element = document.getElementById("productDetails");
		element.innerHTML = "<div class=\"popupHeader\"><a href=\"javascript:window.parent.closeProduct();\">Sluiten</a></div><div id=\"productDetailsInner\">"+params["response"]+"</div>";
	}
}

function showProduct(productID)  {
	var params = new Array();
	ajaxSendGet("/product_details?product_id="+productID, insertProductData, params);
	disablePage(closeProduct);
	openDivPop("productDetails", 750, 500);
}

function closeProduct() {
	closeDivPop("productDetails");
	var params = new Array();
	params["response"] = "Bezig met laden...";
	insertProductData(params);
}

function orderProcessor(params) {
	var target = document.getElementById("productDetailsInner");
	target.innerHTML = params["response"];
}

function orderProduct(url) {
	var params = new Array();
	ajaxSendGet(url, orderProcessor, params);
}

function orderProductSubmit(url) {
	var params = new Array();
	var form = document.getElementById("productOrderForm");
	
	var productname	   = escape(form.productname.value);	
	var organization   = escape(form.organization.value);
	var contact_person = escape(form.contact_person.value);
	var tel_nr		   = escape(form.tel_nr.value);
	var address		   = escape(form.address.value);
	var e_mail 		   = escape(form.e_mail.value);
		
	if(productname == "" || organization == "" || contact_person == "" || tel_nr == "" || address == "" || e_mail == "") {
		var errorElement = document.getElementById("orderError");
		errorElement.innerHTML = "Vul alle velden in";
		errorElement.style.visibility = "visible";
		errorElement.style.height = "20px";
	}
	else {
		var content = 'ingevuld=1&productname='+productname+'+&organization='+organization+'&contact_person='+contact_person+'&tel_nr='+tel_nr+'&address='+address+'&e_mail='+e_mail;
		ajaxSendPost(url, orderProcessor, params, content);
	}
}

/**
 *	News articles
 **/
function insertNewsData(params) {
	if(params["response"] != null) {
		var element = document.getElementById("articleDetails");
		element.innerHTML = "<div class=\"popupHeader\"><a href=\"javascript:window.parent.closeArticle();\">Sluiten</a></div><div id=\"articleDetailsInner\">"+params["response"]+"</div>";
	}
}
function showArticle(articleID) {
	var params = new Array();
	ajaxSendGet("/nieuws/nieuws_detail?story_id="+articleID, insertNewsData, params);
	disablePage(closeArticle);
	openDivPop("articleDetails", 750, 500);
}
function closeArticle() {
	closeDivPop("articleDetails");
	var params = new Array();
	params["response"] = "Bezig met laden...";
	insertNewsData(params);
}

/**
 *	Projects on the 'Etalage'-page
 **/
function insertProjectData(params) {
	if(params["response"] != null) {
		var element = document.getElementById("projectDetails");
		element.innerHTML = "<div class=\"popupHeader\"><a href=\"javascript:window.parent.closeProject();\">Sluiten</a></div><div id=\"projectDetailsInner\">"+params["response"]+"</div>";
	}
}
function showProject(projectID) {
	var params = new Array();
	ajaxSendGet("/etalage/project_detail?projectID="+projectID, insertProjectData, params);
	disablePage(closeProject);
	openDivPop("projectDetails", 750, 500);
}
function closeProject() {
	closeDivPop("projectDetails");
	var params = new Array();
	params["response"] = "Bezig met laden...";
	insertProjectData(params);
}

/**
 * Contact-form in a pop-up
 **/
function insertFormData(params) {
	if(params["response"] != null) {
		var element = document.getElementById("formDetails");
		element.innerHTML = "<div class=\"popupHeader\"><a href=\"javascript:window.parent.closeForm();\">Sluiten</a></div><div id=\"formDetailsInner\">"+params["response"]+"</div>";
	}
}
function showForm(type) {
	var params = new Array();
	ajaxSendGet("/contact_detail?type="+type, insertFormData, params);
	disablePage(closeForm);
	openDivPop("formDetails", 750, 500);
}
function closeForm() {
	closeDivPop("formDetails");
	var params = new Array();
	params["response"] = "Bezig met laden...";
	insertFormData(params);
}
function formProcessor(params) {
	var target = document.getElementById("formDetailsInner");
	target.innerHTML = params["response"];
}
function sendFormSubmit(url) {
	var params = new Array();
	var form = document.getElementById("formForm");
	
	var title	   		 = escape(form.title.value);
	var offeredBy	   	 = escape(form.offeredBy.value);
	var description	   	 = escape(form.description.value);
	var level	   		 = escape(form.level.value);
	var gravity	   		 = escape(form.gravity.value);
	var goals	   		 = escape(form.goals.value);
	var duration	   	 = escape(form.duration.value);
	var costs	   		 = escape(form.costs.value);
	var period 			 = escape(form.period.value);
	var materials	   	 = escape(form.materials.value);
	var max_participants = escape(form.max_participants.value);
	var special	   		 = escape(form.special.value);
	var contactperson	 = escape(form.contactperson.value);
	var address	   		 = escape(form.address.value);
	var postcode	   	 = escape(form.postcode.value);
	var city	   		 = escape(form.city.value);
	var telephoneNumber	 = escape(form.telephoneNumber.value);
	var email	   		 = escape(form.email.value);
	var fax	   			 = escape(form.fax.value);
	var functie	  	     = escape(form.functie.value);
	var explanation		 = escape(form.explanation.value);
	var type 			 = form.type.value;
		
	var disciplines = "";
	var ob = document.getElementById("disciplines"); 
	while (ob.selectedIndex != -1) { 
		disciplines += (ob.options[ob.selectedIndex].value) + ",";
		ob.options[ob.selectedIndex].selected = false;
	}
			
	if(title=="" || offeredBy=="" || description=="" || disciplines=="" || level=="" || gravity=="" || goals=="" || duration=="" || costs=="" || materials=="" || max_participants=="" || special=="" || contactperson=="" || telephoneNumber=="" || email=="" || period=="") {
		var errorElement = document.getElementById("newsletterError");
		errorElement.innerHTML = "Vul alle velden in";
		errorElement.style.visibility = "visible";
		errorElement.style.height = "20px";
	}
	else {
		var content = 'ingevuld=1&type='+type+'&title='+title+'&offeredBy='+offeredBy+'&description='+description+'&disciplines='+disciplines+'&level='+level+'&gravity='+gravity+'&goals='+goals+'&duration='+duration+'&costs='+costs+'&period='+period+'&materials='+materials+'&max_participants='+max_participants+'&special='+special+'&contactperson='+contactperson+'&address='+address+'&postcode='+postcode+'&city='+city+'&telephoneNumber='+telephoneNumber+'&email='+email+'&fax='+fax+'&function='+functie+'&explanation='+explanation;
		ajaxSendPost(url, formProcessor, params, content);
	}
}



function openDivPop(id, divWidth, divHeight) {
	// Width rekenen
	if (!divWidth) {
		divWidth = 750;
	}		

	width = document.getElementById("container_main").offsetWidth;
	width = 0;

	if (width == 0 || !width) {
		width = document.body.offsetWidth;
	}

	if (navigator.appName == 'Microsoft Internet Explorer')
		width = width;
	else
		width = width-40;

	var divLeft = Math.round(width-divWidth);
	divLeft = Math.round(divLeft/2);

	// Height rekenen
	if (!divHeight)
		divHeight = 500;

	height = window.innerHeight;
	if (height == 0 || !height) {
		//height = document.body.offsetHeight;
		height =  document.documentElement.clientHeight;
	}

	// Div aanzetten
	if (!document.getElementById(id)) {
		var popdiv = document.createElement("div");
		popdiv.setAttribute("id", id);
		document.body.appendChild(popdiv);
	}
	
	var divTop = Math.round(height-divHeight);
	divTop = Math.round(divTop/2);

	var element = document.getElementById(id);
	element.style.visibility = 'visible';
	element.style.display = 'block';
	element.style.height = divHeight+"px";
	element.style.width = divWidth+"px";
	element.style.top =  divTop+"px";
	element.style.left= divLeft+"px";
	element.style.opacity = 1;
	element.style.filter = 'alpha(opacity=100)';
	
	try {
		var elementToHide = document.getElementById("gids_producten");
		
		if(elementToHide != null) {
			elementToHide.style.display = 'none';
		}
		
		
		var elementToHide = document.getElementById("gids_form_homepage");
		
		if(elementToHide != null) {
			elementToHide.style.display = 'none';
		}
	}
	catch(e) { }
}

function closeDivPop(id) {
	document.getElementById(id).style.visibility = 'hidden';
	document.getElementById(id).style.display = 'none';
	enablePage();
	
	try {
		var elementToShow = document.getElementById("gids_producten");
		
		if(elementToShow != null) {
			elementToShow.style.display = 'block';
		}
		
		
		var elementToShow = document.getElementById("gids_form_homepage");
		
		if(elementToShow != null) {
			elementToShow.style.display = 'block';
		}
	}
	catch(e) { }
}



function ajaxSendPost(script, handlerfunction, args, content) {
	var xmlHttp;
	try {
		xmlHttp=new XMLHttpRequest();
	} catch (e) {
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}

	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState==4) {
			args["response"] = xmlHttp.responseText;
			if (handlerfunction)
				handlerfunction.call(this, args, false);
		}
	}

	xmlHttp.open("POST", script, true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", content.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(content);
}

function ajaxSendGet(script, handlerfunction, args)
{
	var xmlHttp;
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	} catch (e) {
		// Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				// Kut browser
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}

	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState==4) {
			args["response"] = xmlHttp.responseText;
			if (handlerfunction) {
				handlerfunction.call(this, args, false);
			}
		}
	}


	xmlHttp.open("GET", script, true);
	xmlHttp.send(null);
}

function ajaxFill(params) {
	try {
		if (params["response"] && params["elementid"])
			document.getElementById(params["elementid"]).innerHTML = params["response"];
	} catch (e) { }
}
