﻿
var EmpName;
var UserName;
$(document).ready(function () {
    var userId = getCookie('userId');
    UserName = getCookie('UserName');
    var LastLoginDate = getCookie('LastLoginDate');
    EmpName = getCookie('EmpName');
    var typeEmployee = getCookie('typeEmployee');
    if ((LastLoginDate != null) & (EmpName != null)) {
        $("#Type").html(EmpName);
        $("#lastLogin").html('Last Login:' + LastLoginDate);
        $("#status-bar").show();
        if (typeEmployee == 35) {
            $("#homepage").html("<a href='Default.aspx?Page=AdminControlPannel' >Home</a>");
        }
        else {
            $("#homepage").html("<a href='ViewTickets.aspx' >Home</a>");
        }
    }
    else if (userId != null & UserName != null) {
        $("#Type").html(UserName);
        $("#lastLogin").html('Last Login:' + LastLoginDate);
        $("#status-bar").show();
        $("#homepage").html("<a href='Default.aspx?Page=ViewUserTicket' >Home</a>");
    }
    else {
        $("#status-bar").hide();
    }

    checkLogout();

    $("#btn_Logout").click(function () {
        try {
            //            alert("logogog");
            //            
            //            if (window.location == "Default.aspx?page=userchat") {
            //                alert("logogog");
            //                WebService.UpdateSessionStatus(getCookie('uSessionID'), 59, UpdateSessionStatusSuccess, UpdateSessionStatusFailed);
            //            }            
            if (getCookie('typeEmployee') == "34") {
                deleteEmpXml();
                deleteCookie('empId');
                deleteCookie('empEmail');
                deleteCookie('typeEmployee');
                deleteCookie('LastLoginDate');
                deleteCookie('EmpName');
                window.location = "Default.aspx?page=login";
            }
            else if (getCookie('typeEmployee') == "35") {
                deleteCookie('empId');
                deleteCookie('empEmail');
                deleteCookie('typeEmployee');
                deleteCookie('LastLoginDate');
                deleteCookie('EmpName');
                window.location = "Default.aspx?page=login";
            }
            else {
                deleteCookie('userId');
                deleteCookie('Usermail');
                deleteCookie('UserName');
                deleteCookie('ticketId');
                window.location = "Default.aspx";
            }
        }
        catch (e) {
            window.location = "Default.aspx";
        }
    });
});

function UpdateSessionStatusSuccess(result) {
    if (result > -1) {
        setCookie('SessionStatus', result, 1);
        // alert("Session updated to be inActive!!");
    }
}

function UpdateSessionStatusFailed() {
    // alert("Update Session Status Failed");
}
function deleteEmpXml() {
    var empId = getCookie('empId');
    WebService.deleteEmployeeXML(empId);
}

