var navReady  = 0;
var timing = 1;
var loaded = 0;

IE5 = (navigator.userAgent.indexOf("MSIE 5") > 0) ? 1 : 0;

if (document.all && !IE5) {  //overload document.getElementById for IE4
  document.getElementById = function (name) {
    return document.all(name);
  }
}

function preLoad(imageFile) {
  eval(imageFile + "_on = new Image(); " + imageFile + "_on.src = 'mouseovers/button_" + imageFile + "_on.gif';");   
}

function imageOn(item) {
  if (loaded) {
    eval("document." + item + "I.src = " + item + "_on.src;");
  }
}

function imageOff(item) {
  if (loaded) {
    eval("document." + item + "I.src = 'mouseovers/button_" + item + "_off.gif';");
  }
}

// ------------------------------------------------------------------------

/*
 * restarts an aprox 0.1sec countdown delay before hiding the on state
 */
function doTimer() {
  clearTimeout(timing);
  timing = setTimeout('hideAll();', 100);
}

function hideAll() {
  imageOff('about');
  imageOff('registry');
  imageOff('resources');
  imageOff('special');
  imageOff('links');
  imageOff('members');
}

// ------------------------------------------------------------------------

function newWindow(url, name, width, height) {
  photoWindow = window.open(url, name,"location=no,directories=no,menubar=no,status=no,toolbar=no,scrollbars=yes,height=" +height+ ",width=" +width+ ",resizable=yes");
  try {
    photoWindow.resizeTo(width,height);
    photoWindow.focus();
  } catch (e) {}
  return false;
}

// ------------------------------------------------------------------------

function doShow(id) {
  if (document.all) {
    document.getElementById(id).style.filter = "blur(direction=0, strength=0)";
  }
}

function doBlur(id) {
  if (document.all) {
    document.getElementById(id).style.filter = "xRay(), invert(), alpha(opacity=40)";
  }
}

/*
  AJAX population functions follow
*/
var selectedStateId = "";

/**
 * Constructor for a State.
 * Used for cache
 */
function State (id, title) {
  this.id = id;
  this.title = title;
}

/**
 * Refresh state select box
 */
function refreshStates(targetUrl, paramString, sId) {
  // update pointers to selected items
  if (sId != null) {
    selectedStateId = sId;
  }

  window.status = "Refreshing State cache...";
  var req = newXMLHttpRequest();

  var handlerFunction = getReadyStateHandler(req, _updateStates);
  req.onreadystatechange = handlerFunction;  
  req.open("POST", targetUrl, true);
  req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

  req.send(paramString);
}

/**
 * AJAX - populate list via returned XML
 */
function _updateStates(inputXML) {
  var rootNode = inputXML.getElementsByTagName("country")[0];

  var list = rootNode.getElementsByTagName("state");
  var tmpList = new Array();

  for (var i=0; i<list.length; i++) {
    var item = list[i];

    var id = item.getElementsByTagName("id")[0].firstChild.nodeValue;
    var tmpNode = item.getElementsByTagName("title")[0].firstChild;
    var title = (tmpNode != null) ? tmpNode.nodeValue : '';
        
    // update cache
    tmpList.push(new State(id, title));
  }

  if (tmpList.length > 0) {
    stateCache = tmpList;
    window.status = "Done.";
    drawStateList();
  }
}

/**
 * Draws select box of entities drawn from cache.
 */
function drawStateList() {
  // get contents
  var contents = ap_form.strState;

  // clear list
  clearList(contents);
  contents.options[0] = new Option('Please choose ...', '');

  for (var i=0 ; i<stateCache.length ; i++) {
    tmpState = stateCache[i];

    // populate list
    contents.options[i+1] = new Option(tmpState.title, tmpState.id);

    if (tmpState.id == selectedStateId) {
      contents.options.selectedIndex = i+1;
    }
  }
}

/**
 * Utility function
 * Clear the HTML list used to display the list contents
 */
function clearList(fld) {
  for (i = 0; i < fld.options.length; i++) {
    fld.options[i] = null;
  }
  fld.options.length = 0;
  fld.options[0] = new Option('No results yet...', '');
}
