﻿function disableLogin()
{
    if (document.getElementById('login_main') && document.getElementById('login_main').style.display == 'block')
    {
        document.getElementById("loginbutton").disabled = true;
        document.getElementById("loginuser").disabled = true;
        document.getElementById("loginpass").disabled = true;
    }
}

function enableLogin(bClearPass)
{
    if (document.getElementById('login_main') && document.getElementById('login_main').style.display == 'block')
    {
        document.getElementById("loginbutton").disabled = false;
        document.getElementById("loginuser").disabled = false;
        document.getElementById("loginpass").disabled = false;

        if (bClearPass) { document.getElementById("loginpass").value = ""; }
        if (document.getElementById("loginuser").value == "") { document.getElementById("loginuser").value = getCookie("lastuser"); }

        if (document.getElementById("loginuser").value == "") { document.getElementById("loginuser").focus(); }
        else { document.getElementById("loginpass").focus(); }
    }
}

function runLogin()
{
    parent.bLogin = true;
    var sCheck = companyLogin();
    var sPass2 = "";
    
    if (parent.document.getElementById("loginpass2"))
    {
        sPass2 = parent.document.getElementById("loginpass2").value;
    }

    parent.showMessage(0, 0, "FALSE", "Authentication Failed", "Y");
    parent.moveWait("hidepop");
    parent.moveWait("show", 20000);

    if (document.getElementById("loginuser").value != "" && document.getElementById("loginpass").value != "")
    {
        setCookie("lastuser", document.getElementById("loginuser").value, 30);
        if (sCheck == "Support")
        {
            parent.Services.runLogin(document.getElementById("loginuser").value, document.getElementById("loginpass").value, runLoginBack);
        }
        else
        {
            movewareServices.checkLogin(document.getElementById("loginuser").value, document.getElementById("loginpass").value, sPass2, runLoginBack);
        }
    }
    else
    {
        parent.showLoginArea(false);
    }
}

function runLoginBack(result) {
    if (result.toString() == "" || (result.toString().length >= 5 && result.toString().substr(0, 5) == "FALSE") || result.toString() == "PASS2")
    {
        parent.moveWait('hideall');
        if (document.getElementById('login_main'))
        {
            document.getElementById('login_main').style.display = 'block';
        }
        if (result.toString() == "PASS2") { enableLogin(false); }
        else { enableLogin(true); }
    }
    parent.showMenuArea(result);
}

function getPassword(vEmail)
{
    parent.moveWait("hidepop");
    parent.moveWait("show", 20000);
    movewareServices.getPassword(vEmail, getPasswordBack);
}

function getPasswordBack(result)
{
    parent.moveWait("hide");
    if (result == "TRUE")
    {
        parent.showMessage(1500, 100,'TRUE','Email Sent Successfully','Y');
    }
    else
    {
        if (result.length > 5 && result.substr(0, 5) == "FALSE")
        {
            alert(result.substring(5));
            parent.showMessage(1500, 100,'FALSE','Email Send Failed','Y');
        }
        else
        {
            alert("Your login details could not be emailed at this time.\n" +
                  "Please ensure you are using the same email address as the\n" +
                  "one you registered with, and that the spelling is correct.");
            parent.showMessage(1500, 100,'FALSE','Email Send Failed','Y');
        }
    }
}

function init()
{
    parent.frames['content'].onbeforeunload = null;
    parent.init();
}

function goLite(obj)
{
    obj.style.className = "buttonw";
}

function goDim(obj)
{
    obj.style.className = "buttonb";
}

function setCookie(c_name, value, expiredays)
{
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + expiredays);
    document.cookie = c_name + "=" + escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString());
}

function getCookie(c_name)
{
    if (document.cookie.length > 0)
    {
        c_start = document.cookie.indexOf(c_name + "=");
        if (c_start != -1)
        {
            c_start = c_start + c_name.length + 1;
            c_end = document.cookie.indexOf(";", c_start);
            if (c_end == -1) { c_end = document.cookie.length; }
            return unescape(document.cookie.substring(c_start, c_end));
        }
    }

    return "";
}

