var debug = false;

var ajax_url = './ajax/get_cars.php';

window.onresize = resized;

function resized()
{
	var cs = $('carselector');
	var bg = $('bgbox');
	size = getPageSize();
	if (cs)
	{	
		var x = (size[0]-cs.getWidth()) / 2;
		cs.style.left = x+"px";
	}
	
	if (bg)
	{				
		bg.style.width = size[0]+"px";
		bg.style.height = size[1]+"px";
	}
}

function selectcar(car, value, cost)
{
	if (!value) value = '';
	if (!cost) cost = '';
	if ($('fahrzeug')) $('fahrzeug').value = car;	
	if ($('fahrzeugwert')) $('fahrzeugwert').value = value;
	if ($('kstaktfz')) $('kstaktfz').value = cost;
	if ($('leasingrate')) $('leasingrate').value = cost;
	closeCarSelector();
}

function createselectorline(car, value, cost)
{
	var code = '<tr style="cursor: pointer" onMouseOver="this.style.backgroundColor=\'#FFE4B5\';" onMouseOut="this.style.backgroundColor=\'#FFFFFF\';" onclick="selectcar(\''+car+'\',\''+value+'\',\''+cost+'\');"><td align="left"  style="border: 1px solid grey;">'+car+'</td><td align="right" style="border: 1px solid grey;">'+value+' &euro;</td><td align="right" style="border: 1px solid grey;">'+cost+' &euro;</td></tr>';
	return code;
}

function showCarSelector(trans,json)
{	
	var cs = $('carselector');
	var cstd = $('cstd');
	var bg = $('bgbox');
	var anchor = $('startpoint');
	var code = "";
	var size = getPageSize();
	if (cs)
	{		
		var x = (size[0]-cs.getWidth()) / 2;		
		var x = 0;
		if (json)
		{
			code = '<div id="closebox" style="position: relative;top: 0px; right: 0px; text-align: right;"><a href="javascript: closeCarSelector();" title="Schliessen" style="text-decoration: none;"><img src="close.gif" alt="Schliessen"></a></div></div></a><center><h3 style="color: grey;">Klicken Sie auf das gew&uuml;nschte Fahrzeug:</h3>\n<table id="carselectortable" style="border: 1px solid grey;border-collapse: collapse;">';
			code += '<tr style="border: 1px solid grey;"><th align="left" width="300">Fahrzeug</th><th>Brutto-<br />Listenpreis<br />inkl. USt</th><th>Kosten aktuelles<br />Fahrzeug<br />(= mon. Leasingrate)</th></tr>';
			for (var i = 0; i < json.length;  i++)
			{
				code += createselectorline(json[i].CAR, json[i].PRICE, json[i].COST);
			}
			code += '<tr style=" cursor: pointer; background-color: #EEEEEE;" onMouseOver="this.style.backgroundColor=\'#CCCCCC\';" onMouseOut="this.style.backgroundColor=\'#EEEEEE\';"><td colspan="3" onclick="selectcar(\'Individuelles Fahrzeug\');">Ich m&ouml;chte kein Fahrzeug ausw&auml;hlen, sondern die Daten selbst eingeben</td></tr>';
			code += '</table></center>';
		}
		if (cstd) cstd.innerHTML = code;	
		cs.style.display = "";
		cs.style.left = x+"px";
	}
	if (bg)
	{				
		bg.style.width = size[0]+"px";
		bg.style.height = size[1]+"px";
		bg.style.display = "";
	}
	var selects = document.getElementsByTagName("select");

	for (var i = 0; i < selects.length; i++) {
  		selects[i].style.visibility = "hidden";
	}
}

function closeCarSelector()
{
	var cs = $('carselector');
	var bg = $('bgbox');
	if (cs)
	{
		cs.style.display = "none";
	}
	if (bg)
	{				
		bg.style.display = "none";
	}
	var selects = document.getElementsByTagName("select");

	for (var i = 0; i < selects.length; i++) {
  		selects[i].style.visibility = "";
	}
}

function getCarSelector()
{	
	clearet();
	
	var url = ajax_url;	

	var cs = $('carselector'); 	

	if (cs) {	
		var query = $('fwatform').serialize();				
		
		var updater = new Ajax.Request(
			url,
			{
				method: 'get', 
				parameters: query,		
				message: 'hole Fahrzeuge',
				onSuccess: showCarSelector,
				onError: showError
			}
		);	
	}  
}

function clearet()
{	
	eb = $('errorbox');
	if (eb && debug)
	{		
		eb.innerHTML = "<h1>Error Tracker</h1>";
	}
}

function et(error)
{
	eb = $('errorbox');
	if (eb && debug)
	{
		eb.style.display = "";
		eb.innerHTML += error + "<br />";
	}
}

function showError(o)
{
  et(Object.inspect(o));
}


function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	
//	console.log(self.innerWidth);
//	console.log(document.documentElement.clientWidth);

	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

//	console.log("xScroll " + xScroll)
//	console.log("windowWidth " + windowWidth)

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
//	console.log("pageWidth " + pageWidth)

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}
