﻿var selectedBound = new Object();
var selectedCatBound = new Object();

var selectedProv = "";
var selectedCat = "";
var lastAllIds = "";

var setCategoriesTimer = "";

var dontResizeOnCat = 0;

function setCategories()
{
	clearTimeout(setCategoriesTimer);
	setCategoriesTimer = "";	
	catSelect();
}
function setBoundFromFlash(rez)
{	
	if (map_visibility != 'visible') return;
	if (output_form_type != "cat") return;
	if (selectedCatBound.xMin == rez[0] && selectedCatBound.xMax == rez[1] && selectedCatBound.yMin == rez[2] && selectedCatBound.yMax == rez[3] ) return;
	//var rez2 = rez.split(",");		
	catSelectStart = 1;	
	selectedCatBound.xMin = rez[0];
	selectedCatBound.xMax = rez[1];
	selectedCatBound.yMin = rez[2];
	selectedCatBound.yMax = rez[3];	
		
	if (setCategoriesTimer)	
		clearTimeout(setCategoriesTimer);
	
	if (map_visibility == 'visible')			
		setCategoriesTimer = window.setTimeout(setCategories,300);
	
		
}

function clearCat(donClearSelectedRecord)
{	
	//alert("clear cat");
	selectedCat = "";
	var obj = document.getElementById("srch_box");
	if (obj)
	{
		obj.selectedIndex = 0;
		if (!donClearSelectedRecord)
		{
			selectedRecord = "";
			document.getElementById("selected_record_box").style.display = "none";
		}
		if (map) map.clearExcept(selectedRecord);
		var obj = document.getElementById("srch_rez");
		if (obj)
		obj.innerHTML = "";						
		document.getElementById("srch_info").style.display = "none";
		document.getElementById("srch_rez_box").style.display = "none";
		lastRez = "";
	}
}	

var catSelectStart = 1;

function catSelect(num)
{	
	//alert("test");
	//alert("test1");
	output_form_type = "cat";
	document.getElementById("loader1").style.display = "inline";
	var obj = document.getElementById("srch_box");
	selectedCat = obj;
	if (num)
		catSelectStart = num;
	else
		catSelectStart = 1;
		
	if (obj.selectedIndex > 0)
	{
	document.getElementById("resetLink").style.display = "inline";			
	showMap();	
	if (!dontResizeOnCat)
		mapMaximize();
	dontResizeOnCat = 0;	
	var cat = obj.options[obj.selectedIndex].value;	
	var q = "cat="+cat+"&extraparams=onlyshow";
	if (selectedCatBound.xMin)
	{
		q += "&xMin="+selectedCatBound.xMin;
		q += "&xMax="+selectedCatBound.xMax;
		q += "&yMin="+selectedCatBound.yMin;
		q += "&yMax="+selectedCatBound.yMax;
	}
	var l = document.getElementById("srch_num_items");	
	q += "&num="+l.options[l.selectedIndex].value;
	q += "&start="+(catSelectStart-1);
	q += "&check="+rezMd5;
	
	//document.getElementById("srch_rez").innerHTML = "";
	//document.getElementById("srch_info").style.display = "none";
	if (document.getElementById("moreinfo_box").style.display == "none")
	{		
		document.getElementById("srch_rez_box").style.display = "block";	
	}
	document.getElementById("searchTitle").innerHTML = "<center>Searching Services Data</center>";
	
	xmlhttpPost("/forms/search/cat.php",q,catSelectCB);
	}
	else
	{
		rezMd5 = "";
		selectedRecord = "";
		if (!selectedProv)
		document.getElementById("resetLink").style.display = "none";				
		document.getElementById("srch_rez_box").style.display = "none";				
		document.getElementById("selected_record_box").style.display = "none";
		if (map) map.clear();
	}
}

var cat_change_ok = 1;

function catMouseOver(id,obj)
{
	if (cat_change_ok)
	map.showSubject(id,'minimized,onlyshow,leaveactive');
	
	listItemOver(obj);
	
}

