var delay = 1;
var displayRolloverLabel = true;

function loadProperty(propertyId,toShow) {
	var strURL = "/content/propertydetail.php"
	xmlhttpPostStats(strURL,'property-details','action='+toShow+'&propertyId='+propertyId,true)
	if(toShow=='location-map') setTimeout("propertyMap();", delay * 1000);
	if(toShow=='aerial-photo') setTimeout("propertyArialPhoto();", delay * 1000);
}


function xmlhttpPostStats(strURL,elementId,varString,loadCallBack) {
	var xmlHttpReq = false;
	var self = this;
	// Mozilla/Safari
	if (window.XMLHttpRequest) {
		self.xmlHttpReq = new XMLHttpRequest();
	}
	// IE
	else if (window.ActiveXObject) {
		self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
	}
	self.xmlHttpReq.open('POST', strURL, true);
	self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	self.xmlHttpReq.onreadystatechange = function() {
		if (self.xmlHttpReq.readyState == 4) {
			document.getElementById(elementId).innerHTML = self.xmlHttpReq.responseText;
			if(loadCallBack) 
			{
				loadPropertyCallBack();
				if ($("#property-details-area").css("visibility")=="hidden")
				{
					document.body.style.cursor = 'wait';
					$("#property-details-area").css("visibility", "visible");
					$("#property-details-area").animate({"left": "+=990px"}, "slow");
					$(".select").css("visibility", "hidden");
					document.body.style.cursor = 'default';
				}
				document.body.style.cursor = 'default';
			}
		}
	}
	self.xmlHttpReq.send(varString);
}


var map;
var xAdjustment=100;
var yAdjustment=115;

function getHomePageMap() {
	if (document.getElementById('map'))
	{
		if (GBrowserIsCompatible()) {

			var icon = new GIcon();

			icon.image = "/images/map-icons/map-icon.png";
			icon.iconSize = new GSize(20, 32);
			icon.iconAnchor = new GPoint(10, 32);
			icon.infoWindowAnchor = new GPoint(0, 0);

			
			map = new GMap2(document.getElementById("map"));
			map.enableScrollWheelZoom();
			map.addControl(new GSmallMapControl());
			map.setCenter(new GLatLng(49.465, -2.590), 12);

			GEvent.addListener(map, "zoomend", function() {
				if (map.getZoom()<=12)
				{
					map.setCenter(new GLatLng(49.465, -2.590), 12);
				}
			});


			for (i=0; i<propertiesArray.length; i++)
			{
				createHomePageMarker(icon,i);
			}
		}
	}
}

function createHomePageMarker(icon,arrayId)
{
	var point = new GLatLng(propertiesArray[arrayId][0], propertiesArray[arrayId][1])
	var marker = new GMarker(point, icon);
	marker.value = arrayId;

	GEvent.addListener(marker, "mouseover", function() {
		var gPoint = map.fromLatLngToDivPixel(point);
		var gPoint2 = map.fromLatLngToDivPixel(map.getCenter());
		displayLabel(gPoint.x+(279-gPoint2.x),((gPoint.y+(236-gPoint2.y))*1),arrayId);
	});

	GEvent.addListener(marker, "mouseout", function() {
		hideLabel();
	});

	GEvent.addListener(marker, "click", function() {
		window.location = "/search/index.php?propertyId=" + propertiesArray[arrayId][2];
	});

	map.addOverlay(marker);
}

function displayLabel(x,y,arrayId)
{
	var xposition = x - xAdjustment;
	var yposition = y - yAdjustment;

	xmlhttpPostStats('/content/homepagepropertydetails.php','label','propertyId='+propertiesArray[arrayId][2],false)

	document.getElementById('label').style.left = xposition + 'px';
	document.getElementById('label').style.top = yposition + 'px';
	document.getElementById('label').style.visibility = 'visible';
}

function hideLabel()
{
	document.getElementById('label').style.visibility = 'hidden';
}

function getMap()
{
	if (document.getElementById('properties-map'))
	{
		if (GBrowserIsCompatible()) {

			var icon = new GIcon();

			icon.image = "/images/map-icons/map-icon.png";
			icon.iconSize = new GSize(20, 32);
			icon.iconAnchor = new GPoint(10, 32);
			icon.infoWindowAnchor = new GPoint(0, 0);

			if(($.browser.msie==true) && (jQuery.browser.version.substr(0,1)<7))
			{
				displayRolloverLabel = false;
			}

			map = new GMap2(document.getElementById("properties-map"));
			map.enableScrollWheelZoom();
			map.addControl(new GSmallMapControl());
			map.setCenter(new GLatLng(49.465, -2.590), 12);

			GEvent.addListener(map, "zoomend", function() {
				if (map.getZoom()<=12)
				{
					map.setCenter(new GLatLng(49.465, -2.590), 12);
				}
			});

			for (i=0; i<propertiesArray.length; i++)
			{
				createSearchPageMarker(icon,i);
			}
		}
	}
}


function createSearchPageMarker(icon,arrayId)
{
	var point = new GLatLng(propertiesArray[arrayId][0], propertiesArray[arrayId][1])
	var marker = new GMarker(point, icon);
	marker.value = arrayId;

	if(displayRolloverLabel)
	{

		GEvent.addListener(marker, "mouseout", function() {
			hideLabel();
		});

		GEvent.addListener(marker, "mouseover", function() {
			var gPoint = map.fromLatLngToDivPixel(point);
			var gPoint2 = map.fromLatLngToDivPixel(map.getCenter());
			displayLabel(630+gPoint.x+(279-gPoint2.x),((40+gPoint.y+(236-gPoint2.y))*1),arrayId);
		});

	}

	GEvent.addListener(marker, "click", function() {
		loadProperty(propertiesArray[arrayId][2],"details");
	});



	map.addOverlay(marker);
}




