﻿// require prototype

$GUID=function(){return "{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}".replace(/x/g,function(){return(Math.random()*16|0).toString(16);}).toUpperCase();};
$GUID.New=function(type){return(type||"guid")+"_"+$GUID().replace(/-/g,"_").replace(/^\{|\}$/g,"");}
$C = function(tag){return document.createElement(tag)};

// Namespace
MobileShop = {};

// Mobile Price
MobileShop.FromPrices = [{id:-1,name:"Giá từ"},{id:499,name:"< 500 nghìn"},{id:500,name:"500 nghìn"},{id:1000,name:"1 triệu"},{id:1500,name:"1 triệu rưỡi"},{id:2000,name:"2 triệu"},{id:2500,name:"2 triệu rưỡi"},{id:3000,name:"3 triệu"},{id:4000,name:"4 triệu"},{id:5000,name:"5 triệu"},{id:7000,name:"7 triệu"},{id:10001,name:"> 10 triệu"}]
MobileShop.ToPrices = [{id:-1,name:"Giá đến"},{id:500,name:"500 nghìn"},{id:1000,name:"1 triệu"},{id:1500,name:"1 triệu rưỡi"},{id:2000,name:"2 triệu"},{id:2500,name:"2 triệu rưỡi"},{id:3000,name:"3 triệu"},{id:4000,name:"4 triệu"},{id:5000,name:"5 triệu"},{id:7000,name:"7 triệu"},{id:10000,name:"10 triệu"}]
// Laptop Price
MobileShop.LFromPrices = [{id:-1,name:"Giá từ"},{id:4999,name:"< 5 triệu"},{id:5000,name:"5 triệu"},{id:6000,name:"6 triệu"},{id:7000,name:"7 triệu"},{id:8000,name:"8 triệu"},{id:9000,name:"9 triệu"},{id:10000,name:"10 triệu"},{id:11000,name:"11 triệu"},{id:12000,name:"12 triệu"},{id:13000,name:"13 triệu"},{id:14000,name:"14 triệu"},{id:15000,name:"15 triệu"},{id:16000,name:"16 triệu"},{id:17000,name:"17 triệu"},{id:18000,name:"18 triệu"},{id:19000,name:"19 triệu"},{id:20000,name:"20 triệu"},{id:20001,name:"> 20 triệu"}]
MobileShop.LToPrices = [{id:-1,name:"Giá đến"},{id:5000,name:"5 triệu"},{id:6000,name:"6 triệu"},{id:7000,name:"7 triệu"},{id:8000,name:"8 triệu"},{id:9000,name:"9 triệu"},{id:10000,name:"10 triệu"},{id:11000,name:"11 triệu"},{id:12000,name:"12 triệu"},{id:13000,name:"13 triệu"},{id:14000,name:"14 triệu"},{id:15000,name:"15 triệu"},{id:16000,name:"16 triệu"},{id:17000,name:"17 triệu"},{id:18000,name:"18 triệu"},{id:19000,name:"19 triệu"},{id:20000,name:"20 triệu"}]

MobileShop.Path = "http://vio.com.vn/Home/";
MobileShop.Service =
{
    type     : "MobileShop$Service",
    Url      : MobileShop.Path+"Handler.ashx",
    RegdList : {},
	Register : function(iService,sender)
	{
		if(this.RegdList[sender.regKey]!=null)
			this.RegdList[sender.regKey].list[iService.index] = iService;
		else this.RegdList[sender.regKey] = {"sender":sender,"list":[iService]};

		sender.Request = function(index)
		{
			MobileShop.Service.Request(index,this.regKey);
		};
	},
    Request  : function(index,regKey)
	{
		var o = this.RegdList[regKey];
		if(o!=null)
		{
			var iService = o.list[index];
			//if(iService.index==index)		check index
			//var params = MX.Class.Clone(o.sender.GetParameters(index));	if we want to save the parameters of o.sender
			var params = o.sender.GetParameters(index);
			params.regKey = regKey;
			params.index = index;

			iService.jhr = new Ajax.Request(iService.url,
            {
                parameters : params,
                onSuccess  : MobileShop.Service.Callback
            });
		}
	},
	Callback : function(r,json,p)
	{
	    var iService = MobileShop.Service.RegdList[p.regKey].list[p.index];
		iService.callback.call(MobileShop.Service.RegdList[p.regKey].sender,p,json,r.responseText);
//	    if(json!=null)
//	    {
//		    var iService = MobileShop.Service.RegdList[p.regKey].list[p.index];
//		    iService.callback.call(MobileShop.Service.RegdList[p.regKey].sender,p,json,text);
//        }
	}
};
MobileShop.IService =
{
	type : "MobileShop$IService",
	Implement : function(o,index,url)
	{
		if(o.Implementation[this.type])
		{
			var iService =
			{
				"index"    : index||0,
				"url"      : url||MobileShop.Service.Url,
				"callback" : o.Callback||Prototype.emptyFunction
			};
			MobileShop.Service.Register(iService,o);
		}
	}
}
MobileShop.Request = function(params,callback,sender)
{
    callback = callback||Prototype.emptyFunction;
    sender = sender||this;
    new Ajax.Request(MobileShop.Service.Url,
    {
        parameters : params,
        onSuccess  : callback.bind(sender)
    });
}
/* --------------------- End of Services define --------------------------- */
MobileShop.Cache = {"Model":{},"Provider":{},"Prefecture":{},"ProductName":{}};

