


/* function to initialize error messages,

 * edit fields

 * if this was reached via a back button,

 * don't show previous error messages

 * this confuses the user

 */

function setInitialFormState() {

    var usererror = false;

    var passworderror = false;

    if (this.document.loginForm.initialState.value == "-1") {

        this.document.loginForm.initialState.value = "0";

        var ipmsg = "";

        if (ipmsg.length > 5) {

            var reply = window.confirm(ipmsg + " Do you wish to override?");

            if (reply)

                location.href = location.href + "?ipoverride=1";

        } else {

            if (msg.length > 5) {

                alert(msg.replace(/<br>|<b>|<\/b>/g, ''));

                var domObj = this.document.getElementById("LoginMsg-id");

                domObj.innerHTML = msg;

            }

            usererror = "";

            passworderror = "";

        }

    } else {

        // this isn't the first time this form has been displayed

        // show a clean form, get rid of any errors that might have been shown

        var domObj = this.document.getElementById("LoginMsg-id");

        domObj.innerHTML = "";



    }

    // change look of labels to show error?

    domObj = this.document.getElementById("LoginLabel-id");

    domObj.className = usererror ? "LoginError" : "Login";

    domObj = this.document.getElementById("PasswordLabel-id");

    domObj.className = passworderror ? "LoginError" : "Login";

}



/*

 * This function performs the necessary initialization

 * when page is loaded.

 */

function initLogin() {

    // check if cookie is enabled so applications can run

    if (document.cookie == "") {

        alert("Cookies are disabled. Cannot access mid-tier with this setting.\\nPlease configure your browser to accept cookies.");

        return;

    }



    setInitialFormState();



    // Check browser type and version

    var browserOK = false;

    var browserApp = navigator.appName;



    // Netscape

    if (browserApp == "Netscape") {

        //all NN7 browsers and above are Gecko based

        if (navigator.userAgent.indexOf("Gecko") != -1)

            browserOK = true;

    }

    // IE

    else if (browserApp == "Microsoft Internet Explorer") {

        var browserVersion = navigator.appVersion;

        var MSIEindex = browserVersion.indexOf('MSIE ');



        // Check for IE version 6.0 or higher

        if (MSIEindex >= 0) {

            var MSIEversion = browserVersion.substring(MSIEindex+5);

            var vf = parseFloat(MSIEversion);

            if (vf >= 6.0)

                browserOK = true;

        }

    }



    // Generate message if needed





    // Clear login user name if it has not been set

    if (this.document.loginForm.username.value == "null")

        this.document.loginForm.username.value = "";



    this.document.loginForm.username.onkeypress = doSubmit;

    this.document.loginForm.pwd.onkeypress = doSubmit;

    this.document.loginForm.auth.onkeypress = doSubmit;

    setInitialFocus();

}



function setInitialFocus() {

    // set focus to user input field

    document.loginForm.username.focus();

    document.loginForm.username.select();

}



/*

 * This function handles the "Clear" button on page.

 */

function clearLogin() {

    // clear all input fields

    this.document.loginForm.username.value="";

    this.document.loginForm.pwd.value="";

    this.document.loginForm.auth.value="";



    this.document.loginForm.username.focus();

}



// Scrambles passwords using simple cipher algorithm

function getScrambledPassword(pwd) {

    var cipher = ['k', 's', 'z', 'h', 'x', 'b', 'p', 'j', 'v', 'c', 'g', 'f', 'q', 'n', 't', 'm'];

    var result="";

    if (pwd == null)

        pwd = "";

    pwd = encodeURIComponent(pwd);

    //alert("encoded password: " + pwd);

    for(var i=0;i<pwd.length;i++) {

            var cc = pwd.charCodeAt(i);

        result += cipher[Math.floor(cc/16)] + cipher[cc%16];

    }

    //alert("scrambled password: " + result);

    return result;

}



/*

 * This function validates the information on form

 * and then submits the action if data is correct.

 */

function doLogin() {

    // make sure user has entered a name

    if (this.document.loginForm.username.value == "") {

        alert("You must enter a user name.");

        return;

    }



    // get time zone from the system

  //  this.document.loginForm.timezone.value=getTimezone();



    // scramble the password before submitting

    this.document.loginForm.pwd.value = getScrambledPassword(this.document.loginForm.pwd.value);



    // submit the form action

    document.loginForm.submit();

}



/*

 * This function detects users key presses and sumbits the form

 * if the return key is hit.

 */

function doSubmit(evt) {

    var keycode;



    // extract key code from event

    if (navigator.appName.indexOf("Netscape") != -1)

        keycode = evt.which;

    else if (navigator.appName.indexOf("Microsoft") != -1)

        keycode = window.event.keyCode;



    // detect the "Return" key

    if (keycode == 13) {

        doLogin();



        if (navigator.appName.indexOf("Microsoft") != -1)

            //SW00168184 - stop System Default Beep Sound.

            window.event.keyCode = 0;

    }

}



var msg = "";