function propertyMap()
{
	if (GBrowserIsCompatible()) {

		var icon = new GIcon();

		icon.image = "/images/map-icons/map-icon.png";
		icon.iconSize = new GSize(20, 32);
		icon.iconAnchor = new GPoint(10, 32);
		icon.infoWindowAnchor = new GPoint(0, 0);

		
		map = new GMap2(document.getElementById("property-map-area"));
		map.enableScrollWheelZoom();
		map.addControl(new GSmallMapControl());
		map.setCenter(new GLatLng(document.getElementById('latitude').value, document.getElementById('longitude').value), 14);

		var marker = new GMarker(new GLatLng(document.getElementById('latitude').value, document.getElementById('longitude').value), icon);
		map.addOverlay(marker);
	
	}

}

function propertyArialPhoto()
{
	if (document.getElementById('aerial-photo-area'))
	{
		if (GBrowserIsCompatible()) {

			var icon = new GIcon();

			icon.image = "/images/map-icons/map-icon.png";
			icon.iconSize = new GSize(20, 32);
			icon.iconAnchor = new GPoint(10, 32);
			icon.infoWindowAnchor = new GPoint(0, 0);

			
			map = new GMap2(document.getElementById("aerial-photo-area"));
			map.enableScrollWheelZoom();
			map.enableScrollWheelZoom();
			map.addControl(new GSmallMapControl());
			map.setCenter(new GLatLng(document.getElementById('latitude').value, document.getElementById('longitude').value), 14, G_SATELLITE_MAP);

			var marker = new GMarker(new GLatLng(document.getElementById('latitude').value, document.getElementById('longitude').value), icon);
			map.addOverlay(marker);

		}
	}
}

function loadPropertyPage(propertyId)
{
	window.location = "/search/index.php?propertyId=" + propertyId;
}

function checkRegistrationForm()
{
	var errorList='';
	if((document.getElementById('first_name').value=='') || (document.getElementById('first_name').value=='')) errorList += '\n - First and Last Name';
	if(document.getElementById('address1').value=='') errorList += '\n - Address 1';
	if(document.getElementById('town').value=='') errorList += '\n - Town';
	if(document.getElementById('county').value=='') errorList += '\n - County';
	if(document.getElementById('postcode').value=='') errorList += '\n - Postcode';
	if(document.getElementById('country').value=='') errorList += '\n - Country';
	if((document.getElementById('mobile_phone').value=='') && (document.getElementById('work_phone').value=='') && (document.getElementById('home_phone').value=='')) errorList += '\n - A telephone number';
	if(document.getElementById('email').value=='') errorList += '\n - Email Address';

	passed = false;
	elementId = 'informationOn[]';
	currentName = document.getElementById(elementId).name
	currentField = document.registrationForm.elements[currentName];
	for(u=0; u < currentField.length; u++){ // changed
		if(currentField[u].checked){ // changed
			passed = true;
		}
	}//ends for u
	if (passed == false)
	{
		errorList += '\n - What you would like more information regarding';
	}

	passed = false;
	elementId = 'areasInterestedIn[]';
	currentName = document.getElementById(elementId).name
	currentField = document.registrationForm.elements[currentName];
	for(u=0; u < currentField.length; u++){ // changed
		if(currentField[u].checked){ // changed
			passed = true;
		}
	}//ends for u
	if (passed == false)
	{
		errorList += '\n - Which areas you are interested in';
	}

	if (errorList!='')
	{
		errorList = 'Please complete the following information' + errorList;
		alert(errorList);
		return false;
	} else {
		return true;
	}
}


function checkValuationForm()
{
	var errorList='';
	if((document.getElementById('first_name').value=='') || (document.getElementById('first_name').value=='')) errorList += '\n - First and Last Name';
	if(document.getElementById('address1').value=='') errorList += '\n - Address 1';
	if(document.getElementById('town').value=='') errorList += '\n - Town';
	if(document.getElementById('county').value=='') errorList += '\n - County';
	if(document.getElementById('postcode').value=='') errorList += '\n - Postcode';
	if(document.getElementById('country').value=='') errorList += '\n - Country';
	if((document.getElementById('mobile_phone').value=='') && (document.getElementById('work_phone').value=='') && (document.getElementById('home_phone').value=='')) errorList += '\n - A telephone number';
	if(document.getElementById('email').value=='') errorList += '\n - Email Address';

	if (errorList!='')
	{
		errorList = 'Please complete the following information' + errorList;
		alert(errorList);
		return false;
	} else {
		return true;
	}
}


function copyAddressDetails()
{
	document.getElementById('property_address1').value=document.getElementById('address1').value;
	document.getElementById('property_address2').value=document.getElementById('address2').value;
	document.getElementById('property_town').value=document.getElementById('town').value;
	document.getElementById('property_county').value=document.getElementById('county').value;
	document.getElementById('property_postcode').value=document.getElementById('postcode').value;
	document.getElementById('property_country').value=document.getElementById('country').value;
}