function AddSortedTable() {

    // A small classes setup...
    $.fn.dataTableExt.oStdClasses.sWrapper = 'no-margin last-child';
    $.fn.dataTableExt.oStdClasses.sInfo = 'message no-margin';
    $.fn.dataTableExt.oStdClasses.sLength = 'float-left';
    $.fn.dataTableExt.oStdClasses.sFilter = 'float-right';
    $.fn.dataTableExt.oStdClasses.sPaging = 'sub-hover paging_';
    $.fn.dataTableExt.oStdClasses.sPagePrevEnabled = 'control-prev';
    $.fn.dataTableExt.oStdClasses.sPagePrevDisabled = 'control-prev disabled';
    $.fn.dataTableExt.oStdClasses.sPageNextEnabled = 'control-next';
    $.fn.dataTableExt.oStdClasses.sPageNextDisabled = 'control-next disabled';
    $.fn.dataTableExt.oStdClasses.sPageFirst = 'control-first';
    $.fn.dataTableExt.oStdClasses.sPagePrevious = 'control-prev';
    $.fn.dataTableExt.oStdClasses.sPageNext = 'control-next';
    $.fn.dataTableExt.oStdClasses.sPageLast = 'control-last';

    // Apply to table
    $('.sortable').each(function (i) {
        // DataTable config
        var table = $(this),
					oTable = table.dataTable({
					    /*
					    * We set specific options for each columns here. Some columns contain raw data to enable correct sorting, so we convert it for display
					    * @url http://www.datatables.net/usage/columns
					    */
					    aoColumns: [
							{ bSortable: false }, // No sorting for this columns, as it only contains checkboxes
							{sType: 'string' },
							{ sType: 'string' },
							{ sType: 'string' },
							{ bSortable: false },
                            { bSortable: false}		// No sorting for actions column
						],

					    /*
					    * Set DOM structure for table controls
					    * @url http://www.datatables.net/examples/basic_init/dom.html
					    */
					    sDom: '<"block-controls"<"controls-buttons"p>>rti<"block-footer clearfix"lf>',

					    /*
					    * Callback to apply template setup
					    */
					    fnDrawCallback: function () {
					        this.parent().applyTemplateSetup();
					    },
					    fnInitComplete: function () {
					        this.parent().applyTemplateSetup();
					    }
					});

        // Sorting arrows behaviour
        table.find('thead .sort-up').click(function (event) {
            // Stop link behaviour
            event.preventDefault();

            // Find column index
            var column = $(this).closest('th'),
						columnIndex = column.parent().children().index(column.get(0));

            // Send command
            oTable.fnSort([[columnIndex, 'asc']]);

            // Prevent bubbling
            return false;
        });
        table.find('thead .sort-down').click(function (event) {
            // Stop link behaviour
            event.preventDefault();

            // Find column index
            var column = $(this).closest('th'),
						columnIndex = column.parent().children().index(column.get(0));

            // Send command
            oTable.fnSort([[columnIndex, 'desc']]);

            // Prevent bubbling
            return false;
        });
    });

    /*
    * Datepicker
    * Thanks to sbkyle! http://themeforest.net/user/sbkyle
    */
    $('.datepicker').datepick({
        alignment: 'bottom',
        showOtherMonths: true,
        selectOtherMonths: true,
        renderer: {
            picker: '<div class="datepick block-border clearfix form"><div class="mini-calendar clearfix">' +
							'{months}</div></div>',
            monthRow: '{months}',
            month: '<div class="calendar-controls" style="white-space: nowrap">' +
								'{monthHeader:M yyyy}' +
							'</div>' +
							'<table cellspacing="0">' +
								'<thead>{weekHeader}</thead>' +
								'<tbody>{weeks}</tbody></table>',
            weekHeader: '<tr>{days}</tr>',
            dayHeader: '<th>{day}</th>',
            week: '<tr>{days}</tr>',
            day: '<td>{day}</td>',
            monthSelector: '.month',
            daySelector: 'td',
            rtlClass: 'rtl',
            multiClass: 'multi',
            defaultClass: 'default',
            selectedClass: 'selected',
            highlightedClass: 'highlight',
            todayClass: 'today',
            otherMonthClass: 'other-month',
            weekendClass: 'week-end',
            commandClass: 'calendar',
            commandLinkClass: 'button',
            disabledClass: 'unavailable'
        }
    });
}
function PrepareHtmlHover() {
    $(".myLink6").qtip({
        position: {
            corner: {
                target: 'topRight', //   Where the balloon tip has to point to on the element 
                tooltip: 'bottomLeft'//Where the arrow for balloon tip has to be placed in tip-balloon 
            }
        },
        style: { name: 'cream',
            tip: 'bottomLeft',
            border: {
                width: 1,
                radius: 6,
                color: '#3689BF'
            },
            color: '#5B2E00',
            background: '#FFFFFF'
        }
    });
}
function openModalMessageAlert(MsgTitle, Message, pageUrl) {
    var Modal2 = $.modal({
        content: Message,
        title: MsgTitle,
        maxWidth: 500,
        buttons: {
            'Close': function (win) {
                win.closeModal();
                if (pageUrl.length > 5) {
                    window.location = pageUrl;
                }
            }
        }
    });
}

function checkLogout() {
    // alert(location.href);
    var loc = document.URL;
    // alert(loc);
    if (loc != "http://dreameg.com/") {
        if (loc != "http://dreameg.com/Default.aspx") {
            if (loc != "http://dreameg.com/Default.aspx?page=login") {
                //  alert("document.URL");
                if (getCookie('EmpName') == null & EmpName != getCookie('EmpName')) {
                    EmpName = "";
                    openModalMessageAlert('', 'you are logged out', 'Default.aspx?page=login');
                }
                else if (getCookie('UserName') == null & UserName != getCookie('UserName')) {
                    EmpName = "";
                    openModalMessageAlert('', 'you are logged out', 'Default.aspx');
                }
                else
                    setTimeout(checkLogout, 1000);
            }
        }
    }
}

