window.onload = registerHandler
var options = Array();

function gotourl(url) {
	var newwindow = '';


	if (!newwindow.closed && newwindow.location) {
		newwindow.location.href = url;
	}
	else {
		newwindow=window.open(url,'name','scrollbars=1,width=960');
		if (!newwindow.opener) newwindow.opener = self;
	}
	if (window.focus) {newwindow.focus()}
	return false;


}


function changeState(e, regular)
{
	return;
	if (regular == undefined)
	regular = true;
	code	= document.getElementById('country').value;
	states 	= document.getElementById('state');
	var focused = false;
	for (var i = 0; i < states.options.length; ++i) {
		if (states.options[i].value == '')
		continue;
		if (code != states.options[i].id.substr(0, 2)) {
			states.options[i].style.display = 'none';
		} else {
			states.options[i].style.display = 'block';
			if (regular) {
				if (!focused) {
					focused = true;
					states.options[i].selected = true;
				} else
				states.options[i].selected = false;
			}
		}
	}
}

function registerHandler()
{
	return;
	document.getElementById('country').addEventListener('change', changeState, false);
	changeState(null, false);
}

function checkMinMax(id1,id2)
{
	var add;

	if (document.getElementById(id1).value > 999999) add = 1000000;
	else add = 50000;
	//alert(id1.value);
	min =parseInt (document.getElementById(id1).value);
	max =parseInt (document.getElementById(id2).value);
	//alert(min + "vs" + max);
	if (min >= max) {
		document.getElementById(id2).value=min + add;
		if(document.getElementById(id2).value == ""){
			//document.getElementById(id2).options[document.getElementById(id2).options.length] = new Option(min + add, min + add);
			//document.getElementById(id2).options[document.getElementById(id2).options.length - 1].selected = true;
			document.getElementById(id2).value=min;
		}
	}
}

function displayProcessing() {
	//	alert("1");
	document.getElementById('processing').style.visibility = 'visible';
}

function hiddenProcessing() {
	//	alert("2");
	document.getElementById('processing').style.visibility = 'hidden';
}

function displayLoading() {
	//	alert("3");
	document.getElementById('loading').style.visibility = 'visible';
}

function hiddenLoading() {
	//alert("4");	
	document.getElementById('loading').style.visibility = 'hidden';
}

function generateSearchURL( formName ){
	with( document.forms[ formName ] ){

		/*19.04.07

		if ( !county.value || county.value == "-" ){
		alert("Please, select one city.")
		return false;
		}
		*/

		//24/04/07
		var tradesmen = Array();
		tradesmen[0] = 'All';
		tradesmen[1] = 'Plumbers';
		tradesmen[2] = 'Electricians';
		tradesmen[3] = 'Builders';
		tradesmen[7] = 'Painters_&_Decorators';
		tradesmen[12] = 'Handyman_Firms';
		tradesmen[14] = 'Estate_Agents';
		tradesmen[15] = 'Interior_Designers';
		tradesmen[16] = 'Design_Suppliers';
		tradesmen[17] = 'Carpet_&_Floor_Fitters';
		tradesmen[18] = 'Tilers';
		tradesmen[19] = 'Carpet_Cleaners';
		tradesmen[20] = 'Roofers';
		tradesmen[23] = 'Domestic_Cleaners';
		tradesmen[24] = 'Glaziers';
		tradesmen[25] = 'Removal_Companies';
		tradesmen[26] = 'Chartered_Suveyors';
		tradesmen[27] = 'Property_Lawyers';
		tradesmen[28] = 'Kitchen_Fitters';
		tradesmen[30] = 'Skip_Hire';

		
		if ( area_name.value && !pcode.value ){
			alert(pcode.value);
			pcode.value = area_name.value;
		}
		
		if (!pcode.value){
			alert("Please, type in one city.")
			area_name.value="";
			area_name.focus();
			return false;
		}
		

		if (area_name.value) areaName = area_name.value;
		else areaName = '0';
		
		if (typeof(pcode) != "undefined" && pcode.value) postCode = pcode.value;
		else postCode = '0';
		/*
		if (typeof(beds) != "undefined" && beds.value) bedsN = beds.value;
		else bedsN = '0';
		if (typeof(baths) != "undefined" && baths.value) bathsN = baths.value;
		else bathsN = '0';
		*/
		if (typeof(trade) != "undefined") tradeN = trade.value;
		else tradeN = '0';
		if (typeof(distance) != "undefined") distanceN = distance.value;
		else distanceN = '0';
		
		url = '/tradesmen/';

		url += tradesmen[tradeN] + '-' + tradeN;
		
		/*if (to_rent[1].checked){
			url = '/properties/search';
			//url = '/search';
			if (period.value == 1) url += '-week'
			else url += '-month'
			url += '-rent';

			max = maxRent.value;
			min = minRent.value;
		} else {
			url = '/properties/search-buy';
			//url = '/search-buy';
			max = maxPrice.value;
			min = minPrice.value;
		}

		if (areas.value) {
			//23/04/07
			//areaCode = areas.value;
			areaCode = pcode.value;
			url += '-area'
		}
		else {
			areaCode = '0';
			url += '-city'
		}
		*/
		//alert(county.value);
		//url += '-'+ county.options[county.selectedIndex].text +'-'+ areaName + '-f_' + min;
		
		url += '-'+ areaName;
		//alert(url);
		//url += '-' + postCode +'-'+ areaName;
		//url += '-to_' + max + '-bedroom_' + bedsN + '-bathroom_' + bathsN;
		//url += '-dist_' + distanceN + '-pcode_' + postCode +'.html';
		url += '-' + distanceN + '-' + postCode +'.html';
		/*if (areas.value) {
			url += areaCode + '-'+ areaName +'.html';
		} else {
			url += county.value +'-'+ areaCode + '.html';
		}
		*/
		displayProcessing();
		window.location.href = url;
		//alert(url);

	}
	return false;

}

