var ForRentLevel = "" ;
var Rent_Top, Rent_Bottom, Rent_Left, Rent_Right ;
var Rent_Total = 0 ;
var Rent_Count = new Array () ;
Rent_Count[0] = 0 ;
Prop_Count[1] = 0 ;
var ForRentLocation = new Array () ;
var Rent_IDChecked = new Array () ;
Rent_IDChecked[0] = false ;
Rent_IDChecked[1] = false ;
var Rent_AllMatched = new Array();
var Rent_MatchPos = 0 ;
var Rent_MatchLoc = 0 ;
var rent_dragapproved = false ;
var ForRentLine ;
var RentMinLevel = 5 ;
var rent_rural = 0;

function InitiateForRent() {
	ForRentLevel = GM_Level ;

	var FN = eval('document.CurrentMap');
	if (FN.rent_rural.value>0)
	{
		rent_rural=1;
		RentMinLevel=4;
	}

	Rent_GetDataList();
	ForRentLine = new jsGraphics('ForRentLineDiv');
	var RDBMouse = document.getElementById('ForRentDetailBox') ;
	RDBMouse.onmousedown = Rent_DetailDrag ;
	RDBMouse.onmouseup = new Function("rent_dragapproved=false") ;
}

function CheckForRent() {
	ForRentLevel = GM_Level ; 
	if (ForRentLevel >= RentMinLevel)
	{
		Rent_GetDataList(); 
		Rent_SetArrow();
	}
}

function NewForRent(ID,X,Y,T) {
	this.ID = ID ;
	this.Longitude = X ;
	this.Latitude = Y ;
	this.SaleType = T ;
}

function NewRentLocation(ID) {
	this.ID = ID ;
	this.ForRent = new Array() ;
	this.ForRentIDs = new Array() ;
	this.SaleType = '' ;
}

function Rent_GetDataList() {
	var TL = GM_ConvertPxToDeg(GM_Offset_Left,GM_Offset_Top);
	var BR = GM_ConvertPxToDeg(GM_Offset_Right,GM_Offset_Bottom);
	Rent_Top = TL.Y ;
	Rent_Bottom = BR.Y ;
	Rent_Left = TL.X ;
	Rent_Right = BR.X ;
	var URL = "/CommonLibrary/ForRent.cgi?t="+encodeURI(Rent_Top)+"&l="+encodeURI(Rent_Left)+"&r="+encodeURI(Rent_Right)+"&b="+encodeURI(Rent_Bottom)+"&Level="+GM_Level+"&rural="+rural+"&Ref=" + encodeURI(Math.round(Math.random(0,1)*100000000000)) ;

	Rent_Global_Request("ListData",URL);
}

function Rent_SaveDataList(Data) {
	var D = Data.split("\n");
	var T = D[0].split("\t");
	Rent_Total = T[0];
	Rent_Count[0] = T[1];
	Rent_Count[1] = T[2];
	
	if (ForRentLevel>=RentMinLevel)
	{
		for (var i=1; i<D.length; i++)
		{
			var T = D[i].split("\t");
			var RX = GM_ConvertDegToPx(T[0],T[1]);
			var Regex = /\D/ ;
			var RentLocID = "X"+(RX.X+"").replace(Regex,"_")+"Y"+(RX.Y+"").replace(Regex,"_") ;
			if (!ForRentLocation[RentLocID])
			{
				ForRentLocation[RentLocID] = new NewRentLocation(RentLocID) ;
				ForRentLocation[RentLocID].SaleType = T[3] ;
				ForRentLocation[RentLocID].X = RX.X ;
				ForRentLocation[RentLocID].Y = RX.Y ;
			}
			if (!ForRentLocation[RentLocID].ForRent[T[2]])
			{
				ForRentLocation[RentLocID].ForRent[T[2]] = new NewForRent(T[2],T[0],T[1],T[3]);
				ForRentLocation[RentLocID].ForRentIDs.push(T[2]);
				if (ForRentLocation[RentLocID].SaleType != T[3] || ForRentLocation[RentLocID].SaleType == '999')
				{
					ForRentLocation[RentLocID].SaleType = '999' ;
				}
			}
		}
	}
}

