var currPhoto = 0;
var prevPhoto = 0;
var formSent = new Boolean(false);

function testIsValidObject(objToTest)
{
	if (null == objToTest) {
		return false;
	}
	if ("undefined" == typeof(objToTest) ) {
		return false;
	}
	if ("undefined" == objToTest) {
		return false;
	}
	return true;
}


function swapPhoto(imageUrl,linkUrl)
{

	var mainImage = document.getElementById("largePhoto");
	
	mainImage.src = imageUrl;

	var newlink = new String(linkUrl);

	if(newlink.length == 0)
	{
		document.getElementById("largephotolink").href = newlink.replace(/^/g,"'");
	}
}


function rotatePhoto(clientid,direction,photoList,photowidth,photoheight,photocrop)
{

	var photousewidth,photouseheight,sharpen,Pic;

	Pic = document.images.largePhoto.src;

	if(Pic.indexOf("Sharpen=true") >= 0)
	{
		sharpen = "true";
	}else
	{
		sharpen = "false";
	}

	if(Pic.indexOf("UseHeight=true") >= 0)
	{
		photouseheight = "true";
	}else
	{
		photouseheight = "false";
	}

	if(Pic.indexOf("UseWidth=true") >= 0)
	{
		photousewidth = "true";
	}else
	{
		photousewidth = "false";
	}

	if(testIsValidObject(document.propertydet))
	{

		if(testIsValidObject(document.propertydet.photousewidth))
		{
			photousewidth = document.propertydet.photousewidth.value;
		}
	
		if(testIsValidObject(document.propertydet.photouseheight))
		{
			photouseheight = document.propertydet.photouseheight.value;
		}
	}

	prevPhoto = currPhoto;

	if(direction == 'prev')
	{
		currPhoto = currPhoto - 1;
	}else
	{
		currPhoto = currPhoto + 1;
	}

	
	var photoArr = photoList.split(';');


	if(currPhoto == -1)
	{
		currPhoto = photoArr.length - 1;
	}


	if(photoArr[currPhoto] == null)
	{
		currPhoto = 0;
	}


	document.images.largePhoto.src = "http://www.10ninety.co.uk/1090/PPS/resize.asp?ClientId=" + clientid + "&File=propertyphotos/" + photoArr[currPhoto] + "&Width=" + photowidth + "&Height=" + photoheight + "&C=" + photocrop + "&RS=true&UseWidth=" + photousewidth + "&UseHeight=" + photouseheight + "&Sharpen=" + sharpen;


}




function virtualTour(url,width,height){

	newWin = window.open(url,'virtualTour','width=' + width + ',height=' + height + ',toolbar=no,location=no,scrollbars=yes')

}




function printProperty(PropId,preview,propind){

	newWin = window.open('printproperty.asp?Id=' + PropId + '&Prev=' + preview + '&propind=' + propind,'printWindow','width=725,height=550,toolbar=yes,menubar=yes,location=no,scrollbars=yes')

}



function DoCallback(url){
   // url:    URL to invoke
   // params: string object to pass to the remote URL

   // Add some parameters to the query string
   var pageUrl = url

   // Initialize the XmlHttp object
    try {
        //Mozilla Browsers
        xmlRequest = new XMLHttpRequest();
    } 
    catch (e) {
        try {
            //IE
            xmlRequest = new ActiveXObject("Microsoft.XMLHTTP");
        } 
        catch (e) {
            //Something else that won't work with this code...
            xmlRequest=false;
        }
    } 
    // Post our XmlRequest and get our desired string
    xmlRequest.open("GET", pageUrl, false);
    xmlRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xmlRequest.send(null);

    // Return the XmlHttp object
    return xmlRequest;
}





function changePriceList(){

	changeminPriceList();

	changemaxPriceList();

	changeLocationLists('COUNTRY:TOWN:AREA');

}



function changeminPriceList(){

	var elems,propind,priceList,optionList,listStr,pArr;

	elems = document.propsearch.elements
	elems["minprice"].length = 1;
	elems["minprice"].options[0].text = "No min";
	elems["minprice"].options[0].value = "";

	if(elems["propind"][0].checked)
	{
		propind = elems["propind"][0].value;
	}else
	{
		propind = elems["propind"][1].value;
	}


	if(propind == "L")
	{
		listStr = new String(elems["RentPriceList"].value);
		optionList = listStr.split(";");
	}else
	{
		listStr = new String(elems["SalesPriceList"].value);
		optionList = listStr.split(";");
	}


	for(i = 0;i < optionList.length;i++)
	{
		elems["minprice"].length++;
		pArr = optionList[i].split("-");
		if(pArr[1] == "")
		{
			elems["minprice"].options[i + 1] = new Option("£" + pArr[0] + "+",optionList[i]);
		}else
		{
			elems["minprice"].options[i + 1] = new Option("£" + optionList[i],optionList[i]);
		}
	}

}