function selectArea (id,area,idAreaField, formName) {

	if (!id.checked) {
		areas = document.forms[formName].elements[idAreaField].value;
		replaced = areas.replace(area+", ","");
		document.forms[formName].elements[idAreaField].value = replaced;

	} else
	{
		areas = document.forms[formName].elements[idAreaField].value;
		if (areas.search(area) < 0) {
			areas = areas + area + ', ';
			document.forms[formName].elements[idAreaField].value = areas;
		}
	}
}

function new_window_map(pc) {
	var post_code = pc;
	msgWindow2=window.open("http://www.estatematcher.co.uk/map_popup.php?pc="+post_code,"displayWindow2","menubar=no,scrollbars=yes,width=550,height=400,top=130,left=30")
	return false;

}

function save_search( formName ) {
	with( document.forms[ formName ] ){

		if (!pcode.value){
			alert("Please, type in one area.")
			area_name.value="";
			area_name.focus();
			return false;
		} else if (!email.value){
			alert("Please, type in your e-mail address.")
			email.value="";
			email.focus();
			return false;
		}


		if (area_name.value) areaName = area_name.value;
		else areaName = '0';
		if (typeof(pcode) != "undefined" && pcode.value) postCode = pcode.value;
		else postCode = '0';
		if (typeof(beds) != "undefined" && beds.value) bedsN = beds.value;
		else bedsN = '0';
		if (typeof(distance) != "undefined") distanceN = distance.value;
		else distanceN = '0';

		url = '/properties/saveSearchPopUp.php?';

		if (to_rent[1].checked){
			//url = '/search';
			if (period.value == 1) url += 'rentper=W'
			else url += 'rentper=M'
			url += '&rent_sell=R';

			max = maxRent.value;
			min = minRent.value;
		} else {
			//url = '/search-buy';
			url += '&rent_sell=S';
			max = maxPrice.value;
			min = minPrice.value;
		}

		if (pcode.value) {
			url += '&pcode=' + pcode.value;
		}

		url += '&minPrice=' + min;
		url += '&maxPrice=' + max + '&bedrooms=' + bedsN + '&email=' + email.value;

		msgWindow2=window.open(url,"displayWindowSaveSearch","menubar=no,scrollbars=yes,width=550,height=400,top=130,left=30")

	}
	return false;

}

function show_row(element)
{
	if (document.getElementById(element)) document.getElementById(element).style.display='';
}


function hide_row(element) 
{
	if (document.getElementById(element)) document.getElementById(element).style.display='none';
}

function popUpPropertyImages(propertyId, photoNumber){
	window.open('/actions/viewPropertyImages.php?pid='+propertyId + "&photoNumber=" + photoNumber,'images', "resizable=1,menubar=no,scrollbars=no,width=500,height=300,top=130,left=30");
}

function popUp(propertyId){
	window.open('/actions/viewProperty.php?print=1&pid='+propertyId, 'print');
}

