//Globales
$dominio = "http://www.visitaecuador.com/";
$ruta_servidor = "http://www.visitaecuador.com/menu_vertical/servidor/";
$ruta_certificado = "http://www.visitaecuador.com/club/certificado/certificado_club.php";
//-------------------AJAX---------------------------------------
function inicializa_xhr() {
  if (window.XMLHttpRequest) {
    return new XMLHttpRequest(); 
  } else if (window.ActiveXObject) {
    return new ActiveXObject("Microsoft.XMLHTTP"); 
  } 
}

////////////////CODIGO PARA APLICACION BUSQUEDA///////////////////
//2.-FUNCION PARA MOSTRAR REGIONES------------------------------
function muestraRegiones() {
  if (peticion.readyState == 4) {
    if (peticion.status == 200) {
      var lista = document.getElementById("region");
      var regiones = eval('(' + peticion.responseText + ')');
	  lista.options[0] = new Option("- region -");
      var i=1;
      for(var codigo in regiones) {
        lista.options[i] = new Option(regiones[codigo], codigo);
        i++;
      }
    }
  }
}

//5.-FUNCION PARA CARGAR PROVINCIAS----------------------------------
function cargaProvincias() {
  var lista = document.getElementById("region");
  var region = lista.options[lista.selectedIndex].value;
  if(!isNaN(region)) {
    peticion = inicializa_xhr();
    if (peticion) {
      peticion.onreadystatechange = muestraProvincias;
      peticion.open("POST", $ruta_servidor+"carga_provinciasJSON.php?nocache=" + Math.random(), true);
      peticion.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	  peticion.send("region=" + region);
	}
  }
  document.getElementById("provincia").disabled = false;	
}
//4.-FUNCION PARA MOSTRAR PROVINCIAS--------------------------------
function muestraProvincias() {
  if (peticion.readyState == 4) {
    if (peticion.status == 200) {
      var lista = document.getElementById("provincia");
      var provincias = eval('(' + peticion.responseText + ')');
	  lista.options[0] = new Option("- region -");
	  var i=0;
      for(var codigo in provincias) {
        lista.options[i] = new Option(provincias[codigo], codigo);
        i++;
      }
    }
  }
}

//7.--FUNCION PARA CARGAR CIUDADES EN COMBO
function cargaCiudades() {
  var lista = document.getElementById("provincia");
  var provincia = lista.options[lista.selectedIndex].value;
  if(!isNaN(provincia)) {
    peticion = inicializa_xhr();
    if (peticion) {
      peticion.onreadystatechange = muestraCiudades;
      peticion.open("POST", $ruta_servidor+"carga_ciudadesJSON.php?nocache=" + Math.random(), true);
      peticion.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	  peticion.send("provincia=" + provincia);
	  document.getElementById("ciudad").disabled = false;
    }
  }
}
//6.--FUNCION PARA MOSTRAR CIUDADES
function muestraCiudades() {
  if (peticion.readyState == 4) {
    if (peticion.status == 200) {
      var lista = document.getElementById("ciudad");
      var ciudades = eval('(' + peticion.responseText + ')');
	  lista.options.length = 0;
      var i=0;
      for(var codigo in ciudades) {
        lista.options[i] = new Option(ciudades[codigo], codigo);
        i++;
      }
    }
  }
}
//9.- CARGAR CLASIFICACIONES-----------------------------------------------
function cargaClasificaciones() {
  var lista = document.getElementById("ciudad");
  var ciudad = lista.options[lista.selectedIndex].value;
  peticion = inicializa_xhr();
    if (peticion) {
	  peticion.onreadystatechange = muestraClasificaciones;
      peticion.open("POST", $ruta_servidor+"carga_clasificacionesJSON.php?nocache=" + Math.random(), true);
      peticion.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	  peticion.send("ciudad=" + ciudad);
	  document.getElementById("clasificacion").disabled = false;
	 }
}
//8.- MOSTRAR CLASIFICACIONES--------------------------------------------
function muestraClasificaciones() {
  if (peticion.readyState == 4) {
    if (peticion.status == 200) {
	  var lista = document.getElementById("clasificacion");
	 var clasificaciones = eval('(' + peticion.responseText + ')');
	 lista.options.length = 0;
      var i=0;
      for(var codigo in clasificaciones) {
        lista.options[i] = new Option(clasificaciones[codigo], codigo);
        i++;
      }
    }
  }
}
//9.- CARGAR SERVICIOS
function cargaServicios() {
  document.getElementById("capa_servicio").style.display = 'block';
  var lista = document.getElementById("clasificacion");
  var clasificacion = lista.options[lista.selectedIndex].value;
  var ejemplo = document.getElementById("ciudad");
  var ciudad= ejemplo.options[ejemplo.selectedIndex].value;
  peticion = inicializa_xhr();
    if (peticion) {
	  peticion.onreadystatechange = muestraServicios;
      peticion.open("POST", $ruta_servidor+"carga_serviciosJSON.php?nocache="+ Math.random(), true);
	  peticion.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	  peticion.send("clasificacion=" + clasificacion +"&ciudad="+ ciudad);
	  document.getElementById("servicio").disabled = false;
	 }
}
//10.-MOSTRAR SERVICIOS
function muestraServicios() {
  if (peticion.readyState == 4) {
    if (peticion.status == 200) {
	  var lista = document.getElementById("servicio");
	  var servicios = eval('(' + peticion.responseText + ')');
	  lista.options.length = 0;
      var i=0;
      for(var codigo in servicios) {
        lista.options[i] = new Option(servicios[codigo], codigo);
        i++;
      }
    }
  }
}
//funcion para cargar la url dinamica
function url_ahora(){
	var regi = document.getElementById("region");
  	var regi = regi.options[regi.selectedIndex].value;
	if(regi == 1){
		script = "costa.php";
	}else{
		if(regi==2){
			script ="andes.php";
		}else{
			if(regi==3){
				script = "amazonia.php";
			}else{
				script = "galapagos.php";
			}
		}
	}
	var prov = document.getElementById("provincia");
  	var prov = prov.options[prov.selectedIndex].value;
	var ciud = document.getElementById("ciudad");
  	var ciud = ciud.options[ciud.selectedIndex].value;
	var clas = document.getElementById("clasificacion");
  	var clas = clas.options[clas.selectedIndex].value;
	var serv = document.getElementById("servicio");
  	var serv = serv.options[serv.selectedIndex].value;
	document.location = $dominio+script+"?opcion=datos&provincia="+prov+"&ciudad="+ciud+"&clasificacion="+clas+"&servicio="+serv;
}

