

// ----------------------------------
// cross-browser functions

var IE_all_cache = new Object();
function IE_getElementById(id) {
  if (IE_all_cache[id] == null) {
    IE_all_cache[id] = document.all[id];
  }
  return IE_all_cache[id];
}

if (document.all) {
  if (!document.getElementById) {
    document.getElementById = IE_getElementById;
  }
}

//-----------------------------------
// Browser detection

var agt = navigator.userAgent.toLowerCase();

var is_opera = (agt.indexOf("opera") != -1);
var is_ie = (agt.indexOf("msie") != -1) && document.all && !is_opera;
var is_ie5 = (agt.indexOf("msie 5") != -1) && document.all;

// ---------------------------------

// ----------------------------------
// visibility functions

function tog() {
  // tog: toggle the visibility of html elements (arguments[1..]) from none to
  // arguments[0].  Return what should be returned in a javascript onevent().
  display = arguments[0];
  for( var i=1; i<arguments.length; i++ ) {    
    var x = document.getElementById(arguments[i]);
    if (x.style.display == "none" || x.style.display == "") {
      x.style.display = display;
    } else {
      x.style.display = "none";
    }
  } 

  var e = is_ie ? window.event : this;
  if (e) {
    if (is_ie) {
      e.cancelBubble = true;
      e.returnValue = false;
      return false;
    } else {
      return false;
    }
  }
}

// ----------------------------------
// XML Http requests

function CreateXmlHttpReq(handler) {
  var xmlhttp = null;

  if (is_ie) {
    // Guaranteed to be ie5 or ie6
    var control = (is_ie5) ? "Microsoft.XMLHTTP" : "Msxml2.XMLHTTP";
    try {
      xmlhttp = new ActiveXObject(control);
    } catch(e) {
      // TODO: better help message
      alert("You need to enable active scripting and activeX controls");
      DumpException(e);
    }
  } else {
    // Mozilla
    xmlhttp = new XMLHttpRequest();
  }
  xmlhttp.onreadystatechange = function() {
    handler(xmlhttp);
  }
  return xmlhttp;
}

var uniqnum_counter = (new Date).getTime();
function UniqueNum() {
  ++uniqnum_counter;
  return uniqnum_counter;
}

// Send a (hidden, non blocking) update message to the server
// by sending a xmlhttp request
function SendServerRequest(url) {
  StartGETRequest(url, function() {});
}

// Send a request to the server
function StartGETRequest(url, handler) {
  var xmlhttp = CreateXmlHttpReq(handler);
  xmlhttp.open('GET', url + "&rand=" + UniqueNum(), true);
  xmlhttp.send(null);
}

// Send a post request to the server
function StartPOSTRequest(url, data, handler) {
  var xmlhttp = CreateXmlHttpReq(handler);
  xmlhttp.open('POST', url, true);
  xmlhttp.send(data);
}

// Make functions exist for older browsers
function toggle_watch() {}


function toggle_watch(idstr) {
  var tog = document.getElementsByName(idstr)
  var img = document.getElementsByName(idstr + "_img")

  if (tog.length < 1) {
    
    return; // bad!
  }
  try {
    state = parseInt(tog[0].getAttribute("value"));
  } catch (e) {
    state = 1
  }
  for (var x=0;x<tog.length;x++) {
    if (state == 1) {
      tog[x].setAttribute("value", 0)
      img[x].src = 'http://www.telinet.co.uk/images/watched_n.gif'
    } else {
      tog[x].setAttribute("value", 1)
      img[x].src = 'http://www.telinet.co.uk/images/watched_y.gif'
    }
    tog[x].blur();
  }

  if (state == 1) {
    SendServerRequest("http://www.telinet.co.uk/watchupdate.php?id=" + idstr + "&action=remove");
    //alert("http://www.telinet.co.uk/watchupdate.php?id=" + idstr + "&action=remove");
  } else {
    SendServerRequest("http://www.telinet.co.uk/watchupdate.php?id=" + idstr + "&action=add");
    //alert("http://www.telinet.co.uk/watchupdate.php?id=" + idstr + "&action=add");
  }

  var e = is_ie ? window.event : this;
  if (e) {
    if (is_ie) {
      e.cancelBubble = true;
      e.returnValue = false;
      return false;
    } else {
      return false;
    }
  }
  return false;
}

