$(function () {

	// form validate
	$('#regFrm').validate({
		rules: {
			/**/
			username: {
				fn: function (key, validator){
					value = validator[key].value;
					if (value == ''){
						return validator.show('error', validator[key]);
					}
					if (!/^[a-z]\w{3,14}$/i.test(value)) {
						return validator.show('error', validator[key]);
					}
					
					$.post('service/index.php', {'m' : 'reg', 'a' : 'username', 'username' : encodeURIComponent(value)},function (data) {
						return validator.show(data, validator[key]);
					});
				},
				messages:{
					prompt: '由4-15个字母或数字组成，不支持中文，不区分大小写，注册成功后不可修改。',
					error: '您的填写有误。会员登录名只能由4-15个英文字母或数字组成(不支持中文、不能以数字开头)。',
					exist: '用户名已经存在'
				}
			},
			password: {
				fn: function (key, validator){
					value = validator[key].value;
					if (value == ''){
						return validator.show('error', validator[key]);
					}
					if (!/^\w{6,20}$/i.test(value)) {
						return validator.show('error', validator[key]);
					}
					return validator.show('success', validator[key]);
				},
				messages:{
					prompt: '6-20个字母（区分大小写）或数字组成。',
					error: '您输入的密码有误。密码是由6-20位英文字母和数字组成的，区分大小写!'
				}
			},
			password1: {
				fn: function (key, validator){
					value = validator[key].value;
					if (value == ''){
						return validator.show('error', validator[key]);
					}
					if (!/^\w{6,20}$/i.test(value)) {
						return validator.show('error', validator[key]);
					}
					if(value != validator.password.value){
						return validator.show('error1', validator[key]);
					}
					return validator.show('success', validator[key]);
				},
				messages:{
					prompt: '请再输入一遍上面填写的密码。',
					error: '您输入的密码有误。密码是由6-20位英文字母和数字组成的，区分大小写!',
					error1: '两次输入的密码不相同，请重新输入上面的密码。'
				}
			},
			realname: {
				fn: function (key, validator){
					value = validator[key].value;
					if (value == ''){
						return validator.show('error', validator[key]);
					}
					return validator.show('success', validator[key]);
				}
			},
			job: {
				fn: function (key, validator){
					value = validator[key].value;
					if (value == ''){
						return validator.show('error', validator[key]);
					}
					return validator.show('success', validator[key]);
				}
			},
			email: {
				fn: function (key, validator){
					value = validator[key].value;
					if (value == ''){
						return validator.show('error', validator[key]);
					}
					
					$.post('service/index.php', {'m' : 'reg', 'a' : 'email', 'email' : encodeURIComponent(value)},function (data) {
						return validator.show(data, validator[key]);
					});
				},
				messages:{
					prompt: '这是客户与您联系的首选方式，请谨慎填写。',
					illegal: '电子邮件格式不正确，请重新输入正确的电子邮箱格式。',
					exist: '电子邮件已经存在'
				}
			},
			mobile: {
				fn: function (key, validator){
					value = validator[key].value;
					if (value == ''){
						return;
					}
					
					if (!/^[0-9]{11}$/i.test(value)) {
						return validator.show('error', validator[key]);
					}
					return validator.show('success', validator[key]);
				},
				messages:{
					prompt: '方便客户及时联系到您！中铝网不绑定任何收费服务。',
					error: '请输入11位数的阿拉伯数字！'
				}
			},
			'cbcorpmodel[]': {
				fn: function (key, validator){
					value = validator[key].value;
					if (value == '')
					{
						return validator.show('error', validator[key]);
					}
					return validator.show('success', validator[key]);
				}
			},
			corpname: {
				fn: function (key, validator){
					value = validator[key].value;
					if (value == ''){
						return validator.show('error', validator[key]);
					}
					return validator.show('success', validator[key]);
				},
				messages:{
					prompt: '请填写企业全称，个体经营者请填写执照上的姓名，并标注个体经营，如：李四（个体经营）。'
				}
			},
			addr: {
				fn: function (key, validator){
					value = validator[key].value;
					if (value == ''){
						return validator.show('error', validator[key]);
					}
					return validator.show('success', validator[key]);
				},
				messages:{
					prompt: '请详细填写贵公司经营地址。如“中国.北京.海淀区西直门北大街42号1001信箱。”'
				}
			},
			corpindustry: {
				fn: function (key, validator){
					value = validator[key].value;
					if (value == ''){
						return validator.show('error', validator[key]);
					}
					return validator.show('success', validator[key]);
				},
				messages:{
					prompt: '为了给您提供匹配的产品信息，请点击选择贵公司的主营行业关键字。'
				}
			},
			validatecode: {
				fn: function (key, validator){
					value = validator[key].value;
					if (value == ''){
						return validator.show('error', validator[key]);
					}
					
					$.post('service/index.php', {'m' : 'reg', 'a' : 'validateCode', 'code' : encodeURIComponent(value)},function (data) {
						return validator.show(data, validator[key]);
					});
					return validator.show('success', validator[key]);
				},
				messages:{
					prompt: '请将左侧图片的数字准确地填写到文本框中。',
					illegal: '验证码输入错误!',
					expiration: '验证码过期，请刷新页面重试！'
				}
			}
			
		},
		fn: function () {
			$('input.phone[type=text]').blur();
			$('input.fax[type=text]').blur();
			$('td.location select').blur();
			$('td.location input[name=city]').blur();
		}
	});


	// 特殊验证
	$.each(['phonecountry', 'phonearea', 'faxcountry', 'faxarea', 'mobile'], function() {
		$('#' + this).keyup(function () {
			$(this).val($(this).val().replace(/[^0123456789]/g,''));
		}).bind('beforepaste', function (){
			clipboardData.setData('text',clipboardData.getData('text').replace(/[^0123456789]/g,''));
		});
		if((this == 'phonearea' || this == 'faxarea') && !/^[0-9]+$/i.test($('#' + this).val())){
			$('#' + this).val('区号').css('color', '#808080');
		}
	});
	
	$.each(['phone', 'fax'], function() {
		$('#' + this).keyup(function () {
			$(this).val($(this).val().replace(/[^0123456789\/\-]/g,''));
		}).bind('beforepaste', function (){
			clipboardData.setData('text',clipboardData.getData('text').replace(/[^0123456789\/\-]/g,''));
		});
		if(this == 'phone' && !/^[0-9|\-|\/]+$/i.test($('#' + this).val())){
			$('#' + this).val('固定电话').css('color', '#808080');
		}
		if(this == 'fax' && !/^[0-9|\-|\/]+$/i.test($('#' + this).val())){
			$('#' + this).val('传真号码').css('color', '#808080');
		}
	});
	
	// 固定电话
	
	$('input.phone[type=text]').focus(function () {
		var value = $(this).val();
		if (!/^[0-9|\-|\/]+$/i.test(value)) {
			$(this).val('').css('color', '#000');
			$('label[generated=true][for=phone]').text('多个号码，请用“/”分隔；分机号码，请用“-”分隔。').attr('class', 'prompt');
		}
	});
	
	$('input.phone[type=text]').blur(function () {
		var value = $(this).val();
		var name = $(this).attr('name');
		if (!/^[0-9|\-|\/]+$/i.test(value)) {
			if(name == 'phonearea'){
				$(this).css('color', '#808080').val('区号');
			} else if(name == 'phone'){
				$(this).css('color', '#808080').val('固定电话');
			}
			$('label[generated=true][for=phone]').text('此项为必填项！多个号码，请用“/”分隔；分机号码，请用“-”分隔。').attr('class', 'error');
		}
		if(/^[0-9]+$/i.test($('#phonecountry').val()) && /^[0-9]+$/i.test($('#phonearea').val()) && /^[0-9|\-|\/]+$/i.test($('#phone').val())){
			$('label[generated=false][for=phone]').addClass('label');
			$('label[generated=true][for=phone]').text('填写正确。').attr('class', 'success');
		}
		
	});
	
	// 传真
	
	$('input.fax[type=text]').focus(function () {
		var value = $(this).val();
		if (!/^[0-9|\-|\/]+$/i.test(value)) {
			$(this).val('').css('color', '#000');
			$('label[generated=true][for=fax]').text('多个号码，请用“/”分隔；分机号码，请用“-”分隔。').attr('class', 'prompt');
		}
	});
	
	$('input.fax[type=text]').blur(function () {
		var value = $(this).val();
		var name = $(this).attr('name');
		if (!/^[0-9|\-|\/]+$/i.test(value)) {
			if(name == 'faxarea'){
				$(this).css('color', '#808080').val('区号');
			} else if(name == 'fax'){
				$(this).css('color', '#808080').val('传真号码');
			}
		}
		if(/^[0-9]+$/i.test($('#faxcountry').val()) && /^[0-9]+$/i.test($('#faxarea').val()) && /^[0-9|\-|\/]+$/i.test($('#fax').val())){
			$('label[generated=false][for=fax]').addClass('label');
			$('label[generated=true][for=fax]').text('填写正确。').attr('class', 'success');
		}
		
	});
	
	// 公司性质
	var changecorpmodel = function () {
		var arrName = [];
		var arrID = [];
		$("input[type=checkbox][name=cbcorpmodel[]]:checked").each(function () {
			var iid = $(this).val();
			arrID.push(iid);
			arrName.push($('label[for=corpmodel' + iid + ']').text());
		});
		$('#corpmodel').val(arrID.join(','));
		$('#corpmodelname').val(arrName.join(','));
	}
	changecorpmodel();
	$("input[type=checkbox][name=cbcorpmodel[]]").click(function () {
		changecorpmodel();
	});

	// 公司所在地
	var countrycode = $('#countrycode');
	countrycode.attr('options').length = 0;
	$.each(china, function (key, value){
		countrycode.attr('options').add(new Option(value, key)); 
	});
	$('#countrycode').width(110);
	$('label[for=countrycode]').hide();
	$('#countrycode').val('156');
	$('#provincecode').show().attr('options').length = 1;
	$('#citycode').show().attr('options').length = 1;
	$('#location').show().attr('options').length = 1;
	$.each(province156, function (key, value){
		$('#provincecode').attr('options').add(new Option(value, key));
	});
	$('#province').hide().val('');
	$('label[for=province]').hide();
	$('#city').hide().val('');
	$('label[for=city]').hide();
	$('#countrycode').change(function (){
		var sel = $(this).val();
		if ((sel == '156' || sel == '446' || sel == '158' || sel == '344') && $(this).attr('options').length != 5){
			$(this).width(110);
			$('label[for=countrycode]').hide();
			$(this).attr('options').length = 0;
			
			$.each(china, function (key, value){
				$('#countrycode').attr('options').add(new Option(value, key)); 
			});
			$(this).val(sel);
		} else if (sel == '000000' && $(this).attr('options').length == 5){
			$(this).width(140);
			$('label[for=countrycode]').show();
			$(this).attr('options').length = 0;
			$.each(country, function (key, value){
				$('#countrycode').attr('options').add(new Option(value, key)); 
			});
			$('#phonecountry').val('');
			$('#faxcountry').val('');
			$('#phonearea').val('');
			$('#faxarea').val('');
		}
		
		if (sel == '156'){
			$('#province').hide().val('');
			$('label[for=province]').hide();
			$('#city').hide().val('');
			$('label[for=city]').hide();
			$('#provincecode').show().attr('options').length = 1;
			$('#citycode').show().attr('options').length = 1;
			$('#location').show().attr('options').length = 1;
			$.each(province156, function (key, value){
				$('#provincecode').attr('options').add(new Option(value, key));
			});
		} else if (sel == '446' || sel == '158' || sel == '344') {
			$('#province').hide().val('');
			$('label[for=province]').hide();
			$('#provincecode').hide().attr('options').length = 1;
			$('#citycode').hide().attr('options').length = 1;
			$('#location').hide().attr('options').length = 1;
			$('#city').show().val('');
			$('label[for=city]').show();
		} else {
			$('#provincecode').hide().attr('options').length = 1;
			$('#citycode').hide().attr('options').length = 1;
			$('#location').hide().attr('options').length = 1;
			$('#province').show().val('');
			$('label[for=province]').show();
			$('#city').show().val('');
			$('label[for=city]').show();
		}
		
		if(sel != '000000'){
			$.post('service/index.php', {'m' : 'reg', 'a' : 'phoneCountry', 'country' : encodeURIComponent(sel)},function (data) {
				if(parseInt(data) != 0){
					$('#phonecountry').val(data);
					$('#faxcountry').val(data);
				} else {
					$('#phonecountry').val('');
					$('#faxcountry').val('');
				}
			});
		}
	});

	$('#provincecode').change(function (){
		var sel = $(this).val();
		if (sel != '000000'){
			var province = sel.substr(0, 2);
			$('#citycode').attr('options').length = 1;
			$('#location').attr('options').length = 1;
			$.each(city156, function (key, value){
				if (province == key.substr(0, 2)){
					$('#citycode').attr('options').add(new Option(value, key));
				}
			});
		} else {
			$('#citycode').attr('options').length = 1;
			$('#location').attr('options').length = 1;
		}
	});
	$('#citycode').change(function (){
		var sel = $(this).val();
		if (sel != '000000'){
			var city = sel.substr(0, 4);
			$('#location').attr('options').length = 1;
			$.each(county156, function (key, value){
				if (city == key.substr(0, 4)){
					$('#location').attr('options').add(new Option(value, key));
				}
			});
		} else {
			$('#location').attr('options').length = 1;
		}
	});
	
	$('td.location select').blur(function () {
		if (($('#countrycode').val() != '156' && $('#city').val() != '') || ($('#countrycode').val() == '156' && $('#provincecode').val() != '000000' && $('#citycode').val() != '000000' && $('#location').val() != '000000')){
			$('label[generated=false][for=location]').addClass('label');
			$('label[generated=true][for=location]').text('填写正确。').attr('class', 'success');
		} else {
			$('label[generated=true][for=location]').text('此项为必填项。').attr('class', 'error');
		}
	});
	$('td.location input[name=city]').blur(function () {
		if (($('#countrycode').val() != '156' && $('#city').val() != '') || ($('#countrycode').val() == '156' && $('#provincecode').val() != '000000' && $('#citycode').val() != '000000' && $('#location').val() != '000000')){
			$('label[generated=false][for=location]').addClass('label');
			$('label[generated=true][for=location]').text('填写正确。').attr('class', 'success');
		} else {
			$('label[generated=true][for=location]').text('此项为必填项。').attr('class', 'error');
		}
	});

	// 验证码
	$('#validate').click(function () {
		$(this).attr('src', '/service/index.php?m=validateCode&update=' + Math.random());
	});
/**/
});
