/*     
	2/4/09
	Form Validator
	Jquery plugin for form validation and quick contact forms
	Copyright (C) 2009 Jeremy Fry. www.jeremy-fry.com

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

jQuery.iFormValidate = {
    build : function(user_options) {
    var defaults = {
            ajax: true,
            validCheck: false,
            phpFile:"/js/formvalidator/send.php"
    };

    return $(this).each(
            function() {

                var options = $.extend(defaults, user_options);

                if(options.validCheck){

                    var $inputs = $(this).find(":input").filter(":not(:submit)").filter(":not(:checked)").filter(":not(.novalid)");

                } else {

                   var $inputs = $(this).find(":input").filter(":not(:submit)").filter(":not(:checked)");
                }

                //catch the submit
                $(this).submit(function(){

                    //we need to do a seperate analysis for checboxes
                    var $checked = $(this).find(":checked");

                    //we test all our inputs
                    var isValid = jQuery.iFormValidate.validateForm($inputs);
                    
                    //if any of them come back false we quit
                    if(!isValid){

                        $("#erro").removeClass("esconde").addClass("mostra");

                        $("html, body").animate({
                            scrollTop: $("#enquete").offset().top
                        }, 1000);

                        return false;

                    } else {

                        $("#erro").removeClass("mostra").addClass("esconde");

                     }

                    if(options.ajax){
                        var data = {};

                        $inputs.each(function(){
                            data[this.name] = this.value;
                        });

                        $checked.each(function(){
                            if($(this).is(':checked')){

                                data[this.name] = this.value;

                            } else {

                                    data[this.name] = "";

                            }
                        });

                        $("#enquete_dados").fadeOut("slow", function(){
                            $("#enquete_dados").load(options.phpFile, data, function(){
                                $("#enquete_dados").fadeIn("slow");
                            });
                        });

                            return false;

                    } else {

                            return true;

                    }
            });

            $inputs.bind("keyup", jQuery.iFormValidate.validate);
            $inputs.filter("select").bind("change", jQuery.iFormValidate.validate);

        });
    },

    validateForm : function($inputs) {

            var isValid = true; //benifit of the doubt?
            $inputs.filter(".is_required").each(jQuery.iFormValidate.validate);
            if($inputs.filter(".is_required").hasClass("invalid")){isValid=false;}
            return isValid;

    },
		
    validate : function(){

        var isValid = true;

        if (!$("input[type=radio][name=enq_opcao]:checked").val()) {
            isValid = false;
        }

        if(isValid){

            if($(this).hasClass('is_required')){
                $(this).removeClass("invalid").addClass("valid");
            }

        } else {

            if($(this).hasClass('is_required')){
                $(this).removeClass("valid").addClass("invalid");
            }

        }
    }
}
jQuery.fn.FormValidate = jQuery.iFormValidate.build;
