/* 
*@author Leo Ning
*@date 2008-04-09
*/
( function($) {	
	$.cnCity = function(opts){
		$.cnCity.impl.init(null,opts);
	};
	$.cnCity.version=1.1;
	$.fn.cnCity = function(opts){
		$.cnCity.impl.init($(this),opts);
	};
	$.fn.jNumSelect=function(start,end,fn){
		var options='';
		start=start==null?0:start;
		end=end==null?20:end;
		for(var i=start;i<=end;i++){
			options+='<option value="'+i+'">'+i+'</option>';
		}		
		if(typeof(fn)=='string'){
			$(this).attr('onchange',fn+'(this)');
		}
		$(this).append(options);
	};
	$.cnCity.impl = {
		provinceSelects:null,//省列表框
		cities:null,//省与城市
		len:null,//省的个数
		inited:false,
		pc:0,//省计数
		cc:0,//城市计数
		init:function(provinceSelect,opts){
			opts=opts || {};
			if(!this.inited){//如果没有初始化,则初始化
				this.cities=new Array(35);
				this._initCities();
				this.len=this.cities.length;
				this.provinceSelects=new Array();
			}
			var provinceId;
			if(provinceSelect==null){
				provinceId=opts.provinceId==null?'#province':opts.provinceId;
				provinceSelect=$(this.provinceId);
			}else{
				provinceId='#'+provinceSelect.attr('id');
			}
			if(provinceSelect!=null){
				var cityId=opts.cityId==null?'#city':opts.cityId;
				var citySelect=$(cityId);
				if(citySelect!=null){
					citySelect.extend({id:cityId,xchange:typeof(opts.cityChange)=='function'?opts.cityChange:null});
					citySelect.change(this._cityChange);//当市列表框值改变时触发事件
				}
				this.provinceSelects[this.pc++]={id:provinceId,province:provinceSelect,
				xchange:typeof(opts.provinceChange)=='function'?opts.provinceChange:null,
				city:citySelect,defaultCity:opts.defaultCity};						
				provinceSelect.change(this._provinceChange);//当省列表框值改变时触发事件
				this._fillProvince(provinceSelect,opts.defaultProvince);
			}
		},
		_provinceChange:function(){
			var $this=$(this);
			var id='#'+$this.attr('id');
			var selects=$.cnCity.impl.provinceSelects;
			var len=selects.length;
			var select,xchange,citySelect,defaultCity;
			for(var i=0;i<len;i++){
				select=selects[i];
				if(select.id==id){
					xchange     = select.xchange;
					citySelect  = select.city;
					defaultCity = select.defaultCity;
					break;
				}
			}
			if(xchange!=null&&typeof(xchange)=='function'){
				xchange(select.province.val());
			}
			$.cnCity.impl._fillCity(select.province,citySelect,defaultCity);
		},
		_cityChange:function(){
			var $this=$(this);
			var id='#'+$this.attr('id');
			var selects=$.cnCity.impl.provinceSelects;
			var len=selects.length;
			var select;
			for(var i=0;i<len;i++){
				select=selects[i];
				if(select.city.id==id){
					xchange=select.city.xchange;					
					break;
				}
			}			
			if(xchange!=null&&typeof(xchange)=='function'){
				xchange(select.city.val());
			}
		},
		_fillProvince:function(province,defaultProvince){
			var len=this.len;
			var cities_options='';
			var city;
			var dprovince = 'undefined' == typeof defaultProvince ? '' : defaultProvince;
			province.children().remove();
			for(var i=0;i<len;i++){
				city=this.cities[i];
				if(dprovince != city[0]){
					cities_options+='<option value="'+city[0]+'">'+city[0]+'</option>';
				}else{
					cities_options+='<option value="'+city[0]+'" selected = "selected">'+city[0]+'</option>';					
				}
			}
			province.append(cities_options);
			if(dprovince != ''){
				province.change();				
			}			
		},
		_fillCity:function(province,city,defaultCity){
			var local=province.val();
			var len=this.len;
			city.children().remove();
			var cities_options='';
			var xcity,subCities;
			defaultCity = 'undefined' == typeof defaultCity ? '':defaultCity;
			for(var i=0;i<len;i++){
				xcity=$.cnCity.impl.cities[i];
				if(xcity[0] == local){
					subCities=(xcity[1]).split('|');
					len=subCities.length;
					for(var j=0;j<len;j++){
						if(defaultCity != subCities[j]){
							cities_options+='<option value="'+subCities[j]+'">'+subCities[j]+'</option>';
						}else{
							cities_options+='<option value="'+subCities[j]+'" selected="selected">'+subCities[j]+'</option>';
						}
					}
					city.append(cities_options);
					if(defaultCity != ''){
						city.change();
					}
					break;
				}
			}
		},
		_initCities:function(){
			this.inited=true;
			this.cities[0] = ["选择省份","选择城市"];
			this.cities[1] = ["北京市","北京市"];
			this.cities[2] = ["上海市","上海市"];
			this.cities[3] = ["天津市","天津市"];
			this.cities[4] = ["重庆市","重庆市"];
			this.cities[5] = ["河北省","石家庄市"];
			this.cities[6] = ["山西省","太原市"];
			this.cities[7] = ["内蒙古省","呼和浩特市"];
			this.cities[8] = ["辽宁省","沈阳市"];
			this.cities[9] = ["吉林省","长春市"];
			this.cities[10] = ["黑龙江省","哈尔滨市"];
			this.cities[11] = ["江苏省","南京市"];
			this.cities[12] = ["浙江省","杭州市"];
			this.cities[13] = ["安徽省","合肥市"];
			this.cities[14] = ["福建省","福州市"];
			this.cities[15] = ["江西省","南昌市"];
			this.cities[16] = ["山东省","济南市"];
			this.cities[17] = ["河南省","郑州市"];
			this.cities[18] = ["湖北省","武汉市"];
			this.cities[19] = ["湖南省","长沙市"];
			this.cities[20] = ["广东省","广州市"];
			this.cities[21] = ["广西省","南宁市"];
			this.cities[22] = ["海南省","海口市"];
			this.cities[23] = ["四川省","成都市"];
			this.cities[24] = ["贵州省","贵阳市"];
			this.cities[25] = ["云南省","昆明市"];
			this.cities[26] = ["西藏省","拉萨市"];
			this.cities[27] = ["陕西省","西安市"];
			this.cities[28] = ["甘肃省","兰州市"];
			this.cities[29] = ["宁夏省","银川市"];
			this.cities[30] = ["青海省","西宁市"];
			this.cities[31] = ["新疆省","乌鲁木齐"];
			this.cities[32] = ["香港","香港"];
			this.cities[33] = ["澳门","澳门"];
			this.cities[34] = ["台湾","台北"];
			this.cities[35] = ["其它","|北美洲|南美洲|亚洲|非洲|欧洲|大洋洲"];
		}
	};
})(jQuery);
// 按受理地进行跳转，未定义跳转的上海，车险未定义跳转到杭州

