	
//	var objectsURL = 'http://www.lava-monkey.com/lxrent/dbobjects/';
//	var mainURL = 'http://www.lava-monkey.com/lxrent/';
	var objectsURL = 'http://www.lxrent.com/dbobjects/';
	var mainURL = 'http://www.lxrent.com/';

	
	function removeAllOptions(selectbox)
	{
		var i;
		for(i=selectbox.options.length-1;i>=0;i--)
		{
			//selectbox.options.remove(i);
			selectbox.remove(i);
		}
	}
	
	function addOption(selectbox, value, text)
	{
		var optn = document.createElement("OPTION");
		optn.text = text;
		optn.value = value;
	
		selectbox.options.add(optn);
	}

	function SetPropertyType(target){
		// Load Category Xml
		var propertyTypeXml = XmlRequestLoader(objectsURL+'BORequestPropertyType.asp?target=' + target);
//		var propertyTypeXml = XmlRequestLoader('http://www.lxrent.com/dbobjects/BORequestPropertyType.asp?target=' + target);
//		alert(propertyTypeXml.xml);
		// Read Property Types node
		var propertyTypesListNode = propertyTypeXml.selectSingleNode('/lxrent_response/property_types');
		// 
		if(propertyTypesListNode != null && propertyTypesListNode.childNodes.length > 0){
			// Empty SelectBox
			removeAllOptions(document.getElementById('propertytype'));
			// Add Default SelectBox Option
			addOption(document.getElementById('propertytype'), '0', '-- Todos --');
			// Fill SelectBox with a new 
			for(var i=0;i<propertyTypesListNode.childNodes.length;i++){//percorrendo os filhos do nó
				if(propertyTypesListNode.childNodes[i].nodeType == 1){//ignorar espaços em branco
					addOption(document.getElementById('propertytype'), propertyTypesListNode.childNodes[i].attributes[0].value, propertyTypesListNode.childNodes[i].attributes[1].value);
				}
			}
		}

	}

	function SetCategory(target){
		// Load Category Xml
		var categoryXml = XmlRequestLoader(objectsURL+'BORequestCategory.asp?target=' + target + '&distritovalue=' + document.getElementById('distrito').value + '&concelhovalue=' + document.getElementById('concelho').value + '&locationvalue=' + document.getElementById('location').value);

//		var categoryXml = XmlRequestLoader('http://www.lxrent.com/dbobjects/BORequestCategory.asp?target=' + target + '&distritovalue=' + document.getElementById('distrito').value + '&concelhovalue=' + document.getElementById('concelho').value + '&locationvalue=' + document.getElementById('location').value);
		//alert('XML: ' + categoryXml.xml);
		// Read parent node
		var parentNode = categoryXml.selectSingleNode('/lxrent_response');
		if(parentNode != null){
			var parentID = parentNode.childNodes[0].attributes[0].value;
			var parentName = parentNode.childNodes[0].attributes[1].value;
			var parentLat = parentNode.childNodes[0].attributes[2].value;
			var parentLng = parentNode.childNodes[0].attributes[3].value;
			var parentZoom = parentNode.childNodes[0].attributes[4].value;
			parentNode = null;
			// Set google map possition if available
			if((parentLat != '0' && parentLng != '0') && (parentLat.length > 0 && parentLng.length > 0)){
				if(parentLat.indexOf(',') > -1){parentLat = parentLat.replace(',','.');}
				if(parentLng.indexOf(',') > -1){parentLng = parentLng.replace(',','.');}
				if(top.map != null){
					if(parentLng < -10){if(parseInt(parentZoom) > 10){parentZoom = '10';}}
					top.map.setCenter(new GLatLng(parentLat, parentLng), parseInt(parentZoom));
				}
			}
		}
		// To set the location only we don't need to go further
		if(target.toString().toUpperCase() == 'SET_LOCATION'){return;}
		
		// Read categories node
		var categoryListNode = categoryXml.selectSingleNode('/lxrent_response/categories');

// 
		if(categoryListNode != null && categoryListNode.childNodes.length > 0){
			// Empty SelectBox
			removeAllOptions(document.getElementById(target));
			// 
			if(target == 'distrito'){addOption(document.getElementById('distrito'), '0', '-- Todos --');}
			if(target == 'concelho'){addOption(document.getElementById('concelho'), '0', '-- Todos --');}
			if(target == 'location'){addOption(document.getElementById('location'), '0', '-- Todas --');}
			
			// Fill SelectBox with a new
			for(var i=0;i<categoryListNode.childNodes.length;i++){//percorrendo os filhos do nó
				if(categoryListNode.childNodes[i].nodeType == 1){//ignorar espaços em branco
					addOption(document.getElementById(target), categoryListNode.childNodes[i].attributes[0].value, categoryListNode.childNodes[i].attributes[1].value);
				}
			}
			// Check for hidden values (Existing in case of EDIT)
			if(target == 'distrito'){
				if(setEditCategoryValue('distrito')){
					SetCategory('concelho');
				}else{return;}
			}
			if(target == 'concelho'){
				// Reset Location
				removeAllOptions(document.getElementById('location'));
				addOption(document.getElementById('location'), '0', '-- Depende do Concelho --');
				// Check edit mode
				if(setEditCategoryValue('concelho')){
					SetCategory('location');
				}else{return;}
			}
			if(target == 'location'){
				if(setEditCategoryValue('location')){
					SetCategory('set_location');
				}
			}
		}
		else{
			// Reset sequential SelectBoxes
			if(target == 'distrito'){
				removeAllOptions(document.getElementById('concelho'));
				addOption(document.getElementById('concelho'), '0', '-- Depende do Distrito --');
				removeAllOptions(document.getElementById('location'));
				addOption(document.getElementById('location'), '0', '-- Depende do Concelho --');
			}
			if(target == 'concelho'){
				removeAllOptions(document.getElementById('concelho'));
				addOption(document.getElementById('concelho'), '0', '-- Depende do Distrito --');
				removeAllOptions(document.getElementById('location'));
				addOption(document.getElementById('location'), '0', '-- Depende do Concelho --');
			}
			if(target == 'location'){
				removeAllOptions(document.getElementById('location'));
				addOption(document.getElementById('location'), '0', '-- Depende do Concelho --');
			}
		}
	}
	
	// Check for hidden values (Existing in case of EDIT)
	function setEditCategoryValue(target)
	{
		//alert(document.getElementById(target+'_hidden_value').value);
		var objectNewValue = document.getElementById(target+'_hidden_value');

		if(objectNewValue != null && objectNewValue.value.length > 0){
			var newValue = objectNewValue.value;
			var objectTargetOptions = document.getElementById(target).options;
			for (var i=0;i<objectTargetOptions.length;i++) {
				if (newValue == objectTargetOptions[i].value) {
					objectTargetOptions[i].selected = true;
					return true;
				}
			}
			return true;
		}else{
			return false;
		}
	}
	
	function setDefaultCategoryValues(pageType){
		SetCategory('distrito');
		if(pageType.toUpperCase() != 'EDIT'){
			removeAllOptions(document.getElementById('concelho'));
			addOption(document.getElementById('concelho'), '0', '-- Depende do Distrito --');
			removeAllOptions(document.getElementById('location'));
			addOption(document.getElementById('location'), '0', '-- Depende do Concelho --');
		}else{
			// Set Default point (EDIT mode)
			if(document.getElementById('latitude').value.length > 0 && document.getElementById('longitude').value.length > 0){
				// Get point
				var newLat = parseFloat(document.getElementById('latitude').value);
				var newLng = parseFloat(document.getElementById('longitude').value);
				var newPoint = new GLatLng(newLat, newLng);
				//alert(newPoint);
				// Create our "tiny" marker icon
				var icon = new GIcon();
				icon.image = 'images/bannercasabranco.gif';
				icon.iconSize = new GSize(16, 16);
				icon.iconAnchor = new GPoint(6, 20);
				icon.infoWindowAnchor = new GPoint(5, 1);
				// Set point
				var marker = new GMarker(newPoint, icon);
				map.addOverlay(marker);
				// Center map
				map.setCenter(newPoint, 16);
				//document.getElementById('latitude').value = newLat;
				//document.getElementById('longitude').value = newLng;
			}
		}
	}

	function setDefaultPropertyTypeValues(){
		SetPropertyType('alojamento');
		document.getElementById('searchtype_1').checked = true;
	}

	// Request Xml Document
	function XmlRequestLoader(url){
		var Loader = new ActiveXObject("Msxml2.DOMDocument.3.0");
		Loader.async = false;
		Loader.load(url);
		return Loader;

		
/*		
		if(window.XMLHttpRequest){
		try {
				var Loader = new XMLHttpRequest();
				Loader.open("GET", url, false);
				//Loader.send(null);
				alert('XML...BEFORE RETURN: ' + Loader.responseText);
				return Loader.responseXML;
			
				alert('1');
				var Loader = new XMLHttpRequest();
				Loader.open("GET", url, true);
				Loader.send(null);
				return Loader.responseXML;
			} catch (e) {
				alert(e);
			}
		}else if(window.ActiveXObject){
			alert('2');
			var Loader = new ActiveXObject("Msxml2.DOMDocument.3.0");
			Loader.async = false;
			Loader.load(url);
			return Loader;
		}*/
	}
	
	function getProperties(target, bounds){
		var southWestPoint = bounds.getSouthWest();
		var northEastPoint = bounds.getNorthEast();
		var SWLat = southWestPoint.lat()-3;
		var SWLng = southWestPoint.lng();
		var NELat = northEastPoint.lat()+3;
		var NELng = northEastPoint.lng();
		
		// Handle XML 
		var markersXml = XmlRequestLoader(objectsURL+'BORequestProperties.asp?target='+target+'&SWLatitude='+SWLat+'&SWLongitude='+SWLng+'&NELatitude='+NELat+'&NELongitude='+NELng);
		//alert(objectsURL+'BORequestProperties.asp?target='+target+'&SWLatitude='+SWLat+'&SWLongitude='+SWLng+'&NELatitude='+NELat+'&NELongitude='+NELng);
		//alert('XML: ' + markersXml.xml);

		// Parse selected node
		var markerNode = markersXml.selectSingleNode('/markers');

		// Create Markers
		for(var i=0;i<markerNode.childNodes.length;i++){//percorrendo os filhos do nó
			if(markerNode.childNodes[i].nodeType == 1){//ignorar espaços em branco
				var newLat = markerNode.childNodes[i].attributes[0].value;
				var newLng = markerNode.childNodes[i].attributes[1].value;
				var newID = markerNode.childNodes[i].attributes[2].value;
				var newName = markerNode.childNodes[i].attributes[3].value;
				var newType = markerNode.childNodes[i].attributes[4].value;
				var newTitle = markerNode.childNodes[i].attributes[5].value;
				var newLocation = markerNode.childNodes[i].attributes[6].value;
				var newArea = markerNode.childNodes[i].attributes[7].value;
				var newValor = markerNode.childNodes[i].attributes[8].value;
				var newIsDestaque = markerNode.childNodes[i].attributes[9].value;
				var point = new GLatLng(newLat, newLng);

				// Create Property icon
				var newIcon = new GIcon();
/*				if(target.toUpperCase() == 'ALL'){
					if(newIsDestaque.toUpperCase() == 'TRUE'){newIcon.image = "images/bannercasapreto.gif";}else{newIcon.image = "images/bannercasabranco.gif";};
				}
				if(target.toUpperCase() == 'DESTAQUES'){
					if(newIsDestaque.toUpperCase() == 'TRUE'){newIcon.image = "images/bannercasapreto.gif";}else{newIcon.image = "images/bannercasapreto.gif";};
				}
				if(target.toUpperCase() == 'PROPERTIES'){
					if(newIsDestaque.toUpperCase() == 'TRUE'){newIcon.image = "images/bannercasabranco.gif";}else{newIcon.image = "images/bannercasabranco.gif";};
				}*/
				
				if(newType.toUpperCase() == 'ALOJAMENTO'){
					newIcon.image = "images/bannercasapreto.gif";
				}
				if(newType.toUpperCase() == 'COMERCIAL'){
					newIcon.image = "images/bannercasaazul.gif";
				}
				if(newType.toUpperCase() == 'TURISMO RURAL'){
					newIcon.image = "images/bannercasaverde.gif";
				}
				
				newIcon.iconSize = new GSize(16, 16);
				newIcon.iconAnchor = new GPoint(6, 20);
				newIcon.infoWindowAnchor = new GPoint(5, 1);
				
				// Create Marker
				var marker = new GMarker(point, newIcon);
				marker.tooltip = '<div class="googletooltip">&nbsp;<b>'+newName+'</b><br/>&nbsp;'+newTitle.substring(0,25)+'...<br/>&nbsp;'+newLocation+'</div>';
				// 
				AddGMapEvents(marker, newID);
				// 
				map.addOverlay(marker);
			}
		}
	}
	
	function AddGMapEvents(marker, id){
		GEvent.addListener(marker, "click", function() {
				window.location = mainURL+'propertydetails.asp?ID='+id;
				//marker.openInfoWindowHtml('<b>Title</b><br/>description<br/><a href=\"viewproperty.asp\">visualizar</a>');
		});
		GEvent.addListener(marker,"mouseover", function() {
				showTooltip(marker);
		}); 
		GEvent.addListener(marker,"mouseout", function() {
				tooltip.style.visibility='hidden';
		}); 
	}
	
      // ====== This function displays the tooltip ======
	// it can be called from an icon mousover or a side_bar mouseover
	function showTooltip(marker) {
		tooltip.innerHTML = marker.tooltip;
		var point=map.getCurrentMapType().getProjection().fromLatLngToPixel(map.fromDivPixelToLatLng(new GPoint(0,0),true),map.getZoom());
		var offset=map.getCurrentMapType().getProjection().fromLatLngToPixel(marker.getPoint(),map.getZoom());
		var anchor=marker.getIcon().iconAnchor;
		var width=marker.getIcon().iconSize.width;
		var height=tooltip.clientHeight;
		var pos = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(offset.x - point.x - anchor.x + width, offset.y - point.y -anchor.y -height)); 
		pos.apply(tooltip);
		tooltip.style.visibility="visible";
	}
	  
	function TEST_SaveFreguesia(locationid, latitude, longitude){
		if(locationid.toString().length > 0){
			// Load Category Xml
			var responseXml = XmlRequestLoader(objectsURL+'TEST_SAVE_FREGUESIA.asp?locationID=' + locationid + '&latitude=' + latitude + '&longitude=' + longitude);
//			var responseXml = XmlRequestLoader('http://www.lxrent.com/dbobjects/TEST_SAVE_FREGUESIA.asp?locationID=' + locationid + '&latitude=' + latitude + '&longitude=' + longitude);
			// Read status node
			var statusNode = responseXml.selectSingleNode('/lxrent_response');
	
			if(statusNode != null){
				var statusName = statusNode.childNodes[0].attributes[0].value;
				document.getElementById('status_response').innerHTML=statusName;
			}
		}else{
			document.getElementById('latitude').value = '';
			document.getElementById('longitude').value = '';
			document.getElementById('status_response').innerHTML='Tem de escolher uma freguesia';
		}
	}