function cookieDate(){
		var today = new Date();
		var zero_date = new Date(0,0,0);
		today.setTime(today.getTime() - zero_date.getTime());
		var cookie_expire_date = new Date(today.getTime() + (8 * 7 * 86400000)); 
		return cookie_expire_date;
}
/**
 * Sets a Cookie with the given name and value.
 *
 * name       Name of the cookie
 * value      Value of the cookie
 * [expires]  Expiration date of the cookie (default: end of current session)
 * [path]     Path where the cookie is valid (default: path of calling document)
 * [domain]   Domain where the cookie is valid
 *              (default: domain of calling document)
 * [secure]   Boolean value indicating if the cookie transmission requires a
 *              secure transmission
 */
function setCookie(name, value, expires, path, domain, secure)
{
    document.cookie= name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

/**
 * Gets the value of the specified cookie.
 *
 * name  Name of the desired cookie.
 *
 * Returns a string containing value of specified cookie,
 *   or null if cookie does not exist.
 */
function getCookie(name)
{
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1)
    {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    }
    else
    {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
    {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

/**
 * Deletes the specified cookie.
 *
 * name      name of the cookie
 * [path]    path of the cookie (must be same as path used to create cookie)
 * [domain]  domain of the cookie (must be same as domain used to create cookie)
 */
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 changeLink(data,iid,cid,gid,domName){
	//console.info("Data Loaded: " + data);
	if(data == "cookieSet"){
		$('#smItinerary'+cid+"-"+gid).html('<img src="/common/images/icons/list-remove.gif" alt="Remove" style="border:0px" align="absmiddle" />Remove');
		$('#smItinerary'+cid+"-"+gid).attr('href',"javascript:itineraryCookie('remove',"+cid+","+gid+",'"+domName+"');");
		
	}else if(data == "itemRemove"){
		$('#smItinerary'+cid+"-"+gid).html('<img src="/common/images/icons/list-add.gif" alt="Add to itinerary" style="border:0px" align="absmiddle" />Add&nbsp;to&nbsp;Itinerary');
		$('#smItinerary'+cid+"-"+gid).attr('href',"javascript:itineraryCookie('set',"+cid+","+gid+",'"+domName+"');");
		//console.info("I just removed something from the itinerary");
	}else if(data == "cookieRemove"){
		//does the cookie exist?
		if(getCookie('spItinerary') == null){
			//do nothing
		}else{
			
			$('#smItinerary'+cid+"-"+gid).html('<img src="/common/images/icons/list-add.gif" alt="Add to itinerary" style="border:0px" align="absmiddle" />Add&nbsp;to&nbsp;Itinerary');
			$('#smItinerary'+cid+"-"+gid).attr('href',"javascript:itineraryCookie('set',"+cid+","+gid+",'"+domName+"');");
			var name = 'spItinerary';
			var path = "/";
			var domain = "<?=$_SERVER['SERVER_NAME']?>";
			deleteCookie(name, path, domain);
			//console.info("I should have deleted the cookie because there's nothing left on the itinerary");
		}
	}else{
		//console.info(data);	
	}
}
function itineraryCookie(action,catId,geoId,domName){

	if (action == 'set'){
		if (getCookie('spItinerary')==null){
			//set up the cookie variables
			var name = 'spItinerary';
			var value = Math.floor(Math.random()*10000001);
			var expires = cookieDate();
			var path = "/";
			var domain = domName;
			var secure = "";
			//assign the cookie variables to the cookie
			setCookie(name, value, expires, path, domain, secure);
			//post the information back to the database
			$.post('/poi/php/itinerary.php','action=set&iid='+value+'&cid='+catId+'&gid='+geoId+'',function(data){
				changeLink(data,value,catId,geoId,domName);
				});
		}else{
			var itineraryId = getCookie('spItinerary');
			$.post('/poi/php/itinerary.php','action=set&iid='+itineraryId+'&cid='+catId+'&gid='+geoId+'',function(data){
				changeLink(data,itineraryId,catId,geoId,domName);
				});
		}
	}else if(action == 'remove'){
		var itineraryId = getCookie('spItinerary');
		$.post('/poi/php/itinerary.php','action=remove&iid='+itineraryId+'&cid='+catId+'&gid='+geoId+'',function(data){				
		changeLink(data,itineraryId,catId,geoId,domName);
		});
	
	}else if(action == 'destroy'){
		var itineraryId = getCookie('spItinerary');
		$.post('/poi/php/itinerary.php','action=destroy&iid='+itineraryId+'&cid='+catId+'&gid='+geoId+'',function(data){				
		//changeLink(data,itineraryId,catId,geoId,domName);
		});
	
	}
} 
