/**
 * @author Shawn Welch <shrimpwagon@yahoo.com>
 */
$(document).ready(function(){
    $.UserRegister = new Object();
    
    $.UserRegister.show = function(){
        //$('#colorbox-modal').load('/ajax/ajaxRegister.php', null, function(){
        $.fn.colorbox({
            href: '/ajax/ajaxRegister.php',
            speed: "100",
            width: "900px",
            height: "600px",
            open: true
        }, function(){
            // focus on name box
            document.getElementById('customer_name').focus();
            
            $('#register-form').validate({
				onfocusout: false,
				onkeyup: false,
                rules: {
                    customer_name: "required",
                    email_address: "email required",
                    email_address_confirm: {
                        equalTo: "#email_address",
						required: true
                    },
                    password: "required",
                    password_confirm: "required"
                },
				messages: {
					customer_name: "Customer Name is required",
					email_address: "Valid Email Address is required",
					email_address_confirm: {
						equalTo: "Emails do not match",
						required: "Must confirm email address"
					},
					password: "Password is required",
					password_confirm: "Must confirm password"
				},
                submitHandler: function(form){
                    $('#cboxLoadedContent').block({
                        message: '<img src="/themes/default/images/ajax-loader.gif" />',
                        css: {
                            backgroundColor: 'transparent',
                            border: 'none'
                        }
                    });
                    
                    $('#register-form').ajaxSubmit({
                        dataType: 'json',
                        success: function(data){
                            if (data.status == 'success') {
                                window.location.reload(true);
                            }
                            else {
                                alert(data.message);
                                $('#cboxLoadedContent').unblock();
                            }
                        }
                    });
                },
                showErrors: function(errorMap, errorList){
                    if (errorList.length > 0) {
                        var message = "Please fix the following errors:\n\n"
                        for (key in errorList) {
                            // Add to message
                            message += '- ' + errorList[key].message + "\n";
                            
                            // Highlight red
                            $(errorList[key].element).css('background-color', '#FFEEEE');
                        }
                        alert(message);
                    }
                }
            });
        });
    }
});
