var Cookie = {

	domain:	"emtvalencia.es",

	url: window.location.href,
	
	setCookie : function(cookie_name, cookie_value, lifespan_in_seconds, valid_domain) {
	    var domain_string = valid_domain ? ("; domain=" + valid_domain) : '' ;
	    
	    document.cookie = cookie_name + "=" + cookie_value +
		   	"; max-age=" + 60 * lifespan_in_seconds + "; path=/" + domain_string ;
	},

	getCookie : function(cookie_name) {
		if (document.cookie.length > 0) {
			c_start = document.cookie.indexOf(cookie_name + "=");
			if (c_start != -1) {
				c_start = c_start + cookie_name.length + 1;
				c_end = document.cookie.indexOf(";", c_start);
				if (c_end == -1) 
					c_end = document.cookie.length;
				return unescape(document.cookie.substring(c_start, c_end));
		   	}
		}
		return "";
	},
    
    deleteCookie : function (cookie_name, valid_domain) {
        var domain_string = valid_domain ? ("; domain=" + valid_domain) : '' ;
        document.cookie = cookie_name + "=; max-age=0; path=/" + domain_string ;
    },
	
    setCookieComoIr : function () {
    	if (($('varX').value != 0) && ($('varY').value != 0) && ($('seleccion').value != '') &&
    		($('varXDestino').value != 0) && ($('varYDestino').value != 0) && ($('seleccionDestino').value != '')) {
    		var d = new Date();
    		var cadena = "COMO_IR#" + $('varX').value + "#" + $('varY').value + "#" + $('seleccion').value + "#" + $('tipoOrigen').value + "#" + $('varXDestino').value + "#" + $('varYDestino').value + "#" + $('seleccionDestino').value + "#" + $('tipoDestino').value;
    		Cookie.setCookie('emtherramientasexpress', cadena, 300, Cookie.domain);
    		//setTimeout("document.location.href='./como_ir.php'", 1000);
    		setTimeout("parent.document.location.href='"+Cookie.getURL(_URL_COMO_IR_,_URL_COMO_IR_SEC_)+"'", 1000);
    	}
    	else {
    		alert(Locale.getText("error_seleccion_origen_destino"));
    	}
    },
    
    setCookieComoIrAnterior : function () {
    	if (($('varXO').value != 0) && ($('varYO').value != 0) && ($('seleccionO').value != '') &&
    		($('varXD').value != 0) && ($('varYD').value != 0) && ($('seleccionD').value != '')) {
    		var d = new Date();
    		var cadena = "COMO_IR#" + $('varXO').value + "#" + $('varYO').value + "#" + $('seleccionO').value + "#" + $('tipoO').value + "#" + $('varXD').value + "#" + $('varYD').value + "#" + $('seleccionD').value + "#" + $('tipoD').value;
    		Cookie.setCookie('emtherramientasexpress', cadena, 300, Cookie.domain);
    		//setTimeout("document.location.href='./como_ir.php'", 1000);
    		setTimeout("parent.document.location.href='"+Cookie.getURL(_URL_COMO_IR_,_URL_COMO_IR_SEC_)+"'", 1000);
    	}
    	else {
    		alert(Locale.getText("error_ruta_anterior"));
    		
    	}
    },
    
	setCookieComoIrOrigen : function () {
    	if (($('varX').value != 0) && ($('varY').value != 0) && ($('seleccion').value != '')) {
    		var d = new Date();
    		var cadena = "COMO_IR_ORIGEN#" + $('varX').value + "#" + $('varY').value + "#" + $('seleccion').value + "#" + $('tipoOrigen').value; 
    		Cookie.setCookie('emtherramientasexpress', cadena, 300, Cookie.domain);
    		//setTimeout("document.location.href='./como_ir.php'", 1000);
    		setTimeout("parent.document.location.href='"+Cookie.getURL(_URL_COMO_IR_,_URL_COMO_IR_SEC_)+"'", 1000);
    	}
    	else {
    		alert(Locale.getText("error_seleccionar_calle"));
    	}
    },
    
	setCookieComoIrDestino: function () {
    	if (($('varX').value != 0) && ($('varY').value != 0) && ($('seleccion').value != '')) {
    		var d = new Date();
    		var cadena = "COMO_IR_DESTINO#" + $('varX').value + "#" + $('varY').value + "#" + $('seleccion').value + "#" + $('tipoOrigen').value; 
    		Cookie.setCookie('emtherramientasexpress', cadena, 300, Cookie.domain);
    		//alert("Cookie OK! Falta lanzar PHP...");
    		//setTimeout("document.location.href='./como_ir.php'", 1000);
    		setTimeout("parent.document.location.href='"+Cookie.getURL(_URL_COMO_IR_,_URL_COMO_IR_SEC_)+"'", 1000);
    	}
		else {
			alert(Locale.getText("error_seleccionar_calle"));
		}
	},
    
	setCookieDondeEstaOrigen: function () {
    	if (($('varX').value != 0) && ($('varY').value != 0) && ($('seleccion').value != '')) {
    		var d = new Date();
    		var cadena = "DONDE_ESTA_ORIGEN#" + $('varX').value + "#" + $('varY').value + "#" + $('seleccion').value + "#" + $('tipoOrigen').value + "#" + $('varXDestino').value + "#" + $('varYDestino').value + "#" + $('seleccionDestino').value + "#" + $('tipoDestino').value;
    		Cookie.setCookie('emtherramientasexpress', cadena, 300, Cookie.domain);
    		//setTimeout("document.location.href='./donde_esta.php'", 1000);
    		setTimeout("parent.document.location.href='"+Cookie.getURL(_URL_DONDE_ESTA_,_URL_DONDE_ESTA_SEC_)+"'", 1000);
    	}
		else {
			alert(Locale.getText("error_seleccionar_calle"));
		}
	},
	
	setCookieDondeEstaDestino: function () {
    	if (($('varXDestino').value != 0) && ($('varYDestino').value != 0) && ($('seleccionDestino').value != '')) {
    		var d = new Date();
    		var cadena = "DONDE_ESTA_DESTINO#" + $('varXDestino').value + "#" + $('varYDestino').value + "#" + $('seleccionDestino').value + "#" + $('tipoDestino').value + "#" + $('varX').value + "#" + $('varY').value + "#" + $('seleccion').value + "#" + $('tipoOrigen').value; 
    		Cookie.setCookie('emtherramientasexpress', cadena, 300, Cookie.domain);
    		//setTimeout("document.location.href='./donde_esta.php'", 1000);
    		setTimeout("parent.document.location.href='"+Cookie.getURL(_URL_DONDE_ESTA_,_URL_DONDE_ESTA_SEC_)+"'", 1000);
    	}
		else {
			alert(Locale.getText("error_seleccionar_calle"));
		}
	},
	
	setCookieQueTengoCercaOrigen: function () {
    	if (($('varX').value != 0) && ($('varY').value != 0) && ($('seleccion').value != '')) {
    		var d = new Date();
    		var cadena = "QUE_TENGO_CERCA_ORIGEN#" + $('varX').value + "#" + $('varY').value + "#" + $('seleccion').value + "#" + $('tipoOrigen').value + "#" + $('varXDestino').value + "#" + $('varYDestino').value + "#" + $('seleccionDestino').value + "#" + $('tipoDestino').value; 
    		Cookie.setCookie('emtherramientasexpress', cadena, 300, Cookie.domain);
    		//setTimeout("document.location.href='./donde_esta.php'", 1000);
    		setTimeout("parent.document.location.href='"+Cookie.getURL(_URL_DONDE_ESTA_,_URL_DONDE_ESTA_SEC_)+"'", 1000);
    	}
		else {
			alert(Locale.getText("error_seleccionar_calle"));
		}
	},
	
	setCookieQueTengoCercaDestino: function () {
    	if (($('varXDestino').value != 0) && ($('varYDestino').value != 0) && ($('seleccionDestino').value != '')) {
    		var d = new Date();
    		var cadena = "QUE_TENGO_CERCA_DESTINO#" + $('varXDestino').value + "#" + $('varYDestino').value + "#" + $('seleccionDestino').value + "#" + $('tipoDestino').value + "#" + $('varX').value + "#" + $('varY').value + "#" + $('seleccion').value + "#" + $('tipoOrigen').value; 
    		Cookie.setCookie('emtherramientasexpress', cadena, 300, Cookie.domain);
    		//setTimeout("document.location.href='./donde_esta.php'", 1000); 
    		setTimeout("parent.document.location.href='"+Cookie.getURL(_URL_DONDE_ESTA_,_URL_DONDE_ESTA_SEC_)+"'", 1000);
    	}
		else {
			alert(Locale.getText("error_seleccionar_calle"));
		}
	},   
	
	getCookieComoIr: function () {
		var cadena = Cookie.getCookie('emtherramientasexpress');
		var datos = cadena.split("#");
		var txt = "";
		
		if (datos[0] == 'COMO_IR') {
			$('varX').value = datos[1]; 
			$('varY').value = datos[2];
			$('seleccion').value = datos[3];		
			$('tipoOrigen').value = datos[4];
			if (datos[3].length > 39) txt = datos[3].substr(0,37) + "...";
			else txt = datos[3];
			$('seleccion_origen').innerHTML = txt;
			
			$('varXDestino').value = datos[5]; 
			$('varYDestino').value = datos[6];
			$('seleccionDestino').value = datos[7];
			$('tipoDestino').value = datos[8];
			if (datos[7].length > 39) txt = datos[7].substr(0,37) + "...";
			else txt = datos[7];
			$('seleccion_destino').innerHTML = txt;
			
			FuncionesMapa.calcularRuta();
		}
		else if (datos[0] == 'COMO_IR_ORIGEN') {
			$('varX').value = datos[1]; 
			$('varY').value = datos[2];
			$('seleccion').value = datos[3];
			$('tipoOrigen').value = datos[4];
			if (datos[3].length > 39) txt = datos[3].substr(0,37) + "...";
			else txt = datos[3];
			$('seleccion_origen').innerHTML = txt;
		}
		else if (datos[0] == 'COMO_IR_DESTINO') {
			$('varXDestino').value = datos[1]; 
			$('varYDestino').value = datos[2];
			$('seleccionDestino').value = datos[3];
			$('tipoDestino').value = datos[4];
			if (datos[3].length > 39) txt = datos[3].substr(0,37) + "...";
			else txt = datos[3];
			$('seleccion_destino').innerHTML = txt;
		}
		Cookie.setCookie('emtherramientasexpress', 'BLANK', 300, Cookie.domain);
	},
	
	getCookieDondeEsta: function () {
		var cadena = Cookie.getCookie('emtherramientasexpress');
		var datos = cadena.split("#");
		var txt = "";
		
		if (datos[0] == 'DONDE_ESTA_ORIGEN') {
			$('varX').value = datos[1]; 
			$('varY').value = datos[2];
			$('seleccion').value = datos[3];
			$('tipoOrigen').value = datos[4];
			if (datos[3].length > 39) txt = datos[3].substr(0,37) + "...";
			else txt = datos[3];
			$('seleccion_origen').innerHTML = txt;

			$('varXO').value = datos[1]; 
			$('varYO').value = datos[2];
			$('seleccionO').value = datos[3];
			$('tipoO').value = datos[4];

			$('varXD').value = datos[5]; 
			$('varYD').value = datos[6];
			$('seleccionD').value = datos[7];
			$('tipoD').value = datos[8];

			FuncionesMapaDE.dondeEsta('de');
		}
		if (datos[0] == 'DONDE_ESTA_DESTINO') {
			$('varX').value = datos[1]; 
			$('varY').value = datos[2];
			$('seleccion').value = datos[3];
			$('tipoOrigen').value = datos[4];
			if (datos[3].length > 39) txt = datos[3].substr(0,37) + "...";
			else txt = datos[3];
			$('seleccion_origen').innerHTML = txt;

			$('varXO').value = datos[5]; 
			$('varYO').value = datos[6];
			$('seleccionO').value = datos[7];
			$('tipoO').value = datos[8];

			$('varXD').value = datos[1]; 
			$('varYD').value = datos[2];
			$('seleccionD').value = datos[3];
			$('tipoD').value = datos[4];

			FuncionesMapaDE.dondeEsta('de');
		}
		else if (datos[0] == 'QUE_TENGO_CERCA_ORIGEN') {
			$('varX').value = datos[1]; 
			$('varY').value = datos[2];
			$('seleccion').value = datos[3];
			$('tipoOrigen').value = datos[4];
			if (datos[3].length > 39) txt = datos[3].substr(0,37) + "...";
			else txt = datos[3];
			$('seleccion_origen').innerHTML = txt;
			
			$('varXO').value = datos[1]; 
			$('varYO').value = datos[2];
			$('seleccionO').value = datos[3];
			$('tipoO').value = datos[4];

			$('varXD').value = datos[5]; 
			$('varYD').value = datos[6];
			$('seleccionD').value = datos[7];
			$('tipoD').value = datos[8];
			
			FuncionesMapaQTC.queTengoCerca('qtc');
		}
		else if (datos[0] == 'QUE_TENGO_CERCA_DESTINO') {
			$('varX').value = datos[1]; 
			$('varY').value = datos[2];
			$('seleccion').value = datos[3];
			$('tipoOrigen').value = datos[4];
			if (datos[3].length > 39) txt = datos[3].substr(0,37) + "...";
			else txt = datos[3];
			$('seleccion_origen').innerHTML = txt;
			
			$('varXO').value = datos[5]; 
			$('varYO').value = datos[6];
			$('seleccionO').value = datos[7];
			$('tipoO').value = datos[8];

			$('varXD').value = datos[1]; 
			$('varYD').value = datos[2];
			$('seleccionD').value = datos[3];
			$('tipoD').value = datos[4];
			
			FuncionesMapaQTC.queTengoCerca('qtc');
		}
		Cookie.setCookie('emtherramientasexpress', 'BLANK', 300, Cookie.domain);
	},
	
	setCookieRutaPlano: function() {
		var d = new Date();
     	var cadena = "RUTA#1# ";
        Cookie.setCookie('emtherramientasplano', cadena, 300, Cookie.domain);
        setTimeout("parent.document.location.href='" + Cookie.getURL(_URL_Plano_,_URL_Plano_Secure_) + "'", 1000);
	},

	setCookieDondeEstaPlano: function() {
		var d = new Date();
		var tipoNombre;
		var nombre1 = "";
		var nombre2 = "";
		if ($('tipoOrigen').value == '1') {
			tipoNombre = 'CALLE';
			nombre1 = $('seleccion').value;
			nombre2 = "";
		}
		else if ($('tipoOrigen').value == '2') {
			tipoNombre = 'ESQUINA';
			var nombres = $('seleccion').value.split(" con ");
			nombre1 = nombres[0];
			nombre2 = nombres[1];
		}
		else if ($('tipoOrigen').value == '3') { 
			tipoNombre = 'POI';
			nombre1 = $('seleccion').value;
			nombre2 = "";
		}
		else {
			tipoNombre = 'PARADA';
			var nombres = $('seleccion').value.split(" - ");
			nombre2 = nombres[0];
			for (i=1; i<nombres.length - 1; i++) nombre1 += nombres[i] + " - ";
			nombre1 += nombres[nombres.length - 1];
		}
				
     	var cadena = tipoNombre + "#" + $('whatTab').value + "#" + nombre1 + "#" + nombre2 + "#" + $('varX').value + "#" + $('varY').value;
     	//alert(cadena);
     	Cookie.setCookie('emtherramientasplano', cadena, 300, Cookie.domain);
        setTimeout("parent.document.location.href='" + Cookie.getURL(_URL_Plano_,_URL_Plano_Secure_) + "'", 1000);
	},
	
	getCookiePlano: function() {
		var cadena = Cookie.getCookie('emtherramientasplano');
		//alert(cadena);
		if (cadena != 'BLANK') {
			var datos = cadena.split("#");
			$('llamadaDesdeEsquemaLineas').value = datos[0];
			$('whatTab').value = datos[1];
			$('nombre').value = datos[2];
		}
		Cookie.setCookie('emtherramientasplano', 'BLANK', 300, Cookie.domain);
	},
	
	getURL: function(url, urlSec){
		//COMPROBAMOS SI ESTAMOS EN MODO SEGURO			
		if (Cookie.url.indexOf("https") == -1)			
			return url;				
		else 
			return urlSec;
	}
}