MobileShop.Search = Class.create();
MobileShop.Search.prototype =
{
    // ptype: ProductTypeID
    initialize : function(text,list,ptype)
    {
        this.Implementation = {};
		this.Implementation[MobileShop.IService.type] = true;
		this.regKey = $GUID.New("key");
		this.parameters = [{"action":105,"productTypeID":ptype}];
		MobileShop.IService.Implement(this,0);	// url for local testing
		
        this.ptype = ptype;
        this.tryCount = 0;
        
        this.text = $(text);
        this.list = $(list);
        // default value
        this.text.show();
        this.list.hide();
        
        // handler
        Event.observe(this.text,"keyup",this.OnKeyUp);
        //var keywordLoaderArray = ["Mobell M220", "BlackBerry 8700G", "BlackBerry 8700G (Cam kết Dịch vụ)", "BlackBerry Bold 9000", "BlackBerry Bold 9000 (Cam kết Dịch vụ)", "BlackBerry Curve 8320", "BlackBerry Curve 8320 (Cam kết Dịch vụ)", "BlackBerry Pearl 8100", "BlackBerry Pearl 8100 (Cam kết Dịch vụ)", "Cayon C260", "Cayon C260i", "Cayon C270", "Cayon C308", "Cayon C308i", "Cayon C508", "Cayon C509", "Cayon E520", "Cayon E530", "Cayon V830", "F - Mobile B220 Coffee", "F - Mobile B240 Gold", "F - Mobile B250 Black", "F-Mobile B800", "F-Mobile B860 Black", "F-Mobile B880 Black", "F-Mobile S600", "F-Mobile S880", "HTC Touch Cruice (T4242)", "HTC Touch II (P3452)", "HTC Touch Viva (T2223)", "LG - GB 101", "LG - GB 110 Black 1G", "LG - GB 230 Black", "LG - GB 270 Titan Silver 1G", "LG - GM 200 Black", "LG - GT505 Black", "LG - GW300 Red", "LG - GW525 Silver", "LG - KF 300 Black", "LG - KF 300 Pink", "LG - KF 300 Red", "LG - KF 350 Blue", "LG - KF 350 Pink", "LG - KF 510 Gold 1G", "LG - KF 510 Red 1G", "LG - KF 700", "LG - KF300 Gold", "LG - KG 195 1G", "LG - KG 288 Blue", "LG - KG 288 Red", "LG - KG 288 White", "LG - KM 335 Champagne Gold 1G", "LG - KM 900 ARENA", "LG - KM710", "LG - KP 100 Black", "LG - KP 100 Pink", "LG - KP 100 Silver", "LG - KP 100 White", "LG - KP 110 Black 512", "LG - KP 115 Black", "LG - KP 115 Blue", "LG - KP 115 Red", "LG - KP 175 Silver 1G", "LG - KP 175 Violet 1G", "LG - KP 199", "LG - KP 500 Black", "LG - KP 500 Brown", "LG - KP 500 Gold", "LG - KP 500 Silver", "LG - KS 360 Pink 1G", "Malata HL39 Black", "Malata HL88 Black", "Malata K100 Black", "Malata MT 236", "Malata MT116 Black", "Malata MT157 Grey", "Malata MT706 Black", "Mobell M160", "Mobell M190", "Mobell M200i", "Mobell M210", "Mobell M230", "Mobell M320", "Mobell M360", "Mobell M390i", "Mobell M520", "Mobell M550", "Mobell M590", "Mobell M610", "Mobell M720", "Motorola W230 Orange", "Motorola W230 Silver", "Motorola W270 Black KT", "Motorola W388 Black", "Motorola W388 Grey", "Motorola W396 Black", "Motorola W396 Grey", "Motorola W396 White", "Nokia 1202 Black", "Nokia 1202 Blue", "Nokia 1208 Black", "Nokia 1208 Red", "Nokia 1209 Grey", "Nokia 1209 Midnight Blue", "Nokia 1650 All Black", "Nokia 1650 Black", "Nokia 1650 Red", "Nokia 1661 Black", "Nokia 1661 Grey", "Nokia 1680 Classic Black", "Nokia 1680 Classic Grey", "Nokia 1680 Classic Red", "Nokia 2323 Silver", "Nokia 2330", "Nokia 2600 Classic Blue", "Nokia 2600 Classic Gold", "Nokia 2630 Black", "Nokia 2680 Blue", "Nokia 2680 Grey", "Nokia 2700 Classic", "Nokia 2720 Black", "Nokia 2730 Classic", "Nokia 2760 Gold", "Nokia 2760 Grey", "Nokia 2760 Red", "Nokia 3110 Classic", "Nokia 3600 Charcoal", "Nokia 3600 Dark Red", "Nokia 3600 Slide Grey", "Nokia 3610 Blue", "NOKIA 3710", "Nokia 3720", "Nokia 5000 Blue", "Nokia 5000 Green", "Nokia 5030", "Nokia 5130 Blue 1 GB", "Nokia 5130 Red 1 GB", "Nokia 5220 Blue", "Nokia 5220 Green", "Nokia 5220 Red", "Nokia 5230", "Nokia 5310 All Black", "Nokia 5310 Blue", "Nokia 5310 Blue 1G", "Nokia 5310 Gothic", "Nokia 5310 Purple 1G", "Nokia 5310 Red", "Nokia 5310 Red 1G", "Nokia 5530 XpressMusic", "Nokia 5610 Blue", "Nokia 5610 Blue The 1G", "Nokia 5610 Red", "Nokia 5610 Red 1G", "Nokia 5630 Xpress Music", "Nokia 5730 Red", "Nokia 5800 Blue", "Nokia 5800 Red", "Nokia 6300 Black-Silver", "Nokia 6300 Chocolate", "Nokia 6300 Full Black", "Nokia 6300 White", "Nokia 6303c", "Nokia 6500 Classic", "Nokia 6500 Slide", "Nokia 6500 Slide Brown", "Nokia 6500 Slide Full Black", "Nokia 6600 Fold", "Nokia 6600 Slide 512M", "Nokia 6600i Silver", "Nokia 6700 Black", "Nokia 6700 silver", "Nokia 6710 Navigator", "Nokia 6720", "Nokia 6730 Classic Black", "Nokia 6730 Classic White", "Nokia 6760 Slide", "Nokia 7070 Black-Blue", "Nokia 7070 Black-Pink", "Nokia 7100 Supernova Blue", "Nokia 7100 Supernova Red", "Nokia 7210 Supernova Blue", "Nokia 7210 Supernova Pink", "Nokia 7310 Clacssic Blue-Pink", "Nokia 7310 Classic Blue-Green", "Nokia 7610 Supernova Black", "Nokia 7610 Supernova White", "Nokia 8800 Carbon Arte", "Nokia 8800 Gold Arte", "Nokia E52 Grey", "Nokia E63 Black", "Nokia E63 Blue", "Nokia E63 Red", "Nokia E66 Black", "Nokia E66 Grey", "Nokia E66 Red", "Nokia E66 White", "Nokia E71 Black", "Nokia E71 Grey", "Nokia E71 Red", "Nokia E71 White", "Nokia E72", "Nokia E75 Black Silver", "Nokia E75 Copper Yellow", "Nokia E75 Red", "Nokia N70 Music", "Nokia N70 Music The 1G", "Nokia N79 Brown", "Nokia N79 Grey", "Nokia N85 Black", "Nokia N85 Copper", "Nokia N86", "Nokia N96 Black", "Nokia N96 Dark Grey", "Nokia N97", "Nokia N97 Mini", "Nokia X3", "Philips 9@9U", "Philips X500", "Q - Mobile CF220 Black", "Q - Mobile CF220 Silver", "Q - Mobile CF240 Pink", "Q - Mobile F225 Black", "Q - Mobile F225 Blue", "Q - Mobile F225 Pink", "Q - Mobile F225 Red", "Q - Mobile F252 Black", "Q - Mobile F53 Black Khong the", "Q - Mobile F53 Silver Khong the", "Q - Mobile F680 Black", "Q - Mobile F680 Gold", "Q - Mobile F680 Gold Black", "Q - Mobile F73 Black Coffee", "Q - Mobile F73 Black White", "Q - Mobile F73 Pink White", "Q - Mobile M1 Black", "Q - Mobile M2 Black", "Q - Mobile M22 Red", "Q - Mobile M3 Dark Red", "Q - Mobile ME01 Black", "Q - Mobile Q 130 Black", "Q - Mobile Q 130 Black Grey", "Q - Mobile Q 130 Grey Coffee", "Q - Mobile Q10 Black", "Q - Mobile Q11 Black", "Q - Mobile Q11 Red", "Q - Mobile Q12 Black", "Q - Mobile Q12 Champion", "Q - Mobile Q12 Red Khong the", "Q - Mobile Q120 Black", "Q - Mobile Q121 Black", "Q - Mobile Q126 Black Red", "Q - Mobile Q180 Green", "Q - Mobile Q210 Black", "Q - Mobile Q210 Silver", "Q - Mobile Q212 Black Red", "Q - Mobile Q212 Grey White", "Q - Mobile Q213 Gun Black", "Q - Mobile Q213 Red Black", "Q - Mobile Q218", "Q - Mobile Q230", "Q - Mobile Q24 Black", "Q - Mobile Q24 Red", "Q - Mobile Q25 Black", "Q - Mobile Q25 Pink Gold", "Q - Mobile Q26 Black Champion", "Q - Mobile Q26 Black Grey", "Q - Mobile Q26 Red Black", "Q - Mobile Q266 Black Blue", "Q - Mobile Q266 Black Dark", "Q - Mobile Q266 Black Red", "Q - Mobile Q270", "Q - Mobile Q28 Black Khong the", "Q - Mobile Q28i Black Red", "Q - Mobile Q38 Black", "Q - Mobile Q460 Black", "Q - Mobile Q510 Metan Black", "Q - Mobile Q540 Black", "Q - Mobile Q63 Black", "Q - Mobile T550 Black", "Q -Mobile Q38 Silver", "Samsung B200", "Samsung B200SILVER", "Samsung B2100", "Samsung B220BLUE", "Samsung B220GREY", "Samsung B300DARKGREEN", "Samsung B300RED", "Samsung B3210", "Samsung B520BLUE", "Samsung B520BLUE 1G", "Samsung B520GOLD", "Samsung B520GOLD 1G", "Samsung B520GREY", "Samsung B520GREY 1G", "Samsung B520PINK", "Samsung B520PINK 1G", "Samsung B520RED", "Samsung B520SILVER", "Samsung B7320 Brown", "Samsung C3010 BLACK", "Samsung C3053BLACK", "Samsung C3053ORANGE", "Samsung C3053PINK", "Samsung C3212 Sliver", "Samsung C5212BLACK", "Samsung C5212BLUE", "Samsung C5212RED", "Samsung D900i BLACK", "Samsung D980 BLACK", "Samsung E1125 Silver", "Samsung E1310BLACK", "Samsung E1310RED", "Samsung E1310SILVER", "Samsung E1360BLACK", "Samsung E250BLACK", "Samsung E250BLACK 1G", "Samsung E250ICBLUE", "Samsung E250IEBLACK", "Samsung E250IPINK", "Samsung E250ISILVER", "Samsung E251 Black", "Samsung E2510 Black Gold", "Samsung E2510GREY", "Samsung E2510RED", "Samsung E2510SILVER", "Samsung F250BLUEKT", "Samsung F250BROWNKT", "Samsung F250REDKT", "Samsung I5700", "Samsung I8000 BLACK", "Samsung I8510BLACK8G", "Samsung I8910 BLACK", "Samsung I900BLACK8G", "Samsung J700GOLD", "Samsung J700GOLD 1G", "Samsung J700ITGOLD", "Samsung J700IVPINK", "Samsung J700PINK 1G", "Samsung J700SILVER", "Samsung J700SILVER 1G", "Samsung M2513 BLUE", "Samsung M620GOLD", "Samsung M620GOLD 1G", "Samsung M620GREY", "Samsung M620GREY 1G", "Samsung M620PINK", "Samsung M620PINK 1G", "Samsung M620SILVER", "Samsung M620SILVER 1G", "Samsung M620VIOLET", "Samsung M620VIOLET 1G", "Samsung M7603 Blue", "Samsung S3500E BLACK", "Samsung S3500M SILVER", "Samsung S3500R PINK", "Samsung S3600BLACK", "Samsung S3600GOLD", "Samsung S3600PINK", "Samsung S3600PUPER", "Samsung S3600SILVER", "Samsung S3653", "Samsung S5200 BLUE", "Samsung S5200 RED", "Samsung S5233 Wifi", "Samsung S5233S BLACK", "Samsung S5233S PINK", "Samsung S5233S WHITE", "Samsung S8003 Black", "Samsung S8300 BLACK", "Samsung S8300 RED", "Samsung U300", "Sony Ericson W205 Black", "Sony Ericson W395 Hello Kitty 1G", "Sony Ericsson Aino", "Sony Ericsson C510 512 MB", "Sony Ericsson C903 Red Thẻ 1G", "Sony Ericsson F305 Black", "Sony Ericsson F305 White 512", "Sony Ericsson J132 Black", "Sony Ericsson J132 Blue", "Sony Ericsson Jalou", "Sony Ericsson S302 Blue 1G", "Sony Ericsson S312 Blue", "Sony Ericsson S500i Black", "Sony Ericsson S500i Purple", "Sony Ericsson S500i White", "Sony Ericsson S500i Yellow", "Sony Ericsson Satio", "Sony Ericsson T700 Black red 512 MB", "Sony Ericsson T700 Pink The 512", "Sony Ericsson T707 Blue 512M", "Sony Ericsson T715", "Sony Ericsson W302 Black 512M", "Sony Ericsson W395 Purple Thẻ 1G", "Sony Ericsson W395 Titan Thẻ 1G", "Sony Ericsson W508 White Thẻ 1G", "Sony Ericsson W580i Black 512", "Sony Ericsson W580I Pink 512", "Sony Ericsson W580i Red 512", "Sony Ericsson W595 Blue 1G", "Sony Ericsson W595 Grey 2G", "Sony Ericsson W705 4GB", "Sony Ericsson W705 Silver 4G", "Sony Ericsson W980 Black", "Sony Ericsson W995 Silver Thẻ 8G", "Sony Ericsson Yari", "SUMO SIM E1100T BLACK", "SUMO SIM VT210H", "SUMO SIM VT316Z"];
    },
	Callback : function(p,json1,text)
	{
		switch(p.index)
		{
			case 0: // Get all Model
			    if(text == null) return;
	            var json = text.evalJSON(true);
			    if(json.size()>0)
			    {
			        MobileShop.Cache.ProductName[this.ptype] = json;
			        this.data = json;
			        this.DataBind();
			    }
				break;
		}
	},
	GetParameters : function(index)
	{
		return this.parameters[index];
	},
	DataBind : function()
	{
	    if(MobileShop.Cache.ProductName[this.ptype] != null)
	    {
	        this.data = MobileShop.Cache.ProductName[this.ptype];
	        Autocompleter(this.text,this.data,this.OnAutoComplete.bind(this));
            WaveText.Register(this.text,"Nhập tên máy","#00416A","#00416A");
        }
	    else
	    {
	        this.tryCount++;
	        if(this.tryCount<5) this.Request(0);
	    }
	},
    Mode : function(type)
    {
        this.text.parentNode.style.display = type?"block":"none";
        type?this.list.hide():this.list.show();
    },
    OnAutoComplete : function()
	{
	    DoSearch();
	},
    OnKeyUp : function(e)
    {
        e = e||window.event;
        if (e && e.keyCode==13) 
        {
            DoSearch();
            Event.stop(e);
        }
    }
	
}

