﻿var passwordExpand = null;
var passwordContract = null;
var containerExpand = null;
var containerContract = null;

function Init() {
    var oLoginButton = new YAHOO.widget.Button("QAID_SubmitBtn");
    passwordExpand = new YAHOO.util.Anim('ChangePassword', {
        height: {
            to: 110
        }
    }, 1, YAHOO.util.Easing.easeout);

    passwordExpand.onComplete.subscribe(focusNewPass);

    containerExpand = new YAHOO.util.Anim('container', {
        height: {
            to: 320
        }
    }, 1, YAHOO.util.Easing.easeout);


    passwordContract = new YAHOO.util.Anim('ChangePassword', {
        height: {
            to: 0
        }
    }, 1, YAHOO.util.Easing.easeout);


    containerContract = new YAHOO.util.Anim('container', {
        height: {
            to: 210
        }
    }, 1, YAHOO.util.Easing.easeout);

    if (document.getElementById("chgp").value)
        ChangePasswordClick();

    LoadCapLockPopup("input:password", "h1", "capsLockMsg");
    SetFocus("user");
}

YAHOO.util.Event.onDOMReady(Init);

var focusNewPass = function() {

    var isPostBack = (document.getElementById("isPostBack").value == "1");
    
    if (!isPostBack)
        SetFocus("newp");

    document.getElementById("isPostBack").value = "";        
}

function ChangePasswordClick() {
    var passLink = document.getElementById("ChangePasswordLink");
    var changePassFlag = document.getElementById("chgp");
    
    var noticeDiv = document.getElementById("notice");
    var errMsgDiv = document.getElementById("errmsg");

    var isPostBack = (document.getElementById("isPostBack").value == "1");

    if (noticeDiv && errMsgDiv && !isPostBack) {
        noticeDiv.style.display = "none";
        errMsgDiv.style.display = "none";
    }

    if (passLink.innerHTML == "Reset your password?") {
        passwordExpand.animate();
        containerExpand.animate();
        passLink.innerHTML = "Cancel";
        changePassFlag.value = "1";
    }
    else {
        passwordContract.animate();
        containerContract.animate();
        passLink.innerHTML = "Reset your password?";
        changePassFlag.value = "";
    }
}


function OnSubmit() {
    SetInnerText("errmsg", "");
    
    //hide error msg from showing once form is submitted
    var errmsg = document.getElementById("errmsg");
    if (errmsg)
        errmsg.style.display = "none";

    var user = document.getElementById("user").value;
    var pass = document.getElementById("pass").value;
    var domn = document.getElementById("domn");
    var newp = document.getElementById("newp");
    var conf = document.getElementById("conf");

    var msg = "";
    var focus = null;

    if (user == "") {
        msg = "Enter a username.";
        focus = "user";
    }
    else if (pass == "") {
        msg = "Enter a password.";
        focus = "pass";
    }
    else if (domn && domn.value == "") {
        msg = "Enter a domain name.";
        focus = "domn";
    }
    else if (document.getElementById("chgp").value) {
        if (newp && newp.value == "") {
            msg = "Enter a new password.";
            focus = "newp";
        }
        else if (conf && conf.value == "") {
            msg = "Enter a confirm password.";
            focus = "conf";
        }
        else if (newp && conf && newp.value != conf.value) {
            msg = "The new password and the confirm password do not match.";
            focus = "conf";
        }
    }

    if (msg != "") {
        alert(msg);
        SetFocus(focus);
        return false;
    }
    else
        return true;
}

function SetFocus(id) {
    var obj = document.getElementById(id);

    if (obj) {
        obj.focus();

        if (obj.select)
            obj.select();
    }
}

function ShowRow(id) {
    var e = document.getElementById(id);
    if (e)
        e.style.display = "";
}

function HideRow(id) {
    var e = document.getElementById(id);
    if (e)
        e.style.display = "none";
}

function ChgPass() {
    ShowRow("tr_new");
    ShowRow("tr_cnf");
    ShowRow("tr_can");
    HideRow("tr_chp");
    SetFocus("newp");
    SetInnerText("errmsg", "");
    document.getElementById("chgp").value = "1";
    document.getElementById("main").style.color = "#000001"; // force redraw in Safari
}

function CancelChgPass() {
    HideRow("tr_new");
    HideRow("tr_cnf");
    HideRow("tr_can");
    ShowRow("tr_chp");
    SetFocus("pass");
    document.getElementById("chgp").value = "";
    document.getElementById("main").style.color = "#000000"; // force redraw in Safari
}

function SetInnerText(elem, text) {
    if (typeof (elem) == "string")
        elem = document.getElementById(elem);
    if (elem) {
        if (typeof (elem.innerText) != "undefined")
            elem.innerText = text;
        else
            elem.textContent = text;
    }
}

function CheckPasswordExpires(days) {
    if (confirm("Your password will expire in " + days + " day(s).\nWould you like to change your password now?"))
        ChgPass();
    else
        window.location.href = "../default.aspx";
}


