﻿/**
 * CascadeMenu
 */
function CascadeMenu(brandSelectElementId, seriesSelectElementId, modelSelectElementId){
	this.beName = brandSelectElementId;
	this.seName = seriesSelectElementId;
	this.meName = modelSelectElementId;
	
	this.root = '/static/xml';
	
	// init
	this.init = function(){
		this.be = $(brandSelectElementId);
		this.se = $(seriesSelectElementId);
		this.me = $(modelSelectElementId);
	};
	
	// init & loadBrandMenu
	this.load = function(){
		this.init();
		this.loadBrandMenu();
	};
	
	// load brand menu
	this.loadBrandMenu = function(){
		var selectElement = this.be;
		selectElement.options.add(new Option('---国产品牌---','-1'));
		
		var ajax = new Ajax.Request(
			this.root+'/brands.xml',
			{
				method:'get', 
				parameters:'',
				onComplete:function(originalRequest){
								var xmlDoc = originalRequest.responseXML;
								//alert(originalRequest.responseText);
								var brandList = xmlDoc.documentElement.getElementsByTagName('brand');
								var flag = 0;
								for(i = 0 ; i < brandList.length ; i ++){
									var id = brandList[i].getElementsByTagName('id')[0].childNodes[0].nodeValue;
									var name = brandList[i].getElementsByTagName('name')[0].childNodes[0].nodeValue;
									var seriescount = brandList[i].getElementsByTagName('seriescount')[0].childNodes[0].nodeValue;
									var nation = brandList[i].getElementsByTagName('nation')[0].childNodes[0].nodeValue;
									var index = brandList[i].getElementsByTagName('index')[0].childNodes.length;
									if(index > 0)
										index = brandList[i].getElementsByTagName('index')[0].childNodes[0].nodeValue;
									
									if(flag == 0 && nation == '1'){
										flag = 1;
											selectElement.options.add(new Option('---国际品牌---','-1'));
									}
									if(seriescount != '0')
										selectElement.options.add(new Option(index+' '+name, id));
								}// end for
		}});
	};
	
	
	// load series menu
	this.loadSeriesMenu = function(){
		var brandId = this.be.options[this.be.selectedIndex].value;
		
		this.cleanSeriesOptions();
		this.se.options.add(new Option('选择系列','-1'));
		
		this.cleanModelOptions();
		this.me.options.add(new Option('选择型号','-1'));

		var selectElement = this.se;
		var ajax = new Ajax.Request(this.root+'/brand-'+brandId+'.xml',{method:'get', parameters:'',onComplete:function(originalRequest){
			var xmlDoc = originalRequest.responseXML;
			
			var seriesList = xmlDoc.documentElement.getElementsByTagName('series');
			for(i = 0 ; i < seriesList.length ; i ++){
				var id = seriesList[i].getElementsByTagName('id')[0].childNodes[0].nodeValue;
				var name = seriesList[i].getElementsByTagName('name')[0].childNodes[0].nodeValue;
				var modelcount = seriesList[i].getElementsByTagName('modelcount')[0].childNodes[0].nodeValue;
				if(modelcount != '0')
					selectElement.options.add(new Option(name, id));
			}
		}});
	};
	
	// load model menu
	this.loadModelMenu = function(){
		var seriesId = this.se.options[this.se.selectedIndex].value;
		
		this.cleanModelOptions();
		this.me.options.add(new Option('选择型号','-1'));
		
		var selectElement = this.me;
		var ajax = new Ajax.Request(this.root+'/series-'+seriesId+'.xml',{method:'get', parameters:'',onComplete:function(originalRequest){
			
			var xmlDoc = originalRequest.responseXML;
			var modelList = xmlDoc.documentElement.getElementsByTagName('model');
			
			for(i = 0 ; i < modelList.length ; i ++){
				var id = modelList[i].getElementsByTagName('id')[0].childNodes[0].nodeValue;
				var name = modelList[i].getElementsByTagName('name')[0].childNodes[0].nodeValue;
					selectElement.options.add(new Option(name, id));
			}// end for
		}});
	};
	
	// clean select element
	this.cleanBrandOptions = function(){
		this.be.options.length=0;
	};
	this.cleanSeriesOptions = function(){
		this.se.options.length=0;
	};
	this.cleanModelOptions = function(){
		this.me.options.length=0;
	};
}