// =========================Provider class==========================
MobileShop.Provider = Class.create();
MobileShop.Provider.prototype =
{
    // options is default value of model -- ptype is ProductTypeID
    initialize : function(provider,model,options,ptype)
    {
        this.Implementation = {};
		this.Implementation[MobileShop.IService.type] = true;
		this.regKey = $GUID.New("key");
		this.parameters = [{"action":101,"productTypeID":ptype}];
		MobileShop.IService.Implement(this,0);	// url for local testing
		
		this.ptype = ptype;
        this.tryCount = 0;
		this.model = new MobileShop.Model(model,options);
		this.model.provider = this;
		this.list = new MobileShop.DropDownList(provider);
		this.list.onchange = this.ChangeProvider.bind(this);
	},
	Callback : function(p,json)
	{
		switch(p.index)
		{
			case 0: // Get all Providers
			    if(json == null) return;
			    if(json.size()>0)
			    {
			        MobileShop.Cache.Provider[this.ptype] = json;
			        this.data = json;
			        this.DataBind();
			    }
				break;
		}
	},
	GetParameters : function(index)
	{
		return this.parameters[index];
	},
	AddItem : function(item)
	{
	    this.list.AddItem(item);
	},
	DataBind : function()
	{
	    if(MobileShop.Cache.Provider[this.ptype]!=null)
	    {
	        this.data = MobileShop.Cache.Provider[this.ptype];
	        this.list.DataBind(this.data);
	        var args={"count":this.list.Items.length};
            if(typeof this.onload == "function") this.onload(this,args);
        }
	    else
	    {
	        this.tryCount++;
	        if(this.tryCount<5) this.Request(0);
	    }
	},
	ChangeProvider : function(sender,args)
	{
	    // send a request to get Models
	    var v = this.list.SelectedItem.value();
	    if(v=="-1") this.model.Reset();
	    else this.model.ChangeModel(v);
	},
	SetValue:function(value)
	{
	    this.list.SetValue(value);
	}
};

