﻿var UserPasswordValue = "";
var UserMailValue = "";
var ticketId_u;
var userMail_u;
$(document).ready(function () {
    try {
        
        var IspostBack = "<%Response.Write(Page.IsPostBack);%>";
        if (IspostBack != "True") {
            deleteCookie('empId');
            deleteCookie('empEmail');
            deleteCookie('typeEmployee');
            deleteCookie('LastLoginDate');
            deleteCookie('EmpName');
            $("#status-bar").hide();
        }
    }
    catch (e) {
        e.Message();
    }
    $("#status-bar").hide();
    $('#Loading').hide();
    checkOnlineDaprtments();
    $("#btn_UserLogin").click(function () {
        $("#error_emp").css({ 'display': 'none' });
        if (ValidateUserData()) {
            try {
                $('#Loading').show();
                WebService.AccountLogin(UserMailValue, UserPasswordValue, onUserSuccess, onUserFailed);
            }
            catch (e) {
                alert(e.Message)
            }
        }
    });

    $('#directToRequestChat').click(function () {
        window.open("Default.aspx?page=RequestChat", "Request_Chat", "width=900,height=600,resizable=no");
    });

});


function ValidateUserData() {
    var errorMessage = "";
    var _isValidate = true;
    UserPasswordValue = $("#txt_UserPassword").val();
    UserMailValue = $("#txt_UserMail").val();
    if ((UserPasswordValue.length <= 0) | (UserMailValue.length <= 0)) {
        errorMessage = "Unable to logon : You are required to complete the following fields";
        DisplayErrorMessage(errorMessage);
        _isValidate = false;
    }
    return _isValidate;

}


function onUserSuccess(result_user) {
    var UserId;
    var Usermail
    var UserName;
    try {
        if ((result_user != null) | (result_user.length <= 0)) {
            for (var i = 0; i < result_user.length; i++) {
                UserId = result_user[i].id;
                Usermail = result_user[i].email;
                UserName = result_user[i].name;
            }
            var x = "";
            setCookie('userId', UserId, 1);
            setCookie('Usermail', Usermail, 1);
            setCookie('UserName', UserName, 1);
            setCookie('typeEmployee', x, 1);
            window.location = "Default.aspx?Page=ViewUserTicket";

        }
        else {
            errorMessage = "Unable to logon :Invalid Mail or password .Please try again ...";
            $('#Loading').hide();
            DisplayErrorMessage(errorMessage);
        }
    }
    catch (e) {
        errorMessage = "Unable to logon :Invalid Mail or password .Please try again ...";
        $('#Loading').hide();
        DisplayErrorMessage(errorMessage);
    }
}


function checkOnlineDaprtments() {
    WebService.checkOnlineEmployee(onsuccess, onFailed);
}

function onsuccess(result) {
    if (result == true) {
        document.getElementById("imageNotify").src = "images/chatImages/141x44-online.gif";
    }
    else {
        document.getElementById("imageNotify").src = "images/chatImages/141x44-offline.gif";
    }

    setTimeout(checkOnlineDaprtments, 1000);
}


function onFailed() {
    if (!window.closed) {
        // alert("failed to Load XML.."); 
    }
}



function DisplayErrorMessage(errorMessage) {
    $("#error_emp").css({ 'display': 'inline' });
    $("#MessageDisplay").html(errorMessage);
}
function onUserFailed() {
    alert("Error");
}


/*---------------------------------*/

var employeeType = getCookie('typeEmployee');
var employeeEmail = "";
function TicketSearch() {
    var pageName = DrawEditeProfileForm();
    //    GoToProfilePage(employeeEmails);
    openEditeProfilesModal('UnRegistered User Options', pageName, '1');
}
function openEditeProfilesModal(Title, contentvalue, index) {
    //    setCookie('index', index, 1);
    var new_modal = $.modal({
        content: contentvalue,
        title: Title,
        maxWidth: 1000,
        buttons: {
            'Close': function (win) { win.closeModal(); }
        }
    });
}



function onNotfill() {

}



function errorViewProfil() {

}



