var allowsave = true;
$(function(){
	$(".editable input, .editable select").change(ValueChanged);
	//$("editable .item-checkbox").is( ':checked' ) ? ValueChanged() : ValueUnChanged();
	$(".editable_detail input, .editable_detail select, .editable_detail textarea").change(ValueChanged_detail);
	$(".editable_comment input, .editable_comment select, .editable_comment textarea ").change(ValueChanged_comment);
	//$(".delete").click(DeleteClicked);
	$(".delete1").click(DeleteClicked);
	$("#addSec").click(AddRow);
	$("#addGrp").click(AddRow);
	$("#addManager").click(AddRow);
	$("#addCat").click(AddRow);
	$("#addUnit").click(AddRow);
	$("#addDept").click(AddRow);
	$("#addEq").click(AddRow);
	$("#adduser").click(AddRow);
	$("#sp_name").keyup(supplier);
	$("#cus_name").keyup(customer);

	$("#reset").click(function(){
	    	location.reload(true);
	});
	
	if ($.browser.msie && parseInt($.browser.version) < 8) {
	  alert("Your Version IE Less Than 8, you can not run this website in full action");
	}
	$(".qty1").keyup(checkqty);
	$(".qty1").click(checkqty);
	$(".serial_endr").blur(chQty);
	$(".staff_name").keyup(AutoComplete_d);
	
	/*-------- add in PLANNING system -----------*/
	$("#detail").click(AddRowDetail);
	$("#addcomment").click(AddRowComment);
	
	// $(".btnsave").click(checkqty); // the button save
	/*---------- document =----------*/
	$("#doc_code").blur(IfLastRowAddNewRowForm);
	/*====================== action the REMINDER SYSTEM ============================== added by kognxiong*/
	/*====================== action the REMINDER SYSTEM ============================== */
	$("#datetime").change(selRom);
	$("#datetime2").change(selRom);
	//$("#type_out").load(alert('asdfadsf'));
	
	if(document.getElementById('type_out')){
        var selRow = document.getElementById('type_out').value;
		if(selRow == ""){
			$('#out_selected').hide();
			$('#in_selected').hide();
		}else if(selRow == 1){
			$('#out_selected').hide();
			$('#in_selected').show();
		}
		else if(selRow == 2){
			$('#out_selected').show();
			$('#in_selected').hide();
		}
    }
	$("#other_host").keyup(Auto_other);
	$(".select-all").on("click", function() {
		//if($(".item-checkbox").prop("checked", $(this).prop("checked"))){
		//	ValueChanged_all();
		//}
		$(".item-checkbox").prop("checked", $(this).prop("checked"));
		$(".item-checkbox").is( ':checked' ) ? ValueChanged_chall() : ValueChanged_unall(); 
	
	});
		
	$(".send").on("click", function() {
		$(".send").is( ':checked' ) ? $(this).closest("tr").find(".send").val('checked') : $(this).closest("tr").find(".send").val('undchecked');
		
	});
	$(".back").on("click", function() {
		$(".back").is( ':checked' ) ? $(this).closest("tr").find(".back").val('checked') : $(this).closest("tr").find(".back").val('unchecked');
		
	});
	// $('.section_selected').hide(function()
	// {
	//	$('.selemp').find('option').remove();
	// });
	
	/*================ onload in reminder sys ===============*/
	window.onload = function() {
	  var emp_id = $('#selemp :selected').val();
		if(emp_id == ""){
			$('.section_selected').hide();
			$('.selemp').find('option').remove();
		}
		$('#username').focus();
	};	
		
	$('.dept_selected').hide();
	$('.other_host_selected').hide();
	// Check the action when form load
	if(document.getElementById('pres_id')){
        var selRow = document.getElementById('pres_id').value;
	
		if(selRow != ""){
			selSection();
		}
    }
	
	/*==================== END REMINDER ====================*/
				
    $(".date").datepicker({
    	dateFormat: "yy-mm-dd",
		changeMonth: true,
		changeYear: true
	});
	
	 $('.datetime').datepicker({
    	duration: '',
        showTime: true,
        constrainInput: false
     });	

	 // enable plug-in by DETHSADA
	// $("#editor").htmlarea();
	// add document type 
	
    $(".addcomment").click(function(){
		var cal = this;
		var id = $(this).closest("tr").find(".id").val();
		var orgunitid = $(this).closest("tr").find("#orgunitid").val();
		var short_name = $(this).closest("tr").find("#short_name").val();
		var comm = $(this).closest("tr").find("#comment_").val();
		var name = prompt("ກະລຸນາປ້ອນ ຄໍາເຫັນ");
    	if (name != null){
    	$.getJSON("tableloader.php?&command=edit&name="+name+"&id="+id+"&orgunitid="+orgunitid+"&short_name="+short_name, function(data){
    		//$("#addcomment").append(", "+name);
			if(comm==""){
				$(cal).closest("tr").find("#addcomment").append(" ("+short_name+") "+name);
			}else{
				$(cal).closest("tr").find("#addcomment").append(" ,("+short_name+") "+name);
			}
			
    	});
    	}
    })
	
	


})


