//html code: &#8593:up arrow,&#8595:down arrow,&#8594:right arrow,&#8592: left arrow,&#10003:checked
function getListObjType(fnCallBack,fnCallBackParamArray)
{
	if(g_list_objtype.length>0)
	{
		if(fnCallBack!==undefined)fnCallBack.apply(this,fnCallBackParamArray);
		return;
	}
	var url="?page=list&subpage=objtype";
	var dataString = "";
	jQuery.ajax({
			url: url,
			data: dataString,
			type: "GET",
			success: function(data)
			{
				if(isDataFail(data))
				{
					showMsg(getDataFail(data));
					return;
				}
				var rows=data.split(g_split_row);
				for(var i=0;i<rows.length;i++)
				{
					var vals=rows[i].split(g_split_val);
					if(vals.length==2)
						g_list_objtype[g_list_objtype.length]={"id":vals[0],"name":vals[1]};
				}
				
				if(g_list_objtype.length>0)
				{
					if(fnCallBack!==undefined)fnCallBack.apply(this,fnCallBackParamArray);
				}
			},
			error: function (){
				showMsg("Lỗi");
		}
	});		
}
function getListGoodsCategory()
{
	var url="?page=list&subpage=goods_category";
	var dataString = "";
	jQuery.ajax({
		url: url,
		data: dataString,
		type: "GET",
		success: function(data)
		{
			if(isDataFail(data))
			{
				showMsg("Lỗi tải về danh mục nhóm hàng");
				return;
			}
			g_list_goodscategory_raw=data;
			var rows=data.split(g_split_row);
			var start=0;
			if(rows.length<1)
				return;
			if(isDataStartWith(rows[0],g_sign_columnconfig))
				start=1;
			for(var i=start;i<rows.length;i++)
			{
				var vals=rows[i].split(g_split_val);
				if(vals.length==2)
					g_list_goodscategory[g_list_goodscategory.length]={"id":vals[0],"name":vals[1]};
			}
		},
		error: function (){
			showModal("Lỗi");
		}
	});
}


function getListObjCategory(fnCallBack,fnCallBackParamArray)
{
	if(g_list_objcategory.length>0)
	{
		if(fnCallBack!==undefined)fnCallBack.apply(this,fnCallBackParamArray);
		return;
	}
	var url="?page=list&subpage=objcategory";
	var dataString = "";
	jQuery.ajax({
			url: url,
			data: dataString,
			type: "GET",
			success: function(data)
			{
				if(isDataFail(data))
				{
					showMsg(getDataFail(data));
					return;
				}
				var rows=data.split(g_split_row);
				for(var i=0;i<rows.length;i++)
				{
					var vals=rows[i].split(g_split_val);
					if(vals.length==2)
						g_list_objcategory[g_list_objcategory.length]={"id":vals[0],"name":vals[1]};
				}
				
				if(g_list_objcategory.length>0)
				{
					if(fnCallBack!==undefined)fnCallBack.apply(this,fnCallBackParamArray);
				}
			},
			error: function (){
				showMsg("Lỗi");
		}
	});			
}
function getListStore(fnCallBack,fnCallBackParam)
{
	if(g_list_store.length>1)
	{
		if(fnCallBack!==undefined)fnCallBack(fnCallBackParam);
		return;
	}
	var url="?page=list&subpage=store";
	var dataString = "";
	jQuery.ajax({
			url: url,
			data: dataString,
			type: "GET",
			success: function(data)
			{
				if(isDataFail(data))
				{
					showMsg(getDataFail(data));
					return;
				}
				var rows=data.split(g_split_row);
				for(var i=0;i<rows.length;i++)
				{
					var vals=rows[i].split(g_split_val);
					if(vals.length==2)
						g_list_store[g_list_store.length]={"id":vals[0],"name":vals[1]};
				}
				if(g_list_store.length>0)
				{
					if(fnCallBack!==undefined)fnCallBack(fnCallBackParam);
				}
			},
			error: function (){
				showMsg("Lỗi");
		}
	});			
}