// =================================Prefecture================================
MobileShop.Prefecture = Class.create();
MobileShop.Prefecture.prototype = 
{
    initialize : function(container,provinceID)
    {
        this.Implementation = {};
		this.Implementation[MobileShop.IService.type] = true;
		this.regKey = $GUID.New("key");
		this.parameters = [{"action":103, "provinceID":provinceID},{"action":104}];
		MobileShop.IService.Implement(this,0);	// url for local testing
		MobileShop.IService.Implement(this,1);
        
        this.provinceID = provinceID;
        this.tryCount = 0;
        this.list = $(container);
        this.list.onchange = this.ChangePrefecture.bind(this);
        this.cost = 0;
    },
    Callback : function(p,json1,text)
	{
	    switch(p.index)
		{
		    case 0: // Get all Prefecture
		        if(text == null) return;
	            var json = text.evalJSON(true);
		        if(json.size()>0)
			    {
			        MobileShop.Cache.Prefecture[p.provinceID] = json;
			        this.data = json;
			        this.DataBind();
			    }
			    else this.Reset();
				break;
			case 1: // Get Prefecture cost
			    if(json1 == null) return;
			    if(json1.size()>0) 
			    {
			        this.cost = parseInt(json1[0].cost);
			        BindDeliverCost(this.cost, this.list.id);
			        BuildingCost();
			    }
			    break;
		}
	},
	GetParameters : function(index)
	{
	    return this.parameters[index];
	},
	DataBind : function()
	{
	    if(this.provinceID>0)
	    {
	        if(MobileShop.Cache.Prefecture[this.provinceID] != null)
	        {
	            this.data = MobileShop.Cache.Prefecture[this.provinceID];
	            this.Reset();
                for(var i=0,n=this.data.length;i<n;i++)
	            {
	                var opt = document.createElement('option');
                    opt.text = this.data[i].name;
                    opt.value = this.data[i].id;
                    try
                    {
                        this.list.add(opt, null);
                    }
                    catch(ex)
                    {
                        this.list.add(opt); // for IE only
                    }
	            }
	            this.cost = 0; // reset
	            var args={"count":this.list.length};
                if(typeof this.onBindComplete == "function") this.onBindComplete(this,args);
		    }
		    else
		    {
		        this.tryCount++;
		        if(this.tryCount<5) 
		        {
		            this.Request(0);
		        }
		    }
		}
		else this.Reset();
	},
	Clear : function()
	{
	    for(var i=0,n=this.list.length;i<n;i++)
        {
            this.list.remove(0);
        }
	},
    Reset : function()
    {
        for(var i=0,n=this.list.length;i<n;i++)
        {
            this.list.remove(0);
        }
        var opt = document.createElement('option');
        opt.text = "..: Lựa chọn quận/huyện :..";
        opt.value = "-1";
        try
        {
            this.list.add(opt, null);
        }
        catch(ex)
        {
            this.list.add(opt); // for IE only
        }
    },
    SetValue : function(value)
    {
        this.list.value = value;
    },
    ChangePrefecture : function (sender, args)
    {
        // get arr pType of all product in cart
        var path = '/';
        var exp = new Date();
        exp.setDate(exp.getDate() + 1);
        var cookieType = new CookieHelper("$Type",'',path,exp);
        var tmp = cookieType.Get();
        if(this.list.value == "-1")
        {
            this.cost = 0;
            BindDeliverCost(this.cost, this.list.id);
            BuildingCost();
        }
        else if(tmp)
        {
            this.parameters[1].prefectureID = this.list.value;
            this.parameters[1].pType = tmp;
            this.Request(1); // bind value to this.cost
        }
    }
}

