$(document).ready(function(e) {	
	createMask("mymask");
	$("#line").css("height",$("#nav").outerHeight());
	try{
		if($action=="l")	{
			$("#modal_login").modal("show");	
		}
	}
	catch(e){
		
	}
	
	$("#username_reg").bind("change",function(d){
		loadCheckCode($("#cc_img"),1);
	});
	$("#username_getpwd").bind("change",function(d){
		loadCheckCode($("#cc_img_getpwd"),0);
	});
	
	$("#username").bind("change",function(d){
		loadCheckCode($("#cc_img_login"),0);
	});
	
	$("#submitlogin").bind("click",login);
	
	$(this).bind("click",function(e){
		var t = e.target;
		var bttype = $(t).attr("bttype");
		switch(bttype){
			case "ExitBt":				
				if(!confirm("确定退出？")){
					return;	
				}
				window.location.href = "/user/logout.aspx"
				break;
			case "toLoginBt":
				$("#modal_getpwd").modal("hide");
				$("#modal_reg").modal("hide");
				$("#modal_login").modal("show");
				break;
			case "toRegBt":
				$("#modal_getpwd").modal("hide");
				$("#modal_reg").modal("show");
				$("#modal_login").modal("hide");
				break;
			case "toGetPwdBt":
				$("#modal_getpwd").modal("show");
				$("#modal_reg").modal("hide");
				$("#modal_login").modal("hide");
				break;
			case "submitgetpwdbt":
				submitgetpwd();
				break;
			case "accountsetbt":

				geticolist(20);
				$("#modal_accountset").modal("show");				
				break;
			case "reloadicobt":
				geticolist(20);
				break;
			case "selecticobt":
				selectico($(t).attr("v"));
				break;
			case "savenicknamebt":
				savenickname();
				break;
				
			case "GetCheckCodeBt":
				loadCheckCode(t,0);
				break;
			case "GetCheckCodeBt_reg":
				loadCheckCode(t,0);
				break;
			case "GetGetPWDCheckCodeBt":
				loadCheckCode(t,0);
				break;
			case "SendRegMobCCBt":
				sendregmobcc();
				break;
			case "SendGetPWDMobCCBt":
				sendGetPWDMobcc();
				break;
			case "selectregtypebt":
				//loadCheckCode($("#cc_img"),1);
				$("#reginfo").css("display","block");
				break;
			case "submitregbt":
				submitreg()
				break;
			case "modifypwdbt":
				$("#modal_modifypwd").modal("show");
				break;
			case "domodifypwdbt":
				domodifypwd();
				break;
			case "sendmobcc_reg_bt":
				sendmobcc_reg();
				break;
			case "aboutbt":
				$("#modal_about").modal("show");
				break;
		}
	});
});

function sendmobcc_reg(){
	var o = {};
	o.cc = $("#cc_reg").val();
	o.mob = $("#username_reg").val();
	o.sendtype = "soft_cc";
	
	
	o.t = $("#ccimg_reg").attr("t");
	o.ts = $("#ccimg_reg").attr("ts");
	o.s = $("#ccimg_reg").attr("s");
	o.sid = $("#ccimg_reg").attr("sid");
	
	
	if(o.mob==""){
		msgbox("请输入手机号","warning");	
		return;
	}
	
	
	if(o.t==undefined){
		msgbox("请获取图形码","warning");	
		return;
	}
	
	if(o.cc==""){
		msgbox("请输入图形码","warning");	
		return;
	}
	$("#mymask").modal("show");
	$.post("sendsms.aspx",o,function(d){
		$("#mymask").modal("hide");
		switch(d.result){
			case "ok":
				msgbox("验证码已经发送至您的手机号","success");
				$("#cc_mob_reg").attr("t",d.t);
				$("#cc_mob_reg").attr("ts",d.ts);
				$("#cc_mob_reg").attr("s",d.s);
				$("#cc_mob_reg").attr("sid",d.sid);
				countDown("sendmobcc_reg_bt",60,60);
				break;
			case "timeover":
				msgbox("图形码超时","danger");
				break;
			case "checkcodebad":
				msgbox("验证非法，请重新发送验证码","danger");
				break;
			case "retryover":
				msgbox("错误次数过多，请重新获取图形码","danger");
				break;
			case "ccerr":
				msgbox("验证码错误","danger");
				break;
			case "nullvalue":
				msgbox("请点击\"图形码，点击刷新\"","danger");
				break;
				
			case "urltimeover":
				msgbox("url超时","danger");
				break;
			case "softnotfound":
				msgbox("未找到软件注册信息","danger");
				break;
			case "softsignerr":
				msgbox("软件签名错误","danger");
				break;
			case "smsnumover":
				msgbox("短信配额已经用完","danger");
				break;
			case "smstasknumover":
				msgbox("可用短信数不足以完成任务","danger");
				break;
		}
	},"json");
}


