﻿
var employeeType = getCookie('typeEmployee');
var employeeEmail = "";
var UserEmail = "";

function EditProfile(employeeEmails, userEmail) {
    if (employeeEmails != null) {
        var pageName = DrawEditeProfileForm();
        GoToProfilePage(employeeEmails);
        openEditeProfilesModal('Edit Profile', pageName, '1');
    }
    else if (userEmail != null) {
        UserEmail = userEmail;
        var pageName = DrawEditUserProfileForm();
        GoToUserProfilePage(userEmail);
        openEditeProfilesModal('Edit Profile', pageName, '1');
    }

}
function DrawEditUserProfileForm() {
    var HtmlTags = '<div class="block-controls" style="width: 97.4%;">';
    try {
        HtmlTags = HtmlTags + ' <div style="text-align: left; 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 + '<ul class="controls-buttons">';
        HtmlTags = HtmlTags + '<li><a href="#" onclick="UserUpdate()">';
        HtmlTags = HtmlTags + '<img src="images/icons/edit.png" title="UpDate" width="32" height="32" border="none" />';
        HtmlTags = HtmlTags + '</a></li>';
        HtmlTags = HtmlTags + '</ul>';
        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:400px;">';
        HtmlTags = HtmlTags + '<h1 style="width: 100px;">';
        HtmlTags = HtmlTags + 'Profile Data</h1>';
        HtmlTags = HtmlTags + '<div class="clearAdimProfil">';
        HtmlTags = HtmlTags + '</div>';
        HtmlTags = HtmlTags + '<div class="floatAdminProfile">';
        HtmlTags = HtmlTags + '    Name :</div>';
        HtmlTags = HtmlTags + '<div class="withOfTextAdminProfile">';
        HtmlTags = HtmlTags + '<input type="text" id="txt_AdminName" class="txtbox_style" /></div>';
        HtmlTags = HtmlTags + '<div class="clearAdimProfil">';
        HtmlTags = HtmlTags + '</div>';
        HtmlTags = HtmlTags + '<div class="floatAdminProfile">';
        HtmlTags = HtmlTags + '    E-mail :</div>';
        HtmlTags = HtmlTags + '<div class="withOfTextAdminProfile">';
        HtmlTags = HtmlTags + '   <input type="text" id="txt_AdminMail" class="txtbox_style" /></div>';
        HtmlTags = HtmlTags + '<div class="clearAdimProfil">';
        HtmlTags = HtmlTags + '</div>';
        HtmlTags = HtmlTags + '<div class="floatAdminProfile">';
        HtmlTags = HtmlTags + 'Password :</div>';
        HtmlTags = HtmlTags + '<div class="withOfTextAdminProfile">';
        HtmlTags = HtmlTags + ' <input type="password" id="txt_AdminPassword"  class="txtbox_style" /></div>';
        HtmlTags = HtmlTags + '<div class="clearAdimProfil">';
        HtmlTags = HtmlTags + '</div>';
        HtmlTags = HtmlTags + '<div class="floatAdminProfile">';
        HtmlTags = HtmlTags + 'Confirm Password :</div>';
        HtmlTags = HtmlTags + '<div class="withOfTextAdminProfile">';
        HtmlTags = HtmlTags + '<input type="password" id="txt_AdminConfirmPassword" class="txtbox_style" /></div>';
        HtmlTags = HtmlTags + '<div class="clearAdimProfil">';
        HtmlTags = HtmlTags + '</div>';
        HtmlTags = HtmlTags + '<div class="floatAdminProfile">';
        HtmlTags = HtmlTags + '    Mobile :</div>';
        HtmlTags = HtmlTags + '<div class="withOfTextAdminProfile">';
        HtmlTags = HtmlTags + '   <input id="txt_Mobile" class="txtbox_style" /></div>';
        // HtmlTags = HtmlTags + '   </div>';
        HtmlTags = HtmlTags + '<div class="clearAdimProfil">';
        HtmlTags = HtmlTags + '</div>';
        HtmlTags = HtmlTags + '<div class="floatAdminProfile">';
        HtmlTags = HtmlTags + '  City :</div>';
        HtmlTags = HtmlTags + '<div class="withOfTextAdminProfile">';
        HtmlTags = HtmlTags + '<select id="Slct_AdminDepartment" style="width: 170px;" class="txtbox_style">';
        HtmlTags = HtmlTags + '   </select>';
        HtmlTags = HtmlTags + '</div>';
        HtmlTags = HtmlTags + '<div class="clearAdimProfil">';
        HtmlTags = HtmlTags + '</div>';
        HtmlTags = HtmlTags + '<div class="floatAdminProfile">';
        HtmlTags = HtmlTags + '    PoBox :</div>';
        HtmlTags = HtmlTags + '<div class="withOfTextAdminProfile">';
        HtmlTags = HtmlTags + '   <input id="txt_pobox" class="txtbox_style" /></div>';
        HtmlTags = HtmlTags + '   </div>';
        HtmlTags = HtmlTags + '<div class="clearAdimProfil">';
        HtmlTags = HtmlTags + '</div>';
        //        HtmlTags = HtmlTags + '</div>';
        HtmlTags = HtmlTags + '</div>';
    }
    catch (e) {
    }
    return HtmlTags;
}
function openEditeProfilesModal(Title, contentvalue, index) {
    //    setCookie('index', index, 1);
    modal_var = $.modal({
        content: contentvalue,
        title: Title,
        maxWidth: 16000,
        buttons: {
            'Close': function (win) { win.closeModal(); }
        }
    });
}
function GoToUserProfilePage(UserMail) {
    try {

        if ((UserMail != null)) {
            userEmail = UserMail;
            WebService.PropertiesSelect(44, onfilldepartmentList, onNotfill);
        }
        else {
            window.location = "Default.aspx";
        }
    }
    catch (e) {
        alert(e);
    }
}
function GoToProfilePage(employeeEmails) {
    try {

        if ((employeeEmails != null)) {
            employeeEmail = employeeEmails;
            WebService.PropertiesSelect(33, onfillTypeDropList, onNotfill);
        }
        else {
            window.location = "Default.aspx";
        }
    }
    catch (e) {
        alert(e);
    }
}
function SelectUserData() {

    WebService.selectUserByEmail(UserEmail, ViewUserProfil, errorViewProfil);
}
function ViewUserProfil(AdminDetailes) {

    try {
        document.getElementById("txt_AdminName").value = AdminDetailes[0].employeeName_.toString();

        document.getElementById("txt_AdminMail").value = AdminDetailes[0].employeeEmail_.toString();
        Adminemailold = AdminDetailes[0].employeeEmail_.toString();

        document.getElementById("txt_AdminPassword").value = AdminDetailes[0].EmployeePass_.toString();

        document.getElementById("txt_AdminConfirmPassword").value = AdminDetailes[0].EmployeePass_.toString();

        document.getElementById("txt_pobox").value = AdminDetailes[0].pobox.toString();

        document.getElementById("txt_Mobile").value = AdminDetailes[0].mobile.toString();

        // $('option[text=' + AdminDetailes[0].employeeType_ + ']', document.getElementById("Slct_AdminType")).attr('selected', 'selected');

        // $('option[text=' + AdminDetailes[0].Employeedep_ + ']', document.getElementById("Slct_AdminDepartment")).attr('selected', 'selected');
        $('option[text=' + AdminDetailes[0].city.toString() + ']', document.getElementById("Slct_AdminDepartment")).attr('selected', 'selected');


    }
    catch (e) {

    }
}
function SelectAdminData() {

    WebService.selectEmployeeByEmail(employeeEmail, ViewProfil, errorViewProfil);
}
var Adminemailold;
var AdminEmailNew;
var AdminName;
var AdminPass;
var AdminConfirmPass;
var AdminDepart;
var AdminType;

