////////////////////////////////////////////////////////////////////////////////

window.onload = function(){

    enterAsTab();

    var allLinks = document.getElementsByTagName('A');
    var i = 0;
    while (a = allLinks[i++]) {
         a.hideFocus = true;
    }
 
    if ( the('loginform') ) {
        the('loginform').username.value = readCookie("username");
        the('loginform').password.value = readCookie("password");
    }
 
    if (byId('Content')) {
    
        xZone.init();
    }

    if (document.loginform) {
        document.loginform.username.focus();
    }
}

////////////////////////////////////////////////////////////////////////////////

var D = document,
  DOM = (D.getElementById && D.createElement)?1:0,
  ALL = (D.all)?1:0,
  NS4 = (D.layers)?1:0;

////////////////////////////////////////////////////////////////////////////////
// Grab any element by id, or form field name (if no id found)
function the(s) {
    var nResult;

    function _n4o(s,i){
    // ^ Emulate document.getElementById back to NS4
        var e, o, l = s.layers, c = l.length, t;
        for(e=0;e<c;e++) {
            l[e].id == i       ? o = l[e]         :
            l[e].layers.length ? t = _n4o(l[e],i) : 0;
            t                  ? o = t            : 0;
        }
        return o;
    }

    function _fel(s) {
    // ^ Failsafe, check form fields names if nothing else was found
        for (iForm=0, lForm=D.forms.length; iForm<lForm; iForm++){
            for(iField=0, lField = D.forms[iForm].elements.length; iField<lField; iField++){
                if (s == D.forms[iForm].elements[iField].name) { return D.forms[iForm].elements[iField] };
            }
        }
    }

    nResult = DOM ? D.getElementById(s) :
              ALL ? D.all[s]            :
              NS4 ? _n4o(d,s)           : null;
    
    return (nResult) ? nResult : _fel(s) ? _fel(s) : null;
}
////////////////////////////////////////////////////////////////////////////////

var D = document,
    A = navigator.userAgent;

// LIBRARY ////////////////////////////////////////////////////////////////////

//Cookie script - Scott Andrew
//http://sivamdesign.com/scripts/
function newCookie(name,value,days) {
    var days = 365;   // the number at the left reflects the number of days for the cookie to last
                 // modify it according to your needs
    if (days) {
       var date = new Date();
       date.setTime(date.getTime()+(days*24*60*60*1000));
       var expires = "; expires="+date.toGMTString();
    } else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
    var nameSG = name + "=";
    var nuller = '';
    if (document.cookie.indexOf(nameSG) == -1) return nuller;
    
    var ca = document.cookie.split(';');
    for(var i=0; i<ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameSG) == 0) return c.substring(nameSG.length,c.length);
    }
    return null;
}

function eraseCookie(name) {
  newCookie(name,"",1);
}

function toMem() {
    newCookie('username', the('loginform').username.value);   // add a new cookie as shown at left for every
    newCookie('password', the('loginform').password.value);   // field you wish to have the script remember
//    the('loginform').password.type = 'text';
    the('loginform').submit();
//    return false;
}


function delMem(a) {
   eraseCookie('username');
   eraseCookie('password');
   document.form.username.value = '';
   document.form.password.value = '';
}




enterAsTab = function(){ //v1.0
    function next(e){
        var l, i, f, j, o = e.target;
        if(e.key == 13 && !/textarea|select/i.test(o.type)){
            for(i = l = (f = o.form.elements).length; f[--i] != o;);
            for(j = i; (j = (j + 1) % l) != i && (!f[j].type || f[j].disabled || f[j].readOnly););
            e.preventDefault(), j != i && f[j].focus();
        }
    }
    for(var f, i = (f = document.forms).length; i; addEvent(f[--i], "keypress", next));
};

addEvent = function(o, e, f, s){
    var r = o[r = "_" + (e = "on" + e)] = o[r] || (o[e] ? [[o[e], o]] : []), a, c, d;
    r[r.length] = [f, s || o], o[e] = function(e){
        try{
            (e = e || event).preventDefault || (e.preventDefault = function(){e.returnValue = false;});
            e.stopPropagation || (e.stopPropagation = function(){e.cancelBubble = true;});
            e.target || (e.target = e.srcElement || null);
            e.key = (e.which + 1 || e.keyCode + 1) - 1 || 0;
        }catch(f){}
        for(d = 1, f = r.length; f; r[--f] && (a = r[f][0], o = r[f][1], a.call ? c = a.call(o, e) : (o._ = a, c = o._(e), o._ = null), d &= c !== false));
        return e = null, !!d;
    }
};

removeEvent = function(o, e, f, s){
    for(var i = (e = o["_on" + e] || []).length; i;)
        if(e[--i] && e[i][0] == f && (s || o) == e[i][1])
            return delete e[i];
    return false;
};

function byId(s)    { return (typeof s == 'string') ? D.getElementById(s) : s; };

