// JavaScript Document
///
function lookupCity (sel, fld) {
	var forcematch = true;
	var found = false;
	for (var i = 0; i < sel.options.length; i++) {
		if (sel.options[i].value.toUpperCase().indexOf(fld.value.toUpperCase()) == 0) {
			found=true; break;
		}
	}
	if (found) { sel.selectedIndex = i; }
	else { sel.selectedIndex = -1; }
	
	if (fld.createTextRange) {
		if (forcematch && !found) {
			fld.value=fld.value.substring(0,fld.value.length-1); 
			return;
		}
		var cursorKeys ="8;46;37;38;39;40;33;34;35;36;45;";
		if (cursorKeys.indexOf(event.keyCode+";") == -1) {
			var r1 = fld.createTextRange();
			var oldValue = r1.text;
			var newValue = found ? sel.options[i].value : oldValue;
			if (newValue != fld.value) {
				fld.value = newValue;
				var rNew = fld.createTextRange();
				rNew.moveStart('character', oldValue.length) ;
				rNew.select();
			}
		}
	}
	
}

// Opens a new window to view the images related to a property
function imageWin(pid,pic){
	var options = "toolbar=no"
	newWin = window.open("/viewpics.asp?pid="+pid+"&pic="+pic, "ImageWindow", options);
}

// Shows a detail view of a property
function dispListing(pid){
	location.href = "/showproperty.asp?action=qs&pid=" + pid;
}

// Makes a mailto: link
function nospamMailto(usr,host,lnk){
	var linktext;
	linktext = (!lnk) ? usr + "@" + host : lnk;
	document.write("<a href=" + "mail" + "to:" + usr + "@" + host + ">" + linktext + "</a>");
}

// Opens a new window to allow to send property references to a friend
function emailFriend(emlbanner, pid){
	GP_AdvOpenWindow("/email_friend.asp?emlbanner="+emlbanner+"&pid="+pid,"EmailFriend","fullscreen=no,toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no",650,500,"center","ignoreLink","",0,"");
}

// Opens a new window to allow to send property references to a friend
function emailAgent(emlbanner, pid, fld, aid){
	MM_openBrWindow("/email_agent.asp?emlbanner=" + emlbanner + "&aid=" + aid + "&pid=" + pid + "&fld=" + fld,"EmailAgent","scrollbars=no,resizable=no,width=650,height=500");
}

// Checks a field to see if it is a properly formatted date
function checkDate(fld) {
	if (fld.value != "") {
		if (!isDate(fld)) {
			fld.focus();
			fld.select();
			return false;
		}
	}
	return true;
}

// Converts an input string to a date value
function stringToDate(inputStr){
	while (inputStr.indexOf("-") != -1){inputStr = replaceString(inputStr,"-","/")}
	var delim1 = inputStr.indexOf("/");
	var delim2 = inputStr.lastIndexOf("/");
	var dd = parseInt(inputStr.substring(0, delim1), 10);
	var mm = parseInt(inputStr.substring(delim1+1, delim2), 10);
	var yyyy = parseInt(inputStr.substring(delim2+1, inputStr.length), 10);
	var dt = new Date(yyyy, mm-1, dd);
	return (dt);
}

// Unknown??
function tellme(html){
  tellmetext.innerHTML=html
}

