// Script by Hunter Beanland
// hunter@beanland.net.au
// www.beanland.net.au

function popWindow(loc,popH,popW)
// Create a popup window
// Parses: loc,popH,popW (url, hieght, width)
{
	current_window = window.open(loc, '_blank', 'alwaysRaised=0,resizable,scrollbars=1,location=0,height='+popH+',width='+popW+',top=100,left=100');	
}
function popWindowScroll(loc,popH,popW)
// Create a popup scrollable window
// Parses: loc,popH,popW (url, hieght, width)
{
	current_window = window.open(loc, '_blank', 'alwaysRaised=0,resizable,scrollbars=1,location=0,height='+popH+',width='+popW+',top=100,left=100');	
}
// **** General event traps ****
function errorTrap(msg,url,line)
// Trap all errors and start the error log page
{
	var jsCRLF = String.fromCharCode(13)+String.fromCharCode(10);
	error_window = window.open("/error500.aspx?category=JS&msg="+msg+"&url="+url+"&line="+line, "jserr", "alwaysRaised=0,resizable,scrollbars=0,location=0,height=10,width=50,top="+screen.availHeight+',left='+screen.availWidth);
    alert("Application script caused an error which has been logged"+jsCRLF+"Error: "+msg+jsCRLF+"URL: "+url+jsCRLF+"Line: "+line);
    return true;
}
// Right click traps
function clickIE() {if (document.all) {(message);return false;}}
function clickNS(e) {
	if (document.layers||(document.getElementById&&!document.all)) {
		if (e.which==2||e.which==3) {(message); return false;}
	}
}
function initRightClick() {
// Initialise the right click trap - not for Admin
	if (document.layers) {document.captureEvents(Event.MOUSEDOWN);document.onmousedown=clickNS;}
	else {document.onmouseup=clickNS;document.oncontextmenu=clickIE;}
}
function fieldFocus() {
// Transfer focus to the first field. Call after page is loaded
if (document.forms.length > 0) {
	var field = document.forms[0];
	for (i = 0; i < field.length; i++) {
		if (((field.elements[i].type == "text") || (field.elements[i].type == "textarea")) && (field.elements[i].visible == true))  {
			document.forms[0].elements[i].focus();
			break;
		}
	}
}
}
function trim(s) { 
	return s.replace(/^\s+/,"").replace(/\s+$/,"");
} 
// **** Change tracking functions ****
function initChangeTrack() {
// Track changes to the form
	document.body.onkeydown = keyPressed;
}
function keyPressed(e) {
	formChanged = true;
	if (document.getElementById("lblSaved")) document.getElementById("lblSaved").style.display="none";
}
function checkLoseChanges() {
// Return true if user wishes to lose the changes
	if (formChanged) {
		if (confirm(ScreenArray0[0])) {
			return(true);
		} else {
			return(false);
		}
	} else {
		return(true);
	}
}
function showWait(strID) {
    document.getElementById(strID).innerHTML += ScreenArray0[1];
    document.getElementById(strID).style.display="block";
}
function headerBack() {
// Back button on the main header
	if (checkLoseChanges()) history.go(-1);
}
function headerHome() {
// Home button on the main header
	if (checkLoseChanges()) document.location.href="/main/";
}
function popupHelp()
//Trap F1 and pass the preset HelpPage var to the help page
{
	popWindowScroll("/help/default.asp?page="+HelpPage,400,620);
    return false;
}
// **** TextArea printing helper functions ****
function prepTextAreaPrint() {
    var collTextAreas = document.all.tags("TEXTAREA");
    var oNewDiv;
    var sTemp;
    var i;
    for (i=0; i < collTextAreas.length; i++) {
		oNewDiv = document.createElement("DIV");
		oNewDiv.style.width = collTextAreas(i).clientWidth;
		oNewDiv.style.height = collTextAreas(i).clientHeight;
		oNewDiv.id = collTextAreas(i).uniqueID + "_div";
		sTemp = collTextAreas(i).value.replace(/\n/gi,"<BR>"); 
		sTemp = sTemp.replace(/\s\s/gi,"  "); 
		oNewDiv.innerHTML = sTemp; 
		collTextAreas(i).parentNode.insertBefore(oNewDiv, collTextAreas(i));
		collTextAreas(i).style.display = "none";
		oNewDiv = null;
    }
}
function postTextAreaPrint() {
    var collTextAreas = document.all.tags("TEXTAREA");
    var oDivToRemove;
    var i;
    for (i = 0; i < collTextAreas.length; i++)  {
		oDivToRemove = document.all(collTextAreas(i).uniqueID + "_div");
		if (oDivToRemove != null)	{
			oDivToRemove.removeNode(true);
		}
		collTextAreas(i).style.display = "";
    }
}
function initTextAreaPrint() {
	document.body.onbeforeprint = prepTextAreaPrint;
	document.body.onafterprint = postTextAreaPrint;
}

