function init(){
	if(channelArray.length == 0){
		return;
	}
	channelArray.sort(arraySort);
	
	var defaultCheckedChannel = initChannelView();
	
	channelNameView(defaultCheckedChannel.id);

	productView(defaultCheckedChannel.cpId,defaultCheckedChannel.id);
	
	flowchartView(defaultCheckedChannel);//加载流程图
	//tipsView(channelArray[0]);
}

function arraySort(a,b){
	var aSortValue = eval('channel_' + a.id +".sortValue");
	var bSortValue = eval('channel_' + b.id +".sortValue");
	if(aSortValue == null || bSortValue == null){
		return 0;
	}
	return aSortValue-bSortValue;
}

//初始化通道显示
function initChannelView(){
	var channelView = document.getElementById("channelView");
	//删除所有子元素
	removeAllChild(channelView);
	
	var defaultCheckedChannel = null;
	
	var k = 0;
	for(var i =0 ;i < channelArray.length; i++){
		var displayType = eval('channel_' + channelArray[i].id +".displayType");
		if(displayType == 'hide'){
			continue;//如果有隐藏配置就不显示
		}
		
		var div = document.createElement("div");
		var input = null;
		if(document.all == null){
			input = document.createElement("input"); 
			input.type='radio';
			input.setAttribute("name",'radiobutton1');  
		}else{
			input = document.createElement("<input type='radio' name='radiobutton1' "+(k==0?"checked=true":"")+">"); 
		}
		//var input = document.createElement("<input type='radio' name='radiobutton1' "+(k==0?"checked=true":"")+">"); 
		//var input = document.createElement("input"); 
		var span = document.createElement("span");
		
		if(k==0){
			defaultCheckedChannel = channelArray[i];
			input.checked = true;
		}
		
		
		input.id='radiobutton1'+i;
		input.value=channelArray[i].id;
	
		input.onclick=function(){
				var channel = getChannel(this.value);
				var channelConfig = eval('channel_' + this.value);
		
				if(channelConfig.displayType == 'redirect'){
					//alert(channelConfig.displayType);
					var redirectUrl = channelConfig.redirectUrl.replace(/{cpId}/,channel.cpId);
					window.open(redirectUrl);
					return true;
				}
				
				channelNameView(channel.id);
				productView(channel.cpId,channel.id);
				
				flowchartView(channel);//流程图
				//tipsView(channel);
				return true;
			  }
		
		span.className = 'paddingl10';
		span.innerHTML =  eval('channel_' + channelArray[i].id + '.name');
		
		div.appendChild(input);
		div.appendChild(span);
		
		k++;
		
		channelView.appendChild(div);
		
	}
	
	return defaultCheckedChannel;
}
//显示通道名
function channelNameView(channelId){
		var channelNameView = document.getElementById('channelNameView');
		channelNameView.innerHTML = eval('channel_'+channelId+'.name');
}

//显示通道流程图
function flowchartView(channel){
	
		var flowchartView = document.getElementById('flowchartView');
		removeAllChild(flowchartView);
		
		if(channel.flowchart != null){
			flowchartView.appendChild(channel.flowchart);
			return;
		}
		var channelConfig = eval('channel_'+channel.id);
		if(channelConfig.flowchartUrl_1==''){
			return;
		}
		
		
		var flowchart = document.createElement('span');
		
		var img1 = document.createElement('img');
		img1.src = channelConfig.flowchartUrl_1.replace(/{documentRoot}/,channel.documentRoot);
		img1.border=0;
		
		flowchart.appendChild(img1);
		
		if(channelConfig.flowchartUrl_2 != ''){//存在第二部分流程图
			
			var img2 = document.createElement('img');
			img2.src = channelConfig.flowchartUrl_2.replace(/{documentRoot}/,channel.documentRoot);;
			img2.border=0;
			if(officialWebsiteUrl != ''){
				//存在连接官网
				var a = document.createElement('a');
				a.href = officialWebsiteUrl;
				a.target='_blank';
				
				a.appendChild(img2);
				
				flowchart.appendChild(a);
				
			}else{
				flowchart.appendChild(img2);
			}
		}
		flowchartView.appendChild(flowchart);
		
		channel.flowchart = flowchart;
		
}

