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

var newsArticleId;

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

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

function scrollWindow(url, name, width, height) {
  myWindow = window.open(url, name,"location=no,directories=no,menubar=no,statusbar=no,toolbar=no,scrollbars=yes,height=" +height+ ",width=" +width+ ",resizable=yes"); 
  myWindow.resizeTo(width,height);
  myWindow.focus();
  return false;
}

// Round to nearest thousand
function myRound(value) {   
  if (value == 0) {
    return 0;
  }
  value = "" + Math.round(value + 500);
  if (value.length > 3) {
    value = value.substring(0, value.length - 3) + "000";
  }
  return value;
}

function numberValueOf(frm, field, index) {
  if (frm == null || field == null) {
    return 0;
  }

  field = field + "_" + index;
  var formElem = frm.elements[field];
  if (formElem == null) {
    return 0;
  }
  var value = formElem.value;
  var numberVal = parseFloat(value);
  return (isNaN(numberVal)) ? 0 : numberVal;
}

function updateCurrencyValue(id, value, index) {
  var node = document.getElementById(id + "_" + index);
  value = new String(Math.round(value));

  if (node == null || value == null) {
    return;
  }
  var isNegative = (value < 0);
  if (isNegative) {
    value = -value;
  }
  var count = 0;
  var commified = "";
  for (var i = value.length - 1; i >= 0; i--) {
    count++;
    if (count == 3 && i > 0) {
      commified += value.charAt(i);
      commified += ",";
      count = 0;
    } else {
      commified += value.charAt(i);
    }            
  }
  var commified2 = "";
  for (var i = commified.length - 1; i >= 0; i--) {
    commified2 += commified.charAt(i);
  }

  if (isNegative) {
    node.innerHTML = "-$" + commified2;
  } else {
    node.innerHTML = "$" + commified2;
  }
}

function clearForm(frm) {
  frm = document.getElementById("insuranceCalc");
  for (i = 0; i < frm.elements.length; i++) {
    var elem = frm.elements[i];
    if (elem.type == "text") {
      elem.value = 0;
    }
  }
}

function newsReadMore(id) {
  newsArticleId = id;
  return;
}

function viewReadMore(url) {
  location.href = url + newsArticleId;
  return false;
}


/**
 * Attaches an onclick event to each link with rel="external" that opens the
 * link in a new window.
 */
function initExternalLinks() {
  var as = document.getElementsByTagName("a");
  for (var i = 0; i < as.length; i++) {
    if (as[i].getAttribute("rel") == "external") {
      as[i].onclick = openNewWindow;
    }
  }

  function openNewWindow() {
    window.open(this.href);
    return false;
  }
}
