$(document).ready(function(){
    bindLabelClick();
    bindJoinTable();
});

function bindLabelClick(){
    var label = $("label.rlabel");
    if(label.size() > 0){
        label.bind("click", function(){
            $(this).siblings("input").attr("checked", true);
        });
    }
}

function bindJoinTable(){
    $("#joinTable table").bind("mouseenter", function(){
        $(this).css("background", "#eef8fd");
    }).bind("mouseleave", function(){
        $(this).css("background", "transparent");
    });
}

function cChange(obj){
    var t = $(obj.options[obj.selectedIndex]).attr("rel");
    if (t.indexOf("-") > 0){
        $("input[name=c_number]").val(t.split("-")[0]);
        $("input[name=a_number]").val(t.split("-")[1]);
    }else if(t.length > 0){
        $("input[name=c_number]").val(t);
        $("input[name=a_number]").val("");
    }else{
        $("input[name=c_number]").val("");
        $("input[name=a_number]").val("");
    }
    $("#countrySelect").siblings("img").remove();
    $("#countrySelect").after(" <img src=\"http://img.mysoocuu.com/flag/"+obj.value.toLowerCase()+".png\" alt=\"\" />");
}

function checkForm(){
    memberId = $("input[name=member_id]");
    a = $("input[name=name]");
    b = $("input[name=company_name]");
    c_n = $("input[name=c_number]");
    a_n = $("input[name=a_number]");
    t_n = $("input[name=t_number]");
    email = $("input[name=email]");
    pass = $("input[name=password]");
    repass = $("input[name=repassword]");
    code = $("input[name=code]");
    if ((new RegExp(/^[0-9a-z][0-9a-z\-]+[0-9a-z]$/)).test($.trim(memberId.val()))){
        if ($.trim(memberId.val()).length < 6 || $.trim(memberId.val()).length > 20){
            $.iAlert({text:"Member ID must be 6 - 20 characters (A-Z, a-z, 0-9 only)", onClose:function(){
                memberId.get(0).focus();
            }});
            return false;
        }else{
            if ($.trim(a.val()).length == 0){$.iAlert({text:'Should not be left Your Name blank!', onClose:function(){a.get(0).focus();}});return false;}
            if ($.trim(b.val()).length == 0){$.iAlert({text:"Should not be left Company Name blank!", onClose:function(){b.get(0).focus();}});return false;}
            if ($.trim(c_n.val()).length == 0 || /[^0-9]/.test(c_n.val())){$.iAlert({text:"Country code must be numeric!", onClose: function(){c_n.get(0).focus();}});return false;}
            if ($.trim(a_n.val()).length == 0 || /[^0-9]/.test(a_n.val())){$.iAlert({text:"Area code must be numeric!", onClose: function(){a_n.get(0).focus();}});return false;}
            if ($.trim(t_n.val()).length == 0 || /[^0-9]/.test(t_n.val())){$.iAlert({text:"Telephone number must be numeric!", onClose: function(){t_n.get(0).focus();}});return false;}
            if (!(new RegExp(/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/)).test(email.val())){$.iAlert({text:"Email Address is not correct", onClose: function(){email.get(0).focus();}});return false;}
            if ($.trim(pass.val()).length < 6||$.trim(pass.val()).length>20||(new RegExp(/[^0-9a-zA-Z]/)).test(pass.val())){$.iAlert({text:"Password must be 6 - 20 characters (A-Z, a-z, 0-9 only)", onClose: function(){pass.get(0).focus();}});return false;}
            if ($.trim(pass.val())!= $.trim(repass.val())){$.iAlert({text:"two password are different", onClose: function(){pass.get(0).focus();}});return false;}
            if ($.trim(code.val()).length != 5){$.iAlert({text:"The Auth Code is not a valid number.", onClose: function(){code.get(0).focus();}});return false;}
            $.ajax({
                url: "lib/ajax/join/checkCode.php",
                type:"post",
                data:"code="+code.val(),
                success: function(text){
                    if (text == "1"){
                        $.ajax({
                            url: "lib/ajax/join/isExistsMember.php",
                            type: "post",
                            data: "m="+$.trim(memberId.val()),
                            success: function(res){
                                if (res == "0"){
                                    datas = $("#baseForm").serialize() + "&s_country=" + $("select[name=country] option:selected").text();
                                    $.ajax({
                                        url: "lib/ajax/join/addMember.php",
                                        type: "post",
                                        data: datas,
                                        success: function(t){
                                            if (t == "-1"){
                                                $.iAlert({text:"<span style='color:#900'>"+memberId.val()+"</span> 's email has been exist.", onClose:function(){
                                                    memberId.get(0).focus();
                                                }});
                                            }else if (t == "1"){
                                                window.location = "join_setup2.php";
                                            }else{
                                                $.iAlert({text: "parameter lost"});
                                            }
                                        },
                                        error: function(){
                                            $.iAlert({text:"System error"});
                                        }
                                    });
                                    return false;
                                }else{
                                    $.iAlert({text:"<span style='color:#900'>"+memberId.val()+"</span> has been exist.", onClose:function(){
                                        memberId.get(0).focus();
                                    }});
                                    return false;
                                }
                            }
                        });
                    }else{
                        $.iAlert({text:"Auth Code is not correct! Change another Image By Click refresh image.", onClose:function(){
                            code.get(0).select();
                        }});
                        return false;
                    }
                },
                error: function(){
                    $.iAlert({text:"System error"});
                }
            });
        }
    }else{
        $.iAlert({text:"Member ID must be 6 - 20 characters (A-Z, a-z, 0-9 only)", onClose:function(){
            memberId.get(0).focus();
        }});
        return false;
    }
}

