/********************************IMMIDIATE CHECK ITEM VALIDATION************************************/
$(document).ready(function(){
$('#user_type').keyup(usernames_checkitem);
}); 
function usernames_checkitem(){ 
var user_types = jQuery('#user_type').val();
if(user_types == "" || user_types.length < 0){
jQuery('#user_type');
}else{
jQuery.ajax({
   type: "POST",
   url: "chk_user_type_db.php",
   data: 'user_types='+ user_types,
   cache: false,
   success: function(response){
if(response != 0)
{
document.getElementById("user_type_validation_div").innerHTML="User Type Already Exists.";
}
 else
 {
	 document.getElementById("user_type_validation_div").innerHTML="";
 }
}
});
}
}
/****************validation*************************/
function user_type_validation()
{
    var user_type = document.getElementById("user_type").value;
	if(user_type=='')
	{
		document.getElementById("user_type_validation_div").innerHTML="* This field is Required.";
		document.getElementById("user_type").focus();
		return false;
	}
	
	var active_status = document.getElementById("active_status").value;
	if(active_status=='')
	{
		document.getElementById("status_validation_div").innerHTML="* This field is Required.";
		document.getElementById("active_status").focus();
		return false;
	}
}

function user_type_clear_validation(str)
{
	if(str!='')
	{
		document.getElementById("user_type_validation_div").innerHTML="";
	}
	
	if(str!='')
	{
		document.getElementById("status_validation_div").innerHTML="";
	}
}

/***********************ADD**************************/
function user_type_add(user_type,active_status,edit_id,action)
{ 
  if((user_type!='')&&(active_status!=''))
     {

	jQuery.ajax({
		type: "POST",
	    url:"user_type_db.php",
		data:"user_type="+user_type+"&active_status="+active_status+"&edit_id="+edit_id+"&action="+action,
		success: function(msg){
		jQuery("#user_type_div").html(msg);
		alert(msg);
		window.close();
		window.opener.location.reload(true);
		}
		});
	 }
	 else
	 {
		 user_type_validation();
	 }
   }

/****************Delete********************************/
function user_type_delete_row_pass(user_id) {

    if (!confirm('Are you sure you want to delete?')) return;

    const dataToSend = $.param({ id: user_id, action: 'del_user_type' });
    console.log("AJAX URL: user_type/user_type_db.php");
    console.log("AJAX Data: ", dataToSend);

    $.ajax({
        type: "POST",
        url: "user_type/user_type_db.php",
        data: dataToSend,
        success: function(response) {
            console.log("Server Response:", response);
            $("#user_type_div").html(response);
            //alert("Deleted Successfully");
        },
        error: function(xhr, status, error) {
            console.error("Deletion failed:", error);
            alert("An error occurred while deleting the user type.");
        }
    });
}





