/* Hecho */
function CreateService() {
    var url = "action=createService&NombreServicio=" + document.getElementById("NombreServicio").value + permisosZonas();

    disabled();
    AjaxUploadtoServer("MapBrain", url,"loadResults");

}

/* Hecho */
function joinZonesAndServices() {
    var url = "action=joinZonesAndServices" + permisosZonas();
    
    disabled();
    AjaxUploadtoServer("MapBrain", url,"loadResults");
}

function permisosZonas() {
    var url = "";
    
    for (i = 0; i<document.getElementsByName("IdServicio").length; i++) {
        if (document.getElementsByName("IdServicio")[i].checked) {
            url += "&IdServicio=" + document.getElementsByName("IdServicio")[i].value;
        }
    }
    
    for (i = 0; i<document.getElementsByName("PermisoEL").length; i++) {
        if (document.getElementsByName("PermisoEL")[i].checked) {
            url += "&PermisoEL=" + document.getElementsByName("PermisoEL")[i].value;
        }
    }

    for (i = 0; i<document.getElementsByName("SoloPuntos").length; i++) {
        if (document.getElementsByName("SoloPuntos")[i].checked) {
            url += "&SoloPuntos=" + document.getElementsByName("SoloPuntos")[i].value;
        }
    }

    for (i = 0; i<document.getElementsByName("zonas").length; i++) {
        if (document.getElementsByName("zonas")[i].checked) {
            url += "&zonas=" + document.getElementsByName("zonas")[i].value;
        }
    }

    return url;
}

/* Hecho */
function newService() {
    var url = "action=PageNewService";
    
    loadIbox();
    AjaxUploadtoServer("MapBrain", url, "infoIbox");
}

function newUser() {
    //alert("El registro de usuario no está disponibble temporalmente por mejoras en el servicio.\nDisculpen las molestias.");
    //return false;
    var url = "action=registerUserForm";
    
    loadIbox();
    AjaxUploadtoServer("MapBrain", url, "infoIbox");
}

function registerUser(){
    var url = "action=registerUser&usuario=" + document.getElementById("usuario").value + "&contrasena=" + document.getElementById("contrasena").value + "&correo_e=" + document.getElementById("correo_e").value;
    
    disabled();
    AjaxUploadtoServer("MapBrain", url,"loadResults");
}

/* Hecho */
function kindsearch(type) {
    var url = "action=kindSearch&typeSearch=" + type;
    
    loadIbox();
    AjaxUploadtoServer("MapBrain", url, "infoIbox");
}

/* Hecho */
function Search() {
    var url = "action=search&typeSearch=" + document.getElementById("typeSearch").value + "&whatSearch=" + document.getElementById("whatSearch").value;
    
    disabled();
    AjaxUploadtoServer("MapBrain", url,"loadResults");
}

/* Hecho */
function PageSearchIntoService(nameservice, zone, service) {
    var url = "action=PageSearchIntoService&nameservice=" + nameservice +"&idZone=" + zone + "&idService=" + service;
    
    loadIbox();
    AjaxUploadtoServer("MapBrain", url, "infoIbox");
}

/* Hecho */
function showInfo(ToShowInfo, type) {
    var url = "action=showInfo&toShowInfo=" + ToShowInfo + "&type=" + type;
    
    loadIbox();
    AjaxUploadtoServer("MapBrain", url, "infoIbox");
}

/* Hecho */
function seeMore(type, num) {
    var url = "action=seeMore&type=" + type + "&num=" + num;
    
    loadIbox();
    AjaxUploadtoServer("MapBrain", url, "infoIbox");
}

function getLasts() {
     var url = "action=search&typeSearch=tags&whatSearch=&lastUpdated=true";
     disabled();
     AjaxUploadtoServer("MapBrain", url,"loadResults");
}

/* Hecho */
function searchIntoService() {
    var url = "action=searchIntoService&zoneToSearch=" + document.getElementById("zoneToSearch").value + "&serviceToSearch=" + document.getElementById("serviceToSearch").value + "&whatSearch=" + document.getElementById("whatSearch").value;
     
     disabled();
     AjaxUploadtoServer("MapBrain", url,"loadResults");
}

/* Hecho */
function addZonesToService(idService, nameService) {
    var url = "action=addZonesToService&idService=" + idService + "&nameService=" + nameService;
    
    loadIbox();
    AjaxUploadtoServer("MapBrain", url, "infoIbox");
}

/* Hecho */
function addServicesToZone(idZone, nameZone) {
    var url = "action=addServicesToZone&idZone=" + idZone + "&nameZone=" + nameZone;
    
    loadIbox();
    AjaxUploadtoServer("MapBrain", url, "infoIbox");
}