function showDocOut(aa){
	selRow = aa;
	//var selRow = document.getElementById('type_out').value;
	if(selRow != "" && (selRow == 1 || selRow == 11)){
		$('#out_selected').hide();
		$('#in_selected').show();
	}else if(selRow != "" && selRow == 2){
		$('#out_selected').show();
		$('#in_selected').hide();
	}else if(selRow != "" && selRow == 3){
		$('#out_selected').show();
		$('#in_selected').show();
	}else{
		//$('#out_selected').hide();
		//$('#in_selected').hide();
	}
}
/*---------- check for Supplier Exist or Not-----------------------*/
function supplier(){
	
	var $currentrow = $(this).closest("tr");
    var $list = $currentrow.find(".list");
    var value = $(this).val();
    $list.empty();
	
    if (value != ""){
		//alert(value);
		$('.sp_name').autocomplete("receive/list_supplier.php", {
				width: 260,
				matchContains: true,
				selectFirst: false
		});
		$('.sp_name').result(function(event, data, formatted) {
				$(this).closest("table").find(".sp_id").val(data[1]);
				$(this).closest("table").find("#sp_name").val(data[0]);
				$(this).closest("table").find("#sp_tel").val(data[2]);
				$(this).closest("table").find("#sp_addr").val(data[3]);
				$(this).closest("table").find("#sp_email").val(data[4]);
		});
    }
}
/*---------- check for Customer Exist or Not-----------------------*/
function customer(){
	
	var $currentrow = $(this).closest("tr");
    var $list = $currentrow.find(".list");
    var value = $(this).val();
    $list.empty();
	
    if (value != ""){
		//alert(value);
		$('.cus_name').autocomplete("sale/list_customer.php", {
				width: 260,
				matchContains: true,
				selectFirst: false
		});
		$('.cus_name').result(function(event, data, formatted) {
				$(this).closest("table").find(".cus_id").val(data[1]);
				$(this).closest("table").find("#cus_name").val(data[0]);
				$(this).closest("table").find("#cus_tel").val(data[2]);
				$(this).closest("table").find("#cus_addr").val(data[3]);
				$(this).closest("table").find("#cus_email").val(data[4]);
		});
    }
}