function submitreg(){
	var o = {};

	o.un = $("#username_reg").val();
	o.pwd = $("#password_reg").val();
	o.cc = $("#cc_mob_reg").val();
	
	o.t = $("#cc_mob_reg").attr("t");
	o.ts = $("#cc_mob_reg").attr("ts");
	o.s = $("#cc_mob_reg").attr("s");
	o.sid = $("#cc_mob_reg").attr("sid");
	
	
	
	o.resetpwd = 0;
	if (o.un == "") {
	    $("#username_reg").focus();
	    msgbox("请输入您的手机号", "warning");
	    return;	
	}
	
	
	
	if($("#cc_mob_reg").attr("t")==undefined){
		msgbox("尚未获取图形码","warning");
		return;	
	}
	
	
	if(o.cc==""){
		$("#cc_mob_reg").focus();
		msgbox("请输入验证码","danger");
		return;	
	}
	if(o.pwd==""){
		$("#password_reg").focus();
		msgbox("请输入密码","danger");
		return;	
	}
	
	
	if(o.pwd.length<6){
		$("#password_reg").focus();
		msgbox("密码长度至少6位","danger");
		return;	
	}
	
	

	$("#mymask").modal("show");

	$.post("userreg_submit.aspx",o,function(d){
		$("#mymask").modal("hide");
		switch(d.result){
			case "ccerr":
				msgbox("验证码错误","danger");
				$("#ccimg_reg").focus();
				break;
			case "repeat":
				msgbox("手机已被占用","danger");
				$("#username_reg").focus();
				break;
			case "ok":
				msgbox("注册成功,请登录","success");
				$("#modal_getpwd").modal("hide");
				$("#modal_reg").modal("hide");
				$("#modal_login").modal("show");
				break;
			case "timeover":
				msgbox("验证码超时","danger");
				break;
			case "notfound":
				msgbox("没有发现帐户","danger");
				break;
			case "retryover":
				msgbox("错误次数过多","danger");
				break;
			case "usernumover":
				break;
		}
	},"json");
}


function domodifypwd(){
	var o = {};
	o.pwd = $("#pwd_old").val();
	o.pwd_new = $("#pwd_new").val();
	o.pwd_new_re = $("#pwd_new_re").val();
	o.cc = $("#cc_modifypwd").val();
	o.resetpwd = "yes";
	
	o.t = $("#cc_img_modifypwd").attr("t");
	o.ts = $("#cc_img_modifypwd").attr("ts");
	o.s = $("#cc_img_modifypwd").attr("s");
	o.sid = $("#cc_img_modifypwd").attr("sid");
	
	
	if(o.pwd==""){
		msgbox("请输入原密码","warning");
		$("#pwd_old").focus();
		return;	
	}
		
	if(o.pwd_new==""){
		msgbox("请输入新密码","warning");
		$("#pwd_new").focus();
		return;	
	}
	
	if(o.pwd_new.length<6){
		msgbox("新密码长度至少6位","warning");
		$("#pwd_new").focus();
		return;	
	}
	
	if(o.pwd_new_re!=o.pwd_new){
		msgbox("密码确认不正确","warning");
		$("#pwd_new_re").focus();
		return;	
	}
	
	if(o.cc==""){
		msgbox("请输入验证码","warning");
		$("#cc").focus();
		return;	
	}
	
	$("#mymask").modal("show");
	$.post("modifypwd.aspx",o,function(d){
		console.log(d)
		$("#mymask").modal("hide");
		
		
		switch(d.result){
			case "modifyok":
				msgbox("密码修改完成，请重新登录","success");
				$("#pwd_old").val("");
				$("#pwd_new").val("");
				$("#pwd_new_re").val("");
				$("#modal_modifypwd").modal("hide");
				break;
			case "timeover":
				msgbox("图形码超时","danger");
				break;
			case "checkcodebad":
				msgbox("验证非法，请重新发送验证码","danger");
				break;
			case "notfound":
				msgbox("密码错误","danger");
				break;
			case "retryover":
				msgbox("错误次数过多，请重新获取图形码","danger");
				break;
			case "ccerr":
				msgbox("验证码错误","danger");
				break;
			case "nullvalue":
				msgbox("请点击\"图形码，点击刷新\"","danger");
				break;
		}
	},"json");
}

