///GLOBAL_VARIABLE///
var g_app = "_APP_";//access from what: windows/adroid browser, android app
/*aray object wrap javascript function for each div - tab. Main page content multiple div - tab with the same function*/
var g_pageobj=[];
/*Data value and row separator*/
var g_split_val="_!_";
var g_split_row="~|~";
var g_sign_ok="ok";
var g_sign_fail="fa";
var g_sign_pageconfig="pageconfig";
var g_sign_columnconfig="columnconfig";
var g_width_gridcmd="45";
/*Window Tabs*/
var g_window_tabs=[];//content window tab contents (innerHTML of div tab)
var g_window_tabs_status=[];//content input value inside div tab
var g_window_page="";//current div tab name
/*Modal dialog*/
var g_showmodalblock=false;
var g_showmodalblockresult=false;
/*Data*/
var g_retprefixok="ok";
var g_language=new Map();//
var g_data=[];
/*Special char*/
var g_char_yes="&#10003;";
var g_colors=['#04AA6D','#f2f2f2'];
/*Status*/
var g_menubarfixeddata="";
var g_waitstatusid="";
var g_waitstatusimage="";
/*Scanner Camera*/
var g_scanner=null;
//Load UI	
//common function

function gLoadUI()
{
	/*
	var menufile="data/menu_"+g_username+".js";
	fetch(menufile)
		.then
		(
			function(response)
			{
				return response.text();
			}
		)
		.then
		(
			function(html)
			{
				var idmenu=document.getElementById("idmenu");
				idmenu.innerHTML=html;
				gLoadUI_3_MenuSearch();
			}
		);
	*/
	
	var menubar=document.getElementById("iddropdownmenubarmain");
	if(!menubar)
		return;
    g_menubarfixeddata=menubar.innerHTML;
    var obj=document.querySelector(".home");
    if(obj.offsetWidth>640)
    {
        setInnerById("iddropdownmenubarmain",menuBuildMain(true));
    }
    else
    {
        setInnerById("iddropdownmenubarmain",menuBuildMain(false));
    }
	for (i=0;i<g_menubar_items.length;i++) 
	{
		var iddivshow="idpopupmenu"+(i+1);
		var htmldiv="<div id=\""+iddivshow+"\" class=\"dropdown-content\"></div>";
		$(htmldiv).appendTo(document.body).html(menuBuildPopup(iddivshow));
	}
    gLayout();
	//gLoadUI_SW();
    //window_newtab('phieuxuatbuon');
}

function gLoadUI_SW()
{
	//
	//console.log("service worker not use in dev");
	//return;
	// Registering Service Worker
	if ('serviceWorker' in navigator){
	// Register a service worker hosted at the root of the
  	// site using the default scope.
  	navigator.serviceWorker.register('indexsw.js')
	.then(
	function(registration) 
	{
		
    	console.log('Service worker ok :', registration);

  	}, 
	/*catch*/ 
	function(error) 
	{
   	 console.log('Service worker fail:', error);
  	}
	);
	}
	else 
	{
 		console.log('Service workers not support.');
	}
}
function getDocumentSize()
{
    var w=window,d=document,e=d.documentElement,g=d.getElementsByTagName('body')[0];
    var x=w.innerWidth||e.clientWidth||g.clientWidth;
    var y=w.innerHeight||e.clientHeight||g.clientHeight;
    return {width:x, height:y}
}
function gLayout()
{
    var size=getDocumentSize();
    var subwidth=400;
    var margin=8;
    var obj=document.querySelector(".home");
    var scrollwidth=obj.scrollHeight-obj.clientHeight;
    if(scrollwidth>0)
        scrollwidth=obj.offsetWidth - obj.clientWidth+2;
    var newwidth=obj.offsetWidth-subwidth-scrollwidth-1;
    if(obj.offsetWidth>640)
    {
        setInnerById("iddropdownmenubarmain",menuBuildMain(true));
    }
    else
    {
        setInnerById("iddropdownmenubarmain",menuBuildMain(false));
    }   
    if(newwidth>subwidth)
    {
        
         $('.home_panelmain').css('background-color','#FFFFFF').css('width',(newwidth-margin)+'px')
            .css('float','left').css('margin-left',margin+'px').css('margin-top',margin+'px');
         $('.home_panelsub').css('background-color','#FFFFFF').css('width',(subwidth-2*margin)+'px')
            .css('left',newwidth+'px').css('float','left').css('margin',margin+'px');
         $('.home_bottombar').css('width','100%').css('bottom','0px');
    }
    else{
        
        $('.home_panelmain').css('background-color','#FFFFFF').css('width','100%').css('margin','0px');
        $('.home_panelsub').css('background-color','#FFFFFF').css('width','100%').css('margin','0px').css('margin-top',margin+'px');
        $('.home_bottombar').css('width','100%').css('left','0px').css('bottom','0px').css('margin-top','8px');
    }
 
}
//isrestore=0:show, <>0: restore
function showWaitStatus(isrestore=1,idshow=null,imagerestore=null){
    if(isrestore==0){
        var obj=document.getElementById(idshow);
        if(isEmpty(obj)){
            g_waitstatusid=null;
            g_waitstatusimage=null;
            return;
        }
        g_waitstatusid=idshow;
        g_waitstatusimage=imagerestore;
        setBackgroundImageById(idshow,"imgs/bt_loading.gif");
    }
    else
    {
        if(isEmpty(g_waitstatusid)) return;
        setBackgroundImageById(g_waitstatusid,g_waitstatusimage);
        g_waitstatusid=null;
        g_waitstatusimage=null;
    }
    
}
/////////////////////
	function getDataOk(recdata)
	{
		var nlensign=g_sign_ok.length;
		var nlendata=recdata.length;
		if(nlensign>nlendata)
			return "";
		if(recdata.substr(0,nlensign)!=g_sign_ok)
			return "";
		return recdata.substr(nlensign,nlendata-nlensign);
	}
	function getDataFail(recdata)
	{
		var nlensign=g_sign_fail.length;
		var nlendata=recdata.length;
		if(nlensign>nlendata)
			return "";
		if(recdata.substr(0,nlensign)!=g_sign_fail)
			return "";
		return recdata.substr(nlensign,nlendata-nlensign);
	}
	function isDataFail(recdata)
	{
		var nlensign=g_sign_fail.length;
		var nlendata=recdata.length;
		if(nlensign>nlendata)
			return false;
		if(recdata.substr(0,nlensign)!=g_sign_fail)
			return false;	
		return true;			
	}
	function isInAndroidApp()
	{
		if(typeof ISHOPMAN_APP_ANDROID === 'object' && ISHOPMAN_APP_ANDROID !== null)
			return true;
		return false;
	}
	function getDataSkipStartWith(data,start)
	{
		var nlensign=start.length;
		var nlendata=data.length;
		if(nlensign>nlendata)
			return "";
		if(data.substr(0,nlensign)!=start)
			return "";
		return data.substr(nlensign,nlendata-nlensign);
	}

	function isDataStartWith(data,start)
	{
		if(data===undefined)return false;if(data===null)return false;
		var nlensign=start.length;
		var nlendata=data.length;
		if(nlensign>nlendata)
			return false;
		if(data.substr(0,nlensign)!=start)
			return false;	
		return true;			
	}

	function isDataOk(recdata)
	{
		var nlensign=g_sign_ok.length;
		var nlendata=recdata.length;
		if(nlensign>nlendata)
			return false;
		if(recdata.substr(0,nlensign)!=g_sign_ok)
			return false;	
		return true;	
	}
	function itxt(key)
	{
		
		if(g_language.has(key))
			return g_language.get(key);
		else
			return key;
	}
	function roundNumber(value, decimals) {
    	return Number(Math.round(value + 'e' + decimals) + 'e-' + decimals);
	}
	function sleep(sleepDuration)
	{
		sleepDuration=500;
		var now = new Date().getTime();
    	while(new Date().getTime() < now + sleepDuration){ 
        /* Do nothing */ 
    	}
	}
	function addDaysToDate(date,days)
	{
		var result = new Date(date);
		result.setDate(result.getDate() + days);
		return result;
	}
	function formatDate(x)
	{
		return x.getDate()+"/"+x.getMonth()+"/"+x.getFullYear();
	}
	function formatNumber(x) 
	{
		if(!x) return "0";
		if(isNaN(x)) return "0";
		
		x=roundNumber(x,3);
		x=x.toString();
		x=x.replace(".",",");
		return x.replace(/\B(?=(\d{3})+(?!\d))/g, ".");
		//return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ".");
	}
	function parseNumber(x)
	{
		if(x===undefined)return 0;
		if(x===null)return 0;
		if(x.length==0)return 0;
		x=x.toString();
		x=x.replace(/\./g,"");
		x=x.replace(/ /g,"");
		x=x.replace(",",".");
		var ret=Number(x);
		if (isNaN(ret)) { return 0; }
		return ret;
	}
	function parseDate(x)
	{
		
		if(x===undefined)return null;
		if(x.length<3)return null;
		var vals=x.split("/");
		if(vals.length!=3)return null;
		
		return new Date(vals[2],vals[1],vals[0]);
	}
	function formatJString(x)
	{
        if(isEmpty(x))return x;
        
        if(!isString(x))return x;
 		x=x.replace(/'/g,"\\\'");		
		x=x.replace(/"/g,"\\\"");
		x=x.replace(/''/g,"\\\"");
        x=x.replace(/(?:\r\n|\r|\n)/g, '\\\r\\\n');;
		return x;
	}
	function getCurDate()
	{
		var today = new Date();
		var dd = String(today.getDate()).padStart(2, '0');
		var mm = String(today.getMonth() + 1).padStart(2, '0'); //January is 0!
		var yyyy = String(today.getFullYear());
		today = dd + '-' + mm + '-' + yyyy;
		return today.toString();
	}
/////////////////////
	function enableEle(nameid,enable)
	{
		var ele=document.getElementById(nameid);
		if(ele===undefined)return;if(ele===null)return;
		if(enable)
			ele.disabled=false;
		else
			ele.disabled=true;
	}
	function ajaxOnReturn()
	{
		if (req.readyState == 4) 
		{ // Complete
			if (req.status == 200) 
			{ 
				alert('please define local function ajaxOnReturn');
				//ajaxOnReturnOK(req.dataExtra,req.responseText);
			}
			else 
			{
			}
		}
	}
	function ajaxGetURL(url,dataExtra,functionCallback) 
	{	
		//typeof functionCallback === 'undefined'
		functionCallback=functionCallback||ajaxOnReturn;
		
		if (window.XMLHttpRequest) 
		{ // Non-IE browsers & New IE
			req = new XMLHttpRequest();
			req.onreadystatechange = functionCallback;
			req.dataExtra=dataExtra;
			try 
			{
				
				req.open("GET", url, true);
				req.setRequestHeader("Cache-Control", "no-cache");
				req.setRequestHeader("Pragma", "no-cache");
				req.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
			} 
			catch (e) 
			{
				alert(e);
			}
			req.send(null);
		} 
		else if (window.ActiveXObject) 
		{ // OLD IE
			req = new ActiveXObject("Microsoft.XMLHTTP");
			if (req) 
			{
				req.dataExtra=dataExtra;
				req.onreadystatechange = ajaxOnReturn;
				req.open("GET", url, true);
				req.send();
			}
		}
	}
	function getInnerById(id)
	{
		var obj=document.getElementById(id);
		if(obj===null)
			return "";
		else
			return obj.innerHTML;
	}

	function getValById(id)
	{
		var obj=document.getElementById(id);
		if(obj===null)
			return "";
		else
			return obj.value;
	}
	function setInnerById(id,val)
	{
		var obj=document.getElementById(id);
		if(obj===null)
			return;
		else
		{
			if(val===undefined)
				obj.innerHTML="";
			else
				obj.innerHTML=val;		
		}	
	}

	function setNumById(id,number)
	{
		var obj=document.getElementById(id);
		if(obj===null)
			return;
		else
		{
			if((number===undefined)||(number==null))
				obj.value="";
			else if(number=="")
				obj.value="";
			else if(number=="0")
				obj.value="";
			else
				obj.value=number;	
		}
	}
    function setFocus(idstring){
        var obj=document.getElementById(idstring);
		if(obj===null)
			return;
		else
		{
			obj.focus();
		}
    }
    function setBackgroundImageById(id,urlimage){
        var obj=document.getElementById(id);
        obj.style.backgroundImage="url("+urlimage+")";
    }
	function setValById(id,val)
	{
		var obj=document.getElementById(id);
		if(obj===null)
			return;
		else
		{
			if((val===undefined)||(val==null))
				obj.value="";
			else
				obj.value=val;	
		}
	}
	function getSelectVal(selectid)
	{
		var selectobj=document.getElementById(selectid);
		if(selectobj===null)
			return 0;
		else
			return selectobj.value;
	}
	function getSelectText(selectid)
	{
		var selectobj=document.getElementById(selectid);
		if(selectobj===null)
			return "";	
		var text="";
		if(selectobj.selectedIndex!==null)
			text=selectobj.options[selectobj.selectedIndex].text;
		return text;		
	}
    function selectShowDropDown(idselect){
        var obj=document.getElementById(idselect);
        obj.size=5;
    }
    function selectHideDropDown(idselect){
        var obj=document.getElementById(idselect);
        obj.size=1;
    }    
	function selectItemByValue(selectid,value)
	{
		var selectobj=document.getElementById(selectid);
		
		if(!isEmpty(selectobj))selectobj.value=value;
	}
	function setActiveControlText(text)
	{
		document.activeElement.value=text;
	}
	function uriEncode(text)
	{
		text=text.replaceAll("&","%26");
		text=text.replaceAll("@","%40");
		text=text.replaceAll("%","%25");
		text=text.replaceAll("#","%23");
		text=text.replaceAll("+","%2B");
		text=text.replaceAll("=","%3D");
		return text;
	}
	function isString(x)
	{
		if(x===undefined)return false;
		if(x===null)return false;
		if(x.constructor.name==="String")
			return true;
		return false;
	}
    function isVisiableKeyCode(keycode){
       
        var valid = 
        (keycode > 47 && keycode < 58)   || // number keys
        (keycode == 32)                  || // spacebar & return key(s) (if you want to allow carriage returns)
        (keycode > 64 && keycode < 91)   || // letter keys
        (keycode > 95 && keycode < 112)  || // numpad keys
        (keycode > 185 && keycode < 193) || // ;=,-./` (in order)
        (keycode > 218 && keycode < 223) || // [\]' (in order)
        (keycode >  128);

        return valid;

    }
    function isScrollBarVisible()
    {
        alert(document.scrollHeight+'-'+document.innerHeight)
        if(document.scrollHeight > document.innerHeight)
            return false;
        return true;

    }
	function isEmpty(value)
	{
    	return (value === null || typeof(value) === 'undefined' || value === '');
	}
	function isObject(value)
	{
		return (typeof value === 'object' && value !== null);
	}
    /***************************************CONTROL******/

/*****************************MODAL DIALOG*************************/
function showMsg(msg,header)
{
	g_showmodalblock=false;
	myModalHead=document.getElementById("myModalHead");
	if(typeof header !== 'undefined')
		myModalHead.innerHTML=header;
	else
		myModalHead.innerHTML="iShopman";
	var modal = document.getElementById('myModal');
	var myModalContent=document.getElementById("myModalContent");
	myModalContent.innerHTML="<div style='padding:10px;width:100%;-moz-box-sizing:border-box;box-sizing:border-box;text-align:center'>"+msg+"</div>";
	myModalContent.style.top="300px";
	modal.style.display = "block";
	
}
function setModalHeading(heading)
{
	myModalHead=document.getElementById("myModalHead");
	myModalHead.innerHTML=heading;
}
function showModalBlock(msg,header,idfocus,execscriptinsidehtml)
{
	if(typeof header === 'undefined')header="iShopmanOS";
	myModalHead=document.getElementById("myModalHead");
	myModalHead.innerHTML=header;
	g_showmodalblock=true;
	var modal = document.getElementById('myModal');
	var myModalContent=document.getElementById("myModalContent");
	myModalContent.innerHTML=msg;
	myModalContent.style.top="0px";
	modal.style.display = "block";	
	if(!isEmpty(idfocus))
	{
		document.getElementById(idfocus).focus();
	}
	if(execscriptinsidehtml!==undefined)
	{
		var arr = myModalContent.getElementsByTagName('script')
		for (var n = 0; n < arr.length; n++)
			eval(arr[n].innerHTML)//run script inside div
	}
}
function hideModalBlock(result)
{
	g_showmodalblockresult=result;
	g_showmodalblock=false;
	hideModal();
}
function showModalBlockEnd(result,functionCallback)
{
	g_showmodalblockresult=result;
	g_showmodalblock=false;
	hideModal();
	
	if(!isEmpty(functionCallback))
		functionCallback();
}
function showWaitModal(block)
{
	if(block===undefined)
		showModal("<div style='width:100%;text-align:center;'><img src='imgs/bt_loading.gif' style='float:center'></div>","CHỜ NẠP..");
	else
		showModalBlock("<div style='width:100%;text-align:center;'><img src='imgs/bt_loading.gif' style='float:center'></div>","CHỜ NẠP..");
}

function showModalCaption(caption)
{
	var myModalHead=document.getElementById("myModalHead");
	myModalHead.innerHTML=caption;
}
function isModalVisible()
{
    var modal = document.getElementById('myModal');
    if(modal===null)
        return false;
    return (modal.style.display=="block");
}

function showModal(msg,header,execscriptinsidehtml)
{
	g_showmodalblock=false;
	if(header===undefined)header="iShopmanOS";
	var myModalHead=document.getElementById("myModalHead");
	if(typeof header !== 'undefined')
		myModalHead.innerHTML=header;
	else
		myModalHead.innerHTML="";
	var modal = document.getElementById('myModal');
	var myModalContent=document.getElementById("myModalContent");
	myModalContent.innerHTML=msg;
	myModalContent.style.top="300px";
	modal.style.display = "block";
	if(execscriptinsidehtml!==undefined)
	{
		var arr = myModalContent.getElementsByTagName('script')
		for (var n = 0; n < arr.length; n++)
			eval(arr[n].innerHTML)//run script inside div
	}
}

function hideModal()
{
	if(g_showmodalblock==true)
		return;
	var modal = document.getElementById('myModal');
	modal.style.display = "none";
}

// When the user clicks anywhere outside of the modal, close it
document.onclick = function(event) 
{
	// Get the modal
	var modal = document.getElementById('myModal');
    if ((event.target == modal)&&(!g_showmodalblock)) {
        modal.style.display = "none";
   	}
}

/*****************************END-MODAL*************************/
/*********************************MENU********************/
/*Hide all popup menu if show*/
function window_menuhide(iddivshowcurrent)
{
   var dropdowns = document.getElementsByClassName("dropdown-content");
   var i;
   for (i = 0; i < dropdowns.length; i++) 
   {
      var openDropdown = dropdowns[i];
	  if((!isEmpty(iddivshowcurrent))&&(openDropdown.id==iddivshowcurrent.id))
		  continue;
	  openDropdown.style.display="none";
    }	
}
/* When the user clicks on the button, toggle between hiding and showing the dropdown content */
function window_menushow(iddivshow) 
{
	if(!isObject(iddivshow))
		iddivshow=document.getElementById(iddivshow);
	window_menuhide(iddivshow);
	if(iddivshow.style.display!="block")
	{
		iddivshow.style.display="block";
        iddivshow.style.width=iddivshow.parentElement.style.width;
        
		$(iddivshow).appendTo(document.body);
	}
	else
	{
		iddivshow.style.display="none";
	}
}
// Close the dropdown menu if the user clicks outside of it
window.onclick = function(event) 
{
	if(event.target.classList=='overlay')
	{
		var idpopupwindow=document.getElementById("popupwindow");
	}
	if(event.target.classList!='dropbtn') 
	{
		var dropdowns = document.getElementsByClassName("dropdown-content");
    	var i;
	    for (i = 0; i < dropdowns.length; i++) 
		{
	      var openDropdown = dropdowns[i];
		  if(openDropdown.id!="myDropdownSearch")
			  openDropdown.style.display="none";
    	}
	}
}
/*********************************WINDOW TAB********************/
function window_tablistadd(page)
{
    
	var itemtext="";
	var i=0;
	var nitem=g_menubar_popup_items.length-1;
	while(i<nitem)
	{
		if(g_menubar_popup_items[i+2]==page)
		{
			itemtext=g_menubar_popup_items[i+3];
			break;
		}
		i+=4;
	}
	if(itemtext.length<1)
		return;
   
	var myDropdownTab=document.getElementById("myDropdownTab");
   

	var htmlitem="<div id='windowtablist"+page+"' class='divtablist'>";
	htmlitem+="<div class='divtablisttitle' onclick=\"window_newtab('"+page+"')\">"+itemtext+"</div>";
	htmlitem+="<div class='divtablistbuttonclose' onclick=\"window_tablistclose('"+page+"')\">x</div>";
	htmlitem+="</div>";
	myDropdownTab.innerHTML+=htmlitem;
    
	window_tablistactive(page);
	var window_tablistbutton=document.getElementById("window_tablistbutton");
	window_tablistbutton.innerHTML=parseInt(window_tablistbutton.innerHTML)+1;
}
function window_tablistactive(page)
{
	var tablistitem = document.getElementsByClassName("divtablistfocus");
	for (i = 0; i < tablistitem.length; i++)
	{
		tablistitem[i].classList="divtablist";
	}	 
	var tablistitemcur=document.getElementById("windowtablist"+page);
    if(!isEmpty(tablistitemcur))
	    tablistitemcur.classList="divtablistfocus";		
}
function window_tablistclose(page)
{	
	window_tablistbutton.innerHTML=parseInt(window_tablistbutton.innerHTML)-1;
	document.getElementById("windowtablist"+page).remove();
	if(g_window_tabs[page])
	{
		delete g_window_tabs[page];
	}
	if(g_window_tabs_status[page])
	{
		delete g_window_tabs_status[page];
	}
	if(page==g_window_page)
	{
		g_window_page="";
		var pageclose=document.getElementById("window_"+page);
		if(pageclose)
			pageclose.remove();
	}
	else
		return;
	//xem con tab nao hay khong
	var bhas=false;
	for (key in g_window_tabs) 
	{
		window_showpage(key);
		window_tablistactive(key);
		bhas=true;
		break;
	}
	//neu khong con tab nao
	if(bhas==false)
	{
		g_window_page="";
		var pageclose=document.getElementById("window_"+page);
		if(pageclose)
			pageclose.remove();
	}
}
//Get status value of input inside div
function window_formToString(filledForm) 
{
    formObject = new Object;
    filledForm.find("input, select, textarea").each(function() 
	{
        if (this.id) {
            elem = $(this);
            if (elem.attr("type") == 'checkbox' || elem.attr("type") == 'radio') {
                formObject[this.id] = elem.attr("checked");
            } else {
                formObject[this.id] = elem.val();
            }
        }
    });
    formString = JSON.stringify(formObject);
    return formString;
}
//Restore input value status inside div
function window_stringToForm(formString, unfilledForm) {
	if(isEmpty(formString))
		return;
	if(unfilledForm===null)
		return;
    formObject = JSON.parse(formString);
    unfilledForm.find("input, select, textarea").each(function() {
        if (this.id) {
            id = this.id;
            elem = $(this); 
            if (elem.attr("type") == "checkbox" || elem.attr("type") == "radio" ) {
                elem.attr("checked", formObject[id]);
            } else {
                elem.val(formObject[id]);
            }
        }
    });
}
function window_refresh()
{
	window_menuhide();
	if(isEmpty(g_window_page))
		return;
	showWaitModal();
	ajaxGetURL("/?page="+g_window_page+"&uimode=full","window_refresh"+g_split_val+g_window_page,window_ajaxOnReturn);
}
function window_showpage(page)
{
    if((g_window_page=="")&&(page!=""))
    {
        var pagecur=document.getElementById("window_initpage");
        if(!isEmpty(pagecur))
            pagecur.remove();
    }
	//neu trung voi hien tai, bo qua
	if(page==g_window_page)
		return;
    
	if(g_window_page.length>0)
	{	
		var pagecur=document.getElementById("window_"+g_window_page);
		g_window_tabs[g_window_page]=pagecur.innerHTML;
		g_window_tabs_status[g_window_page]=window_formToString($("#window_"+g_window_page));
		pagecur.remove();
	}
	g_window_page=page;
	var htmldiv="<div id='window_"+g_window_page+"' class='home'></div>";//class="divtab"
	$(htmldiv).appendTo(document.body).html(g_window_tabs[g_window_page]).fadeIn('fast');
	window_stringToForm(g_window_tabs_status[g_window_page],$("#window_"+g_window_page));
    gLayout();
}
function window_showpagecurrentcontent(value)
{
	if(g_window_page.length<1)
		return;
	var pagecur=document.getElementById("window_"+g_window_page);
	pagecur.innerHTML=value;
    gLayout();
}

function window_newtab(page,param="")
{
	if(page=="ht_dangxuat")
	{
		window.open("/?page=dangxuat","_self");
		return false;
	}
	if(isEmpty(g_window_tabs[page]))
	{
		showWaitModal();
		ajaxGetURL("/?page="+page+"&uimode=full&"+param,"window_newtab"+g_split_val+page,window_ajaxOnReturn);
	}
	else
	{
		window_showpage(page);
		window_tablistactive(page);
	}
}
function window_newtabreceive(page,value)
{	
	hideModal();
	g_window_tabs[page]=value;
	window_showpage(page);
	window_tablistadd(page);
}
function window_showrefresh(page,value)
{
	hideModal();
	if(g_window_page.length<1)
		return;
	if(page!=g_window_page)
		return;
	g_window_tabs[page]=value;
	g_window_tabs_status[g_window_page]="";
	var pagecur=document.getElementById("window_"+g_window_page);
	pagecur.remove();
	var htmldiv="<div id='window_"+g_window_page+"' class='home'></div>";
    
	$(htmldiv).appendTo(document.body).html(g_window_tabs[g_window_page]).fadeIn('fast');
    gLayout();
}
function window_ajaxOnReturn()
{
	//trang thai: 1->2, neu khong thanh cong bi mat mang hay server down se khong toi 3-4
	if (req.readyState == 4) 
	{ 
		if (req.status == 200) 
		{ 
			var arparams=req.dataExtra.split(g_split_val);
			if(arparams.length==2)
			{
				if(arparams[0]=="window_newtab")
					window_newtabreceive(arparams[1],req.responseText);
				else if(arparams[0]=="window_refresh")
                    window_showrefresh(arparams[1],req.responseText);
				else
					hideModal();
			}
		}
	}
    // Network error (i.e. connection refused, access denied due to CORS, etc.)
	else if(req.readyState==0)
	{
		
	}
	else
	{
		
	}
}
//******************************************************************************************************************/
//common function
function gViewContactInfo(contact_id)
{
	showWaitModal();	
   	var url = "?page=list_details&subpage=contact";
    var dataString = "contact_id="+contact_id;
	
    jQuery.ajax({
            url: url,
            data: dataString,
            type: "GET",
		success: function(data)
		{
			hideModal();
			showMsg(data,"Xem đối tác");
	    },
        error: function (){
			hideModal();
			showMsg("Thất bại","Xem đối tác");
		}
   });
   return false;	
}


function divToggleNext(ele,functionCallback) {
	
    //var ele = document.getElementById(elementId);
	if(isEmpty(ele))
		return;
	ele.classList.toggle("active");
	ele=ele.nextElementSibling;
	if(isEmpty(ele))
		return;
	
    if((ele.style.display == "block")||((ele.className=="collapsiblecontentexpand")&&(ele.style.display==""))) {
            ele.style.display = "none";
    }
    else {
        ele.style.display = "block";
    }
	if(functionCallback!==undefined)
		functionCallback();
} 

function ajaxJQuery()
{
   	var url="?page=getlist&type=store_list";
    var dataString="";
    jQuery.ajax({
        url: url,
        data: dataString,
        type: "GET",
        success: function(data)
		{
			if(isDataFail(data))
			{
				showModalBlock(getDataFail(data));
				return;
			}
			else if(isDataOk(data))
			{
				hideModal();
			}
			else
				showModal("Lỗi");
        },
        error: function (){
			showModal("Lỗi");
		}
    });
}
function ajaxJQueryForm(f)
{
	var type_id = f.elements.namedItem("type_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)
		{
			
		},
		error: function (){
			hideModal();
			showMsg("Thất bại","");
		}
	});
	return false;
}
//UI_SHOW_START
function g_ShowStoreInSelect(select_id,nofirstzero)
{
	var url = "?page=list&subpage=store";
    var dataString = "";
    jQuery.ajax({
        url: url,
        data: dataString,
		async: false,
        type: "GET",
        success: function(data)
		{
			var arstore=data.split(g_split_row);
			var selectinner="";
			for(i=0;i<arstore.length;i++)
			{
				var stores=arstore[i].split(g_split_val);
				selectinner+="<option value="+stores[0]+">"+stores[1]+"</option>";
			}
			if(nofirstzero===undefined)
			{
				selectinner="<option value=0>(kho hàng)</option>"+selectinner;
			}	
			var obj_select_id=document.getElementById(select_id);
			obj_select_id.innerHTML=selectinner;
		},
        error: function (){
			
			showMsg("Thất bại","DANH SÁCH KHO");
		}
    });
}
//UI_SHOW_END
	function DlgInput(arFields,prompt,fnCallBack)
	{
		var retparam="";
		var html="<div style='width:100%;-moz-box-sizing:border-box;box-sizing:border-box;padding:5px;line-height:150%'>";
		html+="<p>"+prompt+"</p>";
		for(var i=0;i<arFields.length;i++)
		{
			var id="g_inputdialog_text"+i;
			if(retparam!="")retparam+=",";retparam+=id;
			html+=arFields[i]+": <input type=\"text\" id=\""+id+"\" prompt=\"\" style=\"width:100%;margin-bottom:5px\"><br>";
		}
		html+="</div>";
		html+="<input type=\"button\" class=\"modalbutton modalbuttoncancel\" style=\"width:50%\" value=\"Bỏ qua\" onclick=\"hideModalBlock(false)\">";
		html+="<input type=\"button\" class=\"modalbutton modalbuttonok\" style=\"width:50%\" value=\"Đồng ý\" onclick=\"return "+fnCallBack+"("+retparam+")\">";
		
		showModalBlock(html,'Nhập',"g_inputdialog_text0");		
	}
	function DlgGetText(prompt,fnCallBack,fnParamArray)
	{
		var html="<div style='width:100%;-moz-box-sizing:border-box;box-sizing:border-box;padding:5px;line-height:150%'>";
		html+="<p>"+prompt+"</p>";
		html+="<input type=\"text\" id=\"g_inputdialog_text\" prompt=\"\" style=\"width:100%;margin-bottom:5px\"><br>";
		html+="</div>";
		html+="<input type=\"button\" class=\"modalbutton modalbuttoncancel\" style=\"width:50%\" value=\"Bỏ qua\" onclick=\"hideModalBlock(false)\">";
		html+="<input type=\"button\" class=\"modalbutton modalbuttonok\" style=\"width:50%\" value=\"Đồng ý\" onclick=\"return "+fnCallBack+"(g_inputdialog_text";
        if(Array.isArray(fnParamArray))
        {
            for(i=0;i<fnParamArray.length;i++){
                html+=",'"+formatJString(fnParamArray[i])+"'";
            }
        }
        html+=")\">";
		
		showModalBlock(html,'Nhập',"g_inputdialog_text");
	}
