﻿// JScript File
 if(typeof MX == "undefined")
{
    MX = {};
    MX.ToArray = function(l,s,e){var a=[];s=s||0;e=e||l.length;if(s<=e)for(var i=s;i<e;++i)a.push(l[i]);return a;};
    MX.Delegate = function(){var Q=MX.ToArray(arguments,2);var I=arguments[0],bo=arguments[1];return function(){var aC=MX.ToArray(arguments);return bo.apply(I,Q.concat(aC))}};
}

AdsManager = function(json,link)
{        
    // this classID is stand for adobe
    this.embed = (document.all)?('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="#w#" height="#h#" ><param name="movie" value="$" /><param name="wmode" value="transparent" /><param name="autostart" value="1" /><param name="quality" value="high" /><param name="flashvars" value="MMplayerType=ActiveX" /></object>'):('<embed type="application/x-shockwave-flash" src="$" width="#w#" height="#h#" wmode="transparent" autostart="1" quality="high" flashvars="MMplayerType=PlugIn"/>');    
    this.Initialize.apply(this,arguments);
}
AdsManager.prototype = 
{
    Initialize : function(json,link)
    {           
        this.showInterval = MX.Delegate(this,this.Show);
        this.Items = [];
        for(var i=0,n=json.items.length;i<n;++i)
        {
            this.Items.push(json.items[i]);
        }
        this.current=-1;
        this.link = document.getElementById(link);
        this.link.target = "_blank";    
        this.arrImg = [];
        this.flag = [];              
        for(var i=0,n=this.Items.length;i<n;++i)
        {
            if(this.Items[i].type == "image")
            {
                this.arrImg.push(null);              
                this.flag.push(0);
            }
            else
            {
                this.arrImg.push("$");
                this.flag.push(-1);
            }                      
        }            
        this.player = document.createElement("div");   
        this.html = document.createElement("div");
        this.link.appendChild(this.player);
        this.link.appendChild(this.html);
        this.Show();
    }
    , CreateImage : function(item)
    {
        var img = document.createElement("img");
        img.src = item.src;
        img.style.cssText = "border:none; width:"+item.width+"px; height:"+item.height+"px;";
        this.link.appendChild(img);
        this.arrImg[this.current] = img;
    }
    , Show : function()
    {       
        this.Reset();
        this.current++;
        if(this.current>=this.Items.length) this.current = 0;
        
        var item = this.Items[this.current];        
        this.link.href = item.link;
        
        if(item.type == "image")
        {
            if(this.flag[this.current]==0)  
            {        
                this.CreateImage(item);
                this.flag[this.current]=1;
            }
            else if(this.flag[this.current]==1)  
            {
                this.arrImg[this.current].style.display = "block";
            }              
        }                
        else if(item.type == "flash")
        {
            this.player.style.display = "block";
            this.player.innerHTML = this.BuildPlayer(item.src,item.width,item.height);
        }
        else if(item.type == "html")
        {
            this.html.style.display = "block";
            this.html.innerHTML = item.src;
        }
        else
        {
            // unknow item type
        }
        window.setTimeout(this.showInterval,item.period);        
    }
    , Reset : function()
    {   
        if(this.flag[this.current]==1)
        {
            this.arrImg[this.current].style.display = "none"; 
        }
        this.player.style.display = "none";
        this.player.innerHTML = "";
        this.html.style.display = "none";    
        
    }
    , BuildPlayer : function(src,width,height)
    {
        var s = this.embed.replace("$",src).replace("#w#",width).replace("#h#",height);             
        return s;
    }
}