// ============================Model==========================
MobileShop.Model = Class.create();
MobileShop.Model.prototype =
{
    initialize : function(container,options)
    {
        this.Implementation = {};
		this.Implementation[MobileShop.IService.type] = true;
		this.regKey = $GUID.New("key");
		this.parameters = [{"action":102}];
		MobileShop.IService.Implement(this,0);	// url for local testing

        this.options = options||false;
		this.list = new MobileShop.DropDownList(container);
	},
	Callback : function(p,json)
	{
		switch(p.index)
		{
			case 0: // Get all Providers
			    if(json.size()>0)
			    {
			        this.data = json;
			        this.DataBind();
			        MobileShop.Cache.Model[p.providerID] = json;
			    }
			    else this.Reset();
				break;
		}
	},
	GetParameters : function(index)
	{
		return this.parameters[index];
	},
	DataBind : function()
	{
		this.Reset();
		this.list.DataBind(this.data);
		var args={"count":this.list.Items.length};
        if(typeof this.onBindComplete == "function") this.onBindComplete(this,args);
	},
    ChangeModel : function(providerID)
    {
        if(MobileShop.Cache.Model[providerID]!=null)
        {
            this.data = MobileShop.Cache.Model[providerID];
            this.DataBind();
        }
        else
        {
            this.parameters[0].providerID = providerID;
            this.Request(0);
        }
    },
    Reset : function()
    {
        this.list.Clear();
	    if(this.options) this.list.AddItem(this.options);
    },
	SetValue : function(value)
	{
	    this.list.SetValue(value);
	}
};

