/*
Created by: Marta Salek, Proper Systems Pty Ltd
Date created: 2/02/2006 11:55 AM
Last modified:
    17/02/2006 9:30 AM - Added setDivs for three column layout format and heights of divs
    7/03/2006 3:30 PM - Added setScrollD, scrollToD and initD functions - used to  maintain div scroll position as well as window scroll position
    20/05/2006 11:12 AM - Added the setGlobalWait function
    29/05/2006 11:12 AM - Added the windowSize, setTextBoxWidth, chkNoChars functions
    31/05/2006 10:44 AM - Added trim function
*/

var l, t, w, h, lt, lw;
var ex = 25;
lw = 0; w = 0;
var cur_td = 0;
var pmenu = "off";
var selObj, selObj1;
var rte_suff;
var rte = false;
var tt_ex;
var t_off = 8;
var l_off = 0;
var t_marg = 20;
var dvObj;
var lw = 400;   /* Width of left div for textbox pairs, plus extra 100 px for left and right large divs and margins, etc*/
var lm = 100;
var th = 25;
var lh = 14;
var em = 2;
var regEm = /^\+?[a-z0-9](([-+.]|[_]+)?[a-z0-9]+)*@([a-z0-9]+(\.|\-))+[a-z]{2,6}$/i;
var regWeb = /^(http(s?):\/\/)?([0-9a-zA-Z\-~_]+\.)+[a-zA-Z]{2,6}(\:[0-9]+)?(\/\S*)?$/i;
try {
	var oPopup = window.createPopup();
	t_off = 25; // Get here, must be running IE
	l_off = 10;
}
catch (e) {}

function initLoad(so, so1, rte_bool, rte_txt) {
    if (so && so != "") 
        selObj = document.getElementById(so);
    if (so1 && so1 != "") 
        selObj1 = document.getElementById(so1);

    if (rte_txt) {
        rte = rte_bool;
        rte_suff = rte_txt;
    }
     
    if (document.attachEvent) {
        document.attachEvent("onmousemove", updateIt);
		document.attachEvent("onscroll", setScroll);
		document.attachEvent("onkeypress", setScroll);
		document.attachEvent("onclick", setScroll);
	}
	else if (document.addEventListener) {
	    document.addEventListener("mousemove", updateIt_arg, false);
		document.addEventListener("scroll", setScroll, false);
		document.addEventListener("keypress", setScroll, false);
		document.addEventListener("click", setScroll, false);
	}
	scrollTo();
}

function initD(dv, so, so1) {
    dvObj = document.getElementById(dv);
    if (so && so != "") 
        selObj = document.getElementById(so);
    if (so1 && so1 != "") 
        selObj1 = document.getElementById(so1);
            
    if (document.attachEvent) {
        document.attachEvent("onmousemove", updateIt);
	    document.attachEvent("onscroll", setScrollD);
	    document.attachEvent("onkeypress", setScrollD);
	    document.attachEvent("onclick", setScrollD);
    }
    else if (document.addEventListener) {
        document.addEventListener("mousemove", updateIt_arg, false);
	    document.addEventListener("scroll", setScrollD, false);
	    document.addEventListener("keypress", setScrollD, false);
	    document.addEventListener("click", setScrollD, false);
    }
    scrollToD();
}

function contextMenu(mn,c,r) {
	hidePopup();
	l = findPosX(document.getElementById("td_m"+mn)) + l_off;
	lt = findPosY(document.getElementById("td_m"+mn));
	t = lt + document.getElementById("td_m"+mn).offsetHeight + t_off;
	document.getElementById("td_m"+mn).className = "topMenuOver";
	w = c*6+20;						//Expected width of text
	h = r*14+20;
	lw = document.getElementById("td_m"+mn).offsetWidth;
	lw = Math.max(w,lw);					// Get max width of popup and cell, so when exceed cell width, close popup

	var divObj = document.getElementById("cm" + mn);
	divObj.style.left = l + "px";
	divObj.style.top = t + "px";
	divObj.style.width = w + "px";
	divObj.style.height = h + "px";
	divObj.style.visibility = "visible";
	divObj.style.cursor = "pointer";
	pmenu = "on";
	cur_td = mn;
	
	l = findPosX(divObj);
	t = findPosY(divObj);
	
	if (selObj && checkMenuInt(selObj)) {selObj.style.visibility = "hidden";}
	if (selObj1 && checkMenuInt(selObj1)) {selObj1.style.visibility = "hidden";}
	
    if (rte) {
        /* Only hide select if menu is on it*/
        /*
        var obj;
        obj = document.getElementById("formatblock_" + rte_suff);
        if (checkMenuInt(obj))    obj.style.visibility = "hidden";
        obj = document.getElementById("fontname_" + rte_suff);
        if (checkMenuInt(obj))    obj.style.visibility = "hidden";
        obj = document.getElementById("fontsize_" + rte_suff);
        if (checkMenuInt(obj))    obj.style.visibility = "hidden";        
        */
        /* Hide all selects when menu is on one*/
        try {       
            var obj1 = document.getElementById("formatblock_" + rte_suff);
            var obj2 = document.getElementById("fontname_" + rte_suff);
            var obj3 = document.getElementById("fontsize_" + rte_suff);
            if (checkMenuIntRTE(obj1) || checkMenuIntRTE(obj2) || checkMenuIntRTE(obj3)) {
                obj1.style.visibility = "hidden";
                obj2.style.visibility = "hidden";
                obj3.style.visibility = "hidden";
            }
        }
        catch (e) {}
	}	
}

