
//detect flash
var flashversion = 0;
if (navigator.plugins && navigator.mimeTypes.length) {
	var x = navigator.plugins["Shockwave Flash"];
	if(x && x.description) {
		var y = x.description;
		flashversion = parseInt(y.substr(y.indexOf(".") - 2, 2), 10);
	}
} else {
	result = false;
	for(var i = 30; i >= 3 && result != true; i--){
		execScript('on error resume next: result = IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.'+i+'"))','VBScript');
		flashversion = i;
	}
}

if (flashversion >= 5) {
	// remove flicker of non flash
    document.getElementsByTagName('html')[0].className = 'flashInstalled';

}




function getFlash(what, movie, query, width, height) {
	
	new_txt = '<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="'+width+'" height="'+height+'">\n';
	new_txt += '<param name="movie" value="'+movie+query+'">\n';
	new_txt += '<param name="quality" value="high" />\n';
	new_txt += '<param name="menu" value="false">\n';
	new_txt += '<param name="wmode" value="transparent">\n';
	new_txt += '<param name="scale" value="noscale" />\n';
	new_txt += '<param name="salign" value="lt" />\n';
	new_txt += '<embed src="'+movie+query+'" width="'+width+'" height="'+height+'" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" menu="false" scale="noscale" salign="lt" wmode="transparent"></embed>\n';
	new_txt += '</object>';
	what.innerHTML = new_txt;
	what.style.visibility= 'visible';

}

function flashRewrite(x, movie, query, type){

// x: the element, movie: swf address, query: added info to send, type: 1 = TagName  0 = id	
	if (flashversion >= 5){
		
	if (type == '1'){
	
	xArea = x.split('>'); //if TagName in a id
	if (xArea[1]){
		
	var x = document.getElementById(xArea[0]).getElementsByTagName(xArea[1]);	
	} else {	
	var x = document.getElementsByTagName(x);	
	}
	
	for (var i=0;i<x.length;i++){
	
		width = x[i].offsetWidth;
		height = x[i].offsetHeight;
		title = x[i].innerHTML;
		if (title) {title = title.replace("&", "%26")}
		
			if (!query){
				q= "?title="+ title;
			}else{
				q= query +"&title="+ title;
			}
			

			getFlash(x[i], movie, q, width, height)
			q = '';
	}
	} else {
	
	var x = document.getElementById(x);
	
		width = x.offsetWidth;
		height = x.offsetHeight;
		getFlash(x, movie, query, width, height)
	}
}	
}


function openWindow(){
	var x = document.getElementsByTagName('a');
	for (var i=0;i<x.length;i++)
	{
	if (x[i].getAttribute('title') >'') {
		
		if (x[i].getAttribute('title').indexOf("Link opens in a new window") >= 0)
		{
		x[i].onclick = function () {window.open(this.href); return false}
		}	
	}	
	}
}

function formStyle(){
	var formRoot = document.getElementsByTagName("form"); 
	for (var f=0;f<formRoot.length;f++)
	{
		var r = formRoot[f].elements;
		var rlen = r.length;

		for (var h = 0; h < rlen; h++) {
		 var node = r[h];
				
					node.onfocus=function() {
					if(this.type !=='checkbox' && this.type !=='radio' && (this.tagName =='INPUT' || this.tagName =='TEXTAREA')) {
							this.className="textformfocus";
							if (this.value.charAt(0) == '-') { this.value='' } 
					}
			}
												
					node.onblur=function() {
					if(this.type !=='checkbox' && this.type !=='radio' && (this.tagName =='INPUT' || this.tagName =='TEXTAREA')) {
							this.className="textform";
					}
			}
			
		}
	}
}	

function boxLinks(){
		
		allNodes = $A(document.getElementsByClassName("rbox")); 
		allNodes.each(function(node, i) {
		allNodes[i].onclick = function() {
			if (this.id !='nolink') {	window.location = this.getElementsByTagName("a")[0].href };
			}
		});
		
}


function addEvent( obj, type, fn )
{
	if (obj.addEventListener)
		obj.addEventListener( type, fn, false );
	else if (obj.attachEvent)
	{
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
	}
}

function cssswitch() {
	
	
//load cookie for css switcher
	var cookie = readCookie("style");
	var title = cookie ? cookie : getPreferredStyleSheet();
	setActiveStyleSheet(title);
	
	//show correct size on navi
	if (cookie){
		cookiestr = cookie.toString();
			if (document.getElementById(cookiestr)){
			document.getElementById(cookiestr).className="on";
			}else{
			document.getElementById('Default').className="on";
			}
	}else{
	document.getElementById('Default').className="on";
	}
	
	//navi
	atopnaviRoot = document.getElementById("textchange");
		
		for (i=0; i<atopnaviRoot.childNodes.length; i++) {
			node = atopnaviRoot.childNodes[i];
			
				if (node.nodeName=="LI") {
				node.onclick=function() {
					
					for (j=0; j<atopnaviRoot.childNodes.length; j++) {
					atopnaviRoot.childNodes[j].className="";
					}
					this.className="on";
					setActiveStyleSheet(this.id); return false;
				}
			}
		}
		
	
	}
	

