
/** DISPLAY FUNCTIONS */

function showDiv(id) { document.getElementById(id).style.visibility = "visible"; }
function hideDiv(id) { document.getElementById(id).style.visibility = "hidden"; }
function toggleDiv(id) {
	if(document.getElementById(id).style.display == 'none')
		document.getElementById(id).style.display = 'block';
	else
		document.getElementById(id).style.display = 'none';
}

/** FORM BEHAVIOUR */

function loginForm(frm) {
	if((frm.Password.value == '') && (frm.Username.value == '')) { return false; }
	if(frm.Password.value != '')
	{
		frm.PasswordHash.value = MD5(frm.Password.value);
		frm.Password.value = '';
		frm.MD5.value = 1;
	}
	processForm(frm);
	return true;
}

function processForm(frm) {
	frm.Submit.disabled = true;
	showDiv('formPosting');
	return true;
}

function formFocus() {
    if (document.forms.length > 0) {
        var field = document.forms[0];
        for (var i = 0; i < field.length; i++) {
            if (
                (field.elements[i].type == "text") ||
                (field.elements[i].type == "password") ||
                (field.elements[i].type == "textarea") ||
                (field.elements[i].type == "select")) {
                if(field.elements[i].value != '') { continue; }
                document.forms[0].elements[i].focus();
                break;
            }
        }
    }
}

function increaseTextareaHeight(areaid, add) {
	// used with a nod to alex king, slightly updated with offsetHeight
	thisTextarea = document.getElementById(areaid);
	if (thisTextarea) {
		newHeight = thisTextarea.offsetHeight + add;
		thisTextarea.style.height = newHeight.toString() + "px";
	}
}

function decreaseTextareaHeight(areaid, subtract) {
	thisTextarea = document.getElementById(areaid);
	if (thisTextarea) {
		if ((thisTextarea.offsetHeight - subtract) > 50) {
			newHeight = thisTextarea.offsetHeight - subtract;
			thisTextarea.style.height = newHeight.toString() + "px";
		}
		else {
			thisTextarea.style.height = "50px";
		}
	}
}

/** PAGE EFFECTS */

// zebra tables
function hasClass(obj) {
	var result = false;
	if (obj.getAttributeNode("class") != null) {
	 result = obj.getAttributeNode("class").value;
	}
	return result;
}

function stripe(id) {
	var even = false;
	var evenColor = arguments[1] ? arguments[1] : "#fff";
	var oddColor = arguments[2] ? arguments[2] : "#eee";
	var table = document.getElementById(id);
	if (! table) { return; }
	var tbodies = table.getElementsByTagName("tbody");
	for (var h = 0; h < tbodies.length; h++) {
	  var trs = tbodies[h].getElementsByTagName("tr");
	  for (var i = 0; i < trs.length; i++) {
	    if (! hasClass(trs[i]) &&
	        ! trs[i].style.backgroundColor) {
	      var tds = trs[i].getElementsByTagName("td");
	      for (var j = 0; j < tds.length; j++) {
	        var mytd = tds[j];
	        if (! hasClass(mytd) &&
	            ! mytd.style.backgroundColor) {
	          mytd.style.backgroundColor =
	            even ? evenColor : oddColor;
	        }
	      }
	    }
	    even =  ! even;
	  }
	}
}

function findZebras() {
    if (!document.getElementsByTagName) return;
    tblList = document.getElementsByTagName("table");
    for (ti=0;ti<tblList.length;ti++) {
        thisTbl = tblList[ti];
        if (((' '+thisTbl.className+' ').indexOf("zebra") != -1) && (thisTbl.id)) {
            stripe(thisTbl.id, '#ffffff', '#edf3fe');
        }
    }
}

