/**************************************************************
author:john
dwr相关js代码
**************************************************************/

function regUser(){   //用户注册页面
	var email=document.form1.email.value;
	var account=document.form1.account.value;
	var name=document.form1.name.value;
	var password=document.form1.password.value;
	var repeatPassword=document.form1.repeatPassword.value;
	var tel=document.form1.tel.value;
	var hasContact=document.form1.hasContact.value;
	var validateCode=document.form1.validateCode.value;
	var  source=document.form1.source.value;
	var contacts=document.form1.contactsList.value;

	if(account=="" || account.length<3 || account.length>12){
		alert("用户名不能为空,长度限制为3-12个字符");
		return ;
	}
	if(password=="" || password.length<6){
		alert("密码不能为空，并且长度不小于6");
		return ;
	}
	if(repeatPassword=="" || repeatPassword.length<6){
		alert("第二次输入密码不能为空，并且长度不小于6");
		return ;
	}
	if(password!=repeatPassword){
		alert("两次输入密码不一致");
		return ;
	}
	if(name=="" || name.length<2){
		alert("请输入真实姓名");
		return ;
	}
	
	if(tel=="" ){
		alert("手机号码不能为空");
		return ;
	}
	if(tel.length!=11 ){
		alert("手机号码位数不正确");
		return ;
	}
	if(validateCode=="" || validateCode.length!=4){
		alert("验证码不能为空");
		return ;
	}
		
	if(hasContact==2 || hasContact=="2"){
		alert("请选择联系人!");
		return ;
		
	}
	if(hasContact==0){
		if( source==0 || source=="0" ){
			alert("请选择来源!");
			return ;
		}
	}
	if(hasContact==1 || hasContact=="1"){
		if( contacts==0 || contacts=="0" ){
			alert("请选择联系人!");
			return ;
		}
		
	}
	
userAction.dwrWebRegUser(account,password,name,email,tel,hasContact,validateCode,source,contacts,{
			callback:function(str){		
				
				if(str=="doubleAccount")
				  	alert("该用户名已经存在");
				else if(str =="fail")
					alert("注册失败");
				else if(str=="validateError")
					alert("验证码不正确");
				else if(str =="success")
					alert("注册成功，现在不是比赛，请比赛时间登陆参加比赛！");
		
				else if(str =="successGo")
					
					window.location="fqhd.jsp";
				}
		});	

}
function contactChange(){  //改变联系人弹出情况
	if(document.form1.hasContact.value==0 || document.form1.hasContact.value=="0"){
		document.getElementById("sourceArea").style.display="block";
		document.getElementById("contactArea").style.display="none";
	}
	else if(document.form1.hasContact.value==1 || document.form1.hasContact.value=="1"){
		document.getElementById("contactArea").style.display="block";
		document.getElementById("sourceArea").style.display="none";
	}else{
		document.getElementById("contactArea").style.display="none";
		document.getElementById("sourceArea").style.display="none";
	}
}
function login(){ //用户登录页面
	var account=document.loginForm.account.value;
	var password=document.loginForm.password.value;
	var validateCode=document.loginForm.validateCode.value;
	if(account=="" ){
		alert("请输入账户名");
		return ;
	}
	if(password=="" || password.length<6){
		alert("密码不能为空，并且不小于6位");
		return ;
	}
	if(validateCode=="" ||validateCode.length!=4){
		alert("请正确输入验证码");
		return ;
	}
	document.loginForm.submit();
	
}
function forgetPassword(){
	var email=document.form1.email.value;
	if(email==""){
		alert("请输入邮箱");
		return ;
	}
	userAction.dwrForgetPassword(email,{
			callback:function(str){		
				
				if(str==true)
				  	alert("找回密码成功，请到邮箱查看");
				else
					alert("错误");
		
				}
		});	
	
}

function  getCoinResult(){
	 
	  gameAction.dwrGetCoinResult({
			callback:function(str){
		
				document.getElementById("div_market").innerHTML=str;				
			}
	 });
}

//验证
function changeMarketPrice(){
	if(document.sheetForm.operation.value>2){
		var coinID=document.sheetForm.coinID.value;
		var operation=document.sheetForm.operation.value;
		document.sheetForm.marketPrice.readOnly=true;
	   
		document.sheetForm.marketPrice.value=aa[coinID][operation];
	}	
	else{
		document.sheetForm.marketPrice.readOnly=false;
		document.sheetForm.marketPrice.value="";
	}
}

