var array_urls = [];
if(document.cookie.indexOf('urls=') < 0){
    array_urls = [];
    //console.log("NON ESISTE, QUINDI LO CREO");
} else {
    //console.log("ESISTE GIA', QUINDI NON LO RICREO, LO RIEMPIO CON I DATI PRESI DAL COOKIE.");
    var urls = readCookie('urls');
    array_urls = urls.split(",");
}


Array.prototype.in_array = function(p_val) {
	for(var i = 0, l = this.length; i < l; i++) {
		if(this[i] == p_val) {
			return true;
		}
	}
	return false;
}

function pushInTheCookie(valueToBePushed, section) {
    /*
    if (!array_urls.in_array(valueToBePushed)) {
        array_urls.unshift(valueToBePushed);
    }
    */

    if ($.inArray(valueToBePushed+"."+section, array_urls) < 0) {
        //console.log("ok, I will push this: " + valueToBePushed);
        array_urls.unshift(valueToBePushed+"."+section);
    } else {
        //console.log(valueToBePushed + " is already there: " + array_urls)
    }

    createCookie("urls", array_urls, 1);
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}


function clientSideInclude(id, url) {
  var req = false;
  // For Safari, Firefox, and other non-MS browsers
  if (window.XMLHttpRequest) {
    try {
      req = new XMLHttpRequest();
    } catch (e) {
      req = false;
    }
  } else if (window.ActiveXObject) {
    // For Internet Explorer on Windows
    try {
      req = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        req = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {
        req = false;
      }
    }
  }
 var element = document.getElementById(id);
 if (!element) {
  alert("Bad id " + id +
   "passed to clientSideInclude." +
   "You need a div or span element " +
   "with this id in your page.");
  return;
 }

  if (req) {
    // Synchronous request, wait till we have it all
    req.open('GET', url, false);
    req.send(null);
    element.innerHTML = req.responseText;
  } else {
    element.innerHTML =
   "Sorry, your browser does not support " +
      "XMLHTTPRequest objects. This page requires " +
      "Internet Explorer 5 or better for Windows, " +
      "or Firefox for any system, or Safari. Other " +
      "compatible browsers may also exist.";
  }
}


function removeFavourite(nFavourite) {
    //console.log("nFavourite: " + nFavourite);
    var nToRemove;
    for (a=0; a < array_urls.length; a++) {
        if (array_urls[a] == nFavourite) {
            nToRemove = a;
            break;
        }
    }

    if (nToRemove >= 0) {
        array_urls.splice(nToRemove, 1);
        //console.log("array_urls: " + array_urls);

        eraseCookie("urls");
        createCookie("urls", array_urls, 1, true);

        clientSideInclude('includeone', '../../favourites.html');
    }
}


$(document).ready(function() {
    clientSideInclude('info_legali', '../../info_legali.html');
    clientSideInclude('sitemap', '../../sitemap.html');
});