// **** Cookies ****
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 exp = new Date(); 
	exp.setTime(exp.getTime() + (31*24*60*60*1000)); //31 days
	var argv = SetCookie.arguments;  
	var argc = SetCookie.arguments.length;  
	var expires = (argc > 2) ? argv[2] : exp.toGMTString();  
	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)) + 
	((path == null) ? "" : ("; path=" + path)) +  
	((domain == null) ? "" : ("; domain=" + domain)) +    
      ((secure == true) ? "; secure" : "");
}
// **** Nav box ****
function LeftNavToggle(panelID) {
//Toggle the collapsable menu
    panelContent = document.getElementById(panelID)
    if (panelContent.style.display == "none") {
        panelContent.style.display = "";
        LeftNavPanelMove(panelID,'E');
    } else {
        LeftNavPanelMove(panelID,'C');
    }
}
function LeftNavPanelMove(panelID,modeCE) {
//Move the collapsable menu by one step
    panelContent = document.getElementById(panelID);
    var currHeight = parseInt(panelContent.style.height);
    var maxHeight = panelContent.scrollHeight;
    if (modeCE=="E") {
        if (isNaN(currHeight)) { currHeight = 20; } else { currHeight += 20; }
        if (maxHeight<=1) maxHeight=40;
    } else {
        if (isNaN(currHeight) || currHeight==0) { currHeight = panelContent.offsetHeight-20 } else { currHeight -= 20; }
    }
    panelContent.style.overflow = "hidden";
    if (modeCE=="C" && currHeight <=  0) {
        panelContent.style.height = 0 +"px";
        panelContent.style.display = "none";
        LeftNavResize();
    } else if (modeCE=="E" && currHeight >=  maxHeight) {
        panelContent.style.height = "auto";
        panelContent.style.display = "";
        LeftNavResize();
    } else {
        panelContent.style.height = currHeight +"px";
 		strTimer = "LeftNavPanelMove('" +panelID+ "','" +modeCE+ "')";
 		panelTimerID = setTimeout(strTimer,30);
    }
}

function LeftNavSlide() {
//Toggle the whole of the left menu
    panelContent = document.getElementById("LeftNavPane")
    if (panelContent.style.display == "none") {
        panelContent.style.display = "";
        LeftNavSlideMove(panelContent.id,'E');
    } else {
        LeftNavSlideMove(panelContent.id,'C');
    }
}
function LeftNavSlideMove(panelID,modeCE) {
//slide the nav pane to the left by one step
    panelContent = document.getElementById(panelID);
    panelHeader = document.getElementById(panelID+"Header");
    panelMainContent = document.getElementById("ContentPane");
    var currLeft = parseInt(panelContent.style.left);
    var maxLeft = 150;
    if (modeCE=="E") {
        if (isNaN(currLeft)) { currLeft = 20; } else { currLeft += 20; }
    } else {
        if (isNaN(currLeft)) { currLeft = -20 } else { currLeft -= 20; }
    }
    panelContent.style.overflow = "hidden";
    if (modeCE=="C" && currLeft <= 10-maxLeft) {
        panelContent.style.left = -150 +"px";
        panelContent.style.display = "none";
        panelHeader.style.left = "0px";
        panelMainContent.style.left = "10px";
    } else if (modeCE=="E" && currLeft >=  0) {
        panelContent.style.left = "0px";
        panelContent.style.display = "";
        panelHeader.style.left = "";
        panelMainContent.style.left = (maxLeft+10) + "px";
    } else {
        panelContent.style.left = currLeft +"px";
        panelHeader.style.left = currLeft +"px";
        panelMainContent.style.left = (currLeft+maxLeft+10) +"px";
 		strTimer = "LeftNavSlideMove('" +panelID+ "','" +modeCE+ "')";
 		slideTimerID = setTimeout(strTimer,30);
    }
}
function LeftNavResize() {
// Resize the LeftNav panel to full browser window height or content doc height. Header=47 + Top margin=3 = 50
    var ContentHeight;
    if ((document.getElementById("ContentPane").clientHeight > document.getElementById("LeftNavPane").scrollHeight) || (document.getElementById("LeftNavPane").scrollHeight == 1000)) {
        ContentHeight = document.getElementById("ContentPane").clientHeight;
    } else {
        ContentHeight = document.getElementById("LeftNavPane").scrollHeight;
    }
    if (ContentHeight > document.documentElement.clientHeight-50) {
        document.getElementById("LeftNavPaneHeader").style.height = ContentHeight+3+"px";
        document.getElementById("LeftNavPane").style.height = ContentHeight+3+"px";
    } else {
        var newHeight=document.documentElement.clientHeight-47;
        if (newHeight<25) newHeight=400;
        document.getElementById("LeftNavPaneHeader").style.height = newHeight+"px";
        document.getElementById("LeftNavPane").style.height = newHeight+"px";
    }
}

// Initialise the error trap
onerror=errorTrap;
// Initialise right click dummy command
var message="";
// Initialise the F1 help
window.onhelp=popupHelp;
// Initialise the form change tracker
var formChanged = false;
// Find Browser (Mozilla Engine or Microsoft)
if (document.layers||(document.getElementById&&!document.all)) {
	UserBrowser="MZ";
} else {
	UserBrowser="MS";
}
//window.onscroll = moveLeftNavBox;
// Initialise the session timer

