var opera = navigator.userAgent.indexOf("Opera") != -1;
var konqueror = navigator.appName == "Konqueror";
var netscape4 = navigator.appName == "Netscape" && navigator.appVersion < "5";
var ie = navigator.appName == "Microsoft Internet Explorer";

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 && !ie)
      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 popup3(url,width,height) {
  popup5(url,width,height,"no","no");
}

function popup5(url,width,height,size,scroll) {
  window.open(url,"_blank","left=0,top=0,width=" + width + ",height=" + height +
    ",menubar=no,location=no,status=no,toolbar=no,resizable=" + size + ",scrollbars=" +
    scroll);
}

function doDetail(query,pics) {
  h = pics == 0 ? 360 : 770; //(screen.height > 770 ? 760 : 530);
  popup5("detail.jsp?" + query,730,h,"yes","yes");
}

function doEnquiry(id,query) {
  window.open("enquire.jsp?advid=" + id + query,"_blank",
    "left=0,top=0,width=720,height=520,menubar=no,location=no,status=no,toolbar=no,resizeable=no,scrollbars=no");
}

function nthChild(parent, tag, n) {
  var list = parent.childNodes;
  for (i = 0; i < list.length; i++)
    if (list[i].tagName == tag) {
      if (--n == 0)
        return list[i];
    }
  return null;
}

function changeTab(cell) {
  var row = cell.parentNode;
  var index = 0;
  var count = 0;
  var cells = row.childNodes;
  for (i = 0; i < cells.length; i++) {
    var c = cells[i];
    if (c.tagName == "TD") {
      if (c == cell) {
        c.style.background = "url(images/TabActive.gif)";
        index = count;
      }
      else
        c.style.background = "url(images/TabInactive.gif)";
      count++;
    }
  }
  var node = row.parentNode.parentNode.parentNode.parentNode.parentNode;
  row = nthChild(node, "TR", 2);
  node = nthChild(row, "TD", 2);
  node = nthChild(node, "TABLE", 1);
  node = nthChild(node, "TBODY", 1);
  rows = node.childNodes;
  for (i = 0; i < rows.length; i++) {
    row = rows[i];
    if (row.tagName == "TR")
      row.style.display = (index-- == 0 ? "" : "none");
  }
}

function limitText(limitField, limitCount, limitNum) {
	if (limitField.value.length > limitNum)
		limitField.value = limitField.value.substring(0, limitNum);
	else
		limitCount.innerHTML = limitNum - limitField.value.length;
}

function get(id) {
  return document.getElementById(id);
}
