var LASTEL = null;
var LASTELNAME = null;

function mailTo(articleid) {
  var theUrl =  escape(window.location.href);
  // get center positions
  var theLeft = ((document.all? document.body.clientWidth : window.innerWidth - 8) / 2) - (470 / 2);
  var theTop = ((document.all? document.body.clientHeight : window.innerHeight - 8) / 2) - (380 / 2);

  window.open('default.asp?action=mailto&id=' + articleid + '&url=' + theUrl, 'mailto','left=' + theLeft + ',top=' + theTop + ',width=490,height=425,resizable=no,scrollbars=yes')
}

// switch displaying of categorized articles
function selectCategory(theCategory, theType) {
  try {
    var theEl = document.getElementById(theCategory + theType);
    var theImg = document.getElementById("img" + theCategory + theType);
    theEl.style.display = (theEl.style.display == '')? 'none' : '';
    theImg.src = (theEl.style.display == '')? 'img/mapopen.gif' : 'img/map.gif';
  } catch(e) {}
};

function email() {
  var to = "";
  var subject = "DCDD Article: " + document.getElementById("title").innerText;
  var body = "Hello, \n\nThere is an article on the DCDD website that I think you will find interesting.\nHere is the url:\n\n"
    + document.location.href
    + "\n\nGreetings,\n\n";

  var doc = "mailto:" + to +
    "?subject=" + escape(subject) +
    "&body=" + escape(body);

    window.location.href = doc;
}

var main = {
  login : function() {
    var u = document.getElementById("username").value;
    var p = document.getElementById("password").value;
    var url = "/login.asp?username=" + u + "&password=" + p + "&x=true&d=" + new Date().getTime();
    this.send(url);
  },
  logout : function() {
    var url = "/login.asp?username=logout&d=" + new Date().getTime();
    this.send(url);
  },
  send : function(url) {
    var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    xmlhttp.open("POST", url, false);
    xmlhttp.send(null);

    try { var s=''+xmlhttp.responseText;
    } catch(e) { var s = ''; }

    if (s) {
      var tests = s.split(":");
      if (tests.length > 0) {
        switch(tests[0]) {
          case "javascript":
            var script = tests[1];
            eval(script);
            break;
          default:
            alert(s);
            break;
        }
        return;
      }
      alert(s);
    } else {
      document.location.reload();
    }
  }
}

function deleteArticles(theForm) {
  var frm = document.getElementById(theForm);
  if (itemsChecked(frm)) {
    if (confirm('Press OK to delete selected document(s).')) {
      frm.submit();
    }  
  } else {
    alert('Cannot delete document(s). No document(s) selected.');
  }
  return false;
}

function deleteMembers(theForm) {
  var frm = document.getElementById(theForm);
  if (itemsChecked(frm)) {
    if (confirm('Press OK to delete selected member(s).')) {
      frm.submit();
    }  
  } else {
    alert('Cannot delete member(s). No member(s) selected.');  
  }
  return false;
}

function itemsChecked(frm) {
  var el, i = 0;
  var checked = false;
  while (el = frm.elements[i++]) {
    if ((el.type == 'checkbox') && (el.checked == true)) checked = true;  
  }
  return checked;
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

function externalLinks() {
  if (!document.getElementsByTagName) return;
  var anchors = document.getElementsByTagName("a");
  for (var i=0; i<anchors.length; i++) {
    var anchor = anchors[i];
    if (anchor.getAttribute("href") &&
      anchor.getAttribute("rel") == "external")
      anchor.target = "_blank";
  }
}

addLoadEvent(externalLinks);
