//----COMPROBAR FORMS
//Filtros
var filters = {
    required: function(el) {return ($(el).val() != '' && $(el).val() != -1);},
        email: function(el) {return /^(?:[\w\!\#\$\%\&\'\*\+\-\/\=\?\^\`\{\|\}\~]+\.)*[\w\!\#\$\%\&\'\*\+\-\/\=\?\^\`\{\|\}\~]+@(?:(?:(?:[a-zA-Z0-9](?:[a-zA-Z0-9\-](?!\.)){0,61}[a-zA-Z0-9]?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9\-](?!$)){0,61}[a-zA-Z0-9]?)|(?:\[(?:(?:[01]?\d{1,2}|2[0-4]\d|25[0-5])\.){3}(?:[01]?\d{1,2}|2[0-4]\d|25[0-5])\]))$/.test($(el).val());},
    telefono: function(el){return /^[0-9]*$/.test($(el).val());},
	number: function(el) {valor=$(el).val(); return !isNaN(valor);},
	compPass: function(el) {return $(el).val()==$('#REPETIR_PASS').val();},
	isChecked: function(el) {return el.checked;},
	isImage: function(el) {valor=$(el).val(); return (valor != '' && (valor.indexOf('jpg')>0 || valor.indexOf('jpeg')>0 || valor.indexOf('gif')>0));}
};	
// Extensiones
$.extend({
	stop: function(e){
        if (e.preventDefault) e.preventDefault();
        if (e.stopPropagation) e.stopPropagation();
    }
});
// Código
$(document).ready(function(){
	$("form").bind("submit", function(e){
		//Limpiamos las clases de error anteriores si las hay
		$(this).find(".error").removeClass("error");
		$(this).find(".errorSpan").removeClass("errorSpan");
		
		if (typeof filters == 'undefined') return;
	    $(this).find("input, textarea, select").each(function(x,el){
	        if ($(el).attr("className") != 'undefined') {
	        $.each(new String($(el).attr("className")).split(" "), function(x, klass){
	            if ($.isFunction(filters[klass]))
	                if (!filters[klass](el))  {
						//Definimos las clases de error
						$(el).addClass("error");
						$(el.parentNode).addClass("errorSpan");
					}
			});
	        }
	    });
		if ($(this).find(".error").size() > 0) {
			$.stop(e || window.event);
			$(this).find(".error:first").focus();
			return false;
		}
	    return true;
	});
});
//---- FIN COMPROBAR FORMS



//------------------------------------------------------
//Cambios de enlaces para blank, back, etc...
$(document).ready(function () {
    //Ponemos funciones a los buscadores
	$("#nuevaBusqueda").click(function() {
		$(":input").attr('value','');
		$(":text:first").focus();
	});
	
	//Convertimos los enlaces que tengan clase external a blank
	$("a.external").attr('target', '_blank');
	
	//Ponemos history back en los enlaces que corresponda
	$("a.volverAtras").click(function() {
		window.history.back();
		return false;
	});	

});