function duplicate(txt){
	$(".eq_code_auto").each(function(){ 
		var thiscode = this.value;
    	if (thiscode != ""){
	    	// Check exist code in the page
    		var count = 0;
    		$(".eq_code_auto").each(function(){
    			if (this.value == thiscode) count++;
    		})
	    	if (count > 1){
	    		alert("ເລກລະຫັດ "+thiscode+" ມີຢູ່ໃນລາຍການແລ້ວ");
	    		allowsave = false;
	    	} else {
	    		allowsave = true;
	    	}
    	}	
	});
	
}
/*----- When disttribute to another in branch -----------*/
function duplicatedist(txt){
	$(".eq_code_auto_d").each(function(){ 
		var thiscode = this.value;
    	if (thiscode != ""){
	    	// Check exist code in the page
    		var count = 0;
    		$(".eq_code_auto_d").each(function(){
    			if (this.value == thiscode) count++;
    		})
	    	if (count > 1){
	    		alert("ເລກລະຫັດ "+thiscode+" ມີຢູ່ໃນລາຍການແລ້ວ");
	    		allowsave = false;
	    	} else {
	    		allowsave = true;
	    	}
    	}	
	});
	
}  
/*----- When delivery to D2D -----------*/
function duplicated(txt){
	$(".serials_t").each(function(){ 
		var thiscode = this.value;
    	if (thiscode != ""){
	    	// Check exist code in the page
    		var count = 0;
    		$(".serials_t").each(function(){
    			if (this.value == thiscode) count++;
    		})
	    	if (count > 1){
	    		alert("ເລກ serial: "+thiscode+" ມີຢູ່ໃນລາຍການແລ້ວ");
	    		allowsave = false;
	    	} else {
	    		allowsave = true;
	    	}
    	}	
	});
	
} 

function selDept(){
	//var sel_dept_id = document.getElementById('section').value;
	//if(sel_sec_id != ""){
		var $currentrow = $(this).closest("form");		
		var $list = $currentrow.find(".seldept");
		var value = $(this).val();
		$list.empty();		
		//if(value !="") {	
			$.getJSON("reminder/admin/list_dept.php", function(data){		// ?r_id="+this.value
				for (i in data) {
					$list.append("<option value='"+data[i][0]+"'>"+data[i][1]+"</option>\n");					
				}
			})
		//} else {
		//	$list.append("<span>--select--</span>");
		//}
	//}else{
	
	//}
}


function AddRow(){
		var $newrow = $(".template tr").clone();
		//$newrow.find(".eq_name_auto").keyup(AutoComplete);
		//$newrow.find(".eq_code_auto").keyup(AutoComplete);
		$newrow.find(".e_name").keyup(AutoComplete_emp); // in reminder system when add emp
		$newrow.find(".staff_name").keyup(AutoComplete_d);
		// $newrow.find(".staff_name").blur(HideList); 
		/*----------*/
		$newrow.find(".date").removeAttr("id");
		$newrow.find(".eq_code_auto").removeAttr("id");
		$newrow.find(".eq_code_auto").removeClass("ac_input");
		$newrow.find(".eq_code_auto").removeClass("valid");
		$newrow.find(".date").removeClass("hasDatepicker");
		$newrow.find(".date").datepicker({
	    	dateFormat: "yy-mm-dd",
			changeMonth: true,
			changeYear: true
		});
	
		$newrow.find(".qty1").removeAttr("id");
		$newrow.find(".qty1").keyup(checkqty);
		$newrow.find(".qty1").click(checkqty);
		/*------------------ add to check only ----------------*/
		$newrow.find(".delete").click(DeleteClicked);
		$newrow.find(".delete1").click(DeleteClicked1);
		// $newrow.find("input,select").change(IfLastRowAddNewRow);
		$(".editable").append($newrow);
		ReorderEditable()
}
/*new s*/
function HideList(){
	var $elem = $(this);
    setTimem(function(){
        var $list = $elem.closest("tr").find(".list");
        $list.empty();
    }, 200);
}
/*---- on blur in document -----*/
function IfLastRowAddNewRowForm(){
	if ($(".template").closest("tr").next().length == 0){
		AddRow();
	}
}
function IfLastRowAddNewRow(){
	if ($(this).closest("tr").next().length == 0){
		AddRow();
	}
}