function onfillTypeDropList(Listtype) {
    try {
        if ((Listtype != null) & (Listtype.length > 0)) {
            for (var i = 0; i < Listtype.length; i++) {
                var Text = Listtype[i].Name.toString()
                var Id = Listtype[i].Id.toString()
                var opt = document.createElement("option"); // Create an Option object
                // Add an Option object to Drop Down/List Box                                                                           
                opt.text = Text;
                opt.value = Id;
                document.getElementById("Slct_AdminType").options.add(opt); // Assign text and value to Option object   
            }
        }
        WebService.PropertiesSelect(1, onfilldepartmentList, onNotfill);
    }
    catch (e) {
    }
}

function onfilldepartmentList(ListDepartment) {
    try {
        if ((ListDepartment != null) & (ListDepartment.length > 0)) {
            for (var i = 0; i < ListDepartment.length; i++) {
                var Text = ListDepartment[i].Name.toString()
                var Id = ListDepartment[i].Id.toString()
                var opt = document.createElement("option"); // Create an Option object
                // Add an Option object to Drop Down/List Box                                                                           
                opt.text = Text;
                opt.value = Id;
                document.getElementById("Slct_AdminDepartment").options.add(opt); // Assign text and value to Option object   
            }
        }
        if (employeeEmail != "") {
            SelectAdminData();
        }
        else if (UserEmail != "") {
            SelectUserData();
        }

    }
    catch (e) {
    }
}

function onNotfill() {

}

