﻿// JScript File
// Đối tượng Laptop để sử dụng ajax
function AjaxLaptopObject(PriceClassMin, PriceClassMax, IDIndex, IDBranch, DivName,IntHot)
{
    this.PriceClassMin  = PriceClassMin;
    this.PriceClassMax  = PriceClassMax;
    this.IDIndex        = IDIndex;    
    this.IDBranch       = IDBranch;
    this.DivName        = DivName;
    this.IntHot         = IntHot;
}

function GetAjaxData(dataSource, divID) 
{ 
    var XMLHttpRequestObject = false; 
    if (window.XMLHttpRequest)
    {
        XMLHttpRequestObject = new XMLHttpRequest();
    }
    else if (window.ActiveXObject)
    {
        //IE
        try
        {
            XMLHttpRequestObject = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e)
        {
            try
            {
                XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e)
            {}
        }
    }
    if(XMLHttpRequestObject)
    {
        var obj = document.getElementById(divID); 
        XMLHttpRequestObject.open("GET", dataSource); 
        XMLHttpRequestObject.onreadystatechange = function() 
        { 
            if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200)
            {
                obj.innerHTML = XMLHttpRequestObject.responseText; 
            } 
        } 
        XMLHttpRequestObject.send(null);
    }
}


function DisplayLaptopList(obj)
{
    var postStr = "PriceClassMin=" + encodeURI(obj.PriceClassMin) + "&PriceClassMax=" + encodeURI(obj.PriceClassMax) + "&IDIndex=" + encodeURI(obj.IDIndex) + "&IDBranch=" + encodeURI(obj.IDBranch)+ "&IntHot=" + encodeURI(obj.IntHot);    
    GetAjaxData(strAjaxServer + "?" + postStr, obj.DivName);
}


