// JavaScript Document

//OBJETO AJAX - NECESARIO
function objetoAjax()
	{
	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;
  	}


//ENVIADOR DE CORREO CONSTRUIDO PARA VIVA
function sendMailer()
	{
	//VARIABLES
	var nombre=document.getElementById('NOMBRE').value;
	var correo=document.getElementById('CORREO').value;
	var fono1=document.getElementById('FONO1').value;
	var fono2=document.getElementById('FONO2').value;
	var ciudad=document.getElementById('CIUDAD');
	var ciudadSel = ciudad.selectedIndex;
	var ciudadValue = ciudad[ciudadSel].value;
	var comentario=document.getElementById('COMENTARIO').value;
	var planvnet = document.getElementById('PLANVNET').value;
	// OBJETO AJAX
	divx = document.getElementById('DIVFRMMAIL');	
	divx.innerHTML='<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td height="5">Enviando su solicitud</td></tr><tr><td align="center" valign="middle"><img border="0" src="phpmailer/bar.gif" /></td></tr></table>';

	//instanciamos el objetoAjax  
	ajax=objetoAjax();
	//uso del medotod POST
	//archivo que realizará la operacion
	ajax.open("POST", "phpmailer/promosender.php",true);
	
	ajax.onreadystatechange=function() {
										if (ajax.readyState==4) 
											{
											//mostrar resultados en esta capa
											divx.innerHTML = ajax.responseText;
											}
										}  
	ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	//enviando los valores	
	ajax.send("NOMBRE=" + nombre + "&CORREO=" + correo + "&FONO1=" + fono1 + "&FONO2=" + fono2 + "&CIUDAD=" + ciudadValue + "&COMENTARIO=" + comentario + "&PLANVNET=" + planvnet);
	}

// VERIFICAR SI UN CORREO ESTA CORRECTAMENTE ESCRITO
function isMail(texto)
	{ 
	var mailres = true;             
	var cadena = "abcdefghijklmnñopqrstuvwxyzABCDEFGHIJKLMNÑOPQRSTUVWXYZ1234567890@._-"; 
	 
	var arroba = texto.indexOf("@",0); 
	if ((texto.lastIndexOf("@")) != arroba) arroba = -1; 
	 
	var punto = texto.lastIndexOf("."); 
				 
	for (var contador = 0 ; contador < texto.length ; contador++)
		{ 
		if (cadena.indexOf(texto.substr(contador, 1),0) == -1)
			{ 
			mailres = false; 
			break; 
		 	} 
		} 	
	if ((arroba > 1) && (arroba + 1 < punto) && (punto + 2 < (texto.length)) && (mailres == true) && (texto.indexOf("..",0) == -1)) 
		 mailres = true; 
	else 
		 mailres = false; 
	return mailres; 
	} 

// VERIFICA SI LOSDATOS DE PROMO ESTAN COMPLETOS
function validarPromo()
	{
	/* NOMBRE CORREO FONO1 FONO2 CIUDAD COMENTARIO */
	var sw=true;
	document.getElementById("DIVNOMBRE").innerHTML="";
	document.getElementById("DIVNOMBRE").style.display="none";
	document.getElementById("DIVCORREO").innerHTML="";
	document.getElementById("DIVCORREO").style.display="none";
	document.getElementById("DIVFONO1").innerHTML="";
	document.getElementById("DIVFONO1").style.display="none";
	document.getElementById("DIVCIUDAD").innerHTML="";
	
	if (document.getElementById("NOMBRE").value=="")
		{
		document.getElementById("DIVNOMBRE").innerHTML="Requerido";
		document.getElementById("DIVNOMBRE").style.display="";
		sw=false;
		}
	if (document.getElementById("CORREO").value=="")
		{
		document.getElementById("DIVCORREO").innerHTML="Requerido";
		document.getElementById("DIVCORREO").style.display="";
		sw=false;
		}
	else
		{
		if (!isMail(document.getElementById("CORREO").value))
			{
			document.getElementById("DIVCORREO").innerHTML="Incorrecto";
			document.getElementById("DIVCORREO").style.display="";
			sw=false;
			}
		}
	if (document.getElementById("FONO1").value=="")
		{
		document.getElementById("DIVFONO1").innerHTML="Requerido";
		document.getElementById("DIVFONO1").style.display="";
		sw=false;
		}
	var ciudad=document.getElementById("CIUDAD");
	var ciudadSel = ciudad.selectedIndex;
	var ciudadValue = ciudad[ciudadSel].value;
	if (ciudadValue=="Seleccione")
		{
		document.getElementById("DIVCIUDAD").innerHTML="Requerido";
		sw=false;
		}
	if (sw)
		{
		sendMailer();	
		}	
	}