//========================
function setDivPos() { //alert("document.getElementById(divDir).style.visibility= " +document.getElementById("divDir").style.visibility)
    if (document.getElementById("divDir").style.visibility == "hidden") { //alert("div is hidden...reposition")
        document.getElementById('divDir').style.visibility = 'visible';
        repositionDiv("divDir")
    }
    else {  //alert("div is visible...hiding div")
        document.getElementById("divDir").style.visibility = "hidden"
    }

}

//=============================================================
function doSubmit(fAction) {

    document.getElementById("hdnData").value = fAction;
    if (fAction == "KEY") {
        document.getElementById("thisForm").submit();
        return true
    }
    if (fAction == "PASSWORD") {
        document.getElementById("thisForm").submit();
        return true;
    }
    if (fAction == "REGISTER") {
        if (validateAcctNum() && validateAcctPW()) {
            document.getElementById("thisForm").submit();
            return true;
        }
    } //end REGISTER
    if (fAction == "LOGIN") {
        if (validateUserLogin()) { //alert("doSubmit returns true.");
            document.getElementById("bodyLogin").style.cursor = "wait";
            document.getElementById("thisForm").submit();
        }
        else { //alert("doSubmit returns false.");
            return false;
        }
    } // end if LOGIN  
} // end Function   
//=============================================================
function validateUserLogin() {
    if (document.getElementById("hdnData").value == "PASSWORD") return true;
    if (document.getElementById("hdnData").value == "REGISTER") return true;
    if (document.getElementById("hdnData").value == "LOGIN") return validateUNPW();
    if (document.getElementById("hdnData").value == "ALOGIN") return true;

}
//=============================================================
function validateUNPW() {
    if (document.getElementById("uName").value.length < 4 || document.getElementById("uName").value.length > 8) {
        alert("Invalid User Name--Please Re-Enter. (Valid User Names are 4 to 8 characters.)")
        document.getElementById("uName").focus();
        return false;
    }
    if (document.getElementById("uPwd").value.length < 4 || document.getElementById("uPwd").value.length > 12) {
        alert("Invalid Password--Please Re-Enter. (Valid Passwords are 4 to 12 characters.)")
        document.getElementById("uPwd").focus();
        return false;
    }
    return true;
}

//=============================================================
function validateAcctNum() { //alert("validateAcctNum")
    if (document.getElementById("uName").value.length > 3 && document.getElementById("uPwd").value.length > 3)
    { return true; }
    var acct = document.getElementById("acctnum").value
    // 	if(acct>0 && acct.length >= 4 && acct.length <= 16) 
    if (acct.length >= 4 && acct.length <= 16) {
        document.getElementById("password").focus();
        return true;
    }
    else {
        alert("Invalid account. Account numbers are between 4 and 16 characters long and must be numbers only.");
        document.getElementById("acctnum").value = ""
        document.getElementById("acctnum").focus();
        return false;
    }
}

function validateAcctPW() { //alert("validateAcctPW") 	
    var pwd = document.getElementById("password").value;
    if (pwd.length >= 4 && pwd.length <= 16 || (document.getElementById("uPwd").value.length > 3 && document.getElementById("uName").value.length > 3)) {
        return true;
    }
    else {
        alert("Invalid password. Passwords must be between 4 and 16 characters long.");
        document.getElementById("password").focus();
        return false;
    }
}
//=======================
function validateName() {
    return true; //not relevant in Login2
    var fn = document.getElementById("FName").value;
    var ln = document.getElementById("LName").value;

    if (fn.length >= 2 && ln.length >= 2)
    { return true; }
    else {
        if (fn.length < 2) {
            alert("You must enter your First Name to login.")
            document.getElementById("FName").focus()
            return false;
        }
        if (ln.length < 2) {
            alert("You must enter your Last Name to login.")
            document.getElementById("LName").focus()
            return false;
        }
    } // end of else
}

//====================
function checkKey() {
    var ns = (navigator.appName.indexOf("Microsoft") == -1) ? true : false
    var ie = (navigator.appName.indexOf("Microsoft") != -1) ? true : false
    var bt;
    if (ns)
    { if (event.which == 13) doSubmit('Login'); }
    else
    { if (event.keyCode == 13) doSubmit('Login'); }
}

//====================
function isEnter(ctrl, e) {
    document.getElementById("curControl").value = ctrl;
    var ns = (navigator.appName.indexOf("Microsoft") == -1) ? true : false
    var ie = (navigator.appName.indexOf("Microsoft") != -1) ? true : false
    var bt;
    if (ns)
    { if (e.which == 13) return true; }
    else
    { if (e.keyCode == 13) return true; }
    return false;
}
//==============================================
function DoDevLogin() {
    document.getElementById("uName").value = "hess";
    document.getElementById("uPwd").value = "hess";
}

//==============================================
function DoDevLogin2() {
    document.getElementById("uName").value = "vitas";
    document.getElementById("uPwd").value = "vitas";
}
//==============================================
function DoDevLogin3() {
    document.getElementById("uName").value = "link2mon";
    document.getElementById("uPwd").value = "link2mon";
}
//===============================================
function repositionDiv(inputID) {
    var dTop = parseInt(getAnchorPosition("A" + inputID).y) + "px";
    document.getElementById("divDir").style.top = parseInt(getAnchorPosition("A" + inputID).y) - 155;
    document.getElementById("divDir").style.left = parseInt(getAnchorPosition("A" + inputID).x) - 155;

}

