﻿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/></p>');
	$('name').setAttribute('rule','blank#realName');
	$('name').setAttribute('tip','<p>请一定填写你的<b style="color:red">真实中文姓名</b></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');
	$('accept').setAttribute('rule','check');
	$('accept').setAttribute('error','请阅读开心网服务条款');
	$('ismsn').setAttribute('rule','custom:ismsnok');
	
	XN.ARRAY.each($('regform').gender,function(i,v){
		if(isElement(v)){
			v.setAttribute('rule','selectOne');
			v.setAttribute('error','您还没有选择性别');
		}
	});
	
	window.ismsnok = function(){
		//$('waiting').style.display="none";
		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>'){
			$('msnValid').value = '1';
		}
		$('waiting').style.display="none";
		return true;
	};
	
	check1 = 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';
	}	
	
	document.onkeydown = function(e) {
		e = e || event;
		var c = e.keyCode || e.which;
		if(c == 13) $('regform').submit();
	}
	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;
			}
		}

	}

});

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(delay) {
	if (!delay && delay !== 0) delay = 1000;
	setTimeout(function() {
	$('reg_layer').style.display='block';
	var layerHeight = $('reg_layer').offsetTop;
	var scrollTop = document.body.scrollTop || document.documentElement.scrollTop || window.pageYOffset || 0;
	var windowHeight = window.innerHeight || document.documentElement.clientHeight || 0;
	var top = (windowHeight - layerHeight) / 5 + scrollTop + 'px';
	$('reg_layer').style.top = top;
	XN.effect.slideOpen($('generic_dialog_popup_block'), showPopBack);
	}, delay);
}
function showPopBack() {
	XN.effect.fadeIn($('mask_layer'));
	$('generic_dialog_popup_block').style.display = 'none';
	$('generic_dialog_popup').style.display = 'block';
}




