﻿//XN.DEBUG.On();
XN.DOM.readyDo(function(){
	$('regEmail').setAttribute('rule','blank#regName');
	$('regEmail').setAttribute('tip','<p>重要！请填有效邮箱地址完成注册。</p>');
	$('pwd').setAttribute('rule','blank#length:6,20');
	$('pwd').setAttribute('tip','<p>密码由6-20个英文字母、数字或特殊字符组成<br />密码强度: <span id="pwdStrength"></span><span style="background-image:url(http://xnimg.cn/kaixin/imgpro/reg/pwd.png);background-position:0 -120px;display:block;line-height:5px;height:5px;marign:0;padding:0;" id="pwdScore">&nbsp;</span></p>');
	$('repwd').setAttribute('rule','blank#compare:pwd#length:6,20');
	$('repwd').setAttribute('error','您的两次输入不一致或者不符合密码规则');
	$('name').setAttribute('rule','blank#realName');
	$('name').setAttribute('tip','<p>请一定填写你的<b style="color:red">真实中文姓名</b>，以方便朋友联络<br />默认设置下，只有你的熟人才能看到你的资料<br/></p>');
	if ($('birth_year')) {
		$('birth_year').setAttribute('rule','blank');
		$('birth_year').setAttribute('error','请填写您的出生年份');
		$('birth_month').setAttribute('rule','blank');
		$('birth_month').setAttribute('error','请填写您的出生月份');
		$('birth_day').setAttribute('rule','blank');
		$('birth_day').setAttribute('error','请填写您的出生日期');
	}
	if ($('homeprovince')) {
		$('homeprovince').setAttribute('rule','blank');
		$('homeprovince').setAttribute('error','请您选择省份');
		$('homecitycode').setAttribute('rule','blank');
		$('homecitycode').setAttribute('error','请您选择城市');
	}
	$('icode').setAttribute('rule','blank#verifyCode');
	//$('workplacename').setAttribute('rule','blank#custom:isworkplace');
	$('accept').setAttribute('rule','check');
	$('accept').setAttribute('error','请阅读开心网服务条款');
	$('ismsn').setAttribute('rule','custom:ismsnok');
	/*
	window.isworkplace = function(){
		var str = this.element.value;
		if(!str)return true;
		if(str == '请填写公司名称'){
			XN.FORM.checkers.custom.error = '公司名不能为空';
			return false;
		}
		var req = new XN.NET.xmlhttp({
			url:'/AjaxRegisterWorkplaceAuth.do?authType=workplace&value=' + encodeURIComponent(str) + '&key_id=' + $('key_id').value,
			asynchronous:false
		});
		var msg = XN.STRING.trim(req.transport.responseText);
		if(msg != 'OKWORKPLACE'){
			XN.FORM.checkers.custom.error = msg;
			return false;
		}
		return true;	
	};
	*/
	window.ismsnok = function(){
		//alert($('waiting').innerHTML);
		//$('waiting').style.display="block";
		//alert($('waiting').style.display);
		var req = new XN.NET.xmlhttp({
			url:'/ajxmsnchk.do',
			data:'uAccount='+$('regEmail').value+'&importer=@hotmail.com&pwd=' + $('pwd').value,
			asynchronous:false
		});
		var msg = XN.STRING.trim(req.transport.responseText);
		
		if(msg == '<ret>0</ret>'){
			$('msnAccountOk').value = '1';
			//alert($('msnAccountOk').value);
		}
		//$('waiting').style.display="none";
		//alert('end');
		return true;
	};

	XN.ARRAY.each($('regform').gender,function(i,v){
		if(isElement(v)){
			v.setAttribute('rule','selectOne');
			v.setAttribute('error','您还没有选择性别');
		}
	});
	
	/*
	function toggleWorkplace(){
		var isXiaobai = $('regform').stage[0].checked;
		$('workplacenameContainer')[isXiaobai ? 'show' : 'hide']();
	}
	
	var wks = $('regform').stage;
	XN.ARRAY.each(wks,function(i,v){
		if(isElement(v)){
			v.onclick = toggleWorkplace;
			if(v.value==30)//默认选已工作
				v.checked = true;
		}
	});
	wks[0].setAttribute('rule','check');
	wks[0].setAttribute('error','开心网目前只对已工作人士开放。');
	wks[1].setAttribute('rule','noCheck');
	wks[1].setAttribute('error','友情提示: 开心网目前只对已工作人士开放。');
	wks[2].setAttribute('rule','noCheck');
	wks[2].setAttribute('error','友情提示: 开心网目前只对已工作人士开放。');
	if(XN.STRING.isBlank($('workplacename').value))(new XN.FORM.inputHelper('workplacename')).setDefaultValue('请填写公司名称');
	*/
	
	check2 = new XN.FORM.autoChecker({
		id:'regform',
		alertMethod:'div',
		tipMethod:'tip'
		//submit:'reg_sub'
	});

	XN.FORM.checkPasswordStrength('pwd',function(r){
		if($('pwdScore'))$('pwdScore').style.backgroundPosition = '0 -' + ((5 - r.level) * 30) + 'px';
		if($('pwdStrength'))$('pwdStrength').innerHTML = r.strength;
	});
	
	window.refreshCode = function(){
		var el = $('verifyPic');
		if(XN.BROWSER.IE6){
			new Image().src = el.src + 'x';
		}
		el.src += 'x';
	}
	
//	if ($('regEmail')) {
//		$('regEmail').focus();
//	}

	XN.event.addEvent('birth_month' , 'change' , function()	{
		chageDate();
	})
	window.chageDate = function() {
		var year = parseInt($('birth_year').value);
		var month = parseInt($('birth_month').value);
		var day = parseInt($('birth_day').value);
		var monthLength = 30;
		_o = parseFloat(year)/4;
		if (XN.array.include([1, 3, 5,  7, 8, 10, 12],month)) {
			monthLength = 31;
		} 
		else if (month == 2) {
			monthLength =  (_o == Math.ceil(_o)) ? 29 : 28;
		}
		$('birth_day').options.length = 0;
		
		$('birth_day').options[0] = new Option('--', '');
		for (var i = 0; i < monthLength; i++) {
			if ((i + 1) < 10){
			$('birth_day').options[i + 1] = new Option('0' + (i +1), '0' + (i +1));
			}
			else if ((i + 1) > 9){
			$('birth_day').options[i + 1] = new Option(i +1, i +1);
			}
			else
			if (i +1 == day) {
				$('birth_day').options[i + 1].selected = true;
			}
		}

	}
	document.onkeydown = function(e) {
		e = e || event;
		var c = e.keyCode || e.which;
		if(c == 13) $('regform').submit();
	}
});