function Rent_DisplayMenu() {
	var R = document.getElementById('ForRentCheck');
	
	var CRent = ((Rent_IDChecked[0]) && (parseInt(Rent_Total) == parseInt(Rent_Count[0])) || !Rent_IDChecked[0])?"":"checked";
	R.innerHTML = "<b><input type=\"checkbox\" onclick=\"RentCheck(this,0)\" "+CRent+"> <img align=absmiddle src=\"images/rent0.gif\"> For Rent ("+Rent_Total+")</b>";
}

function GetRuralForRent(Data) {
	var D = Data.split("\t");

	var Level = (D[0]>0)?5:4;
	var CRent = (Rent_IDChecked[0])?"checked":"" ;
	var rural = (D[0]>0)?0:1;
	
	if (ForRentLevel<Level) {
		var URL = "mapsearch.cgi?x="+encodeURI(PrevX)+"&y="+encodeURI(PrevY)+"&level="+encodeURI(Level)+"&rent="+encodeURI(CRent)+"&rent_rural="+encodeURI(rural);
		location.href=URL;
	}
}

function CheckForRentRural() {
	var URL = "/CommonLibrary/ForRent.cgi?x="+encodeURI(PrevX)+"&y="+encodeURI(PrevY)+"&checkrural=1&Ref=" + encodeURI(Math.round(Math.random(0,1)*100000000000)) ;

	Rent_Global_Request("CheckRural",URL);
}

function RentCheck(Obj,ID) {
	var RL = document.getElementById('GM_DrawingLayer7');
	RL.innerHTML = "" ;
	
	if (Obj)
	{
		if (Obj.checked)
		{
			Rent_IDChecked[ID] = true ;
			CheckForRentRural();
		} else {
			Rent_IDChecked[ID] = false ;
			Rent_Close();
		}

		var Ar = new Array ('CurrentMap') ;
		for (var i=0; i<Ar.length; i++) {
			if (eval("document."+Ar[i])) {
				var FN = eval("document."+Ar[i]);
				FN.rent.value = (Rent_IDChecked[0])?"checked":"";
			}
		}
	}

	if (ForRentLevel>=RentMinLevel)
	{
		for (var i in ForRentLocation)
		{
			var R = ForRentLocation[i] ;
			if ((R.SaleType == '2' && Rent_IDChecked[0]) || ((Rent_IDChecked[0]) && R.SaleType == '999'))
			{
				var Img = document.createElement('img');
				Img.id = 'GM_ForRent_' + R.ID ;
				Img.name = Img.id ;
				var IconNo = '0';
				Img.src = "images/rent"+IconNo+".gif" ;
				Img.LX = R.X ;
				Img.LY = R.Y ;
				Img.MyID = R.ID ;
				Img.style.position = 'absolute' ;
				Img.style.cursor = 'pointer' ;

//				Img.style.top = Img.LY - Math.round(parseInt(Img.height)/2);
//				Img.style.left = Img.LX - Math.round(parseInt(Img.width)/2);
			      	//////// change to opera compatible //////////
			     	Img.style.top = Img.LY - Math.round(parseInt(Img.height)/2) - GM_YCenter + 'px';
			     	Img.style.left = Img.LX - Math.round(parseInt(Img.width)/2) - GM_XCenter + 'px';
			     	////////////////////////////////////////////////

				Img.alt = 'Click To View Detail' ;
				Img.onload = function() {
//					this.style.top = this.LY - Math.round(parseInt(this.height)/2);
//					this.style.left = this.LX - Math.round(parseInt(this.width)/2);
				      //////// change to opera compatible //////////
				      this.style.top = this.LY - Math.round(parseInt(this.height)/2) - GM_YCenter + 'px';
				      this.style.left = this.LX - Math.round(parseInt(this.width)/2) - GM_XCenter + 'px';
				      //////////////////////////////////////////////
				}
				Img.onmouseover = function() {
					this.PrevZIndex = this.style.zIndex ;
					this.style.zIndex = '99998' ;
				}
				Img.onmouseout = function() {
					this.style.zIndex = this.PrevZIndex ;
				}
				Img.onclick = function() {
					Rent_MatchPos = 0 ;
					Rent_MatchLoc = this.MyID ;
					Rent_OpenDataHouse(this.MyID) ;
					Rent_OpenDialogBox();
					Rent_GetDataDetail(this.MyID);
					Rent_SetArrow();
				}
				RL.appendChild(Img);
				Img = null ;
			}
		}
	}
}