//===============================================================
// getAnchorPosition(anchorname)
//   This function returns an object having .x and .y properties which are the coordinates
//   of the named anchor, relative to the page.
function getAnchorPosition(anchorname) {
    // This function will return an Object with x and y properties
    var useWindow = false;
    var coordinates = new Object();
    var x = 0, y = 0;
    // Browser capability sniffing
    var use_gebi = false, use_css = false, use_layers = false;
    if (document.getElementById) { use_gebi = true; }
    else if (document.all) { use_css = true; }
    else if (document.layers) { use_layers = true; }
    // Logic to find position
    if (use_gebi && document.all) {
        x = AnchorPosition_getPageOffsetLeft(document.all[anchorname]);
        y = AnchorPosition_getPageOffsetTop(document.all[anchorname]);
    }
    else if (use_gebi) {
        var o = document.getElementById(anchorname);
        x = AnchorPosition_getPageOffsetLeft(o);
        y = AnchorPosition_getPageOffsetTop(o);
    }
    else if (use_css) {
        x = AnchorPosition_getPageOffsetLeft(document.all[anchorname]);
        y = AnchorPosition_getPageOffsetTop(document.all[anchorname]);
    }
    else if (use_layers) {
        var found = 0;
        for (var i = 0; i < document.anchors.length; i++) {
            if (document.anchors[i].name == anchorname) { found = 1; break; }
        }
        if (found == 0) {
            coordinates.x = 0; coordinates.y = 0; return coordinates;
        }
        x = document.anchors[i].x;
        y = document.anchors[i].y;
    }
    else {
        coordinates.x = 0; coordinates.y = 0; return coordinates;
    }
    coordinates.x = x;
    coordinates.y = y;
    return coordinates;
}
//================================================================
// getAnchorWindowPosition(anchorname)
//   This function returns an object having .x and .y properties which are the coordinates
//   of the named anchor, relative to the window
function getAnchorWindowPosition(anchorname) {
    var coordinates = getAnchorPosition(anchorname);
    var x = 0;
    var y = 0;
    if (document.getElementById) {
        if (isNaN(window.screenX)) {
            x = coordinates.x - document.body.scrollLeft + window.screenLeft;
            y = coordinates.y - document.body.scrollTop + window.screenTop;
        }
        else {
            x = coordinates.x + window.screenX + (window.outerWidth - window.innerWidth) - window.pageXOffset;
            y = coordinates.y + window.screenY + (window.outerHeight - 24 - window.innerHeight) - window.pageYOffset;
        }
    }
    else if (document.all) {
        x = coordinates.x - document.body.scrollLeft + window.screenLeft;
        y = coordinates.y - document.body.scrollTop + window.screenTop;
    }
    else if (document.layers) {
        x = coordinates.x + window.screenX + (window.outerWidth - window.innerWidth) - window.pageXOffset;
        y = coordinates.y + window.screenY + (window.outerHeight - 24 - window.innerHeight) - window.pageYOffset;
    }
    coordinates.x = x;
    coordinates.y = y;
    return coordinates;
}

// Functions for IE to get position of an object
function AnchorPosition_getPageOffsetLeft(el) {
    var ol = el.offsetLeft;
    while ((el = el.offsetParent) != null) { ol += el.offsetLeft; }
    return ol;
}
function AnchorPosition_getWindowOffsetLeft(el) {
    return AnchorPosition_getPageOffsetLeft(el) - document.body.scrollLeft;
}
function AnchorPosition_getPageOffsetTop(el) {
    var ot = el.offsetTop;
    while ((el = el.offsetParent) != null) { ot += el.offsetTop; }
    return ot;
}
function AnchorPosition_getWindowOffsetTop(el) {
    return AnchorPosition_getPageOffsetTop(el) - document.body.scrollTop;
}
//=============================================================
function forgotPassword(fAction) {
    if (document.getElementById("uName").value.length < 4) {
        alert("You must enter a valid, registered User Name")
        return false;
    }
    var msg = "If you are a registered InfolinkXM user and you entered a valid User Name, your password will be sent to the email address you entered "
    msg = msg + "during the registration process. If the email address is no longer valid or you cannot recall your User Name, please contact an Info-Hold Associate for assistance."
    alert(msg)
    doSubmit(fAction);
}

//=============================================================
function validateRegister() {
    if (isNaN(document.getElementById("acctnum").value)) {
        alert("Invalid Account Number");
        document.getElementById("acctnum").value = "";
        document.getElementById("acctnum").focus();
        return false;
    }
    if (document.getElementById("acctnum").value.length == 0 || document.getElementById("password").value.length == 0) {
        alert("You must enter a valid Account Number and matching Password to Register as an InfoLinkXM User.")
        return false;
    }
    document.getElementById("hdnData").value = "Register"
    doSubmit('Register');
}
//=============================================================
function maxWin() {
    window.moveTo(screen.availLeft, screen.availTop);
    window.resizeTo(screen.availWidth, screen.availHeight);

}

