// JavaScript Document
ValidarDatos={	
	enFoco:false,
	hayError:false,
	validarForm:function(form){
		var checkProductos=false;
		var finBucle=form.elements.length-3;
		if(this.enFoco){
			this.enFoco=false;
		}
		if(this.hayError){
			this.hayError=false;
		}
		for(i=0;i<finBucle;i++){
			if(form.elements[i].id=='nombre' || form.elements[i].id=='apellido' || form.elements[i].id=='email'){
				this.chequearInput(form.elements[i]);				
			}
		}
		/*for(j=6;j<finBucle;j++){
			if(form.elements[j].checked){
				checkProductos=true;
				break;
			}
		}
		if(!checkProductos){
			this.mostrarError(form.elements[6],"Debe selecionar qué productos le interesan");
			if(!this.hayError){
				this.hayError=true;
			}
		}else{
			this.sacarSpan(form.elements[6]);			
		}*/
		if(this.hayError){	
			return false;
		}else{
			return true;
		}
	},
	validarBlanco:function(valor){
		var resultado=true;
		var patron=/\s+/;
		var cadena_con_blanco=valor.replace(patron,'');
		if(cadena_con_blanco.length==0){
			resultado=false;
		}
		return resultado;
	},
	sacarSpan:function(elemento){
		var span=document.getElementById('error_'+elemento.id);
		if(span){
			span.parentNode.removeChild(span);		
		}
	},
	mostrarError:function(elemento,error){
		this.sacarSpan(elemento);
		span=document.createElement('span');
		var br=null;
		if(elemento.id=='productos'){
			br=document.createElement('br');
		}
		span.appendChild(document.createTextNode(error));
		if(br){
			span.appendChild(br);
		}
		span.setAttribute('id','error_'+elemento.id);
		span.className='error';
		if(elemento.id!='productos'){
			elemento.parentNode.appendChild(span);
		}else{
			elemento.parentNode.insertBefore(span,elemento);
		}
		if(!this.enFoco && elemento.type!='checkbox'){
			elemento.focus();
			this.enFoco=true;
		}
	},
	chequearInput:function(elemento){
		var patron_mail=/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/;
		var patron_tel=/^[0-9 ()-]{8,}$/;
		if(!elemento.value || !this.validarBlanco(elemento.value)){
			if(elemento.id=='tel'){
				this.mostrarError(elemento,'Debe tipear su teléfono');
			}else{
				this.mostrarError(elemento,'Debe tipear su '+elemento.id);
			}
			if(!this.hayError){
				this.hayError=true;
			}
			return false;
		}else{
			if(elemento.id=='email'){
				if(patron_mail.test(elemento.value)){
					this.sacarSpan(elemento);
					return true;					
				}else{
					this.mostrarError(elemento,'Email incorrecto');
					if(!this.hayError){
						this.hayError=true;
					}
					return false;
				}
			}else if(elemento.id=='tel'){
				if(patron_tel.test(elemento.value)){
					this.sacarSpan(elemento);
					return true;					
				}else{
					this.mostrarError(elemento,'Teléfono incorrecto.');
					if(!this.hayError){
						this.hayError=true;
					}
					return false;
				}
			}else{
				this.sacarSpan(elemento);
				return true;				
			}		
		}
	},
	agregarOnFocus:function(form){
		form.elements[0].focus();
		for(i=0;i<form.elements.length;i++){
			if(form.elements[i].id=='nombre' || form.elements[i].id=='apellido' || form.elements[i].id=='email'){
				form.elements[i].onblur=function(){
					ValidarDatos.chequearCampo(this);
				}
			}
		}
	},
	chequearCampo:function(elemento){
		if(!this.chequearInput(elemento)){
			this.mostrarError(elemento,'Debe tipear '+elemento.id);
		}else{
			this.sacarSpan(elemento);		
		}
	}
}
function agregarValidacion(){
	var formulario=document.getElementsByTagName('form')[0];
	if(document.getElementById && document.createElement){
		if(formulario){
			formulario.onsubmit=function(){
				return ValidarDatos.validarForm(this);
			}
		}			
	}
	ValidarDatos.agregarOnFocus(formulario);		
}
window.onload=agregarValidacion;