function showDialog(url, width, height)
{
    return showWindow(url, false, false, false, false, false, false, true, true, width, height, 0, 0);
}
function showDialog(url, width, height,isScrollbars)
{
    return showWindow(url, false, false, isScrollbars, false, false, isScrollbars, true, true, width, height, 0, 0);
}
function showWindow(url, isStatus, isResizeable, isScrollbars, isToolbar, isLocation, isFullscreen, isTitlebar, isCentered, width, height, top, left)
{
    if (isCentered)
    {
	    top = (screen.height - height) / 2;
	    left = (screen.width - width) / 2;
    }

    open(url, '_blank', 'status=' + (isStatus ? 'yes' : 'no') + ','
    + 'resizable=' + (isResizeable ? 'yes' : 'no') + ','
    + 'scrollbars=' + (isScrollbars ? 'yes' : 'no') + ','
    + 'toolbar=' + (isToolbar ? 'yes' : 'no') + ','
    + 'location=' + (isLocation ? 'yes' : 'no') + ','
    + 'fullscreen=' + (isFullscreen ? 'yes' : 'no') + ','
    + 'titlebar=' + (isTitlebar ? 'yes' : 'no') + ','
    + 'height=' + height + ',' + 'width=' + width + ','
    + 'top=' + top + ',' + 'left=' + left);
}

var showAdvanceSearch = false;
AdvanceSearch = function(d)
{
    if(showAdvanceSearch) 
    {
        d.className = "advance-search-button-down";
        Effect.BlindUp($("divAdvanceSearchArea"));
    }
    else 
    {
        d.className = "advance-search-button-up";
        Effect.BlindDown($("divAdvanceSearchArea"));
    }
    showAdvanceSearch = !showAdvanceSearch;
}
/*
document.getElementsByClassName = function(className, parent, tag)
{
	tag = tag||"*";
	parent = parent||document.body;
	var children = parent.getElementsByTagName(tag);
	var elements = [], child, pattern = new RegExp("(^|\\s)" + className + "(\\s|$)");
	for (var i = 0, length = children.length; i < length; i++)
	{
		child = children[i];
		var elementClassName = child.className;
		if (elementClassName.length == 0) continue;
		if (elementClassName == className || elementClassName.match(pattern)) elements.push(child);
  	}
  	return elements;
}
*/

function GetURLParam(name)
{
    name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    var regexS = "[\\?&]"+name+"=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    var txt = "";
    if(results != null)
        txt = results[1];
     return txt;
}

function addCommas(nStr)
{
    nStr += '';
    x = nStr.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
	    x1 = x1.replace(rgx, '$1' + '.' + '$2');
    }
    return x1 + x2;
}

// Method for cut left and right white space
String.prototype.trim = function()
{
    return this.replace(/^\s*|\s*$/g,"");
}

// Method add product to cookie $Temp
function AddProductToCart(id, name, price, promotion,type)
{
    var path = '/';
    var exp = new Date();
    exp.setDate(exp.getDate() + 1);
    
    var cookie = new CookieHelper("$Temp",[id,name,price,promotion,type].join("$"),path,exp);        
    cookie.Save();
}

BindDataPrice = function(lst, index, defaultText)
{
    var arrPriceValue = [500,1000,1500,2000,2500,3000,4000,5000,7000,10000];
    var arrPriceText = ["5 trăm nghìn","1 triệu","1.5 triệu","2 triệu","2.5 triệu","3 triệu","4 triệu","5 triệu","7 triệu","10 triệu"];
    if(typeof lst == 'undefined') return;
    
    RemoveAllOption(lst);
    
    for(var i=0;i<=arrPriceValue.length;i++)
    {
        var o = document.createElement("option");
        if(i==0)
        {
            o.text = defaultText;
            o.value = "-1";
            try { lst.add(o, null); } // standards compliant
            catch (ex) { lst.add(o); } // IE only
        }
        else if(i>=index)
        {
            o.text = arrPriceText[i-1];
            o.value = arrPriceValue[i-1]*1000;
            try { lst.add(o, null); } // standards compliant
            catch (ex) { lst.add(o); } // IE only
        }
    }
}
RemoveAllOption = function(lst)
{
    if(typeof lst == 'undefined') return;
    
    while(lst.options.length>0) lst.remove(0);    
}
OptionSelected = function(lst, value)
{
    if(typeof lst == 'undefiend') return;
    
    for(var i=0;i<lst.options.length;i++)
    {
        if(lst.options[i].value == value)
        {
            lst.options[i].selected = true;
            break;
        }
    }
}

