﻿function $d(i, d) { return document.getElementById(i).style.display = d; }
function $(i) { return document.getElementById(i); }

var selectedLimit = 50;
function propSelect(c, i, t) {
    propSelect(c, i, t, 'favourite');
}
function propSelect(c, i, t, base) {
    if (c.className == "pselect") {
        if (countSelected() >= selectedLimit) {
            alert('Your list already contains ' + selectedLimit + ' properties. Remove any to be able to add new one.');
            return;
        }
        c.className = "punselect";
        setText(c, "<img src=\"/images/v2/" + base + "-remove.jpg\" />");
        saveSelectedProperty(i, t);
    }
    else {
        c.className = "pselect";
        setText(c, "<img src=\"/images/v2/" + base + "-add.jpg\" />");
        removeSelectedProperty(i);
    }
    setSelectedCount();
}

function setText(c, t) {
    if (c != null) {
        if (document.all) {
            c.innerHTML = t;
        } else {
            c.innerHTML = t;
        }
    }    
}

function saveSelectedProperty(i, t) {
    var propertyIds = getCookie("pids");
    var propertyInTheList = false;

    if (propertyIds != null) {
        var mySplitResult = propertyIds.split("#");

        for (j = 0; j < mySplitResult.length; j++) {
            if (parseInt(mySplitResult[j]) == i) {
                propertyInTheList = true;
                break;
            }
        }
    }

    if (propertyInTheList == false) {
        propertyIds = propertyIds.concat(i.toString(), "$", t.toString(), "#");
        setCookie("pids", propertyIds, 30);
    }
}

function removeSelectedProperty(i) {
    var propertyIds = getCookie("pids");
    var newValue = "";

    if (propertyIds != null) {
        var mySplitResult = propertyIds.split("#");

        for (j = 0; j < mySplitResult.length; j++) {
            var splititem = mySplitResult[j].split("$");
            var idFromList = parseInt(splititem[0]);
            if (!isNaN(idFromList) && (idFromList != i)) {
                newValue = newValue.concat(mySplitResult[j].toString(), "#");
            }
        }
    }
    setCookie("pids", newValue, 30);
}

function setCookie(c_name, value, expiredays) {
    var today = new Date();
    today.setTime(today.getTime());
    expiredays = expiredays * 1000 * 60 * 60 * 24;
    var expires_date = new Date(today.getTime() + (expiredays));

    document.cookie = c_name + "=" + escape(value) + ((expiredays == null) ? "" : ";expires=" + expires_date.toGMTString()) + ";path=/";
}

function getCookie(c_name) {
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(c_name + "=");
        if (c_start != -1) {
            c_start = c_start + c_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 "";
}

function setSelectedCount() {
    var propertyCount = countSelected();
    setText(document.getElementById("selected-count"), propertyCount);
}

function countSelected() {
    var propertyIds = getCookie("pids");
    var propertyCount = 0;

    if (propertyIds != null) {
        var mySplitResult = propertyIds.split("#");

        for (j = 0; j < mySplitResult.length; j++) {
            var idFromList = parseInt(mySplitResult[j]);
            if (!isNaN(idFromList)) {
                propertyCount = propertyCount + 1;
            }
        }
    }
    return propertyCount;
}
