// JavaScript Document
//invoice item: goods_id,unit_id,unit_name,goods_name,export_price,quantity,serial

g_pageobj['{_UID_}']= 
{
	m_uid:"{_UID_}",
	m_invoice_prd:[],
	m_invoice_prd_index:"'{_INVOICE_TYPE_}{_UID_}'",
	m_invoice_id:0,
	m_lastdiv:null,
	m_last_row:null,
	m_direct_print:false,
	m_input_quantity_selected:null,
	m_goods_category_id:0,
	m_invoice_type:"{_INVOICE_TYPE_}",
	m_contact_type:{_CONTACT_TYPE_},
	initGlobalDataMap:function(){
		
		var key="edit"+this.m_invoice_type+"invoiceid";
		if(!isEmpty(g_data[key]))
		{
			var id=document.getElementById("pageheader"+this.m_uid);
			if(id.innerHTML.indexOf(" [SỬA]")<0)
				id.innerHTML=id.innerHTML+" [SỬA]";
			if(isEmpty(g_data[this.m_invoice_prd_index]))
			{
				g_data[this.m_invoice_prd_index]=new Array();
				this.invoiceLoad(g_data[key]);
			}else
			{
				if(g_data[this.m_invoice_prd_index].length==0)
					this.invoiceLoad(g_data[key]);
			}
			this.m_invoice_id=g_data[key];
			this.m_invoice_prd=g_data[this.m_invoice_prd_index];
			//showGoodsForEximSelect();
		}
		else
		{
			if(isEmpty(g_data[this.m_invoice_prd_index]))
			{
				g_data[this.m_invoice_prd_index]=new Array();
			}
			this.m_invoice_id=0;
			this.m_invoice_prd=g_data[this.m_invoice_prd_index];
			//if(this.m_invoice_type!="")
			//	showGoodsForEximSelect();
		}	
		
		return true;
	},
	invoiceLoad:function(invoice_id)
	{
		
		var url='?page='+this.m_invoice_type+'&subpage=invoice_load&invoice_type='+this.m_invoice_type+'&invoice_id='+invoice_id;
		
        jQuery.ajax({
            url: url,
            data: "",
            type: "GET",
            success: function(data)
			{
                
				var iditem=null;
				if(isDataOk(data))
				{
					data=getDataOk(data);
					var argoods=data.split(g_split_row);
                    
					if(argoods.length<2)
					{
 						showMsg("Thất bại","NẠP PHIẾU");
						return;
					}
					var goods;
					goods=argoods[0].split(g_split_val);
					//invoice_date,vat,invoice_money_discount,total_cost,comment,money_debit,invoice_discount,store_id,bank_account_id,trade_user_id
					
					if(goods.length==10)
					{
						var invoice_date=document.getElementById("invoice_date");
						var invoice_vat=document.getElementById("invoice_vat");
						var invoice_discount=document.getElementById("invoice_discount");
						var invoice_debit=document.getElementById("invoice_debit");
						var invoice_pay=document.getElementById("invoice_pay");
						var invoice_bankaccount=document.getElementById("invoice_bankaccount");
						var invoice_comment=document.getElementById("invoice_comment");
						
						
						invoice_date.value=goods[0];
						invoice_vat.value=formatNumber(goods[1]);
						invoice_discount.value=formatNumber(goods[2]);
						invoice_debit.value=formatNumber(goods[5]);
						invoice_comment.innerHTML=goods[4];
						selectItemByValue("select_store_id",goods[7]);
						selectItemByValue("invoice_bankaccount",goods[8]);
					}	
					//contact_id,contact_name,contact_address,phone
					goods=argoods[1].split(g_split_val);
					if(goods.length==4)
					{
						onContactSelect(goods[0],goods[1],goods[2],goods[3]);
					}	
                    
					for(i=2;i<argoods.length;i++)
					{
						goods=argoods[i].split(g_split_val);
						//goods_id,unit_id,unit,goods_name,unit_price,quantity,serial,warranty,discount,id_exim_invoice_details,color_name
						
						if(goods.length>=10)
						{
							var ii=new Object();
							ii.id=goods[0];
							ii.unit_id=goods[1];
							ii.unit_name=goods[2];
							ii.name=goods[3];
							ii.price=goods[4];
							ii.quantity=goods[5];
							ii.serial=goods[6];
                            ii.warranty=goods[7];
							ii.extra_id=goods[9];
                            if(goods.length>=11)
                                ii.color_name=goods[10];
							ii.quantity_max=0;
							
							g_pageobj['{_UID_}'].m_invoice_prd[g_pageobj['{_UID_}'].m_invoice_prd.length]=ii;
						}
					}
					g_pageobj['{_UID_}'].m_invoice_id=invoice_id;
					g_pageobj['{_UID_}'].invoiceShow();
					hideModal();
				}
				else
					showMsg("Thất bại","NẠP PHIẾU");
            },
            error: function (){
				hideModal();
				showMsg("Thất bại","NẠP PHIẾU");
			}
        });
    	return false;
	},
	ajaxOnReturn:function()
	{
		if (req.readyState != 4) 
			return;
		if (req.status != 200) 
			return;
		var param=req.dataExtra,value=req.responseText;

		if(param=="them")
		{
			value=value.replace("_INVOICE_SUBMIT_","g_pageobj['{_UID_}'].themHangGhi(this)");
			showModal(value,"THÊM HÀNG");
		}
		if(param=="invoice_save")
			g_pageobj['{_UID_}'].invoiceSaveDone(value);//value=ok+id
		if(param=="goodsaddbycode")
			g_pageobj['{_UID_}'].goodsAddByCodeReceivedInfo(value);
	},

	goodsAdd:function()
	{
		ajaxGetURL("/?page=kb_danhmuchang&action=add","them",this.ajaxOnReturn);
	},
	themHangGhi:function(f)
	{
		var goods_id = f.elements.namedItem("goods_id").value;
		var type=f.elements.namedItem("type").value;
     	var form = $(f);
    	var url = form.attr('action');
        var dataString = form.serialize();
		
        jQuery.ajax({
            url: url,
            data: dataString,
            type: "POST",
            success: function(data)
			{
				var isok="";
				if(data.length>2)
					isok=data.substr(0,2);
				var iditem=null;
				if((isok=="ok")&&(type=="add"))
				{
					setModalHeading("OK - "+f.elements.namedItem("tenhang").value);
					document.getElementById("txtgoodsfind").value=f.elements.namedItem("tenhang").value;
					f.elements.namedItem("tenhang").value="";
					f.elements.namedItem("mahang").value="";
					f.elements.namedItem("dvthang").value="";
					f.elements.namedItem("giale").value="";
					f.elements.namedItem("giabuon").value="";
					f.elements.namedItem("gianhap").value="";
					f.elements.namedItem("ghichu").value="";
					f.elements.namedItem("tenhang").focus();
					if(themhangGhi_Refresh&&typeof(themhangGhi_Refresh)=="function")themhangGhi_Refresh(data);
				}
            },
            error: function (){
				hideModal();
				showMsg("Thất bại","THÊM SỬA HÀNG");
			}
        });
		
    	return false;
	},
	goodsAddByCode:function(goods_code)
	{
		ajaxGetURL('?page='+this.m_invoice_type+'&subpage=chitietmathang&goods_code='+uriEncode(goods_code),"goodsaddbycode",this.ajaxOnReturn);
	},
	goodsAddByCodeReceivedInfo:function(info)
	{
		var prds=info.split(g_split_val);
		if(prds.length!=8)
			return;
		goods_id=prds[0];
		goods_name=prds[1];
		goods_code=prds[2];
		unit_name=prds[3];
		import_price=parseFloat(prds[4]);
		export_price=parseFloat(prds[5]);
		export_price_wholesale=parseFloat(prds[6]);
		unit_id=prds[7];
		g_pageobj['{_UID_}'].invoiceGoodsAdd(goods_id,unit_id,goods_name,unit_name,export_price);
	},
	//id,name,price,quantity,serial,extra_id
	invoiceGoodsAdd:function(goods_id,unit_id,goods_name,unit_name,export_price,color_name,optional_extra_id,optional_quantity_max)
	{
		
		//phieunhaptralai
		if(!isEmpty(optional_extra_id)&&!isEmpty(optional_quantity_max))
		{
			for(i=0;i<this.m_invoice_prd.length;i++)
			{
				if(this.m_invoice_prd[i].extra_id==optional_extra_id)
				{
					if(this.m_invoice_prd[i].quantity>=this.m_invoice_prd[i].quantity_max)
						return;
					this.m_invoice_prd[i].quantity++;
					g_pageobj['{_UID_}'].invoiceShow();
					return;
				}
			}
		}
		//goods_id,unit_id,unit_name,goods_name,export_price,quantity,serial
		var ii=new Object();
		ii.id=goods_id;
		ii.unit_id=unit_id;
		ii.unit_name=unit_name;
		ii.name=goods_name;
		ii.price=export_price;
		ii.quantity=1;
		ii.serial="";
		ii.extra_id=optional_extra_id;
		ii.quantity_max=optional_quantity_max;
        ii.color_name=color_name;
		this.m_invoice_prd[this.m_invoice_prd.length]=ii;
		this.invoiceShow();
	},
	invoiceGoodsRemove:function(index)
	{
		
		if(index<0)
			return;
		if(index>=this.m_invoice_prd.length)
			return;
		if(index<0)
		{
			showModal("Lỗi. Không tìm thấy hàng");
			return;
		}
		var ii=this.m_invoice_prd.splice(index,1);
		ii=null;delete ii;
		this.invoiceShow();
	},
	invoiceGoodsSerialChange:function(index)
	{
		if(index<0)
			return;
		if(index>=this.m_invoice_prd.length)
			return;
		var serialid="serial"+index;
		inputserial=document.getElementById(serialid);
		var serial=inputserial.value;
		this.m_invoice_prd[index].serial=serial;
	},
	invoiceGoodsPriceQuantityChange:function(index)
	{
		
		if(index<0)
			return;
		if(index>=this.m_invoice_prd.length)
			return;
		var ii=this.m_invoice_prd[index];
	
		var quantityid="quantity"+index;
		var priceid="price"+index;
		inputquantity=document.getElementById(quantityid);
		inputprice=document.getElementById(priceid);
		this.m_invoice_prd[index].quantity=parseNumber(inputquantity.value);
		this.m_invoice_prd[index].price=parseNumber(inputprice.value);
		this.invoiceShowSum();
	},
	invoicePriceQuantityInputToIndex:function(inputid)
	{
		if ((typeof(inputid)== "undefined")||(inputid==null))
		{
			return -1;
		}
		var id=inputid.id;
	
		if(id.substring(0,8)=="quantity")
		{
			index=id.substring(8,id.length);
			return index;
		}
		if(id.substring(0,5)=="price")
		{
			index=id.substring(5,id.length);
			return index;
		}
		if(id.substring(0,6)=="serial")
		{
			index=id.substring(6,id.length);
			return index;
		}		
		return -1;
	},
	invoiceGoodsPriceQuantityFocus:function(inputid)
	{
		if ((typeof(this.m_input_quantity_selected)!= "undefined") &&(typeof(inputid)!= "undefined")&&(this.m_input_quantity_selected!=null))
		{
			if(this.m_input_quantity_selected.id==inputid.id)
			{
				this.m_input_quantity_selected.select();
				return;
			}
			this.invoiceGoodsPriceQuantityBlur(this.m_input_quantity_selected);
		}
		this.m_input_quantity_selected=inputid;
		//noi bat div co id item{goods_id}
		var index=this.invoicePriceQuantityInputToIndex(this.m_input_quantity_selected);
		if(index>=0)
		{
			var itemid="item"+index;
			var divitem=document.getElementById(itemid);
			if(typeof(divitem)=="object");
			{
				divitem.style.background="white";
			}
		}
		//m_input_quantity_selected.value="";
		this.m_input_quantity_selected.select();
	},
	invoiceGoodsPriceQuantityBlur:function(inputid)
	{
		var index=this.invoicePriceQuantityInputToIndex(inputid);
		if(index<0)
			return;
		var itemid="item"+index;
		var divitem=document.getElementById(itemid);
		if(typeof(divitem)=="object");
			divitem.style.background="#F0F0F0";
	},
    invoiceGoodsChangeColor:function(index){
        
        DlgSelect.showGoodsColor('g_pageobj[\'{_UID_}\'].invoiceGoodsChangeColorDo', new Array(index.toString()));

    },
    invoiceGoodsChangeColorDo:function(index,color_id,color_name,short_name){
        hideModalBlock();
       
        if(index<0)
            return;
        if(index>=this.m_invoice_prd.length)
            return;
        if(index<0)
        {
            showModal("Lỗi. Không tìm thấy hàng");
            return;
        }
        var ii=this.m_invoice_prd[index];  
        ii.color_name=color_name;
        this.invoiceShow();
    },
    invoiceGoodsPopup:function(index)
	{
		if(index<0)
			return;
		if(index>=this.m_invoice_prd.length)
			return;
		if(index<0)
		{
			showModal("Lỗi. Không tìm thấy hàng");
			return;
		}
		var ii=this.m_invoice_prd[index];   
		var menu="";
		menu+="<a href='javascript:void(0)' onclick=\"javascript:g_pageobj['{_UID_}'].hangCapnhatSerial("+index+")\" class='list_item_cmd'>Cập nhật Serial"+"</a>";
		menu+="<a href='javascript:void(0)' onclick=\"javascript:g_pageobj['{_UID_}'].invoiceGoodsChangeColor("+index+")\" class='list_item_cmd'>Đổi màu sắc"+"</a>";
		menu+="<a href='javascript:void(0)' onclick=\"javascript:g_pageobj['{_UID_}'].invoiceGoodsRemove("+index+")\" class='list_item_cmd'>Xóa hàng khỏi phiếu"+"</a>";
		showModal(menu,"MENU HÀNG");
	},
	invoiceShow:function()
	{
		var bhideprice=false;
		if((this.m_invoice_type=="phieuxuatchuyenkho")||(this.m_invoice_type=="phieunhapchuyenkho"))
			bhideprice=true;
		var item="";
		for(i=0;i<this.m_invoice_prd.length;i++)
		{
			var ii=this.m_invoice_prd[i];
			item+="<div class=\"divpt\">";
			item+="<div class=\"divpr\" id=\"item"+i+"\" style='border-bottom: 1px dotted #6C0;padding:1px'>";
	
			item+="<div class='divpc padding' style='width:50%;'>"
                +"<a href=\"javascript:void(0)\" onclick=\"g_pageobj['{_UID_}'].invoiceGoodsRemove("
                +i+")\"><span class='goods_cmdremove'>×</span>"
                +"<a href=\"javascript:void(0)\" onclick=\"g_pageobj['{_UID_}'].invoiceGoodsPopup("
                +i+")\"><span class='goods_cmd'>"+ii.name+"</span>";
            if(ii.unit_name.length>0)
                item+=" *"+ii.unit_name+"*"; 
            item+="</a>";
            if(!isEmpty(ii.color_name))
                item+=" <br><span class='goods_color'>- "+ii.color_name+"</span>";
            item+="</div>";
			item+="<div class='divpc padding' style='width:15%;'>"
               +"<input type='text' class='input_invoice' style='text-align:left;width:100%' size='1' value='"+ii.serial+"' id=\"serial"+i+"\"";
			item+=" onchange=\"g_pageobj['{_UID_}'].invoiceGoodsSerialChange("+i+")\" ";
			item+="onfocus=\"g_pageobj['{_UID_}'].invoiceGoodsPriceQuantityFocus(this)\"></div>";
		
			item+="<div class='divpc padding' style='width:12%;'><input type='text' class='input_invoice' style='text-align:right;width:100%' size='1' value='"+formatNumber(ii.quantity)+"' id=\"quantity"+i+"\"";
			item+=" onchange=\"g_pageobj['{_UID_}'].invoiceGoodsPriceQuantityChange("+i+")\" ";
			item+="onfocus=\"g_pageobj['{_UID_}'].invoiceGoodsPriceQuantityFocus(this)\"></div>";
			if(bhideprice==false)
			{
				item+="<div class='divpc padding' style='width:23%;'><input type='text' class='input_invoice' style='text-align:right;width:100%' size='5' value='"+formatNumber(ii.price)+"' id=\"price"+i+"\"";
			}
			else
			{
				item+="<div style='width:0%;display: none'><input type='text' style='text-align:right;width:0%' size='0' value='"+formatNumber(ii.price)+"' id=\"price"+i+"\"";
			}
			item+=" onchange=\"g_pageobj['{_UID_}'].invoiceGoodsPriceQuantityChange("+i+")\" ";
			item+="onfocus=\"g_pageobj['{_UID_}'].invoiceGoodsPriceQuantityFocus(this)\"></div>";
			
			item+="</div>";
			item+="</div>";
		}
		
		div_invoicegoodslist=document.getElementById("div_invoicegoodslist");
		div_invoicegoodslist.innerHTML=item;
		this.invoiceShowSum();
	},
	invoiceShowSum:function()
	{
		var sum=this.invoiceGetSum();
		invoice_sum=document.getElementById("invoice_sum");
        if(isEmpty(invoice_sum))return;
		invoice_sum.value=formatNumber(sum);
		this.invoiceCalc();
	},
	invoiceGetSum:function()
	{
		
		var sum=0;
		for(i=0;i<this.m_invoice_prd.length;i++)
		{
			sum+=this.m_invoice_prd[i].quantity*this.m_invoice_prd[i].price;
			
		}
		
		var invoice_moneyrate=document.getElementById("invoice_moneyrate");
		if(invoice_moneyrate===null){invoice_moneyrate=0;}else invoice_moneyrate=invoice_moneyrate.value;
		return sum;
	},
	invoiceGetRawData:function()
	{
		
		var data="";
		for(i=0;i<this.m_invoice_prd.length;i++)
		{
			var ii=this.m_invoice_prd[i];
			if(data.length>0)
				data+=g_split_val;
			data+=ii.id+g_split_val+ii.unit_id+g_split_val+formatNumber(ii.quantity)+g_split_val+formatNumber(ii.price)+g_split_val+uriEncode(ii.serial);
            data+=g_split_val;
            if(isEmpty(ii.color_name))
                data+="";
            else
                data+=uriEncode(ii.color_name);
            data+=g_split_val;
			if(isEmpty(ii.extra_id))
				data+="";
			else
				data+=ii.extra_id;
		}
		
		return data;
	},
	invoiceCalc:function()
	{
		var invoice_sum=document.getElementById("invoice_sum");
		var invoice_vat=document.getElementById("invoice_vat");
		var invoice_discount=document.getElementById("invoice_discount");
		var invoice_sumreal=document.getElementById("invoice_sumreal");
		var invoice_pay=document.getElementById("invoice_pay");
		var invoice_debit=document.getElementById("invoice_debit");
        if(isEmpty(invoice_sum))return;

		invoice_sumreal.value=formatNumber(parseNumber(invoice_sum.value)*(1+parseNumber(invoice_vat.value)/100)-parseNumber(invoice_discount.value));
		invoice_pay.value=formatNumber(parseNumber(invoice_sumreal.value)-parseNumber(invoice_debit.value));
		//invoice_debit.value="";
	},
	invoiceCalcPayDebt:function(ischangefrompay)
	{
		var invoice_sumreal=document.getElementById("invoice_sumreal");
		var invoice_pay=document.getElementById("invoice_pay");
		var invoice_debit=document.getElementById("invoice_debit");
		if(ischangefrompay)
		{
			invoice_debit.value=formatNumber(parseNumber(invoice_sumreal.value)-parseNumber(invoice_pay.value));	
		}
		else{
			invoice_pay.value=formatNumber(parseNumber(invoice_sumreal.value)-parseNumber(invoice_debit.value));	
		}
	},
	invoiceCheckValidQuantity:function()
	{
		
		if(this.m_invoice_prd.length<1)
		{
			showModal("Phiếu chưa có mặt hàng nào. Không thể ghi!");
			return false;
		}
		
		if(this.m_invoice_type=="phieunhaptralai")
		{
			for(i=0;i<this.m_invoice_prd.length;i++)
			{
				if(!isEmpty(this.m_invoice_prd[i].quantity_max))
				{
					if(this.m_invoice_prd[i].quantity>this.m_invoice_prd[i].quantity_max)
					{
						showMsg("<b>"+this.m_invoice_prd[i].name+"</b>: số lượng vượt quá cho phép("+this.m_invoice_prd[i].quantity+">"+this.m_invoice_prd[i].quantity_max+")");
						return false;
					}
				}
			}
		}
		return true;
	},
	invoiceSave:function()
	{
		if(!this.invoiceCheckValidQuantity())
			return false;
		var buttonghi=document.getElementById("buttonghi");
		if(isObject(buttonghi))buttonghi.disabled=true;

		var contact_id=getSelectVal("select_contact");
		var store_id=getSelectVal("select_store_id");
		
		var ncontactname=document.getElementById("ncontactname");
		var ncontactcomment=document.getElementById("ncontactcomment");
		var ncontactaddress=document.getElementById("ncontactaddress");
		var ncontactphone=document.getElementById("ncontactphone");
		var store_id_swap=document.getElementById("store_id_swap");
		var invoice_id_swap=document.getElementById("invoice_id_swap");
		var invoice_date=document.getElementById("invoice_date");
		var sncontactname="",sncontactcomment="",sncontactaddress="",sncontactphone="";	
		if(ncontactname===null){}else sncontactname=ncontactname.value;
		if(ncontactcomment===null){}else sncontactcomment=ncontactcomment.value;
		if(ncontactaddress===null){}else sncontactaddress=ncontactaddress.value;
		if(ncontactphone===null){}else sncontactphone=ncontactphone.value;
		if(store_id_swap===null){}else store_id_swap=store_id_swap.value;
		if(invoice_id_swap===null){}else invoice_id_swap=invoice_id_swap.value;
		
		//phieunhapchuyenkho&phieuxuatchuyenkho
		if((this.m_invoice_type=="phieuxuatchuyenkho"))
		{
			if(parseInt(store_id)==parseInt(store_id_swap))
			{
				alert("Kho Xuất và Kho Nhập phải khác nhau");
				if(isObject(buttonghi))buttonghi.disabled=false;
				return;
			}
		}
		//var invoice_vat=document.getElementById("invoice_vat");
		//if(isEmpty(invoice_vat)){invoice_vat="";}else invoice_vat=invoice_vat.value;

		var invoice_discount=document.getElementById("invoice_discount");
		if(invoice_discount===null){invoice_discount="";}else invoice_discount=invoice_discount.value;
		
		var invoice_debit=document.getElementById("invoice_debit");
		if(invoice_debit===null){invoice_debit="";}else invoice_debit=invoice_debit.value;
		
		var invoice_sum=document.getElementById("invoice_sum");
		if(invoice_sum===null){invoice_sum="";} else invoice_sum=invoice_sum.value;
		
		var invoice_vat=document.getElementById("invoice_vat");
		if(invoice_vat===null){invoice_vat="";}else invoice_vat=invoice_vat.value;
		
		var invoice_comment=document.getElementById("invoice_comment");
		if(invoice_comment===null){invoice_comment="";}else invoice_comment=invoice_comment.value;
		
		var invoice_bankaccount=document.getElementById("invoice_bankaccount");
		if(invoice_bankaccount===null){invoice_bankaccount="";} else invoice_bankaccount=invoice_bankaccount.value;
		
		if(isNaN(contact_id))
		{
			showModal("Chưa xác định đối tác. Hãy chọn từ danh sách hoặc  nhập tên tìm để hiện ra danh sách");
			if(isObject(buttonghi))buttonghi.disabled=false;
			return;
		}
		if(isNaN(store_id))
		{
			showModal("Chưa xác định kho hàng");
			if(isObject(buttonghi))buttonghi.disabled=false;
			return;
		}
		
		sncontactname=uriEncode(sncontactname);
		sncontactcomment=uriEncode(sncontactcomment);
		sncontactaddress=uriEncode(sncontactaddress);
		sncontactphone=uriEncode(sncontactphone);
		invoice_comment=uriEncode(invoice_comment);
		
		var url='?page='+this.m_invoice_type+'&subpage=invoice_save&invoice_type='+this.m_invoice_type+'&invoice_discount='+invoice_discount+
			'&ncontactname='+sncontactname+'&invoice_vat='+invoice_vat+'&invoice_comment='+invoice_comment+
			'&ncontactcomment='+sncontactcomment+'&ncontactaddress='+sncontactaddress+'&ncontactphone='+sncontactphone+
			'&invoice_debit='+invoice_debit+'&contact_id='+contact_id+'&store_id='+store_id+
			'&store_id_swap='+store_id_swap+'&invoice_id_swap='+invoice_id_swap+
			'&bank_account='+invoice_bankaccount+
			'&invoice_id='+this.m_invoice_id+
			'&invoice_date='+invoice_date.value+
			'&data='+this.invoiceGetRawData();
           
		ajaxGetURL(url,"invoice_save",this.ajaxOnReturn);
	},
	invoiceClear:function()
	{
		var id=document.getElementById("pageheader"+this.m_uid);
		if(id!=null)
		{
			var header=id.innerHTML;
			header=header.replace(" [SỬA]","");
			id.innerHTML=header;
		}	
		var key="edit"+this.m_invoice_type+"invoiceid";
		g_data[key]=null;
		this.m_invoice_id=0;
		while(this.m_invoice_prd.length>0)
		{
			var ii=this.m_invoice_prd.splice(0,1);
			ii=null;
			delete ii;
		}
		delete g_data[this.m_invoice_prd_index];
		g_data[this.m_invoice_prd_index]=null;
		this.m_invoice_prd=null;

		//this.m_invoice_prd.length=0;
		var div_invoicegoodslist=document.getElementById("div_invoicegoodslist");
		div_invoicegoodslist.innerHTML="";
		
		var invoice_vat=document.getElementById("invoice_vat");
		invoice_vat.value="";
		var invoice_discount=document.getElementById("invoice_discount");
		invoice_discount.value="";
		invoice_sum=document.getElementById("invoice_sum");
        if(!isEmpty(invoice_sum))
        	invoice_sum.value="";
		invoice_sumreal=document.getElementById("invoice_sumreal");
		invoice_sumreal.value="";
		var invoice_pay=document.getElementById("invoice_pay");
		invoice_pay.value="";
		var invoice_debit=document.getElementById("invoice_debit");
		invoice_debit.value="";
		var invoice_bankaccount=document.getElementById("invoice_bankaccount");
		invoice_bankaccount.value="0";
		var invoice_comment=document.getElementById("invoice_comment");
		invoice_comment.value="";
			
       
		UiControl.inputWithClearButtonOnClear("select_contact");
        UiControl.inputWithClearButtonOnClear(UiControl.goodsSearchInputGetInputId("idcontact"));
	},
	invoiceNew:function()
	{
	
		this.invoiceClear();
		this.initGlobalDataMap();
		var buttonghi=document.getElementById("buttonghi");
		buttonghi.disabled=false;
	
	},

	invoicePrint:function()
	{
		if(this.m_invoice_id==0)
		{
			alert('Ch­ưa có phiếu');
			return;
		}
        
		window.open('/?page=in&type='+this.m_invoice_type+'&id='+this.m_invoice_id,'_blank');
	},
	invoiceSaveDone:function(ret)
	{
		var buttonghi=document.getElementById("buttonghi");
		
		if(isDataOk(ret))
		{
			this.m_invoice_id=parseInt(getDataOk(ret));
            
			showMsg("Đã ghi phiếu");
			//this.invoiceClear();

			if(isObject(buttonghi))
			{
				buttonghi.disabled=true;
				buttonghi.style.visibility="visible";
			}
		}
		else
		{
			this.m_invoice_id=0;
			showMsg("(Lỗi. Không ghi được phiếu): "+ret);
			if(isObject(buttonghi))
			{
				buttonghi.disabled=true;
				buttonghi.style.visibility="visible";//visible,hidden
			}
		}
	},
	showpage:function(pageno)
	{
		fhtk=document.getElementById("fhangtonkho");
		fhtk.pageno.value=pageno;
		fhtk.submit();
	},
	resetpageno:function()
	{
		fhtk=document.getElementById("fhangtonkho");
		fhtk.pageno.value=1;
	},
	showcategory:function()
	{
		fhtk=document.getElementById("fhangtonkho");
		fhtk.pageno.value=1;
		fhtk.submit();
	},
	contactAddDo:function()
	{
		ncontactname=document.getElementById("ncontactname");
		ncontactaddress=document.getElementById("ncontactaddress");
		ncontactphone=document.getElementById("ncontactphone");
		ncontactcomment=document.getElementById("ncontactcomment");		
		
		dlgncontactname=document.getElementById("dlgncontactname");
		dlgncontactaddress=document.getElementById("dlgncontactaddress");
		dlgncontactphone=document.getElementById("dlgncontactphone");
		dlgncontactcomment=document.getElementById("dlgncontactcomment");
		
		ncontactname.value=dlgncontactname.value;
		ncontactaddress.value=dlgncontactaddress.value;
		ncontactphone.value=dlgncontactphone.value;	
		ncontactcomment.value=dlgncontactcomment.value;
		
		select_contact=document.getElementById("select_contact");
		select_contact.innerHTML="<option value=''>+"+ncontactname.value+"["+ncontactaddress.value+"]["+ncontactphone.value+"]</option>";
	},
	contactAdd:function()
	{
		var html="";
		html+="<table style='border-collapse: collapse' width='100%' border='0' cellspacing='0' cellpadding='0' >";
		html+="<tr><td>Tên</td><td><input type='text' id='dlgncontactname' size='20' style='width:100%'></td></tr>";
		html+="<tr><td>ĐC</td><td><input type='text' id='dlgncontactaddress' size='20' style='width:100%'></td></tr>";
		html+="<tr><td>ĐT</td><td><input type='text' id='dlgncontactphone' size='20' style='width:100%'></td></tr>";
		html+="<tr><td>Ghi chú</td><td><input type='text' id='dlgncontactcomment' size='20' style='width:100%'></td></tr>";
		html+="<tr><td colspan='2'><input type='button' value='OK' onclick=\"return showModalBlockEnd(true,g_pageobj['{_UID_}'].contactAddDo)\" style='width:120px;float:left;height:30px'>";
		html+="<input type='button' value='Cancel' onclick='return showModalBlockEnd(false)' style='width:120px;float:right;height:30px'>";
		html+="</td></tr></table>";
		showModalBlock(html,"Thêm đối tác");
	},

};	
function showGoodsForEximSelect()
{
	DlgSelect.iddivshow=document.getElementById("iddivgoodsselectlist");
	//DlgSelect.showlistheading=0;
	DlgSelect.pagesize=10;
	DlgSelect.showGoods(0,'onGoodsSelect',0,getSelectVal("select_store_id"),0,'',g_pageobj['{_UID_}'].m_invoice_type);

}
function onSaleUserSelect(id,name,mobile,comment)
{
	
	var sale_user=document.getElementById("sale_user");
	sale_user.value=name;
	var sale_user_id=document.getElementById("sale_user_id");
	sale_user_id.value=id;
	hideModalBlock();
}
function onContactSelect(id,name,address,phone)
{   
    var fname=name;
	if((address!==undefined)&&(address.length>0))
		fname+=" ("+address+")";
	if((phone!==undefined)&&(phone.length>0))
		fname+=" ("+phone+")";
    UiControl.inputTextSetValue("idcontact",fname,"select_contact",id);
	hideModalBlock();	
}

function onNewContact(data_doitac){
    if(!Array.isArray(data_doitac))
    {
        alert("Lỗi");return;
    }
    onContactSelect(data_doitac[0],data_doitac[1],data_doitac[3],data_doitac[4]);
}

function onGoodsSelect(goods_id,unit_id,goods_name,unit_name,stock,export_price,color_name)
{
    if(isEmpty(unit_name))unit_name="";
	g_pageobj['{_UID_}'].invoiceGoodsAdd(goods_id,parseNumber(unit_id),goods_name,unit_name,parseNumber(export_price),color_name);
	//hideModalBlock();	
    setModalHeading("Đã thêm: "+goods_name);
}
function onGoodsCodeSelect(goods_code){
    g_pageobj['{_UID_}'].goodsAddByCode(goods_code);
}