WaveText = Class.create()
WaveText.prototype = 
{
    initialize : function(element,value,defaultColor, activeColor)
    {
        this.element = $(element);
        this.value = value||"input";
        this.defaultColor = defaultColor||"silver";
        this.activeColor = activeColor||"black";
        this.element.onfocus = this.OnClick.bind(this);
        this.element.onblur = this.OnBlur.bind(this);
        
        this.element.value = this.value;
        this.element.style.color = this.defaultColor;
    },
    OnClick : function()
    {
        if($F(this.element).trim() == this.value)
        {
            this.element.value = "";
            this.element.style.color = this.activeColor;
        }
        this.element.select();        
    },
    OnBlur : function()
    {
        if($F(this.element).trim() == "")
        {
            this.element.value = this.value;
            this.element.style.color = this.defaultColor;
        }            
    }
}
WaveText.Register = function(element,value, defaultColor, activeColor)
{
    return new WaveText(element,value, defaultColor, activeColor);
}

// Tab
//TabStyle = Class.create();
//TabStyle.prototype =
//{
//    initialize : function()
//    {
//        var arr = $$("a.tab-style");
//        this.selectedIndex = null;
//        for(var i=0;i<arr.length;i++)
//        {
//            Event.observe(arr[i],"click",this.Active.bind(this,i));
//        }
//        this.arr = arr;
//        this.tabLeft = $("TabStyleLeft");
//        this.arrStyle = ["tab-orange","tab-blue","tab-silver","tab-gray","tab-pink"];
//    },
//    Active : function(index)
//    {
//        if(index==this.selectedIndex) return;
//        this.tabLeft.className = "tab-style-left " + (index==0?"tab-style-left-active":"tab-style-left-normal");
//        this.arr[index].className = "tab-style " + this.arrStyle[index];
//        
//        // last link is pink color (Danh cho phu nu)
//        if(index==(this.arr.length-1)) this.arr[index].style.color="";
//        else this.arr[this.arr.length-1].style.color="#fc00ff";
//        
//        if(this.selectedIndex!=null) this.arr[this.selectedIndex].className = "tab-style tab-style-default";	        
//        this.selectedIndex = index;
//    }
//}

TabStyle = Class.create();
TabStyle.prototype =
{
    initialize : function(tabWrapper, tabLeft, isStyle)
    {
        this.isStyle = isStyle||false;
        var arr = $$("#"+tabWrapper+" a.tab-style");
        this.selectedIndex = null;
        for(var i=0;i<arr.length;i++)
        {
            Event.observe(arr[i],"click",this.Active.bind(this,i));
        }
        this.arr = arr;
        this.tabLeft = $(tabLeft);
        this.arrStyle = ["tab-orange","tab-blue","tab-silver","tab-gray","tab-pink"];
    },
    Active : function(index)
    {
        if(index==this.selectedIndex) return;
        this.tabLeft.className = "tab-style-left " + (index==0?"tab-style-left-active":"tab-style-left-normal");
        if(this.isStyle)
        {
            this.arr[index].className = "tab-style " + this.arrStyle[index];
            // last link is pink color (Danh cho phu nu)
            if(index==(this.arr.length-1)) this.arr[index].style.color="";
            else this.arr[this.arr.length-1].style.color="#fc00ff";
        }
        else
            this.arr[index].className = "tab-style " + this.arrStyle[0];
        
        if(this.selectedIndex!=null) this.arr[this.selectedIndex].className = "tab-style tab-style-default";	        
        this.selectedIndex = index;
    }
}

// stop event
StopEvent = function(e)
{
    if ( e.stopPropagation ) e.stopPropagation();
    else if ( e.cancelBubble ) e.cancelBubble = true;
    if ( e.preventDefault ) e.preventDefault();
    else e.returnValue = false;
}