function submitgetpwd(){
	var o = {};	
	o.un = $("#username_getpwd").val();
	o.pwd = $("#newpassword_getpwd").val();
	o.cc = $("#cc_mob_getpwd").val();
	
	o.t = $("#cc_mob_getpwd").attr("t");
	o.ts = $("#cc_mob_getpwd").attr("ts");
	o.s = $("#cc_mob_getpwd").attr("s");
	o.sid = $("#cc_mob_getpwd").attr("sid");
	
	o.resetpwd = "yes";
	
	if(o.un==""){
		$("#username_getpwd").focus();
		msgbox("请输入登录名","danger");
		return;		
	}
	if(o.cc==""){
		$("#cc_mob_getpwd").focus();
		msgbox("请输入手机中收到的验证码","danger");
		return;	
	}
	if(o.pwd==""){
		$("#newpassword_getpwd").focus();
		msgbox("请输入新密码","danger");
		return;	
	}
	if(o.pwd.length<6){
		$("#newpassword_getpwd").focus();
		msgbox("密码长度至少6位","danger");
		return;	
	}
	
	$("#mymask").modal("show");
	
	$.post("userreg_submit.aspx",o,function(d){
		$("#mymask").modal("hide");
		switch(d.result){
			case "ccerr":
				msgbox("手机验证码错误","danger");
				$("#cc_mob_getpwd").focus();
				break;
			case "notfound":
			case "modifybad":
				msgbox("未发现帐号","danger");
				$("#username_getpwd").focus();
				break;
			case "modifyok":
				msgbox("密码修改成功","success");
				$("#mymask").modal("hide");
				$("#modal_getpwd").modal("hide");
				$("#username_getpwd").val("");
				$("#cc_getpwd").val("");
				$("#cc_mob_getpwd").val("");
				$("#newpassword_getpwd").val("");				
				break;
			case "notcheckcode":
				msgbox("验证非法，请重新发送验证码","danger");
				break;
			case "checkcodebad":
				msgbox("验证非法，请重新发送验证码","danger");
				break;
			case "timeover":
				msgbox("验证码超时","danger");
				break;
			case "resetpwdok":
				msgbox("密码重置成功","danger");
				break;
			case "retryover":
				msgbox("错误次数过多","danger");
				break;
			case "null":
				msgbox("参数不能为空","danger");
				break;
		}
	},"json");
}



//$("#modal_active").modal("show");


function createActiveMobPostLink(mob){
	var domain = mob.split("@")[1].toLowerCase();
	var a = [];
	var result = "";
	switch(domain){
	case "163.com": 
		result = "https://mail.163.com";
		break;
	case "126.com": 
		result = "https://mail.126.com";
		break;
	case "139.com": 
		result = "https://mail.139.com";
		break;
	case "aliyun.com": 
		result = "https://mail.aliyun.com";
		break;
	case "sina.com": 
		result = "https://mail.sina.com.cn/";
		break;
	case "sina.cn": 
		result = "https://mail.sina.com.cn/";
		break;
	case "qq.com": 
		result = "https://mail.qq.com";
		break;
	case "sohu.com": 
		result = "https://mail.sohu.com";
		break;
	case "263.net": 
		result = "https://mail.263.net";
		break;	
	case "formax.com": 
		result = "https://mail.qq.com";
		break;	
	case "outlook.com": 
		result = "https://login.live.com/login.srf"
		break;
	case "hotmail.com": 
		result = "https://login.live.com/login.srf";
		break;
	default:
		result = "http://mail."+domain;
		break;
	}
	return result;
}

