/**************************************************** AJAX */
function initiate() {
	// when the page loads, load Buying data
	$('chkBuy').checked = true;
	fillList('lstMinPrice',1,'first');
	fillList('lstMaxPrice',1,'last');
	Form.enable('quicksearch');	
}

function eventsConstructor() {
	// create some events to listen out for 
	$('chkBuy').onclick = function() {
		$('hiddenDBT').value = 1; // 1 = Buying
		fillList('lstMinPrice',1,'first');
		fillList('lstMaxPrice',1,'last');
	}
	
	$('chkLet').onclick = function() {
		$('hiddenDBT').value = 2; // 2 = Letting
		fillList('lstMinPrice',2,'first');
		fillList('lstMaxPrice',2,'last');		
	}
	
	$('btnSearch').onclick = function() {
		document.forms.quicksearch.submit();
	}
	
	$('btnListAll').onclick = function() {
		document.forms.listall.submit();
	}	
	
}

/*function fillList(target,type,selected) {	
	var url = "http://www.orchardsuk.com/ajax/buy-let-data.php";
	var pars = "type=" + type;
	var request = new Ajax.Request( url, { 
		method: 'get',
		parameters: pars,
		onLoading: function() {
			//Form.disable('quicksearch');
		},
		onComplete: function(requestObject) {
			clearSelect(target,selected);
			var responses = requestObject.responseXML.getElementsByTagName('price');
			//alert(responses.length);
			for (var i = 0; i < responses.length; i++) {
				value = responses[i].getElementsByTagName('value')[0].firstChild.nodeValue;
				title = responses[i].getElementsByTagName('title')[0].firstChild.nodeValue;
				newoption = document.createElement('option');
				newoption.innerHTML = title;
				newoption.value = value;
				$(target).appendChild(newoption);
				newoption = null;
			}
		}
	});

}*/

function fillList(target,type,selected) {	
	var url = "http://www.orchardsuk.com/ajax/buy-let-data.php?type="+type;
	var transport = new XMLHTTP()
	transport.onerror = function(description) {
		alert('Error: ' + description)
	}
	transport.onreadystatechange = function() {
		if(transport.readyState == 4) {
			clearSelect(target,selected);
			//alert(transport.responseXML.getElementsByTagName('price'));
			//alert(transport.responseText);
			var responses = transport.responseXML.getElementsByTagName('price');
			//alert(responses.length);
			for (var i = 0; i < responses.length; i++) {
				var value = responses[i].getElementsByTagName('value')[0].firstChild.nodeValue;
				var title = responses[i].getElementsByTagName('title')[0].firstChild.nodeValue;
				var newoption = document.createElement('option');
				newoption.innerHTML = title;
				newoption.value = value;
				$(target).appendChild(newoption);
				newoption = null;
			}
		}
	}
	transport.open('GET', url)
	transport.send()
	return false
}

function clearSelect(target,selected) {
	// clear a select box
	target = $(target);
	while (target.childNodes.length) {
		target.removeChild(target.firstChild);
	}
	
	if(selected == 'first') {
		newoption = document.createElement('option');
		newoption.value = "";
		newoption.innerHTML = "Any";
		target.appendChild(newoption);
	} else if(selected == 'last'){
		newoption = document.createElement('option');
		newoption.value = "99000000";
		newoption.innerHTML = "Any";
		target.appendChild(newoption);
	}	
}

function addLoadEvent(func) {
	// attach multiple onLoad events to the <body> tag
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			if (oldonload) {
				oldonload();
			}
			func();
		}
	}
}

addLoadEvent(function() {
	initiate(); // load default values
	eventsConstructor(); // create event listeners
});

/*********************************************** MISC Functions for image popups, etc */

function getQueryVariable(variable) {
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) {
      return pair[1];
    }
  } 
  //alert('Query Variable ' + variable + ' not found');
}

