﻿/// <reference path="../Script/jquery-1.2.6.js" />

//Give all elements the click function
//This is used by some of the modal dialogs 
var browser = navigator.appName;
if (browser == "Netscape") {
    HTMLElement.prototype.click = function() {
        var evt =
    this.ownerDocument.createEvent('MouseEvents'); evt.initMouseEvent('click', true, true,
    this.ownerDocument.defaultView, 1, 0, 0, 0, 0, false, false, false, false, 0,
    null); this.dispatchEvent(evt);
    }
}



//DOM Load
$(document).ready(function() {
});

function resetDialog() {
   //Clear the fields if they were set from before
   $('#txtUser_ML').val('');
   $('#txtPass_ML').val('');
   $('#LoginBox .rfStars').hide();
   $('#loginError').css("visibility", "hidden");
   //Set initial focus
   setTimeout('FirstFocus()', 200); //must wait for dialog to render
   return false;//cancel redirect
}

function FirstFocus() {
    $('#txtUser_ML').focus();
}

//////////////////////

function loginUser() {

    var user = $('#txtUser_ML').val();
    var pass = document.getElementById('txtPass_ML').value;
    var terms = document.getElementById('chkTerms_ML');
//    alert(terms.checked);
    if (user.length * pass.length == 0 || terms.checked == false) {
        $('#LoginBox .rfStars').show();
        return false;
    }
    else {
        $('#LoginBox .rfStars').hide();
    }

    Sys.Services.AuthenticationService.login(user, pass, false,
		null, null, onLoginCallCompleted, null, user);

    return false; //no postback
}

function onLoginCallCompleted(result, context, methodName) {
    if (result == true) {
        $('#loginError').css("visibility", "hidden");

        //Reload the page in the new state
        setTimeout(Reload, 100);
        
        //Close this dialog
        $('#ibCancel').click(); //Fire the Mode Cancel Event since we are done now
    } 
    else {
        $('#loginError').css("visibility", "visible");
    }
}

function logoutUser() {
    //make the logout url
    var subDomainSeparatorIndex = window.location.host.indexOf('.');
    var hasSubdomain = subDomainSeparatorIndex > 0;
    var logoutUrl = window.location.href;
    if (hasSubdomain) {
        logoutUrl = window.location.href.replace(window.location.host.substr(0, subDomainSeparatorIndex + 1), '');
    }
    
    //Logout
    Sys.Services.AuthenticationService.logout(logoutUrl, onLogoutCallCompleted, null, null);
    //Do not worry about cancelling the postback because redirect should occur anyway to get a fresh logged out version of the page.
}

function onLogoutCallCompleted(result, context, methodName) {
}

function Reload() {
     window.location.reload(true);
}