function ViewProfil(AdminDetailes) {

    try {
        document.getElementById("txt_AdminName").value = AdminDetailes[0].employeeName_.toString();

        document.getElementById("txt_AdminMail").value = AdminDetailes[0].employeeEmail_.toString();
        Adminemailold = AdminDetailes[0].employeeEmail_.toString();

        document.getElementById("txt_AdminPassword").value = AdminDetailes[0].EmployeePass_.toString();

        document.getElementById("txt_AdminConfirmPassword").value = AdminDetailes[0].EmployeePass_.toString();

        $('option[text=' + AdminDetailes[0].employeeType_ + ']', document.getElementById("Slct_AdminType")).attr('selected', 'selected');

        $('option[text=' + AdminDetailes[0].Employeedep_ + ']', document.getElementById("Slct_AdminDepartment")).attr('selected', 'selected');


    }
    catch (e) {

    }
}

function errorViewProfil() {

}



function UserUpdate() {
    try {

        AdminName = document.getElementById("txt_AdminName").value.toString();
        AdminEmailNew = document.getElementById("txt_AdminMail").value.toString();
        AdminPass = document.getElementById("txt_AdminPassword").value.toString();
        AdminConfirmPass = document.getElementById("txt_AdminConfirmPassword").value.toString();
        PoBox = document.getElementById("txt_pobox").value.toString();
        Mobile = document.getElementById("txt_Mobile").value.toString();


        // var e = document.getElementById("Slct_AdminType");
        // AdminType = e.options[e.selectedIndex].value;

        var s = document.getElementById("Slct_AdminDepartment");
        UserCity = $("#Slct_AdminDepartment option:selected").val();


        if (AdminName == "" || AdminEmailNew == "" || AdminPass == "" || AdminConfirmPass == "") {
            openModalMessageAlert("Alert", "Insert All Data Please", '');
        }

        else {
            if (AdminPass == AdminConfirmPass) {
                var txtUserEmail = document.getElementById("txt_AdminMail").value;
                var UserEmailValidate = /\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/.test(txtUserEmail);
                if (UserEmailValidate == true) {
                    WebService.UserUpdate(AdminName, AdminPass, Adminemailold, AdminEmailNew, Mobile, UserCity, OnSuccessUpdate, OnFailedUpdate);

                }
                else {
                    openModalMessageAlert("Alert", "Please insert your Email in correct syntax (EX: mail@mail.com)", '');
                }

            }

            else {
                document.getElementById("txt_AdminPassword").value = "";
                document.getElementById("txt_AdminConfirmPassword").value = "";
                openModalMessageAlert("Alert", "Password not equal to confirm password try again", '');
            }
        }
    }

    catch (e) {

    }
}

function AdminUpdate() {
    try {

        AdminName = document.getElementById("txt_AdminName").value.toString();
        AdminEmailNew = document.getElementById("txt_AdminMail").value.toString();
        AdminPass = document.getElementById("txt_AdminPassword").value.toString();
        AdminConfirmPass = document.getElementById("txt_AdminConfirmPassword").value.toString();
        var e = document.getElementById("Slct_AdminType");
        AdminType = e.options[e.selectedIndex].value;

        var s = document.getElementById("Slct_AdminDepartment");
        AdminDepart = s.options[s.selectedIndex].value;


        if (AdminName == "" || AdminEmailNew == "" || AdminPass == "" || AdminConfirmPass == "") {
            openModalMessageAlert("Alert", "Insert All Data Please", '');
        }

        else {
            if (AdminPass == AdminConfirmPass) {
                var txtUserEmail = document.getElementById("txt_AdminMail").value;
                var UserEmailValidate = /\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/.test(txtUserEmail);
                if (UserEmailValidate == true) {
                    WebService.empUpdate(AdminName, AdminPass, Adminemailold, AdminEmailNew, AdminType, AdminDepart, OnSuccessUpdate, OnFailedUpdate);
                }
                else {
                    openModalMessageAlert("Alert", "Please insert your Email in correct syntax (EX: mail@mail.com)", '');
                }

            }

            else {
                document.getElementById("txt_AdminPassword").value = "";
                document.getElementById("txt_AdminConfirmPassword").value = "";
                openModalMessageAlert("Alert", "Password not equal to confirm password try again", '');
            }
        }
    }

    catch (e) {

    }
}

