function $() {
  var elements = new Array();
  for (var i = 0; i < arguments.length; i++) {
    var element = arguments[i];
    if (typeof element == 'string')
      element = document.getElementById(element);
    if (arguments.length == 1)
      return element;
    elements.push(element);
  }
  return elements;
}

function winHeight() {
  var myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    //not sure why...but 20px are needed here
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myHeight = document.body.clientHeight;
  }
  return myHeight;
}

function fixHeight(el) {
  var h_ = winHeight();
  if (el.offsetHeight>h_)
		return;
  if (el.style.height<h_)
    el.style.height = h_ - document.getElementById("wrapper").offsetHeight + "px";
}

function doSearch(url) {
    var params = new Array();
    var par = "";
    
    if(document.getElementById("selTown").value > 0)
        params[params.length] = format('t={0}',document.getElementById("selTown").value);
    if(document.getElementById("selBed").value > 0)
        params[params.length] = format('bed={0}',document.getElementById("selBed").value);
    if(document.getElementById("selType").value > 0)
        params[params.length] = format('typ={0}',document.getElementById("selType").value);
    if(document.getElementById("selCategory").value > 0)
        params[params.length] = format('cat={0}',document.getElementById("selCategory").value);
    if(document.getElementById("cbOrderBy")!=null){
        if(document.getElementById("cbOrderBy").checked)
            params[params.length] = 's=abc';
    }  
    if(params.length > 0)
        par = format('?{0}',params.join("&"));
  
    window.location.href = format('{0}{1}', url, par);
  return false;
}

function email(n,h,isL,oN) { 
  var e = n.substring(3) + '@' + h; 
  var _o = '';
  
  var lt = e; 
  
  if (oN) 
    { lt = oN; isL = 1; } 
  if (isL) 
    _o = '<a href=\"mailto:' + e + '\">';
    
  _o = _o + lt;
  
  if (isL) 
  _o = _o + '</a>';
  
  document.write(_o); 
  
  document.close(); 
}

function globalSearch(input_){
  if (input_.value.length>0)
    document.location.href = '/search.aspx?q=' + encodeURIComponent(input_.value);
  return false;
}

function checkEnter(e){
		var cc_;
		if(e && e.which){ e = e; cc_ = e.which; }
		else { e = event; cc_ = e.keyCode; }
		if(cc_ == 13) {
			//alert($('query').value);
			document.forms[0].action = '/search.aspx?q=' + encodeURIComponent($('q').value);
			document.location.href = '/search.aspx?q=' + encodeURIComponent($('q').value);
			return false;
			}
		else { return true; }
}

var win = null;
function openCenteredPopup(mypage,myname,w,h,features) {
  if (features=='default')
    features = 'resizable=yes,scrollbars=no';
  var winl = (screen.width-w)/2;
  var wint = (screen.height-h)/2;
  if (winl < 0) winl = 0;
  if (wint < 0) wint = 0;
  var settings = 'height=' + h + ',';
  settings += 'width=' + w + ',';
  settings += 'top=' + wint + ',';
  settings += 'left=' + winl + ',';
  settings += features;
  win = window.open(mypage,myname,settings);
  win.window.focus();
}

String.prototype.replaceAll = function(strTarget, strSubString ) {
    var strText = this;
    var intIndexOfMatch = strText.indexOf(strTarget);
    
    while (intIndexOfMatch != -1){
      strText = strText.replace( strTarget, strSubString )
      intIndexOfMatch = strText.indexOf( strTarget );
    } 
    return( strText );
}

function format(str) {
  for(i = 1; i < arguments.length; i++)   {
    while(str.indexOf('{'+(i - 1)+'}') > -1) {
        str = str.replace('{'+(i - 1)+'}', arguments[i]);
    } 
  }
  return str;
}

// Make the call...AJAX call
function xhr(j,k){
	var w = window;
	if (typeof XMLHttpRequest != "undefined") {
        r = new XMLHttpRequest();
    } else if (typeof ActiveXObject != "undefined") {
        r = new ActiveXObject("Microsoft.XMLHTTP");
    } else {
        r = null;
    }
	
	if (r) {
		r.onreadystatechange=function() {
			if(r.readyState==4){
				k(r.responseText,r.responseXML);
			} 
		};
		r.open('GET',j,!0);
		r.send('');
	}
}

// On DOM load start function
startStack=function() { };  // A stack of functions to run onload/domready

registerOnLoad = function(func) {
   var orgOnLoad = startStack;
   startStack = function () {
      orgOnLoad();
      func();
      return;
   }
}
var ranOnload=false; // Flag to determine if we've ran the starting stack already.

if (document.addEventListener) {
  // Mozilla actually has a DOM READY event.
   document.addEventListener("DOMContentLoaded", function(){if (!ranOnload) {ranOnload=true; startStack();}}, false);
}  else if (document.all && !window.opera) {
  // This is the IE style which exploits a property of the (standards defined) defer attribute
  document.write("<scr" + "ipt id='DOMReady' defer=true " + "src=//:><\/scr" + "ipt>");  
  document.getElementById("DOMReady").onreadystatechange=function(){
    if (this.readyState=="complete"&&(!ranOnload)){
      ranOnload=true;
      startStack();
    }
  }
}
var orgOnLoad=window.onload;
window.onload=function() {
   if (typeof(orgOnLoad)=='function') {
     orgOnLoad();
   }
   if (!ranOnload) {
    ranOnload=true;
     startStack();
   }
}
