window.addEvent('domready', function() 
{
	$("contacto").onsubmit=function()
					{
						return validacion(this);
					};
					
	$("nombre").focus();
	$("facturas").style.display="none";
	
});


function verDatosFac()
{
		
	ver=($("facturas").style.display=="none")? "block":"none";
	$("facturas").style.display=ver;
}

function validaCorreo(valor)
{
	var reg=/(^[a-zA-Z0-9._-]{1,30})@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3}$/;
	if(reg.test(valor)) return true;
	else return false;
}

function validaCodigo(valor)
{
	var reg=/(^[A-Z0-9]{4}$)/;
	if(reg.test(valor)) return true;
	else return false;
}

var registroCliente=false;

function validacion(formulario)
{
	//alert("Iniciando el proceso de \"Validación...\" ");
	var validacion=true;
	var error="";
	
	
	for(var i=0; i<formulario.elements.length; i++) 
	{
		var tipo = formulario.elements[i].type;
		var id = formulario.elements[i].id;
		var valor = formulario.elements[i].value;
		var nombre = formulario.elements[i].name;
		
		
		if(tipo=="text" || tipo=="textarea")
		{
			if(registroCliente==false)	
			{
				if(nombre!="tel2" && nombre!="website" && nombre!="recomenda" && nombre!="otro" && nombre!="comentarios" && nombre!="razonsocial" && nombre!="direccionc" && nombre!="telc" && nombre!="ciudadc" && nombre!="paisc" && nombre!="cpcliente" && nombre!="rfc")
				{
					if(isBlank(valor))
					{
						error="Vacio: "+nombre;
						marca(id,"req");
						validacion=false;
						alert(error);
						break;
						
					}else{
							marca(id,"normal");
						}
				}
				
			}else{
					if(nombre!="tel2" && nombre!="website" && nombre!="recomenda" && nombre!="otro" && nombre!="comentarios")
					{
						if(isBlank(valor))
						{
							error="Vacio: "+nombre;
							marca(id,"req");
							validacion=false;
							alert(error);
							break;
							
						}else{
								marca(id,"normal");
							}
					}
				}
			
			
			
				
			if((nombre=="email" && !validaCorreo(valor)))
			{
				error="Correo: "+valor+" No válido";
				marca(id,'req');
				validacion=false;
				alert(error);
				break;
			}
			
		}
		
		if(id=="cliente")
		{
			registroCliente=formulario.elements[i].checked;
		}
		
	}
	
	return validacion;
}	

function isBlank(val)
{
	if(val==null){
		return true;
	}
	for(var i=0;i<val.length;i++) {
		if ((val.charAt(i)!=' ')&&(val.charAt(i)!="\t")&&(val.charAt(i)!="\n")&&(val.charAt(i)!="\r")){
			return false;
		}
	}
	return true;

}

function marca(elem,css)
{
	$(elem).focus();
	$(elem).select();
	$(elem).className=css;
}
