var es_join = function() {
    return {
        init : function() {
            $('#submit_button').bind('click', es_join.submit);
        }
        ,submit: function() {
            $("#join_form").submit(function() {
                    return false;
                });

            $("#join_form").validate({
                    rules: {
                        password: {
                            required: true,
                            minlength: 6
                        }
                        ,verify : {
                            required: true,
                            equalTo:'#password' 
                        }
                        ,email: {
                            required: true,
                            email: true
                        }
                        ,first_name : {required:true}
                        ,last_name : {required:true}
                        ,address : {required:true}
                        ,city : {required:true}
                        ,state : {required:false}
                        ,zip : {required:true}
                        ,institution : {required:true}
                        ,res_interest : {required:true}
                        ,wno : {required:true}
                        ,fax : {required:true}
                    }
                    ,messages: {
                        name: "Please specify your name",
                        password: "Your password is too short",
                        email: {
                            required: "We need your email address to contact you",
                            email: "Your email address must be in the format of name@domain.com"
                        }
                    }
                    ,errorElement: "div"
                    ,errorClass: "join_error"
                    ,errorPlacement: function(error, element) {
                        error.appendTo( element.parent("td") );
                    }
                    ,submitHandler: function(form) {
                        terms = $('#join_form').serializeArray();

                        $.ajax({
                                url: 'submit/', 
                                data: terms, 
                                dataType:'json',
                                type:'post',
                                success: function(d) {
                                    if(d.success == 1) {
                                        document.location.href = 'thanks/';
                                    }
                                    else if(d.errors) {
                                        if(d.errors.DUPLICATE_ACCOUNT == 1) {
                                            alert('Sorry, but that email address is already in use.');
                                        }
                                    }
                                }
                            });
                    }
                });



//            terms = $('#form').serializeArray();
//            var missing = '';
//            $.each(terms, function(a,b,c) {
//                    if(b.value == '' && b.name != 'fax') {
//                        missing = missing+$('#'+b.name).attr('realname')+", ";
//                    }
//                });
//            if(missing != '') {
//                missing = missing.substring(0, missing.length-2);
//                alert("You must fill the following fields: "+missing);
//            }
//            else if($('#password').val() != $('#verify').val()) {
//                alert('Your password does not match in both fields.');
//            }
//            else {
//            }
        }
    }
}();
$(es_join.init);
 

