function valid(){
    $("#submitForm").submit(function(){
        var result=true;
        $('#error_title').html("<span class='error ui-state-highlight'></span>");
        $(".error").css("color","red");
        $(".error").css("display","none");
        $(".error").html("不能空白.");
        $('.valid').removeClass( "ui-state-error" );
        $(this).find(".valid").each(function(){
            var name=$(this).attr("name");
            if(name.match("email")){
                if(!emailFormat($(this).val())){
                    $(this).addClass( "ui-state-error" );
                    $(".error").show();
                    result=false;
                    return false;
                }
            }else if(name.match("code")){
                if(!checkVerifCode()){
                    $(this).addClass( "ui-state-error" );
                    $(".error").show();
                    result=false;
                    return false;
                }
            }else{
                if(isEmpty($(this).val())){
                   $(this).addClass( "ui-state-error" );
                    $(".error").show();
                    result=false;
                    return false;
                }
            }
        });
        return result;
    });
}

function isEmpty(content){
    if(content==null||content.lenght==0||content==""){
            return true;
    }else{
            return false;
    }
}

function emailFormat(content){
    if(isEmpty(content)){
        return false;
    }else{
        if(!content.match(/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/)){
            $(".error").text("請輸入正確電郵地址.");
            return false;
        }
    }
    return true;
}

function checkVerifCode(){
    $.ajax({
        url: 'index.php?action=verify_code&code='+$("#code").val(),
        type:"get",
        dataType: "html",
        async:false,
        success: function(response) {
                output = response;
        },
        complete: function() {
               if(output.match("false")){
                   $(".error").text("不正確");
                   $(".error").css("color","red");
                    $(".error").show();
                   result=false;
               }else{
                    result=true;
               }
        }

    });
    return result;
}