function readLicence(){
    $.blockUI({message:$('#licence'),centerY:0, css:{'width':'750px', 'height':'500px','overflow':'auto','textAlign':'left','top':($(window).height() - 500) / 2+"px", "left":($(window).width()-750)/2+"px"}});$('.blockOverlay').attr('title','Click to unblock').click($.unblockUI);
}

function loadnewimage(obj){
    $(obj).siblings("img").attr("src", $(obj).siblings("img").attr("src").replace(/(\?).+/, '')+"?"+Math.random());return false;
}

(function($){
    $.iAlert = function($options){
        var $default = {
            title : "Important Information",
            text : "This is an information, You maybe lose something",
            close: "close",
            onClose: function(){}
        }
        var $o = $.extend($default, $options);
        $v = '<div style="background-color:#67a7e3">';
        $v+= '<div style="padding:5px;background-color:#bcdbf3;font-weight:600;';
        $v+= 'color:#0d4a6f;font-size:14px;text-align:left;">'+$o.title+'</div>';
        $v+= '<div style="padding:5px;background-color:#fff;color:#333;';
        $v+= 'font-weight:bold;height:90px;text-align:left;">';
        $v+= '<div style="line-height:30px;height:70px;">'+$o.text+'</div>';
        $v+= '<div style="text-align:right;">';
        $v+= '<a onclick="$.unblockUI({onUnblock:'+$o.onClose+'});" ';
        $v+= 'style="border: 1px solid #aaa;background-color:#f0f0f0;';
        $v+= 'padding: 3px 8px;color:#333;text-decoration:none;font-weight:200" ';
        $v+= 'href="#">'+$o.close+'</a></div></div></div>';
        $.blockUI({
            message: $($v),
            showOverlay: true,
            css: {
                border: '1px solid #3c5f82',
                padding: '5px',
                backgroundColor: '#67a7e3',
                '-webkit-border-radius': '5px',
                '-moz-border-radius': '5px',
                opacity: '1',
                color: '#000' ,
                fontSize: '12px',
                fontFamily: 'verdana,tahoma'
            }
        });
    };
})(jQuery);

function changeImage(){$("#authPic").attr("src",$("#authPic").attr("src").replace(/(\?).+/, '')+"?"+Math.random());return false;}
