/*****************************
 * Creado por Gabriel Memmel *
 *    gabimem[]gmail.com     *
 *       Junio - 2010        *
 *****************************/
 
 
/*
.obli {
	border-color:red;
}
*/
 
(function($){
	$.extend($.fn, {
		validar: function (){
			var contenedor = this;
			var inco  = false;
			var nopop = false;
			var sel   = Object();
			
			// Asigna el evento onkeyup para sacar el borde rojo al rellenar el campo
			$(contenedor).find('input.requerido, textarea.requerido, select.requerido').each(function(){
				$(this).keyup(function(){
					$(this).removeClass('obli');
				});
				$(this).change(function(){
					$(this).removeClass('obli');
				});
			});
			
			// Recorre el formulario
			$(contenedor).find('.requerido').each(function(){
				var $this_arr = new Array(), thisInd;
				
				$(this).removeClass('obli');
				if(!/input|textarea|select/i.test(this.tagName)){
					$this_arr[++thisInd] = this;
					var $this = $this_arr[thisInd];
					
					$(this).find('input, textarea, select').not('[disabled],.no-req').each(function(){
						// Agrega las funciones para sacar la clase obli
						$(this).keyup(function(){
							$($this).removeClass('obli');
						});
						$(this).change(function(){
							$($this).removeClass('obli');
						});
						if(this.type == 'radio' || this.tagName == 'select'){
							$(this).click(function(){
								$($this).removeClass('obli');
							});
						}
						
						// Verifica los campos
						if(!verificar(this, contenedor)){
							$($this).addClass('obli');
							inco = true;
						}
					});
				}
				else{
					if(!verificar(this)){
						$(this).addClass('obli').focus();
						inco = true;
					}
				}
			});
			
			function verificar(obj, padre){
				if($(obj).attr('type') == 'radio' || $(obj).attr('type') == 'checkbox'){
					if($(padre).find('input[name="'+$(obj).attr('name')+'"]:checked').length > 0){
						return true;
					}
					else{
						return false;
					}
				}
				else if($(obj).attr('value') == ''){
					return false;
				}
				else{
					if($(obj).hasClass('req-email')){
						if(!validarEmail($(obj).attr('value'))){
							if(!nopop) alert('El email ingresado no es válido.');
							nopop = true;
							return false;
						}
					}
					if($(obj).hasClass('req-numero')){
						if(!validarNumero($(obj).attr('value'))){
							if(!nopop) alert('El valor debe ser numérico.');
							nopop = true;
							return false;
						}
					}
					if($(obj).hasClass('req-fecha')){
						if(!validarFecha($(obj).attr('value'))){
							if(!nopop) alert("La fecha ingresada no es válida.\nEl formato debe ser dd/mm/YYYY");
							nopop = true;
							return false;
						}
					}
					if($(obj).hasClass('req-hora')){
						if(!validarHora($(obj).attr('value'))){
							if(!nopop) alert("La hora ingresada no es válida.\nEl formato debe ser HH:mm");
							nopop = true;
							return false;
						}
					}
				}
				return true;
			};
			
			function validarEmail(valor) {
				if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(valor)) {
					return true;
				} else {
					return false;
				}
			}
			function validarNumero(valor) {
				if (/^[0-9\.\,]+$/.test(valor)) {
					return true;
				} else {
					return false;
				}
			}
			function validarFecha(valor) {
				if (/^[0-9]{2,4}[\/-][0-9]{1,2}[\/-][0-9]{1,2}$/.test(valor)) {
					return true;
				} else if (/^[0-9]{1,2}[\/-][0-9]{1,2}[\/-][0-9]{2,4}$/.test(valor)) {
					return true;
				} else {
					return false;
				}
			}
			function validarHora(valor) {
				if (/^[0-9]{1,2}:[0-9]{2}(:[0-9]{2})?(\s?(am|pm|hs))?$/i.test(valor)) {
					return true;
				} else {
					return false;
				}
			}
			
			if(inco){
				if(!nopop){
					$(contenedor).find(".requerido[value='']:first").focus();
					alert('Por favor, complete los campos obligatorios, marcados en rojo');
				}
				return false;
			}
			else{
				return true;
			}
		}
	})
})(jQuery);