function sendGetPWDMobcc(){
	var o = {};
	o.cc = $("#cc_getpwd").val();
	o.mob = $("#username_getpwd").val();
	o.t = $("#cc_img_getpwd").attr("t");
	o.ts = $("#cc_img_getpwd").attr("ts");
	o.s = $("#cc_img_getpwd").attr("s");
	o.sid = $("#cc_img_getpwd").attr("sid");
	o.sendtype = "soft_cc";

	
	if(o.mob==""){
		msgbox("请输入手机号","warning");
		$("#username_getpwd").focus();
		return;	
	}
	
	if(!o.mob._Mob()){
		msgbox("手机号格式不正确","warning");
		$("#username_getpwd").focus();
		return;	
	}
	
	if(o.cc==""){
		msgbox("请输入图形码","warning");
		$("#cc_getpwd").focus();
		return;	
	}
	$.post("sendsms.aspx?",o,function(d){
		switch(d.result){
			case "ok":
				msgbox("验证码已发送到您的手机","success");
				$("#cc_mob_getpwd").attr("t",d.t);
				$("#cc_mob_getpwd").attr("ts",d.ts);
				$("#cc_mob_getpwd").attr("s",d.s);
				$("#cc_mob_getpwd").attr("sid",d.sid);
				countDown("SendGetPWDMobCCBt",60,60);
				break;
			case "null":
				$("#cc_getpwd").focus();
				loadCheckCode($("#cc_img_getpwd"),0);
				msgbox("手机号不能为空","danger");
				break;
			case "ccerr":
			case "signerr":
				$("#cc_getpwd").focus();
				loadCheckCode($("#cc_img_getpwd"),1);
				msgbox("图形码错误","danger");
				break;
			case "timeover":
				$("#cc_getpwd").focus();
				loadCheckCode($("#cc_img_getpwd"),1);
				msgbox("图形码超时","danger");
				break;
			case "retryover":
				$("#cc_getpwd").focus();
				loadCheckCode($("#cc_img_getpwd"),1);
				msgbox("错误次数太多","danger");
				break;
				
				
			
		}
	},"json");
}

function loadCheckCode(o,t){
	$.get("getcheckcode.aspx?t="+t+"&"+Math.random(),function(d){
		$(o).attr("src",d.img);
		$(o).attr("sid",d.sid);
		$(o).attr("s",d.s);
		$(o).attr("t",d.t);
		$(o).attr("ts",d.ts);
		
	},"json");

}

function login() {

        var o = {};
        o.un = $("#username").val();
		o.pwd = $("#password").val();		
		o.t = $("#cc_img_login").attr("t");
		o.ts = $("#cc_img_login").attr("ts");
		o.s = $("#cc_img_login").attr("s");
		o.sid = $("#cc_img_login").attr("sid");
		o.cc = $("#cc_login").val();
		
        if (o.un == "") {
            msgbox("请输入手机号", "danger");
            $("#username").focus();
            return;
        }
		
		if(!o.un._Mob()){
            msgbox("手机号格式不正确", "danger");
            $("#username").focus();
            return;
		}

        if (o.pwd == "") {
            msgbox("请输入密码", "danger");
            $("#password").focus();
            return;
        }
		if(o.cc==""){
            msgbox("请输入验证码", "danger");
            $("#cc_login").focus();
			return;	
		}

        $.post("submitlogin.aspx", o, function (d) {
            var msg = [];
            msg["0"] = "登录成功";
            msg["-1"] = "用户名或密码错误";
            msg["-2"] = "用户名或密码错误";
            msg["-3"] = "用户被禁用";
            msg["-4"] = "错误过多";
            msg["nullvalue"] = "请获取图形码";
            msg["retryover"] = "重试错误过多";
            msg["timeover"] = "图形码超时，请重新获取码";
            msg["bad"] = "图形码失效，请重新获取图形码";
			
			
            if (d.result != "ok") {
				if(d.result=="ccerr"){
					msgbox("验证码错误", "danger");				
				}
				else if(d.result=="notcheckcode"){
					msgbox("验证码不能为空", "danger");
				}
				else{
					msgbox(msg[d.result], "danger");
	                return;
				}
                
            }
			
            else {
                msgbox("登录成功", "success");

				window.location.href = "default.aspx";
				//$("#modal_login").modal("hide");
				//loadSalaryList();
                return;
            }

        }, "json");
}