function listSelectCatItem(obj,params,dontload)
{				
	if (obj.parentNode.selectedItem != obj)
	{
	cat_change_ok = 0;
	
	window.setTimeout(new Function("cat_change_ok = 1;"),2000);
	
	if (obj.parentNode.selectedItem)
			obj.parentNode.selectedItem.className = "searchItem";
	
	obj.parentNode.selectedItem  = obj;		
	obj.className = "searchItemSelected";		
	obj.params = params;
	}	
	if (obj.loc)
	{					
		selectedRecord = params;
		if (!selectedRecord)
			document.getElementById("selected_record_box").style.display = "none";
		document.getElementById("selected_record_info").innerHTML = obj.parentNode.selectedItem.innerHTML;
		document.getElementById("selected_record_box").style.display = "block";
		if (params && map && !dontload)
		{
			map.getBound(selectedCatBound.xMin+","+selectedCatBound.xMax+","+selectedCatBound.yMin+","+selectedCatBound.yMax);
			map.showSubject(params,"onlyshow,active,open");		
		}
	}	
}

var lastRez = "";

var rezMd5 = "";

function catSelectCB(rez)
{					
	output_form_type = "cat";
	loadSearchResults(rez,"catSelect");
}

function loadSearchResults(rez,func)
{				
	rez = rez.split("#$#");
	str = rez[0];
	num = parseInt(rez[1]);
	count = rez[2];	
	//alert(rez[3]);	
	catSelectStart = parseInt(catSelectStart);
	if (rez == "loaded") {}
	else
	if (str /*&& lastRez != str*/)
	{		
		rezMd5 = rez[3];
		//map.zoomToFull();
		
		var obj = document.getElementById("srch_rez");
		var rez = '<div id="srch_searchList" class="searchList" style="background-color:#ffffff; border:1px solid #999999;height:320px;">'+str+'</div>';
		obj.innerHTML = rez;
		
		var l = document.getElementById("srch_num_items");	
		var k = parseInt(l.options[l.selectedIndex].value);
		
		var obj = document.getElementById("srch_other");
		var rez = "Showing: "+catSelectStart+" to "+(catSelectStart+num-1)+" of "+count;
		rez += "<br/>";
		pitem = catSelectStart-k;
		nitem = catSelectStart+k;
		if (pitem < 1) pitem = 1;
		if (nitem > count) nitem = count;
		if (catSelectStart > 1)
		{
			if (pitem == 1)
				rez += '<a href="javascript:'+func+'('+pitem+')">&lt;&lt; First '+k+'</a>';
			else				
				rez += '<a href="javascript:'+func+'('+pitem+')">&lt;&lt; Previous '+k+'</a>';
		}
			
		if (catSelectStart+num-1 < count)
		{
			if (catSelectStart > 1)
				rez += ' | ';
			if (count - nitem < k)
			rez += '<a href="javascript:'+func+'('+nitem+')"> Last '+(count - nitem+1)+' &gt;&gt;';
			else
			rez += '<a href="javascript:'+func+'('+nitem+')"> Next '+k+' &gt;&gt;';
		}
		obj.innerHTML = rez;
		document.getElementById("srch_info").style.display = "block";
				
		
		if (document.getElementById("moreinfo_box").style.display == "none")
		document.getElementById("srch_rez_box").style.display = "block";
		
	
		
		var srch = document.getElementById("srch_searchList");	  	   		
		var alldata = "";
		var tmpid = "";
		if (map) map.clearExcept(selectedRecord);	
		
			for (i=0; i < srch.childNodes.length; i++)
			{				
				tmpid = srch.childNodes[i].id;				
				if (tmpid && tmpid.substring(0,7) == "itmlok_" && selectedRecord != tmpid.substring(7))		
				{		
					srch.childNodes[i].loc = true;					
					
					alldata += tmpid.substring(7)+"#!r!#" + srch.childNodes[i].childNodes[1].value+"$$mm$$";
//					var d = srch.childNodes[i].childNodes[1].value.split("#!r!#");						
//					map.createMarker(tmpid.substring(7),d[0],d[1],d[2],d[3],d[4]);					
					
				}
			}		
		//alert(alldata);
		//alert(mapInit);		
		if (alldata)
		map.createMultiMarkers(alldata);		
		if (srch.childNodes.length == 1)
		{
			selectedRecord = tmpid.substring(7);
			map.showSubject(selectedRecord,"noclean,open,onlyshow,active");
		}
			
		//alert(alldata);		
		srch.style.display = "block";			
	}	
	else
	{			  	
	  	document.getElementById("searchTitle").innerHTML = "<center>No Results Found</center>";
	  	//obj = document.getElementById("searchLocRez");	  	
	  	//obj.innerHTML = "no result";      	  	
	  		
			rezMd5 = "";
			var obj = document.getElementById("srch_rez");
			obj.innerHTML = "";			
			document.getElementById("srch_info").style.display = "none";
			if (!selectedRecord)
			{
				//document.getElementById("srch_rez_box").style.display = "none";
				window.setTimeout(hideSearchRezDontReset,2000);	  
			}
			if (map) map.clearExcept(selectedRecord);	
	}	
	if (selectedRecord)
			clickListItem(selectedRecord,true);
	
	if (selectedRecord)
		map.showSubject(selectedRecord,"noclean,onlyshow,active,open");		
	
	document.getElementById("loader1").style.display = "none";
	
	//alert(str);
}

