//
// Useful JavaScript Functions
//
///////////////////////////////
//
// Author: Aaron
// Date: Oct. 16/07
//

// Function call tester
function checker() {
  alert('yaba daba dooo');
}

// Adds removed drop-shadow from sidbar img
function shadow(id) {
var elem = document.getElementById(id);
elem.style.margin = '-6px 6px 6px -6px';
}

// Removes drop-shadow from sidebar img
function noshadow(id) {
var elem = document.getElementById(id);
elem.style.margin = '0px 0px 0px 0px';
}

// Return element with id 'v'
function $(v) {
  return(document.getElementById(v));
}

// Return element's style with id 'v'
function $S(v) {
  return($(v).style);
}

// Returns positive integer if matched browser type
// Otherwise 0
function browser(v) {
  return(Math.max(navigator.userAgent.toLowerCase().indexOf(v), 0));
}

var http_request = false;
function makeRequest(url, parameters, handler) {
  http_request = false;
  if(window.XMLHttpRequest) {
    http_request = new XMLHttpRequest();
  } else if(window.ActiveXObject) {
    try {
      http_request = new ActiveXObject("Microsoft.XMLHTTP");
    } catch(e) {}
  }
  if(!http_request) {
    alert('Cannot create XMLHTTP instance');
    return false;
  }
  http_request.onreadystatechange =
    function() {
      alertContents(handler);
    }
  http_request.open("GET", url + parameters, true);
  http_request.send(null);
}

function alertContents(handler) {
  if(http_request.readyState == 4) {
    if(http_request.status == 200) {
      var xmldoc = http_request.responseXML;
      var root = xmldoc.getElementsByTagName("root").item(0);
      handler.xmlAction(getNodeData(root));
    } else {
      alert("There was a problem with the request: " + http_request.status);
    }
  }
}

function getNodeData(parentNode) {
//  alert("**" + parentNode.nodeName);
  if(parentNode.childNodes.length == 1 &&
     parentNode.childNodes.item(0).nodeName == "#text") {
//    alert(parentNode.nodeName + " = " + parentNode.childNodes.item(0).nodeValue);
    return parentNode.childNodes.item(0).nodeValue;
  }
  var xmldata = new Array();
  for(var i = 0; i < parentNode.childNodes.length; i++) {
    var childNode = parentNode.childNodes.item(i);
    xmldata[childNode.nodeName] = getNodeData(childNode);
  }
  return xmldata;
}