function DrawEditeProfileForm() {
    var HtmlTags = '<div class="block-controls" style="width: 97.4%;">';
    try {
        HtmlTags = HtmlTags + ' <div style="text-align: left; margin-top:15px; float: left; width: 300px; height: 50px; vertical-align: middle; font-family: Arial, Helvetica, sans-serif; font-size: large; color: #0575CA;font-weight: bold;">';
        HtmlTags = HtmlTags + '<img src="images/icons/users_2states.png" alt="" /></div>';

        HtmlTags = HtmlTags + '</div>';
        HtmlTags = HtmlTags + '<div class="clearAdimProfil">';
        HtmlTags = HtmlTags + '</div>';
        HtmlTags = HtmlTags + '<div class="block-border" style="width: 420px; border: 1px solid gray; background-color: White; margin-left: auto; margin-right: auto;">';
        HtmlTags = HtmlTags + '<div class="block-content" style="height: 80px;">';
        HtmlTags = HtmlTags + '<h1 style="width: 100px;">';
        HtmlTags = HtmlTags + 'Options</h1>';
        HtmlTags = HtmlTags + '<div class="clearAdimProfil">';
        HtmlTags = HtmlTags + '</div>';

        HtmlTags = HtmlTags + '<div class="floatAdminProfile">';
        HtmlTags = HtmlTags + '<a href="Default.aspx?page=AddNewUser" class="RegisterLink">New User</a> </div>';
        HtmlTags = HtmlTags + '<div class="clearAdimProfil">';
        HtmlTags = HtmlTags + '</div>';
        HtmlTags = HtmlTags + '<div class="floatAdminProfile">';
        HtmlTags = HtmlTags + '    <a href="default.aspx?page=CreateTicket" class="RegisterLink">Create New Ticket</a> </div>';

        HtmlTags = HtmlTags + '<div class="clearAdimProfil">';
        HtmlTags = HtmlTags + '</div>';
        HtmlTags = HtmlTags + '<div class="floatAdminProfile">';
        HtmlTags = HtmlTags + '<a href="javascript:void(0)" class="RegisterLink" onclick="OpenSearch();"> Ticket Search</a></div>';
        HtmlTags = HtmlTags + '<div class="clearAdimProfil">';
        HtmlTags = HtmlTags + '</div>';

        HtmlTags = HtmlTags + '</div>';
        HtmlTags = HtmlTags + '</div>';
    }
    catch (e) {
    }
    return HtmlTags;
}

function OpenSearch() {
    var x = "";
    setCookie('typeEmployee', x, 1);
    var pageName = DrawSearchTicketForm();
    openSearchTicketModal('View Ticket', pageName, '1');
}

function DrawSearchTicketForm() {
    var HtmlTags = '<div class="block-controls" style="width: 97.4%;">';
    try {
        HtmlTags = HtmlTags + ' <div style="text-align: left; margin-top:15px; float: left; width: 100px; height: 30px; vertical-align: middle; font-family: Arial, Helvetica, sans-serif; font-size: large; color: #0575CA;font-weight: bold;">';
        HtmlTags = HtmlTags + '<img src="images/icons/users_2states.png" alt="" /></div>';

        HtmlTags = HtmlTags + '</div>';
        HtmlTags = HtmlTags + '<div class="clearAdimProfil">';
        HtmlTags = HtmlTags + '</div>';
        HtmlTags = HtmlTags + '<div style="color:#1F72B1; font-weight:bold;" >Enter Your Mobile Or Email, Please</div>';
        HtmlTags = HtmlTags + ' <table class="Login" >';
        HtmlTags = HtmlTags + ' <tr>';
        HtmlTags = HtmlTags + ' <td class="withOfTextAdminProfile" ></td>';
        HtmlTags = HtmlTags + ' </tr>';
        HtmlTags = HtmlTags + ' <tr>';
        HtmlTags = HtmlTags + ' <td class="withOfTextAdminProfile">Email</td>';
        HtmlTags = HtmlTags + ' <td><input type="text" id="txt_mail" class="txtbox_style" /></td>';
        HtmlTags = HtmlTags + ' </tr>';
        HtmlTags = HtmlTags + ' <tr>';
        HtmlTags = HtmlTags + ' <td class="withOfTextAdminProfile">Mobile</td>';
        HtmlTags = HtmlTags + ' <td><input type="text" id="txt_mobile" class="txtbox_style" /></td>';
        HtmlTags = HtmlTags + ' </tr>';
        HtmlTags = HtmlTags + ' <tr>';
        HtmlTags = HtmlTags + ' <td class="withOfTextAdminProfile">Complaint No</td>';
        HtmlTags = HtmlTags + ' <td> <input type="text" id="txt_Ticket" class="txtbox_style"  /></td>';
        HtmlTags = HtmlTags + ' </tr>';
        HtmlTags = HtmlTags + ' </table>';
        HtmlTags = HtmlTags + ' <div class="btn" >';
        HtmlTags = HtmlTags + ' <a href="#" class="login_btn" id="div_getticket" onclick="getUserTicket_u();">';
        HtmlTags = HtmlTags + ' <img src="images/icons/search.png" /></a>';
        HtmlTags = HtmlTags + ' </div>';
        HtmlTags = HtmlTags + '</div>';
    }
    catch (e) {
    }
    return HtmlTags;
}


