﻿/*用户报名*/
var studentRegisterFormElementsValidationConfig = {
	trainingName:{/*培训名称*/
		required:true,
		id:'training-name',
		tipId:'',
		type:'select',
		txt:{
			normal:'',
			exist:''
		},
		cls:{
			normal:'',
			notValid:''
		},
		tracelog:{
			change:'',
			isNull:'',
			exist:'',
			notValid:''
		},
		check:function(v,lv,uv){
			
		}
	},
	trainingTime:{/*培训时间*/
		required:true,
		id:'training-time',
		tipId:'',
		type:'select',
		txt:{
			normal:'',
			exist:''
		},
		cls:{
			normal:'',
			notValid:''
		},
		tracelog:{
			change:'',
			isNull:'',
			exist:'',
			notValid:''
		},
		check:function(v,lv,uv){
			//校验
		}
	},
	trainingAddress:{/*培训地点*/
		required:true,
		id:'training-address',
		tipId:'',
		type:'select',
		txt:{
			normal:'',
			exist:''
		},
		cls:{
			normal:'',
			notValid:''
		},
		tracelog:{
			change:'',
			isNull:'',
			exist:'',
			notValid:''
		},
		check:function(v,lv,uv){
			//校验
		}
	},
	studentName:{/*姓名*/
		required:false,
		id:'student-name',
		tipId:'v-student-name',
		type:'input',
		txt:{
			normal:'请填写您的姓名。',
			specialWord:'您的姓名含有特殊字符（@、#、$、%等），请重新填写。',
			exist:''
		},
		cls:{
			normal:'normal',
			notValid:'error',
			correct:'correct'
		},
		tracelog:{
			change:'',
			isNull:'',
			exist:'',
			notValid:''
		},
		check:function(v,lv,uv){
			if(v.indexOf('@') > -1 || v.indexOf('#') > -1 || v.indexOf('$') > -1 || v.indexOf('%') > -1) return 'specialWord';
		}
	},
	phoneNumber:{/*固定电话*/
		required:false,
		id:'phone-number',
		tipId:'v-phone-number',
		type:'input',
		txt:{
			normal:'请填写真实的电话号码，以保证我们的服务人员能够联系上您。',
			notValid:'请您检查号码格式，例如0571-12341234-1234',
			lessLength:'您输入的电话号码少于7位，请重新填写。',
			sameNumber:'请您不要输入相同的数字',	
			exist:''
		},
		cls:{
			normal:'',
			notValid:''
		},
		tracelog:{
			change:'',
			isNull:'',
			exist:'',
			notValid:''
		},
		check:function(v,lv,uv){
			if(!(/^[\d\/\-]+$/.test(v)) || !(/\d/.test(v))) return 'notValid';
			if(v.length < 7) return 'lessLength';
			if(/^(\d)\1+$/.test(v)) return 'sameNumber';
		}
	},
	mobile:{/*手机*/
		required:false,
		id:'mobile',
		tipId:'v-mobile',
		type:'input',
		txt:{
			normal:'请填写真实的电话号码，以保证我们的服务人员能够联系上您。',
			notNumbers:'手机号码只能是数字，请重新填写。',
			exist:''
		},
		cls:{
			normal:'',
			notValid:''
		},
		tracelog:{
			change:'',
			isNull:'',
			exist:'',
			notValid:''
		},
		check:function(v,lv,uv){
			if(!(/^\d+$/.test(v))) return 'notNumbers';
		}
	},
	email:{/*email*/
		required:false,
		id:'email',
		tipId:'v-email',
		type:'input',
		txt:{
			normal:'请填写有效的电子邮箱。',
			notValid:'您输入的电子邮箱格式不正确，请重新填写。如：abc@yahoo.cn',
			exist:''
		},
		cls:{
			normal:'',
			notValid:''
		},
		tracelog:{
			change:'',
			isNull:'',
			exist:'',
			notValid:''
		},
		check:function(v,lv,uv){
			if(!(/^[\w\-]+(\.[\w\-]*)*@[\w\-]+([\.][\w\-]+)+$/.test(v))) return 'notValid';
		}
	},
	areaProvince:{/*省份*/
		required:false,
		id:'area-province',
		tipId:'',
		type:'select',
		txt:{
			normal:'',
			notValid:'',
			exist:''
		},
		cls:{
			normal:'',
			notValid:''
		},
		tracelog:{
			change:'',
			isNull:'',
			exist:'',
			notValid:''
		},
		check:function(v,lv,uv){
			//校验
		}
	},
	areaCity:{/*地级市*/
		required:false,
		id:'area-city',
		tipId:'',
		type:'select',
		txt:{
			normal:'',
			notValid:'',
			exist:''
		},
		cls:{
			normal:'',
			notValid:''
		},
		tracelog:{
			change:'',
			isNull:'',
			exist:'',
			notValid:''
		},
		check:function(v,lv,uv){
			//校验
		}
	},
	address:{/*具体地址*/
		required:false,
		id:'address',
		tipId:'v-address',
		type:'input',
		txt:{
			normal:'请填写具体地址。',
			notValid:'',
			exist:''
		},
		cls:{
			normal:'',
			notValid:''
		},
		tracelog:{
			change:'',
			isNull:'',
			exist:'',
			notValid:''
		},
		check:function(v,lv,uv){
			//校验
		}
	},
	postCode:{/*邮编*/
		required:false,
		id:'post-code',
		tipId:'v-post-code',
		type:'input',
		txt:{
			normal:'请填写邮编号码。',
			notValid:'邮编号码应为6位数字。',
			exist:''
		},
		cls:{
			normal:'',
			notValid:''
		},
		tracelog:{
			change:'',
			isNull:'',
			exist:'',
			notValid:''
		},
		check:function(v,lv,uv){
			if(!(/^\d+$/.test(v))) return 'notValid';
			if(v.length > 6) return 'notValid';
			
		}
	},
	schoolAge:{/*学历*/
		required:false,
		id:'school-age',
		tipId:'',
		type:'select',
		txt:{
			normal:'',
			notValid:'',
			exist:''
		},
		cls:{
			normal:'',
			notValid:''
		},
		tracelog:{
			change:'',
			isNull:'',
			exist:'',
			notValid:''
		},
		check:function(v,lv,uv){
			//校验
		}
	},
	englishLevel:{/*英语水平*/
		required:false,
		id:'english-level',
		tipId:'',
		type:'select',
		txt:{
			normal:'',
			notValid:'',
			exist:''
		},
		cls:{
			normal:'',
			notValid:''
		},
		tracelog:{
			change:'',
			isNull:'',
			exist:'',
			notValid:''
		},
		check:function(v,lv,uv){
			//校验
		}
	},
	expectedAddress:{/*期望培训地点*/
		required:false,
		id:'expected-address',
		tipId:'',
		type:'select',
		txt:{
			normal:'',
			notValid:'',
			exist:''
		},
		cls:{
			normal:'',
			notValid:''
		},
		tracelog:{
			change:'',
			isNull:'',
			exist:'',
			notValid:''
		},
		check:function(v,lv,uv){
			//校验
		}
	}
};
var studentRegisterFormTrackConfig = {
	trainingTime:"enroll_trainTime",
	trainingAddress:"enroll_trainCity",
	studentName:"enroll_userName",
	phoneNumber:"enroll_phone",
	mobile:"enroll_moblie",
	email:"enroll_email",
	areaProvince:"enroll_addrProvince",
	areaCity:"enroll_addrCity",
	address:"enroll_address",
	postCode:"enroll_zipCode",
	schoolAge:"enroll_education",
	englishLevel:"enroll_englishLevel"
};
var studentRegisterFormTrackList=["training-time",
	"training-address",
	"student-name",
	"phone-number",
	"mobile",
	"email",
	"area-province",
	"area-city",
	"address",
	"post-code",
	"school-age",
	"english-level"
];
!function(){
	var Y = YAHOO.util,
		D = Y.Dom,
		E = Y.Event,
		get = D.get;
	var url = "http://stat.china.alibaba.com/alixueyuan_enroll.html";
	YAHOO.namespace("StudentRegistration");
	YAHOO.StudentRegistration={
		initRegisterForm:function(){
			var elementsGroup = studentRegisterFormElementsValidationConfig;
			for(var i in elementsGroup){
				if(!get(elementsGroup[i]['id'])) continue;
				if(elementsGroup[i]['type']=='input'){
					/*input值初始化,如果有设定初始值(约定放在data中),则置为这个初始值*/
					get(elementsGroup[i]['id']).value=get(elementsGroup[i]['id']).getAttribute('data')?get(elementsGroup[i]['id']).getAttribute('data'):'';
					E.on(elementsGroup[i]['id'],'focus',function(e){
						var obj = elementsGroup[D._toCamel(this.id)];
						var tipId = obj['tipId'];
						if(this.className.indexOf("i-error")>-1){
							this.select();
						}else{
							D.addClass(this,"i-text");
							if(get(tipId)){
								if(get(tipId).className.indexOf('correct')==-1){
									D.addClass(tipId,'normal');
									get(tipId).innerHTML = obj['txt']['normal'];
								}
							}
						}
					});
					E.on(elementsGroup[i]['id'],'blur',function(e){
						D.removeClass(this,"i-text");
						var obj = elementsGroup[D._toCamel(this.id)];
						var tipId = obj['tipId'];
						var check = obj.check;
						if(this.value!=''){
							var returnV  = check(this.value);
							if(!returnV){
								if(get(tipId)){
									get(tipId).innerHTML = "正确";
									D.removeClass(tipId,'normal');
									D.removeClass(tipId,'error');
									D.addClass(tipId,'correct');
									D.removeClass(obj['id'],'i-error');
								}
							}else{
								D.removeClass(tipId,'normal');
								D.removeClass(tipId,'correct');
								D.addClass(tipId,'error');
								get(tipId).innerHTML = obj['txt'][returnV];
								D.addClass(obj['id'],'i-error');
							}
						}else{
							D.removeClass(tipId,'normal');
							D.removeClass(tipId,'correct');
							D.removeClass(tipId,'error');
							get(tipId).innerHTML = "";
							D.removeClass(this,"i-error");
						}

					});
				}else if(elementsGroup[i]['type']=='select'){
					E.on(elementsGroup[i]['id'],'change',function(e){
						if(this.options[0].className=='default'){
							if(this.value!=''){
								D.removeClass(this,'default');
							}else{
								D.addClass(this,'default');
							}
						}
					});
				}
			}
		}
	}
	YAHOO.namespace('countryAreaManage');
	YAHOO.countryAreaManage={
		initCountryArea:function(province,city){
			if(typeof conturyAreaData!='undefined'){
				var docFlagment = YAHOO.countryAreaManage.createFlagMent('','省份');
				var defaultValue = get(province).getAttribute("dd");
				var defaultText = "";
				get(city).options.length = 0;
				var i = 0,pos = 0;
				for(var name in conturyAreaData[0]){
					name = name.split('_');
					var opt = document.createElement('option');
					opt.value = name[1];
					opt.text = opt.innerText = name[0];
					if(defaultValue == name[1]){
						pos = i;
						defaultText = name[0];
					} 
					i++;
					docFlagment.appendChild(opt);
				}
				get(province).appendChild(docFlagment);
				setTimeout(function(){
					get(province).selectedIndex = (defaultValue==""?0:pos+1);
				},0);
				
				if(defaultValue!=""){
					YAHOO.countryAreaManage.createCitySelect(defaultText,defaultValue,city);	
				}else{
					var docFlagmentCity = YAHOO.countryAreaManage.createFlagMent('','地级市');
					get(city).appendChild(docFlagmentCity);
				}
				YAHOO.countryAreaManage.provinceSelectChangeEvent(province,city);
			}
		},
		createFlagMent:function(v,t){
			var docFlagment = document.createDocumentFragment(),
			opt = document.createElement('option');
			opt.value = v;
			opt.text = opt.innerText = t;
			docFlagment.appendChild(opt);
			return docFlagment;
		},
		provinceSelectChangeEvent:function(provinceId,cityId){
			E.on(provinceId,'change',function(){
					var t = this.options[this.selectedIndex].text;
					var v = this.options[this.selectedIndex].value;
				YAHOO.countryAreaManage.createCitySelect(t,v,cityId);
			});
		},
		createCitySelect:function(t,v,cityId){
			var docFlagment = YAHOO.countryAreaManage.createFlagMent('','地级市');
			var defaultValue = get(cityId).getAttribute("dd");
				get(cityId).options.length = 0;
				if(v==''){
				}else{
					var key = t+"_"+v;
					var cityStr="";
					var i = 0,pos = 0;
					var cityArray = (conturyAreaData[0][key]);
					get(cityId).options.length = 0;
					for(var j=0;j<cityArray.length;j++){
						cityStr = cityArray[j].split('_');
						var opt = document.createElement('option');
						opt.value = cityStr[1];
						
						opt.text = opt.innerText = cityStr[0];
						if(defaultValue==cityStr[1]){
							pos = i+1;
						}
						i++;
						docFlagment.appendChild(opt);
					}
				}
				get(cityId).appendChild(docFlagment);
				setTimeout(function(){
					get(cityId).selectedIndex = pos?(pos):0;
				},0);
		}
	}

	YAHOO.util.Event.addListener(studentRegisterFormTrackList, "blur", function(e){
		if(this.value.length>0)
		{
			var jsonString='{"'+studentRegisterFormTrackConfig[D._toCamel(this.id)]+'":"'+this.value+'"}';
			try{
				var jsonObject=YAHOO.lang.JSON.parse(jsonString);
				dmtrack.clickstat(url,jsonObject);
			}
			catch(e)
			{
			}
		}
	});

}();