//===================================================================
//  Javascript dynamic content module
//  by Bert vt Veer
//===================================================================

// Globals
var lastclick;
var activeImg = "";
	
	
//---------------------------------------------------------
// Menu system
//---------------------------------------------------------	

function link_click(elem)
{
	if (lastclick) document.getElementById(lastclick).style.background = "";
	lastclick = elem;
	document.getElementById(elem).style.background = "rgb(46,42,39)";
}


//---------------------------------------------------------
// Dynamic page loading
//---------------------------------------------------------

function getpage(target,url,form)
{
	var httpRequest;
	meth = (form==undefined) ? 'GET' : 'POST';
	if (window.XMLHttpRequest) { // Mozilla, Safari, ...
		httpRequest = new XMLHttpRequest();
		if (httpRequest.overrideMimeType) {
			httpRequest.overrideMimeType('text/xml');
		}
	}
	else if (window.ActiveXObject) { // IE
		try { httpRequest = new ActiveXObject("Msxml2.XMLHTTP"); }
		catch (e) {
			try { httpRequest = new ActiveXObject("Microsoft.XMLHTTP"); }
			catch (e) {}
		}
	}
	if (!httpRequest) {
		alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	}
	
	httpRequest.onreadystatechange = function() { alertContents(target,httpRequest); };
	httpRequest.open(meth, url, true);
	if (meth=='POST') {
		parms = '';
		form = document.forms[form];
		for (i=0; i<form.length; i++) {
			parms += '&' + form.elements[i].name + '=' + form.elements[i].value;
		}
		httpRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		//httpRequest.setRequestHeader("Content-length", parms.length);
		//httpRequest.setRequestHeader("Connection", "close");
		httpRequest.send(parms);
	} else 
	    httpRequest.send('');
}
	
function alertContents(target,httpRequest)
{
	if (httpRequest.readyState == 4) {
		document.getElementById(target).innerHTML = httpRequest.responseText;
		//document.getElementById(target).style.visibility = "visible";
	}
}


//---------------------------------------------------------
// Toggle elements on/off
//---------------------------------------------------------

function toggle(elem)
{
	disp = document.getElementById(elem).style.display=="block" ? "none" : "block";
	document.getElementById(elem).style.display = disp;
}


//---------------------------------------------------------
// Main menu
//---------------------------------------------------------

function menu_click(menuopt, page)
{
	getpage("content1", page+".php");
	link_click("menuopt"+menuopt);
	activeImg = "but1";
}


//---------------------------------------------------------
// Popup window
//---------------------------------------------------------

function getDocHeight() {
	var D = document;
	return Math.max(
		Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
		Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
		Math.max(D.body.clientHeight, D.documentElement.clientHeight)
	);
}


function popup_show(page,ptitle,pw,ph,px,py)  // page, left,top,width,height
{
	// Load content
	getpage("mainwin_client", page);
	var title = (ptitle==undefined) ? '&nbsp;' : ptitle;
	document.getElementById("mainwin_titlebar").innerHTML = title;

	// Set window size & position	
	var midx = Math.floor(document.body.clientWidth/2);
	var midy = Math.floor(document.body.clientHeight/2);
	var w = (pw==undefined) ? 600 : pw; 
	var h = (ph==undefined) ? 300 : ph; //getDocHeight(); 
	var x = (px==undefined) ? Math.floor(midx-(w/2)) : midx+px;
	var y = (py==undefined) ? Math.floor(midy-(h/2)) : py; //(document.body.scrollTop+160); //document.body.clientHeight/4);
	         
	// window
	var win = document.getElementById("mainwin").style;
	var cl = document.getElementById("mainwin_client").style;
	cl.width = w+"px";
	cl.height = h+"px";
	win.left = x+"px";
	win.top = y+"px";
	win.visibility = "visible";
	   
	// shadow
	var sh = document.getElementById("mainwin_shadow").style;
	sh.width = (w+42)+"px";//"100%";
	sh.height = (h+90)+"px";
	sh.left = (x+10)+"px";
	sh.top = (y+10)+"px";
	sh.visibility = "visible";
}

  
function popup_hide()
{
	document.getElementById("mainwin_shadow").style.visibility="hidden"; 
	document.getElementById("mainwin").style.visibility="hidden"; 
	document.getElementById("mainwin_client").innerHTML = "";
}


//---------------------------------------------------------
// Product selection
//---------------------------------------------------------

function img_click(elem,page)
{
	if (activeImg) document.getElementById(activeImg).className = "thumbs";
	document.getElementById(elem).className = "thumbs_hi";
	activeImg = elem;
	getpage('content2', page);
}