/*--------------- to check the Qty when receive new product Serial Number ----------*/
function chQty(){
	var cal = this;

	/*---get serial number --*/				
	var serial_start = $(this).closest("tr").find(".serial_start").val();
	var serial_end = $(this).closest("tr").find(".serial_endr").val();
	var eid = $(this).closest("tr").find(".id").val();
	
	if(serial_start != "" && serial_end != ""){
		//alert('start'+ serial_start + '+ serial_end' + serial_end);
		$.get("geQty.php", {"eid": eid, "serial_start": serial_start, "serial_end": serial_end}, function(data){
			if (data != null){
				var remain = new Number(data);
				// var newamount = new Number(serial_end.value);
				if (remain <= 0){
					alert('ເລກ Serial_end ຕ້ອງໃຫ່ຍກວ່າ ຫຼື ເທົ່າກັບ Serial_start....!')
					$(serial_end).closest("tr").find(".serial_endr").show();
					$(cal).closest("tr").find(".qtyr").val("");
									
				}else{
					$(cal).closest("tr").find(".qtyr").val(remain);
				}
			}
		})
	}
	
	
}
/*--------------- to check the ----------*/

/*------------- calculate the due price when recieve new product ------*/
function cal_recieve(){
	var cal = this;
	var due = $(this).closest("tr").find(".due").val();
	var total_price = $(this).closest("tr").find(".total_price").val();
	var paid = $(this).closest("tr").find(".paid").val();
	var total_price_r = total_price.replace(/,/g, '');
	var paid_r = paid.replace(/,/g, '');
	var total = total_price_r - paid_r;
	if(total >=0 ){
		$(cal).closest("tr").find(".due").val(total);
	}else{
		alert("ທ່ານປ້ອນລາຄາຈ່າຍ (ເງີນທີ່ຈ່າຍ) ຫຼາຍກວ່າ ລາຄາທັງໝົດທີ່ຊື້ສິນຄ້ານີ້");
		$(cal).closest("tr").find(".due").val(total);
	}
}
/*------------- calculate the total in Qoutation form------*/
function c_total(){
	var cal = this;
	var price_sale = $(this).closest("tr").find(".price_sale_q").val();
	var qty = $(this).closest("tr").find(".qtyq").val();
	var discount = $(this).closest("tr").find(".discountq").val();
	var price_sale_q = price_sale.replace(/,/g, '');
	var qty_q = qty.replace(/,/g, '');
	var discount_q = discount.replace(/,/g, '');
	
	var total =(price_sale_q*qty_q) - discount_q;
	if(total >=0 ){
		$(cal).closest("tr").find(".total").val(total);
	}else{
		alert("ກະລຸນາປ້ອນລາຄາ ຫຼື ສ່ວນຫຼຸດກ່ອນ....!");
		$(cal).closest("tr").find(".total").val(total);
	}
}
/*---------------- check serial number exist or not --------------*/
function chSerialSell(){
/**/
	var cal = this;
	var due = $(this).closest("tr").find(".due").val();
	var price_sale = $(this).closest("tr").find(".price_sale").val();
	var paid = $(this).closest("tr").find(".paid_s").val();
	var discount = $(this).closest("tr").find(".discount").val();
	var price_sale_s = price_sale.replace(/,/g, '');
	var paid_s = paid.replace(/,/g, '');
	var discount_s = discount.replace(/,/g, '');
	var total = (price_sale_s - paid_s)-discount_s;
	//alert(price_sale_s+" - "+paid_s + "="+total);
	/*---get serial number --*/				
	var serials1 = $(this).closest("tr").find(".serials_t").val();
	var eid = $(this).closest("tr").find(".id").val();
	//var w_id = document.getElementById("wid").value;
	//var did = document.getElementById("h_doc_id").value;
	//alert('price_sale '+ price_sale + '+ price_buy='+paid+' paid' + paid_s+ "/");
	$.get("chserialsel.php", {"eid": eid, "serials": serials1}, function(data){
		if (data != null){
			var remain = new Number(data);
			// var newamount = new Number(serial_end.value);
			if (remain < 1){
				// $(serial_end).closest("td").find(".serials").text("ເລກ Serial ບໍ່ມີໃນຖານຂໍ້ມູນ");
				//$(serial).closest("td").find(".qty1").val(remain);
				alert('ເລກ Serial ທີ່ທ່ານປ້ອນ ບໍ່ມີໃນຖານຂໍ້ມູນ ຫຼື ຖືກຂາຍແລ້ວ....!')
				$(serials).closest("td").find(".serials").show();
								
			}else{
				if(total >=0 ){
					//$(total).closest("tr").find(".due").show();
					$(cal).closest("tr").find(".due").val(total);
				}else{
					alert("ທ່ານປ້ອນລາຄາຊື້ (ເງີນທີ່ຈ່າຍ) ຫຼາຍກວ່າ ລາຄາຂາຍ");
					$(cal).closest("tr").find(".due").val(total);
				}
			}
		}
	})
	
}

