function URLEncodeValue(obj)
{
    // The Javascript escape and unescape functions do not correspond
    // with what browsers actually do...
    var SAFECHARS = "0123456789" +                    // Numeric
                    "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +    // Alphabetic
                    "abcdefghijklmnopqrstuvwxyz" +
                    "-_.!~*'()";                    // RFC2396 Mark characters
    var HEX = "0123456789ABCDEF";

    var plaintext = obj;
    var encoded = "";
    for (var i = 0; i < plaintext.length; i++ ) {
        var ch = plaintext.charAt(i);
        if (ch == " ") {
            encoded += "+";                // x-www-urlencoded, rather than %20
        } else if (SAFECHARS.indexOf(ch) != -1) {
            encoded += ch;
        } else if (ch == ",") {
            encoded += "";
        } else {
            var charCode = ch.charCodeAt(0);
            if (charCode > 255) {
                alert( "Unicode Character '" 
                        + ch 
                        + "' cannot be encoded using standard URL encoding.\n" +
                          "(URL encoding only supports 8-bit characters.)\n" +
                          "A space (+) will be substituted." );
                encoded += "+";
            } else {
                encoded += "%";
                encoded += HEX.charAt((charCode >> 4) & 0xF);
                encoded += HEX.charAt(charCode & 0xF);
            }
        }
    } // for
    return encoded.toLowerCase();
}

function URLEncodeSubdomain(obj)
{
    // The Javascript escape and unescape functions do not correspond
    // with what browsers actually do...
    var SAFECHARS = "0123456789" +                    // Numeric
                    "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +    // Alphabetic
                    "abcdefghijklmnopqrstuvwxyz" +
                    "-_.!~*'()";                    // RFC2396 Mark characters
    var HEX = "0123456789ABCDEF";

    var plaintext = obj;
    var encoded = "";
    for (var i = 0; i < plaintext.length; i++ ) {
        var ch = plaintext.charAt(i);
        if (ch == " ") {
            encoded += "-";                // x-www-urlencoded, rather than %20
        } else if (SAFECHARS.indexOf(ch) != -1) {
            encoded += ch;
        } else {
            var charCode = ch.charCodeAt(0);
            if (charCode > 255) {
                alert( "Unicode Character '" 
                        + ch 
                        + "' cannot be encoded using standard URL encoding.\n" +
                          "(URL encoding only supports 8-bit characters.)\n" +
                          "A space (+) will be substituted." );
                encoded += "-";
            } else {
                encoded += "%";
                encoded += HEX.charAt((charCode >> 4) & 0xF);
                encoded += HEX.charAt(charCode & 0xF);
            }
        }
    } // for
    return encoded.toLowerCase();
}