function OnSuccessUpdate(ResultOfUpdate) {
    try {

        if (ResultOfUpdate == "1 Row Updated") {
            if (employeeType == 35) {
                openModalMessageAlert("Update Profile", "Updated Successfully", "Default.aspx?Page=ViewProfiles");

            }
            else if (employeeType == 34) {

                openModalMessageAlert("Update Profile", "Updated Successfully", "");
            }
            else  {

                openModalMessageAlert("Update Profile", "Updated Successfully", "Default.aspx?Page=ViewUserTicket");
            }

        }
        else {

            openModalMessageAlert("Update Profile", "Update Failed", '');
        }
    }
    catch (e) {

    }
}
function OnFailedUpdate() {

}

function DrawEditeProfileForm() {
    var HtmlTags = '<div class="block-controls" style="width: 97.4%;">';
    try {
        HtmlTags = HtmlTags + ' <div style="text-align: left; 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 + '<ul class="controls-buttons">';
        HtmlTags = HtmlTags + '<li><a href="#" onclick="AdminUpdate()">';
        HtmlTags = HtmlTags + '<img src="images/icons/edit.png" title="UpDate" width="32" height="32" border="none" />';
        HtmlTags = HtmlTags + '</a></li>';
        HtmlTags = HtmlTags + '</ul>';
        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: 300px;">';
        HtmlTags = HtmlTags + '<h1 style="width: 100px;">';
        HtmlTags = HtmlTags + 'Profile Data</h1>';
        HtmlTags = HtmlTags + '<div class="clearAdimProfil">';
        HtmlTags = HtmlTags + '</div>';
        HtmlTags = HtmlTags + '<div class="floatAdminProfile">';
        HtmlTags = HtmlTags + '    Name :</div>';
        HtmlTags = HtmlTags + '<div class="withOfTextAdminProfile">';
        HtmlTags = HtmlTags + '<input type="text" id="txt_AdminName" class="txtbox_style" /></div>';
        HtmlTags = HtmlTags + '<div class="clearAdimProfil">';
        HtmlTags = HtmlTags + '</div>';
        HtmlTags = HtmlTags + '<div class="floatAdminProfile">';
        HtmlTags = HtmlTags + '    E-mail :</div>';
        HtmlTags = HtmlTags + '<div class="withOfTextAdminProfile">';
        HtmlTags = HtmlTags + '   <input type="text" id="txt_AdminMail" class="txtbox_style" /></div>';
        HtmlTags = HtmlTags + '<div class="clearAdimProfil">';
        HtmlTags = HtmlTags + '</div>';
        HtmlTags = HtmlTags + '<div class="floatAdminProfile">';
        HtmlTags = HtmlTags + 'Password :</div>';
        HtmlTags = HtmlTags + '<div class="withOfTextAdminProfile">';
        HtmlTags = HtmlTags + ' <input type="password" id="txt_AdminPassword"  class="txtbox_style" /></div>';
        HtmlTags = HtmlTags + '<div class="clearAdimProfil">';
        HtmlTags = HtmlTags + '</div>';
        HtmlTags = HtmlTags + '<div class="floatAdminProfile">';
        HtmlTags = HtmlTags + 'Confirm Password :</div>';
        HtmlTags = HtmlTags + '<div class="withOfTextAdminProfile">';
        HtmlTags = HtmlTags + '<input type="password" id="txt_AdminConfirmPassword" class="txtbox_style" /></div>';
        HtmlTags = HtmlTags + '<div class="clearAdimProfil">';
        HtmlTags = HtmlTags + '</div>';
        HtmlTags = HtmlTags + '<div class="floatAdminProfile">';
        HtmlTags = HtmlTags + '  Department :</div>';
        HtmlTags = HtmlTags + '<div class="withOfTextAdminProfile">';
        HtmlTags = HtmlTags + '<select id="Slct_AdminDepartment" style="width: 170px;" class="txtbox_style">';
        HtmlTags = HtmlTags + '   </select>';
        HtmlTags = HtmlTags + '</div>';
        HtmlTags = HtmlTags + '<div class="clearAdimProfil">';
        HtmlTags = HtmlTags + '</div>';
        HtmlTags = HtmlTags + '<div class="floatAdminProfile">';
        HtmlTags = HtmlTags + '    Type :</div>';
        HtmlTags = HtmlTags + '<div class="withOfTextAdminProfile">';
        HtmlTags = HtmlTags + '    <select id="Slct_AdminType" style="width: 170px;" class="txtbox_style">';
        HtmlTags = HtmlTags + '    </select></div>';
        HtmlTags = HtmlTags + '</div>';
        HtmlTags = HtmlTags + '</div>';
    }
    catch (e) {
    }
    return HtmlTags;
}
//    function openProfilesModal(Title, contentvalue) {
//        modal_var = $.modal({
//            content: contentvalue,
//            title: Title,
//            maxWidth: 16000,
//            buttons: {
//                'Close': function (win) { win.closeModal(); }
//            }
//        });
//    }



