var reg_ph = /^(13|15|18)[\d]{9}$/;
var reg_code = /^[\d]{6}$/;
var regi_oning = false;
var loadingStr = "<div style='width:100%;height:30px;text-align:center;font-size:12px;'><table><tr><td><img src='images/loading.gif'></td><td>loading...</td></tr></table></div>";
var myphnum="";
$(document).ready(function(){
	init_img();
    init_msg();
	init_countuser();
	init_reg();
});
function init_img()
{
    var img = new Image();
    img.src="images/clgl_2_03_0.gif";
}
function init_msg()
{
	$("#user_msg").html(loadingStr);
	$.get("server.jsp",{mt:"getFB",r:Math.random()},function(data){
	   $("#user_msg").html(data.trim());
	});
}
function init_countuser()
{
	$("#userCount").html("...");
	$.get("server.jsp",{mt:"getUC",r:Math.random()},function(data){
	    $("#userCount").html(data.trim());
		window.setTimeout(function(){
		    init_countuser(); // timeout 1m
		},60000);
	});
}
function init_reg()
{
	$("#div2_cont_2").html(loadingStr);
	$.get("frames.jsp",{mt:"1",r:Math.random()},function(data){
	   $("#div2_cont_2").html(data.trim());
	   window.setTimeout(function(){
		   if(myphnum && myphnum.match(reg_ph) != null ){ $i("glv_pnum").value = myphnum; }
		    $i("glv_pnum").select();
		},500);
	});
}
function glv_getCode()
{
	myphnum = $i("glv_pnum").value.trim();
	if($i("glv_pnum").value.trim() == "") 
	{
		$("#reginfo").html("<span style='color:red'>请输入手机号码！</span>");
		$i("glv_pnum").focus();
	}
	else if($i("glv_pnum").value.trim().match(reg_ph) == null) 
	{
        $("#reginfo").html("<span style='color:red'>号码不合法，正确的号码是以13、15或18开头的11位号码。</span>" );
	    $i("glv_pnum").select();
    } 
	else
	{
		$i("a_getcode").disabled = true;
		$("#reginfo").html("<span style='color:blue'>正在获取...</span>");
		$.get("server.jsp?mt=gc&ph="+$i("glv_pnum").value.trim()+"&rr="+Math.random(),function(data){
			$i("a_getcode").disabled = false;
            var returnCode = $.trim(data);

			if(returnCode=="00")
			{
		        $("#reginfo").html("<span style='color:green'>验证码已经发送！</span>");
			}
			else
			{
				glv_otherReturnWords(returnCode);
			}
		});
	}
}
function glv_doReg()
{
	myphnum = $i("glv_pnum").value.trim();
	if(regi_oning) return false;

	if($i("glv_pnum").value.trim() == "") 
	{
		$("#reginfo").html("<span style='color:red'>请输入手机号码！</span>");
		$i("glv_pnum").focus();
	}
	else if($i("glv_pnum").value.trim().match(reg_ph) == null) 
	{
        $("#reginfo").html("<span style='color:red'>号码不合法，正确的号码是以13、15或18开头的11位号码。</span>" );
	    $i("glv_pnum").select();
    } 
	else if($i("glv_code").value.trim() == "") 
	{
		$("#reginfo").html("<span style='color:red'>请输入验证码！</span>");
		$i("glv_code").focus();
	}
	else if($i("glv_code").value.trim().match(reg_code) == null) 
	{
        $("#reginfo").html("<span style='color:red'>验证码格式不正确！</span>" );
	    $i("glv_code").select();
    } 
	else
	{
		regi_oning = true;

		$("#reginfo").html("<span style='color:blue'>正在注册...</span>");
		$.post("server.jsp",{mt:"reg",ph:$i("glv_pnum").value.trim(), cd:$i("glv_code").value.trim(), rr:Math.random() },function(data){
			regi_oning = false;

            var returnCode = $.trim(data);
			if(returnCode=="00")
			{
				$("#div2_cont_2").html(loadingStr).load("frames.jsp?mt=2");
			}
			else
			{
				glv_otherReturnWords(returnCode);
			}
		});
	}
}
function glv_otherReturnWords(returnCode)
{
	if (returnCode=="a1")
	{
        $("#div2_cont_2").html(loadingStr).load("frames.jsp?mt=3");
	}
	else if (returnCode=="c1")
	{
        $("#div2_cont_2").html(loadingStr).load("frames.jsp?mt=4");
	}
	else if (returnCode=="b1")
	{
        $("#div2_cont_2").html(loadingStr).load("frames.jsp?mt=5");
	}
	else if(returnCode=="d1")
	{
		$("#reginfo").html("<span style='color:red'>验证码失效</span>");
	}
	else if(returnCode=="d2")
	{
		$("#reginfo").html("<span style='color:red'>验证码错误</span>");
	}
	else if(returnCode=="e1")
	{
		$("#reginfo").html("<span style='color:red'>非常抱歉，您获取验证码次数太频繁，十分钟后才能再次获取</span>");
	}
	else if(returnCode=="e2")
	{
		$("#reginfo").html("<span style='color:red'>非常抱歉，您获取验证码的次数已经超过10次，请明天重试！</span>");
	}
	else
	{
		$("#reginfo").html("<span style='color:red'>非常抱歉，现在系统比较忙，请稍后重试！</span>");
	}
}
function sendOpenSm()
{
	$("#sendOpenSm_inf").html("<br><span style='font-size: 14px; color: #FF6633; font-weight:bold'>正在发送短信...</span>");
	$.get("/user/sendSm.jsp?phonenumber="+myphnum,function(data){
		data = data.trim();
		if(data=="000000")
		{
	        $("#sendOpenSm_inf").html("<span style='font-size: 12px;'>开户短信已成功发送到您的手机，回复短信内容“<span style='font-size: 12px; color: #FF6633; font-weight:bold'>KT</span>”，即可开通彩铃。</span>");
		}else{
            $("#sendOpenSm_inf").html("<br><span style='font-size: 12px; color: #FF6633; font-weight:bold'>短信发送失败!</span>");
		}
	});
}