// JavaScript Document
function num(tam){
	if (event.keyCode < 45 || event.keyCode > 57) 
	{
		event.returnValue = false;
		alert('Por favor, introduzca valores numéricos.');
	}

}

function pup (URL){
    window.open(URL,"p","width=650,height=540,scrollbars=YES") 
}

function validarEmail(c){
    if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(c)){				
	    return true;
	}else{
		return (false);
	}
}

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;
}

function TelfOK(tlf){
    var tel=tlf;
	if (tel.length==9){
	    if ( (tel.substring(0,1)=="9") || (tel.substring(0,1)=="8") || (tel.substring(0,1)=="6")){
		    return true;
		}
	}			
    return false;				
}

function formularioCobertura(){	
    if(TelfOK(document.getElementById('mailing_telefono').value) && validarEmail(document.getElementById('mailing_email').value)) {
						
        if(document.getElementById('mailing_direccion').value!='' && document.getElementById('mailing_nombre').value!=''){
		    var rutaSrv = "srv.php?a=1";
			var ruta = rutaSrv+"&obj=InsertaTelefono&telefono="+document.getElementById('mailing_telefono').value+"&email="+document.getElementById('mailing_email').value+"&nombre="+document.getElementById('mailing_nombre').value+"&direccion="+document.getElementById('mailing_direccion').value;
			var ajax=objetoAjax();
			ajax.open("GET", ruta, true);
			ajax.send(null);
			
			ajax.onreadystatechange=function(){
			if(ajax.readyState==4){
			    if (ajax.status == 200){
				    var respuesta = ajax.responseText;
					    if(respuesta==2){
						    alert("Error inesperado al almacenar sus datos, intentelo de nuevo. Disculpe las molestias.");
						}else{
						    if(respuesta==0){
							    alert("Ya existe este teléfono en nuestro sistema");
							}else{
								document.getElementById('mailing_telefono').value='';
								document.getElementById('mailing_email').value='';
								document.getElementById('mailing_direccion').value='';
								document.getElementById('mailing_nombre').value='';
								alert("Sus datos han sido enviados con éxito, pronto le llegará un email. Gracias.");
							}
						}
					}else {
					    alert("No se puede conectar con el servicio.");
					}
				}
			}
		}else{
			alert("Debe introducir sus datos correctamente, revise el su dirección, nombre, teléfono o email.");
		}
	}else{				
	    alert("Debe introducir sus datos correctamente, revise el su dirección, nombre, teléfono o email.");
	}
}
