﻿$(function(){
    $("#AuthDIV").dialog({title: 'Please enter email & password'});
    $("#AuthDIV").parent().appendTo($("form:first"));
});
function LogIn() {
    $("#AuthDIV").dialog("open");
    if ($("#AuDlg").html() == "") MakeAuDlg();
    else $("#LogError").hide();
}
function LogOut(){
    $.ajax({
        url: $_WsPath + "/common/AuSVC.asmx/Quit",
        data: JSON.stringify({}),
        beforeSend: function() {$AjaxCompleteUnblockUI = false; $.blockUI({ message: "<h3>Log out...</h3>"});},
        error: function(xhr) {if (xhr.status == 0) return; alert("ERROR: " + xhr.status + "-" + xhr.statusText); $.unblockUI({fadeOut: 1000}); },
        success: function(result) {window.location = $rawUrl;}
    });
}
function MakeAuDlg() {
    $.ajax({
        type: "GET",
        contentType: "application/x-www-form-urlencoded",
        dataType: 'html',
        url: $_WsPath + "/common/PubAuDlg.html",
        cache: false,
        beforeSend: function() {$('#AuDlg').block({ message: "<b>Loading...</b>",overlayCSS: { backgroundColor: '#CCCCCC',opacity: 0.8 }});},
        success: function(html){
            $('#AuDlg').html(html);
            $("#LogError").hide();
            $("#tu").watermark("Email");
            $("#tp").watermark("Password");
            $("#BtnLGClose").click(function(){
                $("#AuthDIV").dialog("close");
            });
            $("#BtnLGOK").click(function() {
                $("#AuthDIV").dialog("close");
                $.ajax({
                    url: $_WsPath + "/common/AuSVC.asmx/ValidateUser",
                    data: JSON.stringify({'usr': $("#tu").val(), 'pwd': $("#tp").val()}),
                    beforeSend: function() {$('#AuDlg').block({ message: "<b>Checking...</b>",overlayCSS: { backgroundColor: '#CCCCCC',opacity: 0.8 }});},
                    success: function(result) {
                        if (result == 0) {
                            $("#LogError").show();
                            $("#AuthDIV").dialog("open");
                        } else {
                            $.blockUI({ message: "<h3>Reloading...</h3>"});
                            window.location = $rawUrl;
                        }
                    },
                    complete: function() {$('#AuDlg').unblock({fadeOut: 1000}); }
                });
            });
        },
        complete: function() {$('#AuDlg').unblock({fadeOut: 1000}); }
    });
}