var connection;
var connection2;
var connectionform;
var imageconnection;
var active_obj = null;
var active_content = null;
var active_content2 = null;
var active_contentform = null;
var popupwidth = 410;
var popupheight = 303;
var offset_y = 60;

function checkEnabled()
{
	if (active_obj!=null) return false;
	else return true;
}

function relocatePopup()
{
	setPopupLocation(active_obj);
	var d = document.getElementById('ibody');
	//var d2 = document.getElementById('blackoutdiv');
	var divHeight;
	try{
		if(d.clientHeight){
			document.getElementById('blackoutdiv').style.height = d.clientHeight;
		} else if(document.body.scrollHeight){
			document.getElementById('blackoutdiv').style.height = d.scrollHeight;
		}
	} catch(e){document.getElementById('blackoutdiv').style.height = '100%';}
	var d2 = document.layers ? document.layers['blackoutdiv'] :
	document.getElementById ?  document.getElementById('blackoutdiv').style :
	document.all['blackoutdiv'].style;




	if (d2.setProperty)
	{ //Mozilla
		var docHeight = Math.max(d.offsetHeight, document.documentElement.clientHeight, d.scrollHeight, document.documentElement.scrollHeight, window.innerHeight);
		var docWidth = Math.max(d.offsetWidth, document.documentElement.clientWidth, d.scrollWidth, document.documentElement.scrollWidth, window.innerWidth);
		d2.setProperty('height',docHeight+offset_y,null);
		d2.setProperty('width',docWidth+offset_y,null);
	}
	else
	{
		var docHeight = Math.max(d.offsetHeight, document.documentElement.clientHeight, document.documentElement.scrollHeight, d.scrollHeight, d.style.pixelHeight, d.style.height);
		var docWidth = Math.max(d.offsetWidth, document.documentElement.clientWidth, document.documentElement.scrollWidth, d.scrollWidth, d.style.pixelWidth, d.style.width);
		//d3 = document.getElementById('blackoutdiv');//d2.height = docHeight;
		//d3.style.height = docHeight;
		d2.height = docHeight+offset_y;
		d2.width = docWidth+offset_y;
	}
}

function setPopupLocation(obj)
{
	
	if (obj.setProperty)
	{ //Mozilla
		var scrollxpos = window.pageXOffset;
		var scrollypos = getScrollPosition();
		var windowwidth = getWindowSize().x;
		var windowheight = getWindowSize().y;
		var newy = scrollypos + (windowheight/2) - (popupwidth / 2);

		if (newy < 0) obj.top = 0;
		else obj.top = (newy + offset_y) + 'px';
		obj.left = ((windowwidth / 2) - (popupwidth /2))  + 'px';
	}
	else
	{ //IE
		var windowwidth = getWindowSize().x;
		var windowheight = getWindowSize().y;
		var scrollxpos = document.body.scrollLeft;
		var scrollypos = getScrollPosition();

		var newy = scrollypos + (windowheight/2) - (popupwidth / 2);
		if (newy < 0) obj.top = 0;
		else obj.top = (newy + offset_y) + 'px';
		obj.left = ((windowwidth / 2) - (popupwidth /2))  + 'px';


	}
}

function callBack(content){
	active_content.innerHTML = content;
	headerReplacement(content);
}

function callBack2(content)
{
	active_content2.innerHTML = content;
	headerReplacement(active_content2);
}

function callBackForm(content)
{
	if(active_contentform){
		active_contentform.innerHTML = content;
		headerReplacement(active_contentform);
	}
}

