
/* 

	Cs shops viewer
	by small, 2009/3/24

*/

function CsViewer(arr_locations, arr_companys){

	/* pre-loaded data */
	this._locations=arr_locations;
	this._companys=arr_companys;

	/* controls */
	this._container=null;
	this._locationspan=null;
	this._locationselector=null;
	this._companylistspan=null;
	this._shopslistspan=null;

	/* network & database */
	this._csbaseurl="";
	this._currentcompanyid=0;
	this._currentlocationid=0;
	this._cachedshops=[];

	/* methods */

	this.GetCurrentCompany=function(){
		var companyid=this._currentcompanyid;
		if(companyid>0){
			var list=this._companys;
			for(var i=0;i<list.length;i++){
				if(list[i].ID==companyid){
					return list[i];
				}
			}
		}
		return null;
	}

	this.OnSingleShopSelected=function(){
	}

	this.AppendSingleShopToContainer=function(container, company, shopitem){
		var sh_row=document.createElement("span");
		container.appendChild(sh_row);
		sh_row.style.cssText="display:block; margin:0px; padding:0px 0px 6px 0px;";
		// company name
		var sh_co_name=document.createElement("span");
		sh_row.appendChild(sh_co_name);
		sh_co_name.style.cssText="padding:0px 6px 0px 6px;";
		sh_co_name.innerHTML=company.Name;
		// shop name [BOLD]
		var sh_name=document.createElement("span");
		sh_row.appendChild(sh_name);
		sh_name.style.cssText="font-weight:bold; padding:0px 8px 0px 6px; color:#0066cc;";
		sh_name.innerHTML=shopitem.name;
		// shop description
		var sh_desc=document.createElement("span");
		sh_row.appendChild(sh_desc);
		sh_desc.innerHTML=shopitem.Text + (shopitem.tel?(" (电话: "+shopitem.tel+")"):"");
		// shop tel
		// shop service property
		var sh_svc=document.createElement("span");
		sh_row.appendChild(sh_svc);
		sh_svc.style.cssText="padding-left:13px; color:#8fc31f;";
		var svctext=shopitem.mailing>0?"取件":"";
		svctext=svctext?(shopitem.cardsell>0?svctext+"，售卡":svctext):(shopitem.cardsell>0?"售卡":"");
		sh_svc.innerHTML=svctext; //+"("+shopitem.mailing+", "+shopitem.cardsell+")";
	}

	this.OnShopsReceived=function(obj){
		var sh_span=this._shopslistspan;
		if(!sh_span){ return; }
		sh_span.innerHTML="";
		if(obj){
			company=this.GetCurrentCompany();
			if(obj.Items){
				var actualcount=0;
				for(var i=0;i<obj.Items.length;i++){
					if(obj.Items[i].mailing>0 || obj.Items[i].cardsell>0){
						this.AppendSingleShopToContainer(sh_span, company, obj.Items[i]);
						actualcount++;
					}
				}
				if(actualcount<=0){
					sh_span.innerHTML=" 该区域内没有“"+company.Name+"”网点，部分网点可能临时不提供服务。";
				}
			}else{
				if(company){
					sh_span.innerHTML=" 该区域内没有“"+company.Name+"”网点。";
				}else{
					sh_span.innerHTML=" 该区域内没有网点。";
				}
			}
		}
	}
	
	this.OnCompanySelected=function(companyid){
		this._currentcompanyid=companyid;
		var locationid=this._currentlocationid;
		var csviewer=this;
		if(companyid>0 && locationid>0){
			var req = new XRequest();
			//var shopstext = document.getElementById(_csShopsTextSpanID);
			//if (shopstext) { shopstext.innerHTML = "<img src=\"images/loading2.gif\" border=\"0\" align=\"absmiddle\" /> 正在查询该地区的所有<b>" + this.cs_GetCompanyName(_currentCompanyID) + "</b>网点, 请稍等..."; }
			var sh_span=this._shopslistspan;
			if(sh_span){
				sh_span.innerHTML="<img src=\"images/loading2.gif\" border=\"0\" align=\"absmiddle\" /> 正在查询请稍等...";
			}
			var callback=function(obj, viewer){
				if(viewer && obj){
					viewer.OnShopsReceived.call(viewer, obj);
				}
			}
			req.GetRemoteObject(this._csbaseurl + "/CsSelector.ashx?location=" + locationid + "&company=" + companyid, callback, csviewer);
		}
	}

	this.AppendCompanySpanToContainer=function(container, companyid, companyname){
		var root=this;
		var co_item=document.createElement("span");
		container.appendChild(co_item);					
		co_item.style.cssText="padding:0px 12px 0px 0px;";

		var ar=document.createElement("span");
		co_item.appendChild(ar);
		ar.innerHTML="&raquo;";
		ar.style.cssText="padding-right:3px; color:gray;";
		var cname=document.createElement("span");
		co_item.appendChild(cname);
		cname.innerHTML=companyname;
		cname.style.cssText="cursor:pointer;";
		cname.onmouseover=function(){
			this.style.cssText="cursor:pointer; color:#ff9900;";
		}
		cname.onmouseout=function(){
			this.style.cssText="cursor:pointer; color:auto;";
		}
		cname.onclick=function(){
			root.OnCompanySelected.call(root, companyid);
		}
	}

	this.OnRemoteCompanysReceived=function(obj){
		var co_span=this._companylistspan;
		if(!co_span){
			return;
		}
		var sh_span=this._shopslistspan;
		co_span.innerHTML="";
		if (obj) {
			if (obj.Items) {
				var cs_count=obj.count;
				var cols=7;
				for(var i=0;i<obj.Items.length;i++){
					var companyid=obj.Items[i].id;
					var companyname=obj.Items[i].Text;
					this.AppendCompanySpanToContainer(co_span, companyid, companyname);
					if((i+1)>=cols && (i+1)%cols==0){
						var br=document.createElement("br");
						co_span.appendChild(br);
					}
				}
				if(obj.Items.length==1){
					this.OnCompanySelected(obj.Items[0].id);
				}else{
					sh_span.innerHTML="请选择公司。";
				}
				if(cs_count<=0){
					co_span.innerHTML="所选地区没有网点.";
				}
			}else{
				co_span.innerHTML="所选地区没有网点.";
			}
		}
	}

	this.ClearCompanyList=function(companys){
		var co_span=this._companylistspan;
		if(co_span){
			co_span.innerHTML="";
		}
	}

	this.OnLocationChanged=function(location_id){
		var root=this;
		this._currentlocationid=location_id;
		if(location_id>0){
			var req = new XRequest();
			//var companytext = document.getElementById(_csCompanyTextSpanID);
			//if (companytext) { companytext.innerHTML = "<img src=\"images/loading2.gif\" border=\"0\" align=\"absmiddle\" /> 正在查询该地区的网点信息, 请稍等..."; }
			var co_span=this._companylistspan;
			if(co_span){
				co_span.innerHTML="<img src=\"images/loading2.gif\" border=\"0\" align=\"absmiddle\" /> 正在查询请稍等...";
			}
			var callback=function(obj, csviewer){ 
				if(csviewer && obj){
					csviewer.OnRemoteCompanysReceived.call(csviewer, obj); 
				}
			}
			req.GetRemoteObject(this._csbaseurl + "/CsSelector.ashx?location=" + location_id, callback, this);
		}else{
			this.ClearCompanyList();
		}
	}

	this.LocationHasSubnodes=function(locationnode){
		var locations=this._locations;
		if(locations){
			for(var i=0;i<locations.length;i++){
				if(locations[i].ParentID==locationnode.ID){
					return true;
				}
			}
		}
		return false;
	}

	this.FillLocations=function(container, locationparentnode, parent_prefix){
		var locations=this._locations;
		if(locations){			
			var l_parent=locationparentnode;
			if(!parent_prefix){ parent_prefix=""; }
			for(var i=0;i<locations.length;i++){
				if(l_parent){
					if(l_parent.ID==locations[i].ParentID){
						if(this.LocationHasSubnodes(locations[i])){
							if(container.tagName.toLowerCase()=="optgroup"){
								this.FillLocations(container, locations[i], parent_prefix + " &raquo; "+locations[i].Name);
							}else{
								var optgroup=document.createElement("optgroup");
								container.appendChild(optgroup);
								optgroup.label=locations[i].Name;
								this.FillLocations(optgroup, locations[i], parent_prefix + " &raquo; "+locations[i].Name);
							}
						}else{
							var opt=document.createElement("option");
							container.appendChild(opt);
							opt.value=locations[i].ID;
							opt.innerHTML=parent_prefix + " &raquo; "+locations[i].Name;
						}
					}
				}else{
					if(locations[i].ParentID<=0){
						if(this.LocationHasSubnodes(locations[i])){
							var optgroup=document.createElement("optgroup");
							container.appendChild(optgroup);
							optgroup.label=locations[i].Name;
							this.FillLocations(optgroup, locations[i], locations[i].Name);
						}else{
							var opt=document.createElement("option");
							container.appendChild(opt);
							opt.value=locations[i].ID;
							opt.innerHTML=locations[i].Name;							
						}
					}
				}
			}
		}
	}

	/* methods */
	this.Show=function(containerid){
		var container=document.getElementById(containerid);
		if(container){
			var root=this;
			this._container=container;
			// location span & selector
			var loc_span=document.createElement("span");
			container.appendChild(loc_span);
			this._locationspan=loc_span;
			loc_span.style.cssText="display:block;";

			// company span
			var co_span=document.createElement("span");
			container.appendChild(co_span);
			this._companylistspan=co_span;
			co_span.style.cssText="display:block; margin:4px 0px 4px 0px; padding:8px 0px 8px 8px; line-height:1.5em; background-color:#fffece;";
			co_span.innerHTML="请选择地区/城市。";

			// shops list span
			var sh_span=document.createElement("span");
			container.appendChild(sh_span);
			sh_span.style.cssText="display:block; margin:5px 0px 0px 8px; line-height:1.5em;";
			this._shopslistspan=sh_span;

			// selector
			var sel=this._locationselector;
			if(!sel){
				sel=document.createElement("select");
				loc_span.appendChild(sel);
				this._locationselector=sel;
			}
			this.FillLocations(sel);
			sel.onchange=function(){
				root.OnLocationChanged.call(root, this.value);
			}

		}
	}

}