<!-- 

/* 
    A Better Form - A jQuery plugin
    ==================================================================
    ©2010 JasonLau.biz - Version 1.1.5
    ==================================================================
    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/>.
*/

(function($){
    
 	$.fn.extend({ 
 	  
 		abform: function(options){
 		 
			var defaults = {
			 
			    ajax_form : false,
                attributes : 'action="#" method="post"',
                clickonce : true,
                convert : false,
                custom_classes : false,
                filtertext : true,
                form_id : randomString(10),
                html : '',
                invalid_alert : true,
                json : false,
                json_form : false,
                multipart : false,
                pluggable : false,
                sequential_disable : true,              
                serialized : true,
                submit_class : 'absubmit',
                textfilters : 'url=,link=,http:,www.,href,<a',
                alert_invalid_email : 'Invalid email!',
                alert_invalid_url : 'Invalid URL address!',
                alert_required_field : 'Field is required!',
                alert_invalid_length : 'Field must be at least $ characters in length!',
                alert_invalid_content : 'Invalid field content!',
                alert_invalid_characters_numbers : 'Field accepts numbers only!',
                alert_invalid_characters_letters : 'Field accepts letters only!',
                alert_invalid_characters : 'Field accepts letters and numbers only!',
                alert_invalid_username : 'Field accepts letters, numbers, and underscores only!',
                alert_failed_test : 'Field does not accept the following characters $',
                alert_special_chars : 'Special characters were automatically removed. Try again.'                                  
			}
            				
			var options =  $.extend(defaults, options), obj = $(this), id = obj.attr('id');
                
                if(options.html){
                    
                    obj.html(options.html);
                    activate(id);
                    
                } else if(options.ajax_form){

                    $.ajax({
                        url: options.ajax_form,
                        success: function(data) {
                            obj.html(data);
                            activate(id);
                        }
                    });

                } else if(options.json_form){

                    $.getJSON(options.json_form, function(data){
                       obj.html(data.form);
                       activate(id);
                    });

                } 
                
                if(options.convert){
                    
                    var elements = options.convert.split('{');
                    
                    for(var i in elements){
                      if(i > 0){
                        var element = elements[i].split('}');
                      element = element[0];
                      
                      var e = element.split('|');
                      var e_id = (!e[0]) ? alert('Error! Code misconfiguration. Disable A Better Form convert option or check the documentation to learn how to properly code the convert option. You are missing the element id option.') : e[0];
                      var e_type = (!e[1]) ? alert('Error! Code misconfiguration. Disable A Better Form convert option or check the documentation to learn how to properly code the convert option. You are missing the element type option.') : e[1];    
                      var e_attributes = (e[2]) ? ' ' + e[2] : '';
                      var e_value = $("#" + e_id).html();                      
                      var newElement = '';
                      
                      switch(e_type){
                        
                        case 'text':                        
                        newElement = '<input id="' + e_id + '" name="' + e_id + '" type="' + e_type + '" value="' + e_value + '"' + e_attributes + ' />';
                        break;
                        
                        case 'textarea':                        
                        newElement = '<textarea id="' + e_id + '" name="' + e_id + '"' + e_attributes + '>' + e_value + '</textarea>';
                        break;
                        
                        case 'password':                        
                        newElement = '<input id="' + e_id + '" name="' + e_id + '" type="' + e_type + '" value="' + e_value + '"' + e_attributes + ' />';
                        break;
                        
                        case 'file':                        
                        newElement = '<input id="' + e_id + '" name="' + e_id + '" type="' + e_type + '" value="' + e_value + '"' + e_attributes + ' />';
                        break;
                        
                        case 'button':                        
                        newElement = '<input id="' + e_id + '" name="' + e_id + '" type="' + e_type + '" value="' + e_value + '"' + e_attributes + ' />';
                        break;
                        
                        case 'submit':                        
                        newElement = '<input id="' + e_id + '" name="' + e_id + '" type="' + e_type + '" value="' + e_value + '"' + e_attributes + ' />';
                        break;
                        
                        case 'reset':                   
                        newElement = '<input id="' + e_id + '" name="' + e_id + '" type="' + e_type + '" value="' + e_value + '"' + e_attributes + ' />';
                        break;
                        
                        case 'image':                        
                        newElement = '<input id="' + e_id + '" name="' + e_id + '" type="' + e_type + '" src="' + e_value + '"' + e_attributes + ' />';
                        break;
                                                
                        case 'radio':                        
                        newElement = '<input id="' + e_id + '" type="' + e_type + '" value="' + e_value + '"' + e_attributes + ' />';
                        break;
                        
                        case 'checkbox':                        
                        newElement = '<input id="' + e_id + '" type="' + e_type + '" value="' + e_value + '"' + e_attributes + ' />';
                        break;
                        
                        case 'select':                        
                        newElement = '<select id="' + e_id + '" name="' + e_id + '"' + e_attributes + '>';
                        $("#" + e_id + " ul").each(function(){
                            if($(this).attr('title')){
                              newElement += '<optgroup label="' + $(this).attr('title') + '">';  
                            }                       
                            
                        $("#" + e_id + " ul li").each(function(){
                            newElement += '<option value="' + $(this).attr('id') + '">' + $(this).html() + '</option>'; 
                        });
                        
                        if($(this).attr('title')){
                              newElement += '</optgroup>';  
                            }
                        });
                        newElement += '</select>'; 
                        
                        break;
                        
                        case 'hidden':                        
                        newElement = '<input id="' + e_id + '" name="' + e_id + '" type="' + e_type + '" value="' + e_value + '"' + e_attributes + ' />';
                        break;
                        
                        
                      }
                      
                      $("#" + e_id).replaceWith(newElement);
                                            
                      }                    
                  }
                  activate(id);
                }
                
             function activate(id){  
            
                var all_fields = $('#'+id+' input, #'+id+' textarea, #'+id+' select'), all_ids = [];
                $('#'+id+' input, #'+id+' textarea, #'+id+' select').each(function(a){
                    
                    if($(this).is('input') && ($(this).is('input:radio') || $(this).is('input:checkbox'))){
                    if($(this).attr('rel')){
                        $("input[rel='"+$(this).attr('rel')+"']").each(function(){
                           $(this).attr('name',$(this).attr('rel')); 
                        });                      
                    } else {
                        $(this).attr('name',$(this).attr('id'));
                    }
                    
                    $(this).not("input:checkbox").bind('change',function(){
                       if($(this).is(':checked')){
                        $("input[name='" + $(this).attr('rel') + "']").not(this).attr('checked','');                       
                       } 
                    });                   
                }
                    var next_item = a+1;
                    var next_id = $('#'+all_ids[next_item]).attr('id'); 
  
                    if($(this).is('select') || ($(this).is('input') && ($(this).is('input:file') || $(this).is('input:radio') || $(this).is('input:checkbox')))){
                        
                        $(this).bind('change',function(){
                            if($(this).is('select')){                           
                            if($(this).val() != ''){
                                
                                $(this).attr('name',$(this).attr('id'));
                                $('#'+all_ids[next_item]).attr('disabled', '');
                                                                            
                            } else {
                                
                                $(this).attr('name','');                               
                             
                            }
                            
                            }
                            
                            if($(this).is(':checkbox') && $(this).hasClass('abcheckall')){
                                var cb_id = $(this).attr('id');
                                $('#' + id + ' input:checkbox').not(this).each(function(){
                                    if($("#" + cb_id).is(':checked')){
                                        $(this).attr('checked','checked');
                                    } else {
                                        $(this).attr('checked','');
                                    }
                                });
                            }
                            
                            if(options.sequential_disable){ seq_dis(id, all_ids); }
                                                    
                        });
                    }
                    
                    if($(this).is('input, textarea') && !$(this).is('input:file') && !$(this).is('input:radio') && !$(this).is('input:checkbox')){
                        
                        $(this).bind('keyup change',function(){
                            
                            if($(this).val() != ''){
                                
                                if(options.filtertext){
                                    
                                  var t_filters = options.textfilters.split(',');
                                  
                                  for(var i in t_filters){
                                    
                                    var checkit = $(this).val().split(t_filters[i]);
                                    
                                    if(checkit.length > 1){
                                        
                                        $(this).val(checkit[0]);
                                        
                                    }
                                  }  
                                    
                                }
                                                                
                                $('#'+all_ids[next_item]).attr('disabled', '');
                                $(this).attr('name',$(this).attr('id')); 
                                
                            } else {
                                
                                $(this).attr('name','');
                                                                 
                            }
                            
                            if(options.sequential_disable){ seq_dis(id, all_ids); } 
                                                   
                        });
                    } 
                    
                    
                    all_ids.push($(this).attr('id'));
                
                });
                
                if(options.sequential_disable){
                    
                   all_fields.attr('disabled', 'disabled');
                   all_fields.first().attr('disabled', '');
                   seq_dis(id, all_ids);
                    
                }
                                
                $('#'+id+' optgroup').each(function(i){
                    
                    if($(this).hasClass('aboptgroup')){
                        
                        var this_html = $(this).html();
                        $(this).replaceWith('\n<optgroup label="' + $(this).attr('id') +'">\n'
                                            + this_html
                                            + '\n</optgroup>\n');  
                        $(this).attr('label',$(this).attr('id'));
                        
                    } else {
                        
                        $(this).attr('name',$(this).attr('id'));
                                               
                    }
                });
                               
                $('.' + options.submit_class).mouseup(function(){
                    all_fields.removeClass('aberror');
                    var formid = $("#"+obj.attr('id')+" form").attr('id');
                    var pass = true;
                    pass = pass && $(this).is(':disabled') ? false : true;                    
                    if(formid == undefined){
                       pass = pass && ($(this).parent().attr('id') == obj.attr('id')) ? true : false;                        
                    } else {                        
                       pass = pass && (formid == $(this).parent().attr('id')) ? true : false; 
                    }
                    if(pass){
                    $('.abemail').each(function(){
                        regexp = /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i;
                        if(!(regexp.test($(this).val()))){
                            pass = false;
                            if(options.invalid_alert) alert(options.alert_invalid_email);
                            $(this).addClass('aberror');
                        }
                    });
                    
                    $('.aburl').each(function(){
                        regexp = /^(https?|ftp|mms):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i;
                        if(!(regexp.test($(this).val()))){
                            pass = false;
                            if(options.invalid_alert) alert(options.alert_invalid_url);
                            $(this).addClass('aberror');
                        }
                    });
                    
                    $('.abrequired').each(function(){
                        if($(this).val().length < 1){
                            pass = false;
                            if(options.invalid_alert) alert(options.alert_required_field);
                            $(this).addClass('aberror');
                        }
                    });
                    
                    $('.ablength').each(function(){
                        var req_range = $(this).attr('rel').split(','),
                        range_bottom = (req_range[0]) ? req_range[0] : 1;
                        
                        if($(this).val().length > req_range[1] || $(this).val().length < req_range[0]){
                            pass = false;
                            if(req_range[1]){
                               if(options.invalid_alert) alert(options.alert_invalid_length.replace('$',range_bottom +' - '+ req_range[1])); 
                            } else {
                              if(options.invalid_alert) alert(options.alert_invalid_length.replace('$',range_bottom));  
                            }
                            
                            $(this).addClass('aberror');
                        }
                    });
                    
                    if(options.custom_regexp != ''){
                       $('.abcustom').each(function(){
                        regexp = eval($(this).attr('rel'));
                        if(!(regexp.test($(this).val()))){
                            pass = false;
                            if(options.invalid_alert) alert(options.alert_invalid_content);
                            $(this).addClass('aberror');
                        }
                    }); 
                    }
                    
                    $('.abnumbers').each(function(){
                        if(isNaN(parseInt($(this).val()))){ // numbers
                            pass = false;
                            if(options.invalid_alert) alert(options.alert_invalid_characters_numbers);
                            $(this).addClass('aberror');  
                        }
                    });
                    
                    $('.abletters').each(function(){
                        var validChars = /^[a-zA-Z]+$/;
                        if($(this).val().match(validChars)){
                            // passed
                        } else {
                            pass = false;
                            if(options.invalid_alert) alert(options.alert_invalid_characters_letters);
                            $(this).addClass('aberror');
                        }
                    });
                    
                    $('.aballnum').each(function(){
                        var invalidChars = /[\W_]/; // letters and numbers
                        if(invalidChars.test($(this).val())){
                            pass = false;
                            if(options.invalid_alert) alert(options.alert_invalid_characters);
                            $(this).addClass('aberror');  
                        }
                    });
                    
                    $('.abusername').each(function(){
                        var invalidChars = /\W/; // letters, numbers, and underscores
                        if(invalidChars.test($(this).val())){
                            pass = false;
                            if(options.invalid_alert) alert(options.alert_invalid_username);
                            $(this).addClass('aberror');  
                        }
                    });
                    
                    $('.abtest').each(function(){
                        if($(this).attr('rel')){
                            for(var i = 0; i < $(this).val().length; i++){
                                if ($(this).attr('rel').indexOf($(this).val().charAt(i)) != -1){
                                    pass = false;
                                    if(options.invalid_alert) alert(options.alert_failed_test.replace('$',$(this).attr('rel')));
                                    $(this).addClass('aberror');
                                }
                            }
                        }
                    });
                    
                    $('.abnospecial').each(function(){
                        var original = $(this).val();
                        spec_chars = /\$|,|@|#|~|`|\%|\*|\^|\&|\(|\)|\+|\=|\[|\-|\_|\]|\[|\}|\{|\;|\:|\'|\"|\<|\>|\?|\||\\|\!|\$|\./g;
                        $(this).val($(this).val().replace(spec_chars, ""));
                        if($(this).val() != original){                          
                           if(options.invalid_alert) {
                            alert(options.alert_special_chars);
                            pass = false;
                           }
                           $(this).addClass('aberror'); 
                        }
                    });
                   
                    if(options.custom_classes){
                        var c_classes = options.custom_classes.split('{');
                        c_classes.shift();
                        for(var i in c_classes){
                            var cclass = c_classes[i].split(':');                           
                            var cname = cclass[0];
                            myFunction = eval(cclass[1].replace('}',''));
                            var alert_language = cclass[2];
                            if($.isFunction(myFunction)){
                                $('.' + cname).each(function(){
                                    var myCallback = myFunction($(this).attr('id'),$(this).val());
                                    if($.type(myCallback) === 'string'){
                                        if(options.invalid_alert){
                                            alert(myCallback);
                                            pass = false;
                                        }
                                        $(this).addClass('aberror');
                                    }
                                }); 
                            }                                                  
                        }
                    }
                                        
                    if(pass){
                                      
                        $('#'+id+' input').each(function(){
                            if($(this).is(':hidden')){
                              $(this).attr({
                                disabled : '',
                                name : $(this).attr('id')
                                });  
                            }
                        });
                        $('#'+id+' input').is(':hidden')
                        var innerWrap = (options.multipart) ? "<form id=\"" + options.form_id + "\" " + options.attributes + " enctype=\"multipart/form-data\"></form>" : "<form id=\"" + options.form_id + "\" " + options.attributes + "></form>" ;
                        
                        if(!obj.has("form").length){
                           obj.wrapInner(innerWrap);
                        } 
                                               
                        if(options.pluggable && $.isFunction(options.pluggable)){
                            
                            if(options.serialized && !options.json){
                                
                                return options.pluggable($(this).parent().serialize());
                                 
                            } else if(options.json) {
                                
                                s = $(this).parent().serialize().split('&');
                                d = '{';
                                c = s.length-1;
                                for(var i in s){
                                    f = s[i].split('=');
                                    d += '"'+f[0]+'":"'+f[1]+'"'
                                    if(i < c){
                                      d += ',';  
                                    }
                                }
                                d += '}';
                                return options.pluggable(d);
                                
                            } else {
                                
                                return options.pluggable();
                                
                            }                      
                            
                        } else {
                            
                           $(this).parent().submit(); 
                           
                        }
                    }
                    }
                    if(options.clickonce){
                        
                       $(this).attr('disabled','disabled'); 
                        
                    }
                     
                });

            } // activate 
             
            function seq_dis(id, all_ids){
                $('#'+id+' input, #'+id+' textarea, #'+id+' select').each(function(b){
                    
                    var sd = false;
                    
                    for(var c in all_ids){
                        
                        if(c < b){
                            
                            if($('#'+all_ids[c]).val() == ''){
                                
                                sd = true;
                                
                            }
                        }
                    }
                    
                    if(sd){
                        
                        $('#'+all_ids[b]).attr('disabled', 'disabled');
                        
                    } else {
                        
                        if($(this).is('input') && ($(this).is('input:radio') || $(this).is('input:checkbox'))){
                            
                          $('[id=' + $(this).attr('id') + ']').each(function(){
                            
                            $(this).attr('disabled', '');
                            
                          }); 
                           
                        } else {
                            
                          $('#'+all_ids[b]).attr('disabled', '');  
                          
                        }
                        
                    } 
                                          
                });
            };
            
            function randomString(length) {
                var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz'.split('');
                if (! length) {
                    length = Math.floor(Math.random() * chars.length);
                    }
                    var str = '';
                    for (var i = 0; i < length; i++) {
                        str += chars[Math.floor(Math.random() * chars.length)];
                    }
                return str;
            }
                                  
 		}
	});	
    
})(jQuery);

 -->
