function getXMLHttp()
{
  var xmlHttp
  try
  {
    //Firefox, Opera 8.0+, Safari
    xmlHttp = new XMLHttpRequest();
  }
  catch(e)
  {
    //Internet Explorer
    try
    {
      xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch(e)
    {
      try
      {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch(e)
      {
        alert("Your browser does not support AJAX!")
        return false;
      }
    }
  }
  return xmlHttp;
}

function MakeRequest()
{
  var xmlHttp = getXMLHttp();
  xmlHttp.onreadystatechange = function()
  {
    if(xmlHttp.readyState == 4)
    {
	  Txt =xmlHttp.responseText.split("*#");
		var Marks=new Array();
		for(i=0;i<Txt.length;i++){
			if((Txt[i]!="")){
				Marks[i]=new Array();
				marca=Txt[i].split("#");
				if((marca[0]!="")&&(marca[1]!="")){
					Marks[i][0]=marca[0];
					Marks[i][1]=marca[1];
					Marks[i][2]=marca[2];
				}
			}
		}
	  redraw(Marks);
    }
  }
  xmlHttp.open("GET", "consultasajax/Take_puntos", true);
  xmlHttp.send(null);
}

function TakeProvincia(ccaa,preselect)
{
		document.getElementById('provincia').options.length=0;
		document.getElementById('localidad').options.length=0;
		document.getElementById('provincia').options[0] = new Option("Provincia","");
		document.getElementById('localidad').options[0] = new Option("Localidad","");
	if(ccaa!=""){
		var xmlHttp = getXMLHttp();
		xmlHttp.onreadystatechange = function()
		{
		 if(xmlHttp.readyState == 4)
		 {
		  opciones=xmlHttp.responseText.split("#");
		  j=0;
			for (i=0;(i<opciones.length/2);i++){
				document.getElementById('provincia').options[i]= new Option(opciones[j+1],opciones[j]);
				j=j+2;
			}
			if(preselect!=""){
			document.getElementById('provincia').value=preselect;
			}
			resetlist();
		}
	  }
	  xmlHttp.open("GET", "consultasajax/Take_provincia?ccaa="+ccaa, true);
	  xmlHttp.send(null);
	 }
}


function TakePoblacion(provincia,preselect)
{

	document.getElementById('localidad').options.length=0;
	document.getElementById('localidad').options[0] = new Option("Localidad","");
	if(provincia!=""){
	  var xmlHttp = getXMLHttp();
	  xmlHttp.onreadystatechange = function()
	  {
		if(xmlHttp.readyState == 4)
		{
		  opciones=xmlHttp.responseText.split("#");
		  j=0;
			for (i=0;(i<opciones.length/2);i++){
				document.getElementById('localidad').options[i]= new Option(opciones[j+1],opciones[j]);
				j=j+2;
			}
			resetlist();
			if(preselect!=""){
				document.getElementById('localidad').value=preselect;
			}
			resetlist();
		}
	  }
	  xmlHttp.open("GET", "consultasajax/Take_poblacion?provincia="+provincia, true);
	  xmlHttp.send(null);
  }
}

function HandleResponse(response)
{
  document.getElementById('ResponseDiv').innerHTML = response;
}


function buscarpunto()
{
document.getElementById('MensajeError').style.display="none";
cp=document.getElementById('cp').value;
comunidad=document.getElementById('comunidad').value;
provincia=document.getElementById('provincia').value;
localidad=document.getElementById('localidad').value;
document.getElementById('cubre_tabla').style.display='block';

var xmlHttp = getXMLHttp();
  xmlHttp.onreadystatechange = function()
  {
  if(xmlHttp.readyState == 4)
    {
	if(xmlHttp.responseText!=""){
	  Txt =xmlHttp.responseText.split("*#");
		var Marks=new Array();
		var Tabla=new Array();
		document.getElementById('map_canvas').innerHTML="<div style='text-align:center;font-size:18px;background-color:none;height:300px;padding-top:250px;'>Cargando...</div>";
		for(i=0;i<Txt.length;i++){
			if((Txt[i]!="")){
				Marks[i]=new Array();
				Tabla[i]=new Array();
				marca=Txt[i].split("#");
				if((marca[0]!="")&&(marca[1]!="")){
					Marks[i][0]=marca[0];
					Marks[i][1]=marca[1];
					Marks[i][2]=marca[2];
										Tabla[i][0]=marca[0];
					Tabla[i][1]=marca[1];
					Tabla[i][2]=marca[2];
					Tabla[i][3]=marca[3];
					Tabla[i][4]=marca[4];
					Tabla[i][5]=marca[5];
				}
			}
		}
	  redraw_zone(Marks);
	  fnClickAddRow(Tabla);
	  }
	  else{
		document.getElementById('MensajeError').innerHTML="<p>La búsqueda por código postal no encuentra ningún punto limpio, intenta la búsqueda a través de la Comunidad Autónoma, la Provincia y la Localidad.</p>";
		document.getElementById('MensajeError').style.display="block";
		document.getElementById('cp').value="Código postal";
	  }
    }
  }
  if(cp.length>0 && cp!="Código postal") {
	if(cp.length==5){
		xmlHttp.open("GET", "consultasajax/Take_zona_by_cp?cp="+cp, true);
		xmlHttp.send(null);
	}
   else{
		 document.getElementById('MensajeError').innerHTML="<p>Por favor, asegurese de haber introducido 5 dígitos</p>";
		 document.getElementById('MensajeError').style.display="block";
   }
  }
  else{
	if(comunidad!=""){
		//document.getElementById('map_canvas').innerHTML="<div style='text-align:center;font-size:18px;background-color:none;height:300px;padding-top:250px;'>Cargando...</div>";
		xmlHttp.open("GET", "consultasajax/Take_zona?l="+localidad+"&p="+provincia+"&c="+comunidad, true);
		xmlHttp.send(null);
	}
  }
}

function pl_consulta(id,point){
  var xmlHttp = getXMLHttp();
   xmlHttp.onreadystatechange = function()
  {
	if(xmlHttp.readyState == 4)
		{
			infox=xmlHttp.responseText;
			map.openInfoWindowHtml(point, infox);
		}
  }
  xmlHttp.open("GET", "consultasajax/Pl_consulta?id="+id, true);
  xmlHttp.send(null);
}

function mostrar_punto(id){
  var xmlHttp = getXMLHttp();
  xmlHttp.onreadystatechange = function()
  {
    if(xmlHttp.readyState == 4)
    {
	  Txt =xmlHttp.responseText.split("*#");
		var Marks=new Array();
		for(i=0;i<Txt.length;i++){
			if((Txt[i]!="")){
				Marks[i]=new Array();
				marca=Txt[i].split("#");
				if((marca[0]!="")&&(marca[1]!="")){
					Marks[i][0]=marca[0];
					Marks[i][1]=marca[1];
					Marks[i][2]=marca[2];
				}
			}
		}
	   redraw_zone(Marks);
    }
  }
  xmlHttp.open("GET", "consultasajax/Take_punto_mas_visto?id="+id, true);
  xmlHttp.send(null);
}

function limitar_combos(){
cp=document.getElementById('cp').value;
	if(cp.length>0){
	document.getElementById('comunidad').options.length=1;
	document.getElementById('provincia').options.length=1;
	document.getElementById('localidad').options.length=1;
	resetlist();
	}
	else if(cp.length==0){
		document.getElementById('comunidad').options.length=0;
		var xmlHttp = getXMLHttp();
		xmlHttp.onreadystatechange = function()
		{
		 if(xmlHttp.readyState == 4)
		 {
		  opciones=xmlHttp.responseText.split("#");
		  j=0;
			for (i=0;(i<opciones.length/2);i++){
				document.getElementById('comunidad').options[i]= new Option(opciones[j+1],opciones[j]);
				j=j+2;
			}
			resetlist();
		}
	  }
	  xmlHttp.open("GET", "consultasajax/Take_ccaa_ajax", true);
	  xmlHttp.send(null);

	}

}

function enviar_form(){
  var xmlHttp = getXMLHttp();
   xmlHttp.onreadystatechange = function()
  {
  contenido_ok=document.getElementById('enviar_ok');
  contenido_ko=document.getElementById('enviar_ko');
	if(xmlHttp.readyState == 4)
		{
		alert(xmlHttp.responseText);
			if(xmlHttp.responseText=="ok"){
				Shadowbox.open(contenido_ok);
			}
			else{
				Shadowbox.open(contenido_ko);
			}
		}
  }
var params = ["opcion_correo=" + document.getElementById('opcion_correo').value,
	"nombre=" + document.getElementById('nombre').value,
	"email=" + document.getElementById('email').value,
	"sugerencia=" + document.getElementById('sugerencia').value,
	"geoposicion=" + document.getElementById("geoposicion").value,
	"file=" + document.getElementById('file').value
	].join('&');//tidier
	xmlHttp.open("POST", "prueba.php", true);
	xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	//xmlHttp.onreadystatechange = handleRequestStateChange;
	xmlHttp.send(params); 
}