function getListEmployee(fnCallBack,fnCallBackParamArray)
{
	if(g_list_employee.length>1)
	{
		if(fnCallBack!==undefined)fnCallBack.apply(this,fnCallBackParamArray);
		return;
	}
	var url="?page=list&subpage=employee";
	var dataString = "";
	jQuery.ajax({
			url: url,
			data: dataString,
			type: "GET",
			success: function(data)
			{
				if(isDataFail(data))
				{
					showMsg(getDataFail(data));
					return;
				}
				var rows=data.split(g_split_row);
				for(var i=0;i<rows.length;i++)
				{
					if(rows[i].substr(0,g_sign_pageconfig.length)==g_sign_pageconfig)
						continue;
					if(rows[i].substr(0,g_sign_columnconfig.length)==g_sign_columnconfig)
						continue;
					var vals=rows[i].split(g_split_val);
					if(vals.length==4)
						g_list_employee[g_list_employee.length]={"id":vals[0],"name":vals[1],"mobile":vals[2],"comment":vals[3]};
				}
				if(g_list_employee.length>0)
				{
					if(fnCallBack!==undefined)fnCallBack.apply(this,fnCallBackParamArray);
				}
			},
			error: function (){
				showMsg("Lỗi");
		}
	});			
}
function showEmployeeInSelect(selectname,selectedid,showzero)
{
	if(g_list_employee.length<1)
	{
		getListEmployee(showEmployeeInSelect,[selectname,selectedid,showzero]);
		return;
	}
	var ops="";
	for(var i=0;i<g_list_employee.length;i++)
	{
		if((selectedid!==undefined)&&(i===selectedid))
			ops+="<option value='"+g_list_employee[i].id+"' selected>"+g_list_employee[i].name+"</option>";
		else
			ops+="<option value='"+g_list_employee[i].id+"'>"+g_list_employee[i].name+"</option>";
	}
	if(showzero!==undefined)
		ops="<option value=''>(nhân viên)</option>"+ops;
	
	setInnerById(selectname,ops);	
}
function showStoreInSelect(selectname,selectedid)
{
	if(g_list_store.length<1)
	{
		getListStore(showStoreInSelect,selectname);
		return;
	}
	var ops="";
	for(var i=0;i<g_list_store.length;i++)
	{
		if((selectedid!==undefined)&&(i===selectedid))
			ops+="<option value='"+g_list_store[i].id+"' selected>"+g_list_store[i].name+"</option>";
		else
			ops+="<option value='"+g_list_store[i].id+"'>"+g_list_store[i].name+"</option>";
	}
	setInnerById(selectname,ops);
}
function showBankAccountInSelect(idselect,showzero)
{
	if(g_phien_bankaccountdata.length>0)
	{
		if(showzero==1)
			DlgSelect.showDataToSelectBox(idselect,g_phien_bankaccountdata,"(Tiền mặt)");
		else
		DlgSelect.showDataToSelectBox(idselect,g_phien_bankaccountdata);
		return;
	}
	var url="?page=list&subpage=bank";
    var dataString = "";
    jQuery.ajax({
        url: url,
        data: dataString,
        type: "GET",
        success: function(data)
		{
			if(data.substr(0,g_sign_fail.length)==g_sign_fail)
			{
				showModalBlock(data.substr(g_sign_fail.length));
				return;
			}
			DlgSelect.showDataToSelectBox(idselect,data,"(Tiền mặt)");
        },
        error: function (){
			showModal("Lỗi");
		}
    });	
}
/*KHAI_BAO*/
    var m_fnCallBackAddSuccess=null;
    var m_fnCallBackEditSuccess=null;
    function kb_danhmuchang_themHang(fnCallBackAddSuccess)//ham chu khong phai chuoi
    {   
        m_fnCallBackAddSuccess=null;
        var url="?page=kb_danhmuchang&action=add";
        var dataString = "";
        jQuery.ajax({
            url: url,
            data: dataString,
            type: "GET",
            success: function(data)
            {
                m_fnCallBackAddSuccess=fnCallBackAddSuccess;
                showModalBlock(data,"Thêm hàng");	
            },
            error: function (){
                showModal("Lỗi thêm hàng");
            }
        });	
    
    }
    function kb_danhmuchang_xemHang(goodsid)
	{
        var url="?page=kb_danhmuchang&action=view&goodsid="+goodsid;
        var dataString = "";
        jQuery.ajax({
            url: url,
            data: dataString,
            type: "GET",
            success: function(data)
            {
                showModal(data,"Chi tiết hàng");	
            },
            error: function (){
                showModal("Lỗi xem chi tiết hàng");
            }
        });	
	}
    function kb_danhmuchang_suaHang(goodsid,fnCallBackEditSuccess)
	{
        m_fnCallBackEditSuccess=null;
        var url="?page=kb_danhmuchang&action=edit&goodsid="+goodsid;
        var dataString = "";
        jQuery.ajax({
            url: url,
            data: dataString,
            type: "GET",
            success: function(data)
            {
                m_fnCallBackEditSuccess=fnCallBackEditSuccess;
                showModalBlock(data,"Sửa hàng");	
            },
            error: function (){
                showModal("Lỗi thêm hàng");
            }
        });	
			
	}
    function kb_danhmuchang_themHangGhi(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)
			{
				if(isDataFail(data))
				{
					showMsg("Lỗi: "+getDataFail(data));
					return;
				}
				var iditem=null;
				if(isDataOk(data))
				{
					if(type=="edit")
					{
						
                        if(!isEmpty(m_fnCallBackEditSuccess))
                            m_fnCallBackEditSuccess(getDataOk(data));
						hideModalBlock();
					}
					else if(type=="add")
					{	
						
                        if(!isEmpty(m_fnCallBackAddSuccess))
                            m_fnCallBackAddSuccess(getDataOk(data));
						setModalHeading("OK - "+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();
					}
				}
				else
					showMsg(data,"THÊM SỬA HÀNG");
            },
            error: function (){
				hideModalBlock();
				showMsg("Thất bại","THÊM SỬA HÀNG");
			}
        });
    	return false;
	}	
    function kb_doitac_them(fnCallBackAddSuccess){
        GridStructData.data_struct=grid_data_struct_doitac;
        GridStructData.page_source="kb_danhmucdoitac";
        GridStructData.page_title="Khai báo đối tác";
        GridStructData.showEditForm(0,null,"",null,"grid",0,fnCallBackAddSuccess);
    }