function productView(cpId,channelId){

	var productTableView = document.getElementById('productTableView');
	//删除所有子元素
	removeAllChild(productTableView);


	var channel = getChannel(channelId);//获取通道类对象
	
	if(channel.products != null){//已经加载过
		productTableView.appendChild(channel.products);
		tipsView(channel);
		return;
	}
	
	var data = "cpId="+cpId+"&channelId=" + channelId;
	var loading = document.createElement('div');
	loading.className ='trbg';
	loading.innerHTML ='&nbsp;&nbsp;&nbsp;&nbsp;<img src="images/blue-loading.gif" width="15" height="15"/>&nbsp;&nbsp;加载中，请稍后。。。';
	productTableView.appendChild(loading);
	//alert(productTableView.innerHTML);
	XMLHttp.sendReq('POST',"QueryProductServlet",data,function(objXMLHttp){
													var retText = objXMLHttp.responseText;
													
													removeAllChild(productTableView);
													eval("var obj="+retText);
													var productTable = null;
													//var productTable = document.createElement('<table width="350" border="0" cellspacing="1" cellpadding="5" class="tc">');
													if(document.all == null){
														//火狐
														productTable = document.createElement("table");
														productTable.width = '350px';
														//productTable.border = "0px";
														//productTable.cellspacing = 1;
														//productTable.cellpadding = 5;
														productTable.className = 'tc';
														productTable.setAttribute("border",'0');  
														productTable.setAttribute("cellspacing",'1');  
														productTable.setAttribute("cellpadding",'5');  
													}else{
														 productTable = document.createElement('<table width="350" border="0" cellspacing="1" cellpadding="5" class="tc">');
													}
												//	var productTable = document.createElement("table");
													
													
													var rowTitle = productTable.insertRow(productTable.rows.length);    
													
													var cellTitle1= rowTitle.insertCell(0); 
													
													cellTitle1.style.backgroundColor ='#F6F6F6';
													cellTitle1.innerHTML = '产品名称';
													
													var cellTitle2= rowTitle.insertCell(1); 
													
													cellTitle2.style.backgroundColor ='#F6F6F6';
													cellTitle2.innerHTML = '产品代码';
													
													var cellTitle3= rowTitle.insertCell(2); 
													
													cellTitle3.style.backgroundColor ='#F6F6F6';
													cellTitle3.innerHTML = '声讯购买费用';
													
													var tipsProdPrice = '';
													for(var i = 0;i<obj.length;i++){
														var rowBody = productTable.insertRow(productTable.rows.length);
														
														var cellBody1= rowBody.insertCell(0); 
														
														
														cellBody1.className ='trbg';
														cellBody1.innerHTML = obj[i].PROD_NAME;
														
														var cellBody2= rowBody.insertCell(1); 
														
														
														cellBody2.className ='trbg';
														cellBody2.innerHTML =  obj[i].PROD_ID;
														
														var cellBody3= rowBody.insertCell(2); 
														
														
														cellBody3.className ='trbg';
														cellBody3.innerHTML = obj[i].PC_PRICE;
														
														if(channel.documentRoot == 'default'){
															cellBody1.style.backgroundColor ='#FEEAF3';
															cellBody2.style.backgroundColor ='#FEEAF3';
															cellBody3.style.backgroundColor ='#FEEAF3';
														}
														
														tipsProdPrice += obj[i].PROD_NAME+'--实际扣费'+obj[i].PC_PRICE+'元；';
													}
													
													channel.products = productTable;
													channel.tipsProdPrice = tipsProdPrice;
												
													productTableView.appendChild(productTable);
													tipsView(channel);
												 });
	
	
	
}

//提示语
function tipsView(channel){

	var tipsView = document.getElementById('tipsView');
	
	removeAllChild(tipsView);
	
	if(channel.tips != null){
		tipsView.appendChild(channel.tips);
		return;
	}
	
	var ul = document.createElement('ul');
	var tipsArray = eval('channel_'+channel.id+'.tips');
	
	for(var i = 0;i<tipsArray.length;i++){
		var li = document.createElement('li');
		var temp = tipsArray[i].replace(/{prodPrice}/,channel.tipsProdPrice);
		li.innerHTML = (i+1)+"、" + temp;
		ul.appendChild(li);
	}
	channel.tips = ul;
	tipsView.appendChild(ul);
}