function areaFormChange(id, type, value)
{	
   	var url;
   	var initAry = new Array();
   	initAry['北京市'] = ['beij/', 'auto', 'compulsory', 'colligate'];//,'own','abroad','visa'
   	initAry['浙江省'] = ['hz/', 'auto', 'compulsory'];
   	initAry['江苏省'] = ['nanj/', 'auto', 'compulsory'];
   	initAry['广东省'] = ['guangz/', 'auto', 'compulsory'];   	
	if (arr_find(initAry[value], type) != -1) {
		url = '/product/'+initAry[value][0]+type+'/?Command=Index&'+jump;
	} else {
		if ((type == 'auto' || type == 'compulsory') && value != '上海市') {
			url = '/product/hz/'+type+'/?Command=Index&'+jump;
		} else if(type == 'abroad' || type == 'visa' || type == 'home' || type == 'traffic' || type == 'air'){ // 签证 出国 旅游 全国统一
			url = '/product/nationwide/'+type+'/?Command=Index&'+jump;
		}else{
			url = '/fastins/?Command=Index&type='+type+'&'+jump;
		}
	}
	$('#'+id).attr('action', url);
}
function arr_find(arr, d)
{
	if (typeof arr != 'undefined') {
		var length = arr.length;
		for (var i = 0; i < length; i++) {
			if (arr[i] == d) {
				return i;
			}
		}
	}
	return -1;
}
