var days = ["NA","MO","TU","WE","TH","FR","SA","SU"];
var ie5=document.all;
var scrolListen;
var callBackCloseAlert;
function ei(id) {
    if (document.getElementById(id)) {
        return document.getElementById(id);
    } else {
        return null;
    }
}

function ce(tag) {
    return document.createElement(tag);
}

function ens(name) {
    return document.getElementsByName(name);
}

function en(name) {
    if (ens(name).length>0) {
        return ens(name)[0];
    } else {
        return null;
    }
}

function ets(tagName) {
    return document.getElementsByTagName(tagName);
}

function executeQry(qry,obj,schema) {
    var queryLang = TrimPath.makeQueryLang(schema);
    var statement = queryLang.parseSQL(qry);
    return statement.filter(obj);
}

function addDigits(val,maxDigits) {
    if (typeof(val)=="string") {
        while (val.length<maxDigits) {
            val = "0" + val;
        }
        return val;
    } else {
        val = ""+val;
        return addDigits(val,maxDigits);
    }
}

function writeConsole(t) {
    if (window.console) {
        //console.info(t)
    }
}
window.onerror = function(e) {
    alert(e);
}

String.prototype.trim=function() {
    var i=0;
    while (this[i]==" ") {
        i++;
    }
    var j=this.length-1;
    while (this[j]==" ") {
        j--;
    }
    return (i==this.length || this.length<1?"":this.substring(i,j+1));
}
Array.prototype.search=function(va) {
    var i=0;
    var ret=[];
    for (i=0;i<this.length;i++) {
        if (this[i]==va || (typeof(this[i])=="string" && this[i].indexOf(va)!=-1)) {
            ret[ret.length]=this[i];
        }
    }
    return ret;
}
Array.prototype.indexOf=function(va,start) {
    var strt=0;
    if (arguments.length>1) {
        if (start==null || typeof(start)!="number") {
            throw "Illegal argument specified for start.cannot be null must be a numeric";
            return;
        } else if (start<0) {
            throw "Illegal argument specified for start.cannot be less than zero";
            return;
        } else if (start>=this.length) {
            throw "Illegal argument specified for start.cannot be greater than array length";
            return;
        } else {
            strt=start;
        }
    }
    var i=0;
    for (i=strt;i<this.length;i++) {
        if (this[i]==va) {
            return i;
        }
    }
    return -1;
}

function showLoad() {
    ei('pointer').style.display="block";
    document.onmousemove= function mousemoveloading(event) {
        e=event;
        if (!e) {
            e=window.event;
        }
        var x=e.clientX;
        var y=e.clientY;
        var dv = ei('pointer');
        dv.style.left=""+(x-8);
        dv.style.top=""+y;
    }
}

function hideLoad() {
    document.onmousemove=null;
    ei('pointer').style.display="none";
}

function getAbsolutePos(el) {
    var SL = 0, ST = 0;
    var is_div = /^div$/i.test(el.tagName);
    if (is_div && el.scrollLeft)
        SL = el.scrollLeft;
    if (is_div && el.scrollTop)
        ST = el.scrollTop;
    var r = { x: el.offsetLeft - SL, y: el.offsetTop - ST };
    if (el.offsetParent) {
        var tmp = getAbsolutePos(el.offsetParent);
        r.x += tmp.x;
        r.y += tmp.y;
    }
    return r;
}

function setPositions(elem,e) {
    var rightedge;
    if (ie5) {
        rightedge = document.body.clientWidth-event.clientX;
    } else {
        rightedge = window.innerWidth-e.clientX;
    }
    var bottomedge;
    if (ie5) {
        bottomedge = document.body.clientHeight-event.clientY;
    } else {
        bottomedge = window.innerHeight-e.clientY;
    }
    
    //if the horizontal distance isn't enough to accomodate the width of the context menu
    if (rightedge < elem.offsetWidth) {
        //move the horizontal position of the menu to the left by it's width
        if(ie5) {
            elem.style.left = ""+(document.body.scrollLeft+event.clientX-elem.offsetWidth-2)+"px";
        } else {
            elem.style.left = ""+(window.pageXOffset+e.clientX-elem.offsetWidth-2)+"px";
        }
    } else {
        //position the horizontal position of the menu where the mouse was clicked
        if (ie5) {
            elem.style.left = ""+(document.body.scrollLeft+event.clientX+2)+"px";
        } else {
            elem.style.left = ""+(window.pageXOffset+e.clientX+2)+"px";
        }
    }
    //same concept with the vertical position
    if (bottomedge < elem.offsetHeight) {
        if (ie5) {
            elem.style.top = ""+(document.body.scrollTop+event.clientY-elem.offsetHeight-2)+"px";
        } else {
            elem.style.top = ""+(window.pageYOffset+e.clientY-elem.offsetHeight-2)+"px";
        }
    } else {
        if (ie5) {
            elem.style.top = ""+(document.body.scrollTop+event.clientY+2)+"px";
        } else {
            elem.style.top = ""+(window.pageYOffset+e.clientY+2)+"px";
        }
    }
}
function getMessage() {
    if (arguments.length<1) {
        return null;
    }
    var tmp = "var val=messages."+arguments[0];
    eval(tmp);
    var d=1;
    for (d=1;d<arguments.length;d++) {
        val=val.replace("{"+(d-1)+"}",arguments[d]);
    }
    return val;
}
function hidealertbox() {
    ei('hiddenoverlay').style.display="none";
    ei('confirmboxok').style.display="none";
    ei('mainAlert').style.display="none";
    window.onscroll=function() {};
    if (callBackCloseAlert!=null) {
        if(typeof(callBackCloseAlert)=="function") {
            callBackCloseAlert();
        } else {
            try {
                eval(callBackCloseAlert);
            } catch (e) {
            }
        }
    }
}

