var tableVisible;
var helpid = 'help-row';

function initHighlight() {

    if (!document.getElementsByTagName){ return; }
    var allfields = document.getElementsByTagName("input");
    
    // loop through all input tags and add events
    for (var i=0; i<allfields.length; i++){
        var field = allfields[i];
        if ((field.getAttribute("type") == "text") 
			|| (field.getAttribute("type") == "password")) {
            field.onfocus = function () {this.className = 'input-active';}
            field.onblur = function () {this.className = 'input';}
        }
    }

	var allfields = document.getElementsByTagName("textarea");
    for (var i=0; i<allfields.length; i++){
        var field = allfields[i];
            field.onfocus = function () {this.className = 'input-active';}
            field.onblur = function () {this.className = 'textarea';}
    }

}

function SetTableStatus() {
	var expdate = new Date ();
	expdate.setTime (expdate.getTime() + (24 * 60 * 60 * 1000 * 31));
	SetCookie ("MT_HELP_TABLE", tableVisible, expdate);
}

function togglefirst() {
	var tableVisible = GetCookie('MT_HELP_TABLE');

	var tr = document.getElementById(helpid);
	if (tr==null) { return; }
	if(tableVisible=="true") {	
		// Previous time stored as visible, so show the row
		//temp.style.position = 'relative';
		//temp.style.visibility = 'visible';
	} else {				
		// Previous time stored as hidden, so hide the table
		//temp.style.position = 'absolute';
		//temp.style.visibility = 'hidden';
		var bExpand = tr.style.display == '';
		tr.style.display = (bExpand ? 'none' : '');
		var img = document.getElementById('img-'+helpid);
		var txt = document.getElementById('txt-'+helpid);
		if (img!=null)
		{
			if (!bExpand) 
			{
				txt.innerHTML = 'Hide Help';
				img.src = '../media/images/icons/minus.gif';
			} else
			{
				txt.innerHTML = 'Show Help';
				img.src = '../media/images/icons/plus.gif';
			}
		}
	}
}

function toggle(id)
{
	var tr = document.getElementById(id);
	if (tr==null) { return; }
	var bExpand = tr.style.display == '';
	tr.style.display = (bExpand ? 'none' : '');
	var img = document.getElementById('img-'+id);
	var txt = document.getElementById('txt-'+id);

	if (img!=null)
	{
		if (!bExpand) 
		{
			txt.innerHTML = 'Hide Help';
			img.src = '../media/images/icons/minus.gif';
			tableVisible = "true";
		} else {
			txt.innerHTML = 'Show Help';
			img.src = '../media/images/icons/plus.gif';
			tableVisible = "false";
		}
	}
	SetTableStatus();
}

function getCookieVal (offset) {
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1)
		endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie (name) {
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg)
			return getCookieVal (j);
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break; 
	}
	return null;
}  

function SetCookie (name, value) {
	var argv = SetCookie.arguments;
	var argc = SetCookie.arguments.length;
	var expires = (argc > 2) ? argv[2] : null;
	var path = (argc > 3) ? argv[3] : null;
	var domain = (argc > 4) ? argv[4] : null;
	var secure = (argc > 5) ? argv[5] : false;
	document.cookie = name + "=" + escape (value) +
		((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
		((path == null) ? "" : ("; path=" + path)) +
		((domain == null) ? "" : ("; domain=" + domain)) +
		((secure == true) ? "; secure" : "");
}

// Nifty function to add onload events without overwriting
// ones already there courtesy of the lovely and talented
// Simon Willison http://simon.incutio.com/
function addLoadEvent(func) {   
    var oldonload = window.onload;
    if (typeof window.onload != 'function'){
        window.onload = func;
    } else {
        window.onload = function(){
        oldonload();
        func();
        }
    }
}



addLoadEvent(initHighlight);
addLoadEvent(togglefirst);
