<script src="/js/plugins/jquery-validation/additional-methods.min.js"></script>
<script>
$(document).ready(function(){
	$(".phone_number_div").hide();
	$(".no_phone_number_div").show();
	$(".mobilephone").hide();
	
});
	$("#create-prod-asset-form").validate({
		rules: {
			location_id: {
				required: true,
			},
			po_id: {
				required: true,
			},
			asset_type_id: "required",
			asset_tag: {
				required: true,
				minlength: 2,
				unique: true,
				clientunique:true,
			},
			po_id: {
				required: true,
				minlength: 2
			},
			serial_no: {
				required: true,
				unique: true,
				minlength: 2,
				serialunique: 5,
			},
	
			make_and_model_id: { 
				required : true
			},
			technical_spec_id: {
				required: true
			},
			ticket_no : {
				required:true,
			}

		},
		errorPlacement: function (error, element) {
    	    error.insertAfter($(element).next($('.errorText')));
    	},
	});

	$("#upload-asset-form").validate({
		rules: {
			bulk_upload_asset: {
				extension: "csv",
				required: true,
			}
		},
		messages: {
			bulk_upload_asset: {
				extension: "Please upload a csv file"
			}
		}
	});

	$('#upload-asset-form').submit(function (e) {

        e.preventDefault();
        if($('#upload-asset-form').valid()) {
	        $('.alert-danger').hide();

	        var formData = new FormData();
			formData.append('file', $('#bulk_upload_asset')[0].files[0]);

	        $.ajax({
			    type: "POST",  
			    url: "/av-assets/upload",  
			    data: formData,
			    async: false,  
			    success: function(value) { 
			    	$('tbody').append(value.data);
			    	$('#upload-asset-form').append(value.errors);
			    	$('#upload-asset-form').append(value.duplicate_errors);
			    	$('#asset_count').text(value.countVal);
			    	$('#upload-asset-form')[0].reset();
			    },
			    cache: false,
			    contentType: false,
			    processData: false
			});

	    }
	});



	
    $("#asset_type_id").change(function(){
		
		
		if($(this).val()!='' && $(this).val()!='--Select--' && $(this).val()!='0') { 
			try{
 				
 				$.ajax({  
		 	   		type: "POST", 
		 	   		data:{id:$(this).val(), type:'avassets'},
			    	url: "/assets-create/listHardware",  
		 	   		success: function(data) { 
		 	    		$('#make_and_model_id').empty();
		 	    		$('#make_and_model_id').append($('<option>', {
                  			  value: null,
                  			  text: "--Select--"
               				 }));
		 	    		$("#make_and_model_id").select2();
		 	    		$('#technical_spec_id').empty();
		 	    		$('#technical_spec_id').append($('<option>', {
                  			  value: null,
                  			  text: "--Select--"
               				 }));
		 	    		$("#technical_spec_id").select2();
		 	    		$.each(data,function (key,value) {
		 	    			$('#make_and_model_id').append($('<option>', {
                  			  value: value.id,
                  			  text:  value.hardware  
               				 }));
		 	    		});




	
	            	},
	            	error: function(value) { 
		 	    		
	            	}
		 	 	});
			}catch(err){
				
			}
			
		}
      		 
  	});
	

	function sort(a,b){
	    a = a.text.toLowerCase();
	    b = b.text.toLowerCase();
	    if(a > b) {
	        return 1;
	    } else if (a < b) {
	        return -1;
	    }
	    return 0;
	}
  	$("#make_and_model_id").change(function(){
     
     	if($(this).val()!='' && $(this).val()!='--Select--' && $(this).val()!='0') { 
			try{
 				$.ajax({  
		 	   		type: "POST", 
		 	   		data:{id:$(this).val()},
			    	url: "/assets-create/listtechnics",  
		 	   		success: function(data) { 
		 	    		$('#technical_spec_id').empty();
		 	    		$('#technical_spec_id').append($('<option>', {
                  			  value: 0,
                  			  text: "--Select--"
               				 }));
		 	    		$("#technical_spec_id").select2();
		 	    		$.each(data,function (key,value) {
		 	    			$('#technical_spec_id').append($('<option>', {
                  			  value: value.id,
                  			  text: value.details
               				 }));
		 	    		});




	
	            	},
	            	error: function(value) { 
		 	    		
	            	}
		 	 	});
			}catch(err){
				
			}
			
		}
  	});

  	$('.lease-date').datepicker({

		todayBtn:"linked",

		clearBtn:true,

		keyboardNavigation: false,

		forceParse: false,

		calendarWeeks: false,

		autoclose: true

	});

</script>