/*
	Exweb Login JS
	Mike Hearfield
	November 2006
*/

	strText = "";
	strAd = "Home";
	//Advertising random number generators
	var intRandom = Math.round(Math.random()*1000000);
	var intRandom2 = Math.round(Math.random()*1000000);
	
	//====================================================================
	function getNextSibling(startBrother){
		endBrother=startBrother.nextSibling;
		while(endBrother.nodeType!=1){
			endBrother = endBrother.nextSibling;
		}
		return endBrother;
	}
	//====================================================================

	//====================================================================
	// Adds event to window.onload without overwriting currently 
	// assigned onload functions.
	function addLoadEvent(func){  
		var oldonload = window.onload;
			if (typeof window.onload != 'function') {
					window.onload = func;
			}else{
					window.onload = function(){
					        if (oldonload !=null)
					        {
							oldonload();
							}
							func();
					}
			}
	}
	//====================================================================

	//====================================================================
	function $(id){
		var d = document.getElementById(id);
		return d;
	}
	//====================================================================
	
	//====================================================================
	function addClass(obj,cls){
		obj.className = obj.className+" "+cls;
	}
	
	function removeClass(obj,cls){
		obj.className = obj.className.replace(cls,"");
	}

	function toggleClass(obj,cls){
		var string = obj.className;
		var cond1 = (string == cls); // it's the only thing
		var cond2 = (string.indexOf(" "+cls+" ") != -1); // it's in there
		var cond3 = (string.indexOf(cls+" ") == 0); // it's first
		var cond4 = (string.lastIndexOf(" "+cls) == string.length-cls.length); // it's last
		if( cond1 || cond2 || cond3 || cond4){
			string = string.replace(cls,"");
		}else{
			string = string+" "+cls;
		}
		obj.className = string;
	}
	//====================================================================
	
	//====================================================================
	function toggleBlock(id){
		if($(id).style.display != "block")
			$(id).style.display = "block";
		else
			$(id).style.display = "none";
	}
	//====================================================================
	
	//====================================================================
	function centreBlock(id){
		$(id).style.top = "150px";
		var pos = (document.body.offsetWidth/2) - ($(id).offsetWidth/2);
		$(id).style.left = pos + "px";
	}
	//====================================================================
	
	//====================================================================
	function showMe(id){
		$(id).className = "expand";
	}
	function hideMe(id){
		$(id).className = "collapse";
	}
	function toggleMe(id){
		if($(id).className == "expand")
			hideMe(id);
		else
			showMe(id);
	}
	
	function toggleName(thing){
		if(thing.innerHTML.indexOf("[+]") != -1){
			thing.innerHTML = thing.innerHTML.replace("[+]","[-]");
		}else{
			if(thing.innerHTML.indexOf("[-]") != -1){
				thing.innerHTML = thing.innerHTML.replace("[-]","[+]");
			}				
		}		
	}
	//====================================================================

	//====================================================================
	function focusOnFirstFieldIn(id){
		var inps = $(id).getElementsByTagName("INPUT");
		for(var i=0, j=inps.length; i<j; i++){
			if(inps[i].type != "hidden"){
				inps[i].focus();
				break;
			}
		}
	}
	
	function toggleForm(id){
		toggleMe(id);
		if($(id).className == "expand"){
			focusOnFirstFieldIn(id);
		}
	}
	//====================================================================

	//====================================================================
	function setupDTs(id){
		var dta = $(id).getElementsByTagName("dt");
		for(var i=0, j=dta.length; i<j; i++){			
			dta[i].onmouseover = function(){addClass(this,"hover");};
			dta[i].onmouseout = function(){removeClass(this,"hover");};
			dta[i].onclick = function(){toggleMe(getNextSibling(this).id);toggleName(this);};
			dta[i].setAttribute("title","Click here to expand / collapse this section.");			
			if(dta[i].className != "expand")
				hideMe(getNextSibling(dta[i]).id);
			toggleName(dta[i]);
		}
	}
	//====================================================================

	//====================================================================
	function hoverLIs(id){
		var lia = $(id).getElementsByTagName("li");
		for(var i=0, j=lia.length; i<j; i++){			
			lia[i].onmouseover = function(){addClass(this,"hover");};
			lia[i].onmouseout = function(){removeClass(this,"hover");};
		}
	}
	//====================================================================

	//==================================================================== EXPAND A THING
	function prepObjForSliding(objId){
		obj = $(objId);
		obj.style.height = obj.clientHeight + 'px';
		obj.style.display = 'none';
	}
	
	function toggleSlide(objId, options){
		var obj = $(objId);
		if(obj.style.display != 'block'){
			Slide(objId).down(options);
			//focusOnFirstFieldIn(objId);
		}else{
			Slide(objId).up(options);
		}
	}
	
	//from http://firblitz.com/2007/3/6/re-how-to-create-digg-comment-style-sliding-divs-with-javascript-and-css
	var slideInUse = new Array();
	
	function Slide(objId, options) {
		this.obj = $(objId);
		this.duration = 1;
		this.height = parseInt(this.obj.style.height);
	
		if(typeof options != 'undefined') { this.options = options; } else { this.options = {}; }
		if(this.options.duration) { this.duration = this.options.duration; }
			
		this.up = function() {
			this.curHeight = this.height;
			this.newHeight = '1';
			if(slideInUse[objId] != true) {
				var finishTime = this.slide();
				window.setTimeout("Slide('"+objId+"').finishup("+this.height+");",finishTime);
			}
		}
		
		this.down = function() {
			this.newHeight = this.height;
			this.curHeight = '1';
			if(slideInUse[objId] != true) {
				this.obj.style.height = '1px';
				this.obj.style.display = 'block';
				this.slide();
			}
		}
		
		this.slide = function() {
			slideInUse[objId] = true;
			var frames = 30 * duration; // Running at 30 fps
	
			var tIncrement = (duration*1000) / frames;
			tIncrement = Math.round(tIncrement);
			var sIncrement = (this.curHeight-this.newHeight) / frames;
	
			var frameSizes = new Array();
			for(var i=0; i < frames; i++) {
				if(i < frames/2) {
					frameSizes[i] = (sIncrement * (i/frames))*4;
				} else {
					frameSizes[i] = (sIncrement * (1-(i/frames)))*4;
				}
			}
			
			for(var i=0; i < frames; i++) {
				this.curHeight = this.curHeight - frameSizes[i];
				window.setTimeout("document.getElementById('"+objId+"').style.height='"+Math.round(this.curHeight)+"px';",tIncrement * i);
			}
			
			window.setTimeout("delete(slideInUse['"+objId+"']);",tIncrement * i);
			
			if(this.options.onComplete) {
				window.setTimeout(this.options.onComplete, tIncrement * (i-2));
			}
			
			return tIncrement * i;
		}
		
		this.finishup = function(height) {
			this.obj.style.display = 'none';
			this.obj.style.height = height + 'px';
		}
		
		return this;
	}
	//====================================================================

	//==================================================================== TRIM
	String.prototype.trim = function() {
		a = this.replace(/^\s+/, '');
		return a.replace(/\s+$/, '');
	}
	//====================================================================
	function LaunchPopup(popupId)	{
	    var strPopupId = 'div' + popupId;
	    toggleBlock(strPopupId); 
	    centreBlock(strPopupId);
	}
	//====================================================================

	//====================================================================
	function ClosePopup(popupId)	{
		 var strPopupId = 'div' + popupId;
		toggleBlock(strPopupId); 
	}
	//====================================================================

