function get_hsncode() {
	var search = $('#txt_hsncode').val();
        if(search != ""){
            $.ajax({
                url: 'getsearch_hsncode.php',
                type: 'post',
                data: {search:search},
                dataType: 'json',
                success:function(response){
				var len = response.length;
				$("#searchResult").empty();
				for (var i = 0; i < len; i++) {
					var id = response[i]['id'];
					var hsncode = response[i]['hsncode'];
					$("#searchResult").append("<li value='" + hsncode + "'>" + hsncode + "</li>");
				}
				// binding click event to li
				$("#searchResult li").bind("click", function() {
					setText(this);
				});
			}
		});
	}
}

function setText(element){
    console.log(element);
	var value = $(element).text();
	var userid = $(element).val();
	$("#txt_hsncode").val(value);
	$("#hsn_code").val(userid);
	$("#searchResult").empty();
	if (userid != "") {
		$.ajax({
			url: 'getsearch_hsncode.php',
			type: 'post',
			data: {
				search: userid
			},
			dataType: 'json',
			success: function(response) {
				var len = response.length;
				for (var i = 0; i < len; i++) {
					var id = response[i]['id'];
					var hsncode = response[i]['hsncode'];
					var igst = response[i]['igst'];
					var cgst = response[i]['cgst'];
					var sgst = response[i]['sgst'];

					$("#igst").val(igst);
					$("#cgst").val(cgst);
					$("#sgst").val(sgst);
				}
			}
		});
	}
}
function setText2(){
	var value=$("#txt_hsncode").val();
	console.log(value);
// 	$("#hsn_code").val(value);
// 	$("#searchResult").empty();
		$.ajax({
			url: 'getsearch_hsncode.php',
			type: 'post',
			data: {
				search: value
			},
			dataType: 'json',
			success: function(response) {
				var len = response.length;
				for (var i = 0; i < len; i++) {
					var id = response[i]['id'];
					var hsncode = response[i]['hsncode'];
					var igst = response[i]['igst'];
					var cgst = response[i]['cgst'];
					var sgst = response[i]['sgst'];

					$("#igst").val(igst);
					$("#cgst").val(cgst);
					$("#sgst").val(sgst);
				}
			}
		});
}


function fetch_client() {
	var client_id = $('#client_id').val();
	if (client_id != 'none') {
		$.ajax({
			type: 'POST',
			url: 'get_client_data.php',
			data: {
				id: client_id
			},
			dataType: 'json',
			success: function(response) {
				var len = response.length;
				$('#shipper_name').val(response[0]['full_name']);
				$('#rto_name').val(response[0]['full_name']);
				$('#location').val(response[0]['location']);

				$('#shipper_address').val(response[0]['address']);
				$('#rto_address').val(response[0]['address']);

				$('#shipper_city').val(response[0]['city']);
				$('#rto_city').val(response[0]['city']);

				$('#shipper_state').val(response[0]['state']);
				$('#rto_state').val(response[0]['state']);

				$('#shipper_phone').val(response[0]['phone']);
				$('#rto_phone').val(response[0]['phone']);

				$('#shipper_pincode').val(response[0]['pincode']);
				$('#rto_pincode').val(response[0]['pincode']);
			}
		});
	}
}

function checkCscsAwb(){
	var awb = $('#cscs_awb').val();
	var old_cscs = $('#old_cscs_awb').val();
	if(old_cscs==awb)
	{
	$("#cscs_awb_txt").text('');
	$("#cscs_awb_service").val('yes');
	}else{
	    
	   if(awb!=''){ 
	$.ajax({
		type: 'POST',
		url: 'get_unique_awb.php',
		data: {
			awb: awb,
			type: 'cscs_awb'
		},
		dataType: 'json',
		success: function(response) {
			if (response[0]['error'] == '0') {
			    $("#cscs_awb_service").val('yes');
				$("#cscs_awb_txt").css('color', 'green');
				$("#cscs_awb_txt").text(response[0]['txt']);
			} else {
				$("#cscs_awb_txt").css('color', 'red');
				$("#cscs_awb_txt").text(response[0]['txt']);
				$("#cscs_awb_service").val('no');
			}
		}
	});
	   }else
	   {
	      $("#cscs_awb_txt").text('CSCS AWB Can\'t be empty');
	      $("#cscs_awb_txt").css('color', 'red');
	      $("#cscs_awb_service").val('no'); 
	   }
	}
}