/*CALL_FUNCTION_BY_STRING_NAME AND PASS ARRAY PARAMETERS
	const fnCallBackParamArray = ['p0', 'p1', 'p2'];
	window[fnCallBackName].apply(this, fnCallBackParamArray);
*/
/*GLOBAL_APP_DATA*/
var g_list_goodscategory=[];//{id,name}
var g_list_services=[];//{id,objcategory,name,unit,price,hourly,minutelimit,iscombo}						
var g_list_bankaccount=[];//[{id,code,name,bank,owner}]
var g_list_store=[];//[{id,name}]
var g_list_employee=[];//[{id,name,mobile,comment}]
var g_list_objcategory=[];//[{id,name}]
var g_list_objtype=[];//[{id,name}]
var g_list_contacttype=[{"id":1,"name":"Khách hàng"},{"id":2,"name":"Nhà cung cấp"},{"id":3,"name":"Cả hai"}];
var g_list_gender=[{"id":1,"name":"Nam"},{"id":2,"name":"Nữ"},{"id":3,"name":"Chưa xác định"}];

var g_list_goodscategory_raw="";
//PHIEN
var g_phien_obj=null;//invoiceid,costtotal,costfixed,costhourly,costmix,
					//5 discountfoods,discountdrink,discountgoods,discountservice,invoicediscount,vat,invoicemoneydiscount,
					//12 contactid,contactname,contactaddress,phone,datebirth (12->16)

var g_phien_objservicelist=[];//[{id,goodsid,name,price,unit,quantity,objcategory,from,hourly,minutelimit,iscombo,to}]
var g_phien_objgoodslist=[];//[{id,goodsid,name,price,unit,quantity,objcategory,from,hourly,minutelimit,iscombo,to}]
var g_phien_cartgoods=[];//[{id,name,unit,price,quantity}]
var g_phien_listobj=[];//[{id,name,objcategory,typename,totalcost,invoiceid,statusextra}]
var g_phien_listobj_html="";
var g_phien_listrmgoods=[];
var g_phien_refreshinterval=1000;
var g_phien_service_list="";
var g_phien_goods_list="";
var g_phien_bankaccountdata="";//{id,code,name,bank,owner}
var g_phien_idcartdes=null;

/*DATA_STRUCT*/
var grid_data_struct_doitac=[
    {name:"id",head:"",visible:0},
    {name:"name",head:"Tên đối tác",visible:1,type:"text",required:"1"},
    {name:"code",head:"Mã",visible:0,type:"text",required:"0"},
    {name:"address",head:"Địa chỉ",visible:1,type:"text",required:"0"},
    {name:"phone",head:"Điện thoại",visible:1,type:"text",required:"0"},
    {name:"birth",head:"Ngày sinh",visible:1,type:"text",required:"0",prompt:"(ngày/tháng/năm, ví dụ: 20/12/2000)"},
    {name:"email",head:"Email",visible:2,type:"text",required:"0"},
    {name:"mst",head:"Mã số thuế",visible:2,type:"text",required:"0"},
    {name:"cmnd",head:"Số căn cước",visible:2,type:"text",required:"0"},
    {name:"bank",head:"Số tài khoản",visible:2,type:"text",required:"0"},
    {name:"type",head:"Kiểu đối tác",visible:2,type:"select",required:1,data:g_list_contacttype},
    {name:"sex",head:"Giới tính nam",visible:2,type:"text",required:"0",prompt:"1: Nam, 2: Nữ"},
    {name:"comment",head:"Ghi chú",visible:2,type:"text",required:"0"},
    ];	