XN.effect.fadeIn = function( element,callBack )
{
	if(element.fadetimer)return;
	callBack = callBack || XN.FUNC.empty;
	var originalOpacity = XN.element.getStyle(element, 'opacity');
	var op = 0;
	element.setOpacity(0);
	element.style.display = '';
	element.fadetimer = setInterval(function(){
		XN.Element.setOpacity(element,(op += 0.15));
		if(op >= originalOpacity){
			clearInterval(element.fadetimer);
			element.fadetimer = null;
			callBack(element);
		}
	},50);
};
XN.effect.slideOpen = function(element, callBack){
	if(element.slidetimer)return;
	if(!element.slideHeight || !element.slideWidth){
		element.show();
		element.slideHeight = element.offsetHeight;
		element.slideWidth = element.offsetWidth;
		element.slideTop = element.offsetTop;
		element.hide();
	}
	var eh = element.slideHeight,h = 0, ew = element.slideWidth,w = 0, _ch;
	var stepH = parseInt(eh / 10);
	var stepW = parseInt(ew / 10);
	element.style.height = element.style.width = '0px';
	element.style.display = '';
	element.style.overflow = 'hidden';
	element.slidetimer = setInterval(function(){
		_ch = (h += stepH);
		element.style.height = _ch + 'px';
		element.style.width = (w += stepW) + 'px';
		element.style.top = (element.slideHeight - _ch)/2 + 'px';
		if(h >= eh){
			clearInterval(element.slidetimer);
			element.slidetimer = null;
			element.style.height = eh;
			element.style.width = ew;
			element.style.top = '0px';
			callBack();
		}
	},10);
};	

function refreshCode() {
	$('verifyPic').src = $('verifyPic').src + Math.random();
}
function showPop() {
	$('reg_layer').style.display='block';
	XN.effect.slideOpen($('generic_dialog_popup_block'), showPopBack);
}
function showPopBack() {
	XN.effect.fadeIn($('mask_layer'));
	$('generic_dialog_popup_block').style.display = 'none';
	$('generic_dialog_popup').style.display = 'block';
}