function changemaxPriceList(){

	var elems,propind,priceList,optionList,listStr,pArr;

	elems = document.propsearch.elements
	elems["maxprice"].length = 1;
	elems["maxprice"].options[0].text = "No max";
	elems["maxprice"].options[0].value = "";

	if(elems["propind"][0].checked)
	{
		propind = elems["propind"][0].value;
	}else
	{
		propind = elems["propind"][1].value;
	}


	if(propind == "L")
	{
		listStr = new String(elems["RentPriceList"].value);
		optionList = listStr.split(";");
	}else
	{
		listStr = new String(elems["SalesPriceList"].value);
		optionList = listStr.split(";");
	}


	for(i = 0;i < optionList.length;i++)
	{
		elems["maxprice"].length++;
		pArr = optionList[i].split("-");
		if(pArr[1] == "")
		{
			elems["maxprice"].options[i + 1] = new Option("£" + pArr[0] + "+",optionList[i]);
		}else
		{
			elems["maxprice"].options[i + 1] = new Option("£" + optionList[i],optionList[i]);
		}
	}

}




function changeLocationLists(locTypes)
{
	var propind,elems;

	if(document.propsearch.propind)
	{

		elems = document.propsearch.elements
		
		if(eval(elems["propind"][0]))
		{
			if(elems["propind"][0].checked)
			{
				propind = elems["propind"][0].value;
			}else
			{
				propind = elems["propind"][1].value;
			}
		}else
		{
			propind = elems["propind"].value;
		}
	
	}else
	{
		propind = "L";
	}

	if(locTypes.indexOf("COUNTRY") != -1)
	{
		changeCountryList(propind);
	}
	
	if(locTypes.indexOf("TOWN") != -1)
	{
		changeTownList(propind);
	}

	if(locTypes.indexOf("AREA") != -1)
	{
		changeAreaList(propind);
	}

}




function changeCountryList(proptype){

	var elems,propind,priceList,optionList,wrkList,listStr,wrkc,wrkid,wrkname;

	if(testIsValidObject(document.propsearch.country))
	{

		elems = document.propsearch.elements

		if(!isNaN(parseFloat(elems["country"].length)))
		{

		elems["country"].length = 2;
		elems["country"].options[0].text = "All";
		elems["country"].options[0].value = "";
		elems["country"].options[1].text = "-----";
		elems["country"].options[1].value = "";

		if(proptype == "L")
		{
			listStr = new String(elems["RentCountryList"].value);
			optionList = listStr.split(";");
		}else
		{
			listStr = new String(elems["SalesCountryList"].value);
			optionList = listStr.split(";");
			//alert (listStr)
		}

		for(i = 0;i < optionList.length;i++)
		{
			elems["country"].length++;
			wrkc = new String(optionList[i]);
			if(wrkc.indexOf(":") != -1)
			{
				wrkList = wrkc.split(":");
				wrkid = wrkList[0];
				wrkname = wrkList[1];
			}else
			{
				wrkid = optionList[0];
				wrkname = optionList[0];
			}
			elems["country"].options[elems["country"].length - 1] = new Option(wrkname,wrkid);
		}

		}

	}
}




function changeTownList(proptype){

	var elems,propind,priceList,optionList,listStr,doChange,townresponse,wrkList,listStr,wrkc,wrkid,wrkname,country,ignoresetlists;
	var extratownsql;

	doChange = false;
	ignoresetlists = true;

	if(document.propsearch.proptown && testIsValidObject(document.propsearch.proptown))
	{

		elems = document.propsearch.elements
		elems["proptown"].length = 2;
		elems["proptown"].options[0].text = "All";
		elems["proptown"].options[0].value = "";
		elems["proptown"].options[1].text = "-----";
		elems["proptown"].options[1].value = "";

		if(document.propsearch.country && !isNaN(parseFloat(elems["country"].length)))
		{
			if(elems["country"].options[elems["country"].selectedIndex].value != "")
			{
				country = elems["country"].options[elems["country"].selectedIndex].value;
				doChange = true;
			}else
			{
				elems["proptown"].length++;
				elems["proptown"].options[elems["proptown"].length - 1] = new Option("Please select a country","");
			}				
		}else if(testIsValidObject(document.propsearch.country))
		{
			country = elems["country"].value;
			doChange = true;
		}else
		{
			country = "";
			doChange = true;
		}

		if(doChange)
		{
			if(elems["RentTownList"] && elems["SalesTownList"] && !ignoresetlists)
			{
				if(proptype == "L")
				{
					listStr = new String(elems["RentTownList"].value);
					optionList = listStr.split(";");
				}else
				{
					listStr = new String(elems["SalesTownList"].value);
					optionList = listStr.split(";");
					//alert (listStr)
				}
			}else
			{
				if(testIsValidObject(document.propsearch.extratownsql))
				{
					extratownsql = document.propsearch.extratownsql.value;
				}else
				{
					extratownsql = "";
				}

				areaResponse = DoCallback('gettowns.asp?Search=true&Country=' + country + '&PropInd=' + proptype + '&extrasql=' + extratownsql);
				listStr = areaResponse.responseText;
				//alert (listStr);
				optionList = listStr.split(";");
			}

			for(i = 0;i < optionList.length;i++)
			{
				elems["proptown"].length++;
				wrkc = new String(optionList[i]);
				if(wrkc.indexOf(":") != -1)
				{
					wrkList = wrkc.split(":");
					wrkid = wrkList[0];
					wrkname = wrkList[1];
				}else
				{
					wrkid = optionList[i];
					wrkname = optionList[i];
				}
				elems["proptown"].options[elems["proptown"].length - 1] = new Option(wrkname,wrkid);
			}
		}
	}
}