function showLoader()
{
	document.getElementById("loading").style.display = "inline";
}

function hideLoader()
{
	document.getElementById("loading").style.display = "none";
}

function disableDropDowns(start,end,cur)
{	
	if (!end) end = 6;
	for (var i=start; i <= end; i++)
	{
		obj = document.getElementById("searchSel"+i);	
		if (obj)
		{	
			if (i != cur)
			{		
				obj.selectedIndex = 0;			
				obj.disabled = "disabled";
			}
		}
	}	
}

function enableDropDowns(start,end)
{
	dropDownsCount = 0;
	dropDownsLeft = 0;
	if (!end) end = 6;	
	//alert(end);
	for (var i=start; i <= end; i++)
	{
		obj = document.getElementById("searchSel"+i);		
		if (obj) obj.disabled = "";
	}	
}

var dropDownsLeft = 0;
var dropDownsCount = 0;

function setObjBound(obj)
{
	var opt = obj.options[obj.selectedIndex];
	var val = opt.value.split(";");	
	var b = val[0].split(",");
	selectedBound.xMin = b[0];
	selectedBound.xMax = b[1];
	selectedBound.yMin = b[2];
	selectedBound.yMax = b[3];
	//alert(selectedBound.xMin);
	
	if (map && updateMapBound)
	{
		//map.modifyObjectsColor("cntry"+val[2]);
		//alert(" - "+val[0]);
		map.getBound(val[0]);	
	}		
}

function searchSelect1(obj,noupdate)
{
	var q = "";	
	if (obj.selectedIndex > 0)
	{				
	document.getElementById("resetLink").style.display = "inline";			
	showMap();		
	var opt = obj.options[obj.selectedIndex];
	var val = opt.value.split(";");	
	
	setObjBound(obj);
	
	selectedProv = val[1];
	
	q = "prov="+val[1];
		
	}
	else
	{		
		document.getElementById("resetLink").style.display = "none";
		map.zoomToFull();
		selectedProv = "";
		selectedBound = new Object();		
	}
			
	if (!noupdate)
	{				
		//hideSearchRez();		
		
		/*
		if (obj.selectedIndex)
		{
			showLoader();			
			if (dropDownsCount < 5)
			dropDownsCount = 5;
			dropDownsLeft = 3;			
			disableDropDowns(1,6,1);
		}
		else
			disableDropDowns(2);
			*/
		
		//xmlhttpPost("/forms/search/sel1.php",q,searchSelect1CB);		
		
		//xmlhttpPost("/forms/search/sel2.php",q,searchSelect2CB);
		disableDropDowns(1,6,1);
		xmlhttpPost("/forms/search/sel3.php",q,searchSelect3CB);
		//xmlhttpPost("/forms/search/sel4.php",q,searchSelect4CB);					
	}
}

