$(document).ready(function(){
	
	$("#submit").click(function(){
		var error_msg = "";
		var system_base_url = "";
		var username = "";
		var email = "";

        var strPrevUserName = $("#prev_username").val();
        var strPrevEmail = $("#prev_email").val();
		system_base_url = $("#system_base_url").val();
		username = $("#username").val();
		email = $("#email").val();
		error_msg = "<table class='table table-white table-vertical-center margin-none table-striped footable' style='color: red;text-align: left'>";
		var count = 0;
        var boolUserName = 0;
        var boolEmail = 0;

        if (strPrevUserName != username) {
            boolUserName = fnCheckUserName(username, system_base_url);
        }
        if (boolUserName > 0 ) {
			error_msg = error_msg + "<tr><td>User name already in use</td></tr>"
			$("#username").css('border-color', 'red');
			count = 1;
		}


        if (strPrevEmail != email) {
            boolEmail = fnCheckEmail(email,system_base_url);
        }


		if (boolEmail > 0) {
			error_msg = error_msg + "<tr><td>Email address already in use</td></tr>"
			$("#email").css('border-color', 'red');
            count = 1;
            
		}
		error_msg = error_msg + "</table>";
		if (count > 0 ){
			$("#all_msgs").html(error_msg);
            return false;
		}

	});

    $("#update_access").click(function(){
        var system_base_url = $("#system_base_url").val();
        var prev_name = $("#prev_name").val();
        var new_name = $("#name").val();
        var boolresult = 0;
        if (new_name != prev_name) {
            $.ajax({
                url:system_base_url + "index.php/users/fnCheckAccessName",
                'type' : 'GET',
                async:false,
                'data' : {
                    'name' :new_name
                },
                success:function(result){
                    if (result > 0) {
                        $("#all_msgs").html("This access name already in use, please try different name");
                        $("#name").css('border-color', 'red');
                        boolresult = 1;
                    }

                }
            });
        }

        if (boolresult == 1) {
            return false;
        }

    });
	
});

function fnCheckUserName(user_name, system_base_url)
{
	var boolValue = 0;
	$.ajax({
            url:system_base_url+"index.php/users/fnCheckDetails",
            'type' : 'GET',
            async:false,
            'data' : {
                'value' :user_name,
                'field' : 'username'
            },
            success:function(result){
                if (result > 0) {
                    boolValue = 1;
                }
            }
        });

	return boolValue;
}

function fnCheckEmail(email, system_base_url)
{
	var boolValue = 0;
	$.ajax({
            url:system_base_url+"index.php/users/fnCheckDetails",
            'type' : 'GET',
            async:false,
            'data' : {
                'value' :email,
                'field' :'email'
            },
            success:function(result){
                if (result > 0) {
                    boolValue = 1;
                }
            }
        });

	return boolValue;
}
