$(document).ready(function () {



});



function createuser(type) {

    let username = $("#txtUserName").val();
    let password = $("#txtPassword").val();
    let repssword = $("#txtRePassword").val();
    let name = $("#txtName").val();
    let surname = $("#txtSurname").val();
    let position = $("#txtPosition").val();
    let email = $("#txtEmail").val();
    let code = $("#txtCode").val();
    let userid = $("#lbluserid").html();

    if (code == "") {
        swal({
            title: "Warning",
            text: "Please input information!",
            type: 'warning'
        });

        $("#txtCode").focus();
        $("#txtCode").select();

    }
    else if (username == "" || password == "" || repssword == "") {

        swal({
            title: "Warning",
            text: "Please input information!",
            type: 'warning'
        });

        $("#txtUserName").focus();
        $("#txtUserName").select();
    }


    else if (password != repssword) {

        swal({
            title: "Warning",
            text: "Password is not match!",
            type: 'warning'
        });

        $("#txtRePassword").focus();
        $("#txtRePassword").select();

    }
    else {
        $.ajax({
            type: "POST",
            url: "CreateUser.aspx/saveuser",
            data: "{username:'" + username + "',password:'" + password + "',repassword:'" + repssword + "',name:'" + name + "',surname:'" + surname + "',position:'" + position + "',email:'" + email + "',code:'" + code + "',useridcreate:'" + userid + "',type:'"+type+"'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (response) {

                if (response.d == "") {


                    swal({
                        title: "Success",
                        text: "Successfully created a new user.",
                        type: 'success'
                    });



                    $("#txtUserName").val("");
                    $("#txtPassword").val("");
                    $("#txtRePassword").val("");
                    $("#txtName").val("");
                    $("#txtSurname").val("");
                    $("#txtUserName").focus();
                    $("#txtUserName").select();
                    $("#txtPosition").val("");
                    $("#txtEmail").val("");
                    $("#txtCode").val("");


                }
                else {
                    swal({
                        title: "Error",
                        text: response.d,
                        type: "error"
                    });



                }

            },
            failure: function (response) {
                alert(response.d);
            },
            error: function (response) {
                alert(response.d);
            }
        });

    }
}


function getdata()
{
  


        let empcode = $("#txtCode").val();        

        $.ajax({
            type: "POST",
            url: "CreateUser.aspx/getdata",
            data: '{code:"' + empcode + '"}',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (response) {
                var xmlDoc = $.parseXML(response.d);
                var xml = $(xmlDoc);
                var customers = xml.find("Table");
                if (customers.length == 0) {
                    $("#txtUserName").val("");
                    $("#txtPassword").val("");
                    $("#txtRePassword").val("");
                    $("#txtName").val("");
                    $("#txtSurname").val("");
                    $("#txtPosition").val("");
                    $("#txtEmail").val("");
     


                }

                $.each(customers, function () {
                    var customer = $(this);
                                 
                    $("#txtUserName").val($(this).find("username").text());
                    $("#txtPassword").val($(this).find("password").text());
                    $("#txtRePassword").val($(this).find("password").text());
                    $("#txtName").val($(this).find("firstname").text());
                    $("#txtSurname").val($(this).find("lastname").text());
                    $("#txtPosition").val($(this).find("position").text());
                    $("#txtEmail").val($(this).find("emailaddress").text());
                    $("#txtCode").val($(this).find("usercode").text());
             


                });

           

            },
            failure: function (response) {
                alert(response.d);
            },
            error: function (response) {
                alert(response.d);
            }
        });


    




}