﻿/** filename        : bluebox.js  
*** file modified   : 22-04-11
*** created by      : abhishek 
**/
/**** start : user events and authentication *****/
function getBaseURL() {
    var url = location.href;  // entire url including querystring - also: window.location.href;
    var baseURL = url.substring(0, url.indexOf('/', 14));
    if (baseURL.indexOf('http://localhost') != -1) {
        // Base Url for localhost
        var url = location.href;  // window.location.href;
        var pathname = location.pathname;  // window.location.pathname;
        var index1 = url.indexOf(pathname);
        var index2 = url.indexOf("/", index1 + 1);
        var baseLocalUrl = url.substr(0, index2);
        return baseLocalUrl + "/";
    }
    else {
        // Root Url for domain name        
        return baseURL + "/";
    }
}
var baselink = getBaseURL();
function newCookie(name, value, days) {
    var days = 1;   // the number at the left reflects the number of days for the cookie to last
    // modify it according to your needs
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}
function readCookie(name) {
    var nameSG = name + "=";
    var nuller = '';
    if (document.cookie.indexOf(nameSG) == -1)
        return nuller;
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameSG) == 0) return c.substring(nameSG.length, c.length);
    }
    return null;
}
function eraseCookie(name) { newCookie(name, "", 1); }
function toMem() {
    //getBaseURL function get from headermenu control
    if (jQuery.trim($("#txtUname").val()) == "" && jQuery.trim($("#txtPass").val()) == "") {
        $('#lblErrorBox').fadeIn('fast'); $('#lblErrorBox').text('Please enter username or password');
        return false;
    }
    else {
        $.ajax({
            type: "POST",
            url: getBaseURL() + "Service/usrVldte.asmx/IsUserAuthenticate",
            data: "{ txtunm : '" + jQuery.trim($("#txtUname").val()) + "',txtpass : '" + jQuery.trim($("#txtPass").val()) + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (msg) {
                if (msg.d.StrTitle != "") {
                    $('#lblErrorBox').fadeIn('fast'); $('#lblErrorBox').text(msg.d.StrTitle);
                }
                else {
                    if (msg.d.IsAdmin)
                        window.top.location.href = baselink + "admin/default.aspx";
                    else
                        window.top.location.href = window.top.location.href.indexOf("#") > 0 ? window.top.location.href.substring(0, window.top.location.href.indexOf("#")) : window.top.location.href;                        
                    //window.top.location.href = baselink + "home";
                    return true;
                }
            },
            error: function (xhr, ajaxOptions, thrownError) { return false; }
        });
    }  
    return false;
}
function delMem() {
    eraseCookie('theName');   // make sure to add the eraseCookie function for every field
    eraseCookie('theEmail');
}
$(document).ready(function () {
    // remove static button event call
    var mouse_is_inside = false;
    //$("#imgLogin").click(function () {toMem();});    
    $("#container").click(function () {
        if ($("#signin_menu").is(":visible") && !mouse_is_inside) {
            $("#signin_menu").hide();
        }
    })
    $("#signin_menu").click(function () { mouse_is_inside = true; })
    $("[id$='_A6']").click(function () {    
        mouse_is_inside = true;
        $("#signin_menu").toggle('slow', function () {
            $("#txtUname").focus();
        });        
    });
    $("#container").mouseup(function () {
        mouse_is_inside = false;
    });
    $(".close").click(function () { mouse_is_inside = false; $("#signin_menu").toggle(); $('#lblErrorBox').text(''); });
});