function checkMenuInt(obj) {
    var st = findPosY(obj) + t_off - 4;
    var sl = findPosX(obj) + l_off;
    var sw = obj.offsetWidth;
    // If left of select is in popup, or right of select is in popup or popup width is entirely within select
    // AND if top of popup is in select - hide select and label
    return (((sl > l && sl <= l + w) || (sl + sw > l && sl + sw <= l + w) || (l > sl && l + w <= sl + sw)) && ((st >= t && st < t + h)));
}

function checkMenuIntRTE(obj) {
    var st = findPosY(obj);
    var sl = findPosX(obj);
    var sw = obj.offsetWidth;
    // If left of select is in popup, or right of select is in popup or popup width is entirely within select
    // AND if top of popup is in select - hide select and label
    return (((sl > l && sl <= l + w) || (sl + sw > l && sl + sw <= l + w) || (l > sl && l + w <= sl + sw)) && ((st >= t && st < t + h)));
}

function updateIt() { //IE, Opera
	if (tt_ex) showTT("");
	if (pmenu == "off") return;

	var x = window.event.clientX + document.documentElement.scrollLeft;
	var y = window.event.clientY + document.documentElement.scrollTop;
	
	if (x > l+lw || x < l) hidePopup();
	else if (y > t+h || y < lt) hidePopup();
}

function updateIt_arg(event) { // Firefox
	if (tt_ex) showTT("");
	if (pmenu == "off") return;

	var x = event.clientX + document.documentElement.scrollLeft;
	var y = event.clientY + document.documentElement.scrollTop;
	
	if (x > l+lw || x < l) hidePopup();
	else if (y > t+h || y < lt) hidePopup();
	
}

function hidePopup() {
	pmenu = "off";
	if (cur_td != 0) {
		document.getElementById("cm"+cur_td).style.visibility = "hidden";
		document.getElementById("td_m"+cur_td).className = "topMenu";
		cur_td = 0;
		if (selObj) {selObj.style.visibility = "visible";}
		if (selObj1) { selObj1.style.visibility = "visible";}
		
		if (rte) {
		    try {
                document.getElementById("formatblock_" + rte_suff).style.visibility = "visible";
                document.getElementById("fontname_" + rte_suff).style.visibility = "visible";
                document.getElementById("fontsize_" + rte_suff).style.visibility = "visible";
            }
            catch (e) {}
        }
	}	
}

function findPosX(obj) {
	var curLeft = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
      			curLeft += obj.offsetLeft;
       			obj = obj.offsetParent; 
       		}
       	}
	else if (obj.x)
		curLeft += obj.x;

	return curLeft;
}

function findPosY(obj) {
	var curTop = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curTop += obj.offsetTop;
			obj = obj.offsetParent; }
		}
	else if (obj.y) 
		curTop += obj.y;
	
	return curTop;
}

function setLocation(url_nm) {
    window.location = url_nm;
}

function writeFrame(val, obj_nm) {
    var ifrm = frames[obj_nm].document;
    ifrm.open();
    ifrm.write(unescape(val));
    ifrm.close();
}

function setScroll() {
	document.getElementById("scrollX").value = document.documentElement.scrollLeft;
    document.getElementById("scrollY").value = document.documentElement.scrollTop;
    //alert(document.getElementById("scrollX").value + " " + document.getElementById("scrollY").value);
}

