$('#save,#update').on("click",function (e) {
	var base_url=$("#base_url").val();
    //Initially flag set true
    var flag=true;

    function check_field(id)
    {
      if(!$("#"+id).val() ) //Also check Others????
        {
            $('#'+id+'_msg').fadeIn(200).show().html('Required Field').addClass('required');
            //$('#'+id).css({'background-color' : '#E8E2E9'});
            flag=false;
        }
        else
        {
             $('#'+id+'_msg').fadeOut(200).hide();
             //$('#'+id).css({'background-color' : '#FFFFFF'});    //White color
        }
    }

    //Validate Input box or selection box should not be blank or empty	
	check_field("sales_type_name");
	check_field("sales_type_initial");	
	
    if(flag==false)
    {

		toastr["warning"]("You have Missed Something to Fillup!")
		return;
    }

    var this_id=this.id;

    if(this_id=="save")  //Save start
    {
					///if(confirm("Do You Wants to Save Record ?")){
						
						e.preventDefault();
						data = new FormData($('#sales-types-form')[0]);//form name
						/*Check XSS Code*/
						if(!xss_validation(data)){ return false; }
						
						$(".box").append('<div class="overlay"><i class="fa fa-refresh fa-spin"></i></div>');
						$("#"+this_id).attr('disabled',true);  //Enable Save or Update button
						$.ajax({
						type: 'POST',
						url: base_url+'sales_types/new_sales_type',
						data: data,
						cache: false,
						contentType: false,
						processData: false,
						success: function(result){
             // alert(result);return;
							if(result=="success")
							{
								//alert("Record Saved Successfully!");
								window.location=base_url+"sales_types";
								return;
							}
							else if(result=="failed")
							{
								toastr["error"]("Sorry! Failed to save Record.Try again!");
							}
							else
							{
								toastr["error"](result);
							}
							$("#"+this_id).attr('disabled',false);  //Enable Save or Update button
							$(".overlay").remove();
						   }
						   });
					//}
    }
    else if(this_id=="update")  //Update start
    {

					//if(confirm("Do You Wants to Update Record ?")){
						e.preventDefault();
					
						data = new FormData($('#sales-types-form')[0]);//form name
						/*Check XSS Code*/
						if(!xss_validation(data)){ return false; }
						
						$(".box").append('<div class="overlay"><i class="fa fa-refresh fa-spin"></i></div>');
						$("#"+this_id).attr('disabled',true);  //Enable Save or Update button
						$.ajax({
						type: 'POST',
						url: base_url+'sales_types/update_sales_type',
						data: data,
						cache: false,
						contentType: false,
						processData: false,
						success: function(result){
             // alert(result);return;
							if(result=="success")
							{
								toastr["success"]("Record Updated Successfully!");
								window.location=base_url+"sales_types";
								return;
							}
							else if(result=="failed")
							{
								toastr["error"]("Sorry! Failed to Update Record.Try again!");
							}
							else
							{
								toastr["error"](result);
							}
							$("#"+this_id).attr('disabled',false);  //Enable Save or Update button
							$(".overlay").remove();
						   }
						   });
					//}
    }
    
});

// Auto convert initials to uppercase on input
$('#sales_type_initial').on('input', function() {
    this.value = this.value.toUpperCase();
});

function save(id)
{

	var base_url=$("#base_url").val();
    //Initially flag set true
    var flag=true;

    function check_field(id)
    {
      if(!$("#"+id).val() ) //Also check Others????
        {
            $('#'+id+'_msg').fadeIn(200).show().html('Required Field').addClass('required');
            //$('#'+id).css({'background-color' : '#E8E2E9'});
            flag=false;
        }
        else
        {
             $('#'+id+'_msg').fadeOut(200).hide();
             //$('#'+id).css({'background-color' : '#FFFFFF'});    //White color
        }
    }

    //Validate Input box or selection box should not be blank or empty	
	check_field("sales_type_name");	
	check_field("sales_type_initial");
	
    if(flag==false)
    {
		toastr["warning"]("You have Missed Something to Fillup!")
		return;
    }

    var this_id=id;

    if(this_id=="save")  //Save start
    {
					
						data = new FormData($('#sales-types-form')[0]);//form name
						/*Check XSS Code*/
						if(!xss_validation(data)){ return false; }
						
						$(".overlay").remove();
						$(".box").append('<div class="overlay"><i class="fa fa-refresh fa-spin"></i></div>');
						$("#"+this_id).attr('disabled',true);  //Enable Save or Update button
						$.ajax({
						type: 'POST',
						url: base_url+'sales_types/new_sales_type',
						data: data,
						cache: false,
						contentType: false,
						processData: false,
						success: function(result){
             // alert(result);return;
							if(result=="success")
							{
								//alert("Record Saved Successfully!");
								//window.location=base_url+"sales_types";
								toastr["success"]("Record Saved Successfully!");
								$('#sales_type_name').val('');
								$('#sales_type_initial').val('');
								$('#description').val('');
								$('#sales_type_name').focus();
								
								return;
							}
							else if(result=="failed")
							{
								toastr["error"]("Sorry! Failed to save Record.Try again!");
							}
							else
							{
								toastr["error"](result);
							}
							$("#"+this_id).attr('disabled',false);  //Enable Save or Update button
							$(".overlay").remove();
						   }
						   });
    }
    
}
