﻿var m_objValidatePaste = 0,
    m_objControl = null,
    m_intMaxLength = 0;

function setFocus(p_objTextField) {
    var objTextField = document.getElementById(p_objTextField);
    objTextField.focus();
}

function gotoAnchor(p_strAnchorName) {
    window.location.hash = p_strAnchorName;

}

function keyPressed(e) {
    var keynum;

    if (window.event) {
        keynum = e.keyCode;
    }
    else if (e.which) // Netscape/Firefox/Opera
    {
        keynum = e.which;
    }

    return keynum;
}

function toggleNewSubmenu(p_strSubMenu, p_strImage, p_strPath, p_booIE6Browser){//, p_strSubmenuArrows) {
    var objSubMenu = document.getElementById(p_strSubMenu);
    var objImage = document.getElementById(p_strImage);
   
    try {
        if (objSubMenu) {
            if (objSubMenu.className == "hideSubmenu") {
                objSubMenu.className = "showSubmenu";

                if (objImage) {
                    if (p_strPath.length > 0) {
                        objImage.src = p_strPath + 'Up.png';
                    }

                    objImage.title = 'Hide Submenu...';
                }
            }
            else {
                objSubMenu.className = "hideSubmenu";

                if (objImage) {
                    if (p_strPath.length > 0) {
                        objImage.src = p_strPath + 'Down.png';
                    }

                    objImage.title = 'View Submenu...';
                }
            }

            if (p_booIE6Browser) {
                //pngfix();
            
//                if (p_strSubmenuArrows.length > 0) {
//                    var strList = p_strSubmenuArrows.split(',');

//                    alert('list[0] = ' + strList[0]);
//                    alert('list = ' + strList);

//                    if (strList.length > 0) {
//                        for (intIndex = 0; intIndex < strList.length; intIndex++) {
//                            alert('strList[' + intIndex + '] = ' + strList[intIndex]);
//                            fixPng(strList[intIndex]);
//                        }
//                    }
//                }
//                else {
                    fixPng(p_strImage);
//                }
            }

            //positionSearch is a javascript function on all master pages!
            positionSearch();
        }
    }
    catch (Err) {
        alert(Err.description);
    } 
}

function togglesubmenu(p_strSubMenu) {
    var objSubMenu = document.getElementById(p_strSubMenu);
    
    if (objSubMenu != null) {
        if (objSubMenu.style.display == "none") {
            objSubMenu.style.display = "";
        }
        else {
            objSubMenu.style.display = "none";
        }

        //positionSearch is a javascript function on all master pages!
        positionSearch();
    }
}

function MM_preloadImages() { //v3.0
    var d = document;

    if (d.images) {
        if (!d.MM_p)
            d.MM_p = new Array();

        var i,
            j = d.MM_p.length,
            a = MM_preloadImages.arguments;

        for (i = 0; i < a.length; i++)
            if (a[i].indexOf("#") != 0) {
            d.MM_p[j] = new Image;
            d.MM_p[j++].src = a[i];
        }
    }
}

function MM_swapImgRestore() { //v3.0
    var i,
        x,
        a = document.MM_sr;

    for (i = 0; a && i < a.length && (x = a[i]) && x.oSrc; i++)
        x.src = x.oSrc;
}

function MM_findObj(n, d) { //v4.01
    var p,
        i,
        x;

    if (!d)
        d = document;

    if ((p = n.indexOf("?")) > 0 && parent.frames.length) {
        d = parent.frames[n.substring(p + 1)].document;
        n = n.substring(0, p);
    }

    if (!(x = d[n]) && d.all)
        x = d.all[n];

    for (i = 0; !x && i < d.forms.length; i++)
        x = d.forms[i][n];

    for (i = 0; !x && d.layers && i < d.layers.length; i++)
        x = MM_findObj(n, d.layers[i].document);

    if (!x && d.getElementById)
        x = d.getElementById(n);

    return x;
}

function MM_swapImage() { //v3.0
    var i,
        j = 0,
        x,
        a = MM_swapImage.arguments;

    document.MM_sr = new Array;

    for (i = 0; i < (a.length - 2); i += 3)
        if ((x = MM_findObj(a[i])) != null) {
        document.MM_sr[j++] = x;

        if (!x.oSrc)
            x.oSrc = x.src;

        x.src = a[i + 2];
    }
}

