	$(document).keydown(function(objEvent) {
		if (objEvent.keyCode == 9) {
			objEvent.preventDefault();
		}
	})
	flag_time = true;
	timer = '';
	setInterval(function(){phpJavascriptClock();},1000);
	
	function phpJavascriptClock()
	{
		if ( flag_time ) {
		timer = 1645343102*1000;
		}
		var d = new Date(timer);
		months = new Array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec');
		
		month_array = new Array('January', 'Febuary', 'March', 'April', 'May', 'June', 'July', 'Augest', 'September', 'October', 'November', 'December');
		
		day_array = new Array( 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
		
		currentYear = d.getFullYear();
		month = d.getMonth();
		var currentMonth = months[month];
		var currentMonth1 = month_array[month];
		var currentDate = d.getDate();
		currentDate = currentDate < 10 ? '0'+currentDate : currentDate;
		
		var day = d.getDay();
		current_day = day_array[day];
		var hours = d.getHours();
		var minutes = d.getMinutes();
		var seconds = d.getSeconds();
		
		var ampm = hours >= 12 ? 'PM' : 'AM';
		hours = hours % 12;
		hours = hours ? hours : 12; // the hour ’0′ should be ’12′
		minutes = minutes < 10 ? '0'+minutes : minutes;
		seconds = seconds < 10 ? '0'+seconds : seconds;
		var strTime = hours + ':' + minutes+ ':' + seconds + ' ' + ampm;
		timer = timer + 1000;
		document.getElementById("demo2").innerHTML='Date : ' + currentDate+'-' +(month+1)+'-' +currentYear + ' Time : ' + strTime ;
		flag_time = false;
	}
function GetCustCode(x) {
   $.ajax({
      url: 'getsuptno',
      data: "",
      dataType: 'json',
      success: function(data){
		$('#customer_code').val(data.srno);		
      } 
    });
}
	$(document).ready(function() {
		load_data();
		GetCustCode();
		function load_data(query) {
			$.ajax({
				url: "search",
				method: "post",
				data: {
					query: query
				},
				success: function(data) {
					$('#result').html(data);
					//$('#acc_type').val('');
					$('#customer_name').val('');
					$('#cust_address_one').val('');
					$('#cust_address_two').val('');
					$('#cust_address_three').val('');
					$('#cust_place').val('');
					$('#cust_pin_code').val('');
					$('#cust_st_code').val('');
					$('#cust_st_name').val('');
					$('#cont_person').val('');
					$('#cust_phone').val('');
					$('#cust_mobile').val('');
					$('#cust_email').val('');
					$('#cust_gstno').val('');
					$('#cust_pan_no').val('');
					$('#cust_aadhar_no').val('');
					$('#tax_code').val('');
					$('#opening').val('');
					$('#debit').val('');
					$('#credit').val('');
					$('#clbalance').val('');
					$('#transporter_name').val('');
					$('#bankname').val('');
					$('#bankaccno').val('');
					$('#bankaccname').val('');
					$('#ifsccode').val('');
					$('#due_days').val('');
					$('#remarks').val('');
					$('#action').text("Add");
					//$('customer_code).focus();
					//$('#customer_name').focus();
				}
			});
		}
		function load_data1(query){
			$.ajax({
				url:"search",
				method:"post",
				data:{query:query},
				success:function(data)
				{
					$('#result').html(data);
					$('#search_text').focus();
				}
			});
		}
		$('#search_text').keyup(function(){
			var search = $(this).val();
			if(search != '')
			{
				load_data1(search);
			}
			else
			{
				load_data();			
			}
		});
		$(function() {
			$("#cust_st_code").autocomplete({
				source: function(request, response) {
					$.ajax({
						url: "fetchcustomer",
						type: 'post',
						dataType: "json",
						data: {
							search: request.term
						},
						success: function(data) {
							response(data);
						}
					});
				},
				select: function(event, ui) {
					$('#cust_st_name').val(ui.item.label);
					$('#cust_st_code').val(ui.item.value);
					$('#cust_gstno').val(ui.item.value);
					document.getElementById("cust_gstno").focus();
					return false;
				}
			});
		});
		$(function() {
			$("#acc_type").autocomplete({
				source: function(request, response) {
					$.ajax({
						url: "acctype",
						type: 'post',
						dataType: "json",
						data: {
							search: request.term
						},
						success: function(data) {
							response(data);
						}
					});
				},
				select: function(event, ui) {
					$('#acc_type').val(ui.item.value);
					document.getElementById("customer_name").focus();
					return false;
				}
			});
		});
		$('#action').click(function() {
		    var acc_type = $('#acc_type').val();
			var customer_code = $('#customer_code').val();
			var customer_name = $('#customer_name').val();
			var cust_address_one = $('#cust_address_one').val();
			var cust_address_two = $('#cust_address_two').val();
			var cust_address_three = $('#cust_address_three').val();
			var cust_place = $('#cust_place').val();
			var cust_pin_code = $('#cust_pin_code').val();
			var cust_st_code = $('#cust_st_code').val();
			var cust_st_name = $('#cust_st_name').val();
			var cont_person = $('#cont_person').val();
			var cust_phone = $('#cust_phone').val();
			var cust_mobile = $('#cust_mobile').val();
			var cust_email = $('#cust_email').val();
			var cust_gstno = $('#cust_gstno').val();
			var cust_pan_no = $('#cust_pan_no').val();
			var cust_aadhar_no = $('#cust_aadhar_no').val();
			var tax_code = $('#tax_code').val();
			var opening = $('#opening').val();
			var debit = $('#debit').val();
			var credit = $('#credit').val();
			var clbalance = $('#clbalance').val();
			var transporter_name = $('#transporter_name').val();
			var bankname = $('#bankname').val();
			var bankaccno = $('#bankaccno').val();
			var bankaccname = $('#bankaccname').val();
			var ifsccode = $('#ifsccode').val();
			var due_days = $('#due_days').val();
			var remarks = $('#remarks').val();
			var id = $('#user_id').val();
			var action = $('#action').text();
			$actiontext = $('#action').text();
			if (customer_code != '' && customer_name != '' && cust_address_one != '' && cust_address_two != '' && cust_address_three != '' && cust_place != '' && cust_pin_code != '' && cust_st_code != '' && cust_st_name != '' && cont_person != '' && cust_phone != '' && cust_mobile != '' && cust_email != '' && cust_gstno != '' && cust_pan_no != '' && cust_aadhar_no != '' && tax_code != '' && opening != '' && debit != '' && credit != '' && clbalance != '' && transporter_name != '' && bankname != '' && bankaccno != '' && bankaccname != '' && ifsccode != '' && due_days != '' && remarks != '') {
				$.ajax({
					url: "action",
					method: "POST",
					data: {
					    acc_type: acc_type,
						customer_code: customer_code,
						customer_name: customer_name,
						cust_address_one: cust_address_one,
						cust_address_two: cust_address_two,
						cust_address_three: cust_address_three,
						cust_place: cust_place,
						cust_pin_code: cust_pin_code,
						cust_st_code: cust_st_code,
						cust_st_name: cust_st_name,
						cont_person: cont_person,
						cust_phone: cust_phone,
						cust_mobile: cust_mobile,
						cust_email: cust_email,
						cust_gstno: cust_gstno,
						cust_pan_no: cust_pan_no,
						cust_aadhar_no: cust_aadhar_no,
						tax_code: tax_code,
						opening: opening,
						debit: debit,
						credit: credit,
						clbalance: clbalance,
						transporter_name: transporter_name,
						bankname: bankname,
						bankaccno: bankaccno,
						bankaccname: bankaccname,
						ifsccode: ifsccode,
						due_days: due_days,
						remarks: remarks,
						id: id,
						action: action
					},
					success: function(data) {						
						$('.success').show(1000).html("Record Saved Successfully.").delay(1000).fadeOut(1000);
						load_data();
						GetCustCode();
					}
				});
			} else {
				$('.success').show(1000).html("All Fields are Required").delay(1000).fadeOut(1000);
			}
		});

		$(document).on('click', '.update', function() {
			var id = $(this).attr("id");
			$.ajax({
				url: "fetch",
				method: "POST",
				data: {
					id: id
				},
				dataType: "json",
				success: function(data) {
					$('#action').text("Update");
					$('#user_id').val(id);
					$('#acc_type').val(data.acc_type);
					$('#customer_code').val(data.customer_code);
					$('#customer_name').val(data.customer_name);
					$('#cust_address_one').val(data.cust_address_one);
					$('#cust_address_two').val(data.cust_address_two);
					$('#cust_address_three').val(data.cust_address_three);
					$('#cust_place').val(data.cust_place);
					$('#cust_pin_code').val(data.cust_pin_code);
					$('#cust_st_code').val(data.cust_st_code);
					$('#cust_st_name').val(data.cust_st_name);
					$('#cont_person').val(data.cont_person);
					$('#cust_phone').val(data.cust_phone);
					$('#cust_mobile').val(data.cust_mobile);
					$('#cust_email').val(data.cust_email);
					$('#cust_gstno').val(data.cust_gstno);
					$('#cust_pan_no').val(data.cust_pan_no);
					$('#cust_aadhar_no').val(data.cust_aadhar_no);
					$('#tax_code').val(data.tax_code);
					$('#opening').val(data.opening);
					$('#debit').val(data.debit);
					$('#credit').val(data.credit);
					$('#clbalance').val(data.clbalance);
					$('#transporter_name').val(data.transporter_name);
					$('#bankname').val(data.bankname);
					$('#bankaccno').val(data.bankaccno);
					$('#bankaccname').val(data.bankaccname);
					$('#ifsccode').val(data.ifsccode);
					$('#due_days').val(data.due_days);
					$('#remarks').val(data.remarks);
				}
			})
		});
		$(document).on('click', '.delete', function() {
			var id = $(this).attr("id");
			if (confirm("Are you sure you want to remove this data?")) {
				var action = "Delete";
				$.ajax({
					url: "action",
					method: "POST",
					data: {
						id: id,
						action: action
					},
					success: function(data) {
						$('.success').show(1000).html("Record Deleted Successfully.").delay(1000).fadeOut(1000);
						load_data();
						GetCustCode();
					}
				})
			} else {
				return false;
			}
		});
		//To check the input is not empty and send the cursor to next input.
		$(document).ready(function() {
			$('input').on('keypress', function(e) {
				if (e.which == 13) {
					switch ($(this).attr('id')) {
						case 'customer_code':
							if (!document.getElementById("customer_code").value.length == 0) {
								$('#customer_name').focus();
								e.preventDefault();
								break;
							}
							case 'customer_name':
								if (!document.getElementById("customer_name").value.length == 0) {
									$('#cust_address_one').focus();
									e.preventDefault();
									break;
								}
								case 'cust_address_one':
									if (!document.getElementById("cust_address_one").value.length == 0) {
										$('#cust_address_two').focus();
										e.preventDefault();
										break;
									}
									case 'cust_address_two':
										if (!document.getElementById("cust_address_two").value.length == 0) {
											$('#cust_address_three').focus();
											e.preventDefault();
											break;
										}
										case 'cust_address_three':
											if (!document.getElementById("cust_address_three").value.length == 0) {
												$('#cust_place').focus();
												e.preventDefault();
												break;
											}
											case 'cust_place':
												if (!document.getElementById("cust_place").value.length == 0) {
													$('#cust_pin_code').focus();
													e.preventDefault();
													break;
												}
												case 'cust_pin_code':
													if (!document.getElementById("cust_pin_code").value.length == 0) {
														$('#cust_st_code').focus();
														e.preventDefault();
														break;
													}
													case 'cust_st_code':
														if (!document.getElementById("cust_st_code").value.length == 0) {
															$('#cust_gstno').focus();
															e.preventDefault();
															break;
														}
														case 'cust_gstno':
															if (!document.getElementById("cust_gstno").value.length == 0) {
																if (document.getElementById("cust_gstno").value.length == 15) {
																	$cust_gstno = document.getElementById('cust_gstno').value;
																	$company_st_code = document.getElementById('company_st_code').value;
																	$custgstno = $cust_gstno.substr(0, 2);
																	if ($custgstno == $company_st_code) {
																		document.getElementById('tax_code').value = 'GST-B2B';
																		document.getElementById('cust_pan_no').value = $cust_gstno.substr(2, 10);
																	}
																	if ($custgstno != $company_st_code) {
																		document.getElementById('tax_code').value = 'I-GST';
																		document.getElementById('cust_pan_no').value = $cust_gstno.substr(2, 10);
																	}
																	$('#cust_aadhar_no').focus();
																	e.preventDefault();
																break;
																}
																if (document.getElementById("cust_gstno").value.length == 2) {
																	$cust_gstno = document.getElementById('cust_gstno').value;
																	$company_st_code = document.getElementById('company_st_code').value;
																	$custgstno = $cust_gstno.substr(0, 2);
																	document.getElementById('tax_code').value = 'GST-B2C';
																	document.getElementById('cust_pan_no').value = '-';
																	$('#cust_aadhar_no').focus();
																	e.preventDefault();
																break;
																}
															}
															case 'cust_aadhar_no':
																if (!document.getElementById("cust_aadhar_no").value.length == 0) {
																	$('#cont_person').focus();
																	e.preventDefault();
																	break;
																}
																case 'cont_person':
																	if (!document.getElementById("cont_person").value.length == 0) {
																		$('#cust_phone').focus();
																		e.preventDefault();
																		break;
																	}
																	case 'cust_phone':
																		if (!document.getElementById("cust_phone").value.length == 0) {
																			$('#cust_mobile').focus();
																			e.preventDefault();
																			break;
																		}
																		case 'cust_mobile':
																			if (!document.getElementById("cust_mobile").value.length == 0) {
																				$('#cust_email').focus();
																				e.preventDefault();
																				break;
																			}
																			case 'cust_email':
																				if (!document.getElementById("cust_email").value.length == 0) {
																					$('#opening').focus();
																					e.preventDefault();
																					break;
																				}
																				case 'opening':
																					if (!document.getElementById("opening").value.length == 0) {
																						$('#debit').focus();
																						e.preventDefault();
																						break;
																					}
																					case 'debit':
																						if (!document.getElementById("debit").value.length == 0) {
																							$('#credit').focus();
																							e.preventDefault();
																							break;
																						}
																						case 'credit':
																							if (!document.getElementById("credit").value.length == 0) {
																								$('#clbalance').focus();
																								e.preventDefault();
																								break;
																							}
																							case 'clbalance':
																								if (!document.getElementById("clbalance").value.length == 0) {
																									$('#transporter_name').focus();
																									e.preventDefault();
																									break;
																								}
																								case 'transporter_name':
																									if (!document.getElementById("transporter_name").value.length == 0) {
																										$('#bankname').focus();
																										e.preventDefault();
																										break;
																									}
																									case 'bankname':
																										if (!document.getElementById("bankname").value.length == 0) {
																											$('#bankaccno').focus();
																											e.preventDefault();
																											break;
																										}
																										case 'bankaccno':
																											if (!document.getElementById("bankaccno").value.length == 0) {
																												$('#bankaccname').focus();
																												e.preventDefault();
																												break;
																											}
																											case 'bankaccname':
																												if (!document.getElementById("bankaccname").value.length == 0) {
																													$('#ifsccode').focus();
																													e.preventDefault();
																													break;
																												}
																												case 'ifsccode':
																													if (!document.getElementById("ifsccode").value.length == 0) {
																														$('#due_days').focus();
																														e.preventDefault();
																														break;
																													}
																													case 'due_days':
																														if (!document.getElementById("due_days").value.length == 0) {
																															$('#remarks').focus();
																															e.preventDefault();
																															break;
																														}
																														case 'remarks':
																															if (!document.getElementById("remarks").value.length == 0) {
																																$('#action').focus();
																																e.preventDefault();
																																break;
																															}																															
					}
				}
			});
		});
	});

	function exportToExcel(tableID, filename = '') {
		var downloadurl;
		var dataFileType = 'application/vnd.ms-excel';
		var tableSelect = document.getElementById(tableID);
		var tableHTMLData = tableSelect.outerHTML.replace(/ /g, '%20');
		filename = filename ? filename + '.xls' : 'export_excel_data.xls';
		downloadurl = document.createElement("a");
		document.body.appendChild(downloadurl);
		if (navigator.msSaveOrOpenBlob) {
			var blob = new Blob(['\ufeff', tableHTMLData], {
				type: dataFileType
			});
			navigator.msSaveOrOpenBlob(blob, filename);
		} else {
			downloadurl.href = 'data:' + dataFileType + ', ' + tableHTMLData;
			downloadurl.download = filename;
			downloadurl.click();
		}
	}