function check_hsncode() {
	var hsn = $('#txt_hsncode').val();
        console.log(hsn)
	if(hsn!=''){
	$.ajax({
		type: 'POST',
		url: 'get_unique_awb.php',
		data: {	awb: hsn,type: 'hsncode' },
		dataType: 'json',
		success: function(response) {
		    console.log(response);
			if (response[0]['error'] == '0') {
				$("#ncheckhsn").css('color', 'green');
				$("#ncheckhsn").text(response[0]['txt']);
			} else {
				$("#ncheckhsn").css('color', 'red');
				$("#ncheckhsn").text(response[0]['txt']);
			}
		}
	});
	}else
	{
	   // console.log('empty');
	    $("#ncheckhsn").text('');
	}
}

function check_ref() {
	var awb = $('#ref_no').val();
	var old_ref_no = $('#old_ref_no').val();
	if(old_ref_no==awb)
	{
	$("#ncheck2").text('');
	$("#ref_service").val('yes');
	}else{
	$.ajax({
		type: 'POST',
		url: 'get_unique_awb.php',
		data: {
			awb: awb,
			type: 'ref_no'
		},
		dataType: 'json',
		success: function(response) {
			if (response[0]['error'] == '0') {
				$("#ncheck2").css('color', 'green');
				$("#ncheck2").text(response[0]['txt']);
				$("#ref_service").val('yes');
			} else {
				$("#ncheck2").css('color', 'red');
				$("#ncheck2").text(response[0]['txt']);
				$("#ref_service").val('no');
			}
		}
	});
	}
}

function check_3pl() {
	var pl3_awb = $('#3pl_awb').val();
	var old_3plawb = $('#3pl_awb_old').val();
	var courier_id = $('#courier_id').val();
	
	$('#pl3_service').val('no');
		if(old_3plawb==pl3_awb)
	{
	  $('#pl3_service').val('yes');
	$("#ncheck1").text('');
	}else{
		$.ajax({
			type: 'POST',
			url: 'get_unique_awb.php',
			data: {
				awb: pl3_awb,
				type: 'pl3_awb',
				courier_id: courier_id
			},
			dataType: 'json',
			success: function(response) {
				if (response[0]['error'] == '0') {
					$("#ncheck1").css('color', 'green');
					$("#ncheck1").text(response[0]['txt']);
					$('#pl3_service').val('yes');
				} else {
					$("#ncheck1").css('color', 'red');
					$("#ncheck1").text(response[0]['txt']);
					$('#pl3_service').val('no');
				}
			}
		});
	}
}



function get_availability() {
	var pincode_mode = $('#pincode_mode').val();
	var pincode_type = $('input[name="pincode_type"]:checked').val();
	var pincode = $('#consignee_pincode').val();
	$('#service').val('no');
	var tot_length = $("#consignee_pincode").val().replace(/ /g, '').length;
	if (tot_length > 5) {
		$.ajax({
			type: 'POST',
			url: 'get_oda_pincode.php',
			data: {
				pincode_mode: pincode_mode,
				pincode_type: pincode_type,
				pincode: pincode
			},
			success: function(response) {
				if (response == 1) {
					$('#availability').css('color', 'green');
					$('#availability').html('Service Available !');
					$('#service').val('yes');
				} else if (response == 0) {
					//   $('#form_submit').prop('disabled', true);
					$('#availability').css('color', 'red');
					$('#availability').html('Service Not Available For Your Pincode !');
					$('#service').val('no');
				}
			}
		});
	}
}