function MM_validateForm() { //v4.0
    var i,
        p,
        q,
        nm,
        test,
        num,
        min,
        max,
        errors = '',
        args = MM_validateForm.arguments;

    for (i = 0; i < (args.length - 2); i += 3) {
        test = args[i + 2];
        val = MM_findObj(args[i]);

        if (val) {
            val.value = TrimAll(val.value);
            nm = val.name;

            if ((val = val.value) != "") {
                if (test.indexOf('isEmail') != -1) {
                    p = val.indexOf('@');

                    if (p < 1 || p == (val.length - 1))
                        errors += '- ' + args[i + 1] + ' must contain an e-mail address.\n';

                }
                else if (test != 'R') {
                    num = parseFloat(val);

                    if (isNaN(val))
                        errors += '- ' + args[i + 1] + ' must contain a number.\n';

                    if (test.indexOf('natural') != -1) {
                        if (num < 0)
                            errors += '- ' + args[i + 1] + ' must contain a non-negative number.\n';
                    }

                    if (test.indexOf('inRange') != -1) {
                        p = test.indexOf(':');

                        min = test.substring(8, p);
                        max = test.substring(p + 1);

                        if (num < min || max < num)
                            errors += '- ' + args[i + 1] + ' must contain a number between ' + min + ' and ' + max + '.\n';
                    }
                }
            }
            else if (test.charAt(0) == 'R') {
                errors += '- ' + args[i + 1] + ' is required.\n';
            }

            //checking maxLength before submitting
            var textobj = MM_findObj(args[i]);

            if (((textobj.type == "text") || (textobj.type == "textarea")) && (textobj.maxlength || textobj.maxLength)) {
                var maxLength;

                if (textobj.maxlength)
                    maxLength = textobj.maxlength;

                if (textobj.maxLength)
                    maxLength = textobj.maxLength;

                if (textobj.value.length > maxLength) {
                    errors += 'The length of text in field \"' + args[i + 1] + '\" should not exceed ' + maxLength + ' characters.\n';
                }
            }

        }
    }

    if (errors)
        alert('The following needs validating:\n' + errors);

    document.MM_returnValue = (errors == '');
}

function TrimAll(p_s) {
    if (p_s == null)
        p_s = ""
    else
        p_s = p_s.replace(/^\s*|\s*$/g, "");

    return p_s;
}

function getPos(el) {
    for (var lx = 0, ly = 0;
         el != null;
         lx += el.offsetLeft, ly += el.offsetTop, el = el.offsetParent);
    return { x: lx, y: ly };
}

function findPosX(obj) {
    var curleft = 0;
    if (obj.offsetParent)
        while (1) {
        curleft += obj.offsetLeft;
        if (!obj.offsetParent)
            break;
        obj = obj.offsetParent;
    }
    else if (obj.x)
        curleft += obj.x;
    return curleft;
}

function findPosY(obj) {
    var curtop = 0;
    if (obj.offsetParent)
        while (1) {
        curtop += obj.offsetTop;
        if (!obj.offsetParent)
            break;
        obj = obj.offsetParent;
    }
    else if (obj.y)
        curtop += obj.y;
    return curtop;
}

function toggleHeaderTarget(p_strRef, p_strTargetControlID, p_strImgPath, p_booIE6Browser) {
    var objTargetControl = document.getElementById(p_strTargetControlID);
    var objImage = document.getElementById(p_strRef + '_imgArrow');
    var objImageLabel = document.getElementById(p_strRef + '_lblStatus');

    try {
        if (objTargetControl) {
            if (objTargetControl.className == "hideHeaderTarget") {
                objTargetControl.className = "showHeaderTarget";

                if (document.all) {
                    objImageLabel.innerText = 'Hide Details »';
                }
                else {
                    objImageLabel.textContent = 'Hide Details »';
                }

                objImage.title = 'Hide Details';
                objImage.src = p_strImgPath + 'Up.png';
            }
            else {
                objTargetControl.className = "hideHeaderTarget";

                if (document.all) {
                    objImageLabel.innerText = 'View Details »';
                }
                else {
                    objImageLabel.textContent = 'View Details »';
                }

                objImage.title = 'View Details';
                objImage.src = p_strImgPath + 'Down.png';
            }
            
            if (p_booIE6Browser) {
                //setPng(p_strRef + '_imgArrow');
                //pngfix();
                //objImage.style.display = "block";
                fixPng(p_strRef + '_imgArrow');
            }
        }
    }
    catch (Err) {
        alert(Err.description);
    }
}

function toggleCtrl(p_strCtrl) {
    var objCtrl = document.getElementById(p_strCtrl);

    try {
        if (objCtrl) {
            if (objCtrl.style.display == 'none') {
                objCtrl.style.display = 'block';
            }
            else {
                objCtrl.style.display = 'none';
            }
        }
    }
    catch (Err) {
        alert(Err.description);
    }
}

function showCtrl(p_strCtrl) {
    var objCtrl = document.getElementById(p_strCtrl);

    try {
        if (objCtrl) {
            objCtrl.style.display = 'block';
        }
    } 
    catch (Err) {
        alert(Err.description);
    }
}

function hideCtrl(p_strCtrl) {
    var objCtrl = document.getElementById(p_strCtrl);

    try {
        if (objCtrl) {
            objCtrl.style.display = 'none';
        }
    }
    catch (Err) {
        alert(Err.description);
    }
}

function validateInput(p_strField, p_intMaxLength) {
    var intMaxLength = p_intMaxLength;
    var _tbxField = document.getElementById(p_strField);

    if (_tbxField) {
        if (_tbxField.value.length > intMaxLength) {
            _tbxField.value = _tbxField.value.substring(0, intMaxLength);
        }
    }
}
