/* 
 * Validate form plugin
 * @author ash
 * 
 * hash parametrs
 * 
 * id       :'#element1, #elementn'
 * style    :'border style'
 * el_style :'border style'
 * 
 * Examples
 * 
 * $('#au_form').validateForm({id:'#micro_words, #micro_site'});
 * $('#words_form').validateForm();
 * 
 */


(function( $ ){

  $.fn.validateForm = function(hash) {
    var form = this;     // this form
    
    if(!hash){
        hash = {id:false, style:false, el_style:false};
    }
    
    var style = (hash.style) ? hash.style : '1px solid red' ;
    var el_style = (hash.el_style) ? hash.el_style : '' ;
    
    $(this).submit(function(){
        
        var not_send = false;        // Not send flag
        
        if(hash.id){
            var elements = hash.id.split(',');
            
            // Find all input elements
            $(form).find(":input").each(function(e){
                
                // Find all elements in hash
                for(var i in elements){
                    if(this.id == elements[i].trim().substr(1)){
                        // If border style
                        if($(this).css('border') == style){
                            $(this).css({'border':el_style});
                        }
                        // if field empty
                        if(!this.value){
                            not_send = true;
                            $(this).css({'border':style});
                        }
                    }
                }
            }); 
        }
        else{
            // Find all input elements
            $(form).find(":input").each(function(e){
                
                if(this.type == 'textarea' || this.type == 'text' || this.type == 'password'){
                    
                    // If border style
                    if($(this).css('border') == style){
                        $(this).css({'border':el_style});
                    }
                    // if field empty
                    if(!this.value){
                        not_send = true;
                        $(this).css({'border':style});
                    }
                }
            }); 
        }
       
       if(not_send) return false;
       
    });

  };
})( jQuery );