function searchSelect1CB(str)
{		
	var obj = document.getElementById("searchSel2");			
	if (!str) 
	{
		obj.selectedIndex = 0;
		obj.disabled = "disabled";		
		selectedBound = new Object();
		populating = 0;
		updateMapBound = 1;
		selectedDist = "";		
		selectedMainp = "";
		selectedMunic = "";
		selectedSub = "";
	}
	else
	{			
		var obj_cont = document.getElementById("searchSel2_cont");
		var rez = '<select style="width:180px;margin-bottom:5px;height:20px;" id="searchSel2" onchange="searchSelect2(this)" disabled="disabled">';		
		rez += "<option >-- District Municipality --</option>"+str;		
		rez += "</select>"
		obj_cont.innerHTML = rez;					
		if (populating)
		setDropDown("searchSel2","fn_dist",searchSelect2);				
		enableDropDowns(1,2);
	}		
}

function searchSelect3CB(str)
{			
	var obj = document.getElementById("searchSel4");
	if (!obj) return;
	
	if (!str) 
	{		
		obj.selectedIndex = 0;
		obj.disabled = "disabled";
		selectedSub = "";		
		selectedBound = new Object();		
		populating = 0;
		updateMapBound = 1;			
	}
	else
	{		
		var obj_cont = document.getElementById("searchSel4_cont");
		if (obj_cont)
		{
			var rez = '<select style="width:180px;margin-bottom:5px;height:20px;" id="searchSel4" onchange="searchSelect4(this)" disabled="disabled">';		
			rez += "<option>> Town or Place</option>"+str;		
			rez += "</select>"
			obj_cont.innerHTML = rez;		
			if (populating)
			setDropDown("searchSel4","fn_mainp",searchSelect4);
		}
		enableDropDowns(1,4);		
	}		
	hideLoader();		
}


function searchSelect4(obj,noupdate,w)
{
	var q = "";
		
	if (obj.selectedIndex > 0)
	{
		document.getElementById("resetLink").style.display = "inline";			
		showMap();
		var opt = obj.options[obj.selectedIndex];
		var val = opt.value.split(";");										
			var hid = document.getElementById("fn_mainp");	
	if (hid) hid.value = val[1];
		
				
		if (!noupdate)
		{							
			if (map && updateMapBound)	
			map.getBound(val[0]);		
		
			var b = val[0].split(",");
			selectedBound.xMin = b[0];
			selectedBound.xMax = b[1];
			selectedBound.yMin = b[2];
			selectedBound.yMax = b[3];
	
			q = "id="+val[1];		
			
			selectedMunic = "";
			selectedSub = "";
		}
		
		selectedMainp = val[2];
				
				
	}
	else
	{
		selectedMainp = "";		
		setObjBound(document.getElementById("searchSel1"));					
	}			
	if (!noupdate)	
	{
		//hideSearchRez();
		//alert("rtest");
		disableDropDowns(4,6,4);
		xmlhttpPost("/forms/search/sel4.php",q,searchSelect4CB);		
	}
}

function searchSelect4CB(str)
{			
	var obj = document.getElementById("searchSel5");
	if (!populating)
	{		
		var hid = document.getElementById("fn_suburb");	if (hid) hid.value = "";
		var hid = document.getElementById("fn_street");	if (hid) hid.value = "";
	}
	if (!str) 
	{		
		obj.selectedIndex = 0;
		obj.disabled = "disabled";
		selectedSub = "";		
		selectedBound = new Object();		
		populating = 0;
		updateMapBound = 1;

	}
	else
	{		
		var obj_cont = document.getElementById("searchSel5_cont");
		var rez = '<select style="width:180px;margin-bottom:5px;height:20px;" id="searchSel5" onchange="searchSelect5(this)" disabled="disabled">';		
		rez += "<option>>Suburb</option>"+str;		
		rez += "</select>"
		obj_cont.innerHTML = rez;		
		if (populating)
		setDropDown("searchSel5","fn_suburb",searchSelect5);
		enableDropDowns(1,5);		
	}					
}