function ShowLaptopList()
{
    if (bolIsLoadLaptop)
    {
        for (var i = 0; i < intLaptopListTypeCount; i++)
        {
            DisplayLaptopList(arrLaptopListType[i]);
        }
    }
}
function ShowProductInfo(IDIndex, IDBranch, IDProduct, Column)
{
    var XMLHttpRequestObject = false; 
    if (window.XMLHttpRequest)
    {
        XMLHttpRequestObject = new XMLHttpRequest();
    }
    else if (window.ActiveXObject)
    {
        //IE
        try
        {
            XMLHttpRequestObject = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e)
        {
            try
            {
                XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e)
            {}
        }
    }
    if(XMLHttpRequestObject)
    {
        var compareDataSource = strCompareAjaxServer + "?IDIndex=" + IDIndex + "&IDBranch=" + IDBranch + "&IDProduct=" + IDProduct;
        XMLHttpRequestObject.open("GET", compareDataSource); 
        XMLHttpRequestObject.onreadystatechange = function() 
        { 
            if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200)
            {
                RefreshProductInfo(IDProduct, Column, XMLHttpRequestObject.responseText);
            } 
        } 
        XMLHttpRequestObject.send(null);
    }
}
function ShowPromotion(productid)
{
}
function ShowStandardKit(productid)
{
}
function ChangePrice(newPrice,PreOrderPrice)
{
    try
    {
        document.getElementById("hdfProductPrice").value = newPrice;
        if(document.getElementById("hdfPreOrderPrice")!=null)
            document.getElementById("hdfPreOrderPrice").value = PreOrderPrice;
    }
    catch(e)
    {}
}
function ChangeCondition(newCondition, newProductCode, newPrice, IsExist)
{
    try
    {
        document.getElementById("hdfProductCondition").value = newCondition;
        document.getElementById("hdfProductCode").value = newProductCode;        
        
        if(parseFloat(newPrice) > 0)
            document.getElementById("dvStockProduct").style.display = "block";
        else
            document.getElementById("dvStockProduct").style.display = "none";
    }
    catch(e)
    {
        document.getElementById("dvStockProduct").style.display = "none";
    }
}
function RefreshProductInfo(intProduct, intColumn, strResult)
{
	//Xử lý chuỗi
	var strNewResult = ""; //Biến gia tri cuoi cung moi
	var strDestination = ""; //Biến lấy khung về
	strDestination = document.getElementById("spInforProduct").innerHTML;
	var strTextNewProduct = strResult;
	var agt = navigator.userAgent.toLowerCase(); 
	var appName = navigator.appName.toLowerCase();
	if(appName.indexOf('microsoft')!=-1)
	{
		if(agt.indexOf('opera')!=-1)
		{
		 strDestination = strDestination.replace(/"/gi,"");
		 strTextNewProduct = strTextNewProduct.replace(/'/gi,"");
		}
		else
		{
            strDestination = strDestination.replace(/"/gi,"");
            strTextNewProduct = strTextNewProduct.replace(/'/gi,"");
        }
	}
	else
	{
		if(agt.indexOf('firefox')!=-1)
		{
		    strDestination = strDestination.replace(/"/gi,"");
			strTextNewProduct = strTextNewProduct.replace(/'/gi,"");
		}
		if(agt.indexOf('netscape')!=-1)
		{
			strDestination = strDestination.replace(/"/gi,"");
			strTextNewProduct = strTextNewProduct.replace(/'/gi,"");
		}
	}
	
	var pub_intLastDesIndex = 0;
	var pub_intLastSouIndex = 0;
	while(pub_intLastDesIndex >=0 && pub_intLastDesIndex < strDestination.length)
	{	
		var strElementFirstDes = "AtrGrpValueDetail" + intColumn + ">";
		var objDestinationElement = GetElementValue(strDestination, strElementFirstDes, "</TD>", pub_intLastDesIndex);
		var objSourceElement	  = GetElementValue(strTextNewProduct, "class=AtrGrpValueDetail>", "</td>", pub_intLastSouIndex);
		
		if(objDestinationElement.intFirstIndex < 0 || objSourceElement.intFirstIndex < 0)
			break;
		pub_intLastDesIndex = objDestinationElement.intLastIndex;			
		pub_intLastSouIndex = objSourceElement.intLastIndex;
		strDestination = strDestination.substring(0, objDestinationElement.intFirstIndex + strElementFirstDes.length) + objSourceElement.strValue 
						+ strDestination.substring(objDestinationElement.intLastIndex + 1, strDestination.length);
	}
	
	document.getElementById("spInforProduct").innerHTML = strDestination;
}

function ElementClass(strElem, strValu, intFirstInd, intLastInd)
{
	this.strElement = strElem;
	this.strValue   = strValu;
	this.intFirstIndex = intFirstInd;
	this.intLastIndex  = intLastInd;
}

function GetElementValue(strSource, strFirstElement, strLastElement, intIndex)
{
	//Start replace
	var agt = navigator.userAgent.toLowerCase(); 
	var appName = navigator.appName.toLowerCase();
	
	if(appName.indexOf('microsoft')!=-1)
	{
		if(agt.indexOf('opera')!=-1)
		{
			strSource = strSource.replace(/'/gi,"");
		}
		else
		{
			strSource = strSource.replace(/'/gi,"");	
		}
	}
	else
	{
		if(agt.indexOf('firefox')!=-1)
		{
			strSource = strSource.replace(/"/gi,"");
			strSource = strSource.replace(/'/gi,"");
			strLastElement = strLastElement.toLowerCase();
		}
		if(agt.indexOf('netscape')!=-1)
		{
			strSource = strSource.replace(/"/gi,"");
			strSource = strSource.replace(/'/gi,"");
			strLastElement = strLastElement.toLowerCase();	
		}
	}
	//End replace
	
	var strResult = "";
	var intFirstIndex = strSource.indexOf(strFirstElement, intIndex);
	var intLastIndex  = strSource.indexOf(strLastElement, intFirstIndex + strFirstElement.length);
	if(intLastIndex < 0)
		intLastIndex = strSource.indexOf(strLastElement.toLowerCase(), intFirstIndex + strFirstElement.length);
	intLastIndex --;	
	
	strResult = strSource.substring(intFirstIndex + strFirstElement.length, intLastIndex + 1);
	if(intFirstIndex + strFirstElement.length >= intLastIndex)
		strResult = "";
	
	return new ElementClass(strFirstElement, strResult, intFirstIndex, intLastIndex);
}

//Ẩn/hiển thị layer trên control Branch
function ShowHideBranch(layer_ref) 
{
    var state = document.getElementById(layer_ref).style.display;

    if (state == 'block') 
    {
        state = 'none';
    }
    else
    {
        state = 'block';
    }
    if (document.all) 
    {                   //IS IE 4 or 5 (or 6 beta)
        eval( "document.all." + layer_ref + ".style.display = state");
    }
    if (document.getElementById &&!document.all) 
    {
        hza = document.getElementById(layer_ref);
        hza.style.display = state;
    }
}

//đổi hình mũi tên trái sang mũi tên down
function ChangeImage(layer_ref)
{
    var img1 = strGlobalRoot + "/Images/SiteImages/Branch/IconArrowLeft.gif";
    var img2 = strGlobalRoot + "/Images/SiteImages/Branch/IconArrowUp.gif";
    var img = document.getElementById(layer_ref).src;
    
    if (img.indexOf("IconArrowLeft.gif") >= 0)
        document.getElementById(layer_ref).src = img2;
    else
        document.getElementById(layer_ref).src = img1;
}

//Đổi màu sản phẩm và giá
function ChangeProductColor(image, priceVAT, price,preorder,pricedown)
{
    try
    {
        document.getElementById("PhoneDetailImage").src =  image;
    }
    catch (e)
    {}
    try
    {       
        document.getElementById("PhoneDetailPrice").innerHTML = price + " VND";        
        if(document.getElementById("divPreOrderPrice")!=null)
        {
            if(preorder!="" && preorder != "0,000")
            {
                document.getElementById("divOrder").style.display = "none";
                document.getElementById("divPreOrderPrice").innerHTML = "Giá đặt trước: " + preorder + " VND";                                
                document.getElementById("hdfPreOrderPrice").value = preorder;                
                if(pricedown != "0")
                {
                  document.getElementById("hdValuePreOrder").value = pricedown;
                  document.getElementById("divPreOrder_Detail").style.display = "";
                  document.getElementById("spValuePre").innerHTML = "Đặt hàng online giá rẻ hơn: " + pricedown + " VND";
                  document.getElementById("spPopupPriceDown").innerHTML = pricedown + " đ";
                }
                else
                {
                   document.getElementById("hdValuePreOrder").value = "0";
                   document.getElementById("divPreOrder_Detail").style.display = "none";
                }                
            }
            else
            {                
                document.getElementById("divOrder").style.display = "none";
                document.getElementById("divPreOrderPrice").innerHTML ="";
                document.getElementById("hdfPreOrderPrice").value = "0";
                document.getElementById("hdValuePreOrder").value = "0";
                document.getElementById("divPreOrder_Detail").style.display = "none";
            }
        }       
    }
    catch (e)
    {
       document.getElementById("divPreOrder_Detail").style.display = "none";
    }
    try
    {        
        if(priceVAT != "Giá đã bao gồm VAT" && priceVAT !="0")
            document.getElementById("PhoneDetailPriceVAT").innerHTML = priceVAT;
        else
            document.getElementById("PhoneDetailPriceVAT").innerHTML = "Giá đã bao gồm VAT";        
    }
    catch (e)
    {}
}
function ChangeLink(strRoot,productid)
{    
    var strLink = strRoot + "/Laptop/OnlineRegister.aspx?CompanyID=2&ProductID=" + productid;    
    try
    {
       productcode = document.getElementById("hdfProductCode").value;
       strLink += "&ProductCode=" + productcode;
       window.location=strLink;
    }
    catch(e)
    {   
    }
}
/*
 *	Code for 4 tab general
 */
function ActiveTabGeneral(tab)
{
    DeactiveTabGeneral('tab1');
    DeactiveTabGeneral('tab2');
    DeactiveTabGeneral('tab3');
    DeactiveTabGeneral('tab4');
    document.getElementById(tab).style.display = 'block';
    document.getElementById(tab + '_Left').className = 'Block24hActiveLeft';
    document.getElementById(tab + '_Center').className = 'Block24hActiveBg';
    document.getElementById(tab + '_Right').className = 'Block24hActiveRight';
}
function DeactiveTabGeneral(tab)
{
    document.getElementById(tab).style.display = 'none';
    document.getElementById(tab + '_Left').className = 'Block24hNormalLeft';
    document.getElementById(tab + '_Center').className = 'Block24hNormalBg';
    document.getElementById(tab + '_Right').className = 'Block24hNormalRight';
}
function ActiveTabTwo(tab)
{
    DeactiveTabTwo('tab1');
    DeactiveTabTwo('tab2');   
    document.getElementById(tab).style.display = 'block';
    document.getElementById(tab + '_Left').className = 'Block24hActiveLeft';
    document.getElementById(tab + '_Center').className = 'Block24hActiveBg';
    document.getElementById(tab + '_Right').className = 'Block24hActiveRight';
}
function DeactiveTabTwo(tab)
{
    document.getElementById(tab).style.display = 'none';
    document.getElementById(tab + '_Left').className = 'Block24hNormalLeft';
    document.getElementById(tab + '_Center').className = 'Block24hNormalBg';
    document.getElementById(tab + '_Right').className = 'Block24hNormalRight';
}

// Đối tượng Ajax trang trang trí
function AjaxAdornObject(IDIndex, IDGroup, DivName,IDBranch)
{    
    this.IDIndex        = IDIndex;    
    this.IDGroup        = IDGroup;
    this.DivName        = DivName;
    this.IDBranch       = IDBranch;
}

function ShowAdornList()
{
    if (bolIsLoadAdorn)
    {
        for (var i = 0; i < intAdornIndex; i++)
        {
            DisplayAdornList(arrAdornList[i]);
        }
    }
}

function DisplayAdornList(obj)
{
    var postStr = "IDIndex=" + encodeURI(obj.IDIndex) + "&GroupID=" + encodeURI(obj.IDGroup)+ "&IDBranch=" + encodeURI(obj.IDBranch);
    GetAjaxData(strAjaxAdorn + "?" + postStr, obj.DivName);
}

function DisplayAdornListAll(obj,ThemeID)
{
    var postStr = "IDIndex=" + encodeURI(obj.IDIndex) + "&GroupID=" + encodeURI(obj.IDGroup) + "&RowDisplay=0&ThemeID=" + ThemeID;    
    GetAjaxData(strAjaxAdorn + "?" + postStr, obj.DivName);
}

function DisplayAdornListByPrice(obj,intPrice90,intPrice110,IDIndex,intIDProduct,BranchID)
{
    var postStr = "IDIndex=" + encodeURI(obj.IDIndex) + "&GroupID=" + encodeURI(obj.IDGroup) + "&RowDisplay=0&PriceFro=" + intPrice90 + "&PriceTo=" + intPrice110 + "&IDProduct=" + intIDProduct + "&IDBrach=" + BranchID;     
    GetAjaxData(strAjaxAdorn + "?" + postStr, obj.DivName);
}
function FormatNumber(fnum)
{
    var orgfnum = fnum;
    var flagneg = false;
    
    if(fnum.charAt(0) == "-")
    {
        flagneg = true;
        fnum = fnum.substr(1, fnum.length-1);
    }
    
    psplit = fnum.split(".");

    var cnum = psplit[0],
        parr = [],
        j = cnum.length,
        m = Math.floor(j / 3),
        n = cnum.length % 3 || 3;

    // break the number into chunks of 3 digits; first chunk may be less than 3
    for (var i = 0; i < j; i += n) {
        if (i != 0) {n = 3;}
        parr[parr.length] = cnum.substr(i, n);
        m -= 1;
    }

    // put chunks back together, separated by comma
    fnum = parr.join(",");

    // add the precision back in
    //if (psplit[1]) {fnum += "." + psplit[1];}
    if (orgfnum.indexOf(".") != -1)  
    {
        fnum += "." + psplit[1];
    }
    
    if(flagneg == true)
    {
        fnum = "-" + fnum;
    }
    
    return fnum;
}