function cargar_busqueda(){
 	peticion = inicializa_xhr();
	  if(peticion) {
		peticion.onreadystatechange = muestraRegiones;
		peticion.open("GET", $ruta_servidor+"carga_regionesJSON.php?nocache="+Math.random(), true);
		peticion.send(null);
		//bloqueos
	  document.getElementById("provincia").disabled = true;
	  document.getElementById("ciudad").disabled = true;
	  document.getElementById("clasificacion").disabled = true;
	  //document.getElementById("servicio").disabled = true;
	  document.getElementById("capa_servicio").style.display = 'none';
	  //cambios eventos
	  document.getElementById("region").onchange = cargaProvincias;
	  document.getElementById("provincia").onchange = cargaCiudades;
	  document.getElementById("ciudad").onchange = cargaClasificaciones;
	  document.getElementById("clasificacion").onchange = cargaServicios;
  	  }
}
////////////////CODIGO PARA APLICACION INFORMACION////////////////
//2.-FUNCION PARA MOSTRAR MENUS_SECCIONES-----------------------------------
function muestraSecciones() {
  if (peticion_http.readyState == 4) {
    if (peticion_http.status == 200) {
      var lista = document.getElementById("seccion");
      var secciones = eval('(' + peticion_http.responseText + ')');
	  lista.options[0] = new Option("- Informacion General -");
      var i=1;
      for(var codigo in secciones) {
        lista.options[i] = new Option(secciones[codigo], codigo);
        i++;
      }
    }
  }
}

//4.-FUNCION PARA CARGAR MENUS----------------------------------
function cargaMenus() {
  document.getElementById("capa_menu").style.display = 'block';
  var lista = document.getElementById("seccion");
  var seccion = lista.options[lista.selectedIndex].value;
  peticion_http = inicializa_xhr();
    if (peticion_http) {
      peticion_http.onreadystatechange = muestraMenus;
      peticion_http.open("POST", $ruta_servidor+"menus_menusJSON.php?nocache=" + Math.random(), true);
      peticion_http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	  peticion_http.send("seccion=" + seccion);
    }
  
}
//4.-FUNCION PARA MOSTRAR MENUS--------------------------------
function muestraMenus() {
  if (peticion_http.readyState == 4) {
    if (peticion_http.status == 200) {
	 var lista = document.getElementById("menu");
	 var menus= eval('(' + peticion_http.responseText + ')');
	 lista.options.length = 0;
      var i=0;
      for(var codigo in menus) {
        lista.options[i] = new Option(menus[codigo], codigo);
        i++;
      }
    }
  }
}

