
function registryCheck(){
	var canReg=true;
	if($("userId").value==""){
		$("userId").focus();
		alert("請輸入帳號");
		return false;
	}else{
		new Ajax.Request('./ajax/registryFunc.php', {
				method: 'post',
				parameters: {'action':'isUserIdValid','userId':$("userId").value}
				,asynchronous:false
				,onSuccess: function(msg) {
					if (msg.responseText=="false"){
						$("userId").focus();
						alert("帳號已曾登記");
						canReg=false;
					}
				}
			});
	}
	if(!canReg)return;
	
	if($("email").value==""){
		$("email").focus();
		alert("請輸入電郵地址");
		return false;
	}
	if(!checkEmail($("email").value,false)){
		$("email").focus();
		alert("請確定電郵地址正確");
		return false;
	}else{
		new Ajax.Request('./ajax/registryFunc.php', {
				method: 'post',
				parameters: {'action':'isEmailValid','email':$("email").value}
				,asynchronous:false
				,onSuccess: function(msg) {
					if (msg.responseText=="false"){
						$("email").focus();
						alert("電郵地址已曾登記");
						canReg=false;
					}
				}
			});
	}
	if(!canReg)return;
	
	if($("password1").value==""){
		$("password1").focus();
		alert("請輸入密碼");
		return false;
	}
	if($("password2").value==""){
		$("password2").focus();
		alert("請輸入確認密碼");
		return false;
	}
	if($("password1").value!=$("password2").value){
		$("password2").focus();
		alert("密碼和確認密碼不相符");
		return false;
	}
	if($("agreeStatment").checked==false){
		alert("必須接受服務條款才註冊");
		return false;
	}
	$("formAction").value="add";
	$("form").submit();
}

function registrycConfirm(){
	if($("userId").value==""){
		$("userId").focus();
		alert("請輸入帳號");
		return false;
	}
	if($("password1").value==""){
		$("password1").focus();
		alert("請輸入密碼");
		return false;
	}
	$("formAction").value="confirm";
	$("form").submit();
}

function forgetPasswordCheck(){
	if($("userId").value==""){
		$("userId").focus();
		alert("請輸入帳號");
		return false;
	}
	if(!checkEmail($("email").value,false)){
		$("email").focus();
		alert("請確定電郵地址正確");
		return false;
	}
	$("formAction").value="forget";
	$("form").submit();
}

function checkEmail (theField, emptyOK){ 

 if (checkEmail.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) 
		return true;
    else if (!isEmail(theField, false)) 
       return false;
    else 
		return true;
}

function isEmail (s){
   if (isEmpty(s)) 
       if (isEmail.arguments.length == 1) return defaultEmptyOK;
    else
		return (isEmail.arguments[1] == true);
	
    // is s whitespace?
    if (isWhitespace(s)) return false;
    
    // there must be >= 1 character before @, so we
    // start looking at character position 1 
    // (i.e. second character)
    var i = 1;
    var sLength = s.length;

    // look for @
    while ((i < sLength) && (s.charAt(i) != "@"))
    { i++
    }

    if ((i >= sLength) || (s.charAt(i) != "@")) return false;
    else i += 2;

    // look for .
    while ((i < sLength) && (s.charAt(i) != "."))
    { i++
    }

    // there must be at least one character after the .
    if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
    else return true;
}

function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}

function isWhitespace (s)

{   var i;

    // Is s empty?
    if (isEmpty(s)) return true;

    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);

        if (whitespace.indexOf(c) == -1) return false;
    }

    // All characters are whitespace.
    return true;
}

var whitespace = " \t\n\r";