var detect = navigator.userAgent.toLowerCase();
var OS,browser,version,total,thestring;

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i>d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_showHideLayers() { //v3.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v='hide')?'hidden':v; }
    obj.visibility=v; }
}
function doLayers(num) {
	for(i=0;i<20;i++){
		MM_showHideLayers('layer'+num,'','show');
		MM_showHideLayers('layer'+i,'','hide');
	}
}

/*function doSearch() {	
	if(navigator.appName.indexOf("Netscape")>(-1)){
			f = document.forms.quicksearch;
			minprice = f.minprice.options[f.minprice.selectedIndex].value;
			maxprice = f.maxprice.options[f.maxprice.selectedIndex].value;
			numberofbedrooms = f.minimumbedrooms.options[f.minimumbedrooms.selectedIndex].value;
			//popup the search results
			window.open("http://www.vebra.com/home/solex/search.asp?slo=undefined&fid=11334&bid=0&dbt=1&nre=1&thu=&vto=&ord=&nba=&bed=" + numberofbedrooms + "&lop=" + minprice + "&hip=" + maxprice, "results", "height=575, width=705, status=false, resizable=false, toolbar=false, scrollbars=yes");

	} else { 
		 document.forms.quicksearch.submit(); 
	}
}*/

function doSearch(f) {	
	LeftPosition = (screen.width) ? (screen.width-700)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-600)/2 : 0;
	if(checkBrowser('netscape')){
			minprice = f.minprice.options[f.minprice.selectedIndex].value;
			maxprice = f.maxprice.options[f.maxprice.selectedIndex].value;
			numberofbedrooms = f.minimumbedrooms.options[f.minimumbedrooms.selectedIndex].value;
			//popup the search results
			window.open("http://www.vebra.com/home/solex/search.asp?slo=undefined&fid=11334&bid=0&dbt=1&nre=1&thu=&vto=&ord=&nba=&bed=" + numberofbedrooms + "&lop=" + minprice + "&hip=" + maxprice, "results", "height=600, width=700, status=false, resizable=false, toolbar=false, scrollbars=yes,left="+LeftPosition+",top="+TopPosition);
	} else if(checkBrowser('firefox')){
			minprice = f.minprice.options[f.minprice.selectedIndex].value;
			maxprice = f.maxprice.options[f.maxprice.selectedIndex].value;
			numberofbedrooms = f.minimumbedrooms.options[f.minimumbedrooms.selectedIndex].value;
			//popup the search results
			window.open("http://www.vebra.com/home/solex/search.asp?slo=undefined&fid=11334&bid=0&dbt=1&nre=1&thu=&vto=&ord=&nba=&bed=" + numberofbedrooms + "&lop=" + minprice + "&hip=" + maxprice, "results", "height=600, width=700, status=false, resizable=false, toolbar=false, scrollbars=yes,left="+LeftPosition+",top="+TopPosition);

	} else { 
		 f.submit(); 
	}
}

function checkBrowser(string) {
	place = detect.indexOf(string) + 1;
	thestring = string;
	return place;
}

function openPictureWindow_Fever(imageType,imageName,imageWidth,imageHeight,alt,posLeft,posTop) {  // v4.01
	newWindow = window.open("","newWindow","width="+imageWidth+",height="+imageHeight+",scrollbars=no,left="+posLeft+",top="+posTop);
	newWindow.document.open();
	newWindow.document.write('<html><title>'+alt+'</title><body bgcolor="#FFFFFF" leftmargin="0" topmargin="0" marginheight="0" marginwidth="0" onBlur="self.close()">'); 
	if (imageType == "swf"){
	newWindow.document.write('<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,0,0\" width=\"'+imageWidth+'\" height=\"'+imageHeight+'\">');
	newWindow.document.write('<param name=movie value=\"'+imageName+'\"><param name=quality value=high>');
	newWindow.document.write('<embed src=\"'+imageName+'\" quality=high pluginspage=\"http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash\" type=\"application/x-shockwave-flash\" width=\"'+imageWidth+'\" height=\"'+imageHeight+'\">');
	newWindow.document.write('</embed></object>');	}else{
	newWindow.document.write('<img src=\"'+imageName+'\" width='+imageWidth+' height='+imageHeight+' alt=\"'+alt+'\">'); 	}
	newWindow.document.write('</body></html>');
	newWindow.document.close();
	newWindow.focus();
}

