var form = {
    validated: true,
    form_id: null,
    init: function(){
        form.form_submit();
    },
    form_submit: function(){
        $('.form_error_message').hide();
        $('.success_message_big').hide();
        $('.generic_form').submit(function(e){
            e.preventDefault();
            form.form_id = $(this).attr('id');
            form.validated = true;
            $('.required', '#'+$(this).attr('id')).each(function(){
                if($(this).val() == '')
                {
                    form.validated = false;
                    $('label[for='+$(this).attr('id')+']').addClass('form_error');
                    $(this).addClass('field_error');
                    $('.form_error_message[rel='+form.form_id+'] span').html('You have not filled in one or more required fields');
                    $('.form_error_message[rel='+form.form_id+']').show();
                }
                else
                {
                    $('label[for='+$(this).attr('id')+']').removeClass('form_error');                
                    $(this).removeClass('field_error');
                }
                if($(this).hasClass('email'))
                {
                    var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
                    if(!pattern.test($(this).val()))
                    {
                        form.validated = false;
                        $('label[for='+$(this).attr('id')+']').addClass('form_error');
                        $(this).addClass('field_error');
                        $('.form_error_message[rel='+form.form_id+'] span').html('You have not entered a valid email address');
                        $('.form_error_message[rel='+form.form_id+']').show();
                    }
                    else
                    {
                        $('label[for='+$(this).attr('id')+']').removeClass('form_error');                
                        $(this).removeClass('field_error');
                    }
                    
                }
                
            });
            if(form.validated)
            {
               $('.form_error_message[rel='+form.form_id+']').hide();
               $.ajax({url: $(this).attr('action'),
                       dataType: 'json',
                       data: $(this).serialize(),
                       type: 'POST',
                       success: function(response){
                            if(response.error == "true")
                            {
                                $('.form_error_message[rel='+form.form_id+'] span').html(response.msg);
                                $('.form_error_message[rel='+form.form_id+']').show();                                
                            }
                            else
                            {
                                $('.sub_form[rel='+form.form_id+']').remove();
                                $('.success_message_big[rel='+form.form_id+'] span').html(response.msg);
                                $('.success_message_big[rel='+form.form_id+']').show();                                
                            }
                            Cufon.refresh('cufon_text');
                       
                       }
               });           
            }        
        });
        $('.generic_submit').click(function(e){
            e.preventDefault();
            $('#'+$(this).attr('rel')).submit();
        });
    }
}