/*---------------- check serial number exist or not // or SEll out --------------*/
function chSerialExist(){
/**/
	var serial_start = $(this).closest("tr").find("input[name='serial_start[]']").val();
	var serial_end = this;
	var serial_end1 = serial_end.value;
	var eid = $(this).closest("tr").find(".eq_id").val();
	var w_id = document.getElementById("wid").value;
	var did = document.getElementById("h_doc_id").value;
	// alert('start'+ serial_start + '+ end' + serial_end1)
	$.get("chserialexist.php", {"eid": eid, "w_id": w_id, "did": did, "serial_start": serial_start, "serial_end1": serial_end1}, function(data){
		if (data != null){
			var remain = new Number(data);
			var newamount = new Number(serial_end.value);
			if (remain < 1){
				$(serial_end).closest("td").find(".start_date").text("ເລກ Serial ບໍ່ມີໃນຖານຂໍ້ມູນ");
				//$(serial).closest("td").find(".qty1").val(remain);
				alert('ເລກ Serial ທີ່ທ່ານປ້ອນ ບໍ່ມີໃນຖານຂໍ້ມູນ ຫຼື ມອບໃຫ້ທີມອືນກ່ອນແລ້ວ....!')
				$(serial_start).closest("td").find(".start_date").show();
								
			}
		}
	})
	
}
/*----------------- validate qty --------------*/
function checkqty(){
	var amt = this;
	var eid = $(this).closest("tr").find(".eq_id").val();
	var w_id = document.getElementById("wid").value;
	var did = document.getElementById("h_doc_id").value;
	
	$.get("checkqty.php", {"eid": eid, "w_id": w_id, "did": did}, function(data){
		if (data != null){
			var remain = new Number(data);
			var newamount = new Number(amt.value);
			if (remain < newamount){
				$(amt).closest("td").find(".qtyerr").text("ອຸປະກອນໃນສາງມີ:"+ remain.toFixed(2));
				$(amt).closest("td").find(".qty1").val(remain);
				$(amt).closest("td").find(".qtyerr").show();
								
			}else{
				if(amt.value <= 0 ){
					$(amt).closest("td").find(".qtyerr").text("ຈໍານວນຕ້ອງໃຫ່ຍກວ່າ 0");
					$(amt).closest("td").find(".qty1").val();
					$(amt).closest("td").find(".qtyerr").show();
				}else{
					$(amt).closest("td").find(".qtyerr").hide();
				}
			}
		}
	})
	
}
function ReorderEditable(){
	var num = 0;
	$(".editable tr").each(function(){
		$(this).find("td").first().children("span").text(num);
		num++;
	})
}