function openSearchTicketModal(Title, contentvalue, index) {
    //    setCookie('index', index, 1);
    var new_modal = $.modal({
        content: contentvalue,
        title: Title,
        maxWidth: 1000,
        buttons: {
            'Close': function (win) { win.closeModal(); }
        }
    });
}

function getUserTicket_u() {
    if (ValidateData_u()) {
        var Type;
        try {
            if (document.getElementById("txt_mail").value != "") {
                Type = "Email";
                userMail_u = document.getElementById("txt_mail").value;
                ticketId_u = document.getElementById("txt_Ticket").value;

                //call web service....
                WebService.TicketUnRegisterUser(ticketId_u, userMail_u, "Email", TicketDisplay_u, onFaileddisplay_u);
            }

            else if (document.getElementById("txt_mobile").value != "") {
                Type = "Mobile";
                userMail_u = document.getElementById("txt_mobile").value;
                ticketId_u = document.getElementById("txt_Ticket").value;

                //call web service....
                WebService.TicketUnRegisterUser(ticketId_u, userMail_u, "Mobile", TicketDisplay_u, onFaileddisplay_u);
            }
        }
        catch (e) {
            alert(e.Message);
        }
    }
}


function ValidateData_u() {
    try {
        var errorMessage = "";
        ticketId_u = document.getElementById("txt_Ticket").value.toString();
        if (ticketId_u == "") {
            errorMessage = "You are required to complete the all Feilds";
            openModalMessageAlert("Alert", errorMessage, '');
            return false;
        }
        if (document.getElementById("txt_mail").value != "") {

            var userMail_u = document.getElementById("txt_mail").value;
            var UserEmailValidate = /\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/.test(userMail_u);
            if (UserEmailValidate == true) {
                return true;
            }
            else {
                errorMessage = "Please insert your Email in correct syntax (EX: mail@mail.com)";
                openModalMessageAlert("Alert", errorMessage, '');
                return false;
            }
        }
        if (document.getElementById("txt_mobile").value == "") {

            errorMessage = "Enter Mobile Or Email , please";
            openModalMessageAlert("Alert", errorMessage, '');
            return false;
        }
        return true;
    }
    catch (e) {
        alert(e.Message);
    }
}

function TicketDisplay_u(result) {
    if (result != null && result.length > 0) {
        var userMail_u = document.getElementById("txt_mail").value;
        var ticketId_u = document.getElementById("txt_Ticket").value;

        setCookie('Usermail', userMail_u, 1);
        setCookie('ticketId', ticketId_u, 1);

        window.location = "Default.aspx?Page=TicketPost";
    }
}


function onFaileddisplay_u() {
    openModalMessageAlert("Alert", "Data no found", '');
}

//    function openProfilesModal(Title, contentvalue) {
//        modal_var = $.modal({
//            content: contentvalue,
//            title: Title,
//            maxWidth: 16000,
//            buttons: {
//                'Close': function (win) { win.closeModal(); }
//            }
//        });
//    }