function checkSub(num1,num2){
	var  length=0;
	num1Str = num1.toString();
	num2Str=num2.toString();
				
	length=num1Str.length-1-num1Str.indexOf(".");
	if(length<(num2Str.length-1-num2Str.indexOf(".")))
		length=(num2Str.length-1-num2Str.indexOf("."));
	for(var i=0;i<length;i++){
		num1=num1*10;
		num2=num2*10;
	}
	if(num1-num2<0 )
		return num2-num1;
	else
		return num1-num2;
}

function check(){
	var coinID=document.sheetForm.coinID.value;
	var operation=document.sheetForm.operation.value;
	var marketPrice=document.sheetForm.marketPrice.value;
	var stoplossPrice=document.sheetForm.stoplossPrice.value;
	var targetPrice=document.sheetForm.targetPrice.value;
	var reason=document.sheetForm.reason.value;
	if(operation==0){
		alert("请选择操作类型");
		return false;
	}
	if(marketPrice==""){
		alert("进场价格不能为空");
		return false;
	}
	if(stoplossPrice==""){
		alert("止损价格不能为空");
		return false;
	}
	if(targetPrice==""){
		alert("目标价格不能为空");
		return false;
	}
	/*if(reason=="" || reason.length>200 || reason.length<5){
		alert("做单理由不能为空,且字数在5到200之间");
		return false;
	}
	*/
	if(operation == 1 || operation ==3){ //买入时候，目标价不低于市场价

		if(marketPrice> targetPrice || checkSub(document.sheetForm.marketPrice.value,document.sheetForm.targetPrice.value)<5 ){
			alert("目标价必须高于入场价5点起，请核对后再提交!");
			return false;
		}
		if(marketPrice<stoplossPrice || checkSub(document.sheetForm.marketPrice.value,document.sheetForm.stoplossPrice.value)<5){
			alert("止损价必须低于进场价5点起，请核对后再提交!");
			return false;
		}
					
	}
	
	if(operation == 2 || operation ==4){ //卖出时候，目标价不高于市场价
	
		if(marketPrice<targetPrice || checkSub(document.sheetForm.marketPrice.value,document.sheetForm.targetPrice.value)<5){
			alert("目标价必须低于进场价5点起，请核对后再提交!");
			return false;
		}	
		if(marketPrice>stoplossPrice || checkSub(document.sheetForm.marketPrice.value,document.sheetForm.stoplossPrice.value)<5){
			alert("止损价必须高于进场价5点起，请核对后再提交!");
			return false;
		}		
	}
	//目标点数大于止损点数
	if(checkSub(document.sheetForm.marketPrice.value,document.sheetForm.stoplossPrice.value)>checkSub(document.sheetForm.marketPrice.value,document.sheetForm.targetPrice.value))
	{	
		alert("盈利目标点数 必须大于等于止损点数");
		return ;
	}
	if(checkSub(document.sheetForm.marketPrice.value,document.sheetForm.stoplossPrice.value)>200){
		alert("止损点数不能大于200点");
		return ;
	}
		
	sheetAction.dwrSaveSheet(coinID,operation,marketPrice,targetPrice,reason,stoplossPrice,{
		callback:function(str){	
			if(str=="unLogin")
				alert("没有登录,不能发起喊单!");
			else if(str=="dayMore")
				alert("每天最多发起三次喊单");
			else if(str=="coinMore")
				alert("每天每个货币只能发起一次喊单");
			else if(str=="success"){
				alert("发布成功");
				document.sheetForm.marketPrice.value="";
				document.sheetForm.stoplossPrice.value="";
				document.sheetForm.targetPrice.value="";
	  			document.sheetForm.reason.value="";
				//window.loaction="listMySheet.do";
			}else 
				alert("发生错误,请重试");
		}
	});
}


		
function saveVote(){
	var bbs=0;
	var email=0;
	var friend=0;
	var hfc=0;
	var google=0;
	var baidu=0;
	var other=0;
	
	if(document.vote.bbs.checked )
		bbs=1;
	if(document.vote.email.checked)
		email=1;
	if(document.vote.friend.checked)
		friend=1;
	if(document.vote.hfc.checked)
		hfc=1;
	if(document.vote.google.checked)
		google=1;
	if(document.vote.baidu.checked)
		baidu=1;
	if(document.vote.other.checked)
		other=1;

	voteAction.dwrSaveVote(bbs,email,friend,hfc,google,baidu,other,{
		callback:function(str){	
			if(str=="success"){
				alert("发布成功");
				
			}else 
				alert("发生错误,请重试");
		}
	});
}
function seeVote(){
	voteAction.dwrSeeVote({
		callback:function(str){	
				alert(str);
		}
	});
}