// Appear Value change on Input
function ValueChanged_chall(){
	$(".item-checkbox").closest("table").find("input[name='type[]']").val("edited");
	$(".item-checkbox").closest("table").css("backgroundColor", "#FFE2BC");
	$(".item-checkbox").parent().css("border", "solid #FF9200 0.15em");
}
// Appear Value change on Input
function ValueChanged_unall(){
	$(".item-checkbox").closest("table").find("input[name='type[]']").val("unchanged");
	$(".item-checkbox").closest("table").css("backgroundColor", "#FFFFFF");
	$(".item-checkbox").parent().css("border", "dotted #000000 0.1em");
}
// Appear Value change on Input
function ValueChanged(){
	$(this).closest("tr").find("input[name='type[]']").val("edited");
	$(this).closest("tr").find("input[name='type_ict[]']").val("edited");
	$(this).closest("tr").find("input[name='orgunitid[]']").val("checked");
	$(this).closest("tr").css("backgroundColor", "#FFE2BC");
	$(this).parent().css("border", "solid #FF9200 0.15em");
}
// Appear Value unchange on Input
function ValueUnChanged(){
	$(this).closest("tr").find("input[name='type[]']").val("unchanged");
	$(this).closest("tr").find("input[name='type_ict[]']").val("unchanged");
	$(this).closest("tr").css("backgroundColor", "#FFE2BC");
	$(this).parent().css("border", "dotted #000000 0.1em");
}
// on the planning system
function ValueChanged_detail(){
	$(this).closest("tr").find("input[name='type_detail[]']").val("edited");
	$(this).closest("tr").css("backgroundColor", "#FFE2BC");
	$(this).parent().css("border", "solid #FF9200 0.15em");
}
// on the planning system
function ValueChanged_comment(){
	$(this).closest("tr").find("input[name='type_comment[]']").val("edited");
	$(this).closest("tr").css("backgroundColor", "#FFE2BC");
	$(this).parent().css("border", "solid #FF9200 0.15em");
}
// on change the destinition value
/*
function AllValueChanged(){
	$(this).closest("form").find("input[name='type[]']").val("edited");
	$(this).closest("form").css("backgroundColor", "#FFE2BC");
	$(this).parent().css("border", "solid #FF9200 0.15em");
}
*/
//
function DeleteClicked(){
	if ($(this).closest("tr").find("input[name='id[]']").val() == ""){
		$(this).closest("tr").remove();
		
	} else {
		$(this).closest("td").css("backgroundColor", "red");
		$(this).closest("tr").fadeTo("medium", 0.2);
		$(this).closest("tr").find("input[name='type[]']").val("deleted");
		$(this).closest("tr").find("input[name='type_g[]']").val("deleted");
		$(this).closest("tr").find("input[type=text], input[type=number], input[type=file], select").attr("disabled", true);
	}
	ReorderEditable();
}
function DeleteClicked1(){
	//if ($(this).closest("tr").find("input[name='eq_id[]']").val() == ""){
	if ($(this).closest("tr").find("input[name='id[]']").val() == ""){
		$(this).closest("tr").remove();
	}
}

