// Load the jQuery script
var eJQuery = document.createElement('script');
eJQuery.setAttribute("type", "text/javascript");
eJQuery.setAttribute("src", "http://www.esalestrack.com/js/jquery-1.5.min.js");
if (typeof eJQuery != "undefined") {
    //document.getElementsByTagName("head")[0].appendChild(eJQuery)
}



function LoginControls(cssStyle) {
    //0 = Standard, 1 = (To be completed later)
    var uN = "";
    var chk = "";
    if (getCookie('estLoginUName') != null && getCookie('estLoginUName') != undefined && getCookie('estLoginUName') != '') {
        uN = getCookie('estLoginUName');
        chk = "checked = 'checked'";
    }
    
    var sControls = "";
    sControls += "<table width='300px' border='0'><tr><td nowrap='nowrap' align='right' width='40%'>User Name:";
    sControls += "</td><td width='60%' style='padding-left:10px'><input type='text' id='estUserName' value='" + uN + "' width='100px' /></td></tr>";
    sControls += "<tr><td nowrap='nowrap' align='right' width='40%'>Password:";
    sControls += "</td><td width='60%' style='padding-left:10px'><input type='password' id='estPassWord' width='100px' /></td></tr>";
    sControls += "<tr><td nowrap='nowrap' colspan='2' align='right' style='padding-top:7px'><input id='estRemember' " + chk + " type='checkbox' />&nbsp;Remember Me&nbsp;&nbsp;&nbsp;";
    sControls += "<input type='button' value='Login' onclick='AuthMe()' /></td></tr>";
    sControls += "</table>";

    return sControls;
}

function AuthMe() {
    var u = window.document.getElementById('estUserName').value;
    var p = window.document.getElementById('estPassWord').value;
    var r = window.document.getElementById('estRemember').checked;

    if (r) {
        setCookie('estLoginUName', u, 365);
    }
    else {
        setCookie('estLoginUName', '', 1);
    }
    
    var nV = encrypt(u + '|' + p + '|' + r);

    window.location.href = 'https://est05.esalestrack.com/eSalesTrack/Login.aspx?s=' + nV;
}

$(document).ready(function() {
    var sContents = LoginControls(0);
    $('#divESTLoginArea').html(sContents);
});

function encrypt(_string) {
    var _return = "";
    for (var i = 0; i < _string.length; i++) {
        _return += _string.charCodeAt(i) + 'x';
    }
    return _return;
}

function setCookie(c_name, value, exdays) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + exdays);
    var c_value = escape(value) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString());
    document.cookie = c_name + "=" + c_value;
}

function getCookie(c_name) {
    var i, x, y, ARRcookies = document.cookie.split(";");
    for (i = 0; i < ARRcookies.length; i++) {
        x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("="));
        y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
        x = x.replace(/^\s+|\s+$/g, "");
        if (x == c_name) {
            return unescape(y);
        }
    }
}