function showhide() {
	
	//contact office
	if (document.getElementById("contact")) {
		topnaviRoot = document.getElementById("contact");
			
			for (i=0; i<topnaviRoot.childNodes.length; i++) {
				node = topnaviRoot.childNodes[i];
				
				
					if (node.nodeName=="LI") {
					node.onclick=function() {
						
						for (j=0; j<topnaviRoot.childNodes.length; j++) {
						topnaviRoot.childNodes[j].className="";
						}
						
						this.className+=" over";
						if (this.childNodes[0].getAttribute('title')!='Back to Profile'){this.childNodes[0].href='#'};
						//return false;
					}
				}
			}
	}
	
	
	}	
	
function BuyOrRent() {
	if (document.getElementById("saletype")) {
		if (document.getElementById("saletype").value=='') {
			document.getElementById("areacode").disabled=true;
			document.getElementById("minprice").disabled=true;
			document.getElementById("maxprice").disabled=true;
			document.getElementById("letminprice").disabled=true;
			document.getElementById("letmaxprice").disabled=true;
			document.getElementById("bedrooms").disabled=true;
			document.getElementById("office").disabled=true;
		} else {
			document.getElementById("areacode").disabled=false;
			document.getElementById("minprice").disabled=false;
			document.getElementById("maxprice").disabled=false;
			document.getElementById("letminprice").disabled=false;
			document.getElementById("letmaxprice").disabled=false;
			document.getElementById("bedrooms").disabled=false;
			document.getElementById("office").disabled=false;
		}
		
	}
	Prices();
}

function Prices() {

		
	if (document.getElementById("saletype").value=='buy') {
		document.getElementById("letprices").style.display='none'; 
		document.getElementById("buyprices").style.display='block';
	}
	if (document.getElementById("saletype").value=='rent') {
		document.getElementById("letprices").style.display='block';
		document.getElementById("buyprices").style.display='none';
	}


}

function showimg() {
	if (document.getElementById("thumbnails") && (document.getElementById("property-image"))) {
		var theroot = document.getElementById("thumbnails");
			for (i=0; i<theroot.childNodes.length; i++) {
				node = theroot.childNodes[i];
  					if (node.nodeName=="LI") {
						node.onclick=function() {
						document.getElementById("property-image").src = this.childNodes[0].href;
						return false;
					}
						
				} 

			} 

	  } 
} 
	
function printProperty() {
	if (document.getElementById("p1")) {
document.getElementById("p1").onclick = function() {window.print();}	
	}
}

function e_type() {
	if (document.getElementById("property")) {
		switch(document.getElementById("property").value) {
			case "res":
				document.getElementById("e-res").style.display = 'block';
				document.getElementById("e-com").style.display = 'none';
			break;
			case "com":
				document.getElementById("e-com").style.display = 'block';
				document.getElementById("e-res").style.display = 'none';
			break;
			default:
				document.getElementById("e-res").style.display = 'block';
				document.getElementById("e-com").style.display = 'none';
		}
	 }
}

//addEvent(window, 'load', showhide);
addEvent(window, 'load', cssswitch);
addEvent(window, 'load', openWindow);
addEvent(window, 'load', formStyle);
addEvent(window, 'load', BuyOrRent); 
addEvent(window, 'load', showimg);
addEvent(window, 'load', printProperty);
addEvent(window, 'load', e_type);
addEvent(window, 'load', boxLinks);


addEvent(window, 'load', function() {
								 
	flashRewrite('h1','../images/layup/title.swf', '', '1')
	flashRewrite('leftside>h3','../images/layup/title2.swf', '', '1')
	if (document.getElementById("flatop")) flashRewrite('flatop','../images/layup/topimgs.swf', '', '0')
	
});

function addMarker(theicon, map, point, html) {

 var marker = new GMarker(point,theicon); 

 map.addOverlay(marker);
 GEvent.addListener(marker, "click", function() {
   
 
   marker.openInfoWindowHtml(html);


 });

} 

function resetMap() {
   map.centerAndZoom(new GPoint(-0.157883, 51.45043), 3);

} 

function checksubmit() {
	if (document.getElementById("searchtype").value=='') {
		alert('Please select the type of property (residential or commercial)');
		return false;
	} 
	if (document.getElementById("saletype").value=='') {
		alert('Please select whether to buy or rent');
		return false;
	} 
}

//mortage calculator functions
function IsNumeric(strString)
   //  check for valid numeric strings	
   {
   var strValidChars = "0123456789.-";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   return blnResult;
   }
   
function roundit(Num, Places) {
   if (Places > 0) {
      if ((Num.toString().length - Num.toString().lastIndexOf('.')) > (Places + 1)) {
         var Rounder = Math.pow(10, Places);
         return Math.round(Num * Rounder) / Rounder;
      }
      else return Num;
   }
   else return Math.round(Num);
}

