﻿var propertySelectionCookieName = "propertySelection";

function getPropertySelectionCookieValue() {
    if (document.cookie.length > 0) {
        var start = document.cookie.indexOf(propertySelectionCookieName + "=");
        if (start > -1) {
            start = start + propertySelectionCookieName.length + 1;
            var end = document.cookie.indexOf(";", start);
            if (end == -1) end = document.cookie.length;
            return decodeURIComponent(document.cookie.substring(start, end));
        }
    }
    return "";
}

function setPropertySelectionCookieValue(newValue) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + 365);
    document.cookie = propertySelectionCookieName + "=" + encodeURIComponent(newValue) + ";expires=" + exdate.toGMTString() + ";path=/";
}

function togglePropertySelection(propertyID) {
    var cookieValue = getPropertySelectionCookieValue();
    var found = false;
    var newCookieValue = "";
    if (cookieValue != null) {
        var values = cookieValue.split("|");
        if (values != null) {
            for (var i = 0; i < values.length; i++) {
                if (values[i] == propertyID) {
                    found = true;
                }
                else {
                    if (newCookieValue.length > 0) newCookieValue = newCookieValue.concat("|");
                    newCookieValue = newCookieValue.concat(values[i]);
                }
            }
        }
    }
    if (!found) {
        if (newCookieValue.length > 0) newCookieValue = newCookieValue.concat("|");
        newCookieValue = newCookieValue.concat(propertyID);
    }
    setPropertySelectionCookieValue(newCookieValue);
}

function isInPropertySelection(propertyID) {
    var cookieValue = getPropertySelectionCookieValue();
    var found = false;
    var newCookieValue = "";
    if (cookieValue != null) {
        var values = cookieValue.split("|");
        if (values != null) {
            for (var i = 0; i < values.length; i++) {
                if (values[i] == propertyID) return true;
            }
        }
    }
    return false;
}

function updatePropertySelectionIcon(icon, propertyID, language) {
    var addText;
    var removeText;
    switch (language) {
        case "de":
            addText = "Zur Auswahl hinzufügen";
            removeText = "Aus Auswahl entfernen";
            break;
        case "es":
            addText = "Añadir a selección";
            removeText = "Quitar de selección";
            break;
        default:
            addText = "Add to selection";
            removeText = "Remove from selection";
            break;
    }
    if (isInPropertySelection(propertyID)) {
        icon.src = "/i/img/remove.gif";
        icon.title = removeText;
    }
    else {
        icon.src = "/i/img/add.gif";
        icon.title = addText;
    }
    icon.style.display = "block";
}

function updatePropertySelectionInfo() {
    var cookieValue = getPropertySelectionCookieValue();
    var viewLink = document.getElementById("propertySelectionInfoViewLink");
    var c = 0;
    var found = false;
    if (cookieValue != null) {
        var values = cookieValue.split("|");
        for (var i = 0; i < values.length; i++) {
            if (values[i] != null && values[i].length > 0) c++;
        }
    }
    if (c == 1) {
        document.getElementById("propertySelectionMultiplePropertiesText").style.display = "none";
        document.getElementById("propertySelectionOnePropertyText").style.display = "block";
        //document.getElementById("propertySelectionCount").innerHTML = c.toString();
        //viewLink.innerHTML = "1 property in your selection";
    }
    else {
        document.getElementById("propertySelectionMultiplePropertiesText").style.display = "block";
        document.getElementById("propertySelectionOnePropertyText").style.display = "none";
        document.getElementById("propertySelectionCount").innerHTML = c.toString();
        //viewLink.innerHTML = c.toString().concat(" properties in your selection");
    }
    if (c > 0) {
        document.getElementById("propertySelectionInfo").style.display = "block";
    } else {
        document.getElementById("propertySelectionInfo").style.display = "none";
    }
}

function clearPropertySelection(reload) {
    setPropertySelectionCookieValue('');
    if (reload) window.location.reload();
}
