﻿/*
 * app.js
 * For www.mymingpai.com game application
 */

function signup() {
	
	//collect user information
	var name = $('#name').val();
	var sex = $(':radio:checked').val();
	var email = $('#email').val();
	var year = $('#year')[0].options[$('#year')[0].selectedIndex].value;
	var month = $('#month')[0].options[$('#month')[0].selectedIndex].value;
	var day = $('#day')[0].options[$('#day')[0].selectedIndex].value;
	var birthday = year+'-'+month+'-'+day;
	var city = $('#city').val();
	var mobile = $('#mobile').val();
	
	var code = $('#vcode').val();
	var upuid = $('#upuid').val();
	
	var terms = $('#terms')[0].checked ? true : false;
	var subscribe = $('#subscribe')[0].checked ? '1' : '0';
	
	//validate date
	if(name=='' || name.length<2 || name.length>30) { alert('请正确填写您的姓名'); $('#name')[0].focus(); $('#name').css('background-color','#FEC2BC'); return false; }
	if(typeof sex=='undefined') { alert('请选择您的性别'); return false;}
	if(!echeck($('#email')[0])) { alert('请正确填写您的电子邮件'); $('#email')[0].focus(); $('#email').css('background-color','#FEC2BC'); return false; }
	if(city=='' || city.length<9 || city.length>56) { alert('请正确填写您的收货地址，这个关系到您是否能够收到我们寄送的奖品'); $('#city')[0].focus(); $('#city').css('background-color','#FEC2BC'); return false; }
	if(mobile=='' || mobile.length!=11 || mobile.substr(0,1)!='1') { alert('请正确填写您的手机号码'); $('#mobile')[0].focus(); $('#mobile').css('background-color','#FEC2BC'); return false; }
	if(code=='' || code.length!=4) { alert('请正确填写验证码'); $('#vcode')[0].focus(); $('#vcode').css('background-color','#FEC2BC'); return false; }
	if(!terms) { alert('你需要阅读并点选同意本次的比赛条款才能参加抽奖'); return false; }
	
	
	$('#subtn').hide();
	$('#proc').show();
	//submit the date
	$.post('includes/app.php?act=reg', {name:name,sex:sex,email:email,birthday:birthday,city:city,mobile:mobile,code:code,upuid:upuid,subscribe:subscribe},
			function(res){
				//alert(res);return false;
				//**
				if(res.substr(0,2)=='ok') {
					alert('活动参加还未成功哦，请马上去您的注册邮箱激活链接。\n当然，邀请更多好友加入将使您的获奖机会加倍!');
					location.href='invite-friends.html';
				} else {
					alert(res);
					$('#proc').hide();
					$('#subtn').show();
				}
				//*/
	});
	
	
}


// check email address
function check_email(email) {
	
	return /^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/.test(email);
}



function invite() {
	
	//game_is_over();
	
	var email1 = $('#send1').val();
	var email2 = $('#send2').val();
	var email3 = $('#send3').val();
	var email4 = $('#send4').val();
	var email5 = $('#send5').val();
	var email6 = $('#send6').val();
	var email7 = $('#send7').val();
	var email8 = $('#send8').val();
	
	var empty_tag = 0 ;
	for(var i=1; i<=8;i++) {
		
		if($('#send'+i).val()=='') {
			empty_tag++;
			continue;
		}
		if(!check_email($('#send'+i).val())) {
			alert('您填写的电子邮箱有误，检查一下吧!');
			$('#send'+i)[0].focus();
			$('#send'+i).css('background-color','#FEC2BC');
			return false;
		}
	}
	
	if(empty_tag==8) {
		
		alert('我们建议您至少填写一个好友的电子邮件邀请TA参加抽奖活动，邀请的好友越多您获奖的机会就越大!');
		return false;
	}
	
	$('#subtn').hide();
	$('#proc').show();
	$.post('includes/app.php?act=invite', {email1:email1,email2:email2,email3:email3,email4:email4,email5:email5,email6:email6,email7:email7,email8:email8}, function(res){
		
		if(res.substr(0,2)=='ok') {
			alert('邀请好友参加抽奖成功，感谢您的参与，敬请留意8月8日在买名牌网论坛公布的抽奖结果!');
			location.href='http://mall.mymingpai.com/cosmetics.html';
		} else {
			//alert(res);
			alert('邀请失败，请稍候再试');
			$('#proc').hide();
			$('#subtn').show();
		}
		return false;
	});
	
	
}


function echeck (obj,type) {
	
	if(!type) {
		if(check_email($(obj).val()) && obj.value.length>7 && obj.value.length<56) {
			$(obj).css('background-color','#D3F09D');
			return true;
		} else {
			$(obj).css('background-color','#FEC2BC');
			return false;
		}
	} else {
		if($(obj).val()=='') return false;
		else echeck(obj);
	}
}

function namecheck(obj) {
	
	if(obj.value.length>1 && obj.value.length<30 ) {
		$(obj).css('background-color','#D3F09D');
	} else {
		$(obj).css('background-color','#FEC2BC');
	}
	return;
}

function citycheck(obj) {
	
	  if(obj.value!='' && (obj.value.length>8 && obj.value.length<56) ) {
		  $(obj).css('background-color','#D3F09D');
	  } else {
		  $(obj).css('background-color','#FEC2BC');
	  }
	return;
}

function mobilecheck(obj) {
	  if(obj.value!='' && obj.value.length==11 && obj.value.substr(0,1)=='1' ) {
		  $(obj).css('background-color','#D3F09D');
	  } else {
		  $(obj).css('background-color','#FEC2BC');
	  }
	return;
}

function codecheck(obj) {
	
	if(obj.value.length==4) {
		
		var code = $(obj).val();
		$.get('includes/app.php?act=vcode', {code:code}, function(res) {
			if(res.substr(0,2)=='ok') {
				$(obj).css('background-color','#D3F09D');
			} else {
				$(obj).css('background-color','#FEC2BC');
			}
		});
	} else {
		$(obj).css('background-color','#FEC2BC');
	}
	return;
}

function trywin() {
	
	$.get('includes/app.php', {act:'trywin',ans:$('#mingpai').val()}, function(e){
		
		if(e.substr(0,2)=='ok') {
			self.location.href = 'youwin.html';
		} else if(e.substr(0,2)=='re') {
			alert("您已经答对了我们的问题，希望您能成为赠礼活动的幸运者，谢谢！");
			return false;
		} else {
			self.location.href = 'tryagain.html';
		}
	});
	
}

function game_is_over()
{
	alert("对不起，本次活动已经结束了");
	exit;
}

function selectUser()
{
	var uid = $('#uid').val();
	var action = 'select';
	$('#result').html('<img src="/images/loading.gif" />');
	$.get('/includes/app.php',{act:action,uid:uid},function(res){
		
		$('#result').html(res);
	})
	
}

function selectUser2()
{
	var uid = $('#uid').val();
	var action = 'selec';
	$('#result').html('<img src="/images/loading.gif" />');
	$.get('/includes/app.php',{act:action,uid:uid},function(res){
		
		$('#result').html(res);
	})
	
}

function active(uid)
{
	if(uid=='') {
		alert('Param error');
		return false;
	}
	var action = 'actby';
	$.get('/includes/app.php',{act:action,uid:uid},function(res) {
		if(res.substr(0,2)=='ok') {
			alert('Ok');
			return false;
		} else {
			alert('Failt');
			return false;
		}
	})
}