function AddSortedTable_trans() {

    // A small classes setup...
    $.fn.dataTableExt.oStdClasses.sWrapper = 'no-margin last-child';
    $.fn.dataTableExt.oStdClasses.sInfo = 'message no-margin';
    $.fn.dataTableExt.oStdClasses.sLength = 'float-left';
    $.fn.dataTableExt.oStdClasses.sFilter = 'float-right';
    $.fn.dataTableExt.oStdClasses.sPaging = 'sub-hover paging_';
    $.fn.dataTableExt.oStdClasses.sPagePrevEnabled = 'control-prev';
    $.fn.dataTableExt.oStdClasses.sPagePrevDisabled = 'control-prev disabled';
    $.fn.dataTableExt.oStdClasses.sPageNextEnabled = 'control-next';
    $.fn.dataTableExt.oStdClasses.sPageNextDisabled = 'control-next disabled';
    $.fn.dataTableExt.oStdClasses.sPageFirst = 'control-first';
    $.fn.dataTableExt.oStdClasses.sPagePrevious = 'control-prev';
    $.fn.dataTableExt.oStdClasses.sPageNext = 'control-next';
    $.fn.dataTableExt.oStdClasses.sPageLast = 'control-last';

    // Apply to table
    $('.sortable').each(function (i) {
        // DataTable config
        var table = $(this),
					oTable = table.dataTable({
					    /*
					    * We set specific options for each columns here. Some columns contain raw data to enable correct sorting, so we convert it for display
					    * @url http://www.datatables.net/usage/columns
					    */
					    aoColumns: [
							{ bSortable: false }, // No sorting for this columns, as it only contains checkboxes
							{sType: 'string' },
							{ sType: 'string' },
							{ sType: 'string' }
						],

					    /*
					    * Set DOM structure for table controls
					    * @url http://www.datatables.net/examples/basic_init/dom.html
					    */
					    sDom: '<"block-controls"<"controls-buttons"p>>rti<"block-footer clearfix"lf>',

					    /*
					    * Callback to apply template setup
					    */
					    fnDrawCallback: function () {
					        this.parent().applyTemplateSetup();
					    },
					    fnInitComplete: function () {
					        this.parent().applyTemplateSetup();
					    }
					});

        // Sorting arrows behaviour
        table.find('thead .sort-up').click(function (event) {
            // Stop link behaviour
            event.preventDefault();

            // Find column index
            var column = $(this).closest('th'),
						columnIndex = column.parent().children().index(column.get(0));

            // Send command
            oTable.fnSort([[columnIndex, 'asc']]);

            // Prevent bubbling
            return false;
        });
        table.find('thead .sort-down').click(function (event) {
            // Stop link behaviour
            event.preventDefault();

            // Find column index
            var column = $(this).closest('th'),
						columnIndex = column.parent().children().index(column.get(0));

            // Send command
            oTable.fnSort([[columnIndex, 'desc']]);

            // Prevent bubbling
            return false;
        });
    });

    /*
    * Datepicker
    * Thanks to sbkyle! http://themeforest.net/user/sbkyle
    */
    $('.datepicker').datepick({
        alignment: 'bottom',
        showOtherMonths: true,
        selectOtherMonths: true,
        renderer: {
            picker: '<div class="datepick block-border clearfix form"><div class="mini-calendar clearfix">' +
							'{months}</div></div>',
            monthRow: '{months}',
            month: '<div class="calendar-controls" style="white-space: nowrap">' +
								'{monthHeader:M yyyy}' +
							'</div>' +
							'<table cellspacing="0">' +
								'<thead>{weekHeader}</thead>' +
								'<tbody>{weeks}</tbody></table>',
            weekHeader: '<tr>{days}</tr>',
            dayHeader: '<th>{day}</th>',
            week: '<tr>{days}</tr>',
            day: '<td>{day}</td>',
            monthSelector: '.month',
            daySelector: 'td',
            rtlClass: 'rtl',
            multiClass: 'multi',
            defaultClass: 'default',
            selectedClass: 'selected',
            highlightedClass: 'highlight',
            todayClass: 'today',
            otherMonthClass: 'other-month',
            weekendClass: 'week-end',
            commandClass: 'calendar',
            commandLinkClass: 'button',
            disabledClass: 'unavailable'
        }
    });


}