function calculate() {
		var boxes = $('#boxes').val();
	var v_weight = parseFloat((($('#vw_length').val() * $('#vw_breadth').val() * $('#vw_height').val()) / 5000) * boxes);
	v_weight = v_weight.toFixed(3);
	$('#v_weight').val(v_weight);
	type();
}

function type() {
	var act_weight = $('#a_weight').val();
	var vol_weight = $('#v_weight').val();
	if (Number(act_weight) > Number(vol_weight)) {
		$('#c_weight').val(act_weight);
	} else {
		$('#c_weight').val(vol_weight);
	}
}


function cal_gst() {
	var taxable = $('#taxable_value').val();
	var gst_type = $('#gst_rate').val();
	var igst = $('#igst').val();
	var cgst = $('#cgst').val();
	var sgst = $('#sgst').val();
	if (gst_type == 'gr_igst') {
		var gst_amount = (taxable * igst) / 100;
	} else if (gst_type == 'gr_cgst') {
		var gst = parseFloat(cgst) + parseFloat(sgst);
		var gst_amount = (taxable * gst) / 100;
	}
	$('#gst_amount').val(gst_amount);
	var invoice = 0;
	invoice = parseFloat(taxable) + parseFloat(gst_amount);
	$('#invoice_value').val(invoice);
}