function changeAreaList(proptype){

	var elems,optionList,listStr,areaResponse,townSelect,areaSelect,optionParts,reselectAreaIndex,indexAddOn,propArea,areaSelIndex,propind,wrktown;
	var extraareasql;

	var doChange = false;

	if(document.propsearch.proparea)
	{

		elems = document.propsearch.elements;

		areaSelect = elems["proparea"];
		areaSelect.length = 2;
		areaSelect.options[0].text = "All";
		areaSelect.options[0].value = "";
		areaSelect.options[1].text = "-----";
		areaSelect.options[1].value = "";

		if(elems["proptown"])
		{
			townSelect = elems["proptown"];
			if(townSelect.options[townSelect.selectedIndex].value != "")
			{
				doChange = true;
				wrktown = townSelect.options[townSelect.selectedIndex].value;
			}else
			{
				areaSelect.length++;
				areaSelect.options[areaSelect.length] = new Option("","Please select a town");
			}	
		}else
		{
			wrktown = elems["town"].value;
			doChange = true;
		}

		if(doChange)
		{	

			if(testIsValidObject(document.propsearch.extraareasql))
			{
				extraareasql = document.propsearch.extraareasql.value;
			}else
			{
				extraareasql = "";
			}

			areaResponse = DoCallback('getareas.asp?Search=true&Town=' + wrktown + '&PropInd=' + proptype + '&extrasql=' + extraareasql);

			listStr = areaResponse.responseText;
			//alert(listStr);
	
			optionList = listStr.split(";");
			for(i = 0 ;i < optionList.length;i++)
			{
				// optionParts = optionList[i].split(";");
				// areaSelect.options[i + indexAddOn] = new Option(optionParts[1],optionParts[0]);

				areaSelect.length++;			
				areaSelect.options[areaSelect.length - 1] = new Option(optionList[i],optionList[i]);

				//alert ('value = ' + areaSelect.options[areaSelect.length - 1].value + 'text = ' + areaSelect.options[i+indexAddOn-1].text);

			}

		}
	}
}




function changemlPriceList(){

	var elems,proptype,priceList,optionList,listStr,pArr;

	elems = document.mailinglist.elements
	elems["Price"].length = 0;

	if(elems["proptype"][0].checked)
	{
		proptype = elems["proptype"][0].value;
	}else
	{
		proptype = elems["proptype"][1].value;
	}

	if(proptype == "L")
	{
		listStr = new String(elems["RentPriceList"].value);
		optionList = listStr.split(";");
	}else
	{
		listStr = new String(elems["SalesPriceList"].value);
		optionList = listStr.split(";");
	}


	for(i = 0;i < optionList.length;i++)
	{
		elems["Price"].length++;
		pArr = optionList[i].split("-");
		if(pArr[1] == "")
		{
			elems["Price"].options[i] = new Option(pArr[0] + "+",optionList[i]);
		}else
		{
			elems["Price"].options[i] = new Option(optionList[i],optionList[i]);
		}
	}

}




function loadGoogleMap(lat,long,gmapstart)
{
	if (GBrowserIsCompatible())
	{

		// var lat = document.getElementById("lat").value;
		// var long = document.getElementById("long").value;

		if(eval(lat) && eval(long))
		{
			var map = new GMap2(document.getElementById("map"));
			map.addControl(new GSmallMapControl());
			map.addControl(new GMapTypeControl());
			var point = new GLatLng(lat,long)
			map.setCenter(point, gmapstart);
			map.addOverlay(new GMarker(point));
		}else
		{
			alert("no");
		}

	}
}




function enlargePhoto(Pic){

	var sharpen;

	Pic = document.images.largePhoto.src;

	if(Pic.indexOf("Sharpen=true") >= 0)
	{
		sharpen = "true";
	}else
	{
		sharpen = "false";
	}

	var srcArray = Pic.split("&Width");

	Pic = srcArray[0] + "&Width=600&Height=600&Rs=true&C=false&Sharpen=" + sharpen;
	Pic = Pic.replace("?","*");

	newWin = window.open('viewPhoto.asp?' + Pic,'photoWindow','width=600,height=600,toolbar=no,location=no,scrollbars=yes,resize=yes');

}



function photoGallery(propref,wrkwidth,wrkheight){

	newWin = window.open('photoGallery.asp?Ref=' + propref,'photoWindow','width=' + wrkwidth + ',height=' + wrkheight + ',toolbar=no,location=no,scrollbars=yes,resize=yes');

}