function clickedOK() {
    hidealertbox();
    if (confirmboxcallbackfunction!=null) {
        if(typeof(confirmboxcallbackfunction)=="function") {
            confirmboxcallbackfunction();
        } else {
            try {
                eval(confirmboxcallbackfunction);
            } catch (e) {
            }
        }
    }
}

function confirmbox(msg,callbackfnOK,callBackCancel) {
    confirmboxcallbackfunction=callbackfnOK;
    ei('confirmboxok').style.display="block";
    ei('confirmboxok').onclick=callbackfnOK;
    alertbox(msg,callBackCancel);
}

function checkSetAllignment(elmId) {
    var scrollY=(ie5?document.body.scrollTop:window.pageYOffset);
    var scrollX=(ie5?document.body.scrollLeft:window.pageXOffset);
    var screenHeight = (document.all?document.body.clientHeight:window.innerHeight);
    var screenWidth = (document.all?document.body.clientWidth:window.innerWidth);
    var alb = ei(elmId);
    alb.style.top = ""+(scrollY+(screenHeight/2)-(alb.clientHeight/2))+"px"
    alb.style.left = ""+(scrollX+(screenWidth/2)-(alb.clientWidth/2))+"px"
}

function alertbox(msg, respFunc) {
    var ovrlay = ei('hiddenoverlay');
    if (ovrlay==null) {
        ovrlay = ce("div");
        ovrlay.className="overlayhiddiv";
        ovrlay.setAttribute("id","hiddenoverlay");
        ovrlay.id="hiddenoverlay";
        ets("body")[0].appendChild(ovrlay);
        ovrlay = ei('hiddenoverlay');
    }
    var totalHeight = (ie5?document.body.scrollHeight:document.height);
    var totalwidth = (ie5?document.body.scrollWidth:document.width);
    var scrollY=(ie5?document.body.scrollTop:window.pageYOffset);
    var scrollX=(ie5?document.body.scrollLeft:window.pageXOffset);
    var screenHeight = (document.all?document.body.clientHeight:window.innerHeight);
    var screenWidth = (document.all?document.body.clientWidth:window.innerWidth);
    
    ovrlay.style.height=totalHeight + "px";
    ovrlay.style.width=totalwidth + "px";
    ovrlay.style.display="block";
    ovrlay.style.zIndex="1000";
    if (respFunc!=null && (typeof(respFunc)=="function" || typeof(respFunc)=="string")) {
        ei('closeAlert').onclick=respFunc;
        ei('closeAlertImage').onclick=respFunc;
        callBackCloseAlert = respFunc;
    } else {
        ei('closeAlert').onclick=hidealertbox;
        ei('closeAlertImage').onclick=hidealertbox;
        callBackCloseAlert = null;
    }
    ei('contentAlert').innerHTML=msg;
    var alb = ei('mainAlert');
    alb.style.display="block";
    alb.style.zIndex="1010";
    alb.style.top = ""+(scrollY+(screenHeight/2)-(alb.clientHeight/2))+"px"
    alb.style.left = ""+(scrollX+(screenWidth/2)-(alb.clientWidth/2))+"px"
    window.onscroll = function () {
        checkSetAllignment('mainAlert');
    }
    window.onresize = function () {
        checkSetAllignment('mainAlert');
    }
    ei('closeAlert').focus();
}

function hideLoading() {
    ei('hiddenoverlay').style.display="none";
    ei('loadingDiv').style.display="none";
}

function showLoading() {
    var ovrlay = ei('hiddenoverlay');
    if (ovrlay==null) {
        ovrlay = ce("div");
        ovrlay.className="overlayhiddiv";
        ovrlay.setAttribute("id","hiddenoverlay");
        ovrlay.id="hiddenoverlay";
        ets("body")[0].appendChild(ovrlay);
        ovrlay = ei('hiddenoverlay');
    }
    var totalHeight = (ie5?document.body.scrollHeight:document.height);
    var totalwidth = (ie5?document.body.scrollWidth:document.width);
    var scrollY=(ie5?document.body.scrollTop:window.pageYOffset);
    var scrollX=(ie5?document.body.scrollLeft:window.pageXOffset);
    var screenHeight = (document.all?document.body.clientHeight:window.innerHeight);
    var screenWidth = (document.all?document.body.clientWidth:window.innerWidth);
    
    ovrlay.style.height=totalHeight + "px";
    ovrlay.style.width=totalwidth + "px";
    ovrlay.style.display="block";
    ovrlay.style.zIndex="1000";
    
    var alb = ei('loadingDiv');
    alb.style.display="block";
    alb.style.zIndex="1010";
    alb.style.top = ""+(scrollY+(screenHeight/2)-(alb.clientHeight/2))+"px"
    alb.style.left = ""+(scrollX+(screenWidth/2)-(alb.clientWidth/2))+"px"
    window.onscroll = function () {
        checkSetAllignment('loadingDiv');
    }
    window.onresize = function () {
        checkSetAllignment('loadingDiv');
    }
}

function checkNumericKeyInfo(chr, mozChr) {
    var retVal;
    if(mozChr != null) { // Look for a Mozilla-compatible browser
        if((mozChr >= 48 && mozChr <= 57) || mozChr == 0 || chr == 8 || mozChr == 13) {
            retVal = true;
        }
        else {
            retVal = false;
        }
    } else { // Must be an IE-compatible Browser
        if((chr >= 48 && chr <= 57) || chr == 13) {
            retVal = true;
        } else {
            retVal = false;
        }
    }
    return retVal;
}

function removeLeadingValue(val,rem) {
    while (val.substr(0,1)==rem && val.length>1) {
        val=val.substr(1);
    }
    return val;
}