function timeout(Servicio, Zona, Load, SoloPuntos, simulation) {
    if (Servicio != null && Zona != null) {
        setTimeout("getTitlesOfService('" + Servicio + "', '" + Zona + "')",150);
    }
    
    if (Load != null && SoloPuntos != null) {
        setTimeout("loadService('" + Load + "', '" + SoloPuntos + "')",800);
    }
    
    if(simulation == "bus") {
        setTimeout("BusSimulator()",1200);
    }
    else if (simulation == "parkings") {
        setTimeout("ExternalData('10','1','13/10/2008')",1200);
    }
}

function getComentarios(idAgrupacion) {
    var url = "action=getComentarios&idAgrupacion=" + idAgrupacion;
    
    AjaxUploadtoServer("MapBrain", url, "comentarios");
}

function changeVisibility(){
    if (document.getElementById("buttonopenform") != null) {
        document.getElementById("buttonopenform").disabled = (document.getElementById("buttonopenform").disabled==false ? true : false);
        document.getElementById("buttonopenform").className = (document.getElementById("buttonopenform").disabled==false ? "buttons but_act comm" : "buttons but_act comm-d");
    }
    if (document.getElementById("buttoncloseform") != null) {
        document.getElementById("buttoncloseform").style.display = (document.getElementById("buttoncloseform").style.display=="none" ? "inline" : "none");
    }
    if (document.getElementById("commentsform") != null) {
        document.getElementById("commentsform").style.display = (document.getElementById("commentsform").style.display=="none" ? "block" : "none");
    }
}

function sendComments(idAgr, comentario) {
     var url = "action=writeComments&idAgrupacion=" + idAgr + "&comentario=" + comentario;

    if (document.getElementById("comsubmit") != null) {
        document.getElementById("comsubmit").disabled = true;
    }
    if (document.getElementById("reset") != null) {
        document.getElementById("reset").disabled = true;
    }
    
    AjaxUploadtoServer("MapBrain", url, "comentarios");
    //setTimeout("getComentarios('" + idAgr + "')",800);
    //;
}

function disabled() {
    if (document.getElementById("submitButton") != null) {
        document.getElementById("submitButton").disabled = true;
        document.getElementById("submitButton").style.backgroundColor = "#E5E5E5";
    }
    if (document.getElementById("whatSearch") != null) {
        document.getElementById("whatSearch").disabled = true;
    }
    if (document.getElementById("closeIbox") != null) {
        document.getElementById("closeIbox").disabled = true;
        document.getElementById("closeIbox").style.backgroundColor = "#E5E5E5";
    }
    if (document.getElementsByName("justone") != null) {
        for (var i = 0; i < document.getElementsByName("justone").length; i++) {
            document.getElementsByName("justone")[i].disabled = true;
        }
    }
}

function enabled() {
    if (document.getElementById("submitButton") != null) {
        document.getElementById("submitButton").disabled = false;
        document.getElementById("submitButton").style.backgroundColor = "transparent";
    }
    if (document.getElementById("whatSearch") != null) {
        document.getElementById("whatSearch").disabled = false;
    }
    if (document.getElementById("closeIbox") != null) {
        document.getElementById("closeIbox").disabled = false;
        document.getElementById("closeIbox").style.backgroundColor = "transparent";
    }
}

function loadIbox() {
    document.getElementById("ibox").style.display = "block";
    document.getElementById("infoIbox").style.top = document.body.scrollTop;
    if (navigator.appName == "Microsoft Internet Explorer") {//&& typeof document.body.style.maxHeight == "undefined"
        document.getElementById("ibox").style.height = screen.height;
        //document.getElementById("ibox").style.width = "99%";
       //document.getElementById("ibox").style.height=document.body.clientHeight;
    }
}

function loadIbox2() {
    document.getElementById("ibox").style.display = "block";
    document.getElementById("ibox").style.backgroundColor = "#FFFFFF";
   // document.getElementById("infoIbox").style.top = document.body.scrollTop;
    if (navigator.appName == "Microsoft Internet Explorer") {//&& typeof document.body.style.maxHeight == "undefined"
        document.getElementById("ibox").style.height = screen.height;
        //document.getElementById("ibox").style.width = "99%";
       //document.getElementById("ibox").style.height=document.body.clientHeight;
    }
}

function unloadIbox() {
    document.getElementById("ibox").style.display = "none";
}


function tocloseIbox() {
    document.getElementById("infoIbox").innerHTML = "";
    unloadIbox();
}