// fading messages
// @see http://www.axentric.com/aside/fat/
var Fat = {
	make_hex : function (r,g,b)
	{
		r = r.toString(16); if (r.length == 1) r = '0' + r;
		g = g.toString(16); if (g.length == 1) g = '0' + g;
		b = b.toString(16); if (b.length == 1) b = '0' + b;
		return "#" + r + g + b;
	},
	fade_all : function ()
	{
	if (!document.getElementsByTagName) return;

	var a = document.getElementsByTagName("*");
		for (var i = 0; i < a.length; i++)
		{
			var o = a[i];
			var r = /fader-?(\w{3,6})?/.exec(o.className);
			if (r)
			{
				if (!r[1]) r[1] = "";
				if (o.id) Fat.fade_element(o.id,null,null,"#"+r[1]);
			}
		}
	},
	fade_element : function (id, fps, duration, from, to)
	{
		if (!fps) fps = 30;
		if (!duration) duration = 3000;
		if (!from || from=="#") from = "#00FF00"; // default color if not specified
		if (!to) to = this.get_bgcolor(id);

		var frames = Math.round(fps * (duration / 1000));
		var interval = duration / frames;
		var delay = interval;
		var frame = 0;

		if (from.length < 7) from += from.substr(1,3);
		if (to.length < 7) to += to.substr(1,3);

		var rf = parseInt(from.substr(1,2),16);
		var gf = parseInt(from.substr(3,2),16);
		var bf = parseInt(from.substr(5,2),16);
		var rt = parseInt(to.substr(1,2),16);
		var gt = parseInt(to.substr(3,2),16);
		var bt = parseInt(to.substr(5,2),16);

		var r,g,b,h;
		while (frame < frames)
		{
			r = Math.floor(rf * ((frames-frame)/frames) + rt * (frame/frames));
			g = Math.floor(gf * ((frames-frame)/frames) + gt * (frame/frames));
			b = Math.floor(bf * ((frames-frame)/frames) + bt * (frame/frames));
			h = this.make_hex(r,g,b);

			setTimeout("Fat.set_bgcolor('"+id+"','"+h+"')", delay);

			frame++;
			delay = interval * frame;
		}

		setTimeout("Fat.set_bgcolor('"+id+"','"+to+"')", delay);
	},
	set_bgcolor : function (id, c)
	{
		var o = document.getElementById(id);
		o.style.backgroundColor = c;
	},
	get_bgcolor : function (id)
	{
		var o = document.getElementById(id);
		while(o)
		{
			var c;
			if (window.getComputedStyle) c = window.getComputedStyle(o,null).getPropertyValue("background-color");
			if (o.currentStyle) c = o.currentStyle.backgroundColor;
			if ((c != "" && c != "transparent") || o.tagName == "BODY") { break; }
			o = o.parentNode;
		}
		if (c == "" || c == "transparent") c = "#FFFFFF";
		var rgb = c.match(/rgb\s*\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*\)/);
		if (rgb) c = this.make_hex(parseInt(rgb[1]),parseInt(rgb[2]),parseInt(rgb[3]));
		return c;
	}
}

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";
		}
	}
}

/** NAVIGATION */

function launch_livehelp()
{
	request_url = "http://livesupport.learningstation.com/request.php?l=liveadmin&x=1&deptid=3&page=" +  escape(location.toString());
	newwin = window.open( request_url, 'livehelp', 'scrollbars=no,menubar=no,resizable=0,location=no,screenX=50,screenY=100,width=450,height=360' ) ;
	newwin.focus() ;
	return false;
}

function disable_button(buttonid)
{
	try
	{
		var oButton = document.getElementById(buttonid);
		if(oButton)
		{
			oButton.value="Processing, please wait...";
			oButton.disabled=true;
		}
	}
	catch(_e)
	{
		return false;
	}
}

function simpleWindow(sUrl,sWinName,sParams) {
    simpleWin = window.open(sUrl,sWinName,sParams);
    if (simpleWin.opener == null) simpleWin.opener = self;
}

function confirmToUrl(message,to)
{
	if(confirm(message))
		self.location.href=to;
	return false;
}

/** BROWSER BEHAVIOUR */

// here we fix the focus selector in IE
sfFocus = function() {
	var sfEls = document.getElementsByTagName("INPUT");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onfocus=function() {
			this.className+=" sffocus";
		}
		sfEls[i].onblur=function() {
			this.className=this.className.replace(new RegExp(" sffocus\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfFocus);


// page loading and error handling

function stopError() {
  return true;
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function addUnloadEvent(func) {
  var oldonunload = window.onunload;
  if (typeof window.onunload != 'function') {
    window.onunload = func;
  } else {
    window.onunload = function() {
      oldonunload();
      func();
    }
  }
}



/** PAGE LOAD EVENTS */

addLoadEvent(formFocus);
addLoadEvent(findZebras);
addLoadEvent(Fat.fade_all);
addLoadEvent(externalLinks);

window.onerror = stopError;