MobileShop.DropDownList = Class.create();
MobileShop.DropDownList.prototype =
{
    initialize : function(container,fineTurn)
    {
        this.container = $(container);
        this.Items = [];
        this.isShow = false;
        this.SelectedItem = null;
        this.turn = fineTurn||false;

        this.container.onclick = this.onClick.bind(this);
        this.clickListener = this.clickObserver.bindAsEventListener(this);
		this.addItem = this.AddItem.bind(this);

        this.Render();
    },
    Render : function(items)
    {
    	this.container.className = "popupControl activeTheme";
        this.label = $C("div");
        this.label.className = "popupLabel";

        this.context = $C("div");
        this.context.className = "popupMenu";
        if(this.turn)
        {
        	var d = $C("div");
			d.style.cssText = "width:12px;float:left";
        	var imgUp = $C("img");
        	imgUp.src = MobileShop.Path+"images/Web/up.gif";
        	//imgUp.title = "up";
        	imgUp.style.cssText = "top:3px;left:0px";
        	imgUp.onclick = this.onFineTurn.bind(this,2);

        	var imgDown = $C("img");
        	imgDown.src = MobileShop.Path+"images/Web/down.gif";
        	//imgDown.title = "down";
        	imgDown.style.cssText = "top:6px;left:3px";
        	imgDown.onclick = this.onFineTurn.bind(this,1);

        	d.appendChild(imgUp);
        	d.appendChild(imgDown);
        	this.container.appendChild(d);
        }
        this.container.appendChild(this.label);
    },
    Clear : function()
    {
    	this.Show(false);
    	this.SelectedItem = null;
    	this.Items = [];
    	this.label.innerHTML = "";
    	this.context.innerHTML = "";
    	this.context.style.height = "";
    },
    AddItem : function(item)
    {
        var listItem = new MobileShop.ListItem(this,item,this.Items.length);
        this.context.appendChild(listItem.GUI);
        this.Items.push(listItem);
        if(this.SelectedItem==null) this.SetIndex(0);
    },
    DataBind : function(items)
    {
    	items.each(this.addItem);
    },
    Fire : function(item)
    {
    	// an item is selected
    	if(item!=this.SelectedItem)
    	{
    		if(this.SelectedItem!=null) this.SelectedItem.Active(false);
    		item.Active(true);
    		this.SelectedItem = item;
    		this.SelectedIndex = item.index;
    		this.label.innerHTML = item.text();
    		var args = {"index":item.index,"text":item.text(),"value":item.value()};
    		//this.Show(false);		// because of bubble event
    		if(typeof this.onchange == "function") this.onchange(this,args);
    	}
    },
    onClick : function(e)
    {
    	e = e||window.event;
        Event.stop(e);
        this.Show(!this.isShow);
    },
    onFineTurn : function(type,e)
    {
    	e = e||window.event;
    	Event.stop(e);
    	switch(type)
    	{
    		case 1:		// up
				this.SetIndex(this.SelectedIndex-1);
    			break;
    		case 2:		// down
    			this.SetIndex(this.SelectedIndex+1);
    			break;
    	}
    },
    clickObserver : function(e)
    {
    	this.Show(false);
    },
    Show : function(enabled)
    {
		if(this.isShow==enabled) return;
		this.isShow=enabled;
	    if(this.isShow)		// show
	    {
	    	Event.observe(document,"click",this.clickListener);
	    	document.body.appendChild(this.context);
	    	if(this.context.offsetHeight>210) this.context.style.height = "210px";

	    	if(this.pos==null)
	    	{
	    		var delta = {top:23,left:0};
	    		//if(Prototype.Browser.IE) delta = {top:23,left:0};
	    		this.pos = Position.positionedOffset(this.container);
		        this.context.style.left=(this.pos[0]+delta.left)+"px";
		        this.context.style.top=(this.pos[1]+delta.top)+"px";
		    }
	    }
	    else	// hide
	    {
	    	Event.stopObserving(document,"click",this.clickListener);
	    	document.body.removeChild(this.context);
	    }
		//this.context.style.visibility="visible";
    },
    SetIndex : function(index)
    {
    	if(-1<index&&index<this.Items.length) this.Fire(this.Items[index]);
    },
    SetValue : function(value)
    {
    	var index = -1;
		for(var i=0,n=this.Items.length;i<n;i++)
		{
			if(this.Items[i].value()==value)
			{
				index = i;
				break;
			}
		}
		if(index!=-1) this.SetIndex(index);
    }
};

