﻿var fullURL = document.URL;
var windowID = fullURL.substring(fullURL.indexOf('?') + 1, fullURL.length);
var hasCookie = 0;

$(document).ready(function() {
    //Disable back button
    function disableBackButton() {
        window.history.forward();
    }
    //setTimeout("disableBackButton()", 0);
    
    //Check if user is using ie6 or less
    if ($.browser.msie) {
        if ($.browser.version == '6.0' || $.browser.version == '5.0') {
            browserAlert();
            $('#LoginButton').attr("disabled", "true");
        }
    }

    if (windowID == 'value=1')
        createDialog();

    //Read from cookies

    var remember = $.cookie('chkRemember');
    if (remember == 'true') {
        hasCookie = 1;
        //Clear watermark fields
        document.getElementById('wmUsername_ClientState').value = '';
        document.getElementById('wmUsername_ClientState').style.display = 'none';
        document.getElementById('wmPassword_ClientState').value = '';
        document.getElementById('wmPassword_ClientState').style.display = 'none';
        //alert('Cached cookie');
        var username1 = $.cookie('txtEmail');
        var password1 = $.cookie('txtPassword');
        // autofill the fields
        $('#txtEmail').attr("value", username1);
        $('#txtPassword').attr("value", password1);
        $('#chkRemember').attr("checked", 'true');
    }

});

function setCookie() {
    if (hasCookie == 0) {
        //alert('save cookie111');
        var username = $('#txtEmail').attr('value');
        var password = $('#txtPassword').attr('value');
        // set cookies to expire in 14 days
        $.cookie('txtEmail', username, { expires: 14 });
        $.cookie('txtPassword', password, { expires: 14 });
        $.cookie('chkRemember', true, { expires: 14 });

        hasCookie = 1;
    } else {
        // reset cookies
        //alert('Empty cookies');
        $.cookie('txtEmail', null);
        $.cookie('txtPassword', null);
        $.cookie('chkRemember', null);
        hasCookie = 0;
    }
}

function browserAlert() {
    var dialog = $('<div id="upgradeDiv" class="upgradDiv"></div>').css({ position: "absolute", top: 0, left: 0, zIndex: 1000 }).show('fast').appendTo('body#body');
    var display = $(
                    '<div class="dialogBox" style="width:450px;margin-top:-50px;height:200px">'
                    + '<div style="float:left;width:100%;background:#5cadc8;font-weight:bold;color:#fff;padding:5px 0">&nbsp;Browser alert for IE 6 users!</div>'
                    + '<div style="float:left;padding:10px"><p>To fully experience the features and functionality of Xzotto Desktop, we have found that Google Chrome and Mozilla Firefox support the latest technologies better than other internet browsers, with Chrome performing best. In order to utilize one of these browsers, click on the appropriate link below.</p><ul>'
                    + '<li><img src="../vdImages/chromeLogo.jpg"/><a href="http://www.google.com/chrome" target="_blank" title="Google Chrome">&nbsp;Google Chrome</a></li><li><img src="../vdImages/firefoxLogo.jpg"/><a href="http://www.mozilla.com/en-US/firefox/ie.html" target="_blank" title="Mozilla Firefox">&nbsp;Mozilla Firefox</a></li></ul>'
                    + '</div></div>');
    display.show('fast').appendTo(dialog);
    $('#confirmLogout').focus();
}

function createDialog() {
    if (document.getElementById('Win')) {
        return false;
    }
    var body = document.getElementById('layoutDiv');
    var window1 = document.createElement('div');
    window1.setAttribute('id', 'Win');
    window1.style.position = 'absolute';
    window1.className = 'dialog';
    window1.style.width = '2000px';
    window1.style.height = '2000px';
    window1.style.left = '0px';
    window1.style.top = '0px';
    window1.style.zIndex = '12000';
    body.appendChild(window1);
    buildAlert();
}

function redirect() {
    window.location = 'Login.aspx';
}

function buildAlert() {
    var body = document.getElementById('body');
    var dialog = document.createElement('div');
    dialog.className = 'dialogBox';
    dialog.style.width = '380px';
    dialog.style.height = '120px';

    var titleDiv = document.createElement('div');
    titleDiv.className = 'dialogTitle';
    titleDiv.style.width = '100%';
    titleDiv.style.height = '20px';
    //titleDiv.style.backgroundColor = '#0066cc';

    var paraTitle = document.createElement('div');
    var titleArea = document.createTextNode('Time expired!');
    //paraTitle.style.color = '#fff';
    //paraTitle.style.textAlign = 'center';
    paraTitle.style.fontWeight = 'bold';
    paraTitle.style.paddingLeft = '10px';
    paraTitle.appendChild(titleArea);
    titleDiv.appendChild(paraTitle);

    var para = document.createElement('p');
    var textArea = document.createTextNode('Your session was terminated! To continue with your Xzotto Desktop, please login again.');
    //para.style.padding = '10px';
    para.className = 'dialogPara';
    para.appendChild(textArea);

    var btnOk = document.createElement('button');
    btnOk.setAttribute('id', 'btnOK');
    //btnOk.style.marginLeft = '370px';
    btnOk.style.width = '80px';
    btnOk.style.right = '0';
    btnOk.style.bottom = '0';
    var btnText = document.createTextNode('OK');
    btnOk.appendChild(btnText);

    dialog.appendChild(titleDiv);
    dialog.appendChild(para);
    dialog.appendChild(btnOk);
    body.appendChild(dialog);

    document.getElementById('btnOK').onclick = redirect;
}