function KsFormBase(form) {
    var $form = $(form);
    //$form.addClass('AutoForm');
    
    form.ajaxControl = null;
    form.getAjaxControl = function() { //EL REPARCHE
        if (form.ajaxControl == null) {
            var pathajax = form.id;
            form.ajaxControl = ksAjaxControl(pathajax);
        }
        return form.ajaxControl
    }

    form.acceptChanges = function() {
        $form.find('.FieldControl').each(function(){
            this.acceptChanges();
        });
    }

    form.getFormValues = function(ignoreNoVisibles, ignoreUnchangeds, ignoreEmpties) {
        function AddValueBase(result, control, value) {
            result.values[control.id] = value;
            var validationErrors = control.Validate();
            if (validationErrors.length > 0)
                result.errors[control.id] = validationErrors;
        }
        function AddValue(result, control) {
            control.refreshValue();
            //Con los arrays no funciona correctamente el isChanged
            if (!ignoreUnchangeds || control.isChanged()) {
                var aux = control.getValue();
                //AddValueBase(result, control, aux);
                //OJO, si ignora los vacios, tampoco los valida
                if (ignoreEmpties) {
                    if (aux != null) {
                        if (typeof aux == "string") {
                            aux = $.trim(aux);
                            if (aux != '')
                                AddValueBase(result, control, aux);
                        } else {
                            AddValueBase(result, control, aux);
                        }
                    }
                } else {
                    AddValueBase(result, control, aux);
                }
            }
        }

        var result = new Object();
        result.values = new Object();
        result.errors = new Object();
        var selectorAux = ignoreNoVisibles ? '.FieldControl:visible' : '.FieldControl';
        $form.find(selectorAux).each(function(){    
            AddValue(result, this);
        });
        return result;
    }
    

    form.extendedCheckErrors = function(errorMessage, formvalues) { return true; }

    form.checkErrors = function(formvalues) {
        var errorMessage = $(form).find('#errorMessage');
        if (errorMessage.length == 0) {
            $(form).prepend('<div id="errorMessage" style="display:none;"/>');
            errorMessage = $(form).find('#errorMessage');
        }
        errorMessage.fadeOut('slow');
        if (!KsFormBase.isEmpty(formvalues.errors)) {
           errorMessage.html('<div class="ui-state-error ui-corner-all" style="padding: 0pt 0.7em;"><p><span class="ui-icon ui-icon-alert" style="float: left; margin-right: 0.3em;"/><strong>' + KsTranslations.Atencion + ': </strong>' + KsTranslations.SeDetectaronErrores + '.</p></div>');
           errorMessage.fadeIn('slow');
           return false;
        }
        if (form.extendedCheckErrors(errorMessage, formvalues)) {
            errorMessage.html('');
            return true;
        } else {
            return false;
        }
    }
}
KsFormBase.isEmpty = function (ob) {
   for (var i in ob) {
       return false;
   }
   return true;
}

KsFormBase.Count = function (ob, expr) {
    var i = 0;
    for (var fieldName in ob) {
        if ((expr == undefined) || expr(fieldName, ob[fieldName]))
            i++;
    }
    return i;
}

KsFormBase.SerializeToParam = function(a,includeStrEmpties) {
        var s = [ ];

        function add( key, value, includeStrEmpties ){
            if (includeStrEmpties || value != "")
                s[ s.length ] = encodeURIComponent(key) + '=' + encodeURIComponent(value);
        };

        // If an array was passed in, assume that it is an array
        // of form elements
        if ( jQuery.isArray(a) || a.jquery ) {
                // Serialize the form elements
                jQuery.each( a, function(){
                        add( this.name, this.value, includeStrEmpties );
                });
        }
        // Otherwise, assume that it's an object of key/value pairs
        else
                // Serialize the key/values
                for ( var j in a )
                        // If the value is an array then the key names need to be repeated
                        if ( jQuery.isArray(a[j]) )
                                add( j, JSON.stringify(a[j]), includeStrEmpties);
                        else
                                add( j, jQuery.isFunction(a[j]) ? a[j]() : a[j] , includeStrEmpties);

        // Return the resulting serialization
        return s.join("&").replace(/%20/g, "+");
}

KsFormBase.Gup = function( name ) {
      name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
      var regexS = "[\\?&]"+name+"=([^&#]*)";
      var regex = new RegExp( regexS );
      var results = regex.exec( window.location.href );
      if( results == null )
        return null;
      else
        return decodeURIComponent(results[1]);
}

