// Must include jQuery and jQuery maskedInput

//the form
$(document).ready(function(){ //jQuery document.ready function
      
    var $form = $("#quick_contact"); //This is where the name of the form goes.  

    $form.find("input[name=phone]").mask("?(999) 999-9999");
    $form.find("input[name=cell]").mask("?(999) 999-9999");
    $form.find("input[name=fax]").mask("?(999) 999-9999");
    
    $form.find(':input').each(function(){
        $(this).focus(function(){
            $(this).removeClass('error');
        });
    });
    
    $form.submit( function(){
        $(this).find('#submit').empty().html('<img src="ajax-loader.gif" alt="loading" />');
        $("#error_area").empty(); //This clears the error_area or the area where error messages will be displayed
        var $error = false; //If $error remains false the form will validate and submit
        $(this).find(":input").each(function(){
            $(this).removeClass('error'); //removes the error class from the input elements while we try validation
            var $rule = $(this).attr('validation'); //This is the attribute added to each of the input elements that specifies what rules they must have
            var $string = $(this).val();
            if(!validate($string,$rule)){
                $(this).addClass('error');
                $error = true;
            }
        });
        if(!$error){
            $.post('includes/form_mailer.php',$(this).serialize(),function(data){  //this $.post function is what happens when the form validates as true
                if(data == 'success'){
                      $form.empty().html('<p class="smallGutter subTitle01">Thank you.  Someone will be in touch with you shortly.</p>');
                }else{
                    alert(data); //alerts if there is an error
                    $('#submit').html('<input type="submit" name="Submit" value="Submit" style="float: right; margin-right: 55px;" />');
                }
            });    
        }else{
//            alert('There were some problems with the form, they have been highlighted');  //alerts if there are problems with the form
            $("#error_area").html('There were some problems with the form, they have been highlighted'); //sets the error-area to display errors with the form
            $('#submit').html('<input type="submit" name="Submit" value="Submit" style="float: right; margin-right: 55px;" />');
        }
        
        return false;
    });
    
    function validate($string,$rule){   //function that validates data - always returns true if their are no validation requirements
            switch($rule){
                case 'alpha':
                    var $regex ='[a-zA-Z]+';
                    break;
                case 'alphanum':
                    var $regex ='[a-zA-Z0-9]+';
                    break;
                case 'num':
                    var $regex ='[0-9]+';
                    break;
                case 'zip':
                    var $regex ='[0-9]{5}';
                    break;
                case 'email':
                    var $regex = '[a-zA-Z0-9\.\-\_]+@[a-zA-Z0-9\.\-]+[.][a-zA-Z]{2,3}';
                    break;
                case 'phone':
                    var $regex = "[(][0-9]{3}[)] [0-9]{3}-[0-9]{4}";
                    break;
                default:
                    return true;
            }
            if($string.search($regex) < 0){
                return false;
            }else{
                return true;
            }
}
     
});