var xmlHttp = 0;
function InitAJAX()
{
	// Firefox, Opera 8.0+, Safari  
	try{ xmlHttp = new XMLHttpRequest();}
	// IE
	catch(e)
	{
		try { xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");}
    		catch(e)
    		{
			try { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");}
			// No support
			catch(e)
			{
			    alert("<?php echo AJAX_NOT_SUPPORTED ?>");
        			return false;
      			}	
    		}
	}
}

function ShowPopup(target)
{
	var win = window.open(target,"","left=500,top=150,width=500,height=600,scrollbars=yes,resizable=yes,location=yes");
}

function SizedPopup(target,width,height)
{
	var win = window.open(target,"","left=500,top=150,width="+width+",height="+height+",scrollbars=yes,resizable=yes,location=yes");
}

function UpdateSession(str)
{
// This function takes in a string which
// contains session variables to be renewed	
	InitAJAX();
	//alert(str);

	var p = str.substring(14,21);
	var i = str.substring(24,31);
	//alert(p);
	//alert(i);
	//if(!isNaN(p) && !isNaN(i))
	//ShowLocationRect(p,i);
	// Send request to the server
	
	xmlHttp.onreadystatechange = function(){
		
		 // 4=complete => delete reference
   		if(xmlHttp.readyState == 4) 
		{
			// Session is renewed, reload the frames
			
			//parent.frames['map-small'].location.reload();
			//parent.frames['navigation-link'].location.reload();
			//parent.frames['article'].location.reload();
			//parent.frames['contents'].src = "../contents.php";
			parent.frames['content'].location.reload();
			parent.frames['contents'].location.reload();
			parent.frames['map-big'].location.reload();
			if(parent.frames['debug'])parent.frames['debug'].location.reload();
	
			xmlHttp = null;
		}
  	}
	var request = "set-session.php?"+str;
	xmlHttp.open("GET", request, true);
	xmlHttp.send(null);
}

function TransformCoords(event)
{
	 ORIGO_X = 0 + 8; // of the image
	 ORIGO_Y = 0 - 25;

	 TOP_I  = 3849333;
 	 BOT_I  = 2919833;

	 TOP_P  = 7930087;
   	 BOT_P  = 6551587;

	 XRATIO = (TOP_I-BOT_I) / 158; // image sizes
	 YRATIO = (TOP_P-BOT_P) / 234;

	 x = event.clientX;
	 y = event.clientY;

	 // from equation x = (((I-BOT_I) / XRATIO) - 7) + ORIGO_X;
	 I = ((x-ORIGO_X)*XRATIO)+BOT_I
	 // from equation y = (((TOP_P-P) / YRATIO) - 7) + ORIGO_Y; 
	 P = TOP_P - ((y-ORIGO_Y)*YRATIO);
	
	 UpdateSession("p="+P+"&i="+I);
	 //ShowLocationRect(P,I);
}


// This function takes a clicked position and convert the X,Y to P,I and updates the map
function UpdateLocation(event)
{
	ORIGO_X = 0 + 8; // of the image
	ORIGO_Y = 0 - 25;

	TOP_I  = 3849333;
 	BOT_I  = 2919833;

	TOP_P  = 7930087;
  	BOT_P  = 6551587;

	XRATIO = (TOP_I-BOT_I) / 158; // image sizes
	YRATIO = (TOP_P-BOT_P) / 234;

	x = event.clientX;
	y = event.clientY;

	// from equation x = (((I-BOT_I) / XRATIO) - 7) + ORIGO_X;
	I = ((x-14-ORIGO_X)*XRATIO)+BOT_I
	// from equation y = (((TOP_P-P) / YRATIO) - 7) + ORIGO_Y; 
	P = TOP_P - ((y-7-ORIGO_Y)*YRATIO);
	
	// Show new map (it might be big or small map)						
 	/*map_file = parent.frames['map'].location.href.split(".com/")[1].split("?")[0];
	parent.frames['map'].location.href = "http://www.matkain.com/"+map_file+"?P="+P+"&I="+I+"&ZOOM_LEVEL=0";*/
	UpdateSession("p="+P+"&i="+I+"&zoomLevel=2");
}

// Remove element from DOM
function RemoveElement(elem_id)
{
	var ele = top.document.getElementById(elem_id);
	if(ele) ele.parentNode.removeChild(ele);
}