/*TRANS_TYPE
GOODS: goods_id,goods_name,goods_code,unit_name,unit_id,category_name,category_id,export_price,wholesale_price,is_material,is_product
GOODS_EXIM: goods_id,goods_name,goods_code,unit_name,unit_id,quantity,unit_price,discount,serial
INVOICE: invoice_id,invoice_no,invoice_date,reason,reason_other,comment,contact_id,contact_name,contact_address,contact_phone,sale_id,sale_realname
*/
//*START_CLASS_CONTROL
class UiControl
{
    static goodsSearchInputGetInputId(idcontainter)
    {
        return idcontainter+"input";
    }
    static inputTextSetValue(idcontainter,strvalue,idval,val){
        var inputid=idcontainter+"input";
        setValById(inputid,"");
        UiControl.inputWithClearButtonOnInput(inputid);
        setValById(inputid,strvalue);
        if(!isEmpty(idval))
            setValById(idval,val);
       
        UiControl.inputWithClearButtonOnInput(inputid);
    }
   
    static contactSelectInputCreate(idcontainter,idval,strplaceholder,fnCallBackOnContactSelect)
    {
        var idinput=idcontainter+"input";
        var idimagestatus=idcontainter+"imagestatus";
        var iddatalist=idcontainter+"datalist";
        var img="imgs/customers.png";
        var strout='<input autoComplete="off" placeholder="'+strplaceholder+'" id="'+idinput+'"'+
            ' class="inputwithbutton_inputex" type="text" style="padding-right: 26px;"'+
            ' oninput="showWaitStatus(0,\''+idimagestatus+'\',\''+img+'\');DlgSelect.showContactForEximSelect(\''+idinput+'\',\''+iddatalist+'\',\''+idval+'\');" '+
            ' list="'+iddatalist+'"'+
            '     onchange="this.selectionStart = this.selectionEnd =0;">'+
            '   <span id="'+idimagestatus+'" class="inputwithbutton_imagestatus"></span>'+
            '   <div class="inputwithbutton_autohidegroup" style="right: 26px;">'+
            '         <button class="inputwithbutton_autohidebutton" type="button">×</button>'+
            '   </div>'+
            '   <button class="inputwithbutton_button" type="button" '+
            '   onclick="m_this_idinputcontact=\''+idinput+'\';DlgSelect.showContact(0,\''+
                fnCallBackOnContactSelect+'\');return false;">::</button>  '+
            ' </div>'+
            ' <datalist id="'+iddatalist+'">'+
            '     <option data-value="0">'+strplaceholder+'</option>'+
            ' </datalist>'+
            ' <input type="hidden" id="'+idval+'" name="'+idval+'">';
        
        setInnerById(idcontainter,strout);   
        setBackgroundImageById(idimagestatus,img);
    }
    static inputTextEnterEvent(idcontainter,idinput, strPlaceholder,fnOnInputCallBack){
        if(isEmpty(strPlaceholder))strPlaceholder="";
        //var strout='<div class="inputwithbutton" style="width: 100%;">' +
        var strout='<input placeholder="'+strPlaceholder+'" name="'+idinput+'" id="'+idinput+'"' +
        ' class="inputwithbutton_input" type="text" style="padding-right: 24px;"' +
        ' onkeypress="'+fnOnInputCallBack+'(event);" '+
        ' onchange="">' +

        '<div class="inputwithbutton_autohidegroup" style="right:1px"> '+
            '<button class="inputwithbutton_autohidebutton" type="button">×</button>' +
        '</div>';
        setInnerById(idcontainter,strout);    
    }
    static inputText(idcontainter,idinput, strPlaceholder,fnOnInputCallBack)
    {
        if(isEmpty(strPlaceholder))strPlaceholder="(nhập)";
        //var strout='<div class="inputwithbutton" style="width: 100%;">' +
        var strout='<input placeholder="'+strPlaceholder+'" name="'+idinput+'" id="'+idinput+'"' +
        ' class="inputwithbutton_input" type="text" style="padding-right: 24px;"' +
        ' oninput="'+fnOnInputCallBack+'();" '+
        ' onchange="this.selectionStart = this.selectionEnd =0;">' +

        '<div class="inputwithbutton_autohidegroup" style="right:1px"> '+
            '<button class="inputwithbutton_autohidebutton" type="button">×</button>' +
        '</div>';
        setInnerById(idcontainter,strout);           
    }
    static goodsSearchInputCreate(idcontainter,strPlaceholder,fnOnInputCallBack,fnOnInputCallBackEx)
    {
        var idinput=idcontainter+"input";
        var idinputdatalist=idcontainter+"datalist";
        var idimagestatus=idcontainter+"imagestatus";
        var img="imgs/search-good.png";
        if(isEmpty(strPlaceholder))strPlaceholder="(nhập tên hàng)";
        //var strout='<div class="inputwithbutton" style="width: 100%;">' +
        var strout='<input placeholder="'+strPlaceholder+'" name="'+idinput+'" id="'+idinput+'"' +
        ' class="inputwithbutton_inputex" type="text" style="padding-right: 26px;"' +
        ' list="'+idinputdatalist+'"'+
        ' oninput="showWaitStatus(0,\''+idimagestatus+'\',\''+img+'\');DlgSelect.showGoodsForEximSelect(\''+idinput+'\',\''+idinputdatalist+'\','+fnOnInputCallBack+');" '+
        ' onchange="this.selectionStart = this.selectionEnd =0;">' +
        '   <span id="'+idimagestatus+'" class="inputwithbutton_imagestatus"></span>'+
        '<div class="inputwithbutton_autohidegroup" style="right:26px"> '+
            '<button class="inputwithbutton_autohidebutton" type="button">×</button>' +
        '</div>';
        if(!isEmpty(fnOnInputCallBackEx))
            strout+='<button class="inputwithbutton_button"'+
            'onclick="DlgSelect.iddivshow=null;DlgSelect.showGoods(0,\''+fnOnInputCallBackEx+'\');"  type="button">::</button>';     
        strout+='<datalist id="'+idinputdatalist+'">';
        strout+='</datalist>';
       
        setInnerById(idcontainter,strout);   
        setBackgroundImageById(idimagestatus,img);
    }
    static inputWithClearButtonSetup(){
        const inputs = document.querySelectorAll(".inputwithbutton_input")
        const handleInputChange = (e) => {
            
             inputs.forEach(input => {
                if(input.id==e.currentTarget.myparam)
                {
                    if (e.target.value && !input.classList.contains("inputwithbutton_autohidegroup_touched")) 
                    {
                        input.classList.add("inputwithbutton_autohidegroup_touched")
                    } 
                    else if (!e.target.value && input.classList.contains("inputwithbutton_autohidegroup_touched")) 
                    {
                        input.classList.remove("inputwithbutton_autohidegroup_touched")
                    }                    
                }
                
            });
                
        }
        inputs.forEach(input => {
            input.addEventListener("input", handleInputChange,false )
            input.myparam=input.id;
        });
        
        const clearButtons = document.querySelectorAll(".inputwithbutton_autohidebutton")
        const handleButtonClick = (e) => {
           inputs.forEach(input => {
                if(input.id==e.currentTarget.myparam)
                {
                    input.value = ''
                    input.focus()
                    input.classList.remove("inputwithbutton_autohidegroup_touched")
                }
                
            });

        }
        clearButtons.forEach(clearButton => {
            clearButton.addEventListener("click", handleButtonClick,false)
            clearButton.myparam=clearButton.parentElement.previousElementSibling.id;
            
        });
    }
	static inputWithClearButtonOnInput(idinput){
        var e = window.event; 
        const input=document.getElementById(idinput);
        if(isEmpty(e))
        {
            if (!input.classList.contains("inputwithbutton_autohidegroup_touched")) {
                input.classList.add("inputwithbutton_autohidegroup_touched")
            } else if (input.classList.contains("inputwithbutton_autohidegroup_touched")) {
                input.classList.remove("inputwithbutton_autohidegroup_touched")
            }
            return;
        }
       //const input = document.querySelector(".inputwithbutton_input");
       
       
       if (e.target.value && !input.classList.contains("inputwithbutton_autohidegroup_touched")) {
           input.classList.add("inputwithbutton_autohidegroup_touched")
       } else if (!e.target.value && input.classList.contains("inputwithbutton_autohidegroup_touched")) {
           input.classList.remove("inputwithbutton_autohidegroup_touched")
       }
   }
   static inputWithClearButtonOnClear(idinput){
       const input=document.getElementById(idinput);
       if(input===null)return;
       input.value = '';
       input.focus();
       input.classList.remove("inputwithbutton_autohidegroup_touched");
   }
}
//*END_CLASS_CONTROL
//*START_CLASS_DLG_SELECT
class DlgSelect
{
	static page_source="";
	static page_title="";
	static ar_goodscategory=[];//[{id,name}]
	static last_goods_category_id=0;
	static iddivshow=null;//div_obj se hien thi danh sach hang, khong phai la dialog
	static showlistheading=1;
	static pagesize=20;
	constructor(in_page_source,in_page_title)
	{
		this.page_source=in_page_source;
		if(typeof in_page_title!==undefined)
			this.page_title=in_page_title;
	}
	//chi tiet dang bang 2 cot
	static showDetailsListForm(data)
	{
		if(data.length<1)
		{
			showMsg("Không có dữ liệu");
			return false;
		}
		if(data.length<=g_split_row.length)
		{
			showMsg("Không có dữ liệu");
			return false;
		}
		var rows=[];
		var rcount=0;
		var start=0,arrheads;
		rows=data.split(g_split_row);
		var rcount=rows.length;
		if(rcount<1)
			return false;
		//arrheads la array chua tieu de cac cot, neu trong rong thi cot do khong hien thi
		if(rows[start].substr(0,g_sign_columnconfig.length)==g_sign_columnconfig)
		{
			var datahead=rows[start].substr(g_sign_columnconfig.length);
			arrheads=datahead.split(g_split_val);	
			start++;
		}	
		var data_vals=[];
		data_vals=rows[start].split(g_split_val);
		var html="";
		for(var i=0;i<data_vals.length;i++)
		{
			html+="<div class='divrow'>";
				html+="<div class='divcell'>"+arrheads[i]+"</div>";
				html+="<div class='divcell'>"+data_vals[i]+"</div>";		
			html+="</div>";
		}
		html="<div class='divtable' style='width:100%'>"+html+"</div>";
		showModal(html);
	}
    static showGoodsForEximSelect(idinput,iddatalist,fnCallBack)
    {
         //neu nhan chon lua
        var val = document.getElementById(idinput).value;
        var opts = document.getElementById(iddatalist).children;
        for (var i = 0; i < opts.length; i++) {
            if (opts[i].value === val) {
               var val=opts[i].getAttribute('data-value');
               fnCallBack(val);
               showWaitStatus();
               return;
            }
        }

        var search_key=uriEncode(getValById(idinput));
        var url="?page=list&subpage=goods&invoice_type=phieunhapmua&goods_name="+search_key;
      
        var dataString="";
        jQuery.ajax({
        url: url,
        data: dataString,
        type: "GET",
        success: function(data)
        {
            
                var rows=data.split(g_split_row);
                var dataset="";
                for(i=0;i<rows.length;i++){
                    if(rows[i].substr(0,g_sign_columnconfig.length)==g_sign_columnconfig)
                        continue;
                    if(rows[i].substr(0,g_sign_pageconfig.length)==g_sign_pageconfig)
                        continue;
                    var vals=rows[i].split(g_split_val);
                    var goods_id=vals[0],unit_id=vals[1];
                    var fname=vals[2];
                    if((vals[3]!==undefined)&&(vals[3].length>0))
                        fname+=" ("+vals[2]+")";
                    if((vals[6]!==undefined)&&(vals[6].length>0))
                        fname+=" ("+vals[6]+")";
                    dataset+="<option data-value='"+rows[i]+"'>"+fname+"</option>";
                }
                
                setInnerById(iddatalist,dataset);
                showWaitStatus();
                hideModal();
      
        },
        error: function (){
            showModal("Lỗi");
            showWaitStatus();
        }
        });
    }
    static showContactForEximSelect(idinput,iddatalist,idselectedcontact){
        //var key, x = e || window.event; key = (x.keyCode || x.which);
        //if(!isVisiableKeyCode(key))
        //   return;
        //neu nhan chon lua
        var val = document.getElementById(idinput).value;
        var opts = document.getElementById(iddatalist).children;
        for (var i = 0; i < opts.length; i++) {
            if (opts[i].value === val) {
                // An item was selected from the list!
                // yourCallbackHere()
               var val=opts[i].getAttribute('data-value');
               if(parseNumber(val)>0)
                    setValById(idselectedcontact,val);
                else 
                    setValById(idselectedcontact,0);
                showWaitStatus();
                return;
            }
        }
        var search_key=uriEncode(getValById(idinput));
        var url="?page=list&subpage=contact_searchshort&contactname="+search_key;
       
        var dataString="";
        jQuery.ajax({
        url: url,
        data: dataString,
        type: "GET",
        success: function(data)
        {
           
            if(isDataFail(data))
            {
                showModalBlock(data.substr(g_sign_fail.length));
                return;
            }
            else if(isDataOk(data))
            {
                var rows=getDataOk(data).split(g_split_row);
                var dataset="";
                for(i=0;i<rows.length;i++){
                    if(rows[i].substr(0,g_sign_columnconfig.length)==g_sign_columnconfig)
                        continue;
                    var vals=rows[i].split(g_split_val);
                    var fname=vals[1];
                    if((vals[2]!==undefined)&&(vals[2].length>0))
                        fname+=" ("+vals[2]+")";
                    if((vals[3]!==undefined)&&(vals[3].length>0))
                        fname+=" ("+vals[3]+")";
                    dataset+="<option data-value='"+vals[0]+"'>"+fname+"</option>";
                }
                setInnerById(iddatalist,dataset);
                showWaitStatus();
                hideModal();
            }
            else
                showModal("Lỗi");
            showWaitStatus();
        },
        error: function (){
            showModal("Lỗi");
            showWaitStatus();
        }
        });
    }
	static showSimple(data,fnCallBack,fnViewOtherPage,fnViewOtherPageExtraParamArray)
	{
		var rows=[];
		var rcount=0;
		var start=0,pagesize=0,pageno=0,pagecount=0,arrheads;
		
		
			if(data.length<1)
			{
				showMsg("Không có dữ liệu");
				return false;
			}
			if(data.length<=g_split_row.length)
			{
				showMsg("Không có dữ liệu");
				return false;
			}
			rows=data.split(g_split_row);
			var rcount=rows.length;
			if(rcount<1)
				return false;
			if(rows[0].substr(0,g_sign_pageconfig.length)==g_sign_pageconfig)
			{
				var configs=rows[0].split(g_split_val);	
				start++;
				if(configs.length==3)
				{
					pagesize=parseInt(configs[0]);
					pageno=parseInt(configs[1]);
					pagecount=parseInt(configs[2]);
				}
				
				if(start==rcount)
				{
					showMsg("Không có dữ liệu");
					return false;
				}
			}
			//arrheads la array chua tieu de cac cot, neu trong rong thi cot do khong hien thi
			if(rows[start].substr(0,g_sign_columnconfig.length)==g_sign_columnconfig)
			{
				
				var datahead=rows[start].substr(g_sign_columnconfig.length);
				arrheads=datahead.split(g_split_val);	
				start++;
			}	
		
		var strout="",allrows="",row="",cmd="";
		var i=0,j=0;
		
		for(i=start;i<rcount;i++)
		{
			var data_vals=[];
			data_vals=rows[i].split(g_split_val);
			var paramlist="";
			var cmd="";
			if(fnCallBack!==undefined)
				cmd="<input type=\"button\" class=\"cmd\" onclick=\""+fnCallBack+"(";
			row="<div class=\"divrow\">";
			for(j=0;j<data_vals.length;j++)
			{
				if(Array.isArray(arrheads))
				{
						if((arrheads[j]!=null)&&(arrheads[j].length>0))
							row+="<div class=\"divcell\">"+(data_vals[j])+"</div>";
							
				}
				else
					row+="<div class=\"divcell\">"+(data_vals[j])+"</div>";
				if(paramlist!="")paramlist+=",";
				paramlist+="'"+data_vals[j]+"'";
			
			}
			if(fnCallBack!==undefined)
				cmd+=paramlist+")\" value=\"Chọn\">";
			row+="<div class=\"divcell padding0\">"+cmd+"</div>";
			row+="</div>";
			allrows+=row;
		}
		
		var rowheading="";
		if(DlgSelect.showlistheading==1)
		{
			if(Array.isArray(arrheads))
			{
				rowheading+="<div class='divrow heading' id='idrowheading'>";
					for(i=0;i<arrheads.length;i++)
					{
						if(arrheads[i]!="")
							rowheading+="<div class='divcell'>"+arrheads[i]+"</div>";
					}
					if(fnCallBack!==undefined)
						rowheading+="<div class=\"divcell\">CMD</div>";
				rowheading+="</div>";
			}
		}
		//pageno,pagecount,fnCallBack,fnViewOtherPage,fnViewOtherPageExtraParamArray
		var pageinfo="";
		if(pagecount>1)
		{
			pageinfo+="<div class='pageindex' id='idpageindex'>";
			var i=1;
			for(i=1;i<=pagecount;i++)
				pageinfo+="<a class=\""+((i==pageno)?"pageindexfocus":"pageindex")+"\" href=\"javascript:void(0)\" onclick=\""+fnViewOtherPage+"("+fnViewOtherPageExtraParamArray+","+i+")\">"+i+"</a>";
			pageinfo+="</div>";
		}
		strout="<div class='divtable'>"+rowheading+allrows+"</div>"+pageinfo;
		showModal(strout);
		return true;
	}
	//fnOnSelectCallBack duoc goi voi tat ca cac gia tri
	//goodsid,unitid,goodsname,unit,quantitystock,unitprice,imagename,iddetails
	static showGoods(pageno,fnOnSelectCallBack,show_stock,store_id,category_id,goods_name,invoice_type)
	{
		if(show_stock===undefined)show_stock=0;
		if(store_id===undefined)store_id=0;
		if(category_id===undefined)category_id=0;
		if(goods_name===undefined)goods_name="";
		if(invoice_type===undefined)invoice_type="";
		if(DlgSelect.last_goods_category_id!=0)
			category_id=DlgSelect.last_goods_category_id;
			
		if(DlgSelect.ar_goodscategory.length<1)
		{
			var url="?page=list&subpage=goods_category";
			var dataString="";
			jQuery.ajax({
				url: url,
				data: dataString,
				type: "GET",
				success: function(data)
				{	
					var rows=data.split(g_split_row);
					if(!Array.isArray(rows))
					{
						showModalBlock("Lỗi");
						return;
					}
					var start=0;
					if(isDataStartWith(data,g_sign_columnconfig))
						start++;
					
					for(i=start;i<rows.length;i++)
					{
						var cols=rows[i].split(g_split_val);
						if(cols.length==2)
						{
							DlgSelect.ar_goodscategory[DlgSelect.ar_goodscategory.length]=new Array(cols[0],cols[1]);
						}
					}
					
					DlgSelect.showGoods(pageno,fnOnSelectCallBack,show_stock,store_id,category_id,goods_name,invoice_type);
					return;
				},
				error:function()
				{

				}
			});
			return;
		}
		if(goods_name!="")
			goods_name=uriEncode(goods_name);
		var url="?page=list&subpage=goods&store_id="+store_id+"&category_id="+category_id+"&show_stock="+show_stock+"&invoice_type="+invoice_type+"&goods_name="+(goods_name)+"&pageno="+pageno+"&pagesize="+DlgSelect.pagesize;
		
		var dataString="";
		jQuery.ajax({
			url: url,
			data: dataString,
			type: "GET",
			success: function(data)
			{	
				var option_select="";
				for(i=0;i<DlgSelect.ar_goodscategory.length;i++)
				{
					if(DlgSelect.ar_goodscategory[i][0]==category_id)
						option_select+="<option value="+DlgSelect.ar_goodscategory[i][0]+" selected>"+DlgSelect.ar_goodscategory[i][1]+"</option>";
					else
						option_select+="<option value="+DlgSelect.ar_goodscategory[i][0]+">"+DlgSelect.ar_goodscategory[i][1]+"</option>";
				}
				
				var strout="",strgrid="";
                //xem phan khung: combo nhom hang, hop tim ten hang da co hay chua
                var iddlgselectvariableareashowgoods=document.getElementById("iddlgselectvariableareashowgoods");
                if((iddlgselectvariableareashowgoods===null)||(!isModalVisible())){
                    var strsgoodsname='<div class="inputwithbutton" style="width: 100%;">' +
                        '<input placeholder="(tên hàng)" name="sgoodsname" id="sgoodsname"' +
                        'class="inputwithbutton_input" type="text" style="padding-right: 26px;"' +
                        ' oninput="var sgoodsname=document.getElementById(\'sgoodsname\').value;DlgSelect.showGoods(0,\''+
                        fnOnSelectCallBack+'\','+show_stock+','+store_id+',0,sgoodsname,'+'\''+invoice_type+'\');"'+
                        ' value="'+goods_name +'" '+
                        'onchange="this.selectionStart = this.selectionEnd =0;">' +
                        '<div class="inputwithbutton_autohidegroup" style="right:1px"> '+
                            '<button class="inputwithbutton_autohidebutton" type="button">×</button>' +
                        '</div>'+
                    '</div>';
                    var strsgoodscode='<div class="inputwithbutton" style="width: 100%;">' +
                        '<input placeholder="(mã hàng)" name="sgoodscode" id="sgoodscode"' +
                        'class="inputwithbutton_input" type="text" style="padding-right: 26px;"' +
                        ' oninput="" '+
                        'onchange="this.selectionStart = this.selectionEnd =0;">' +
                        '<div class="inputwithbutton_autohidegroup" style="right:1px"> '+
                            '<button class="inputwithbutton_autohidebutton" type="button">×</button>' +
                        '</div>'+
                    '</div>';
                    
                    /*
                   
					strout+='<div class="divcell padding0" style="width:40%">';
					strout+='<select size="1" name="select_category_id" id="select_category_id" style="width:100%" onchange="DlgSelect.last_goods_category_id=document.getElementById(\'select_category_id\').value;DlgSelect.showGoods(0,\''+fnOnSelectCallBack+'\','+show_stock+','+store_id+',DlgSelect.last_goods_category_id,\'\',\''+invoice_type+'\')">';
					strout+='<option value="0">~tất cả~</option>';
					if(category_id===-1)
						strout+='<option value="-1" selected>Dịch vụ</option>';
					else
						strout+='<option value="-1">Dịch vụ</option>';
					strout+=option_select;
					strout+='</select>';
					strout+='</div>';
                    */
					//strout+='<div class="divcell padding0"><input type="text" name="sgoodsname"  id="sgoodsname" onfocus="this.select();" style="width:100%" value="'+goods_name+'" oninput="var sgoodsname=document.getElementById(\'sgoodsname\').value;DlgSelect.showGoods(0,\''+fnOnSelectCallBack+'\','+show_stock+','+store_id+',0,sgoodsname,'+'\''+invoice_type+'\')" placeholder="tên hàng"></div>';
                    strout+='<div class="divtable" style="background:#039;color:#0C3;font-weight:bold">';
				    strout+='<div class="divrow">';
                    strout+='<div class="divcell padding0">'+strsgoodsname+'</div>';    
					strout+='<div class="divcell padding0">'+strsgoodscode+'</div>';
					strout+='<div class="divcell padding0" style="width:'+g_width_gridcmd+'px">'+
                        '<input type="button" style="padding:5px;width:100%;height:100%;marigin:0px"'+
                        'class=\"cmd\" onclick="onGoodsCodeSelect(getValById(\'sgoodscode\'));setValById(\'sgoodscode\',\'\')" value=">>"></div>';
				    strout+='</div>';
				    strout+='</div>';
                }
				
				strgrid=DlgSelect.formatDataAsGrid(data,fnOnSelectCallBack,"Chọn","DlgSelect.showGoods",new Array(show_stock,store_id,category_id,"'"+goods_name+"'","'"+invoice_type+"'"));
				
				if(DlgSelect.iddivshow!=null)
                {
                    if(iddlgselectvariableareashowgoods===null)
                    {
                        strgrid='<div id="iddlgselectvariableareashowgoods">'+strgrid+'</div>';
                        strout+=strgrid;
                        DlgSelect.iddivshow.innerHTML=strout;
                        UiControl.inputWithClearButtonSetup();
                    }else
                    {
                        iddlgselectvariableareashowgoods.innerHTML=strgrid;
                    }
                    setFocus("sgoodsname");
                }
					
				else
				{
                    if((iddlgselectvariableareashowgoods===null)||(!isModalVisible())){
                        strgrid='<div id="iddlgselectvariableareashowgoods">'+strgrid+'</div>';
                        strout+=strgrid;
                        strout+="<input type=\"button\" class=\"modalbutton modalbuttoncancel\" style=\"width:100%\" value=\"Đóng\" onclick=\"hideModalBlock(false)\">";
                        showModalBlock(strout,"Chọn hàng");
                        UiControl.inputWithClearButtonSetup();    
                    }
                    else{
                        setInnerById("iddlgselectvariableareashowgoods",strgrid);
                    }
                    
				}
			},
			error: function ()
			{
				hideModal();
			}
		});
		
	}
	static getPageIndexGoTo(page,pagemax,fnCallBack,fnViewOtherPage,fnViewOtherPageExtraParamArray)
	{
		var fstring=fnViewOtherPage+"(getValById('idpageindexinput'),'"+fnCallBack+"'";
		if(Array.isArray(fnViewOtherPageExtraParamArray))
		{
			for(var j=0;j<fnViewOtherPageExtraParamArray.length;j++)
			fstring+=",'"+fnViewOtherPageExtraParamArray[j]+"'";
		}
		fstring+=")";

		var idpageindex=document.getElementById("idpageindex");
		var htmlold=idpageindex.innerHTML;
		var htmlnew="<input type=\"text\" onfocus=\"this.select()\" size=\"5\" style=\"height:100%;padding:5px\" id=\"idpageindexinput\" value=\""+page+"\">";
		htmlnew+="<input type=\"button\" style=\"width:60px;height:100%;padding:5px\" value=\"Xem\" onclick=\""+fstring+"\">";
		idpageindex.innerHTML=htmlnew;
	}
	static getPageIndexSelectOnClickString(pageno,fnCallBack,fnViewOtherPage,fnViewOtherPageExtraParamArray)
	{
		var fstring=fnViewOtherPage+"("+pageno;
		if(fnCallBack!==null)fstring+=",'"+fnCallBack+"'"
		if(Array.isArray(fnViewOtherPageExtraParamArray))
		{
			for(var j=0;j<fnViewOtherPageExtraParamArray.length;j++)
			fstring+=","+fnViewOtherPageExtraParamArray[j]+"";
		}
		fstring+=")";
		return fstring;
	}
	static getPageIndexSelect(pageno,pagecount,fnCallBack,fnViewOtherPage,fnViewOtherPageExtraParamArray)
	{
		if(pagecount<=1)
			return "";
		var pageinfo="";
		pageinfo+="<div class='pageindex' id='idpageindex'>";
		var i=1;
		
		if(pagecount<=9)
		{
			for(i=1;i<=pagecount;i++)
				pageinfo+="<a class=\""+((i==pageno)?"pageindexfocus":"pageindex")+"\" href=\"javascript:void(0)\" onclick=\""+DlgSelect.getPageIndexSelectOnClickString(i,fnCallBack,fnViewOtherPage,fnViewOtherPageExtraParamArray)+"\">"+i+"</a>";
		}
		else
		{
			pageinfo+="<a class=\"pageindex\" href=\"javascript:void(0)\" onclick=\""+DlgSelect.getPageIndexSelectOnClickString(pageno-1,fnCallBack,fnViewOtherPage,fnViewOtherPageExtraParamArray)+"\">&#8592</a>";
			pageinfo+="<a class=\""+((1==pageno)?"pageindexfocus":"pageindex")+"\" href=\"javascript:void(0)\" onclick=\""+DlgSelect.getPageIndexSelectOnClickString(1,fnCallBack,fnViewOtherPage,fnViewOtherPageExtraParamArray)+"\">1</a>";

			var hstart=0,hend=0,tstart=0,tend=0;
			if(pageno<=3)
			{
				hstart=2;hend=5;tstart=0;tend=-1;
				for(i=hstart;i<=hend;i++)
					pageinfo+="<a class=\""+((i==pageno)?"pageindexfocus":"pageindex")+"\" href=\"javascript:void(0)\" onclick=\""+DlgSelect.getPageIndexSelectOnClickString(i,fnCallBack,fnViewOtherPage,fnViewOtherPageExtraParamArray)+"\">"+i+"</a>";
				pageinfo+="<a class=\"pageindex\" href=\"javascript:void(0)\" onclick=\"DlgSelect.getPageIndexGoTo("+(hend+1)+","+pagecount+",'"+fnCallBack+"','"+fnViewOtherPage+"',["+fnViewOtherPageExtraParamArray+"])\">...</a>";

			}
			else if(pageno>(pagecount-3))
			{
				hstart=0;hend=-1;tstart=pagecount-4;tend=pagecount-1;
				pageinfo+="<a class=\"pageindex\" href=\"javascript:void(0)\" onclick=\"DlgSelect.getPageIndexGoTo("+(tstart-1)+","+pagecount+",'"+fnCallBack+"','"+fnViewOtherPage+"',["+fnViewOtherPageExtraParamArray+"])\">...</a>";
				for(i=tstart;i<=tend;i++)
					pageinfo+="<a class=\""+((i==pageno)?"pageindexfocus":"pageindex")+"\" href=\"javascript:void(0)\" onclick=\""+DlgSelect.getPageIndexSelectOnClickString(i,fnCallBack,fnViewOtherPage,fnViewOtherPageExtraParamArray)+"\">"+i+"</a>";
			}
			else
			{
				hstart=pageno-1;hend=pageno+1;tstart=0;tend=-1;
				pageinfo+="<a class=\"pageindex\" href=\"javascript:void(0)\" onclick=\"DlgSelect.getPageIndexGoTo("+(pageno-2)+","+pagecount+",'"+fnCallBack+"','"+fnViewOtherPage+"',["+fnViewOtherPageExtraParamArray+"])\">...</a>";
				for(i=hstart;i<=hend;i++)
					pageinfo+="<a class=\""+((i==pageno)?"pageindexfocus":"pageindex")+"\" href=\"javascript:void(0)\" onclick=\""+DlgSelect.getPageIndexSelectOnClickString(i,fnCallBack,fnViewOtherPage,fnViewOtherPageExtraParamArray)+"\">"+i+"</a>";
				pageinfo+="<a class=\"pageindex\" href=\"javascript:void(0)\" onclick=\"DlgSelect.getPageIndexGoTo("+(pageno+2)+","+pagecount+",'"+fnCallBack+"','"+fnViewOtherPage+"',["+fnViewOtherPageExtraParamArray+"])\">...</a>";

			}
			pageinfo+="<a class=\""+((pageno==pagecount)?"pageindexfocus":"pageindex")+"\" href=\"javascript:void(0)\" onclick=\""+DlgSelect.getPageIndexSelectOnClickString(pagecount,fnCallBack,fnViewOtherPage,fnViewOtherPageExtraParamArray)+"\">"+pagecount+"</a>";
			pageinfo+="<a class=\"pageindex\" href=\"javascript:void(0)\" onclick=\""+DlgSelect.getPageIndexSelectOnClickString(pageno+1,fnCallBack,fnViewOtherPage,fnViewOtherPageExtraParamArray)+"\">&#8594</a>";
		}
		pageinfo+="</div>";
		return pageinfo;
	}
    static formatAsDataGrid2(data,fnCallBack,paramsCallBack,fnCallBackTitle)
    {
		if(data.length<=g_split_row.length)
			return "";
		var rows=data.split(g_split_row);
		var rcount=rows.length;
		if(rcount<1)
			return "";
		var start=0,pagesize=0,pageno=0,pagecount=0,arrheads;
		if(rows[0].substr(0,g_sign_pageconfig.length)==g_sign_pageconfig)
		{
			var configs=rows[0].split(g_split_val);	
			start++;
			if(configs.length==3)
			{
				pagesize=parseInt(configs[0]);
				pageno=parseInt(configs[1]);
				pagecount=parseInt(configs[2]);
			}
		}
		//arrheads la array chua tieu de cac cot, neu trong rong thi cot do khong hien thi
		if(rows[start].substr(0,g_sign_columnconfig.length)==g_sign_columnconfig)
		{
			var datahead=rows[start].substr(g_sign_columnconfig.length);
			arrheads=datahead.split(g_split_val);	
			start++;
		}	
		var strout="",allrows="";
		var i=0,j=0;
		
		for(i=start;i<rcount;i++)
		{
			var data_vals=rows[i].split(g_split_val);
			var cmd="",paramlist="";

			cmd="<input type=\"button\" class=\"cmd\" value=\""+fnCallBackTitle+"\" onclick=\"return "+fnCallBack+"(";
            
			for(j=0;j<paramsCallBack.length;j++){
                if(paramlist.length>0)
                    paramlist+=",";
                paramlist+="'"+formatJString(paramsCallBack[j].toString())+"'";
            }
           
 			allrows+="<div class=\"divrow\">";
				for(j=0;j<data_vals.length;j++)
				{
					if(Array.isArray(arrheads))
					{
						if(arrheads[j]!="")	
							allrows+="<div class=\"divcell\">"+(data_vals[j])+"</div>";
							
					}
					else
						allrows+="<div class=\"divcell\">"+(data_vals[j])+"</div>";
					
                    if(paramlist.length>0)
                       paramlist+=",";
                    paramlist+="'"+formatJString(data_vals[j].toString())+"'";
					
				}
                cmd+=paramlist;
				cmd+=")\">";
				allrows+="<div class=\"divcell padding0\" style=\"width:"+g_width_gridcmd+"px\">"+cmd+"</div>";
			allrows+="</div>";
			
		}
		
		var rowheading="";
		if(DlgSelect.showlistheading==1)
		{
			if(Array.isArray(arrheads))
			{
				rowheading+="<div class='divrow heading' id='idrowheading'>";
					for(i=0;i<arrheads.length;i++)
					{
						if(arrheads[i]!="")
							rowheading+="<div class='divcell'>"+arrheads[i]+"</div>";
					}
					rowheading+="<div class='divcell' style='width:"+g_width_gridcmd+"px'>CMD</div>";
				rowheading+="</div>";
			}
		}
        strout="<div class='divtable'>"+rowheading+allrows+"</div>";
		return strout;
    }
	//row luon co cau truc id,data1,data2..
	static formatDataAsGrid(data,fnCallBack,fnCallBackTitle,fnViewOtherPage,fnViewOtherPageExtraParamArray)
	{
		if(data.length<=g_split_row.length)
			return "";
		var rows=data.split(g_split_row);
		var rcount=rows.length;
		if(rcount<1)
			return "";
		var start=0,pagesize=0,pageno=0,pagecount=0,arrheads;
		if(rows[0].substr(0,g_sign_pageconfig.length)==g_sign_pageconfig)
		{
			var configs=rows[0].split(g_split_val);	
			start++;
			if(configs.length==3)
			{
				pagesize=parseInt(configs[0]);
				pageno=parseInt(configs[1]);
				pagecount=parseInt(configs[2]);
			}
		}
		//arrheads la array chua tieu de cac cot, neu trong rong thi cot do khong hien thi
		if(rows[start].substr(0,g_sign_columnconfig.length)==g_sign_columnconfig)
		{
			var datahead=rows[start].substr(g_sign_columnconfig.length);
			arrheads=datahead.split(g_split_val);	
			start++;
		}	
		var strout="",allrows="";
		var i=0,j=0;
		
		for(i=start;i<rcount;i++)
		{
			var data_vals=rows[i].split(g_split_val);
			var cmd="";

			cmd="<input type=\"button\" class=\"cmd\" value=\""+fnCallBackTitle+"\" onclick=\"return "+fnCallBack+"("+data_vals[0];
			
			allrows+="<div class=\"divrow\">";
				for(j=1;j<data_vals.length;j++)
				{
					if(Array.isArray(arrheads))
					{
						if(arrheads[j]!="")	
							allrows+="<div class=\"divcell\">"+(data_vals[j])+"</div>";
							
					}
					else
						allrows+="<div class=\"divcell\">"+(data_vals[j])+"</div>";
					
					cmd+=",'"+formatJString(data_vals[j])+"'";
				}
				cmd+=")\">";
				allrows+="<div class=\"divcell padding0\" style=\"width:"+g_width_gridcmd+"px\">"+cmd+"</div>";
			allrows+="</div>";
			
		}
		 
		var rowheading="";
		if(DlgSelect.showlistheading==1)
		{
			if(Array.isArray(arrheads))
			{
				rowheading+="<div class='divrow heading' id='idrowheading'>";
					for(i=0;i<arrheads.length;i++)
					{
						if(arrheads[i]!="")
							rowheading+="<div class='divcell'>"+arrheads[i]+"</div>";
					}
					rowheading+="<div class='divcell' style='width:"+g_width_gridcmd+"px'>CMD</div>";
				rowheading+="</div>";
			}
		}
		var pageinfo="";
		pageinfo=DlgSelect.getPageIndexSelect(pageno,pagecount,fnCallBack,fnViewOtherPage,fnViewOtherPageExtraParamArray);
		/*
		if(pagecount>1)
		{
			pageinfo+="<div class='pageindex'>";
			for(i=1;i<=pagecount;i++)
			{
				if(i==pageno)
				{
					pageinfo+="<a class=\"pageindexfocus\" href=\"javascript:void(0)\" onclick=\""+fnViewOtherPage+"("+i+",'"+fnCallBack+"'";
					if(Array.isArray(fnViewOtherPageExtraParamArray))
					{
						for(j=0;j<fnViewOtherPageExtraParamArray.length;j++)
							pageinfo+=","+fnViewOtherPageExtraParamArray[j];
					}
						
					pageinfo+=")\">"+i+"</a>";

				}
				else
				{
					pageinfo+="<a class=\"pageindex\" href=\"javascript:void(0)\" onclick=\""+fnViewOtherPage+"("+i+",'"+fnCallBack+"'";
					if(Array.isArray(fnViewOtherPageExtraParamArray))
					{
						for(j=0;j<fnViewOtherPageExtraParamArray.length;j++)
							pageinfo+=","+fnViewOtherPageExtraParamArray[j];
					}
					pageinfo+=")\">"+i+"</a>";

				}
			}
			pageinfo+="</div>";
		}
		*/
		strout="<div class='divtable'>"+rowheading+allrows+"</div>"+pageinfo;
		return strout;
		//showModalBlock(strout);
	}
	static showDataToSelectBox(idselectbox,data,zeroitemfist)
	{
		idselectbox=document.getElementById(idselectbox);
		var rows=data.split(g_split_row);
		if(!Array.isArray(rows)) return false;
		var stroptions=""
		var start=0;
		if(rows[0].substr(0,g_sign_pageconfig.length)==g_sign_pageconfig)
			start++;
		//arrheads la array chua tieu de cac cot, neu trong rong thi cot do khong hien thi
		if(rows[start].substr(0,g_sign_columnconfig.length)==g_sign_columnconfig)
			start++;
		for(i=start;i<rows.length;i++)
		{
			var vals=rows[i].split(g_split_val);
			if(vals.length>=2)
			{
				var label=vals[1];
				for(var j=2;j<vals.length;j++)label+=" - "+vals[j];	
				stroptions+="<option value=\""+vals[0]+"\">"+label+"</option>";
			}
		}
		if(zeroitemfist!==undefined)stroptions="<option value='0'>"+zeroitemfist+"</option>"+stroptions;
		idselectbox.innerHTML=stroptions;
	}
	static showTimeSelect(fnCallBack)
	{
		var today = new Date();
		//var dd = String(today.getDate()).padStart(2, '0');
		//var mm = String(today.getMonth() + 1).padStart(2, '0'); //January is 0!
		//var yyyy = String(today.getFullYear());

		var strout="";
		strout+='<div class="divtable" style="background:#039;color:#0C3;font-weight:bold">';
		strout+='<div class="divrow">';
			strout+='<div class="divcell" style="padding:0px"><input type="text" id="iddthour" onfocus="this.select()" style="width:100%" value="" placeholder="giờ"></div>';
			strout+='<div class="divcell" style="padding:0px"><input type="text" id="iddtmin" onfocus="this.select()" style="width:100%" value="" placeholder="phút"></div>';
			strout+='<div class="divcell" style="padding:0px"><input type="text" id="iddtday" value="'+today.getDate()+'" onfocus="this.select()" style="width:100%" value="" placeholder="ngày"></div>';
			strout+='<div class="divcell" style="padding:0px"><input type="text" id="iddtmonth" value="'+(today.getMonth()+1)+'"onfocus="this.select()" style="width:100%" value="" placeholder="tháng"></div>';
			strout+='<div class="divcell" style="padding:0px"><input type="text" id="iddtyear" value="'+today.getFullYear()+'"onfocus="this.select()" style="width:100%" value="" placeholder="năm"></div>';
		strout+='</div>';
		strout+='</div>';
		strout+='<div style=\"width:100%\">';
			strout+="<input type=\"button\" class=\"modalbutton modalbuttoncancel\" style=\"width:50%\" value=\"Bỏ qua\" onclick=\"hideModalBlock(false)\">";
			strout+="<input type=\"submit\" class=\"modalbutton modalbuttonok\" style=\"width:50%\" value=\"Đồng ý\" onclick=\""+fnCallBack+"(getValById('iddthour'),getValById('iddtmin'),getValById('iddtday'),getValById('iddtmonth'),getValById('iddtyear'))\"";
		strout+='</div>';
		showModalBlock(strout);
	}
    //fnOnSelectCallBack: String -> 'onGoodsSelect'
	static showContact(pageno,fnOnSelectCallBack,contactname,contactaddress,contactphone,contacttype)
	{	
		if(contactname===undefined)contactname="";if(contactaddress===undefined)contactaddress="";if(contactphone===undefined)contactphone="";
		if(contacttype===undefined)contacttype=3;
		var url="?page=list&subpage=contact&type="+contacttype+"&contactname="+uriEncode(contactname)+
			"&contactaddress="+uriEncode(contactaddress)+"&contactphone="+uriEncode(contactphone)+
			"&invoice_type=phieuxuatle&pageno="+pageno;
        
		var dataString="";
		jQuery.ajax({
			url: url,
			data: dataString,
			type: "GET",
			success: function(data)
			{
				var strout="",stronclick;
                //phan fix duoc tao 01 lan khi hien thi lan dau
                //cac lan sau se hien thi phan noi dung
                var iddlgselectvariableareashowcontact=document.getElementById("iddlgselectvariableareashowcontact");
                if((iddlgselectvariableareashowcontact===null)||(!isModalVisible())){
                    stronclick="var scontactname=document.getElementById(\'scontactname\').value;var scontactaddress=document.getElementById(\'scontactaddress\').value;var scontactphone=document.getElementById(\'scontactphone\').value;DlgSelect.showContact(0,\'"+fnOnSelectCallBack+"\',scontactname,scontactaddress,scontactphone)";
                    var scontactname='<div class="inputwithbutton" style="width: 100%;">' +
                        '<input placeholder="(tên)" name="scontactname" id="scontactname"' +
                        'class="inputwithbutton_input" type="text" style="padding-right: 26px;"' +
                        ' oninput="'+stronclick+'"'+
                        'onchange="this.selectionStart = this.selectionEnd =0;">' +
                        '<div class="inputwithbutton_autohidegroup" style="right:1px"> '+
                            '<button class="inputwithbutton_autohidebutton">×</button>' +
                        '</div>'+
                    '</div>';
                    var scontactaddress='<div class="inputwithbutton" style="width: 100%;">' +
                    '<input placeholder="(địa chỉ)" name="scontactaddress" id="scontactaddress"' +
                    'class="inputwithbutton_input" type="text" style="padding-right: 26px;"' +
                    ' oninput="'+stronclick+'"'+
                    'onchange="this.selectionStart = this.selectionEnd =0;">' +
                    '<div class="inputwithbutton_autohidegroup" style="right:1px"> '+
                        '<button class="inputwithbutton_autohidebutton">×</button>' +
                    '</div>'+
                    '</div>';
                    var scontactphone='<div class="inputwithbutton" style="width: 100%;">' +
                        '<input placeholder="(điện thoại)" name="scontactphone" id="scontactphone"' +
                        'class="inputwithbutton_input" type="text" style="padding-right: 26px;"' +
                        ' oninput="'+stronclick+'"'+
                        'onchange="this.selectionStart = this.selectionEnd =0;">' +
                        '<div class="inputwithbutton_autohidegroup" style="right:1px"> '+
                            '<button class="inputwithbutton_autohidebutton">×</button>' +
                        '</div>'+
                    '</div>';
                    strout+='<div class="divtable" style="background:#039;color:#0C3;font-weight:bold">';
                    strout+='<div class="divrow">';
                        strout+='<div class="divcell" style="padding:0px">'+scontactname+'</div>';
                        strout+='<div class="divcell" style="padding:0px">'+scontactaddress+'</div>';
                        strout+='<div class="divcell" style="padding:0px">'+scontactphone+'</div>';
                    strout+='</div>';
                    strout+='</div>';
                    strout+='<div id="iddlgselectvariableareashowcontact">';
                    strout+=DlgSelect.formatDataAsGrid(data,fnOnSelectCallBack,"Chọn","DlgSelect.showContact",new Array("'"+contactname+"'","'"+contactaddress+"'","'"+contactphone+"',"+contacttype));
                    strout+='</div>';
                    strout+="<input type=\"button\" class=\"modalbutton modalbuttoncancel\" style=\"width:100%\" value=\"Đóng\" onclick=\"hideModalBlock(false)\">";
                    
                    showModalBlock(strout);
                    UiControl.inputWithClearButtonSetup();
                    setFocus("scontactname");
                }
                else
                {
                    setInnerById("iddlgselectvariableareashowcontact",DlgSelect.formatDataAsGrid(data,fnOnSelectCallBack,"Chọn","DlgSelect.showContact",new Array("'"+contactname+"'","'"+contactaddress+"'","'"+contactphone+"',"+contacttype)));
                }
			},
			error: function ()
			{
				hideModal();
			}
		});
		
	}
    static showGoodsColor(fnCallBack,arParams){
		var url="?page=list&subpage=color";
		var dataString="";
		jQuery.ajax({
			url: url,
			data: dataString,
			type: "GET",
			success: function(data)
			{
               
				var strout=DlgSelect.formatAsDataGrid2(data,fnCallBack,arParams,"Chọn");
				strout+="<input type=\"button\" class=\"modalbutton modalbuttoncancel\" style=\"width:100%\" value=\"Đóng\" onclick=\"hideModalBlock(false)\">";
				showModalBlock(strout);
			},
			error: function ()
			{
				hideModal();
			}
		});        
    }
	static showEmployee(pageno,fnCallBack)
	{
		var url="?page=list&subpage=employee&invoice_type=phieuxuatle&pageno="+pageno;
		var dataString="";
		jQuery.ajax({
			url: url,
			data: dataString,
			type: "GET",
			success: function(data)
			{
				
				var strout=DlgSelect.formatDataAsGrid(data,fnCallBack,"Chọn","DlgSelect.showEmployee");
				strout+="<input type=\"button\" class=\"modalbutton modalbuttoncancel\" style=\"width:100%\" value=\"Đóng\" onclick=\"hideModalBlock(false)\">";
			
				showModalBlock(strout);
			},
			error: function ()
			{
				hideModal();
			}
		});
		
	}
}
//*END_CLASS_DLG_SELECT
//*START_CLASS_GRID_DATA_STRUCT
class GridStructData
	{
		//var data_struct=[
		//		{name:"id",head:"",visible:0},
		//		{name:""  ,head:"",visible:1/0,type:"text/check/number",required:"1/0"},
		//];
		static data_struct=[];
		static page_source="";
		static page_title="";
		static show_collapsiblerow=0;
		static editfrom_header="";//current edit from, is showing
		static editform_content="";
		static editfrom_firstfocus="";
		constructor(in_data_struct,in_page_source,in_page_title)
		{
			this.data_struct=in_data_struct;
			this.page_source=in_page_source;
			if(in_page_title!==undefined)
				this.page_title=in_page_title;
		}
		static getDataFromId(id)
		{
			var data_vals=[];
			data_vals[0]=id;
			for(i=1;i<this.data_struct.length;i++)
			{
				var cell=document.getElementById(this.data_struct[i].name+id);
				if(this.data_struct[i].visible==1)
					data_vals[i]=(cell.innerHTML);
				else
				{
					data_vals[i]=(cell.value);
				}
				
			}
			return data_vals;
		}
		//de dam bao cau truc, neu field nao khong hien dien trong form 
		//nhung van co trong data_struct se duoc gan trong
		static getDataFromForm(f)
		{
			var data_vals=[];
			data_vals[0]="0";
			for(i=1;i<this.data_struct.length;i++)
			{
				if(isEmpty(f.elements.namedItem(this.data_struct[i].name)))
				{
					data_vals[i]="";
					continue;
				}
				if(this.data_struct[i].type=="check")
				{
					if(this.data_struct[i].readonly==1)
					{
						if(f.elements.namedItem(this.data_struct[i].name).value!="")
							data_vals[i]="1";
						else
							data_vals[i]="";
					}
					else
					{
						if(f.elements.namedItem(this.data_struct[i].name).checked)
							data_vals[i]="1";
						else
							data_vals[i]="";
					}
				}
				else
				{
					data_vals[i]=f.elements.namedItem(this.data_struct[i].name).value;
					if(this.data_struct[i].required=="1")
					{
						if(data_vals[i].length<1)
						{
							showModalBlock(this.data_struct[i].head+": không được để trống");
							return false;
						}
					}
				}
			}
			
			return data_vals;
		}
		static genRowHeadingId()
		{
			return "idrowheading"+this.data_struct[0].name;
		}
		static genRowId(id)
		{
			return "idrow"+this.data_struct[0].name+id;
		}
		static genCellId(col_name,id)
		{
			return col_name+id;
		}

		static genRowCollapsibleItems(data_vals,genoutsideHTML)
		{
			var id="",idrow="",cmd="";
			id=data_vals[0];
			idrow=this.genRowId(data_vals[0]);
			var label="";
			var row="";
			if(genoutsideHTML)
				row+="<div class='collapsibleheader sub' style='margin-bottom:1px;' id='"+idrow+"' onclick=\"divToggleNext(this,itemDetails("+data_vals[0]+",'"+idrow+"sub'),0)\">";

			for(var j=1;j<data_vals.length;j++)
			{
				if(this.data_struct[j].visible==1)
				{
					if(label!="")
					{	label+="<br>&nbsp;";
						label+="<span>"+this.data_struct[j].head+": "+data_vals[j]+"</span>";
					}
					else{
						label+="<b>"+data_vals[j]+"</b>";
					}
				}
				else
				{
					label+="<input type='hidden' id='"+this.genCellId(this.data_struct[j].name,data_vals[0])+"' value='"+data_vals[j]+"'>";
				}
			}
			if(genoutsideHTML)
			{
				row+=label;
				row+="</div>";
				row+="<div class='collapsiblecontent' id='"+idrow+"sub'></div>";
				return row;
			}
			return label;
		}

		static genRow(data_vals)
		{
			var cmd="<input type='button' class='cmd' value='CMD' onclick=\"javascript:itemMenuPopup("+data_vals[0]+","+"0"+")\">";
			var rowid=this.genRowId(data_vals[0]);
			var row="",j=0;
			
			row+="<div class='divrow' id='"+rowid+"'>";
			for(j=1;j<data_vals.length;j++)
			{
				if(this.data_struct[j].visible==1)
				{
					if(this.data_struct[j].type=="check")
					{
						var value="";
						if(data_vals[j]=="")
							value="";
						else
							value="&#10003;";
						
						row+="<div class='divcell' id='"+this.genCellId(this.data_struct[j].name,data_vals[0])+"'>"+value+"</div>";
					}
					else
						row+="<div class='divcell' id='"+this.genCellId(this.data_struct[j].name,data_vals[0])+"'>"+data_vals[j]+"</div>";
				}
				else
					row+="<input type='hidden' id='"+this.genCellId(this.data_struct[j].name,data_vals[0])+"' value='"+data_vals[j]+"'>";
					
			}
			row+="<div class='divcell' style='padding:0px'>"+cmd+"</div>";
			row+="</div>";
			
			return row;
		}
		static saveForm(f,iddivshowitem=null,updategridview=1,fnCallBackNewRecordData=null)
		{
			var editformtype=f.elements.namedItem("editformtype").value;	
			var currentviewmode=f.elements.namedItem("currentviewmode").value;
			var data_vals=GridStructData.getDataFromForm(f);
			for(i=0;i<GridStructData.data_struct.length;i++)
			{
				if(GridStructData.data_struct[i].required==1)
				{
					if(data_vals[i].length<1)
					{
						showModalBlock(GridStructData.data_struct[i].head+": không được bỏ trống");
						return false;
					}
				}
			}
			var label="";if(data_vals.length>1)label=data_vals[1];
			var form = $(f);
			var url = form.attr('action');
			var dataString = form.serialize();
			
			showWaitModal("..");
			jQuery.ajax({
				url: url,
				data: dataString,
				type: "POST",
				success: function(data)
				{
					if(isDataFail(data))
					{
						showMsg("[Thất bại] "+getDataFail(data));
						return;
					}
					var newid=parseNumber(getDataOk(data));
					if(parseInt(newid)<=0)
					{
						showMsg("Thất bại");
						return false;
					}
					data_vals[0]=newid;
                    if(!isEmpty(fnCallBackNewRecordData))
                        fnCallBackNewRecordData(data_vals);
                    if(updategridview!=1)
                    {
                        hideModalBlock();
                        return;
                    }
					var iditem=null;
					var row;
					if(currentviewmode!="grid")
						row=GridStructData.genRowCollapsibleItems(data_vals,(editformtype=="add"));
					else
						row=GridStructData.genRow(data_vals);	
							
					if(editformtype=="edit")
					{
						iditem=document.getElementById(GridStructData.genRowId(newid));
						
						if(iditem!=null)
						{
							if(currentviewmode!="grid")
								iditem.innerHTML=row;
							else
								iditem.outerHTML=row;
						}
						hideModalBlock();
					}
					if(editformtype=="add")
					{
						var iditem=null;
						//co the co nhieu heading neu dinh dang hien thi collapse, tim row heading thuoc ve div nay
						
						if(!isEmpty(iddivshowitem))
						{
							if(currentviewmode!="grid")
							{
								if(isString(iddivshowitem))iddivshowitem=document.getElementById(iddivshowitem);	
								iddivshowitem.innerHTML=iddivshowitem.innerHTML+row;
							}
							else
							{
								iditem=GridStructData.findRowheadingInTable(iddivshowitem);
								if(iditem===null)
									iditem=document.getElementById("idrowheading");
								
								if(iditem!=null)
									iditem.outerHTML=iditem.outerHTML+row;
							}
						}
						
						//add_more
						showModalBlock(GridStructData.editform_content,"Đã thêm: "+label);
					}
					
				},
				error: function (){
					hideModal();
					showMsg("Thất bại",GridStructData.title);
				}
			});
			return false;
		}
		//id==0:add,>0:edit
        //updategridview 0/1: update change value for view if viewed
        //fnCallBackNewRecordData: call back function with new data
		static showEditForm(id,extrafieldvalue=null,iddivshowitem="",rowdata=null,currentviewmode="grid",updategridview=1,fnCallBackNewRecordData)
		{	//extrafieldvalue [{name,value}]
			//iddivshowitem: div chua danh sach item
			GridStructData.editfrom_firstfocus="";
			var i=0;
			var html="",type="",checkstatus="",prompt="",datasrc="";
			var data_vals=[];
			if(parseInt(id)>0)
			{
				data_vals=this.getDataFromId(id);
				type="edit";
				datasrc="in";
			}
			else if(!isEmpty(rowdata))
			{
				data_vals=rowdata.split(g_split_val);
				id=data_vals[0];
				type="edit";
				datasrc="out";
			}
			else
			{
				data_vals=null;
				type="add";
			}
			
			html+="<form  method='POST' action='?page="+this.page_source+"' onsubmit='return GridStructData.saveForm(this,\""+iddivshowitem+"\","+updategridview+","+fnCallBackNewRecordData+")' accept-charset='UTF-16'>";
			
			if(Array.isArray(extrafieldvalue))
			{
				for(i=0;i<extrafieldvalue.length;i++)
				{
					html+="<input type=\"hidden\" name=\""+extrafieldvalue[i].name+"\" value=\""+extrafieldvalue[i].value+"\" />";
				}
				
			}
			
			html+="<input type='hidden' name='editformtype' value='"+type+"' />";
			html+="<input type='hidden' name='currentviewmode' value='"+currentviewmode+"' />";

			html+="<input type='hidden' name='"+this.data_struct[0].name+"' value='"+id+"' />";
			html+="<div class='divtable'>";
			for(i=1;i<this.data_struct.length;i++)
				{	
					var value="";
					if((this.data_struct[i].visible=="")||(this.data_struct[i].visible==0))
					{
						if(!isEmpty(data_vals))
							value=data_vals[i];
						else
							value="";	
						html+="<input type=\"hidden\" name=\""+this.data_struct[i].name+"\" value=\""+value+"\">";
						continue;
					}
					if(this.data_struct[i].readonly==1)
					{
						if(!isEmpty(data_vals))
							value=data_vals[i];
						else
							value="";	
						html+="<input type='hidden' name=\""+this.data_struct[i].name+"\" value=\""+value+"\">";
						continue;
					}
					var name=this.data_struct[i].name;
					if(this.data_struct[i].prompt!==undefined)
						prompt="<div style='line-height:150%'>"+this.data_struct[i].prompt+"</div>";
					else
						prompt="";
					
					html+="<div class='divrow'>";
					value=this.data_struct[i].head;
					
					if(this.data_struct[i].required==1)
					{
						value+="*";
					}
					html+="<div class='divcell' style='width:%'>"+value+"</div>";
					value="";
					if(!isEmpty(data_vals))
						value=data_vals[i];
					else
						value="";
					if(this.data_struct[i].type=="check")
					{
						if(!isEmpty(data_vals))
						{
							if(data_vals[i]!="")
								checkstatus="checked";
							else
								checkstatus="";
						}
						else
							checkstatus="";
						value="on";
						html+="<div class='divcell'><input type='checkbox' name='"+name+"' value='"+value+"' "+checkstatus+" >"+prompt+"</div>";
					}
					else if(this.data_struct[i].type=="number")
					{
						html+="<div class='divcell'><input type='text' onkeyup='return fi_doFormat(this)' name='"+name+"' value='"+value+"'  style='width:100%;'>"+prompt+"</div>";
					}
					else if(this.data_struct[i].type=="select")
					{
						var ops="",select="";
						for(var k=0;k<this.data_struct[i].data.length;k++)
						{
							if((value==this.data_struct[i].data[k].id)||(value==this.data_struct[i].data[k].name))
								ops+="<option value=\'"+this.data_struct[i].data[k].id+"' selected>"+this.data_struct[i].data[k].name+"</option>";
							else
								ops+="<option value=\'"+this.data_struct[i].data[k].id+"'>"+this.data_struct[i].data[k].name+"</option>";	
						}
						if(this.data_struct[i].required!=1)
							ops="<option value=></option>"+ops;
						select="<select size='1' name='"+name+"' style='width:100%'>"+ops+"</select>";
						html+="<div class='divcell'>"+select+"</div>";	
					}
					else
					{
						html+="<div class='divcell'><input type='text' name='"+name+"' value=\""+value+"\" style='width:100%;'>"+prompt+"</div>";
					}
					if(this.data_struct[i].prompt!==undefined)
					{
						
					}
					html+="</div>";
				}
			
			html+="</div>";
			html+="<input type=\"button\" class=\"modalbutton modalbuttoncancel\" style=\"width:50%\" value=\"Đóng\" onclick=\"hideModalBlock(false)\">";
			html+="<input type=\"submit\" class=\"modalbutton modalbuttonok\" style=\"width:50%\" value=\"Ghi\">";
			html+="</form>";
			var title="Thêm";if(type=="edit")title="Sửa";
			//save current edit from for new add
			if(type=="add")
			{
				GridStructData.editfrom_header=title;
				GridStructData.editform_content=html;
			}
			
			showModalBlock(html,title);
		}
		static removeRow(id)
		{
			showWaitModal("Xóa..");
			var url="?page="+this.page_source+"&action=remove&"+this.data_struct[0].name+"="+id;
           
			var dataString = "";
			jQuery.ajax({
				url: url,
				data: dataString,
				type: "GET",
				success: function(data)
				{
					if(isDataOk(data))
					{
						var id=parseNumber(getDataOk(data));
						var idrow=GridStructData.genRowId(id);
						var iditem=document.getElementById(idrow);
						if(iditem!=null)
							iditem.outerHTML="";
						var idrowsub=document.getElementById(idrow+"sub");
						if(idrowsub!=null)idrowsub.outerHTML="";
						hideModalBlock();
					}
					else
					{
						var mes=getDataFail(data);
						showMsg(mes,GridStructData.page_title);
						return;
					}
				},
				error: function (){
					
					showMsg("Thất bại")
				}
			});

		}
		static getAndShow(pageno,idcontentlist,fnCMD,fnCmdTitle)
		{
			var url="?page="+this.page_source+"&action=list&pageno="+pageno;
			
			var dataString="";
			jQuery.ajax({
				url: url,
				data: dataString,
				type: "GET",
				success: function(data)
				{
					GridStructData.showGrid(data,idcontentlist,fnCMD,fnCmdTitle);
					hideModalBlock();
				},
				error: function ()
				{
					hideModalBlock();
				}
			});
		}
		static showUpdateable(data,containeridname)
		{
			var rows=data.split(g_split_row);
			var row_count=rows.length;
			if(row_count<1)
				return;
			var allrows="",j=0,i=0,pageno=0,pagecount=0,pagesize=20,startindex=0;
			
			if(rows[0].substr(0,g_sign_pageconfig.length)==g_sign_pageconfig)
			{
				var configs=rows[0].split(g_split_val);	
				startindex=1;
				
				if(configs.length==3)
				{
					
					pagesize=parseInt(configs[0]);
					pageno=parseInt(configs[1]);
					pagecount=parseInt(configs[2]);
				}
			}
			for(i=startindex;i<row_count;i++)
			{
				var data_vals=rows[i].split(g_split_val);
				var id="",idrow="";
				
				id=data_vals[0];
				idrow=this.genRowId(data_vals[0]);
				allrows+="<div class='divrow' id='"+idrow+"'>";
				for(j=1;j<data_vals.length;j++)
				{
					var value="",name="";
					name=GridStructData.data_struct[j].name+id;
					value=data_vals[j];
					
					if(GridStructData.data_struct[j].visible==1)
					{
						if(GridStructData.data_struct[j].editable==1)
						{
							allrows+="<div class='divcell'>";
							if(GridStructData.data_struct[j].type=="number")
								allrows+="<input type='text' onkeyup='return fi_doFormat(this)' name='"+name+"' value='"+value+"' style='width:100%;text-align:right'>";
							else
								allrows+="<input type='text' name='"+name+"' value='"+value+"' style='width:100%'>";
							allrows+="</div>";	
						}
						else
						{
							if(this.data_struct[j].type=="check")
							{
								if(data_vals[j]=="")
									value="";
								else
									value=g_char_yes;
							}
							else
								value=data_vals[j];
							allrows+="<div class='divcell' id='"+this.genCellId(this.data_struct[j].name,data_vals[0])+"'>"+value+"</div>";
						}
					}
					else
					{
						allrows+="<input type='hidden' name='"+name+"' value='"+value+"'>";
					}
					
				}
				
				allrows+="</div>";
			}
			
			var rowheading="";
			rowheading+="<div class='divrow heading' id='idrowheading'>";
			for(i=1;i<this.data_struct.length;i++)
			{
				if(this.data_struct[i].visible==1)
				{
					var style="";if(this.data_struct[i].width!="")style="style='width:"+this.data_struct[i].width+"'";
					rowheading+="<div class='divcell' "+style+">"+this.data_struct[i].head+"</div>";
				}
				
				//	rowheading+="<div class='divcell'>"+this.data_struct[i].head+"</div>";
			}
			rowheading+="</div>";

			var pageinfo=DlgSelect.getPageIndexSelect(pageno,pagecount,null,"showList");

			/*
			if(pagecount>1)
			{
				pageinfo+="<div class='pageindex'>";
				for(i=1;i<=pagecount;i++)
				{
					if(i==pageno)
						pageinfo+="<a class=\"pageindexfocus\" href=\"javascript:void(0)\" onclick=\"javascript:showList("+i+")\">"+i+"</a>";
					else
						pageinfo+="<a class=\"pageindex\" href=\"javascript:void(0)\" onclick=\"javascript:showList("+i+")\">"+i+"</a>";
				}
				pageinfo+="</div>";
				
			}
			*/
			
			var idshow=document.getElementById(containeridname);
			idshow.innerHTML="<div class='divtable'>"+rowheading+allrows+"</div>"+pageinfo;
		}
		static showListCollapsibleItemsAddRow(data,containeridname,fnCMD)
		{
			var data_vals=data.split(g_split_val);
			var id=data_vals[0],idrow="",cmd="",row="";
			var j=0;
			
			idrow=this.genRowId(data_vals[0]);
			row+="<div class='collapsibleheader sub' style='margin-bottom:1px' id='"+idrow+"' onclick=\"divToggleNext(this,"+fnCMD+"("+data_vals[0]+",'"+idrow+"sub'),0)\">";
			for(j=1;j<data_vals.length;j++)
			{
				if(this.data_struct[j].visible==1)
				{
					
					row+="<span>"+data_vals[j]+"</span>";
				}
				else
				{
					row+="<input type='hidden' id='"+this.genCellId(this.data_struct[j].name,data_vals[0])+"' value='"+data_vals[j]+"'>";
				}
			}
			row+="</div>";
			row+="<div class='collapsiblecontent' id='"+idrow+"sub'></div>";	
			if(isString(containeridname))
			{
				var containeridname=document.getElementById(containeridname);
				containeridname.innerHTML=row+containeridname.innerHTML;	
			}
			else
				containeridname.innerHTML=row+containeridname.innerHTML;	
		}
		//arExtraButtons [{caption,functioncallback(id)}]
		static showListCollapsibleItems(data,containeridname,fnCMD,arExtraButtons)
		{
			var rows=data.split(g_split_row);
			var row_count=rows.length;
			if(row_count<1)
				return;
			var allrows="",j=0,i=0,pageno=0,pagecount=0,pagesize=20,startindex=0;
			if(fnCMD===undefined)fnCMD="itemMenuPopup";
			if(rows[0].substr(0,g_sign_pageconfig.length)==g_sign_pageconfig)
			{
				var configs=rows[0].split(g_split_val);	
				startindex=1;
				
				if(configs.length==3)
				{
					
					pagesize=parseInt(configs[0]);
					pageno=parseInt(configs[1]);
					pagecount=parseInt(configs[2]);
				}
			}
			for(i=startindex;i<row_count;i++)
			{
				var data_vals=rows[i].split(g_split_val);
				var id="",idrow="",cmd="";
				id=data_vals[0];
				idrow=this.genRowId(data_vals[0]);
				allrows+="<div class='collapsibleheader sub' style='margin-bottom:1px;' id='"+idrow+"' onclick=\"divToggleNext(this,"+fnCMD+"("+data_vals[0]+",'"+idrow+"sub'),0)\">";
				if(Array.isArray(arExtraButtons))
				{
					for(var k=0;k<arExtraButtons.length;k++)
					{
						allrows+="<input type='button' class='collapsibleheaderbutton' style='float:right' value='"+arExtraButtons[k].caption+"' onclick=\""+arExtraButtons[k].function+"("+data_vals[0]+");event.stopPropagation()\">";
					}
				}
				var label="";
				for(j=1;j<data_vals.length;j++)
				{
					if(this.data_struct[j].visible==1)
					{
						if(label!="")
						{	label+="<br>&nbsp;";
							label+="<span>"+this.data_struct[j].head+": "+data_vals[j]+"</span>";
						}
						else{
							label+="<b>"+data_vals[j]+"</b>";
						}
					}
					else
					{
						allrows+="<input type='hidden' id='"+this.genCellId(this.data_struct[j].name,data_vals[0])+"' value='"+data_vals[j]+"'>";
					}
				}
				allrows+=label;
				allrows+="</div>";
				allrows+="<div class='collapsiblecontent' id='"+idrow+"sub'></div>";
			}
			
			var rowheading="";
			rowheading+="<div class='divrow heading' id='idrowheading'>";
			for(i=1;i<this.data_struct.length;i++)
			{
				if(this.data_struct[i].visible>0)
				{
					var style="";if(this.data_struct[i].width!="")style="style='width:"+this.data_struct[i].width+"'";
					rowheading+="<div class='divcell' "+style+">"+this.data_struct[i].head+"</div>";
				}
			}
			rowheading+="</div>";

			var pageinfo="";
			if(pagecount>1)
			{
				pageinfo+="<div class='pageindex'>";
				for(i=1;i<=pagecount;i++)
				{
					if(i==pageno)
						pageinfo+="<a class=\"pageindexfocus\" href=\"javascript:void(0)\" onclick=\"javascript:showList("+i+")\">"+i+"</a>";
					else
						pageinfo+="<a class=\"pageindex\" href=\"javascript:void(0)\" onclick=\"javascript:showList("+i+")\">"+i+"</a>";
				}
				pageinfo+="</div>";
				
			}
			var idshow=document.getElementById(containeridname);
			idshow.innerHTML=allrows+pageinfo;
		}
		static findRowheadingInTable(iddivtable)
		{
			if(iddivtable===undefined)return null;
			if(iddivtable==null)return null;
			var searchEles;
			if(isString(iddivtable))
			{
				searchEles = document.getElementById(iddivtable);
				if(searchEles===null)return null;
				searchEles=searchEles.children;
			}	
			else
				searchEles = iddivtable.children;
			
			if(searchEles===undefined)return;if(searchEles===null)return;if(searchEles.length<1)return;
			if(searchEles[0].classList=="divtable")
				searchEles=searchEles[0].children;
			if(searchEles===undefined)return;if(searchEles===null)return;if(searchEles.length<1)return;
			if(searchEles.length<1)return;
			for(var i = 0; i < searchEles.length; i++) {
				
				if(searchEles[i].classList=="divrow heading")
				{
					return searchEles[i];
				}
			}
		} 

		static showGridUpdateCell(divnamecontainer,columnname,idvalincell,newvalue)
		{
			var idvalincell=GridStructData.genCellId(columnname,idvalincell);
			var searchEles=null;
			
			if(isString(divnamecontainer))
				searchEles = document.getElementById(divnamecontainer).children;
			else
				searchEles = divnamecontainer.children;
			if(searchEles===undefined)return;if(searchEles===null)return;if(searchEles.length<1)return;
			if(searchEles[0].classList=="divtable")
				searchEles=searchEles[0].children;
			if(searchEles===undefined)return;if(searchEles===null)return;if(searchEles.length<1)return;
			if(searchEles.length<1)return;
			for(var i = 0; i < searchEles.length; i++) {
				
				if(searchEles[i].classList=="divrow")
				{
					var cols=searchEles[i].children;
					if(cols===undefined)continue;if(cols===null)continue;if(cols.length<1)continue;
					for(var j=0;j<cols.length;j++)
					{
						if(cols[j].id.indexOf(columnname)==0) {
							if(cols[j].id==idvalincell)
							{
								cols[j].innerHTML=newvalue;
								break;
							}
						}		
					}
				}
			}

		}

		//cau truc table: divtable->divrow->cell (columname+idval)
		static viewSetOneChecked(divnamecontainer,columnname,idvalchecked,checkvalue=1,clearothercheck=1)
		{
			var idcellchecked=GridStructData.genCellId(columnname,idvalchecked);
			var searchEles=null;
			
			if(isString(divnamecontainer))
				searchEles = document.getElementById(divnamecontainer).children;
			else
				searchEles = divnamecontainer.children;
			if(searchEles===undefined)return;if(searchEles===null)return;if(searchEles.length<1)return;
			if(searchEles[0].classList=="divtable")
				searchEles=searchEles[0].children;
			if(searchEles===undefined)return;if(searchEles===null)return;if(searchEles.length<1)return;
			if(searchEles.length<1)return;
			for(var i = 0; i < searchEles.length; i++) {
				
				if(searchEles[i].classList=="divrow")
				{
					var cols=searchEles[i].children;
					if(cols===undefined)continue;if(cols===null)continue;if(cols.length<1)continue;
					for(var j=0;j<cols.length;j++)
					{
						if(cols[j].id.indexOf(columnname)==0) {
							if(cols[j].id==idcellchecked)
							{
								if(checkvalue===1)
									cols[j].innerHTML="&#10003";
								else
									cols[j].innerHTML="";
							}
							else if(clearothercheck===1)
								cols[j].innerHTML="";
						}		
					}
				}
			}

		}
		static showGridUpdateRow(data)
		{
			var data_vals=data.split(g_split_val);
			var iditem=document.getElementById(GridStructData.genRowId(data_vals[0]));
			var row=GridStructData.genRow(data_vals);
			iditem.outerHTML=row;			
		}
		static showGridAddRow(data)
		{
			var data_vals=data.split(g_split_val);
			var row=GridStructData.genRow(data_vals);
			var idrowheading=document.getElementById("idrowheading");
			idrowheading.outerHTML=idrowheading.outerHTML+row;
		}
		//visible=0,1,2 
		static showGrid(data,idcontent,fnCMD,fnCmdTitle)
		{
			if(fnCmdTitle===undefined)fnCmdTitle="CMD";
			var idshow=null;
			if(idcontent.constructor.name==="String")
				idshow=document.getElementById(idcontent);
			else
				idshow=idcontent;
            
		//ROW_HEADING
        
			var rowheading="";
				rowheading+="<div class='divrow heading' id='idrowheading'>";
				for(i=1;i<this.data_struct.length;i++)
				{
					if(this.data_struct[i].visible==1)
					{
						var style="";if(this.data_struct[i].width!="")style="style='width:"+this.data_struct[i].width+"'";
						rowheading+="<div class='divcell' "+style+">"+this.data_struct[i].head+"</div>";
					}
				}

				if(fnCMD!=undefined)
                
					rowheading+="<div class='divcell' style='width:10%'>CMD</div>";
			rowheading+="</div>";
              
			if(data.length<=g_split_row.length)
			{
				idshow.innerHTML="<div class='divtable'>"+rowheading+"</div>";
				hideModalBlock();
				return;
			}
			var rows=data.split(g_split_row);
			var row_count=rows.length;
			if(row_count<1)
			{
				idshow.innerHTML="<div class='divtable'>"+rowheading+"</div>";
				hideModalBlock();
				return;
			}
			var allrows="",j=0,i=0,pageno=0,pagecount=0,pagesize=20,startindex=0;
			//if(fnCMD===undefined)
			//	fnCMD="itemMenuPopup";
			if(rows[0].substr(0,g_sign_pageconfig.length)==g_sign_pageconfig)
			{
				var configs=rows[0].split(g_split_val);	
				startindex=1;
				
				if(configs.length==3)
				{
					
					pagesize=parseInt(configs[0]);
					pageno=parseInt(configs[1]);
					pagecount=parseInt(configs[2]);
				}
			}
            
			for(i=startindex;i<row_count;i++)
			{
				var data_vals=rows[i].split(g_split_val);
				var id="",idrow="",cmd="";
				var idparam="";
				id=data_vals[0];
				idrow=this.genRowId(data_vals[0]);
				if(this.data_struct[0].type=="text")
					idparam="\""+data_vals[0]+"\"";
				else
					idparam=data_vals[0];
				if(idparam=="")idparam="0";
				if(fnCMD!=undefined)
					cmd="<input type='button' class='cmd' value='"+fnCmdTitle+"' onclick='"+fnCMD+"("+idparam+",0)'>";
				allrows+="<div class='divrow' id='"+idrow+"'>";
                
				for(j=1;j<data_vals.length;j++)
				{
					if(this.data_struct[j].visible==1)
					{
						var value="",style="";
						if(this.data_struct[j].type=="check")
						{
							if((data_vals[j]=="")||(data_vals[j]=="0"))
								value="";
							else
								value="&#10003;";
						}
						else
						{
							value=data_vals[j];
							if(this.data_struct[j].type=="number")
							{
								style="style='text-align:right;'";
								
							}
						}
						
						allrows+="<div class='divcell' "+style+" id='"+this.genCellId(this.data_struct[j].name,data_vals[0])+"'>"+value+"</div>";
					}
					else
					{
						value=data_vals[j];
						allrows+="<input type='hidden' id='"+this.genCellId(this.data_struct[j].name,data_vals[0])+"' value='"+value+"'>";
					}
				}
				if(fnCMD!=undefined)
					allrows+="<div class='divcell padding0'>"+cmd+"</div>";
				
				allrows+="</div>";
				//allrows+="<div id='"+idrow+"sub'></div>";
			}
            
			var pageinfo=DlgSelect.getPageIndexSelect(pageno,pagecount,null,"showList");
			/*
			if(pagecount>1)
			{
				pageinfo+="<div class='pageindex'>";
				for(i=1;i<=pagecount;i++)
				{
					if(i==pageno)
						pageinfo+="<a class=\"pageindexfocus\" href=\"javascript:void(0)\" onclick=\"javascript:showList("+i+")\">"+i+"</a>";
					else
						pageinfo+="<a class=\"pageindex\" href=\"javascript:void(0)\" onclick=\"javascript:showList("+i+")\">"+i+"</a>";
				}
				pageinfo+="</div>";
				
			}
			*/
			
			idshow.innerHTML="<div class='divtable'>"+rowheading+allrows+"</div>"+pageinfo;
			hideModalBlock();
		}
		static showItemDetails(id)
		{
			//ajaxGetURL("?page=kb_khohang&action=details&store_id="+id,"details",ajaxOnReturnThis);
	
		}
	}
