//***************************************************************************************************************************************
//CODIGO PARA CARGAR PAGINAS
//***************************************************************************************************************************************
function insertarpagina(url, id_contenedor)
{
window.scrollTo(0,0);
void(0);
var pagina_requerida = false
if (window.XMLHttpRequest) {// Si es Mozilla, Safari etc
pagina_requerida = new XMLHttpRequest()
} else if (window.ActiveXObject){ // pero si es IE
try {
pagina_requerida = new ActiveXObject("Msxml2.XMLHTTP")
} 
catch (e){ // en caso que sea una versión antigua
try{
pagina_requerida = new ActiveXObject("Microsoft.XMLHTTP")
}
catch (e){}
}
}
else
return false
pagina_requerida.onreadystatechange=function(){ // función de respuesta
cargarpagina(pagina_requerida, id_contenedor)
}
pagina_requerida.open('GET', url, true) // asignamos los métodos open y send
pagina_requerida.send(null)
}
// todo es correcto y ha llegado el momento de poner la información requerida
// en su sitio en la pagina xhtml
function cargarpagina(pagina_requerida, id_contenedor){
if (pagina_requerida.readyState == 4 && (pagina_requerida.status==200 || window.location.href.indexOf("http")==-1))
document.getElementById(id_contenedor).innerHTML=pagina_requerida.responseText
document.getElementById(id_contenedor).centerDiv
}
//***************************************************************************************************************************************
//CODIGO PARA MANEJO DE FORMULARIOS CON AJAX
//***************************************************************************************************************************************
var nav4 = window.Event ? true : false;
function acceptNum(evt)
{	
	var key = nav4 ? evt.which : evt.keyCode;	
	return (key <= 13 || (key >= 48 && key <= 57));
}
function acceptDec(evt)
{	
	var key = nav4 ? evt.which : evt.keyCode;	
	return ((key <= 13 || (key >= 48 && key <= 57)) || (key == 46)); 
}
function nocomilla(evt)
{	
	var key = nav4 ? evt.which : evt.keyCode;	
	if (key == 39)
	{
		return false;
	}
}
function nopunto(evt)
{	
	var key = nav4 ? evt.which : evt.keyCode;	
	if (key == 46 || key == 39)
	{
		return false;
	}
}
function esEmail(texto)
{ 
	var textoStr = texto.toString() // transformo a string todo el campo
	var tienearroba = 0
	var tienepunto = 0
	if (texto == '')  //controlo si esta en blanco
	{
		return true 
	}
	for(var i = 0;i < texto.length;i++)  // recorro letra por letra
	{
		var oneChar = textoStr.charAt(i) 
		if (oneChar == "@")  // busco una arroba en cada letra
		{
			tienearroba = 1
		}
		if (oneChar == ".")  // busco una arroba en cada letra
		{
			tienepunto = 1
		}
	}
	if (tienearroba == 1 && tienepunto == 1)
	{
		return true 
	}
	else
	{ 
		alert("El Correo Electrónico no es valido") 
		return false 
	} 
}
function validarcampo(texto,nombre)
{
	var textoStr = texto.toString()
	var titulo = nombre.toString()				
	var tienecomilla = 0
	if (texto == '' || texto==' ')
	{
		alert("Es necesario que escriba un dato en " + titulo)
		return false
	}
	for(var i = 0;i < texto.length;i++)
	{
		var oneChar = textoStr.charAt(i) 
		if (oneChar == "'")
		{
			tienecomilla = 1
		}
	}
	if (tienecomilla == 1)
	{
		alert("No se permite el caracter ' en " + titulo)
		return false
	}
	else
	{ 
		return true
	}
}
function validarcamponumerico(texto,nombre)
{
	var textoStr = texto.toString()
	var titulo = nombre.toString()				
	var tienecomilla = 0
	if (texto == '' || texto==' ')
	{
		return false
	}
	for(var i = 0;i < texto.length;i++)
	{
		var oneChar = textoStr.charAt(i) 
		if (oneChar == "'")
		{
			tienecomilla = 1
		}
	}
	if (tienecomilla == 1)
	{
		alert("No se permite el caracter ' en " + titulo)
		return false
	}
	else
	{ 
		return true
	}
}
function validacion(form)
{
	if (!validarcampo(form.nombre.value,"nombre"))
	{
		document.form.nombre.focus()
		return false;
	}
	if (!validarcampo(form.telefono.value,"Telefono"))
	{
		document.form.telefono.focus()
		return false;
	}
	if (!esEmail(form.correo.value))
	{
		document.form.correo.focus()
		return false;
	}
	if (!validarcampo(form.mensaje.value,"Mensaje"))
	{
		document.form.mensaje.focus()
		return false;
	}	
}
function nuevoAjax()
{
	var xmlhttp=false;
	try
	{
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch (e)
	{
		try
		{
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (E)
		{
			xmlhttp = false;
		}
	}
	if (!xmlhttp && typeof XMLHttpRequest!='undefined')
	{
		xmlhttp = new XMLHttpRequest();
	}
return xmlhttp;
}

function enviar()
{
	if(validacion(document.form)!=false)
	{
		element =  document.getElementById('indicador');
		var_nombre = document.getElementById('nombre').value;
		var_telefono = document.getElementById('telefono').value;
		var_celular = document.getElementById('celular').value;
		var_direccion = document.getElementById('direccion').value;
		var_correo = document.getElementById('correo').value;
		var_mensaje = document.getElementById('mensaje').value;
		ajax=new nuevoAjax();
	    element.innerHTML = '<img src="imagenespag/cargando.gif"> Enviando mensaje...';
		ajax.open("POST", "correo.php",true);
		ajax.onreadystatechange=function()
		{
			if (ajax.readyState==4)
			{
				datos=ajax.responseXML;
				var var0=datos.getElementsByTagName("dato0").item(0).firstChild.data;
				if(var0=='1')
				{
				    element.innerHTML = 'Tu mensaje ha sido enviado exitosamente.';
					limpiar();
				}
				else
				{
				   element.innerHTML = 'Tu mensaje no se pudo enviar debido a un problema interno.';
				}
			}
		}
		ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		ajax.send(
		"nombre="+var_nombre+
		"&telefono="+var_telefono+
		"&celular="+var_celular+
		"&direccion="+var_direccion+
		"&correo="+var_correo+
		"&mensaje="+var_mensaje
		);
	}
}
function limpiar()
{
	document.form.nombre.value="";
	document.form.telefono.value="";
	document.form.celular.value="";
	document.form.direccion.value="";
	document.form.correo.value="";
	document.form.mensaje.value="";
}