/* Create a cookie and set its value */
function setCookie(name, value, expires) {
	//alert(name + "=" + value + "; path=/" + 
      //  ((expires == null) ? "" : "; expires=" + expires.toGMTString()));
        
    document.cookie = name + "=" + value + "; path=/" + 
        ((expires == null) ? "" : "; expires=" + expires.toGMTString());
        
}


/* Retrieve a cookie's value */
function getCookie(name) {
    var cookiename = name + "=";
    var dc = document.cookie;
    var begin, end;
    	
   
	 //document.write(dc);
    if (dc.length > 0)
	 {
        begin = dc.indexOf("; "+cookiename);
		 
		  
		  // Still could be there, but maybe at front
		  if (begin == -1 && dc.indexOf(cookiename) == 0 )
		  	 begin = dc.indexOf(cookiename);
		  else if (begin != -1)
		  	 begin += 2;
		  else
		  	 return null;
		  
        if (begin != -1)
		  {	  
			  begin += cookiename.length;
            end = dc.indexOf(";", begin);
            if (end == -1) {
                end = dc.length;
            }
				return dc.substring(begin, end);
        }
    }
    return null;
}


/* Delete a cookie */
function deleteCookie(name) {
    document.cookie = name + "=; expires=Thu, 01-Jan-70 00:00:01 GMT" +  
        "; path=/";
}


/* Set expiration dates */
function dater (num_days) {
	var expdate = new Date(); 
	expdate.setTime(expdate.getTime() + (1000 * 60 * 60 * 24 * num_days));
	return expdate;
}


/* Set crumb value in cookie */
function setCrumb(cookie, name, value, expiry) 
{    
	var cookievalue = getCookie(cookie);
	
	var crumbvalue;
	var crumbname = name + '=';
	
	// If we have match somewhere in the cookie, it may be a false partial match
	// and thus not really a match. Therefore unless our name is right at the front
	// of the cookie, make sure there is a colon before it
	
	// If we get a natch with the colon in front, we are guarenteed
	crumbvalue = getCrumb(cookie, ":"+name);  
	
	if (crumbvalue == null)
	{
		crumbvalue = getCrumb(cookie, name); 
		
		// May be a false alarm unless found right at beginning of cookie
		if (crumbvalue != null && cookievalue.indexOf(crumbname) > 0)
			crumbvalue = null;
	}
	else
		crumbname = ":" + name + '=';
	
	
	if (crumbvalue != null)
	{
		var start = cookievalue.indexOf(crumbname);
			if (start != -1) {            
				var end = cookievalue.indexOf(':', start);
            setCookie(cookie, 
					 cookievalue.substring(0, start) + crumbname 
					 + value + ':' 
					 + cookievalue.substring(end + 1, cookievalue.length),
					 expiry);       
			}
	}    
	else 
	{
        if (cookievalue != null) 
        {
            cookievalue += crumbname + value + ':';      
		}        
		else
		{
        	cookievalue = crumbname + value + ':';     
		}
		
		setCookie(cookie, cookievalue, expiry);    
	}
}


/* Get crumb value from cookie */
function getCrumb(cookie, name) {
	var crumbname = name + '=';    
	var cookievalue = getCookie(cookie);
//alert(null);
	if (cookievalue != null) {
		var start = cookievalue.indexOf(crumbname);       
			
		if (start != -1) {
			start += crumbname.length;
         var end = cookievalue.indexOf(':', start);
         
         if (end != -1) 
				 {
         	return cookievalue.substring(start, end);     
				}
      }    
	}    
	return null;
}

// This will take each element(crumb) of the cookie and return an associative array
function cookieToArray(cookie)
{
	var return_array = new Array();
	var cookievalue = getCookie(cookie);
	
	var start = 0; 
	var end;
	var crumb_value_start; 
	var crumb_value_end;
	var crumb_name;
	var crumb_value;
	var ctr=0;
    
	while (start != cookievalue.length)
	{ 
		 end =  cookievalue.indexOf('=', start);
		 crumb_value_start = end+1;  // Bypass the = sign
		 crumb_value_end = cookievalue.indexOf(':', crumb_value_start);
		
		 crumb_name = unescape(cookievalue.substring(start, end));
		 
		 // This may be a null answer
		 if (crumb_value_start == crumb_value_end)
		 	crumb_value= "";
		 else
		 	crumb_value = unescape(cookievalue.substring(crumb_value_start, crumb_value_end));
		
		//alert ("start "+start+", length: "+cookievalue.length+"-"+crumb_name+"("+start+", "+end+")="+crumb_value+"("+crumb_value_start+", "+crumb_value_end+")");
		
		return_array[crumb_name] = crumb_value;
		
		// Increment start
		start = crumb_value_end+1;
		ctr++;
	}
	return return_array;
}

// This will take each element(crumb) of the cookie and return an associative array
function cookieToSpecialArray(cookie, prefix)
{
	var return_array = new Array();
	var cookievalue = getCookie(cookie);
	
	var start = 0; 
	var end;
	var crumb_value_start; 
	var crumb_value_end;
	var crumb_name;
	var crumb_value;
	var ctr=0;
    
	if (cookievalue != null)
	{
		while (start != cookievalue.length)
		{ 
			 end =  cookievalue.indexOf('=', start);
			 crumb_value_start = end+1;  // Bypass the = sign
			 crumb_value_end = cookievalue.indexOf(':', crumb_value_start);
			
			 crumb_name = unescape(cookievalue.substring(start, end));
			 
			 // This may be a null answer
			 if (crumb_value_start == crumb_value_end)
			 	crumb_value= "";
			 else
			 	crumb_value = unescape(cookievalue.substring(crumb_value_start, crumb_value_end));
			
			//alert ("start "+start+", length: "+cookievalue.length+"-"+crumb_name+"("+start+", "+end+")="+crumb_value+"("+crumb_value_start+", "+crumb_value_end+")");
			
			// Do a quick check
			if ( (crumb_name != prefix+'c' && crumb_name != prefix+"_last") && crumb_name.indexOf(prefix) != -1 )
			{	
				var pg_id = crumb_name.substr(1, crumb_name.length);
				var pg_count = crumb_value;
	
				return_array[ctr] = new Array();
				return_array[ctr]["user_id"] = 1;
		   		return_array[ctr]["page_id"] = pg_id;
		   		return_array[ctr]["count"] = pg_count;
		   		
			}
					
			// Increment start
			start = crumb_value_end+1;
			ctr++;
		}
	}
	return return_array;
}

/* Delete crumb from cookie */
function deleteCrumb(cookie, name, expiry) {
	var cookievalue = getCookie(cookie);
	var crumbvalue = getCrumb(cookie, name);     
	var crumbname = name + '=';
	
	if (crumbvalue != null) {
      var start = cookievalue.indexOf(crumbname); 
      var end = cookievalue.indexOf(':', start);        
		setCookie(cookie, 
         cookievalue.substring(0, start) + 
         cookievalue.substring(end + 1, cookievalue.length),
         expiry);    
	}
}