function popImageOriginal(imageURL,imageTitle){
	if (isNN){imgWin=window.open('about:blank','',optNN);}
	if (isIE){imgWin=window.open('about:blank','',optIE);}
	with (imgWin.document){
		writeln('<html><head><title>Loading...</title><style>body{margin:0px;}</style>');writeln('<sc'+'ript>');
		writeln('var isNN,isIE;');writeln('if (parseInt(navigator.appVersion.charAt(0))>=4){');
		writeln('isNN=(navigator.appName=="Netscape")?1:0;');writeln('isIE=(navigator.appName.indexOf("Microsoft")!=-1)?1:0;}');
		writeln('function reSizeToImage(){');
		//writeln('alert(document.images[0].width);');
		writeln('if (isIE){');
		writeln('window.resizeTo(100,100);');
		//writeln('alert(document.body.clientWidth);');
		//writeln('width=200-(document.body.clientWidth-document.images[0].width);');
		writeln('width=document.images[0].width + 10;');
		writeln('height=document.images[0].height + 35;');
		//writeln('height=100-(document.body.clientHeight-document.images[0].height);');
		//writeln('alert(width);');
		writeln('window.resizeTo(width,height);}');
		writeln('if (isNN){');
		writeln('window.innerWidth=document.images["George"].width;');
		writeln('window.innerHeight=document.images["George"].height;}}');
		writeln('function doTitle(){document.title="'+imageTitle+'";}');writeln('</sc'+'ript>');
		if (!AutoClose) writeln('</head><body bgcolor=000000 scroll="no" onload="reSizeToImage();doTitle();self.focus()">')
		else writeln('</head><body bgcolor=000000 scroll="no" onload="reSizeToImage();doTitle();self.focus()" onblur="self.close()">');
		writeln('<img name="George" src='+imageURL+' style="display:block"></body></html>');
		close();
	}}

	// this function is cutting the image! please do not use it!
	function popImageOriginal2(imageURL,imageTitle){

		var PositionX = 100;
		var PositionY = 100;

		// Set these value approximately 20 pixels greater than the
		// size of the largest image to be used (needed for Netscape)

		var defaultWidth  = 1000;
		var defaultHeight = 1000;

		// Set autoclose true to have the window close automatically
		// Set autoclose false to allow multiple popup windows

		var AutoClose = true;

		// Do not edit below this line...
		// ================================
		if (parseInt(navigator.appVersion.charAt(0))>=4){
			var isNN=(navigator.appName=="Netscape")?1:0;
			var isIE=(navigator.appName.indexOf("Microsoft")!=-1)?1:0;}
			var optNN='scrollbars=no,width='+defaultWidth+',height='+defaultHeight+',left='+PositionX+',top='+PositionY;
			var optIE='scrollbars=no,width=150,height=100,left='+PositionX+',top='+PositionY;

			if (isNN){imgWin=window.open('about:blank','',optNN);}
			if (isIE){imgWin=window.open('about:blank','',optIE);}
			with (imgWin.document){
				writeln('<html><head><title>Loading...</title><style>body{margin:0px;}</style>');writeln('<sc'+'ript>');
				writeln('var isNN,isIE;');writeln('if (parseInt(navigator.appVersion.charAt(0))>=4){');
				writeln('isNN=(navigator.appName=="Netscape")?1:0;');writeln('isIE=(navigator.appName.indexOf("Microsoft")!=-1)?1:0;}');
				writeln('function reSizeToImage(){');writeln('if (isIE){');writeln('window.resizeTo(100,100);');
				writeln('width=100-(document.body.clientWidth-document.images[0].width);');
				writeln('height=100-(document.body.clientHeight-document.images[0].height);');
				writeln('window.resizeTo(width,height);}');writeln('if (isNN){');
				writeln('window.innerWidth=document.images["George"].width;');writeln('window.innerHeight=document.images["George"].height;}}');
				writeln('function doTitle(){document.title="'+imageTitle+'";}');writeln('</sc'+'ript>');
				if (!AutoClose) writeln('</head><body bgcolor=000000 scroll="no" onload="reSizeToImage();doTitle();self.focus()">')
				else writeln('</head><body bgcolor=000000 scroll="no" onload="reSizeToImage();doTitle();self.focus()" onblur="self.close()">');
				writeln('<img name="George" src='+imageURL+' style="display:block"></body></html>');
				close();
			}}

			function confirmSubmit(txt)
			{
				if (!txt) txt = "Continue?";
				var agree=confirm(txt);
				if (agree)
				return true ;
				else
				return false ;
			}