//END_CLASS_GRID_DATA_STRUCT

//*START_CLASS_GRID_VIEW
class GridView
	{
		//var data_struct=[
		//		{name:"id",head:"",visible:0},
		//		{name:""  ,head:"",visible:1/0,type:"text/check/number",required:"1/0"},
		//];
		data_struct=[];
		id_view = null;
		page_source="";
		page_title="";
		show_collapsiblerow=0;
		editfrom_header="";//current edit from, is showing
		editform_content="";
		editfrom_firstfocus="";
		
		constructor(in_data_struct,in_id_view)
		{
			this.data_struct=in_data_struct;
			this.id_view=(in_id_view);
		}
		getDataFromId(id)
		{
			var data_vals=[];
			data_vals[0]=id;
			for(i=1;i<this.data_struct.length;i++)
			{
				var cell=document.getElementById(this.data_struct[i].name+id);
				if(this.data_struct[i].visible==1)
					data_vals[i]=(cell.innerHTML);
				else
				{
					data_vals[i]=(cell.value);
				}
				
			}
			return data_vals;
		}
		//de dam bao cau truc, neu field nao khong hien dien trong form 
		//nhung van co trong data_struct se duoc gan trong
		getDataFromForm(f)
		{
			var data_vals=[];
			data_vals[0]="0";
			for(i=1;i<this.data_struct.length;i++)
			{
				if(isEmpty(f.elements.namedItem(this.data_struct[i].name)))
				{
					data_vals[i]="";
					continue;
				}
				if(this.data_struct[i].type=="check")
				{
					if(this.data_struct[i].readonly==1)
					{
						if(f.elements.namedItem(this.data_struct[i].name).value!="")
							data_vals[i]="1";
						else
							data_vals[i]="";
					}
					else
					{
						if(f.elements.namedItem(this.data_struct[i].name).checked)
							data_vals[i]="1";
						else
							data_vals[i]="";
					}
				}
				else
				{
					data_vals[i]=f.elements.namedItem(this.data_struct[i].name).value;
					if(this.data_struct[i].required=="1")
					{
						if(data_vals[i].length<1)
						{
							showModalBlock(this.data_struct[i].head+": không được để trống");
							return false;
						}
					}
				}
			}
			
			return data_vals;
		}
		genRowHeadingId()
		{
			return this.id_view+"idrowheading"+this.data_struct[0].name;
		}
		genRowId(id)
		{
			return this.id_view+"idrow"+data_struct[0].name+id;
		}
		genCellId(col_name,id)
		{
			return this.id_view+col_name+id;
		}

		genRowCollapsibleItems(data_vals,genoutsideHTML)
		{
			var id="",idrow="",cmd="";
			id=data_vals[0];
			idrow=this.genRowId(data_vals[0]);
			var label="";
			var row="";
			if(genoutsideHTML)
				row+="<div class='collapsibleheader sub' style='margin-bottom:1px;' id='"+idrow+"' onclick=\"divToggleNext(this,itemDetails("+data_vals[0]+",'"+idrow+"sub'),0)\">";

			for(var j=1;j<data_vals.length;j++)
			{
				if(this.data_struct[j].visible==1)
				{
					if(label!="")
					{	label+="<br>&nbsp;";
						label+="<span>"+this.data_struct[j].head+": "+data_vals[j]+"</span>";
					}
					else{
						label+="<b>"+data_vals[j]+"</b>";
					}
				}
				else
				{
					label+="<input type='hidden' id='"+this.genCellId(this.data_struct[j].name,data_vals[0])+"' value='"+data_vals[j]+"'>";
				}
			}
			if(genoutsideHTML)
			{
				row+=label;
				row+="</div>";
				row+="<div class='collapsiblecontent' id='"+idrow+"sub'></div>";
				return row;
			}
			return label;
		}

		genRow(data_vals)
		{
			var cmd="<input type='button' class='cmd' value='CMD' onclick=\"javascript:itemMenuPopup("+data_vals[0]+","+"0"+")\">";
			var rowid=this.genRowId(data_vals[0]);
			var row="",j=0;
			
			row+="<div class='divrow' id='"+rowid+"'>";
			for(j=1;j<data_vals.length;j++)
			{
				if(this.data_struct[j].visible==1)
				{
					if(this.data_struct[j].type=="check")
					{
						var value="";
						if(data_vals[j]=="")
							value="";
						else
							value="&#10003;";
						
						row+="<div class='divcell' id='"+this.genCellId(this.data_struct[j].name,data_vals[0])+"'>"+value+"</div>";
					}
					else
						row+="<div class='divcell' id='"+this.genCellId(this.data_struct[j].name,data_vals[0])+"'>"+data_vals[j]+"</div>";
				}
				else
					row+="<input type='hidden' id='"+this.genCellId(this.data_struct[j].name,data_vals[0])+"' value='"+data_vals[j]+"'>";
					
			}
			row+="<div class='divcell' style='padding:0px'>"+cmd+"</div>";
			row+="</div>";
			
			return row;
		}
		saveForm(f,iddivshowitem)
		{
			var editformtype=f.elements.namedItem("editformtype").value;	
			var currentviewmode=f.elements.namedItem("currentviewmode").value;
			var data_vals=this.getDataFromForm(f);
			for(i=0;i<this.data_struct.length;i++)
			{
				if(this.data_struct[i].required==1)
				{
					if(data_vals[i].length<1)
					{
						showModalBlock(this.data_struct[i].head+": không được bỏ trống");
						return false;
					}
				}
			}
			var label="";if(data_vals.length>1)label=data_vals[1];
			var form = $(f);
			var url = form.attr('action');
			var dataString = form.serialize();
			var thisGV = this
			showWaitModal("..");
			jQuery.ajax({
				url: url,
				data: dataString,
				type: "POST",
				success: function(data)
				{
					if(isDataFail(data))
					{
						showMsg("[Thất bại] "+getDataFail(data));
						return;
					}
					var newid=parseNumber(getDataOk(data));
					if(parseInt(newid)<=0)
					{
						showMsg("Thất bại");
						return false;
					}
					data_vals[0]=newid;
					var iditem=null;
					var row;
					if(currentviewmode!="grid")
						row=thisGV.genRowCollapsibleItems(data_vals,(editformtype=="add"));
					else
						row=thisGV.genRow(data_vals);	
							
					if(editformtype=="edit")
					{
						iditem=document.getElementById(this.genRowId(newid));
						
						if(iditem!=null)
						{
							if(currentviewmode!="grid")
								iditem.innerHTML=row;
							else
								iditem.outerHTML=row;
						}
						hideModalBlock();
					}
					if(editformtype=="add")
					{
						var iditem=null;
						//co the co nhieu heading neu dinh dang hien thi collapse, tim row heading thuoc ve div nay
						
						if(iddivshowitem!==undefined)
						{
							if(currentviewmode!="grid")
							{
								if(isString(iddivshowitem))iddivshowitem=document.getElementById(iddivshowitem);	
								iddivshowitem.innerHTML=iddivshowitem.innerHTML+row;
							}
							else
							{
								iditem=this.findRowheadingInTable(iddivshowitem);
								if(iditem===null)
									iditem=document.getElementById("idrowheading");
								
								if(iditem!=null)
									iditem.outerHTML=iditem.outerHTML+row;
							}
						}
						
						//add_more
						showModalBlock(this.editform_content,"Đã thêm: "+label);
					}
					
				},
				error: function (){
					hideModal();
					showMsg("Thất bại",this.title);
				}
			});
			return false;
		}
		
		showEditForm(id,extrafieldvalue,iddivshowitem,rowdata,currentviewmode)//id==0:add,>0:edit
		{	//extrafieldvalue [{name,value}]
			//iddivshowitem: div chua danh sach item
			this.editfrom_firstfocus="";
			if(currentviewmode===undefined)currentviewmode="grid";
			var i=0;
			var html="",type="",checkstatus="",prompt="",datasrc="";
			var data_vals=[];
			if(parseInt(id)>0)
			{
				data_vals=this.getDataFromId(id);
				type="edit";
				datasrc="in";
			}
			else if((rowdata!==undefined)&&(rowdata!=null))
			{
				data_vals=rowdata.split(g_split_val);
				id=data_vals[0];
				type="edit";
				datasrc="out";
			}
			else
			{
				data_vals=null;
				type="add";
			}
			
			html+="<form  method='POST' action='?page="+this.page_source+"' onsubmit='return this.saveForm(this,\""+iddivshowitem+"\")' accept-charset='UTF-16'>";
			
			if(Array.isArray(extrafieldvalue))
			{
				for(i=0;i<extrafieldvalue.length;i++)
				{
					html+="<input type=\"hidden\" name=\""+extrafieldvalue[i].name+"\" value=\""+extrafieldvalue[i].value+"\" />";
				}
				
			}
			
			html+="<input type='hidden' name='editformtype' value='"+type+"' />";
			html+="<input type='hidden' name='currentviewmode' value='"+currentviewmode+"' />";

			html+="<input type='hidden' name='"+this.data_struct[0].name+"' value='"+id+"' />";
			html+="<div class='divtable'>";
			for(i=1;i<this.data_struct.length;i++)
				{	
					var value="";
					if((this.data_struct[i].visible=="")||(this.data_struct[i].visible==0))
					{
						if(!isEmpty(data_vals))
							value=data_vals[i];
						else
							value="";	
						html+="<input type=\"hidden\" name=\""+this.data_struct[i].name+"\" value=\""+value+"\">";
						continue;
					}
					if(this.data_struct[i].readonly==1)
					{
						if(!isEmpty(data_vals))
							value=data_vals[i];
						else
							value="";	
						html+="<input type='hidden' name=\""+this.data_struct[i].name+"\" value=\""+value+"\">";
						continue;
					}
					var name=this.data_struct[i].name;
					if(this.data_struct[i].prompt!==undefined)
						prompt="<div style='line-height:150%'>"+this.data_struct[i].prompt+"</div>";
					else
						prompt="";
					
					html+="<div class='divrow'>";
					value=this.data_struct[i].head;
					
					if(this.data_struct[i].required==1)
					{
						value+="*";
					}
					html+="<div class='divcell' style='width:%'>"+value+"</div>";
					value="";
					if(!isEmpty(data_vals))
						value=data_vals[i];
					else
						value="";
					if(this.data_struct[i].type=="check")
					{
						if(!isEmpty(data_vals))
						{
							if(data_vals[i]!="")
								checkstatus="checked";
							else
								checkstatus="";
						}
						else
							checkstatus="";
						value="on";
						html+="<div class='divcell'><input type='checkbox' name='"+name+"' value='"+value+"' "+checkstatus+" >"+prompt+"</div>";
					}
					else if(this.data_struct[i].type=="number")
					{
						html+="<div class='divcell'><input type='text' onkeyup='return fi_doFormat(this)' name='"+name+"' value='"+value+"'  style='width:100%;'>"+prompt+"</div>";
					}
					else if(this.data_struct[i].type=="select")
					{
						var ops="",select="";
						for(var k=0;k<this.data_struct[i].data.length;k++)
						{
							if((value==this.data_struct[i].data[k].id)||(value==this.data_struct[i].data[k].name))
								ops+="<option value=\'"+this.data_struct[i].data[k].id+"' selected>"+this.data_struct[i].data[k].name+"</option>";
							else
								ops+="<option value=\'"+this.data_struct[i].data[k].id+"'>"+this.data_struct[i].data[k].name+"</option>";	
						}
						if(this.data_struct[i].required!=1)
							ops="<option value=></option>"+ops;
						select="<select size='1' name='"+name+"' style='width:100%'>"+ops+"</select>";
						html+="<div class='divcell'>"+select+"</div>";	
					}
					else
					{
						html+="<div class='divcell'><input type='text' name='"+name+"' value=\""+value+"\" style='width:100%;'>"+prompt+"</div>";
					}
					if(this.data_struct[i].prompt!==undefined)
					{
						
					}
					html+="</div>";
				}
			
			html+="</div>";
			html+="<input type=\"button\" class=\"modalbutton modalbuttoncancel\" style=\"width:50%\" value=\"Đóng\" onclick=\"hideModalBlock(false)\">";
			html+="<input type=\"submit\" class=\"modalbutton modalbuttonok\" style=\"width:50%\" value=\"Ghi\">";
			html+="</form>";
			var title="Thêm";if(type=="edit")title="Sửa";
			//save current edit from for new add
			if(type=="add")
			{
				this.editfrom_header=title;
				this.editform_content=html;
			}
			
			showModalBlock(html,title);
		}
		removeRow(id)
		{
			showWaitModal("Xóa..");
			var url="?page="+this.page_source+"&action=remove&"+this.data_struct[0].name+"="+id;
			var dataString = "";
			var thisGV = this;
			jQuery.ajax({
				url: url,
				data: dataString,
				type: "GET",
				success: function(data)
				{
					if(isDataOk(data))
					{
						var id=parseNumber(getDataOk(data));
						var idrow=thisGV.genRowId(id);
						var iditem=document.getElementById(idrow);
						if(iditem!=null)
							iditem.outerHTML="";
						var idrowsub=document.getElementById(idrow+"sub");
						if(idrowsub!=null)idrowsub.outerHTML="";
						hideModalBlock();
					}
					else
					{
						var mes=getDataFail(data);
						showMsg(mes,thisGV.page_title);
						return;
					}
				},
				error: function (){
					
					showMsg("Thất bại")
				}
			});

		}
		getAndShow(pageno,idcontentlist,fnCMD,fnCmdTitle)
		{
			var url="?page="+this.page_source+"&action=list&pageno="+pageno;
			var dataString="";
			var thisGV = this;
			jQuery.ajax({
				url: url,
				data: dataString,
				type: "GET",
				success: function(data)
				{
					thisGV.showGrid(data,idcontentlist,fnCMD,fnCmdTitle);
				},
				error: function ()
				{
					var idshow = document.getElementById(idcontentlist);
					idshow.innerHTML = "Lỗi nạp.."
				}
			});
		}
		showUpdateable(data,containeridname)
		{
			var rows=data.split(g_split_row);
			var row_count=rows.length;
			if(row_count<1)
				return;
			var allrows="",j=0,i=0,pageno=0,pagecount=0,pagesize=20,startindex=0;
			
			if(rows[0].substr(0,g_sign_pageconfig.length)==g_sign_pageconfig)
			{
				var configs=rows[0].split(g_split_val);	
				startindex=1;
				
				if(configs.length==3)
				{
					
					pagesize=parseInt(configs[0]);
					pageno=parseInt(configs[1]);
					pagecount=parseInt(configs[2]);
				}
			}
			for(i=startindex;i<row_count;i++)
			{
				var data_vals=rows[i].split(g_split_val);
				var id="",idrow="";
				
				id=data_vals[0];
				idrow=this.genRowId(data_vals[0]);
				allrows+="<div class='divrow' id='"+idrow+"'>";
				for(j=1;j<data_vals.length;j++)
				{
					var value="",name="";
					name=this.data_struct[j].name+id;
					value=data_vals[j];
					
					if(this.data_struct[j].visible==1)
					{
						if(this.data_struct[j].editable==1)
						{
							allrows+="<div class='divcell'>";
							if(this.data_struct[j].type=="number")
								allrows+="<input type='text' onkeyup='return fi_doFormat(this)' name='"+name+"' value='"+value+"' style='width:100%;text-align:right'>";
							else
								allrows+="<input type='text' name='"+name+"' value='"+value+"' style='width:100%'>";
							allrows+="</div>";	
						}
						else
						{
							if(this.data_struct[j].type=="check")
							{
								if(data_vals[j]=="")
									value="";
								else
									value=g_char_yes;
							}
							else
								value=data_vals[j];
							allrows+="<div class='divcell' id='"+this.genCellId(this.data_struct[j].name,data_vals[0])+"'>"+value+"</div>";
						}
					}
					else
					{
						allrows+="<input type='hidden' name='"+name+"' value='"+value+"'>";
					}
					
				}
				
				allrows+="</div>";
			}
			
			var rowheading="";
			rowheading+="<div class='divrow heading' id='idrowheading'>";
			for(i=1;i<this.data_struct.length;i++)
			{
				if(this.data_struct[i].visible==1)
				{
					var style="";if(this.data_struct[i].width!="")style="style='width:"+this.data_struct[i].width+"'";
					rowheading+="<div class='divcell' "+style+">"+this.data_struct[i].head+"</div>";
				}
				
				//	rowheading+="<div class='divcell'>"+this.data_struct[i].head+"</div>";
			}
			rowheading+="</div>";

			var pageinfo=DlgSelect.getPageIndexSelect(pageno,pagecount,null,"showList");

			/*
			if(pagecount>1)
			{
				pageinfo+="<div class='pageindex'>";
				for(i=1;i<=pagecount;i++)
				{
					if(i==pageno)
						pageinfo+="<a class=\"pageindexfocus\" href=\"javascript:void(0)\" onclick=\"javascript:showList("+i+")\">"+i+"</a>";
					else
						pageinfo+="<a class=\"pageindex\" href=\"javascript:void(0)\" onclick=\"javascript:showList("+i+")\">"+i+"</a>";
				}
				pageinfo+="</div>";
				
			}
			*/
			
			var idshow=document.getElementById(containeridname);
			idshow.innerHTML="<div class='divtable'>"+rowheading+allrows+"</div>"+pageinfo;
		}
		showListCollapsibleItemsAddRow(data,containeridname,fnCMD)
		{
			var data_vals=data.split(g_split_val);
			var id=data_vals[0],idrow="",cmd="",row="";
			var j=0;
			
			idrow=this.genRowId(data_vals[0]);
			row+="<div class='collapsibleheader sub' style='margin-bottom:1px' id='"+idrow+"' onclick=\"divToggleNext(this,"+fnCMD+"("+data_vals[0]+",'"+idrow+"sub'),0)\">";
			for(j=1;j<data_vals.length;j++)
			{
				if(this.data_struct[j].visible==1)
				{
					
					row+="<span>"+data_vals[j]+"</span>";
				}
				else
				{
					row+="<input type='hidden' id='"+this.genCellId(this.data_struct[j].name,data_vals[0])+"' value='"+data_vals[j]+"'>";
				}
			}
			row+="</div>";
			row+="<div class='collapsiblecontent' id='"+idrow+"sub'></div>";	
			if(isString(containeridname))
			{
				var containeridname=document.getElementById(containeridname);
				containeridname.innerHTML=row+containeridname.innerHTML;	
			}
			else
				containeridname.innerHTML=row+containeridname.innerHTML;	
		}
		//arExtraButtons [{caption,functioncallback(id)}]
		showListCollapsibleItems(data,containeridname,fnCMD,arExtraButtons)
		{
			var rows=data.split(g_split_row);
			var row_count=rows.length;
			if(row_count<1)
				return;
			var allrows="",j=0,i=0,pageno=0,pagecount=0,pagesize=20,startindex=0;
			if(fnCMD===undefined)fnCMD="itemMenuPopup";
			if(rows[0].substr(0,g_sign_pageconfig.length)==g_sign_pageconfig)
			{
				var configs=rows[0].split(g_split_val);	
				startindex=1;
				
				if(configs.length==3)
				{
					
					pagesize=parseInt(configs[0]);
					pageno=parseInt(configs[1]);
					pagecount=parseInt(configs[2]);
				}
			}
			for(i=startindex;i<row_count;i++)
			{
				var data_vals=rows[i].split(g_split_val);
				var id="",idrow="",cmd="";
				id=data_vals[0];
				idrow=this.genRowId(data_vals[0]);
				allrows+="<div class='collapsibleheader sub' style='margin-bottom:1px;' id='"+idrow+"' onclick=\"divToggleNext(this,"+fnCMD+"("+data_vals[0]+",'"+idrow+"sub'),0)\">";
				if(Array.isArray(arExtraButtons))
				{
					for(var k=0;k<arExtraButtons.length;k++)
					{
						allrows+="<input type='button' class='collapsibleheaderbutton' style='float:right' value='"+arExtraButtons[k].caption+"' onclick=\""+arExtraButtons[k].function+"("+data_vals[0]+");event.stopPropagation()\">";
					}
				}
				var label="";
				for(j=1;j<data_vals.length;j++)
				{
					if(this.data_struct[j].visible==1)
					{
						if(label!="")
						{	label+="<br>&nbsp;";
							label+="<span>"+this.data_struct[j].head+": "+data_vals[j]+"</span>";
						}
						else{
							label+="<b>"+data_vals[j]+"</b>";
						}
					}
					else
					{
						allrows+="<input type='hidden' id='"+this.genCellId(this.data_struct[j].name,data_vals[0])+"' value='"+data_vals[j]+"'>";
					}
				}
				allrows+=label;
				allrows+="</div>";
				allrows+="<div class='collapsiblecontent' id='"+idrow+"sub'></div>";
			}
			
			var rowheading="";
			rowheading+="<div class='divrow heading' id='idrowheading'>";
			for(i=1;i<this.data_struct.length;i++)
			{
				if(this.data_struct[i].visible>0)
				{
					var style="";if(this.data_struct[i].width!="")style="style='width:"+this.data_struct[i].width+"'";
					rowheading+="<div class='divcell' "+style+">"+this.data_struct[i].head+"</div>";
				}
			}
			rowheading+="</div>";

			var pageinfo="";
			if(pagecount>1)
			{
				pageinfo+="<div class='pageindex'>";
				for(i=1;i<=pagecount;i++)
				{
					if(i==pageno)
						pageinfo+="<a class=\"pageindexfocus\" href=\"javascript:void(0)\" onclick=\"javascript:showList("+i+")\">"+i+"</a>";
					else
						pageinfo+="<a class=\"pageindex\" href=\"javascript:void(0)\" onclick=\"javascript:showList("+i+")\">"+i+"</a>";
				}
				pageinfo+="</div>";
				
			}
			var idshow=document.getElementById(containeridname);
			idshow.innerHTML=allrows+pageinfo;
		}
		findRowheadingInTable(iddivtable)
		{
			if(iddivtable===undefined)return null;
			if(iddivtable==null)return null;
			var searchEles;
			if(isString(iddivtable))
			{
				searchEles = document.getElementById(iddivtable);
				if(searchEles===null)return null;
				searchEles=searchEles.children;
			}	
			else
				searchEles = iddivtable.children;
			
			if(searchEles===undefined)return;if(searchEles===null)return;if(searchEles.length<1)return;
			if(searchEles[0].classList=="divtable")
				searchEles=searchEles[0].children;
			if(searchEles===undefined)return;if(searchEles===null)return;if(searchEles.length<1)return;
			if(searchEles.length<1)return;
			for(var i = 0; i < searchEles.length; i++) {
				
				if(searchEles[i].classList=="divrow heading")
				{
					return searchEles[i];
				}
			}
		} 

		showGridUpdateCell(divnamecontainer,columnname,idvalincell,newvalue)
		{
			var idvalincell=this.genCellId(columnname,idvalincell);
			var searchEles=null;
			
			if(isString(divnamecontainer))
				searchEles = document.getElementById(divnamecontainer).children;
			else
				searchEles = divnamecontainer.children;
			if(searchEles===undefined)return;if(searchEles===null)return;if(searchEles.length<1)return;
			if(searchEles[0].classList=="divtable")
				searchEles=searchEles[0].children;
			if(searchEles===undefined)return;if(searchEles===null)return;if(searchEles.length<1)return;
			if(searchEles.length<1)return;
			for(var i = 0; i < searchEles.length; i++) {
				
				if(searchEles[i].classList=="divrow")
				{
					var cols=searchEles[i].children;
					if(cols===undefined)continue;if(cols===null)continue;if(cols.length<1)continue;
					for(var j=0;j<cols.length;j++)
					{
						if(cols[j].id.indexOf(columnname)==0) {
							if(cols[j].id==idvalincell)
							{
								cols[j].innerHTML=newvalue;
								break;
							}
						}		
					}
				}
			}

		}

		//cau truc table: divtable->divrow->cell (columname+idval)
		viewSetOneChecked(divnamecontainer,columnname,idvalchecked,checkvalue=1,clearothercheck=1)
		{
			var idcellchecked=this.genCellId(columnname,idvalchecked);
			var searchEles=null;
			
			if(isString(divnamecontainer))
				searchEles = document.getElementById(divnamecontainer).children;
			else
				searchEles = divnamecontainer.children;
			if(searchEles===undefined)return;if(searchEles===null)return;if(searchEles.length<1)return;
			if(searchEles[0].classList=="divtable")
				searchEles=searchEles[0].children;
			if(searchEles===undefined)return;if(searchEles===null)return;if(searchEles.length<1)return;
			if(searchEles.length<1)return;
			for(var i = 0; i < searchEles.length; i++) {
				
				if(searchEles[i].classList=="divrow")
				{
					var cols=searchEles[i].children;
					if(cols===undefined)continue;if(cols===null)continue;if(cols.length<1)continue;
					for(var j=0;j<cols.length;j++)
					{
						if(cols[j].id.indexOf(columnname)==0) {
							if(cols[j].id==idcellchecked)
							{
								if(checkvalue===1)
									cols[j].innerHTML="&#10003";
								else
									cols[j].innerHTML="";
							}
							else if(clearothercheck===1)
								cols[j].innerHTML="";
						}		
					}
				}
			}

		}
		showGridUpdateRow(data)
		{
			var data_vals=data.split(g_split_val);
			var iditem=document.getElementById(this.genRowId(data_vals[0]));
			var row=this.genRow(data_vals);
			iditem.outerHTML=row;			
		}
		showGridAddRow(data)
		{
			var data_vals=data.split(g_split_val);
			var row=this.genRow(data_vals);
			var idrowheading=document.getElementById("idrowheading");
			idrowheading.outerHTML=idrowheading.outerHTML+row;
		}
		//visible=0,1,2 
		showGrid(data,idcontent,fnCMD,fnCmdTitle)
		{
			if(fnCmdTitle===undefined)fnCmdTitle="CMD";
			var idshow=null;
			if(idcontent.constructor.name==="String")
				idshow=document.getElementById(idcontent);
			else
				idshow=idcontent;

		//ROW_HEADING
			var rowheading="";
				rowheading+="<div class='divrow heading' id='idrowheading'>";
				for(i=1;i<this.data_struct.length;i++)
				{
					if(this.data_struct[i].visible==1)
					{
						var style="";if(this.data_struct[i].width!="")style="style='width:"+this.data_struct[i].width+"'";
						rowheading+="<div class='divcell' "+style+">"+this.data_struct[i].head+"</div>";
					}
				}
				if(fnCMD!=null)
					rowheading+="<div class='divcell' style='width:10%'>CMD</div>";
			rowheading+="</div>";

			if(data.length<=g_split_row.length)
			{
				idshow.innerHTML="<div class='divtable'>"+rowheading+"</div>";
				
				return;
			}
			var rows=data.split(g_split_row);
			var row_count=rows.length;
			if(row_count<1)
			{
				idshow.innerHTML="<div class='divtable'>"+rowheading+"</div>";
				
				return;
			}
			var allrows="",j=0,i=0,pageno=0,pagecount=0,pagesize=20,startindex=0;
			if(fnCMD===undefined)
				fnCMD="itemMenuPopup";
			if(rows[0].substr(0,g_sign_pageconfig.length)==g_sign_pageconfig)
			{
				var configs=rows[0].split(g_split_val);	
				startindex=1;
				
				if(configs.length==3)
				{
					
					pagesize=parseInt(configs[0]);
					pageno=parseInt(configs[1]);
					pagecount=parseInt(configs[2]);
				}
			}
			for(i=startindex;i<row_count;i++)
			{
				var data_vals=rows[i].split(g_split_val);
				var id="",idrow="",cmd="";
				var idparam="";
				id=data_vals[0];
				idrow=this.genRowId(data_vals[0]);
				if(this.data_struct[0].type=="text")
					idparam="\""+data_vals[0]+"\"";
				else
					idparam=data_vals[0];
				if(idparam=="")idparam="0";
				if(fnCMD!=null)
					cmd="<input type='button' class='cmd' value='"+fnCmdTitle+"' onclick='"+fnCMD+"("+idparam+",0)'>";
				allrows+="<div class='divrow' id='"+idrow+"'>";
				for(j=1;j<data_vals.length;j++)
				{
					if(this.data_struct[j].visible==1)
					{
						var value="",style="";
						if(this.data_struct[j].type=="check")
						{
							if((data_vals[j]=="")||(data_vals[j]=="0"))
								value="";
							else
								value="&#10003;";
						}
						else
						{
							value=data_vals[j];
							if(this.data_struct[j].type=="number")
							{
								style="style='text-align:right;'";
								
							}
						}
						
						allrows+="<div class='divcell' "+style+" id='"+this.genCellId(this.data_struct[j].name,data_vals[0])+"'>"+value+"</div>";
					}
					else
					{
						value=data_vals[j];
						allrows+="<input type='hidden' id='"+this.genCellId(this.data_struct[j].name,data_vals[0])+"' value='"+value+"'>";
					}
				}
				if((fnCMD!=null)&&(data_vals.length>1))
					allrows+="<div class='divcell padding0'>"+cmd+"</div>";
				
				allrows+="</div>";
				//allrows+="<div id='"+idrow+"sub'></div>";
			}
			var pageinfo=DlgSelect.getPageIndexSelect(pageno,pagecount,null,"showList");
			/*
			if(pagecount>1)
			{
				pageinfo+="<div class='pageindex'>";
				for(i=1;i<=pagecount;i++)
				{
					if(i==pageno)
						pageinfo+="<a class=\"pageindexfocus\" href=\"javascript:void(0)\" onclick=\"javascript:showList("+i+")\">"+i+"</a>";
					else
						pageinfo+="<a class=\"pageindex\" href=\"javascript:void(0)\" onclick=\"javascript:showList("+i+")\">"+i+"</a>";
				}
				pageinfo+="</div>";
				
			}
			*/
			
			idshow.innerHTML="<div class='divtable'>"+rowheading+allrows+"</div>"+pageinfo;
			
		}
		showItemDetails(id)
		{
			//ajaxGetURL("?page=kb_khohang&action=details&store_id="+id,"details",ajaxOnReturnThis);
	
		}
	}
//END_CLASS_GRID_VIEW
/*INIT_CODE*/
if(typeof(g_menubar_items) =='object')
	gLoadUI();

window.addEventListener('resize', function(event) {
   
   gLayout();
}, true);