<!-- 

/* 
    A Better Form - A jQuery plugin
    ==================================================================
    ©2010 JasonLau.biz - Version 1.1.1
    ==================================================================
    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 = {
			    attributes : 'action="#" method="post"',
                sequential_disable : true,
                pluggable : false,
                serialized : true,
                json : false,
                multipart : false,
                clickonce : true,
                filtertext : true,
                textfilters : 'url=,link=,http:,www.,href,<a',
                convert : false,
                obj :  $(this),
                form_id : randomString(10),
                html : ''                    
			}
            				
			var options =  $.extend(defaults, options);
            var obj = $(this); 
                      
    		return this.each(function(){
    		  
				var o = options,
                attributes = o.attributes,
                sequential_disable = o.sequential_disable,
                pluggable = o.pluggable,
                serialized = o.serialized,
                json = o.json,
                multipart = o.multipart,
                clickonce = o.clickonce,
                filtertext = o.filtertext,
                textfilters = o.textfilters,
                convert = o.convert,
                all_ids = [];
                var id = obj.attr('id');
                
                if(o.html){
                    obj.html(o.html);
                }
                
                if(convert){
                    
                    var elements = 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 + '" name="' + e_id + '" type="' + e_type + '" value="' + e_value + '"' + e_attributes + ' />';
                        break;
                        
                        case 'checkbox':                        
                        newElement = '<input id="' + e_id + '" name="' + 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);
                                            
                      }                    
                  }
                }
                
                var all_fields = $('#'+id+' input, #'+id+' textarea, #'+id+' select');
                $('#'+id+' input, #'+id+' textarea, #'+id+' select').each(function(){
                    
                all_ids.push($(this).attr('id'));
                
                if($(this).is('input') && ($(this).attr('type') == 'radio' || $(this).attr('type') == 'checkbox')){
                    
                    $(this).attr('name',$(this).attr('id'));
                    
                    }
                
                });
                
                if(sequential_disable){
                    
                   all_fields.attr('disabled', 'disabled');
                   all_fields.first().attr('disabled', '');
                   seq_dis(id, all_ids);
                    
                }
                                
                $('#'+id+' input, #'+id+' textarea, #'+id+' select').each(function(a){
                    
                    var next_item = a+1;
                    var next_id = $('#'+all_ids[next_item]).attr('id'); 
  
                    if($(this).is('select') || ($(this).is('input') && ($(this).attr('type') == 'file' || $(this).attr('type') == 'radio' || $(this).attr('type') == 'checkbox'))){
                        
                        $(this).bind('change',function(){
                                                       
                            if($(this).val() != ''){
                                
                                $(this).attr('name',$(this).attr('id'));
                                $('#'+all_ids[next_item]).attr('disabled', '');
                                                                            
                            } else {
                                
                                if($(this).attr('type') != 'radio' || $(this).attr('type') != 'checkbox'){
                                    
                                $(this).attr('name','');  
                                
                                }                              
                             
                            }
                            
                            if(sequential_disable){ seq_dis(id, all_ids); }
                                                    
                        });
                    }
                    
                    if($(this).is('input, textarea') && $(this).attr('type') != 'file' && $(this).attr('type') != 'radio' && $(this).attr('type') != 'checkbox'){
                        
                        $(this).bind('keyup change',function(){
                            
                            if($(this).val() != ''){
                                
                                if(filtertext){
                                    
                                  var t_filters = 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(sequential_disable){ 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'));
                                               
                    }
                });
                                
                $('.absubmit').click(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;
                            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;
                            alert('Invalid URL address!');
                            $(this).addClass('aberror');
                        }
                    });
                    
                    $('.abrequired').each(function(){
                        if($(this).val().length < 1){
                            pass = false;
                            alert('Field is required!');
                            $(this).addClass('aberror');
                        }
                    });
                    
                    $('.ablength').each(function(){
                        var req_range = $(this).attr('rel').split(',');
                        if($(this).val().length > req_range[1] || $(this).val().length < req_range[0]){
                            pass = false;
                            alert('Field must be between ' + req_range[0] + ' and ' + req_range[1] + ' characters in length!');
                            $(this).addClass('aberror');
                        }
                    });
                    
                    if(o.custom_regexp != ''){
                       $('.abcustom').each(function(){
                        regexp = eval($(this).attr('rel'));
                        if(!(regexp.test($(this).val()))){
                            pass = false;
                            alert('Invalid field content!');
                            $(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 = (multipart) ? "<form id=\"" + o.form_id + "\" " + attributes + " enctype=\"multipart/form-data\"></form>" : "<form id=\"" + o.form_id + "\" " + attributes + "></form>" ;
                        
                        if(!obj.has("form").length){
                           obj.wrapInner(innerWrap);
                        } 
                                               
                        if(pluggable && $.isFunction(pluggable)){
                            
                            if(serialized && !o.json){
                                
                                return pluggable($(this).parent().serialize());
                                 
                            } else if(o.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 pluggable(d);
                                
                            } else {
                                
                                return pluggable();
                                
                            }                      
                            
                        } else {
                            
                           $(this).parent().submit(); 
                           
                        }
                    }
                    }
                    if(clickonce){
                        
                       $(this).attr('disabled','disabled'); 
                        
                    }
                     
                });
                
    		});
            
            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).attr('type') == 'radio' || $(this).attr('type') == '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);

 -->
