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 = "";

        existingCookieValue = getCookie(name);
        if(existingCookieValue != null) { 
	   value = existingCookieValue + "," + value;
	} 
	document.cookie = name+"="+value+expires+"; path=/";
}

function getCookie(name) {
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  while (i < clen) {
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg) {
      return getCookieVal (j);
      }
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break; 
    }
  return null;
  }

function getCookieVal (offset) {
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1) { endstr = document.cookie.length; }
  return unescape(document.cookie.substring(offset, endstr));
}

function deleteCookie(name) {
var value = "";
var expires = "; expires=Thu, 01-Jan-1970 00:00:01 GMT";
if ( getCookie( name ))
document.cookie = name+"="+value+expires+"; path=/";
}

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

        existingCookieValue = getCookie(name);
        if(existingCookieValue != null) { 
           if(existingCookieValue.indexOf(",") != -1)  { 
            re = /,$/;
            re2 = /^,/;
 	    value = existingCookieValue.replace(eliminateid,"").replace(re,"").replace(",,",",").replace(re2,"");
    	    document.cookie = name+"="+value+expires+"; path=/";
           } else { 
	    // No comma, just one value expire cookie to delete everything
            value = existingCookieValue.replace(eliminateid,"");
    	    deleteCookie(name);
           }
        }
}


function saveMapId(id) { 
    if(id == null || id == 0){
        alert("Cannot save map by that id")
    } else { 
        // Guardar cookie 
	createCookie("mappoints",id,365);
    }
}

function deleteMapId(id) { 
    if (confirm("Are you sure you want to delete this map point?")) { 
        // Guardar cookie 
         if(id == null || id == 0){
           alert("Cannot delete map by that id")
         } else { 
      	  modifyCookie("mappoints",id,365);
          updateMapPoints();
        }
    } else {
        return false;
    }
}

function updateMapPoints() { 
 // Updated section with maps
 pars='date=TBD';
 // Call the server, and get the updated list
 new Ajax.Request('/maps/updatemappoints', {
   method: 'get',
   parameters: pars, 
   onSuccess: function(transport){
      var response = transport.responseText || "no response text";
      document.getElementById("mappoints").innerHTML=response;
   },
   onFailure: function(){ } //alert('Something went wrong...')
  });
}


function CreateBookmarkLink() {

 title = "Mashup Soft Maps"; 
  // Blogger - Replace with <$BlogItemTitle$> 
  // MovableType - Replace with <$MTEntryTitle$>

 url = "http://www.mashupsoft.com/maps/";

	if (window.sidebar) { // Mozilla Firefox Bookmark
		window.sidebar.addPanel(title, url,"");
	} else if( window.external ) { // IE Favorite
		window.external.AddFavorite( url, title); }
	else if(window.opera && window.print) { // Opera Hotlist
		return true; }
}

