// =========================================================================
//                          Funciones de la Cookie 
// =========================================================================

var poisCookie = "listaPois";

//Runs on body load to check history of checkboxes on the page.
function comprobarChecks()
{	
	var currentCookie = getCookie(poisCookie);
	var inputs = document.getElementsByTagName("input");
	for (i=0; i<inputs.length; i++)
	{		
		if ( inputs[i].type == "checkbox" && inputs[i].id.indexOf("ckbox")==0 )
		{			
			var poiId = inputs[i].id.replace("ckbox","");
			inputs[i].onclick = function() {clickOnCheckBox(this.id ,this.id.replace("ckbox",""));};
            if (currentCookie && currentCookie.indexOf(poiId) > -1)	{
				inputs[i].checked = true;
				var poidiv = document.getElementById("poi-box-"+poiId);
				if (poidiv != null) {
					poidiv.style.backgroundImage='url(/tmb-rutes-portlet/images/fons_poi_box_sel.gif)';
				}
				
			} else {
				var poidiv = document.getElementById("poi-box-"+poiId);
				if (poidiv != null) {
					poidiv.style.backgroundImage='url(/tmb-rutes-portlet/images/fons_poi_box_blanc.gif)';
				}
			}
		}
	}
}


function clickOnCheckBox(idCheckBox, idPoi) {

	var check = document.getElementById(idCheckBox);
	var selectedPois = getCookie(poisCookie);
    if (check.checked) {
        // set a cookie to keep the checkbox checked
    	if (selectedPois == null) {
        	setCookie(poisCookie, idPoi);
        } else if (selectedPois.indexOf(idPoi) == -1) {
            setCookie(poisCookie,selectedPois+","+idPoi);
        }
        var poidiv = document.getElementById("poi-box-"+idPoi);
		if (poidiv != null) {
			poidiv.style.backgroundImage='url(/tmb-rutes-portlet/images/fons_poi_box_sel.gif)';
		}
    } else {        
        // remove it from the checked cookie list        
        if (selectedPois.indexOf(idPoi) > -1) {
           // check for comma after menu
           if (selectedPois.indexOf(idPoi+",") > -1) {
               selectedPois = selectedPois.replace(idPoi+",","");
           } else {
               selectedPois = selectedPois.replace(idPoi,"");
           }
           if (selectedPois == "") {
               deleteCookie(poisCookie);
           } else {
               setCookie(poisCookie,selectedPois);
           }
        }
        var poidiv = document.getElementById("poi-box-"+idPoi);
		if (poidiv != null) {
			poidiv.style.backgroundImage='url(/tmb-rutes-portlet/images/fons_poi_box_blanc.gif)';
		}
    }
    
}
	
/* This function is used to set cookies */
function setCookie(name,value,expires,path,domain,secure) {	
  	document.cookie = name + "=" + escape (value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") + ((secure) ? "; secure" : "");
}

/* This function is used to get cookies */
function getCookie(name) {
	var prefix = name + "="; 
	var start = document.cookie.indexOf(prefix); 

	if (start==-1) {
		return null;
	}
	
	var end = document.cookie.indexOf(";", start+prefix.length) ;
	if (end==-1) {
		end=document.cookie.length;
	}

	var value=document.cookie.substring(start+prefix.length, end);
	return unescape(value);
}

/* This function is used to delete cookies */
function deleteCookie(name,path,domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

function ShowPopUp(html) {
	var popup = Liferay.Popup({
		modal: true,
		noDraggable: true,
		noTitleBar: true,
		message: html,
		messageId: 'poi_pop',
		width:590					
	});
}

function ShowImagePopUp(html, ancho) {
	var mypopup = Liferay.Popup({
		modal: true,
		noDraggable: true,
		noTitleBar: true,
		message: html,
		messageId: 'poi_pop',
		width: ancho
	});
}


