// TODO: check for any old instances of verify_login and verify_forgot and replace with new functions

    var addEvent = window.addEventListener
      ? function(el, ev, fn) { return (el && ev && fn) ? el.addEventListener(ev, fn, false) : null; }
      : function(el, ev, fn) { 
	  return (el && ev && fn) ? el.attachEvent('on' + ev, function(e) { 
	      return fn.call(el, e); }
	    ) : null 
	}

    // For debugging
    function log(text) { if (typeof console.log != 'undefined') { return console.log(text); } else {alert(text); } }

    // TODO: make the function below compatible with multiple class values.
    function tableruler() {
	var tables = document.getElementsByTagName('table');
	for (var i=0; i<tables.length; i++) {
	    if (tables[i].className.indexOf('ruler') != -1) {
		var trs = tables[i].getElementsByTagName('tr');
		for (var j=0; j<trs.length; j++) {
		    if (trs[j].parentNode.nodeName.toLowerCase() == 'tbody' && trs[j].parentNode.nodeName.toLowerCase() != 'tfoot') {
			trs[j].onmouseover = function() {
			    this.className = 'ruled';
			    return false;
			}
			trs[j].onmouseout = function() {
			    this.className = '';
			    return false;
			}
		    }
		}
	    }
	}
    }

    // init function -------------------------------------------------- 
    function init() {
	var changeAccount = document.getElementById('set_account_id');
	    addEvent(changeAccount, 'change', function() {
		change_account(this.value);
		return false;
	    });

	var loginForm = document.getElementById('form-login');
	addEvent(loginForm, 'submit', verifyLogin, false);

	var forgotForm = document.getElementById('form-forgot');
	addEvent(forgotForm, 'submit', verifyForgot, false);

	function verifyLogin(e) {
	    var username = document.getElementById('username').value;
	    var password = document.getElementById('password').value;
	    if (username == '' && password == '') {
		alert('Please fill in username and password.');
		if (e.preventDefault) {
		    e.preventDefault();
		} else {
		    window.event.returnValue = false;
		}
	    }
	}

	function change_account(account, section) {
	    var currentUrl = window.location.href, url;
	    if (currentUrl.indexOf('?') != -1) {
		url = currentUrl + '&account_id=' + account;
	    } else {
		url = location.href + '?account_id=' + account;
	    }
	    window.location.href = url;
	}

	function verifyForgot(e) {
	    var username = document.getElementById("username").value;
	    var email = document.getElementById("email").value;
	    if (username == '' && email == '') {
		alert('You must enter your username or email address.');
		if (e.preventDefault) {
		    e.preventDefault();
		} else {
		    window.event.returnValue = false;
		}
	    }
	    return true;
	}
    }
    // init function --------------------------------------------------

    // attach event handlers
    addEvent(window, 'load', init);
    addEvent(window, 'load', tableruler);

    // custom functions
    function open_details( fid,dw_id ) {
	var opts='width=600,height=600,status=no,resizeable=yes,scrollbars=yes';
	var winname = 'datatoolwindow';
	window.open('http://www.esiteportal.com/modules/datatool/details.php?form_type='+fid+'&dw_id='+dw_id,winname,opts);
    }
