﻿var bolStatusLoad=0;
function StartLoadData(intCount,statesList)
{
if(bolStatusLoad==1)
	return;
bolStatusLoad=1;
var strCondition;
var response;
statesList.disabled=true;  	
var objItem,objSubItem;
	switch(intCount)
	{		
		case "1":
		strItemValue = statesList.value;
		strCondition ="[1]" + strItemValue + "[2]";
		objItem = document.getElementById("idAutoPrice_drpCatePage");
		objSubItem = document.getElementById("spCatePage");
		break;
		case "2":
		strCondition ="[1]" + strItemValue + "[2]" + document.getElementById("idAutoPrice_drpCatePage").value + "[3]";
		objItem = document.getElementById("idAutoPrice_drpPageSize");
		objSubItem = document.getElementById("spPageSize");
		break;
		case "3":
		strCondition ="[1]" + strItemValue + "[2]" + document.getElementById("idAutoPrice_drpCatePage").value + "[3]" + document.getElementById("idAutoPrice_drpPageSize").value + "[4]";
		objItem = document.getElementById("idAutoPrice_drpCan");
		objSubItem = document.getElementById("spCan");
		break;
		case "4":
		strCondition ="[1]" + strItemValue + "[2]" + document.getElementById("idAutoPrice_drpCatePage").value + "[3]" + document.getElementById("idAutoPrice_drpPageSize").value + "[4]" + document.getElementById("idAutoPrice_drpCan").value + "[5]";
		objItem = document.getElementById("idAutoPrice_drpMang");		
		objSubItem = document.getElementById("spMang");
		break;
		case "5":
		strCondition ="[1]" + strItemValue + "[2]" + document.getElementById("idAutoPrice_drpCatePage").value + "[3]" + document.getElementById("idAutoPrice_drpPageSize").value + "[4]" + document.getElementById("idAutoPrice_drpCan").value + "[5]" + document.getElementById("idAutoPrice_drpMang").value + "[6]";
		objItem = document.getElementById("idAutoPrice_drpColor");
		objSubItem = document.getElementById("spColor");
		break;
		case "6":
		strCondition ="[1]" + strItemValue + "[2]" + document.getElementById("idAutoPrice_drpCatePage").value + "[3]" + document.getElementById("idAutoPrice_drpPageSize").value + "[4]" + document.getElementById("idAutoPrice_drpCan").value + "[5]" + document.getElementById("idAutoPrice_drpMang").value + "[6]" + document.getElementById("idAutoPrice_drpColor").value + "[7]";
		objItem = document.getElementById("idAutoPrice_drpNumber");
		objSubItem = document.getElementById("spSoluong");
		break;
		case "7":
		if(document.getElementById("idAutoPrice_drpNumber").value==""){
			document.getElementById("idAutoPrice_drpNumber").disabled=false;
			bolStatusLoad = 0;
			document.getElementById("idDonGiaSanPham").innerHTML="0 vnđ";
			document.getElementById("idAutoPrice_idThanhTien").innerHTML="0 vnđ";
			document.getElementById("idAutoPrice_colTimeRequest").innerHTML="Thời gian giao hàng";				
			document.getElementById("idAutoPrice_idDonGia").innerHTML="";
			return;
			}	
		strCondition ="[1]" + strItemValue + "[2]" + document.getElementById("idAutoPrice_drpCatePage").value + "[3]" + document.getElementById("idAutoPrice_drpPageSize").value + "[4]" + document.getElementById("idAutoPrice_drpCan").value + "[5]" + document.getElementById("idAutoPrice_drpMang").value + "[6]" + document.getElementById("idAutoPrice_drpColor").value + "[7]" + document.getElementById("idAutoPrice_drpNumber").value + "[8]";		
		response = autoprice.GetPriceProduct(strCondition);		
		document.getElementById("idAutoPrice_idDonGia").innerHTML=response.value.substring(0,response.value.indexOf("[*]"));				
		var intPrices,intCount;
			intPrices = parseFloat(response.value.substring(response.value.indexOf("[*]")+3,response.value.indexOf("[**]")).replace(".","").replace(".","").replace(".","").replace(".","").replace(".",""));
			intCount = parseFloat(document.getElementById("idAutoPrice_drpNumber").options[document.getElementById("idAutoPrice_drpNumber").selectedIndex].text.replace(".","").replace(".","").replace(".","").replace(".","").replace(".",""));
		document.getElementById("idDonGiaSanPham").innerHTML=formatNumber("" + Math.floor(intPrices/intCount)) + " vnđ";
		document.getElementById("idAutoPrice_idThanhTien").innerHTML=response.value.substring(response.value.indexOf("[*]")+3,response.value.indexOf("[**]"));
		document.getElementById("idAutoPrice_colTimeRequest").innerHTML="Thời gian giao hàng từ " + response.value.substring(response.value.indexOf("[**]")+4);		
		//if()
		document.getElementById("idAutoPrice_hidMaSanPham").value=document.getElementById("idAutoPrice_idDonGia").innerHTML;	
		//document.getElementById("idAutoPrice_bntMuaHang").style.visibility="visible";	
		statesList.disabled=false;
		bolStatusLoad = 0;
		return;
	break;
	}	
	response = autoprice.CreateDataSource(intCount,strCondition);			
	objItem.options.length = 0; 	
	if(response.value != null && typeof(response.value) == "object" && response.value.Tables != null)
	{ 	
	 for (var i = 0; i < response.value.Tables[0].Rows.length;++i)
	 {	 
		objItem.options[objItem.options.length] = new Option(response.value.Tables[0].Rows[i].TEXT,response.value.Tables[0].Rows[i].VALUE);     
		if(i%2!=0)
			objItem.options[i].style.backgroundColor="beige";
	 }		
	}
		
		bolStatusLoad =0;		
		if (objItem.selectedIndex > -1){		
			if (objItem.fireEvent)
			{	
				objItem.disabled=false;			
				objItem.fireEvent("onchange");
				if(objItem.options.length<2)
					{									  	
						objItem.style.display="none";						
						objSubItem.style.display="inline";
						objSubItem.innerHTML=objItem.options[objItem.selectedIndex].text;
					}
				else
				{
					objItem.style.display="inline";
					objSubItem.style.display="none";//innerHTML=objItem.options[objItem.selectedIndex].text;
				}
			}
			else if(objItem.dispatchEvent)
			{				
				var oEvent = document.createEvent("HTMLEvents"); 
				oEvent.initEvent("change", true, true);
				objItem.dispatchEvent(oEvent);
			}			
		}	
		statesList.disabled=false;			
		//alert(document.getElementById("idAutoPrice_hidMaSanPham").value);
}
function ResetObjItem()
{	
	if(document.getElementById("idAutoPrice_drpCatePage")==null)
		return;
	if(document.getElementById("idAutoPrice_drpCatePage").length<2)
		{
			document.getElementById("idAutoPrice_drpCatePage").style.display="none";
			document.getElementById("spCatePage").innerHTML=document.getElementById("idAutoPrice_drpCatePage").options[document.getElementById("idAutoPrice_drpCatePage").selectedIndex].text;
		}
	if(document.getElementById("idAutoPrice_drpPageSize").length<2)
		{
			document.getElementById("idAutoPrice_drpPageSize").style.display="none";
			document.getElementById("spPageSize").innerHTML=document.getElementById("idAutoPrice_drpPageSize").options[document.getElementById("idAutoPrice_drpPageSize").selectedIndex].text;
		}
	if(document.getElementById("idAutoPrice_drpCan").length<2)
	{
		document.getElementById("idAutoPrice_drpCan").style.display="none";
		document.getElementById("spCan").innerHTML=document.getElementById("idAutoPrice_drpCan").options[document.getElementById("idAutoPrice_drpCan").selectedIndex].text;
	}
	if(document.getElementById("idAutoPrice_drpMang").length<2)
	{
		document.getElementById("idAutoPrice_drpMang").style.display="none";
		document.getElementById("spMang").innerHTML=document.getElementById("idAutoPrice_drpMang").options[document.getElementById("idAutoPrice_drpMang").selectedIndex].text;
	}
	if(document.getElementById("idAutoPrice_drpColor").length<2)
	{
		document.getElementById("idAutoPrice_drpColor").style.display="none";
		document.getElementById("spColor").innerHTML=document.getElementById("idAutoPrice_drpColor").options[document.getElementById("idAutoPrice_drpColor").selectedIndex].text;
	}
	if(document.getElementById("idAutoPrice_drpNumber").length<2)
	{
		document.getElementById("idAutoPrice_drpNumber").style.display="none";
		document.getElementById("spSoluong").innerHTML=document.getElementById("idAutoPrice_drpNumber").options[document.getElementById("idAutoPrice_drpNumber").selectedIndex].text;
	}
}
function formatNumber(strNumber)
{
	var inttemp1,inttemp2="";
	var intstop = parseInt((strNumber.length-1)/3);
	inttemp1= strNumber;	
	for(i=0;i<intstop;i++)
	{	
		inttemp2="." + inttemp1.substring(inttemp1.length - 3) + inttemp2;
		inttemp1=inttemp1.substring(0,inttemp1.length - 3);		
	}	
	return inttemp1 + inttemp2;
}