function selectico(v){
	$("#mymask").modal("show");
	var o = {};
	o.action = "ico";	
	o.ico = v;
	o.r = Math.random();
	$.post("myaccount_saveset.aspx",o,function(d){
		$("#mymask").modal("hide");
		$("#modal_set").modal("hide");
		switch(d){
			case "nullvalue":
				msgbox("输入不能为空","danger");
				break;	
			case "notlogin":
				msgbox("没有登录","danger");
				break;	
			case "ok":
				msgbox("头像修改完成","success");
				$("#ico_show").attr("src","img/ico/"+o.ico);
				$("#smallico").css("background-image","url(img/ico/"+o.ico+")");
				
				break;
			case "ok":
				msgbox("头像修改发生错误","danger");
				break;
		}
	
	},"text")
}

function savenickname(){
	var o = {};
	o.action = "nickname";	
	o.nickname = $("#nickname").val();
	o.r = Math.random();
	if(o.nickname==""){
		msgbox("请输入昵称","warning");
		$("#nickname").focus();
		return;	
	}
	$("#mymask").modal("show");
	$.post("myaccount_saveset.aspx",o,function(d){
		$("#mymask").modal("hide");
		$("#modal_set").modal("hide");
		switch(d){
			case "nullvalue":
				msgbox("输入不能为空","danger");
				break;	
			case "notlogin":
				msgbox("没有登录","danger");
				break;	
			case "ok":
				msgbox("昵称修改完成","success");
				$("#nickname_show").html(o.nickname);
				$("#mynickname").html(o.nickname);
				break;
			case "ok":
				msgbox("昵称修改发生错误","danger");
				break;
		}
	
	},"text")
}

function geticolist(ps){
	var a = [];
	var temp;
	$("#seticobox").html("<div style='height:10rem;line-height:10rem;width:100%;font-size:3rem; text-align:center;color:#f60'><i class='fa fa-spinner fa-spin'></i> loading...</div>");
	for(var i=0;i<ps;i++){
		temp = randomNum(1,2000);
		a.push("<li><img src='img/ico/"+temp+".jpg' v='"+temp+".jpg' bttype='selecticobt' /></li>");	
	}
	a.push("<div style='clear:both'></div>");
	a.push("<div style='text-align:center; margin-top:2rem'><a class='btn btn-default' bttype='reloadicobt' href='#'>换一批</a></div>");
	setTimeout(function(){
	$("#seticobox").html(a.join(""));
	},5);

}

function randomNum(minNum,maxNum){ 
    switch(arguments.length){ 
        case 1: 
            return parseInt(Math.random()*minNum+1,10); 
        break; 
        case 2: 
            return parseInt(Math.random()*(maxNum-minNum+1)+minNum,10); 
        break; 
            default: 
                return 0; 
            break; 
    } 
} 



function countDown(id,n,time) {  
	var o = $("#"+id);
    n = n - 1;  
    o.html(n+"秒后重发");  
	o.attr("disabled","disabled");
    if (n == 0) {  
        o.html("重新发送"); 
		o.removeAttr("disabled");
        n = time; 
        return;  
    }  
    setTimeout('countDown("'+id+'",'+n+','+time+')',1000);  
} 