function MM_openBrWindow(theURL,winName,features, myWidth, myHeight, isCenter) { //v3.0
  if(window.screen)if(isCenter)if(isCenter=="true"){
    var myLeft = (screen.width-myWidth)/2;
    var myTop = (screen.height-myHeight)/2;
    features+=(features!='')?',':'';
    features+=',left='+myLeft+',top='+myTop;
  }
  window.open(theURL,winName,features+((features!='')?',':'')+'width='+myWidth+',height='+myHeight);
}

function MM_callJS(jsStr) { //v2.0
  return eval(jsStr)
}

function YY_checkform() { //v4.71
//copyright (c)1998,2002 Yaromat.com
  var a=YY_checkform.arguments,oo=true,v='',s='',err=false,r,o,at,o1,t,i,j,ma,rx,cd,cm,cy,dte,at;
  for (i=1; i<a.length;i=i+4){
    if (a[i+1].charAt(0)=='#'){r=true; a[i+1]=a[i+1].substring(1);}else{r=false}
    o=MM_findObj(a[i].replace(/\[\d+\]/ig,""));
    o1=MM_findObj(a[i+1].replace(/\[\d+\]/ig,""));
    v=o.value;t=a[i+2];
    if (o.type=='text'||o.type=='password'||o.type=='hidden'){
      if (r&&v.length==0){err=true}
      if (v.length>0)
      if (t==1){ //fromto
        ma=a[i+1].split('_');if(isNaN(v)||v<ma[0]/1||v > ma[1]/1){err=true}
      } else if (t==2){
        rx=new RegExp("^[\\w\.=-]+@[\\w\\.-]+\\.[a-zA-Z]{2,4}$");if(!rx.test(v))err=true;
      } else if (t==3){ // date
        ma=a[i+1].split("#");at=v.match(ma[0]);
        if(at){
          cd=(at[ma[1]])?at[ma[1]]:1;cm=at[ma[2]]-1;cy=at[ma[3]];
          dte=new Date(cy,cm,cd);
          if(dte.getFullYear()!=cy||dte.getDate()!=cd||dte.getMonth()!=cm){err=true};
        }else{err=true}
      } else if (t==4){ // time
        ma=a[i+1].split("#");at=v.match(ma[0]);if(!at){err=true}
      } else if (t==5){ // check this 2
            if(o1.length)o1=o1[a[i+1].replace(/(.*\[)|(\].*)/ig,"")];
            if(!o1.checked){err=true}
      } else if (t==6){ // the same
            if(v!=MM_findObj(a[i+1]).value){err=true}
      }
    } else
    if (!o.type&&o.length>0&&o[0].type=='radio'){
          at = a[i].match(/(.*)\[(\d+)\].*/i);
          o2=(o.length>1)?o[at[2]]:o;
      if (t==1&&o2&&o2.checked&&o1&&o1.value.length/1==0){err=true}
      if (t==2){
        oo=false;
        for(j=0;j<o.length;j++){oo=oo||o[j].checked}
        if(!oo){s+='* '+a[i+3]+'\n'}
      }
    } else if (o.type=='checkbox'){
      if((t==1&&o.checked==false)||(t==2&&o.checked&&o1&&o1.value.length/1==0)){err=true}
    } else if (o.type=='select-one'||o.type=='select-multiple'){
      if(t==1&&o.selectedIndex/1==0){err=true}
    }else if (o.type=='textarea'){
      if(v.length<a[i+1]){err=true}
    }
    if (err){s+='* '+a[i+3]+'\n'; err=false}
  }
  if (s!=''){alert('The required information is incomplete or contains errors:\t\t\t\t\t\n\n'+s)}
  document.MM_returnValue = (s=='');
}