var http = false;

if(navigator.appName == "Microsoft Internet Explorer") {
	http = new ActiveXObject("Microsoft.XMLHTTP");
} else {
	http = new XMLHttpRequest();
}

function getStringFromPhpId (idType, id) {
	http.abort();
	var ajxText;
	http.open("GET", "http://www.carmatcher.co.uk/getString.php?id="+id+"&type="+idType);

	http.onreadystatechange=function() {

		if(http.readyState == 4) {
			if (idType == 'getMake') {
				//alert("1");
				document.getElementById('make2').innerHTML =(http.responseText);
			}

			else {
				//alert("2");
				document.getElementById('model2').innerHTML =(http.responseText);
			}
		}
	}

	http.send(null);

}




function getAjxModels(manufacter, required, defaultValue, destinationId) {

	if ( manufacter ){

		var targetId;
		if (typeof(destinationId)!="undefined") targetId = destinationId;
		else targetId = 'modelsId';


		var addToUrl;
		if (required == 1) addToUrl = '&required=1';
		else addToUrl = '';

		if (typeof(defaultValue)!="undefined" || defaultValue != '') addToUrl += '&defaultValue=' + defaultValue;
		else addToUrl += '';

		http.abort();

		http.open("GET", "http://www.carmatcher.co.uk/getmodels.php?manufacter=" + manufacter + addToUrl, true);
		http.onreadystatechange=function() {
			if(http.readyState == 4) {
				document.getElementById(targetId).innerHTML = http.responseText;
			}
		}
		http.send(null);
	}
}



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

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 ].elements ){

		/*19.04.07

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

		//24/04/07
		if (typeof(pcode) != "undefined" && pcode.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(distance) != "undefined") distanceN = distance.value;
		else distanceN = '0';

		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'
		}


		//url += '-'+ county.options[county.selectedIndex].text +'-'+ areaName + '-f_' + min;
		//url += '-'+ county.value +'-'+ areaName + '-f_' + min;
		url += '-' + postCode +'-'+ areaName + '-f_' + min;
		url += '-to_' + max + '-bedroom_' + bedsN + '-bathroom_' + bathsN;
		url += '-dist_' + distanceN + '-pcode_' + postCode +'-';

		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 selectModel (id,model,idAreaField, formName) {

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

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



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 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 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 + 75;');
		//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  = 3000;
		var defaultHeight = 3000;

		// 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();
			}}