function mustRegistered() {
    alert("Debe logearse para poder crear un mapa, añadir un comentario o realizar una votación.");
}

function votarcomentario(valor, idcomment, orden) {
    var url = "action=voteComment&idComment=" + idcomment + "&voteValue=" + valor;
    
    AjaxUploadtoServer("MapBrain", url, "votacion"+orden);
}

function showTypefilesAndRates (AgrOrPoiID, allowLine) {
    var files = "<div style=\"float: left;\"><span style=\"color:#004696; vertical-align: super;\">Formatos: </span><a href=\"descargarArchivo.jsp?fileType=KML&agrupacionId=" + AgrOrPoiID + "&allowLine=" + allowLine + "\" title=\"Descargar Archivo en formato KML\"><img src=\"img/kml32.png\" alt=\"Descargar KML\"></a>"
    + "&nbsp;<a href=\"descargarArchivo.jsp?fileType=GPX&agrupacionId=" + AgrOrPoiID + "&allowLine=" + allowLine + "\" title=\"Descargar Archivo en formato GPX\"><img src=\"img/gpx32.png\" alt=\"Descargar GPX\"></a>"
    + "</div><div id=\"rateAgr\" style=\"float: right;\"></div><br style=\"clear: both;\" />";

    document.getElementById("filesAndRates").innerHTML = files;
    
    if (allowLine == 1) {
        var url = "action=rateMap&idMap=" + AgrOrPoiID;
        AjaxUploadtoServer("MapBrain", url, "rateAgr");
    }
}

function voteAgrupation (AgrOrPoiID, valor) {
    var url = "action=voteMap&idMap=" + AgrOrPoiID + "&voteValue=" + valor;
    AjaxUploadtoServer("MapBrain", url, "rateAgr");
}

/**
* Delay for a number of milliseconds
*/
function sleep(delay) {
    var start = new Date().getTime();
    while (new Date().getTime() < start + delay);
}

function changeEmail() {
    var url = "action=changeMailForm";
    
    loadIbox();
    AjaxUploadtoServer("MapBrain", url, "infoIbox");
}

function changePass() {
    var url = "action=changePassForm";
    
    loadIbox();
    AjaxUploadtoServer("MapBrain", url, "infoIbox");
}

function changeInfo(tipo) {
    var url = "action=changeUserInfo&tipo=" + tipo;

    if (document.getElementById("newcorreo_e") != null) {
        url += "&email=" + document.getElementById("newcorreo_e").value;
    }
    if (document.getElementById("newcontrasena") != null) {
        url += "&newcontrasena=" + document.getElementById("newcontrasena").value;
    }
    if (document.getElementById("newcontrasena2") != null) {
        url += "&newcontrasena2=" + document.getElementById("newcontrasena2").value;
    }
    if (document.getElementById("oldcontrasena") != null) {
        url += "&oldcontrasena=" + document.getElementById("oldcontrasena").value;
    }

    disabled();
    AjaxUploadtoServer("MapBrain", url,"loadResults");
}

function sendMail() {
    if (trim(document.getElementById("usermail").value) == "") {
        alert ("Introduzca su dirección de correo.")
        return false;
    }
    
    if (trim(document.getElementById("usercomment").value) == "") {
        alert ("Introduzca algún comentario.")
        return false;
    }
    
    var url = "action=contactWithUs&mailUser=" + document.getElementById("usermail").value + "&commentUser=" + document.getElementById("usercomment").value;
    
    AjaxUploadtoServer("MapBrain", url);
    
    document.getElementById("replace_buttons").innerHTML = "Gracias por enviarnos su comentario.";
    
    setTimeout("tocloseIbox()",1500);
}

function trim(cadena) {
    cadena.replace(/^\s*|\s*$/g,"");
    
    return cadena;
}

function helpButton(type) {
    var url = "action=help&type=" + type;
    
    loadIbox();
    AjaxUploadtoServer("MapBrain", url, "infoIbox");
}

function showWait() {
    if (document.getElementById("esta_cargando") != "undefined") {
        document.getElementById("esta_cargando").style.display = (document.getElementById("esta_cargando").style.display=="none" ? "block" : "none");
    }
}

function rememberForm() {
    var url = "action=rememberPassForm";
    
    loadIbox();
    AjaxUploadtoServer("MapBrain", url, "infoIbox");
}

function recordarContrasena() {
    var url = "action=sendForgottenPass&usuario=" + document.getElementById("usuario").value + "&correo_e=" + document.getElementById("correo_e").value;
    
    AjaxUploadtoServer("MapBrain", url,"loadResults");
}