function Rent_GetDataDetail(ID) {
	var R = ForRentLocation[ID] ;
	var IDs = R.ForRentIDs.join(",");
	var URL = "/CommonLibrary/ForRent.cgi?ID="+encodeURI(IDs)+"&Ref=" + encodeURI(Math.round(Math.random(0,1)*100000000000)) ;
	Rent_Global_Request("DetailData_"+ID,URL);
}

function Rent_SaveDataDetail(Data,PID) {
	if (Data)
	{
		var D = Data.split("\n");
		for (var i=0; i<D.length; i++)
		{
			var T = D[i].split("\t");
			ForRentLocation[PID].ForRent[T[0]].Photo = T[1] ; 
			ForRentLocation[PID].ForRent[T[0]].RentType = T[2] ; 
			ForRentLocation[PID].ForRent[T[0]].GID = T[3] ; 
			ForRentLocation[PID].ForRent[T[0]].Address = T[4] ; 
			ForRentLocation[PID].ForRent[T[0]].BedRoom = T[5] ; 
			ForRentLocation[PID].ForRent[T[0]].BathRoom = T[6] ; 
			ForRentLocation[PID].ForRent[T[0]].Price = T[7] ; 
			ForRentLocation[PID].ForRent[T[0]].Desc = T[8] ; 
		}
	}
	Rent_MatchLoc = RID ;
}

function Rent_RequestDone(ID,http_request3) {
	var Data = Trim(http_request3.responseText) ;
	if (Data)
	{
		var Reg = /^DetailData_/ ;
		if (ID == 'ListData') {
			Rent_SaveDataList(Data);
			Rent_DisplayMenu();
			RentCheck('','');
		} else if (ID == 'GetLevel7') {
			stateChanged(Data) ;
		} else if (Reg.test(ID)) {
			RID = ID.replace(Reg,'');
			Rent_SaveDataDetail(Data,RID);
			Rent_DisplayDetailData(Rent_MatchPos);
		} else if (ID == 'CheckRural') {
			GetRuralForRent(Data);
		}
	}
}

function Rent_Global_RequestReady(ID,http_request2) {
 try
 {
	if (http_request2.readyState == 4) {
		if (http_request2.status == 200) {
			Rent_RequestDone(ID,http_request2) ;
		} else {
			//alert('There was a problem with the data request. please refresh the page.');
		}
	}
	}catch(e) {}
}

function Rent_Global_Request(ID,url) {
	var url_post = url;
	var url_send = null;
	if (url.indexOf('?')>0)
	{
		url_post = url.substring(0,url.indexOf('?'));
		url_send = url.substring(url.indexOf('?')+1);
	} else {
		url_post = url ;
		url_send = null ;
	}

	var http_request = false;

	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
			// See note below about this line
		}
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}

	if (!http_request) {
		alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	}
	http_request.onreadystatechange = function() { Rent_Global_RequestReady(ID,http_request) }
	if (url_send != null)
	{
		if (document.getElementById('XXX'))
		{
			var XXX = document.getElementById('XXX') ;
			XXX.innerHTML = url_post + "<br>" ;
			XXX.innerHTML += url_send ;
		}
		http_request.open('POST', url_post, true);
		http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		http_request.setRequestHeader("Content-length", url_send.length);
		http_request.setRequestHeader("Connection", "close");	
		http_request.send(url_send);
	} else {
		http_request.open('GET', url_post, true);
		http_request.send(url_send);
	}
}