function AutoComplete(){
	
	var $currentrow = $(this).closest("tr");
    var $list = $currentrow.find(".list");
    var value = $(this).val();
    $list.empty();
	
    if (value != ""){
		//alert(value);
		$('.eq_name_auto').autocomplete("receive/list_equipment.php", {
				width: 260,
				matchContains: true,
				selectFirst: false
		});
		$('.eq_name_auto').result(function(event, data, formatted) {
				$(this).closest("tr").find(".id").val(data[1]);
				$(this).closest("tr").find(".eq_code_auto").val(data[2]);
				$(this).closest("tr").find(".eq_name_auto").val(data[0]);
				$(this).closest("tr").find(".category_id").val(data[3]);
				$(this).closest("tr").find(".category").val(data[4]);
				$(this).closest("tr").find(".eq_unit").val(data[5]);
		});
		$('.eq_code_auto').autocomplete("receive/list_code_equipment.php", {
				width: 260,
				matchContains: true,
				selectFirst: false
		});
		
		$('.eq_code_auto').result(function(event, data, formatted) {
				$(this).closest("tr").find(".id").val(data[1]);
				$(this).closest("tr").find(".eq_code_auto").val(data[0]);
				$(this).closest("tr").find(".eq_name_auto").val(data[2]);
				$(this).closest("tr").find(".category_id").val(data[3]);
				$(this).closest("tr").find(".category").val(data[4]);
				$(this).closest("tr").find(".eq_unit").val(data[5]);
		});
    }
}
function AutoComplete_d(){
	//var wid = document.getElementById("wid").value;
	var $currentrow = $(this).closest("tr");
    var $list = $currentrow.find(".list");
    var value = $(this).val();
    $list.empty();
	
    if (value != ""){
		//alert(value);
		$('.staff_name').autocomplete("document/docin/list_staff.php", {
				width: 260,
				matchContains: true,
				selectFirst: false
		});
		$('.staff_name').result(function(event, data, formatted) {
				$(this).closest("tr").find(".id").val(data[1]);
				$(this).closest("tr").find(".staff_name").val(data[0]);
				$(this).closest("tr").find(".orgunit").val(data[2]);
				$(this).closest("tr").find(".position").val(data[3]);
				$(this).closest("tr").find(".tel").val(data[4]);
		});
    }
}
/*------------- the planning system --------------------------*/
/*---------------add workflow from section ---------*/
function AddRowDetail(){
	
	var $newrowdetail = $(".templetedetail tr").clone();
	$("#w_detail").append($newrowdetail);
	// $newrowdetail.find(".id").removeAttr("id");
	$newrowdetail.find(".date").removeClass("hasDatepicker");
	$newrowdetail.find(".date").datepicker({
	   	dateFormat: "yy-mm-dd",
		changeMonth: true,
		changeYear: true
	});
	$(".editable_detail").append($newrowdetail);
	ReorderEditable()
}
/*------- add comment from director-------*/
function AddRowComment(){
	var $newrowcomment = $(".templatecomment tr").clone();
	$("#comment").append($newrowcomment);
	$newrowcomment.find(".date").removeClass("hasDatepicker");
	$newrowcomment.find(".date").datepicker({
	    dateFormat: "yy-mm-dd",
		changeMonth: true,
		changeYear: true
	});
}

/*------------ the function print window ----------------------*/
function printWindow(){
	browserVersion = parseInt(navigator.appVersion)
	if (browserVersion >= 4) window.print()
}
/*------------ action when selling equipment -------------*/
function openPrint(c_id, s_no){
	window.open("sale/ReceiptPrint.php?c_id="+c_id+"&s_no="+s_no);
}