function setUrlItem(nombre){

 nombre=nombre.toLowerCase();
 nombre=nombre.replace(/\'/g, "");
 nombre=nombre.replace(/\"/g, "");
 nombre=nombre.replace(/\//g, "");
 nombre=nombre.replace(/\\/g, "");
 nombre=nombre.replace(/\\/g, "");
 nombre=nombre.replace(/ /g, "-");
 nombre=nombre.replace(/\./g, "");
 nombre=nombre.replace(/_/g, "");
 nombre=nombre.replace(/:/g, "");
 nombre=nombre.replace(/;/g, "");
 
 nombre=nombre.replace(/\?/g, "");
 nombre=nombre.replace(/¿/g, "");
 nombre=nombre.replace(/!/g, "");
 nombre=nombre.replace(/¡/g, "");
 nombre=nombre.replace(/\+/g, " ");
 nombre=nombre.replace(/`/g, "");
 nombre=nombre.replace(/^/g, "");
 nombre=nombre.replace(/,/g, "");
 nombre=nombre.replace(/ /g, "-");

 nombre=nombre.replace(/á/g, "a");
 nombre=nombre.replace(/é/g, "e");
 nombre=nombre.replace(/í/g, "i");
 nombre=nombre.replace(/ó/g, "o");
 nombre=nombre.replace(/ú/g, "u");
 
 nombre=nombre.replace(/à/g, "a");
 nombre=nombre.replace(/è/g, "e");
 nombre=nombre.replace(/ì/g, "i");
 nombre=nombre.replace(/ò/g, "o");
 nombre=nombre.replace(/ù/g, "u");

 nombre=nombre.replace(/ä/g, "a");
 nombre=nombre.replace(/ë/g, "e");
 nombre=nombre.replace(/ï/g, "i");
 nombre=nombre.replace(/ö/g, "o");
 nombre=nombre.replace(/ü/g, "u");

 nombre=nombre.replace(/\(/g, "");
 nombre=nombre.replace(/\)/g, "");

 nombre=nombre.replace(/ñ/g, "n");
 nombre=nombre.replace(/ç/g, "c");

 return nombre;
}

function trim (myString){
    return myString.replace(/^\s+/g,'').replace(/\s+$/g,'');
}
function setUrlItemEmpresas(nombre){

    
 nombre=nombre.toLowerCase();
 nombre=trim(nombre);
 nombre=nombre.replace(/\'/g, "");
 nombre=nombre.replace(/\"/g, "");
 nombre=nombre.replace(/\//g, "");
 nombre=nombre.replace(/\\/g, "");
 nombre=nombre.replace(/\\/g, "");
 nombre=nombre.replace(/ /g, "+");
 nombre=nombre.replace(/\./g, "");
 nombre=nombre.replace(/_/g, "");
 nombre=nombre.replace(/:/g, "");
 nombre=nombre.replace(/;/g, "");
 
 nombre=nombre.replace(/\?/g, "");
 nombre=nombre.replace(/¿/g, "");
 nombre=nombre.replace(/!/g, "");
 nombre=nombre.replace(/¡/g, "");
 nombre=nombre.replace(/\+/g, " ");
 nombre=nombre.replace(/`/g, "");
 nombre=nombre.replace(/^/g, "");
 nombre=nombre.replace(/,/g, "");
 nombre=nombre.replace(/ /g, "+");

 nombre=nombre.replace(/á/g, "a");
 nombre=nombre.replace(/é/g, "e");
 nombre=nombre.replace(/í/g, "i");
 nombre=nombre.replace(/ó/g, "o");
 nombre=nombre.replace(/ú/g, "u");
 
 nombre=nombre.replace(/à/g, "a");
 nombre=nombre.replace(/è/g, "e");
 nombre=nombre.replace(/ì/g, "i");
 nombre=nombre.replace(/ò/g, "o");
 nombre=nombre.replace(/ù/g, "u");

 nombre=nombre.replace(/ä/g, "a");
 nombre=nombre.replace(/ë/g, "e");
 nombre=nombre.replace(/ï/g, "i");
 nombre=nombre.replace(/ö/g, "o");
 nombre=nombre.replace(/ü/g, "u");

 nombre=nombre.replace(/\(/g, "");
 nombre=nombre.replace(/\)/g, "");

 nombre=nombre.replace(/ñ/g, "n");
 nombre=nombre.replace(/ç/g, "c");

 return nombre;
}


function rellena_input(i,tipo_lugar){

    formulario_peticion.urlDestino.value = urlDestino[i];
    formulario_peticion.provincia.value = provincia[i];
    formulario_peticion.localidad.value = localidad[i];
    formulario_peticion.urlLocalidad.value = setUrlItem(localidad[i]);
    formulario_peticion.coordenadas.value = coordenadas[i];
    formulario_peticion.direccionCompleta.value = direccionCompleta[i];
    formulario_peticion.urlDireccion.value = urlDireccion[i];
    formulario_peticion.direccion.value = direccion[i];
    
    formulario_peticion.destino.value = direccionCompleta[i];                                        
    formulario_peticion.tipo_lugar.value = tipo_lugar;           
    
    document.getElementById(destino_suge).style.display="none";        
    clearInterval(timer_suge);                             
    
    
}


function overClas(id){
    
    document.getElementById('div_clasif_'+id).style.cursor = "pointer";
}
function outClas(id){
    
    //document.getElementById('p_clasif_'+id).style.color = '#757371';
}

function change_box_ruta(tipo){
    document.getElementById('ruta_'+tipo).style.display = 'none';    
    document.getElementById('ruta_'+tipo+"2").style.display = '';    
}

function change_box_ruta2(tipo){
    document.getElementById('ruta_'+tipo+"b").style.display = 'none';    
    document.getElementById('ruta_'+tipo+"2b").style.display = '';    
}

function busca_actividades(){
    document.getElementById('busca_actividades1').style.display = 'none';
    document.getElementById('busca_actividades2').style.display = '';
}

function buscar_serv(){
    //document.getElementById('buscar_serv').style.display = 'none';
    document.getElementById('buscar_serv2').style.display = '';
    //parent.document.getElementById('empr_cercanas').style.height = '100px';
}


function abre_busq_tiem(){
    document.getElementById('caja_busq').style.display = '';
}

function cambia_lunas(frm){
    var ano = frm.ano.value;
    var mes = frm.mes.value;
    document.location="/tiempo/fases-lunares/"+mes+"-"+ano;
}

var timer_suge;
function cierra_suge(div){
    clearInterval(timer_suge);
    timer_suge = setInterval("cierra_suge2('"+div+"')", 2000);
    
}
function cierra_suge2(div){
    document.getElementById(div).style.display = 'none';
    clearInterval(timer_suge);
}

function send_calle(frm){
    if(frm.url_destino.value==""){
        alert('Por favor, selecciona una calle de la lista para poder continuar')        
    }else{
        document.location = frm.url_destino.value;
    }
}

function rellena_input_tiempo(calle,url){
    document.getElementById('destino').value=calle;
    document.getElementById('url_destino').value=url;
    document.getElementById('call_suggest').style.display = 'none';
    clearInterval(timer_suge);
}

function goto_tiempo(frm){
    if(frm.url_destino.value==""){
        alert('Por favor, selecciona una ciudad de la lista para continuar.');
    }else{
        document.location = "http://"+frm.url_destino.value+".callejero.net/tiempo/";
    }
}

function busca_empresa(frm){
    
    if(frm.actividad.value=="" && frm.empresa.value==""){
        alert('Debes introducir una actividad o el nombre de una empresa para continuar.');
        return false;
    }
    
    var actividad = frm.actividad.value;
    if(actividad==""){
        actividad = "actividad";
    }
    var empresa = frm.empresa.value;
    if(empresa==""){
        empresa = "empresa";
    }
    
    var provincia = frm.provincia.value;
    if(provincia==""){
        provincia = "provincia";
    }
    var ciudad = frm.ciudad.value;
    if(ciudad==""){
        ciudad = "ciudad";
    }
    
    if(provincia=="provincia" && ciudad=="ciudad"){
        document.location = "http://www.callejero.net/empresas/"+setUrlItemEmpresas(actividad)+"-"+setUrlItemEmpresas(empresa)+".html";        
    }else{
        document.location = "http://www.callejero.net/empresas/"+setUrlItemEmpresas(actividad)+"-"+setUrlItemEmpresas(empresa)+"-"+setUrlItemEmpresas(provincia)+"-"+setUrlItemEmpresas(ciudad)+".html";        
    }
    
    
    
    
}


function abre_busq(){
    document.getElementById('caja_busqueda').style.display = '';
    document.getElementById('caja_busqueda_pre').style.display = 'none';
}


/* Funciones validación formularios */
var whitespace = " \t\n\r";
var reWhitespace = /^\s+$/

/** Verifica que no este vacio **/
function isEmpty(s){
    return ((s == null) || (s.length == 0)) 
}
 
/*** Verifica que no sean espacios en blanco o vacio ***/
function isWhitespace (s){
    return (isEmpty(s) || reWhitespace.test(s));
}
 
function isEmail(s){
    return (isWhitespace(s) || validarEmail(s));
}
function validarEmail(valor) {
        if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(valor)){
                return false;
        } else {
                return true;
        }
}


function send_cont(frm){
    if(isWhitespace(frm.nombre.value)){
        alert("Por favor, introduce tu nombre");
        frm.nombre.focus();
        return false;
    }
    if(isWhitespace(frm.mail.value)){
        alert("Por favor, introduce tu e-mail");
        frm.mail.focus();
        return false;
    }
    if(isEmail(frm.mail.value)){
        alert("Tu e-mail no es correcto\n");
        frm.mail.focus();
        return false;
    }
    if(frm.motivo.value=="1"){
        alert("Por favor, selecciona un motivo");
        frm.motivo.focus();
        return false;
    }
    
    var date = new Date();
    var timestamp = date.getTime();
    
    var parameters = "nombre="+frm.nombre.value+"&mail="+frm.mail.value+"&motivo="+frm.motivo.value+"&comentario="+frm.comentario.value+"&time="+timestamp;
    
    ajax = ajaxobj();
    ajax.open("POST", "/ajax/send_contacto.ajax.php");
    ajax.onreadystatechange=function(){
        if (ajax.readyState==4){
            retorno= ajax.responseText;
            document.getElementById('boton_send').style.display = 'none';
            document.getElementById('boton_send2').style.display = '';
            
        }
    }
    ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded;');
    ajax.send(parameters);
 
}

function search_ruta(tipo){
    
    switch(tipo){
        case "calle":
            document.getElementById('caja_busq_c2').style.display = 'none';
            document.getElementById('caja_busqueda').style.display = '';
            break;
        
        case "ruta":
            document.getElementById('caja_busq_c2').style.display = '';
            document.getElementById('caja_busqueda').style.display = 'none';
            break;
    }
}

function add_fav(url,titulo){
    if (document.all){
        window.external.AddFavorite(url,titulo);
    }else{
        alert('Pulsa Control+D para añadir a favoritos.');
    }
}

function send_friend(url){
    
    if(document.getElementById('div_send_friend')){
        div_send = document.getElementById('div_send_friend');
        
        div_send.style.left = (screen.width / 2) - 310 + "px";
        var top = document.documentElement.scrollTop+200;
        div_send.style.top = top+"px";
        div_send.style.position = "absolute";
        div_send.style.height = "360px";
        div_send.style.width = "620px";
        div_send.style.background = "url('/images/fondoc.png')";
        div_send.innerHTML = '<iframe src="/pop/send_friend.php?url='+url+'" width="620" height="360" frameborder="0" allowtransparency="true"></iframe>';
        div_send.style.display = '';
        
    }else{
        
        var div_send = document.createElement("div");
        div_send.setAttribute('id','div_send_friend');
        document.body.appendChild(div_send);
        
        div_send.style.left = (screen.width / 2) - 310 + "px";
        var top = document.documentElement.scrollTop+200;
        div_send.style.top = top+"px";
        div_send.style.position = "absolute";
        div_send.style.height = "360px";
        div_send.style.width = "620px";
        div_send.style.background = "url('/images/fondoc.png')";
        div_send.innerHTML = '<iframe src="/pop/send_friend.php?url='+url+'" width="620" height="360" frameborder="0" allowtransparency="true"></iframe>';
    }
}

function close_div(div){
    div.style.display = 'none';
}

function imprimir(tipo,vari){

    switch(tipo){
        case "ruta":
            url_print = "/pop/print_ruta.php?ruta="+vari;
            break;
            
        case "calle":
            url_print = "/pop/print_calle.php?idc="+vari;
            break;
            
        case "ciudad":
            url_print = "/pop/print_calle.php?urlc="+vari;
            break;
            
        default:
            url_print = "/pop/print.php";
            break;
    }
    width = 750; 
    height = 800; 
    leftPosition = (screen.width) ? (screen.width-width)/2 : 0;
    if (screen.height && (screen.height <= 600)) {
        topPosition = 0;
    } else {
        topPosition = (screen.height) ? (screen.height-height)/2 : 0;
    }
    var settings='width='+width+',  height='+height+', left='+leftPosition+', top=10, toolbar=0, location=0, directories=0, menubar=0, scrollbars=1, resizable=0, status=0';
    var popup=window.open(url_print, 'print', settings);
    popup.focus();

}

var boton_actual = 1;
function filtra_empr(id,tipo){
	document.frm_servicios_zona.act.value=tipo;
	get_empresas_cercanas(document.frm_servicios_zona,'');
/*
	for(x=1;x<=5;x++){
		document.getElementById('botf'+x).style.background='#cc0000';
		document.getElementById('botf'+x).style.color='#ffffff';
	}*/
	document.getElementById('botf'+boton_actual).style.background='#cc0000';
	document.getElementById('botf'+boton_actual).style.color='#ffffff';
	document.getElementById('botf'+id).style.background='#ffffff';
	document.getElementById('botf'+id).style.color='#cc0000';

	boton_actual = id;

}

function ck(tipo){
	if(tipo==2){
		document.cookie = "callejero=2; expires=Fri, 27 Jul 2021 02:47:11 UTC; path=/;domain=callejero.net;";		
	}else{
		document.cookie = "callejero=1; expires=Fri, 27 Jul 2021 02:47:11 UTC; path=/;domain=callejero.net;";
		document.location=document.location;
	}
	
}
