var opera = navigator.userAgent.indexOf("Opera") != -1;
var konqueror = navigator.appName == "Konqueror";
var netscape4 = navigator.appName == "Netscape" && navigator.appVersion < "5";

if (netscape4)
  document.write("<layer id=scriptLayer visibility=hide>&nbsp;</layer>");
else if (konqueror || opera)
  document.write("<iframe id='scriptFrame' width='0' height='0' border='0' scrolling='no'></iframe>");

function clearCombo(combo,value) {
  if (konqueror) {
    for (i = combo.length - 1; i >= 0; i--)
      combo.remove(i);
    if (value)
      combo.add(new Option(value,-1),null);
  }
  else {
    for (i = combo.length - 1; i >= 0; i--)
      combo.options[i] = null;
    if (value) {
      combo.options[0] = new Option();
      combo.options[0].value = -1;
      combo.options[0].text = value;
    }
  }
  combo.selectedIndex = combo.length - 1;
}

var head;
var script;

function runScript(source) {
  if (konqueror)
    document.scriptFrame.location.href=source + "&br=1";
  else if (opera)
    document.frames.scriptFrame.location.href=source + "&br=2";
  else if (netscape4)
    document.scriptLayer.load(source + "&br=1",0);
  else {
    if (!head)
      head = document.getElementsByTagName('head').item(0);
    if (script)
      head.removeChild(script);
    script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = source;
    head.appendChild(script);
  }
}

var combo;
var first;
var doAfter;

function selectedItem(source) {
  var result = null;
  if (source.selectedIndex != -1) {
    var option = source.options[source.selectedIndex];
    result = option.value;
    if (result == '' || result == null)
      result = option.text;
  }
  return result;
}

function selectItem(source, value) {
  var count = source.options.length;
  for (i = 0; i < count; i++) {
    if (source.options[i].value == value) {
      source.selectedIndex = i;
      break;
    }
  }
}

function loadCombo4(source,dest,script,empty) {
  loadCombo6(source,dest,script,empty,null,-1);
}

function loadCombo5(source,dest,script,empty,doOnLoad) {
  loadCombo6(source,dest,script,empty,doOnLoad,-1);
}

function loadCombo6(source,dest,script,empty,doOnLoad,novalue) {
  combo = dest;
  first = empty;
  doAfter = doOnLoad;
  var id = selectedItem(source);
  if (id == novalue)
    clearCombo(dest,empty);
  else {
    clearCombo(dest,"Loading...");
    runScript(script + id);
  }
}

function setComboItems4(combo,names,values,empty) {
  setComboItems5(combo,names,values,empty,false);
}

function setComboItems5(combo,names,values,empty,refresh) {
  clearCombo(combo,empty);
  if (names) {
    for (i = 0; i < names.length; i++) {
      var option = new Option();
      option.text = names[i];
      option.value = values[i];
      combo.options[combo.length] = option;
    }
  }
  combo.selectedIndex = 0;
  if (netscape4 && refresh)
    history.go(0);
}

function loadedCombo(names,values) {
  setComboItems5(combo,names,values,first,doAfter == null);
  if (doAfter != null) {
    if (netscape4)
      window.setTimeout("after();",100);
    else
      doAfter();
  }
  else if (netscape4)
    history.go(0);
}

function after() {
  doAfter();
}

function doDetail(query,pics) {
  h = pics == 0 ? 360 : (screen.height > 750 ? 730 : 530);
  window.open("detail.jsp?" + query,"_blank","left=0,top=0,width=730,height=" + h +     ",menubar=no,location=no,status=no,toolbar=no,resizable=yes,scrollbars=yes");
}

function doEnquiry(id,query) {
  window.open("enquire.jsp?advid=" + id + query,"_blank","left=0,top=0,width=720,height=480,menubar=no,location=no,status=no,toolbar=no,resizeable=no,scrollbars=no");
}