function scrollTo() { 
var x, y;
    x = document.getElementById("scrollX").value;
    y = document.getElementById("scrollY").value;
    window.scroll(x, y);
}

function setScrollD() {
	document.getElementById("scrollXD").value = dvObj.scrollLeft;
    document.getElementById("scrollYD").value = dvObj.scrollTop;
    document.getElementById("scrollX").value = document.documentElement.scrollLeft;
    document.getElementById("scrollY").value = document.documentElement.scrollTop;
    //alert(document.getElementById("scrollX").value + " " + document.getElementById("scrollY").value);
}

function scrollToD() {
var x, y;
    x = document.getElementById("scrollXD").value;
    y = document.getElementById("scrollYD").value;
    dvObj.scrollLeft = x;
    dvObj.scrollTop = y;
    
    x = document.getElementById("scrollX").value;
    y = document.getElementById("scrollY").value;
    window.scroll(x, y);
}

function setDivs(l, r, div) {
    var lDiv = document.getElementById("left_d");
    var cDiv = document.getElementById("centre_d");
    var rDiv = document.getElementById("right_d");
    cDiv.style.marginRight = (r + 10) + "px";
    cDiv.style.marginLeft = (l + 5) + "px";
    var divTop = 15 + findPosY(cDiv) * 1;
    lDiv.style.top = divTop + "px";
    rDiv.style.top = divTop + "px";
    /*rDiv.style.top = "0px";*/
    lDiv.style.width = l + "px";
    rDiv.style.width = r + "px";
    var h = (screen.availHeight - divTop - t_marg) * 0.77;
    var divObj = document.getElementById(div);
     divObj.style.height = h + "px";
}
 
function setLblUI(l, tl) {
    var lDiv = document.getElementById("left_d");
    var cDiv = document.getElementById("centre_d");
    var rDiv = document.getElementById("right_d");
    cDiv.style.marginRight = (r + 15) + "px";
    cDiv.style.marginLeft = (l + 5) + "px";
    var divTop = 15 + findPosY(cDiv) * 1;
    lDiv.style.top = divTop + "px";
    rDiv.style.top = divTop + "px";
    lDiv.style.width = l + "px";
    rDiv.style.width = r + "px";
    var h = (screen.availHeight - divTop - t_marg) * 0.77;
    var divObj = document.getElementById(div);
    divObj.style.height = h + "px";
}

function setGlobalWait () {    /* Require the first rule in stylesheet to be * {cursor: auto }*/
    if (document.styleSheets[0].rules[0])
        document.styleSheets[0].rules[0].style.cursor = 'wait';
    else if (document.styleSheets[0].cssRules[0].style.cursor)
        document.styleSheets[0].cssRules[0].style.cursor = 'wait';
}

function setSpanHeight(spnNm, divNm) {
    document.getElementById(spnNm).style.height = document.getElementById(divNm).offsetHeight + "px"
}
 
function windowSize(flg) { /*http://www.howtocreate.co.uk/tutorials/javascript/browserwindow*/
    var myWidth = 0, myHeight = 0;
    if( typeof( window.innerWidth ) == 'number' ) {
        //Non-IE
        myWidth = window.innerWidth;
        myHeight = window.innerHeight;
    } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
        //IE 6+ in 'standards compliant mode'
        myWidth = document.documentElement.clientWidth;
        myHeight = document.documentElement.clientHeight;
    } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
        //IE 4 compatible, and ie 6 in strict mode
        myWidth = document.body.clientWidth;
        myHeight = document.body.clientHeight;
    }
    return (flg == 'w') ? myWidth : myHeight;
}

function setTextBoxWidth(obj, val) {    /* For label/text box pairs, given that left columns is lw pixels long*/
    var w = windowSize('w');
    var flg = false;
    if (val > w - 80 - lw) {
        /*obj.style.width =  (w - 80) + "px";*/
        flg = true;
    }
    return flg;
}   

function chkNoChars(obj, ml) {
    var count = obj.value.length;
    if(count > ml) 
        obj.value = obj.value.substring(0, ml);
}

function isNumberKey(evt) {
	var code;
	if (evt.keyCode) code = evt.keyCode;
	else if (evt.which) code = evt.which;
	
	if (code > 31 && (code < 48 || code > 57))
		return false;

	return true;
}

function trim(s){
    s = s.replace(/^\s*/,'').replace(/\s*$/, '');
    return s;
}