
//Filtros
var filters = {
    obligatorio: function(el) {return ($(el).val() != '' && $(el).val() != -1 && $(el).val() != null);},
    email: function(el) {
		if ($(el).val() != '' && $(el).val() != -1){
			return /^[A-Za-z][A-Za-z0-9_.]*@[A-Za-z0-9_]+\.[A-Za-z0-9_.]+[A-za-z]$/.test($(el).val());
		}else{ 
			return true; 
		}	
	},
    telefono: function(el){
		if ($(el).val() != '' && $(el).val() != -1){
			return /^[0-9]{9}$/.test($(el).val());
		}else{ 
			return true; 
		}		
	},
    numeros: function(el){
		if ($(el).val() != '' && $(el).val() != -1){
			return /^[0-9]*$/.test($(el).val());
		}else{ 
			return true; 
		}		
	},
	moneda: function(el){
		if ($(el).val() != '' && $(el).val() != -1){
			return /^[0-9]+\.*[0-9]*$/.test($(el).val());
		}else{ 
			return true; 
		}		
	} 
};
	
// Extensiones
$.extend({
	stop: function(e){
        if (e.preventDefault) e.preventDefault();
        if (e.stopPropagation) e.stopPropagation();
    }
});

	function Validar(form,filters){
	
		//actualizar el valor de los textarea con editor ckeditor
		/*
		$('textarea').each(function(){
				var editor = window.CKEDITOR.instances[$(this).attr('id')];
				if(editor){
					var html = editor.getData();
					$(this).val(html);
				}
		});		
		*/
		if (typeof filters == 'undefined') return;
	    $(form).find("input, textarea, select").each(function(x,el){
	        if ($(el).attr("className") != 'undefined') {
			var error = false;
	        $.each(new String($(el).attr("className")).split(" "), function(x, klass){
				if ($.isFunction(filters[klass])){
					if (!filters[klass](el))  error = true;
					
				}				
	        });
				if (error){
					$(el).addClass("error_form");
					/*
					if($(el).attr('tagName') == 'TEXTAREA'){  
						//window.CKEDITOR.instances[$(el).attr('id')].setUiColor('#F299CC');
						var editor = window.CKEDITOR.instances[$(el).attr('id')];
						if(editor){
							editor.setUiColor('#FF3366');
						}
 					}
					*/
				}else{
					$(el).removeClass("error_form");
					/*
					if($(el).attr('tagName') == 'TEXTAREA'){  
						var editor = window.CKEDITOR.instances[$(el).attr('id')];
						if(editor){
							editor.setUiColor('#D3D3D3');
						}
 					}
					*/
				}
				
	        }
	    });
		if ($(form).find(".error_form").size() > 0) {
			//$.stop(window.event);
			return false;
		}
	    return true;
	
	}


// Código
$(document).ready(function(){
	$("form.validable").bind("submit", function(e){
		//actualizar el valor de los textarea con editor ckeditor
		/*
		$('textarea').each(function(){
				var editor = window.CKEDITOR.instances[$(this).attr('id')];
				if(editor){
					var html = editor.getData();
					$(this).val(html);
				}
		});		
		*/
		if (typeof filters == 'undefined') return;
	    $(this).find("input, textarea, select").each(function(x,el){
	        if ($(el).attr("className") != 'undefined') {
			var error = false;
	        $.each(new String($(el).attr("className")).split(" "), function(x, klass){
				if ($.isFunction(filters[klass])){
					if (!filters[klass](el))  error = true;
					
				}				
	        });
				if (error){
					alert($(el).attr("id"));
					$(el).addClass("error_form");
					/*
					if($(el).attr('tagName') == 'TEXTAREA'){  
						//window.CKEDITOR.instances[$(el).attr('id')].setUiColor('#F299CC');
						var editor = window.CKEDITOR.instances[$(el).attr('id')];
						if(editor){
							editor.setUiColor('#FF3366');
						}
 					}
					*/
				}else{
					
					$(el).removeClass("error_form");
					/*
					if($(el).attr('tagName') == 'TEXTAREA'){  
						var editor = window.CKEDITOR.instances[$(el).attr('id')];
						if(editor){
							editor.setUiColor('#D3D3D3');
						}
 					}
					*/
				}
				
	        }
	    });
		if ($(this).find(".error_form").size() > 0) {
			$.stop(e || window.event);
			return false;
		}
	    return true;
	});
	
	


// ------------------- PAGINACION Y ORDEN DE TABLAS
	$(".tablesorter").each(function (){
		$(this).tablesorter({widthFixed: true, widgets: ['zebra']})
	.tablesorterPager({container: $("#pager_"+$(this).attr('id'))});
	});

		
}); //FIN $(document).ready....		