function searchSelect5(obj,noupdate,w)
{
	var q = "";		
	if (obj.selectedIndex > 0)
	{		
		document.getElementById("resetLink").style.display = "inline";			
		showMap();
		var opt = obj.options[obj.selectedIndex];
		var val = opt.value.split(";");													
		
		
		selectedSub = val[1];		
		if (!noupdate)
		{						
		//hideSearchRez();
		selectedMainp = "";
		selectedMunic = "";
		
		setObjBound(obj);							
		
		q = "id="+val[1];	
		}		
		
	}
	else
	{
		setObjBound(document.getElementById("searchSel4"));					
		selectedSub = "";				
		
	}		
	/*
	if (!noupdate)	
	{
		
		hideSearchRez();				
		if (obj.selectedIndex)
		{
		showLoader();		
		if (dropDownsCount < 6)
		dropDownsCount = 6;
		dropDownsLeft = 1;			
		disableDropDowns(5,6,5);		
		}
		else
			disableDropDowns(6);		
		xmlhttpPost("/forms/search/sel5.php",q,searchSelect5CB);		
	}
	*/
}

var updateMapBound = 1;
var populating = 0;

function populateDropDowns()
{
	if (document.getElementById("fn_x").value == 0 && document.getElementById("fn_y").value == 0)
		updateMapBound = 1;
	else
		updateMapBound = 0;
	populating = 1;	
	setDropDown("searchSel1","fn_prov",searchSelect1);
}

function testItemValue(opt,hid)
{
	var val = opt.value.split(";");							
	return (val[1] == hid.value);
}

function testItemValue3(opt,testval)
{
	var val = opt.value.split(";");			
	var ar = val[1].split(",");
				
	for (var k=0; k < ar.length;k++)			
	if (ar[k] == testval)
		return true; 
									
	return false;
}

function testItemValue2(opt,w)
{
	var val = opt.value.split(";");				
	return val[1] == w;
}


function setDropDown(obj_name,hid_name,func)
{
	var obj = document.getElementById(obj_name);
	var hid = document.getElementById(hid_name);
	if (obj && hid)
	{
		for (var i=1; i < obj.options.length;i++)
		if (testItemValue(obj.options[i], hid) || (obj.options[i].innerHTML == hid.value))
		{
			obj.selectedIndex = i;
			func(obj);
			break;
		}
	}
}

function setDropDownLookInArray(obj_name,hid_name,func)
{
	var obj = document.getElementById(obj_name);
	var hid = document.getElementById(hid_name);	
	if (obj && hid)
	{
		for (var i=1; i < obj.options.length;i++)
		if (testItemValue3(obj.options[i], hid.value) || (obj.options[i].innerHTML == hid.value))
		{
			obj.selectedIndex = i;
			func(obj);
			break;
		}
	}
}

function setDropDownLookInArray2(obj_name,val,func)
{
	var obj = document.getElementById(obj_name);	
	if (!obj) return;
	if (val)
	{		
		for (var i=1; i < obj.options.length;i++)
		if (testItemValue3(obj.options[i], val) || (obj.options[i].innerHTML == val))
		{
			obj.selectedIndex = i;
			func(obj,true);		
			break;
		}
	}
}

function setDropDown2(obj_name,val,func)
{
	var obj = document.getElementById(obj_name);		
	if (!obj) return;
	if (val)
	{
		for (var i=1; i < obj.options.length;i++)
		if (testItemValue2(obj.options[i], val) || (obj.options[i].innerHTML == val))
		{
			obj.selectedIndex = i;	
			func(obj,true);		
			return;
		}
	}	
	obj.selectedIndex = 0;
	func(obj,true,val);		
}