﻿var usernameok=false;
var userpwdok=false;
var userpwd2ok=false;
var emailok=false;
var getcodeok=false;
function DOSubmit()
{
	if (!usernameok) {document.registerForm.username.focus();return false;}
	if (!userpwdok) {document.registerForm.password.focus();return false;}
	if (!userpwd2ok) {document.registerForm.password2.focus();return false;}
	if (!emailok) {document.registerForm.email.focus();return false;}
	if (!getcodeok) {document.registerForm.getcode.focus();return false;}

	if (usernameok && userpwdok && userpwd2ok && emailok && getcodeok)
	{
	document.getElementById("AjaxForm").style.display="";
	isEmailExist("0","", "AjaxForm");
	return false;
	}
    return false;
}

function doOnFocus(param) {
    var str = "";
    if (param == "usernametip") {
        str = "<font color='#252525'>由 4－15 位英文字母（不区分大小写）、数字或下划线组成，建议使用公司名称的字母或拼音缩写！</font>";
    }
    if (param == "passwordtip") {
        str = "<font color='#252525'>由 6－20 个字符组成，建议使用复杂且不易被猜到的字符，可在密码中加入如 @ # $ 等特殊符号！</font>";
    }
    if (param == "password2tip") {
        str = "<font color='#252525'>请再次填写您的登录密码！</font>";
    }
    if (param == "emailtip") {
        str = "<font color='#252525'><b>非常重要！</b>我们需要您通过邮箱验证完成注册，请填写您最常用的邮箱，这也是客户联系您的首要方式！没有电子邮箱？点此免费获取：<a target=_blank href=http://mail.163.com class=blue>网易邮箱</a>&nbsp;&nbsp;<a target=_blank href=http://mail.sohu.com class=blue>搜狐邮箱</a></font>";
    }
    if (param == "getcodetip") {
        str = "<font color='#252525'>5位数的验证码，由数字和字母组成，所有0为数字！</font>";
    }
    document.getElementById(param).innerHTML = str;
    document.getElementById(param).className = 'border_1';
}

function checkusername(username) {
    document.getElementById("usernametip").className = "";
    var filter=/^\s*[.A-Za-z0-9_-]{4,15}\s*$/;
    if (username.length == 0 || username.length<4) {
        document.getElementById("usernametip").innerHTML = "<img src='/images/ico_error.gif' border='0'>&nbsp;<font color=red >帐号输入有误！必须由 4－15 位英文字母（不区分大小写）、数字或下划线组成，建议使用公司名称的字母或拼音缩写！</font>";
    	document.getElementById("usernametip").className = 'border_2';
	usernameok=false;
        return false;
    }
    if (!filter.test(username)) {
        document.getElementById("usernametip").innerHTML = "<img src='/images/ico_error.gif' border='0'>&nbsp;<font color=red >帐号输入有误！必须由 4－15 位英文字母（不区分大小写）、数字或下划线组成，建议使用公司名称的字母或拼音缩写！</font>";
    	document.getElementById("usernametip").className = 'border_2';
	usernameok=false;
        return false;
    }


    isEmailExist("1",username, "usernametip");
    return true;
}
function checkpassword(password) {
    document.getElementById("passwordtip").className = "";
    if (password.length == 0  || password.length < 6) {
        document.getElementById("passwordtip").innerHTML = "<img src='/images/ico_error.gif' border='0'>&nbsp;<font color=red >由 6－20 个字符组成，建议使用复杂且不易被猜到的字符，可在密码中加入如 @ # $ 等特殊符号！</font>";
    	document.getElementById("passwordtip").className = 'border_2';
	userpwdok=false;
        return false;
    }
    document.getElementById("passwordtip").innerHTML = "<img src='/images/ico_ok.gif' border='0'>&nbsp;<font color=#252525 >登录密码填写正确！</font>";
	userpwdok=true;
    return true;
}
function checkpassword2(password2) {
    document.getElementById("password2tip").className = "";
    if (password2.length == 0) {
        document.getElementById("password2tip").innerHTML = "<img src='/images/ico_error.gif' border='0'>&nbsp;<font color=red >请再次输入您的密码！</font>";
    	document.getElementById("password2tip").className = 'border_2';
	userpwd2ok=false;
        return false;
    }
    if (document.registerForm.password.value != document.registerForm.password2.value) {
        document.getElementById("password2tip").innerHTML = "<img src='/images/ico_error.gif' border='0'>&nbsp;<font color=red >两次输入的密码不一致，请重新输入！</font>";
    	document.getElementById("password2tip").className = 'border_2';
	userpwd2ok=false;
        return false;
    }
    document.getElementById("password2tip").innerHTML = "<img src='/images/ico_ok.gif' border='0'>&nbsp;<font color=#252525 >两次密码填写正确！</font>";
	userpwd2ok=true;
    return true;
}
function checkemail(email) {
    document.getElementById("emailtip").className = "";
    if (email.length == 0) {
        document.getElementById("emailtip").innerHTML = "<img src='/images/ico_error.gif' border='0'>&nbsp;<font color=red >您的输入有误！电子邮件非常重要！我们需要您通过邮箱验证完成注册，请填写您最常用的邮箱，这也是客户联系您的首要方式！没有电子邮箱？点此免费获取：<a target=_blank href=http://mail.163.com class=blue>网易邮箱</a>&nbsp;&nbsp;<a target=_blank href=http://mail.sohu.com class=blue>搜狐邮箱</a></font>";
    	document.getElementById("emailtip").className = 'border_2';
	emailok=false;
        return false;
    }
    if (checkEmailSpecail(email) == false) {
        document.getElementById("emailtip").innerHTML = "<img src='/images/ico_error.gif' border='0'>&nbsp;<font color=red >您输入的邮件地址含有非法字符！</font>";
	emailok=false;
        return false;
    }
    isEmailExist("2",email, "emailtip");
    return true;
}
function checkgetcode(getcode) {
    document.getElementById("getcodetip").className = "";
    if (getcode.length != 5) {
        document.getElementById("getcodetip").innerHTML = "<img src='/images/ico_error.gif' border='0'>&nbsp;<font color=red >请输入5位数验证码！</font>";
    	document.getElementById("getcodetip").className = 'border_2';
	getcodeok=false;
        return false;
    }
    document.getElementById("getcodetip").innerHTML = "<img src='/images/ico_ok.gif' border='0'>&nbsp;<font color=#252525 >验证码填写正确！</font>";
	getcodeok=true;
    return true;
}
function checkEmailSpecail(email) {
    var mail = trim(email);
    if (mail == "") return false;
    var myReg = /^[_\-\.a-zA-Z0-9]+@([_\-a-zA-Z0-9]+\.)+[a-zA-Z0-9]{2,3}$/;
    if (myReg.test(email) == false) {
        return false;
    }
    return true;
}
function isEmailExist(intId,varVal, objID) {
    vouintId=intId;
    voucharTip = document.getElementById(objID);
    if ( vouintId == "0" )
	{
	totaourl="/System_Ajax.aspx?Id=" + intId + "&username=" + document.registerForm.username.value + "&password=" + document.registerForm.password.value + "&email=" + document.registerForm.email.value + "&getcode=" + document.registerForm.getcode.value;
	}
    else
	{    
	totaourl = "/System_Ajax.aspx?Id=" + intId + "&username=" + varVal;
	}
    send_request(totaourl);
}