function Rent_OpenDataHouse(ID) {
	var R = ForRentLocation[ID] ;
	var Rent_Match = new Array();
	for (var i=0; i < R.ForRentIDs.length; i++)
	{
		var IDR = R.ForRentIDs[i] ;
		var ST  = R.ForRent[IDR].SaleType;
		if (Rent_IDChecked[0] && ST == '2')
		{
			Rent_Match.push(IDR);
		}
	}
	Rent_AllMatched = Rent_Match ;
}

function Rent_OpenDialogBox() {
	ForRentLine.clear();
	var HTML = "<table title=\"Drag & Drop is Available For This Layer\" cellpadding=2 cellspacing=1 border=0 bgcolor=\"#000000\" width=400><tr><td class=content bgcolor=\"#D8DEED\"><table cellpadding=0 cellspacing=2 border=0 width=\"100%\"><tr><td class=content><a href=\"javascript: Rent_Prev();\"><img src=\"images/prop_left.gif\" border=0></a></td><td align=center style=\"color: #02287B; font-weight: bold;\" class=content width=\"100%\" id=Rent_Det_Number></td><td class=content><a href=\"javascript: Rent_Next();\"><img src=\"images/prop_right.gif\" border=0></a></td><td class=content><a href=\"javascript: Rent_Close();\"><img src=\"images/prop_close.gif\" border=0></a></td></tr></table></td></tr><tr><td class=content bgcolor=\"#FFFFFF\"><table cellpadding=2 cellspacing=2 border=0 width=\"100%\"><tr><td nowrap valign=top align=center class=content id=Rent_Det_Left>LOADING...</td><td valign=top class=content width=\"100%\" id=Rent_Det_Right>LOADING...</td></tr></table></td></tr></table>" ;
	var RDB = document.getElementById('ForRentDetailBox') ;
	RDB.innerHTML = HTML ;

	RDB.style.visibility = 'visible' ;
	RDB.style.display = '' ;
	RDB.style.zIndex = '99999' ;
	RDB.style.left = document.getElementById('GenMapDiv').offsetLeft + document.getElementById('GM_ForRent_'+Rent_MatchLoc).LX - GM_Offset_Left + 10 ;
	RDB.style.top = document.getElementById('GenMapDiv').offsetTop + document.getElementById('GM_ForRent_'+Rent_MatchLoc).LY - GM_Offset_Top + 10 ;
	

	if (parseInt(RDB.style.left) + RDB.clientWidth >=  window.screen.width-20)
	{
		RDB.style.left = parseInt(RDB.style.left) - RDB.clientWidth - 38 ;
	}
	if (parseInt(RDB.style.top) + RDB.clientHeight >=  window.screen.height-180)
	{
		RDB.style.top = parseInt(RDB.style.top) - RDB.clientHeight - 38 ;
	}
}

function Rent_DisplayDetailData(Idx) {
	var ID = ForRentLocation[Rent_MatchLoc].ForRentIDs[Rent_MatchPos] ;
	var R = ForRentLocation[Rent_MatchLoc].ForRent[ID] ;
	var RDN = document.getElementById('Rent_Det_Number') ;
	var RDL = document.getElementById('Rent_Det_Left') ;
	var RDR = document.getElementById('Rent_Det_Right') ;
	RDN.innerHTML = Math.round(Idx+1) + " of " + Rent_AllMatched.length;
	
	var SaleType = "<div><b style=\"color: #02287B;\">FOR RENT</b></div>";
	var Foto	 = (R.Photo)?"<div><img onload=\"Rent_SetArrow();\" src=\""+R.Photo+"\" style=\"border:1px solid #000000;\" border=0></div>":"";
	var BedBath	 = "<div style=\"background-color: #D8DEED; color: #02287B;\"><img alt=\"Bedroom\" src=images/beds_icon.gif border=0 align=absmiddle>&nbsp;<b>"+R.BedRoom+"</b>&nbsp; &nbsp;<img alt=\"Bathroom\" src=images/baths_icon.gif border=0 align=absmiddle>&nbsp;<b>" + R.BathRoom +"</b></div>";
	var RentType = "<div style=\"color: #02287B;\">Type : <b style=\"color: #02287B;\">"+R.RentType+"</b></div>" ;
	
	RDL_HTML = SaleType +LBR(3)+ Foto +LBR(3)+ BedBath +LBR(3)+ RentType ;
	RDL.innerHTML = RDL_HTML ;

	
	var Address = (R.Address)?"<div align=justify>"+R.Address+"</div>":"" ;
	var Price = (R.Price)?"<div align=justify><b>"+R.Price+"</b></div>":"" ;
	var Description = (R.Desc)?"<div align=justify>"+R.Desc+"... <a target=RealEstateView href=\"http://www.realestateview.com.au/"+R.ID+"\">[See Details]</a></div>":"" ;

	RDR_HTML = Address +LBR(10)+ Price +LBR(10)+ Description ;
	RDR.innerHTML = RDR_HTML ;
}