function byTag(s,n) {
	s = s.toUpperCase();
	var cNode = byId(n).getElementsByTagName(s);
	var iNode = cNode.length;
	var aNode = [];
//	for (var iNode=0,lNode=cNode.length;iNode<lNode;iNode++) {
	while(iNode--) {
		aNode.push(cNode[iNode]);
	}
	return aNode;
};

function addClass(nElement,sClass) {
	killClass(nElement,sClass);
	return nElement && (
		nElement.className += (nElement.className.length > 0 ? ' ' : '' ) + sClass
	);
}
function killClass(nElement,sClass) {
	nElement && nElement.className && (
		nElement.className = nElement.className.replace(
			new RegExp("^"+sClass+"\\b\\s*|\\s*\\b"+sClass+"\\b",'g'),''
		)
	);
	return true;
}
function hasClass(nElement,sClass) {
	return (!nElement || !nElement.className) ? false
	: (new RegExp("\\b"+sClass+"\\b")).test(nElement.className);
};

// http://developer-test.mozilla.org/docs/Core_JavaScript_1.5_Reference:Objects:Array:filter
if (!Array.prototype.filter) {
	Array.prototype.filter = function (f, obj) {
		var l = this.length;	// must be fixed during loop... see docs
		var res = [];
		for (var i = 0; i < l; i++) {
			if (f.call(obj, this[i], i, this)) {
				res.push(this[i]);
			}
		}
		return res;
	};
}

// http://developer-test.mozilla.org/docs/Core_JavaScript_1.5_Reference:Objects:Array:forEach
if (!Array.prototype.forEach) {
	Array.prototype.forEach = function (f, obj) {
		var l = this.length;	// must be fixed during loop... see docs
//		var i = 0;
//		for (var i = 0; i < l; i++) {
//			f.call(obj, this[i], i, this);
//		}
// ^ slow crap, why not:
		while (l--) {
			f.call(obj, this[l], l, this);
		}
	};
}

function isSection(element, index, array) {
  return hasClass(element,'Section');
}

/******************************************************************************

   xZone allows an array of links (a href) to control
     display of an array of zones (divs)

*/

var xZone = {
	aZones : [],
	iZone  : 0,
	aLinks : [],
	iLink  : 0,
    nShown : null
};

xZone.update = function(nTarget){

	var  iZone = xZone.iZone,
   	    aZones = xZone.aZones,
		aLinks = xZone.aLinks;

	while(iZone--) {
		var nZone = aZones[iZone];
		var nLink = aLinks[iZone];
		if ( nZone.id == nTarget._vZone.id ) {
			nZone.style.display = 'block';
            addClass(nLink,'Selected');
		} else {
			nZone.style.display = 'none';
            killClass(nLink,'Selected');
            killClass(nLink,'Selected');
            killClass(nLink,'Selected');
		}
	}
	var nSubtitle = byId('Subtitle');
	nSubtitle.firstChild.data = nTarget.title;

}

xZone.init = function(){

	xZone.aZones = byTag('div','Content').filter(isSection);
	xZone.iZone  = xZone.aZones.length;
	xZone.aLinks = byTag('a','RightMenu');
	xZone.iLink  = xZone.aLinks.length;


	if (xZone.iZone == xZone.iLink) {
		//alert('Zone/Link mismatch ('+ xZone.iZone + '/' + xZone.iLink + ')');
	
    	var hEvents = {
    		onmousedown  : function(){
    			xZone.update(this);
    			return (/Safari/.test(A)) ? true  : false;
    		}, 
    		// ^ '1' for saf mouse up / '0' stops ff text-drag 
    		onclick      : function(){ return false; },
    		// ^ prevents the href from firing
    		onfocus      : function(){ this.blur(); },
    		// ^ hides dotted link border
    		onmouseup    : function(){ return true; },
    		// ^ restores expected functionality
    		ondblclick   : function(){ return false; },
    		// ^ prevents ie/ff from selecting words
    		unselectable : 'on'
    		// ^ ie6-xp (blocks div drag)
    	};
    	
    	var i = xZone.iZone;
    	while(i--){
    		var nZone = xZone.aZones[i];
    		var nLink = xZone.aLinks[i];
    		nLink._vZone = nZone;
    		for (var iEvent in hEvents) {
    			nLink[iEvent] = hEvents[iEvent];
    		}
    	}
	}
}

function getStyle(el,styleProp)
{
	var x = document.getElementById(el);
	if (x.currentStyle)
		var y = x.currentStyle[styleProp];
	else if (window.getComputedStyle)
		var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(styleProp);
	return y;
}

function adjustContentDiv() {
    var WindowH = getWinY() - 135;
    var ContentH = byId('Content').offsetHeight;

    try {
        if (navigator.appName == 'Microsoft Internet Explorer') {
            byId('Content').style.height = WindowH + 'px';
        } else {
            byId('Content').style.minHeight = WindowH + 'px';
        }
    } catch (e) { };
}