function send_request(url) {//初始化、指定处理函数、发送请求的函数
    http_request = false;
    //开始初始化XMLHttpRequest对象
    if (window.XMLHttpRequest) { //Mozilla 浏览器
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType) {//设置MiME类别
            http_request.overrideMimeType('text/xml');
        }
    }
    else if (window.ActiveXObject) { // IE浏览器
        try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {
            }
        }
    }
    if (!http_request) { // 异常，创建对象实例失败
        window.alert("不能创建XMLHttpRequest对象实例.");
        return false;
    }
    http_request.onreadystatechange = processRequest;
    // 确定发送请求的方式和URL以及是否同步执行下段代码
    http_request.open("GET", url, true);
    if (vouintId=="0")
	{
	document.getElementById("AjaxForm").innerHTML="<IMG src='/images/loading.gif'> 注册信息正在提交中，请稍候…… ";
	}
    http_request.send(null);
}
// 处理返回信息的函数
function processRequest() {
    if (http_request.readyState == 4) { // 判断对象状态　
        //confirm("http_request.status :"+http_request.status);

        if (http_request.status == 200) { // 信息已经成功返回，开始处理信息　
	if (http_request.responseText=="False") {
		if (vouintId=="1")
			{
				voucharTip.innerHTML = "<img src='/images/ico_error.gif' border='0' align='absbottom'>&nbsp;<font color=red >帐号已经存在，请使用其它帐号！</font>";
				document.getElementById("usernametip").className = 'border_2';
				usernameok=false;
				return false;
			}
		else
			{
				voucharTip.innerHTML = "<img src='/images/ico_error.gif' border='0' align='absbottom'>&nbsp;<font color=red >您输入的邮件地址已被注册，请重新输入！</font>";
				document.getElementById("emailtip").className = 'border_2';
				emailok=false;
				return false;
			}
			return false;
			}
	if (vouintId=="0")
		{
			//voucharTip.innerHTML = "验证码错误！";
			var htmlDoc = http_request.responseText;
			set_innerHTML("AjaxForm", htmlDoc);
		}
	if (vouintId=="1")
		{
			voucharTip.innerHTML = "<img src='/images/ico_ok.gif' border='0'>&nbsp;<font color=#252525 >帐号可以注册！</font>";
			usernameok=true;
		}
	if (vouintId=="2")
		{
			voucharTip.innerHTML = "<img src='/images/ico_ok.gif' border='0' align='absbottom'>&nbsp;<font color=#252525 >邮件地址可以注册！</font>";
			emailok=true;
		}
        return true;
        } else {//页面不正常
		alert(http_request.responseText);
            //alert("您所请求的页面有异常。");
            return false;
        }
    }
    return true;
}

function trim(text)
{
    text = text.replace(/^ +/, "");
    //去除串首空格
    text = text.replace(/ +$/, "");
    //去除串尾空格
    return text;
}
function regetcode()
{
getcodeok=false;
document.getElementById("getcodeimg").src+=Math.random();
document.getElementById("getcode").value="";
}