function formValidate() {
    var cp_length = $("#consignee_pincode").val().replace(/ /g, '').length;
	var valid = 1;
	if ($('#client_id').val() == 'none') {
		$('#client_id_txt').text('Please select the Client');
		$('#client_id_txt').css('color', 'red');
	} else {
		$('#client_id_txt').text('');
	}
//     if ($('#pl3_service').val() == 'no'&& $('#3pl_awb').val()!='') {
// 		valid = 0;
// 		$('#ncheck1').text('Please enter the valid 3Pl AWB Number');
// 		$('#ncheck1').css('color', 'red');
// 	} else {
// 		$('#ncheck1').text('');
// 	}
// 	if ($('#ref_no').val() == '' || $("#ref_service").val()=='no') {
// 		valid = 0;
// 		$('#ncheck2').text('Please enter valid reference number');
// 		$('#ncheck2').css('color', 'red');
// 	} else {
// 		$('#ncheck2').text('');
// 	}
	if ($('#cscs_awb').val() == '' || $("#cscs_awb_service").val()=='no') {
		valid = 0;
		$('#cscs_awb_txt').text('Please enter valid CSCS AWB number');
		$('#cscs_awb_txt').css('color', 'red');
	} else {
		$('#cscs_awb_txt').text('');
	}
	if ($('#shipper_name').val() == '') {
		valid = 0;
		$('#shipper_name_txt').text('Please enter the shipper name');
		$('#shipper_name_txt').css('color', 'red');
	} else {
		$('#shipper_name_txt').text('');
	}
	if ($('#shipper_address').val() == '') {
		valid = 0;
		$('#shipper_address_txt').text('Please enter the shipper address');
		$('#shipper_address_txt').css('color', 'red');
	} else {
		$('#shipper_address_txt').text('');
	}
	if ($('#shipper_city').val() == '') {
		valid = 0;
		$('#shipper_city_txt').text('Please enter the shipper city');
		$('#shipper_city_txt').css('color', 'red');
	} else {
		$('#shipper_city_txt').text('');
	}
	if ($('#shipper_state').val() == '') {
		valid = 0;
		$('#shipper_state_txt').text('Please enter the shipper state');
		$('#shipper_state_txt').css('color', 'red');
	} else {
		$('#shipper_state_txt').text('');
	}
	if ($('#shipper_pincode').val() == '') {
		valid = 0;
		$('#shipper_pincode_txt').text('Please enter the shipper pincode');
		$('#shipper_pincode_txt').css('color', 'red');
	} else {
		$('#shipper_pincode_txt').text('');
	}
	if ($('#shipper_phone').val() == '') {
		valid = 0;
		$('#shipper_phone_txt').text('Please enter the shipper phone');
		$('#shipper_phone_txt').css('color', 'red');
	} else {
		$('#shipper_phone_txt').text('');
	}
	if ($('#consignee_name').val() == '') {
		valid = 0;
		$('#consignee_name_txt').text('Please enter the consignee name');
		$('#consignee_name_txt').css('color', 'red');
	} else {
		$('#consignee_name_txt').text('');
	}
	if ($('#consignee_address').val() == '') {
		valid = 0;
		$('#consignee_address_txt').text('Please enter the consignee address');
		$('#consignee_address_txt').css('color', 'red');
	} else {
		$('#consignee_address_txt').text('');
	}
// 	if ($('#consignee_city').val() == '') {
// 		valid = 0;
// 		$('#consignee_city_txt').text('Please enter the consignee city');
// 		$('#consignee_city_txt').css('color', 'red');
// 	} else {
// 		$('#consignee_city_txt').text('');
// 	}
// 	if ($('#consignee_state').val() == '') {
// 		valid = 0;
// 		$('#consignee_state_txt').text('Please enter the consignee state');
// 		$('#consignee_state_txt').css('color', 'red');
// 	} else {
// 		$('#consignee_state_txt').text('');
// 	}
	if ($('#consignee_pincode').val() == '' || cp_length!=6 || $('#service').val()=='no') {
		valid = 0;
		$('#availability').text('Please enter valid consignee pincode');
		$('#availability').css('color', 'red');
	} else {
		$('#availability').text('');
	}
	if ($('#consignee_phone').val() == '') {
		valid = 0;
		$('#consignee_phone_txt').text('Please select the consignee phone');
		$('#consignee_phone_txt').css('color', 'red');
	} else {
		$('#consignee_phone_txt').text('');
	}
// 	if ($('#boxes').val() == '') {
// 		valid = 0;
// 		$('#boxes_txt').text('Please enter the no.of boxes');
// 		$('#boxes_txt').css('color', 'red');
// 	} else {
// 		$('#boxes_txt').text('');
// 	}
// 	if ($('#c_weight').val() == '' && $('#a_weight').val() == '' && $('#v_weight').val() == '') {
// 		valid = 0;
// 		$('#weight_txt').text('Please enter any one weight');
// 		$('#weight_txt').css('color', 'red');
// 	} else {
// 		$('#weight_txt').text('');
// 	}
// 	if ($('#taxable_value').val() == '') {
// 		valid = 0;
// 		$('#taxable_value_txt').text('Please enter the taxable value');
// 		$('#taxable_value_txt').css('color', 'red');
// 	} else {
// 		$('#taxable_value_txt').text('');
// 	}
// 	if ($('#hsn_code').val() == '') {
// 		valid = 0;
// 		$('#hsn_code_txt').text('Please enter the HSN Code'); 
// 		$('#hsn_code_txt').css('color', 'red');
// 	} else {
// 		$('#hsn_code_txt').text('');
// 	}
// 	if ($('#gst_amount').val() == '') {
// 		valid = 0;
// 		$('#gst_amount_txt').text('Please enter the GST Amount');
// 		$('#gst_amount_txt').css('color', 'red');
// 	} else {
// 		$('#gst_amount_txt').text('');
// 	}
// 	if ($('#gst_rate').val() == 'none') {
// 		valid = 0;
// 		$('#gst_rate_txt').text('Please select the GST type');
// 		$('#gst_rate_txt').css('color', 'red');
// 	} else {
// 		$('#gst_rate_txt').text('');
// 	}
// 	if ($('#invoice_value').val() == '') {
// 		valid = 0;
// 		$('#invoice_value_txt').text('Please select the GST type');
// 		$('#invoice_value_txt').css('color', 'red');
// 	} else {
// 		$('#invoice_value_txt').text('');
// 	}
	if ($('#igst').val() == '' && $('#cgst').val() == '' && $('#sgst').val() == '') {
		valid = 0;
		$('#hsn_code_txt').text('Please enter valid HSN Code');
		$('#hsn_code_txt').css('color', 'red');
	} else {
		$('#hsn_code_txt').text('');
	}
	return valid;
}