function Rent_Next() {
	Rent_MatchPos = (Rent_MatchPos >= Rent_AllMatched.length-1)?0:Rent_MatchPos+1;
	Rent_DisplayDetailData(Rent_MatchPos);
}

function Rent_Prev() {
	Rent_MatchPos = (Rent_MatchPos <= 0)?Rent_AllMatched.length-1:Rent_MatchPos-1;
	Rent_DisplayDetailData(Rent_MatchPos);
}

function Rent_Close() {
	var RDB = document.getElementById('ForRentDetailBox') ;
	RDB.style.visibility = 'hidden' ;
	RDB.style.display = 'none' ;
	RDB.style.zIndex = '0' ;
	ForRentLine.clear();
}

function Rent_DetailDrag(e) {
	rent_dragapproved=true;
	r_z = document.getElementById('ForRentDetailBox');
	temp1=parseInt(r_z.style.left);
	temp2=parseInt(r_z.style.top);
	x = DD_nn6 ? e.clientX : event.clientX ;
	y = DD_nn6 ? e.clientY : event.clientY ;
	document.onmousemove=Rent_DetailMove;
}

function Rent_DetailMove(e) {
	if (rent_dragapproved){
		XMouseClick = DD_nn6 ? e.clientX : event.clientX ;
		YMouseClick = DD_nn6 ? e.clientY : event.clientY ;
		r_z.style.left=temp1+XMouseClick-x;
		r_z.style.top=temp2+YMouseClick-y;
		Rent_SetArrow();
		return false;
	}
}


function Rent_SetArrow() {
	ForRentLine.clear();
	var RDB = document.getElementById('ForRentDetailBox') ;
	if (RDB.style.visibility == 'visible')
	{
		var left = parseInt(RDB.style.left);
		var top = parseInt(RDB.style.top);
		var right = left + RDB.clientWidth;
		var bottom = top + RDB.clientHeight;
		
		var ICN = document.getElementById('GM_ForRent_'+Rent_MatchLoc) ;

		var IconLeft = document.getElementById('GenMapDiv').offsetLeft + ICN.LX - GM_Offset_Left;
		var IconTop = document.getElementById('GenMapDiv').offsetTop + ICN.LY - GM_Offset_Top;

		var XB = (IconLeft > (left+right)/2)?right-1:left+1;
		var YB = (IconTop > (top+bottom)/2)?bottom-1:top+1;

		GM_SetLine(ForRentLine,2,'#02287B',IconLeft,IconTop,XB,YB);
		GM_SetLine(ForRentLine,1,'#D8DEED',IconLeft,IconTop,XB,YB);
		
		var RDD = document.getElementById('ForRentLineDiv');
		RDD.style.zIndex = 10000000 ;

		if (!(ICN.LX >= GM_Offset_Left && ICN.LX <= GM_Offset_Right && ICN.LY >= GM_Offset_Top && ICN.LY <= GM_Offset_Bottom))
		{
			Rent_Close();
		}
	}
}