function trim(str) { 
	 return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
} 
	function mortgage_calculator() {
		var a=document.getElementById("amount").value;
		var p=document.getElementById("period").value;
		var r=document.getElementById("rate").value;
		
		document.getElementById("calc0").style.display='block';
		document.getElementById("calc1").style.display='block';
		document.getElementById("calc2").style.display='block';
		document.getElementById("calc3").style.display='block';
		
		if (a.indexOf('£') >= 0) a = a.replace('£','');
		if (a.indexOf(',') >= 0) a = a.replace(',','');
		if (r.indexOf('%') > 0) r = r.replace('%','');
		if (p.indexOf('yrs') > 0) p = p.replace('yrs','');
		if (p.indexOf('ys') > 0) p = p.replace('ys','');
		if (p.indexOf('years') > 0) p = p.replace('years','');
		if (p.indexOf('year') > 0) p = p.replace('year','');
		if (p.indexOf('yers') > 0) p = p.replace('yers',''); //allow for a few spelling mistakes
		if (p.indexOf('yars') > 0) p = p.replace('yars','');
		p = trim(p);
		//allow for months
			var ismonth='';
			if (p.indexOf('months') > 0) {
				p = p.replace('months',''); 
				ismonth='1';
			}
			//allow for a few spelling mistakes
			if (p.indexOf('mnths') > 0) {
				p = p.replace('mnths',''); 
				ismonth='1'; 
			}
			if (p.indexOf('moths') > 0){
				p = p.replace('moths','');  
				ismonth='1';
			}
			if (p.indexOf('mths') > 0) {
				p = p.replace('mths','');  
				ismonth='1';
			}
			if (p.indexOf('mth') > 0){
				p = p.replace('mth','');  
				ismonth='1';
			}
			if (p.indexOf('mnth') > 0) {
				p = p.replace('mnth','');  
				ismonth='1';
			}
			if (ismonth =='1')  p = (p / 12);
			
		if ((IsNumeric(a)) && (IsNumeric(p)) && (IsNumeric(r))) {
		r = r / 100;
		var v = ((a*r)/12) * (1/(1-(Math.pow(1/(1+r),p))));
		document.getElementById("monthly").innerHTML = "£" + roundit(v,0);
		v = (a*r)/12;
		document.getElementById("interest").innerHTML = "£" + roundit(v,0);
		} else {
			alert('We are sorry but you seem to have entered some invalid data.  Please try again. \n');
		}
	}
	
	function resetcalc() {
		document.getElementById("amount").value='-Enter Amount-';
		document.getElementById("period").value='-Enter Years-';
		document.getElementById("rate").value='-Enter Rate-';
		document.getElementById("monthly").innerHTML = '';
		document.getElementById("interest").innerHTML = '';
	}
//		
function hideDrop(hideid) { 
 document.getElementById(hideid).style.display = 'none';
reshow();
} 
var myloc = window.location.href;
var locarray = myloc.split("/");
delete locarray[(locarray.length-1)];
var currentDir = locarray.join("/");

function reshow() {
	 if (document.getElementById("saletype")) {
 document.getElementById("saletype").style.visibility='visible';
 	document.getElementById("areacode").style.visibility='visible';
	document.getElementById("minprice").style.visibility='visible';
	document.getElementById("letminprice").style.visibility='visible';
		var theroot = document.getElementById('saletype');
		if (theroot.options[theroot.selectedIndex].value=='') document.getElementById("bedrooms").style.visibility='visible';
 }
}
function showDrop(theid) {
	thediv = document.getElementById(theid);

	if (thediv.style.display =='none'){
			thediv.style.display = 'block';
			setTimeout("hideDrop('"+theid+"')",10000); 
	}else{
			setTimeout("hideDrop('"+theid+"')",1);
	}
	/*if (document.getElementById("saletype") && (currentDir.indexOf('pages')>0)) {
	document.getElementById("saletype").style.visibility='hidden';
	document.getElementById("areacode").style.visibility='hidden';
	//document.getElementById("minprice").style.visibility='hidden';
	//document.getElementById("letminprice").style.visibility='hidden';
			var theroot = document.getElementById('saletype');
		if (theroot.options[theroot.selectedIndex].value=='') document.getElementById("bedrooms").style.visibility='hidden';
	}*/
}
function setForm(thevalue, thename) {
		document.getElementById('cOffice').value= thevalue;
		document.getElementById('drop').style.display = 'none';
		document.getElementById('droptext').innerHTML = trunc(thename,16);
		reshow();
}
var punctuation = '.,;!? ';
    function trunc(s,size){
      if(!size)size=30;
      if(s.length<=size)return s;

      var p=-1;

      for(var i=0;i<size;i++)
        if(punctuation.indexOf(s.charAt(i))!=-1)p=i;

      if(p==-1)p=size-1;

      return s.substr(0,p)+'...';
    }
	
	function podwin(thisurl){
	
		var win2=window.open(thisurl, '', 'scrollbars=no, status=yes, left=0, top=0, width=600, height=500')
		win2.blur();

	}
