var fsgoo = {
	tmp : new Array(),
	verify_refresh : function()
	{
		$('#verify_img').attr('src', '/verify/?'+Math.random());
	},
	verify_refresh1 : function()
	{
		$('#verify_img11').attr('src', '/verify/?cc=1&'+Math.random());
	},
	verify : function()
	{
		document.write("<img src='/verify/?"+Math.random()+"' border='0' id='verify_img' title='点击刷新' alt='验证码' style='cursor:pointer;'> <a href='#' onclick='fsgoo.verify_refresh();return false;'>看不清？换一张</a>");
		$('#verify_img').click(function(){fsgoo.verify_refresh();});
	},
	verifyone : function()
	{
		document.write("<img src='/verify/?cc=1&"+Math.random()+"' border='0' id='verify_img11' title='点击刷新' alt='验证码' style='cursor:pointer;'>");
		$('#verify_img11').click(function(){fsgoo.verify_refresh1();});
	},
	
	//登录表单验证
	chkfrm_login : function(frm)
	{
		var arr = ['username','请输入用户名','password','请输入密码','verify','请输入验证码'];
		if(this.chkfrm(frm,arr)===false){
			return false;
		}
		frm.submit.value = '正在提交...';
		$(frm.submit).attr('disabled', true);
	},
	reg : {
		can_submit : 1,
		init : function(){
			var frm = document.getElementById('form_reg');
			if(!frm)return ;
			$(frm.username).blur(function(){fsgoo.reg.chk_username(this.value);});
			$(frm.password).blur(function(){fsgoo.reg.chk_password(this.value);});
			$(frm.password2).blur(function(){fsgoo.reg.chk_password2(this.value,frm.password.value);});
			$(frm.email).blur(function(){fsgoo.reg.chk_email(this.value);});
			$(frm.sex).each(function(){
				$(this).click(function(){
					$('#sex_err').html('<img src="/images/img60.gif">');
				});
			});
			$(frm).submit(function(){
				if(fsgoo.reg.can_submit==-2)return false;
				fsgoo.reg.can_submit=1;
				if(this.username.value==''){
					{$('#username_err').html('<img src="/images/img59.gif"> 用户名不能为空');}
					fsgoo.reg.can_submit=-1;
				}
				fsgoo.reg.chk_password(this.password.value);
				fsgoo.reg.chk_password2(this.password2.value, this.password.value);
				fsgoo.reg.chk_email(this.email.value);
				if($(this.sex)[0].checked===false && $(this.sex)[1].checked===false){
					$('#sex_err').html('<img src="/images/img59.gif"> 请选择性别');
					fsgoo.reg.can_submit=-1;
				}else{
					$('#sex_err').html('<img src="/images/img60.gif">');
				}
				if(frm.year.value=='' || frm.month.value=='' || frm.day.value==''){
					$('#birthday_err').html('<img src="/images/img59.gif"> 请选择出生日期');
					fsgoo.reg.can_submit=-1;
				}else{
					$('#birthday_err').html('<img src="/images/img60.gif">');
				}
				if(frm.province.value=='' || frm.city.value==''){
					$('#location_err').html('<img src="/images/img59.gif"> 请选择地区');
					fsgoo.reg.can_submit=-1;
				}else{
					$('#location_err').html('<img src="/images/img60.gif">');
				}
				if(frm.verify.value==''){
					$('#verify_err').html('<img src="/images/img59.gif"> 请输入验证码');
					fsgoo.reg.can_submit=-1;
				}else{
					$('#verify_err').empty();
				}
				if(frm.xieyi.checked==false){
					$('#xieyi_err').html('<img src="/images/img59.gif"> 请同意思注册协议');
					fsgoo.reg.can_submit=-1;
				}else{
					$('#xieyi_err').empty();
				}
				if(fsgoo.reg.can_submit==1){
					this.submit.value = '正在提交...';
					$(this.submit).attr('disabled', true);
					return true;
				}
				else return false;
			});
		},
		//在注册的时候，检测用户名是否可用
		chk_username : function(username){
			if($.trim(username)==''){
				$('#username_err').html('<img src="/images/img59.gif"> 用户名不能为空');
				this.can_submit = -1;
				return flase;
			}
			if(!username.match(/^[^\.\\,"'/\(\)<>\?\|\^%\$#!@\*&=+\-\]\[\{\}\f\n\r\t\v\s\b]{3,16}$/)){
				$('#username_err').html('<img src="/images/img59.gif"> 3-16个字符长度，由中文、数字、字母及下划线组成');
				this.can_submit = false;
				return false;
			}
			jQuery.ajax({
				type : "POST",
				url : "/user/chkusername/",
				data : "username="+username,
				success : function(data){
					if(data==0){
						$('#username_err').html('<img src="/images/img60.gif">');
						fsgoo.reg.can_submit = 1;
					}else if(data==1){
						$('#username_err').html('<img src="/images/img59.gif"> 此用户名已经被占用');
						fsgoo.reg.can_submit = -2;
					}else if(data==-1){
						$('#username_err').html('<img src="/images/img59.gif"> 3-16个字符长度，由中文、数字、字母及下划线组成');
						fsgoo.reg.can_submit = -1;
					}
				}
			});
		},
		chk_password : function(val){
			if(val.length<6 || val.length>16){
				$('#password_err').html('<img src="/images/img59.gif"> 密码长度要求为6-16位字符');
				fsgoo.reg.can_submit = -1;
			}else{
				if(fsgoo.reg.can_submit ==0)fsgoo.reg.can_submit=1;
				$('#password_err').html('<img src="/images/img60.gif">');
			}	
		},
		chk_password2 : function(val,val2){
			if(val==''){
				$('#password2_err').html('<img src="/images/img59.gif"> 确认密码不能为空');
				fsgoo.reg.can_submit = -1;
				return false;
			}
			if(val != val2){
				$('#password2_err').html('<img src="/images/img59.gif"> 两次输入的密码不一致');
				fsgoo.reg.can_submit = -1;
			}else{
				if(fsgoo.reg.can_submit ==0)fsgoo.reg.can_submit=1;
				$('#password2_err').html('<img src="/images/img60.gif">');
			}	
		},
		chk_email : function(val){
			if(!val.match(/^[0-9a-z\-\._]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3}$/i)){
				$('#email_err').html('<img src="/images/img59.gif"> 邮箱地址格式不对');
				fsgoo.reg.can_submit = -1;
			}else{
				if(fsgoo.reg.can_submit ==0)fsgoo.reg.can_submit=1;
				$('#email_err').html('<img src="/images/img60.gif">');
			}
		}
	},
	//找回密码
	chkfrm_findpassword : function(frm)
	{
		var arr = ['username','请输入用户名','email','请输入email','verify','请输入验证码'];
		if(!this.chkfrm(frm,arr))return false;
		if(!frm.email.value.match(/^[0-9a-z\-\._]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3}$/i)){
			alert('邮箱地址格式不对');
			frm.email.focus();
			return false;
		}
	},
	//重设密码
	chkfrm_resetpassword : function(frm)
	{
		var arr = ['newpassword','请输入新密码','newpassword2','请输入确认密码','verify','请输入验证码'];
		if(!this.chkfrm(frm,arr))return false;
		if(frm.newpassword.value != frm.newpassword2.value){
			alert('两次输入的密码不一致');
			frm.newpassword.value=frm.newpassword2.value='';
			return false;	
		}
	},
	chkfrm_code : function(frm){
		var arr = ['code', '请输入code','verify','请输入验证码'];
		if(!this.chkfrm(frm,arr))return false;
		if(!frm.code.value.match(/^[\d]{12,13}$/i)){
			alert('code错误');
			return false;
		}
	},
	chkfrm : function(frm,arr)
	{
		for(var i=0; i<arr.length; i+=2){
			eval('var obj=frm.'+arr[i]);
			if(!this.chk_frm_element(obj, arr[i+1]))return false;
		}
		return true;
	},
	chk_frm_element : function(obj, msg)
	{
		if($.inArray($(obj).attr('type'), ['radio','checkbox'])!=-1){
			var flag = false;
			$(obj).each(function(){if(this.checked==true)flag=true;});
			if(!flag)alert(msg);
			return flag;
		}else if($.trim(obj.value)==''){
			alert(msg);
			obj.focus();
			return false;
		}
		return true;
	},
	change_city : function(province_id)
	{
		var city = document.getElementById('city');
		city.style.display='';
		if(typeof(city_id)=='undefined'){
			city_id = '';	
		}
		if(city){
			city.options.length = 1;
			for(var i=0; i<city_arr.length; i++){
				if(city_arr[i][2]==province_id){
					if(city_id==city_arr[i][0])city.options.add(new Option(city_arr[i][1],city_arr[i][0],true,true));
					else city.options.add(new Option(city_arr[i][1],city_arr[i][0]));
				}
			}
		}
	},
	//自动跳转
	auto_go2 : function(url, time)
	{
		setTimeout("fsgoo.go2('"+url+"')", time);
	},
	go2 : function(url)
	{
		window.location.href = url;	
	},
	chkfrm_gifts_search2 : function(frm)
	{
		$('#expire').val('');
		$(frm).find('input').each(function(){
			if(this.type=='checkbox'){
				if(this.checked){
					$('#expire').val($('#expire').val()+this.value+',');
				}
			}
		});
	},
	chkfrm_gifts_confirm : function(frm)
	{
		if(frm.verify.value==''){
			alert('请输入验证码');
			frm.verify.focus();
			return false;	
		}
		$('#submit').val('正在提交...');
		$('#submit').attr('disabled', true);
		var data = 'verify='+frm.verify.value+'&token='+frm.token.value;
		if(frm.qq_num)data += '&qq_num='+frm.qq_num.value;
		this.showMsgBox(frm.action, data, 'post');
		return false;
	},
	chkfrm_gifts_confirm_userinfo : function(frm)
	{
		$('#submit').val('正在提交...');
		$('#submit').attr('disabled', true);
		var data = "truename="+frm.truename.value+"&address="+frm.address.value+"&zip="+frm.zip.value+"&tel="+frm.tel.value+"&mobile="+frm.mobile.value;
		this.showMsgBox(frm.action, data, 'post');
		return false;
	},
	showMsgBox : function(url,data,type)
	{
		$.ajax({
			type: type,
			url : url,
			data:data,
			dataType:"json",
			success : function(data){
				if(data.flag){
					$.unblockUI();
					$.blockUI({
						message:data.msg,
						css: {
				            border: 'none',
							width:'auto',
							height:'auto'
			       		}
					});
					$('.blockMsg').css('cursor','').css('left',($(window).width() - $('.blockMsg').width()) / 2 + 'px').css('top',($(window).height() - $('.blockMsg').height()) / 2 + 'px');
					$('.blockOverlay').click($.unblockUI); 
				}else{
					alert(data.msg);
					$('#submit').val('提交');
					$('#submit').attr('disabled', false);
					fsgoo.verify_refresh();
				}
			}
		});
	},
	sign : function(obj)
	{
		$.ajax({
			type : "post",
			url : "/sign/add/"+Math.random(),
			data : "ajax=1",
			dataType : "json",
			success : function(data){
				if(data.goods_id==0){
					if(data.msg.indexOf('已经签到过')!=-1){
						alert(data.msg);
						return false;
					}
					fsgoo.tmp[0] = '/images/sign_no_img.jpg';
					fsgoo.tmp[1] = '你没有中奖，谢谢你的参与';
				}else{
					fsgoo.tmp[0] = data.img;
					fsgoo.tmp[1] = data.goods_name;
				}
				$(obj.parentNode.parentNode).find('img').each(function(){
					this.src = fsgoo.tmp[0];
					this.title = fsgoo.tmp[1];
				});
				$(obj.parentNode.parentNode).addClass('liang');
				$(obj.parentNode).html('<img src="images/an06.gif" />');
				//show other pic
				var new_goods = new Array();
				var j = 0;
				for(var i=0; i<all_goods.length; i++){
					if(all_goods[i][0]!=fsgoo.tmp[0]){
						new_goods[j] = all_goods[i];
						j++;
					}
				}
				var start = Math.floor(Math.random()*new_goods.length);
				$('#qiandao').find('input').each(function(i){
					var k = (i+start)%new_goods.length;
					if(Math.floor(Math.random()*2)==1){
						fsgoo.tmp[0] = '/images/sign_no_img.jpg';
						fsgoo.tmp[1] = '你没有中奖，谢谢你的参与';
					}else{
						fsgoo.tmp[0] = new_goods[k][0];
						fsgoo.tmp[1] = new_goods[k][1];
					}
					$(this.parentNode.parentNode).find('img').each(function(){
						this.src = fsgoo.tmp[0];
						this.title = fsgoo.tmp[1];
					});
					$(this.parentNode).html('<img src="images/an06.gif" />');
				});
			}
		});
	},
	copy : function(text)
	{
	    window.clipboardData.setData("Text",text);
	    alert("复制成功!");
	}
};

//添加事件
$(document).ready(function(){
	//表单提交时验证表单
	$('#form_login').submit(function(){return fsgoo.chkfrm_login(this);});
	fsgoo.reg.init();
	$('#form_findpassword').submit(function(){return fsgoo.chkfrm_findpassword(this);});
	$('#form_resetpassword').submit(function(){return fsgoo.chkfrm_resetpassword(this);});
	$('#province').change(function(){fsgoo.change_city(this.value);});
	$('#form_code').submit(function(){return fsgoo.chkfrm_code(this);});
	$('#qiandao').find('input').each(function(){$(this).click(function(){fsgoo.sign(this);});});
	$('#tools_search2').submit(function(){return fsgoo.chkfrm_gifts_search2(this);});
});