MobileShop.ListItem = Class.create();
MobileShop.ListItem.prototype =
{
    initialize : function(parent,item,index)
    {
        this.parent = parent;
        this.index = index;
        this.isActive = false;
        this.item=item;
		this.GUI=$C("div");
		this.GUI.innerHTML=item.name;
		this.GUI.onclick=this.onSelected.bind(this);
		this.GUI.onmouseover=this.onMouseOver.bind(this);
		this.GUI.onmouseout=this.onMouseOut.bind(this);
    },
    onSelected:function()
	{
		this.onMouseOut();
		this.parent.Fire(this);
	},
	onMouseOut:function()
	{
		if(this.isActive) this.GUI.className = "popupSelected";
		else this.GUI.className="popupNormal";
	},
	onMouseOver:function()
	{
		this.GUI.className="popupHover";
	},
	Active : function(enabled)
	{
		if(this.isActive==enabled) return;
		this.isActive=enabled;
		if(this.isActive) this.GUI.className = "popupSelected";
		else this.GUI.className = "popupNormal";
	},
	text : function()
	{
		return this.item.name;
	},
	value : function()
	{
		return this.item.id;
	}
};

//======================== Member login class ============================
MobileShop.MemberLogin = Class.create();
MobileShop.MemberLogin.prototype =
{
    initialize : function(email, password)
    {
        this.email = $(email);
        this.password = $(password);

        this.Implementation = {};
		this.Implementation[MobileShop.IService.type] = true;
		this.regKey = $GUID.New("key");
		this.parameters = [{"action":201},{"action":202}];
		MobileShop.IService.Implement(this,0);
		MobileShop.IService.Implement(this,1);

		this.user = {"Alias":"","ID":0,"LoginStatus":0};
	},
	Callback : function(p,json)
	{
		switch(p.index)
		{
			case 0: // Login
			    if(json) 
			    {
			        switch(json.LoginStatus)
			        {
			            case 1: // login success
			                this.SetUser(json);
			                break;
			            case -1: // login fail
			                alert("Sai tài khoản hoặc mật khẩu!!!");
			                break;
			            case 0: // not login jet
			                alert("Bạn chưa đăng nhập!!!");
			                break;
			            case 2: // logined
			                alert("Bạn đã đăng nhập!!!");
			                break
			        }
			    }
				break;
			case 1:
			    this.SetUser({"Alias":"","ID":0,"LoginStatus":0});
			    break;
		}
	},
	GetParameters : function(index)
	{
	    switch(index)
	    {
	        case 0:
	            this.parameters[index].email = this.email.value;
	            this.parameters[index].password = this.password.value;
	            break;
	        case 1:
	            this.parameters[index].userID = this.user.ID;
	            break;
        }
		return this.parameters[index];
	},
	SignIn : function()
	{
	    if(this.user.ID==0)
	    {
	        var email = $F(this.email);
	        var pass = $F(this.password);
	        if(email.trim()=="Email"||pass.trim()=="Mật khẩu") alert("Nhập email và mật khẩu!!!");
	        else this.Request(0);
        }
	},
	SignOut : function()
	{
	    if(this.user.ID!=0) this.Request(1);
	},
	WelcomeUser : function()
	{
	    $("hasLoggedIn").style.display = (this.user.ID!=0)?"block":"none";
	    $("notLoggedIn").style.display = (this.user.ID!=0)?"none":"block";
	    $("divLogin").style.display = "none";
	},
	SetUser : function(user)
	{
	    this.user = user;
	    this.WelcomeUser();
	}
};

// Member add image class
MobileShop.AddPictureToFavorite = Class.create();
MobileShop.AddPictureToFavorite.prototype =
{
    initialize : function(imageID)
    {
        this.imageID = imageID

        this.Implementation = {};
		this.Implementation[MobileShop.IService.type] = true;
		this.regKey = $GUID.New("key");
		this.parameters = [{"action":301}];
		MobileShop.IService.Implement(this,0);
	},
	Callback : function(p,json)
	{
		switch(p.index)
		{
			case 0:
			    if(json)
			    {
			        this.data = json;
			    }
				break;
		}
	},
	GetParameters : function(index)
	{
	    this.parameters[index].imageID = this.imageID;
		return this.parameters[index];
	}
};