// Opens a new window with advanced featurs
function GP_AdvOpenWindow(theURL,winName,features,popWidth,popHeight,winAlign,ignorelink,alwaysOnTop,autoCloseTime,borderless) { //v2.0
  var leftPos=0,topPos=0,autoCloseTimeoutHandle, ontopIntervalHandle, w = 650, h = 500;  
  if (popWidth > 0) features += (features.length > 0 ? ',' : '') + 'width=' + popWidth;
  if (popHeight > 0) features += (features.length > 0 ? ',' : '') + 'height=' + popHeight;
  if (winAlign && winAlign != "" && popWidth > 0 && popHeight > 0) {
    if (document.all || document.layers || document.getElementById) {w = screen.availWidth; h = screen.availHeight;}
		if (winAlign.indexOf("center") != -1) {topPos = (h-popHeight)/2;leftPos = (w-popWidth)/2;}
		if (winAlign.indexOf("bottom") != -1) topPos = h-popHeight; if (winAlign.indexOf("right") != -1) leftPos = w-popWidth; 
		if (winAlign.indexOf("left") != -1) leftPos = 0; if (winAlign.indexOf("top") != -1) topPos = 0; 						
    features += (features.length > 0 ? ',' : '') + 'top=' + topPos+',left='+leftPos;}
  if (document.all && borderless && borderless != "" && features.indexOf("fullscreen") != -1) features+=",fullscreen=1";
  if (window["popupWindow"] == null) window["popupWindow"] = new Array();
  var wp = popupWindow.length;
  popupWindow[wp] = window.open(theURL,winName,features);
  if (popupWindow[wp].opener == null) popupWindow[wp].opener = self;  
  if (document.all || document.layers || document.getElementById) {
    if (borderless && borderless != "") {popupWindow[wp].resizeTo(popWidth,popHeight); popupWindow[wp].moveTo(leftPos, topPos);}
    if (alwaysOnTop && alwaysOnTop != "") {
    	ontopIntervalHandle = popupWindow[wp].setInterval("window.focus();", 50);
    	popupWindow[wp].document.body.onload = function() {window.setInterval("window.focus();", 50);}; }
    if (autoCloseTime && autoCloseTime > 0) {
    	popupWindow[wp].document.body.onbeforeunload = function() {
  			if (autoCloseTimeoutHandle) window.clearInterval(autoCloseTimeoutHandle);
    		window.onbeforeunload = null;	}  
   		autoCloseTimeoutHandle = window.setTimeout("popupWindow["+wp+"].close()", autoCloseTime * 1000); }
  	window.onbeforeunload = function() {for (var i=0;i<popupWindow.length;i++) popupWindow[i].close();}; }   
  document.MM_returnValue = (ignorelink && ignorelink != "") ? false : true;
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

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() { //v6.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 YY_checkform() { //v4.66
//copyright (c)1998,2002 Yaromat.com
  var args = YY_checkform.arguments; var myDot=true; var myV=''; var myErr='';var addErr=false;var myReq;
  for (var i=1; i<args.length;i=i+4){
    if (args[i+1].charAt(0)=='#'){myReq=true; args[i+1]=args[i+1].substring(1);}else{myReq=false}
    var myObj = MM_findObj(args[i].replace(/\[\d+\]/ig,""));
    myV=myObj.value;
    if (myObj.type=='text'||myObj.type=='password'||myObj.type=='hidden'){
      if (myReq&&myObj.value.length==0){addErr=true}
      if ((myV.length>0)&&(args[i+2]==1)){ //fromto
        var myMa=args[i+1].split('_');if(isNaN(myV)||myV<myMa[0]/1||myV > myMa[1]/1){addErr=true}
      } else if ((myV.length>0)&&(args[i+2]==2)){
          var rx=new RegExp("^[\\w\.=-]+@[\\w\\.-]+\\.[a-z]{2,4}$");if(!rx.test(myV))addErr=true;
      } else if ((myV.length>0)&&(args[i+2]==3)){ // date
         myMa=args[i+1].split("#"); var myAt=myV.match(myMa[0]);
        if(myAt){
          var myD=(myAt[myMa[1]])?myAt[myMa[1]]:1; var myM=myAt[myMa[2]]-1; var myY=myAt[myMa[3]];
          var myDate=new Date(myY,myM,myD);
          if(myDate.getFullYear()!=myY||myDate.getDate()!=myD||myDate.getMonth()!=myM){addErr=true};
        }else{addErr=true}
      } else if ((myV.length>0)&&(args[i+2]==4)){ // time
         myMa=args[i+1].split("#");  myAt=myV.match(myMa[0]);if(!myAt){addErr=true}
      } else if (myV.length>0&&args[i+2]==5){ // check this 2
            var myObj1 = MM_findObj(args[i+1].replace(/\[\d+\]/ig,""));
            if(myObj1.length)myObj1=myObj1[args[i+1].replace(/(.*\[)|(\].*)/ig,"")];
            if(!myObj1.checked){addErr=true}
      } else if (myV.length>0&&args[i+2]==6){ // the same
             myObj1 = MM_findObj(args[i+1]);
            if(myV!=myObj1.value){addErr=true}
      }
    } else
    if (!myObj.type&&myObj.length>0&&myObj[0].type=='radio'){
          var myTest = args[i].match(/(.*)\[(\d+)\].*/i);
           myObj1=(myObj.length>1)?myObj[myTest[2]]:myObj;
      if (args[i+2]==1&&myObj1&&myObj1.checked&&MM_findObj(args[i+1]).value.length/1==0){addErr=true}
      if (args[i+2]==2){
         myDot=false;
        for(var j=0;j<myObj.length;j++){myDot=myDot||myObj[j].checked}
        if(!myDot){myErr+='* ' +args[i+3]+'\n'}
      }
    } else if (myObj.type=='checkbox'){
      if(args[i+2]==1&&myObj.checked==false){addErr=true}
      if(args[i+2]==2&&myObj.checked&&MM_findObj(args[i+1]).value.length/1==0){addErr=true}
    } else if (myObj.type=='select-one'||myObj.type=='select-multiple'){
      if(args[i+2]==1&&myObj.selectedIndex/1==0){addErr=true}
    }else if (myObj.type=='textarea'){
      if(myV.length<args[i+1]){addErr=true}
    }
    if (addErr){myErr+='* '+args[i+3]+'\n'; addErr=false}
  }
  if (myErr!=''){alert('The required information is incomplete or contains errors:\t\t\t\t\t\n\n'+myErr)}
  document.MM_returnValue = (myErr=='');
}

function quickSearch(f){
	var sval = f.searchVal.value;
	if (sval == "") {
		return false;
	}
	if (isPosInteger(sval)){
		if (sval.length == 4){
			f.postcode.value = sval;
			return true;
		} else {
			f.pid.value = sval;
			f.idnum.value = sval;
			f.action = "showproperty.asp";
			return true;
		}
	} else {
		f.city.value = sval;
		return true;
	}
	return false;
}

function isPosInteger(inputVal) {
	inputStr = inputVal.toString()
	for (var i = 0; i < inputStr.length; i++) {
		var oneChar = inputStr.charAt(i)
		if (oneChar < "0" || oneChar > "9") {
			return false
		}
	}
	return true
}

function viewImageWin(img,w,h){
	w += 20; h += 50
	var options = "width=" + w +",height=" + h;
	newWin = window.open("", "ImageWindow", options);
	newWin.document.write("<html><head><title><\/title><\/head><body bgcolor=#000000 alink='#ffffff' vlink='#ffffff' link='#ffffff'>");
	newWin.document.write("<div align='center'><img src='" + img + "' border=0><p>");
	newWin.document.write("<a href='javascript:window.close()'><font face='Verdana,Arial,Helvetica,sans serif' size='1'>close window<\/font><\/a>");
	newWin.document.write("<\/div><\/body><\/html>");
	newWin.document.close();
}

// check the entered month for too high a value
function checkMonthLength(mm,dd) {
	var months = new Array("","January","February","March","April","May","June","July","August","September","October","November","December")
	if ((mm == 4 || mm == 6 || mm == 9 || mm == 11) && dd > 30) {
		alert(months[mm] + " has only 30 days.")
		return false
	} else if (dd > 31) {
		alert(months[mm] + " has only 31 days.")
		return false
	}
	return true
}

// check the entered February date for too high a value 
function checkLeapMonth(mm,dd,yyyy) {
	if (yyyy % 4 > 0 && dd > 28) {
		alert("February of " + yyyy + " has only 28 days.")
		return false
	} else if (dd > 29) {
		alert("February of " + yyyy + " has only 29 days.")
		return false
	}
	return true
}
// extract front part of string prior to searchString
function getFront(mainStr,searchStr){
	foundOffset = mainStr.indexOf(searchStr)
	if (foundOffset == -1) {
		return null
	}
	return mainStr.substring(0,foundOffset)
}

// extract back end of string after searchString
function getEnd(mainStr,searchStr) {
	foundOffset = mainStr.indexOf(searchStr)
	if (foundOffset == -1) {
		return null
	}
	return mainStr.substring(foundOffset+searchStr.length,mainStr.length)
}

// insert insertString immediately before searchString
function insertString(mainStr,searchStr,insertStr) {
	var front = getFront(mainStr,searchStr)
	var end = getEnd(mainStr,searchStr)
	if (front != null && end != null) {
		return front + insertStr + searchStr + end
	}
	return null
}

// remove deleteString
function deleteString(mainStr,deleteStr) {
	return replaceString(mainStr,deleteStr,"")
}

// replace searchString with replaceString
function replaceString(mainStr,searchStr,replaceStr) {
	var front = getFront(mainStr,searchStr)
	var end = getEnd(mainStr,searchStr)
	if (front != null && end != null) {
		return front + replaceStr + end
	}
	return null
}