function xmlRequest(page, postvars, destination_id,focus_id)
{
	// except IE
	if (connection)
	{
		delete connection;
	}

	active_content = document.getElementById(destination_id);
	if(window.XMLHttpRequest)
	{
		connection = new XMLHttpRequest();
	} //IE
	else if(window.ActiveXObject)
	{
		try
		{
			connection = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e)
		{
			connection = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}

	if (connection)
	{
		connection.open("POST", page, true);
		connection.onreadystatechange = function()
		{
						if (connection.readyState != 4)
						{
							var d = document.getElementById(destination_id);
							d.innerHTML = '<div style="height:'+ d.offsetHeight + 'px;"><img src="/images/loading.gif" border="0"></div>';
							return;
						}
						eval('callBack(connection.responseText);');
						if(focus_id){
							try{
								var fcs = document.getElementById(focus_id);
								if(fcs) fcs.focus();
							} catch(e){return;}
						}
						delete connection;
		}
		connection.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		connection.send(postvars);
	}

}


function xmlImageReloadRequest(page, postvars, destinationimg_id)
{
		var imgcontentelt = document.getElementById(destinationimg_id);
		imgcontentelt.src = "" + page +"?" + postvars;
}

function xmlRequestEval(page, postvars)
{
	// except IE
	if (connection)
	{
		delete connection;
	}
	if(window.XMLHttpRequest)
	{
		connection = new XMLHttpRequest();
	} //IE
	else if(window.ActiveXObject)
	{
		try
		{
			connection = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e)
		{
			connection = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}

	if (connection)
	{
		connection.open("POST", page, true);
		connection.onreadystatechange = function()
		{
						if (connection.readyState != 4)
						{
							return;
						}
						eval(connection.responseText);
						delete connection;
		}
		connection.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		connection.send(postvars);
	}
}

function xmlRequest2(page, postvars, destination_id)
{
	// except IE

	if (connection2)
	{
		delete connection2;
	}

	active_content2 = document.getElementById(destination_id);


	if(window.XMLHttpRequest)
	{
		connection2 = new XMLHttpRequest();
	} //IE
	else if(window.ActiveXObject)
	{
		try
		{
			connection2 = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e)
		{
			connection2 = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}

	if (connection2)
	{
		connection2.open("POST", page, true);
		connection2.onreadystatechange = function()
		{
						if (connection2.readyState != 4)
						{
							var d = document.getElementById(destination_id);
							d.innerHTML = '<div style="height:'+ d.offsetHeight + 'px;"><img src="/images/loading.gif" border="0"></div>';
							return;
						}
						eval('callBack2(connection2.responseText);');
						delete connection2;
		}
		connection2.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		connection2.send(postvars);
	}

}

function xmlRequestForm(page, postvars, destination_id,dwdw)
{
	var dwdw = dwdw || window;
	if (connectionform)
	{
		delete connectionform;
	}

	active_contentform = dwdw.document.getElementById(destination_id);


	if(window.XMLHttpRequest)
	{
		connectionform = new XMLHttpRequest();
	} //IE
	else if(window.ActiveXObject)
	{
		try
		{
			connectionform = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e)
		{
			connectionform = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}

	if (connectionform)
	{
		connectionform.open("POST", page, true);
		if(destination_id){
			connectionform.onreadystatechange = function()
			{
				if (connectionform.readyState != 4)
				{
					var d = dwdw.document.getElementById(destination_id);
					var d = document.getElementById(destination_id);
					d.innerHTML = '<div style="height:'+ d.offsetHeight + 'px;"><img src="/images/loading.gif" border="0"></div>';
					return;
				}
				eval('callBackForm(connectionform.responseText);');
				delete connectionform;
			}
		}
		connectionform.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		connectionform.send(postvars);
	}

}

function xmlFormSubmit(form_id, destination_id,the_action,fwdw,dwdw)
{
	var postvars="";
	var i;
	var fwdw  = fwdw || window;
	var dwdw  = dwdw || window;
	theform = fwdw.document.getElementById(form_id);
	for (i=0;i<theform.elements.length;i++)
	{
		//check radio, checkbuttons and selects
		if (theform.elements[i].type=='text' || theform.elements[i].type=='textarea' || theform.elements[i].type=='hidden' || (theform.elements[i].type=='radio' && theform.elements[i].checked) || (theform.elements[i].type=='checkbox' && theform.elements[i].checked) || theform.elements[i].type=='password')
		{
			if (postvars.length==0)	postvars = theform.elements[i].name + "=" + escape(theform.elements[i].value);
			else postvars = postvars + "&" + theform.elements[i].name + "=" + escape(theform.elements[i].value);
		}
		else if (theform.elements[i].type=='select-one' || theform.elements[i].type=='select')
		{
			if (postvars.length==0)	postvars = theform.elements[i].name + "=" + escape(theform.elements[i].options[theform.elements[i].selectedIndex].value);
			else postvars = postvars + "&" + theform.elements[i].name + "=" + escape(theform.elements[i].options[theform.elements[i].selectedIndex].value);
		}
	}
	if(the_action){
		xmlRequestForm(the_action, postvars, destination_id,dwdw);
	} else {
		xmlRequestForm(theform.action, postvars, destination_id,dwdw);
	}
	return false;
}

function popUp(objid, pwidth, pheight)
{
	if (active_obj!=null) return false;

	var obj = document.layers ? document.layers[objid] :
	document.getElementById ? document.getElementById(objid).style :
	document.all[objid].style;

	popupwidth = pwidth;
	popupheight = pheight;

	obj.width = pwidth+"px";
	obj.height = pheight+"px";

	setPopupLocation(obj);
	active_obj = obj;
	obj.visibility = document.layers ? "show" : "visible";
	var d = document.getElementById('ibody');
	var d2 = document.getElementById('blackoutdiv');
	/*;
	if (d.offsetHeight)
	{
		divHeight=d.offsetHeight;
		d2.style.height = divHeight;
	}
	else if(d.style.pixelHeight)
	{
		divHeight=d.style.pixelHeight;
		d2.style.height = divHeight;
	}*/
	try{
		var divHeight = 0;
		if(window.innerHeight){
			divHeight = window.innerHeight;
		}
		if(document.documentElement.clientHeight && document.documentElement.clientHeight > 0){
			divHeight = document.documentElement.clientHeight > divHeight ? document.documentElement.clientHeight : divHeight;
		} 
		if(document.body.clientHeight && document.body.clientHeight > 0){
			divHeight = document.body.clientHeight > divHeight ? document.body.clientHeight : divHeight;
		}
		if(document.body.scrollHeight && document.body.scrollHeight > 0){
			divHeight = document.body.scrollHeight > divHeight ? document.body.scrollHeight : divHeight;
		}
		d2.style.height = divHeight + 'px';
	} catch(e){document.getElementById('blackoutdiv').style.height = '100%';}
	d2.style.visibility = "visible";
}

function closeActivePopUp()
{
	active_obj.visibility = document.layers ? "hide" : "hidden";
	active_obj = null;
	var d2 = document.getElementById('blackoutdiv');
	d2.style.visibility = "hidden";
}

//TOOLTIP FUNCTIONS

var fadetimer;
var fadetimer1;
var fadetimer2;
var fadetimer3;
var fadealpha = 0;

function showTooltipHover(tooltext,obj_id, vlaaitype)
{
	elt = document.getElementById('tooltiphover');
	elt.innerHTML = tooltext;

	obj_pos = findPos(document.getElementById(obj_id));

	var obj2 = document.getElementById('tooltiphover').style;

	obj2.backgroundImage = 'url(/images/hover_bg' + vlaaitype + '.png)';

	if (obj2.setProperty){ //Mozilla
		obj2.setProperty('top',(obj_pos.top - 75) + 'px',null);
		obj2.setProperty('left',(obj_pos.left - 10) + 'px' ,null);
	} else { //IE
		obj2.top = obj_pos.top - 75;
		obj2.left = obj_pos.left - 10;
	}
	if (fadetimer) clearInterval(fadetimer);
	fadealpha = 0;
	fadetimer = setInterval("fadeTooltipHover(1,'" + vlaaitype + "')",20);
}

function hideTooltipHover(vlaaitype)
{
	if (fadetimer) clearInterval(fadetimer);
	fadetimer = setInterval('fadeTooltipHover(-1,"' + vlaaitype + '")',20);
}

function fadeTooltipHover(d, vlaaitype)
{
	obj = document.getElementById('tooltiphover');
	if (d==1)
	{
		if (fadealpha>0) obj.style.visibility = "visible";
		fadealpha += 20;
		if (fadealpha >= 100)
		{
			fadealpha = 100;
			clearInterval(fadetimer);
		}

	}
	else
	{
		fadealpha -= 20;
		if (fadealpha<=0)
		{
			fadealhpa = 0;
			obj.style.visibility = "hidden";
			clearInterval(fadetimer);
		}
	}
	obj.style.opacity = fadealpha * .01;
	obj.style.filter =   'progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=\'true\', sizingMethod=\'crop\', src=\'/images/hover_bg' + vlaaitype + '.png\');alpha(opacity=' + fadealpha + ')';
}

function findPos(obj)
{
	var curleft = 0;
	var curtop = 0;

	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft-obj.scrollLeft;
			curtop += obj.offsetTop-obj.scrollTop;
			var position='';
			if (obj.style && obj.style.position) position=obj.style.position.toLowerCase();
			if ((position=='absolute') || (position=='relative')) break;
			while (obj.parentNode!=obj.offsetParent)
			{
				obj=obj.parentNode;
				curleft -= obj.scrollLeft;
				curtop -= obj.scrollTop;
			}
			obj = obj.offsetParent;
		}
	}
	else
	{
		if (obj.x)      curleft += obj.x;
		if (obj.y)      curtop += obj.y;
	}
	return {left:curleft,top:curtop};
}

//


function product_expand(obj_id)
{
	if (document.layers)
	{
		document.layers['product_omschrijving_' + obj_id + '_2'].display = 'none';
		document.layers['product_omschrijving_' + obj_id + '_1'].display = 'block';
	}
	else if (document.all)
	{
		document.all['product_omschrijving_' + obj_id + '_2'].style.display = 'none';
		document.all['product_omschrijving_' + obj_id + '_1'].style.display = 'block';
	}
	else if (document.getElementById)
	{
		document.getElementById('product_omschrijving_' + obj_id + '_2').style.display = 'none';
		document.getElementById('product_omschrijving_' + obj_id + '_1').style.display = 'block';
	}
}

function product_collapse(obj_id)
{
	if (document.layers)
	{
		document.layers['product_omschrijving_' + obj_id + '_1'].display = 'none';
		document.layers['product_omschrijving_' + obj_id + '_2'].display = 'block';
	}
	else if (document.all)
	{
		document.all['product_omschrijving_' + obj_id + '_1'].style.display = 'none';
		document.all['product_omschrijving_' + obj_id + '_2'].style.display = 'block';
	}
	else if (document.getElementById)
	{
		document.getElementById('product_omschrijving_' + obj_id + '_1').style.display = 'none';
		document.getElementById('product_omschrijving_' + obj_id + '_2').style.display = 'block';
	}
}

function getElementY(elm){
	var y = 0;
	do{y += elm.offsetTop;} while(elm = elm.offsetParent);
	return y;
}
function getWindowSize(){
	if(typeof window.innerWidth != 'undefined'){
		return {'x': window.innerWidth, 'y':window.innerHeight};
	}
	return {'x': document.documentElement.clientWidth, 'y':document.documentElement.clientHeight};
}
function getElementSize(elm){
	return {'x' :elm.offsetWidth, 'y': elm.offsetHeight}
}
function getScrollPosition(){
	var scrollPos = document.body.scrollTop;
	if (scrollPos == 0){
		if (window.pageYOffset) scrollPos = window.pageYOffset;
		else scrollPos = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
	}
	return scrollPos;
}
function keepElementInView(elm){
	if(!elm) return;
	var esize = getElementSize(elm);
	elm.parentNode.style.height = esize.y + 'px';
	var fromTop = getElementY(elm.parentNode) + elm.parentNode.style.paddingTop;
	var curpos = getScrollPosition();
	if(curpos <= fromTop){elm.style.top = fromTop + 'px';return;}
	var wsize = getWindowSize();
	if(esize.y > wsize.y){
		if((curpos - (esize.y - wsize.y)) < fromTop){
			elm.style.top = fromTop + 'px';
		} else {
			elm.style.top = (curpos - (esize.y - wsize.y)) + 'px';
		}	
	} else {
		elm.style.top = curpos + 'px';
	}
}
function addEventSimple(obj,evt,fn) {
	if (obj.addEventListener){
		return obj.addEventListener(evt,fn,false);
	} else if (obj.attachEvent){
		return obj.attachEvent('on'+evt,fn);
	}
}

function removeEventSimple(obj,evt,fn) {
	if (obj.removeEventListener)
		obj.removeEventListener(evt,fn,false);
	else if (obj.detachEvent)
		obj.detachEvent('on'+evt,fn);
}
function documentChecker(){
	keepElementInView(document.getElementById('basketpanel'));
	keepElementInView(document.getElementById('p_menu_links'));
	if(document.getElementById('popupdiv') &&  document.getElementById('popupdiv').style.visibility == 'visible') relocatePopup();
}
function headerReplacement(elm){
	var hlist = document.getElementsByTagName('h2');
	var re = /kabelreplace/;
	var re2 = /repldone/;
	var x;
	var y;
	var content;
	var curelm
	for(var i = hlist.length - 1; i >= 0; i--){
		curelm = hlist[i];
		if(curelm.className.match(re) && !curelm.className.match(re2)){
			curelm.style.backgroundImage = 'url("/buttons/header.php?x=' + escape(curelm.offsetWidth) + '&y=' + escape(curelm.offsetHeight) + '&content=' + escape(curelm.innerHTML) + '")';
			curelm.style.color = 'transparent';
			curelm.style.height = curelm.offsetHeight + 'px';
			curelm.style.width =  curelm.offsetWidth + 'px';
			curelm.innerHTML = '<span style="visibility:hidden">' + curelm.innerHTML + '</span>';
			curelm.className = curelm.className + ' repldone';
		}
	}
}

function toggleWenskaarten(ischecked)
{

	if (ischecked)
	{
			/*document.getElementById('wenskaartdiv').style.display = 'block';						
			try{
				document.getElementById('wenskaarttr').style.display = 'table-row';
			} catch (e) {document.getElementById('wenskaarttr').style.display = 'block';}*/
			
		
	}
	else
	{
			/*document.getElementById('wenskaartdiv').style.display = 'none';
			document.getElementById('wenskaarttr').style.display = 'none';*/
	}
}

function selectWenskaart(wenskaartnr, wenskaartid)
{
	var i = 1;	
	var elt;
	var radioelt;
	var hiddenelt;
	hiddenelt = document.getElementById('fld_wenskaartaan');
	if (wenskaartid==0)
	{
		elt = document.getElementById('wenskaartcell_0');		
		if (elt)
		{
			elt.style.border = '3px solid red';
			radioelt =  document.getElementById('wensradio_0');
			if (radioelt) radioelt.checked = true;
			hiddenelt.value="";
		}
	}
	else		
	{
		elt = document.getElementById('wenskaartcell_0');		
		if (elt)
		{
			elt.style.border = '3px solid #FFEECB';
			radioelt =  document.getElementById('wensradio_0');
			if (radioelt) radioelt.checked = false;
			hiddenelt.value="1";
		}
	}
	while (elt = document.getElementById('wenskaartcell_' + i) )
	{		
		if (elt.id == 'wenskaartcell_' + wenskaartnr)
		{
			var arr = elt.id.split('_');
			radioelt = document.getElementById('wensradio_'+arr[1]);
			if (radioelt) radioelt.checked = true;				
			elt.style.border = '3px solid red';
		}
		else
		{
			var arr = elt.id.split('_');
			radioelt = document.getElementById('wensradio_'+arr[1]);
			if (radioelt) radioelt.checked = false;						
			elt.style.border = '3px solid #FFEECB';
		}
		i++;
	}
}

function updateWenskaartCounter(thetextfield)
{
	var newstr = thetextfield.value;
	thetextfield.value = newstr.substr(0, 128);
	var counterid = document.getElementById('wenskaartcounter');
	counterid.innerHTML = (128 - thetextfield.value.length);
	return true;
}