﻿// 二级select
// 用ajax获得select的options值，并将其添加到指定select中

//通过Ajax获得更新的select类，当select1变动时select2相应变动
//url: 请求的页面地址
//paramName: 参数名
function AjaxSelect(select1, select2, url, paramName) {
	if (typeof select1 == "string")
		select1 = $(select1);
	if (typeof select2 == "string")
		select2 = $(select2);
	
	this.s1 = select1;
	this.s2 = select2;
	this.u = url;
	this.p = paramName;
	this.v = this.s1.options[this.s1.selectedIndex].value;
	
	var _this = this;
	this.s1.onchange = function () {
		_this.chg(_this);
	};
}

AjaxSelect.prototype.chg = function (_this) {
	_this.v = _this.s1.options[_this.s1.selectedIndex].value;
	
	var myAjax = new Ajax.Request(
		_this.u, {
			method: "get",
			parameters: _this.p + "=" + _this.v,
			onComplete: function (req) {
				var r = req.responseText;
				var a = r.split(",");
				
				while(_this.s2.options.length > 0) {
					_this.s2.remove(0);
				}
				_this.s2.options.add(new Option("请选择机型","-1"));
				
				for(var i = 0, j = a.length; i < j; i += 2)
					_this.s2.options.add(new Option(a[i+1], a[i]));
			}
		});
};

/*function selectPhoneType()
{
	var s1 = $("uPhoneMarkRegClt");
	var v = s1.options[s1.selectedIndex].value;
	
	var u;
	u =  "useforall.aspx";
	var p = "action=showphonetype&trademark=" + v + "&r=" + Math.random();
	var myAjax = new Ajax.Request(
		u,
		{
		method: 'get',
		parameters: p,
		onComplete: selectPhoneType2
		});
	
	return false;
}

function selectPhoneType2(originalRequest)
{
	var rsp = originalRequest.responseText;
	var a = rsp.split(",")
	
	var s2 = $("uPhoneTypeRegClt");
	var i;
	while(s2.options.length > 0)
	{
		s2.options.remove(0);
	}
	s2.options.add(new Option("请选择机型","-1"));
	
	var j = a.length;
	for(i = 0; i < j; i += 2)
	{
		s2.options.add(new Option(a[i + 1], a[i]));
	}
	
	return false;
}*/