var selectedindex = new Array();
var totalprice=0;

function init() {
	st = document.getElementById("estado");
	st.innerHTML = '<b><blink>WÄHLEN SIE IHREN PLATZ AUF DER WERBETAFEL</blink></b>';	
	initmap();
}

function initmap() {
	var t="";
	var t2="";
	var m = document.getElementById("mapa");
	var m2 = document.getElementById("selector");
	for (i=0;i<area.length;i++)	{
		var x1=area[i].x;
		var x2=area[i].x+area[i].width-1;
		var y1=area[i].y;
		var y2=area[i].y+area[i].height-1;
		
		t+='<area shape="rect" coords="' + x1 + ',' + y1 + ',' + x2 + ',' +  y2 + '" title="Platz verfügbar. Preis: ' + area[i].cost + ' &euro; Position: ' + area[i].x + ',' + area[i].y + '; Größe: ' + area[i].width + 'x' + area[i].height + '. Wählen Sie mit der Maus und drücken Sie die Taste \'R\', um zu reservieren." onclick="select('+i+');">';

		t2+='<div class="selector" style="visibility:hidden;position:absolute;left:'+area[i].x+'px;top:'+area[i].y+'px;height:' + area[i].height + 'px;width:'+ area[i].width +'px" id="select_' + i +'" onclick="deselect('+i+');" title="Klicken Sie noch einmal, um die Wahl zu entfernen oder \'R\' zum Reservieren."></div>';
		selectedindex[i] = 0;
		selectedcount=0;


	}
	m.innerHTML = t;
	m2.innerHTML= t2;

}

function rent(index)
{
	var coord=area[index].x + ',' + area[index].y;
	var size=area[index].width + ',' + area[index].height;
	var cost=area[index].cost;
	
	window.location = "alquilar.php?coord=" + coord + "&size=" +size + "&price=" + cost;
}

function deselect(index)
{
	selectedcount--;

	itembox = document.getElementById("select_" + index);
	itembox.style.visibility = "hidden";
	selectedindex[index] = 0;
	
	totalprice -= area[index].cost;

	updatebuystatus();

}
function select(index)
{
	selectedcount++;
	itembox = document.getElementById("select_" + index);
	itembox.style.visibility = "visible";
	selectedindex[index] = 1;

	totalprice += area[index].cost;

	updatebuystatus();

}

function updatebuystatus()
{
	st = document.getElementById("estado");
	if (selectedcount>0)
	{
		st.innerHTML = "Klicken Sie 'R',  um diesen Platz zu reservieren. Insgesamt ausgewählt: " + totalprice + " &euro;";
	}
	else
	{
		st.innerHTML = '<b><blink>WÄHLEN SIE IHREN PLATZ AUF DER WERBETAFEL</blink></b>';
	}
	
}

function kpress(e)
{
   e = e || window.event;
   ch = e.which || e.keyCode;
   if( ch != null) {
	   
	   if ((selectedcount>0)&&((ch==114)||(ch==82)))
	   {
		   	var pedido="";
		   	for (var i=0;i<selectedindex.length;i++)
			{
				if (selectedindex[i]==1) {
                   var coord=area[i].x + ',' + area[i].y;
	               var size=area[i].width + ',' + area[i].height;
	               var cost=area[i].cost;
                
                   pedido += i+":"+coord+":"+size+":"+cost+";";
                }   
			}
			pedido = pedido.substring(0,pedido.length-1);

//        	window.location = "formulario.php?pedido="+pedido;
                window.open("formulario.d.php?pedido="+pedido,"VentanaNew","width=800, height=500, titlebar=0, scrollbars=yes");
	   }
   }
}
document.onkeypress = kpress;