function openPrintQuotation(q_no){
	window.open("quotation/quotationPrint.php?q_no="+q_no);
}
/*============================ FUNCTION IN REMINDER ==================== ADDED BY KONGXIONG ==*/
/*============================ FUNCTION IN REMINDER ======================*/
/*============================ FUNCTION IN REMINDER ======================*/
function selRom(){
	var date1 = document.getElementById('datetime').value;
	var date2 = document.getElementById('datetime2').value;
	//var l1 = $("#datetime").val().length;
	//var l2 = $("#datetime2").val().length;

	if( date1 != "" && date2 != ""){
		
		var d1=date1.replace(/\s/gi, '_');
		var d2=date2.replace(/\s/gi, '_');
		
		//alert(d1);
		
		var $currentrow = $(this).closest("form");		
		var $list = $currentrow.find(".rname");
		var value = $(this).val();
		$list.empty();		
		if(value !="") {	
			$.getJSON("reminder/admin/list_room.php?date1="+d1, function(data){		// ?r_id="+this.value
				//$list.append("<label><select name='rname' id = 'rname' OnChange = 'CheckRname();'><option value = ''>--------- &#3713;&#3760;&#3749;&#3768;&#3737;&#3762;&#3776;&#3749;&#3767;&#3757;&#3713; ---------</option>");
				for (i in data) {
				
					$list.append("<option value='"+data[i][0]+"'>"+data[i][1]+"</option>\n");					
				
				}
				//$list.append("</select></label>");
			})
		} else {
			$list.append("<span>--select--</span>");
		}

	}
}
// in reminder system 
function selSection(){
	var selRow = document.getElementById('pres_id').value;
	if(selRow != "" && selRow == 1){
		$('.other_host_selected').hide();
		$('.dept_selected').hide();
		$('.section_selected').show();
	}else if(selRow != "" && selRow == 2){
		$('.other_host_selected').hide();
		$('.dept_selected').show();
		$('.section_selected').hide(function()
			{
				$('.selemp').find('option').remove();
			}
		);
	
	}else{
		$('.section_selected').hide(function()
			{
				$('.selemp').find('option').remove();
			}
		);
		$('.dept_selected').hide();
		$('.section').empty();
		$('.other_host_selected').show();
	}
}
// in reminder system 
function selEmp(){
	var sel_sec_id = document.getElementById('section').value;
	//var selemp_id = document.getElementById('list_emp_id').value;
	
	//if(sel_sec_id != ""){
		var $currentrow = $(this).closest("form");		
		var $list = $currentrow.find(".selemp");
		var value = $(this).val();
		
		$list.empty();	
		//alert("fdsfg");		
		//if(value !="") {	
			//$.getJSON("list_emp_name.php?sec_id="+sel_sec_id+"&e_id="+selemp_id, function(data){		// ?r_id="+this.value
			$.getJSON("reminder/admin/list_emp_name.php?sec_id="+sel_sec_id , function(data){		// ?r_id="+this.value
				for (i in data) {
					$list.append("<option value='"+data[i][0]+"'>"+data[i][1]+"</option>\n");					
				}
			})
		//} else {
		//	$list.append("<span>--select--</span>");
		//}
	//}else{
	
	//}
}
// in reminder system
function selDept(){
	//var sel_dept_id = document.getElementById('section').value;
	//if(sel_sec_id != ""){
		var $currentrow = $(this).closest("form");		
		var $list = $currentrow.find(".seldept");
		var value = $(this).val();
		$list.empty();		
		//if(value !="") {	
			$.getJSON("reminder/admin/list_dept.php", function(data){		// ?r_id="+this.value
				for (i in data) {
					$list.append("<option value='"+data[i][0]+"'>"+data[i][1]+"</option>\n");					
				}
			})
		//} else {
		//	$list.append("<span>--select--</span>");
		//}
	//}else{
	
	//}
}
// in reminder system 
function Auto_other(){
	
	var $currentrow = $(this).closest("tr");
    var $list = $currentrow.find(".list");
    var value = $(this).val();
    $list.empty();
	
    if (value != ""){
		//alert(value);
		$('.other_host').autocomplete("reminder/admin/list_other.php", {
				width: 260,
				matchContains: true,
				selectFirst: false
		});
		$('.other_host').result(function(event, data, formatted) {
				$(this).closest("tr").find(".o_id").val(data[1]);
				$(this).closest("tr").find("#other_host").val(data[0]);
		});
    }
}
// in reminder system
function AutoComplete_emp(){
	
	var $currentrow = $(this).closest("tr");
    var $list = $currentrow.find(".list");
    var value = $(this).val();
    $list.empty();
	
    if (value != ""){
		//alert(value);
		$('.e_name').autocomplete("reminder/admin/list_emp.php", {
				width: 260,
				matchContains: true,
				//mustMatch: true,
				//minChars: 0,
				//multiple: true,
				//highlight: false,
				//multipleSeparator: ",",
				selectFirst: false
		});
		$('.e_name').result(function(event, data, formatted) {
				$(this).closest("tr").find(".e_id").val(data[1]);
				$(this).closest("tr").find(".e_name").val(data[0]);
				$(this).closest("tr").find(".e_email").val(data[2]);
				$(this).closest("tr").find(".e_tel").val(data[3]);
		});
    }
}