//5.-FUNCION PARA CARGAR ITEMS----------------------------------
function cargaItems(){
  document.getElementById("capa_item").style.display = 'block';
  var lista = document.getElementById("menu");
  var menu = lista.options[lista.selectedIndex].value;
  peticion_http = inicializa_xhr();
    if (peticion_http) {
      peticion_http.onreadystatechange = muestraItems;
      peticion_http.open("POST", $ruta_servidor+"menus_itemsJSON.php?nocache=" + Math.random(), true);
      peticion_http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	  peticion_http.send("menu=" + menu);
    }
}
//6.-FUNCION PARA MOSTRAR ITEMS--------------------------------
function muestraItems() {
  if (peticion_http.readyState == 4) {
    if (peticion_http.status == 200) {
	 var lista = document.getElementById("item");
	 var items= eval('(' + peticion_http.responseText + ')');
	 lista.options.length = 0;
      var i=0;
      for(var codigo in items) {
        lista.options[i] = new Option(items[codigo], codigo);
        i++;
      }
    }
  }
}
function url_menu(){
	var secc = document.getElementById("seccion");
  	var secc = secc.options[secc.selectedIndex].value;
	var menn = document.getElementById("menu");
	var menn = menn.options[menn.selectedIndex].value;
	var itte = document.getElementById("item");
	var itte = itte.options[itte.selectedIndex].value;
	script = "menu.php";
	document.location = $dominio+script+"?cod_sec="+secc+"&cod_men"+menn+"&cod_item="+itte;
}
function cargar_informacion(){
	peticion_http = inicializa_xhr();
	if(peticion_http){
		peticion_http.onreadystatechange = muestraSecciones;
		peticion_http.open("GET",$ruta_servidor+"menus_seccionesJSON.php?nocache="+Math.random(), true);
		peticion_http.send(null);
	}
   document.getElementById("capa_menu").style.display = 'none';
   document.getElementById("capa_item").style.display = 'none';
   document.getElementById("seccion").onchange = cargaMenus;
   document.getElementById("menu").onchange = cargaItems;
}
////////////////CODIGO PARA APLIACION LOGEO PARA CLUBVE///////////
//var peticion_log = null;
function crea_query_string() {
	//paso de parametros
	var identificacion = document.getElementById("identificacion");
	var iniciales = document.getElementById("iniciales");
	var password = document.getElementById("password");
	var certificado = document.getElementById("certificado");
	return "identificacion=" + encodeURIComponent(identificacion.value) +
	"&iniciales=" + encodeURIComponent(iniciales.value) +
	"&password=" + encodeURIComponent(password.value) +
	"&certificado=" + encodeURIComponent(certificado.value) +
	"&nocache=" + Math.random();
}
function comprobar() {
	var identificacion = document.getElementById("identificacion").value;
	var iniciales = document.getElementById("iniciales").value;
	var password = document.getElementById("password").value;
	peticion_log = inicializa_xhr();
	if(peticion_log) {
		peticion_log.onreadystatechange = procesaRespuesta;
		peticion_log.open("POST", $ruta_servidor+"compruebaDisponibilidad.php?nocache="+Math.random(), true);
		peticion_log.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		var query_string = crea_query_string();
		//alert(query_string);
		peticion_log.send(query_string);
	}
}

function procesaRespuesta() {
    if(peticion_log.readyState == 1){
		   document.getElementById("disponibilidad").innerHTML = '<img src="http://www.visitaecuador.com/menu_vertical/loading_uno.gif" with="20" height="20" />';
		 //document.getElementById("disponibilidad").innerHTML = 'Procesando..';
	}else if(peticion_log.readyState == 4) {
		if (peticion_log.status == 200) {
				var cadena_recibe = peticion_log.responseText;
				cadena_recibe = cadena_recibe.split('-');
				if(cadena_recibe[0]=="ok"){
				//if(peticion_log.responseText == "ok"){
					
					window.open("http://www.visitaecuador.com/club/certificado/certificado_club.php?miembro="+cadena_recibe[1]+"&promo="+cadena_recibe[2]+"&tipo="+cadena_recibe[3],'club','width=1000,height=600,menubar=yes,scrollbars=yes,toolbar=yes,location=yes,directories=yes,resizable=yes,top=0,left=0');
					document.getElementById("disponibilidad").innerHTML = ' ';
				}else{
					document.getElementById("disponibilidad").innerHTML = peticion_log.responseText;	
				}
					/*if(peticion_log.responseText == "cv"){
						document.getElementById("disponibilidad").innerHTML = "Miembro disponible";
					}
					if(peticion_log.responseText == "cc"){
						document.getElementById("disponibilidad").innerHTML = "Cuenta Caducada";
					}
					if(peticion_log.responseText == "ci"){
						document.getElementById("disponibilidad").innerHTML = "Ide, Iniciales o Password Incorrecto";
					}*/
		}
	}
}

function comprobar_logeo(){
	peticion_log = inicializa_xhr();
	document.getElementById("comprobar").onclick = comprobar;
}
//PRINCIPAL LLAMADAS A ONLOAD-------------------------------------
//Simon Willison funcion
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}
addLoadEvent(cargar_busqueda);
addLoadEvent(cargar_informacion);
addLoadEvent(comprobar_logeo);