// this function gets the cookie, if it exists
function Get_Cookie( name ) {
	
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) )
	{
		return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ";", len );
	if ( end == -1 ) end = document.cookie.length;
	
	return unescape( document.cookie.substring( len, end ) );
}
/*
only the first 2 parameters are required, the cookie name, the cookie
value. Cookie time is in milliseconds, so the below expires will make the 
number you pass in the Set_Cookie function call the number of days the cookie
lasts, if you want it to be hours or minutes, just get rid of 24 and 60.

Generally you don't need to worry about domain, path or secure for most applications
so unless you need that, leave those parameters blank in the function call.
*/
function Set_Cookie( name, value, expires, path, domain, secure ) {

// Debug variable (set to 1 to debug)
var debug = 0;

	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );
	
	// if the expires variable is set, make the correct expires time, the
	// current script below will set it for x number of days, to make it
	// for hours, delete * 24, for minutes, delete * 60 * 24
	if ( expires ) {
		expires = expires * 1000 * 60 * 60 * 24; 
	}
	//alert( 'today ' + today.toGMTString() );// this is for testing purpose only
	var expires_date = new Date( today.getTime() + (expires) );

if (debug)
	alert('name: ' + name + '\n' +
	      'value: ' + value + '\n' +
	      'expires: ' + expires_date.toGMTString());// this is for testing purposes only
	
	var cookieStr = name + "=" +escape( value ) +
		( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + //expires.toGMTString()
		( ( path ) ? ";path=" + path : "" ) + 
		( ( domain ) ? ";domain=" + domain : "" ) +
		( ( secure ) ? ";secure" : "" );
	document.cookie = cookieStr;
}
// this deletes the cookie when called
// In IE when getting cookie, it will return null
// In FF when getting cookie, it will return "" (empty string)
function Delete_Cookie( name, path, domain ) {
	if ( Get_Cookie( name ) ) document.cookie = name + "=" +
			( ( path ) ? ";path=" + path : "") +
			( ( domain ) ? ";domain=" + domain : "" ) +
			";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}
    //======================================================================//
    //=== Create default cookies                                         ===//
    // Cookies created included default map center coordinates and zoom level //
	//======================================================================//
	function newCookie() {
		
		// stores layers, zoom, center for motorists tab

        //*** Brian testing .. modified zoom level from 9 to 10 as the map is bigger now, and changed center point //
		//Set_Cookie( 'motoristsTab', 'visible;visible;9;(49.227463310793176, -122.882080078125);checked;checked;checked;checked;;;;;;;;;;;', 30, '/', '', '' );

		Set_Cookie( 'motoristsTab', 'visible;visible;10;(49.187463310793176, -122.782080078125);checked;checked;checked;checked;;;;;;;;;;;', 30, '/', '', '' );
		// stores layers, zoom, center for transit tab
		//Set_Cookie( 'transitTab', 'visible;visible;9;(49.227463310793176, -122.882080078125);;;;;;;;;;checked;checked;checked;;;', 30, '/', '', '' );
	}
	
	//======================================================================//
	//=== Read an existing cookie                                        ===//
	//======================================================================//
	function retrieveCookie() {
	    
		activeTabMapSettings=Get_Cookie( 'motoristsTab' );
		
		if (activeTabMapSettings != null)
		{
		    activeTab_arr = activeTabMapSettings.split( ";" ); // break settings into an array
		    ck_zoom=activeTab_arr[2]; // set preferred zoom
		    ck_center=activeTab_arr[3]; // set map center
		}
		
	}
	
	//======================================================================//
	//=== Update an existing cookie only if control is not disabled      ===//
	//======================================================================//
	function updateCookie(control, tab ) {
	    
	    alert(document.getElementsById(control.Id).disabled);
	    if (document.getElementsById(control.Id).disabled == "enabled")
	    {
	        updateCookie( tab );
	    }
	}
	
	//======================================================================//
	//=== Update an existing cookie                                      ===//
	//======================================================================//
	function updateCookie( t ) {
		
		activeTab = t;
		Set_Cookie( defaultTabCookieName, GetCookie(activeTabCookieName), cookieExpDays, '/', '', '' );
		
	}


//*******************************************************************************
// Function:     AddOrRemovePartialCookieValue
// Author:       michael.chu@cgi.com
// Project:      iMove Build - 0.8
// Date:         Nov 16, 2006
//
// Description:  The following function adds or removes a partial cookie value
//               from the cookie based on the input parameters.
//
// Parameters:   cookieName - name of the cookie
//               partialCookieValue - partial value of the cookie
//               toAdd - true to add partial cookie value to cookie
// Returns:      
// Calls:        
// Called By:    
//
// Revision History:
//
//   Name        Date        Description
//  ------       ------      ------------------------------------------------
//
//*******************************************************************************    
function AddOrRemovePartialCookieValue(cookieName, partialCookieValue, toAdd) {

    // Retrieve cookie value
    var cookieValue = Get_Cookie( cookieName );

    // If it is empty, then create a new cookie
    if (cookieValue == null)
    {
        // Add the new cookie with this partial cookie value
        if (toAdd)
            Set_Cookie( cookieName, partialCookieValue + ',', 30, '/', '', '' );
        else
            ; // leave cookie as null
	}
    else
    {
        // If cookie is not empty, then we need to check if this route id exists
        var partialCookieArr = cookieValue.split( "," );
        var found = false;
        
        for( var i=0; i<partialCookieArr.length; i++ ) 
        {
            if (partialCookieArr[i] == partialCookieValue)
                found = true;
        }
        
        var newCookieValue = "";
        if (found == true && toAdd == true)
        {
            // It has been added already
            ;
        }
        else if (found == true && toAdd == false)
        {
            // Remove partial cookie value
            for( var i=0; i<partialCookieArr.length; i++ ) 
            {
                if (partialCookieArr[i] == partialCookieValue)
                    partialCookieArr[i] = "";
            }
            
            for( var i=0; i<partialCookieArr.length; i++ ) 
            {
                if (partialCookieArr[i] != "")
                    newCookieValue += partialCookieArr[i] + ",";
            }
            
            Set_Cookie( cookieName, newCookieValue, 30, '/', '', '' );
        }
        else if (found == false && toAdd == true)
        {
            // Add route id
            newCookieValue += cookieValue + partialCookieValue + ",";
            
            Set_Cookie( cookieName, newCookieValue, 30, '/', '', '' );
        }
        else if (found == false && toAdd == false)
        {
            // It doesn't need to be added
            ;
        }
    }  
}


//new testing functions

function eraseCookie(name)
{
  createCookie(name, "", -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=/";
}



//Same function as above, but cookie will expire in 14 days instead of 30
function AddOrRemoveFourteenDayPartialCookieValue(cookieName, partialCookieValue, toAdd) {

    // Retrieve cookie value
    var cookieValue = Get_Cookie( cookieName );

    // If it is empty, then create a new cookie
    if (cookieValue == null)
    {
        // Add the new cookie with this partial cookie value
        if (toAdd)
            Set_Cookie( cookieName, partialCookieValue + ',', 14, '/', '', '' );
        else
            ; // leave cookie as null
	}
    else
    {
        // If cookie is not empty, then we need to check if this route id exists
        var partialCookieArr = cookieValue.split( "," );
        var found = false;
        
        for( var i=0; i<partialCookieArr.length; i++ ) 
        {
            if (partialCookieArr[i] == partialCookieValue)
                found = true;
        }
        
        var newCookieValue = "";
        if (found == true && toAdd == true)
        {
            // It has been added already
            ;
        }
        else if (found == true && toAdd == false)
        {
            // Remove partial cookie value
            for( var i=0; i<partialCookieArr.length; i++ ) 
            {
                if (partialCookieArr[i] == partialCookieValue)
                    partialCookieArr[i] = "";
            }
            
            for( var i=0; i<partialCookieArr.length; i++ ) 
            {
                if (partialCookieArr[i] != "")
                    newCookieValue += partialCookieArr[i] + ",";
            }
            
            Set_Cookie( cookieName, newCookieValue, 14, '/', '', '' );
        }
        else if (found == false && toAdd == true)
        {
            // Add route id
            newCookieValue += cookieValue + partialCookieValue + ",";
            
            Set_Cookie( cookieName, newCookieValue, 14, '/', '', '' );
        }
        else if (found == false && toAdd == false)
        {
            // It doesn't need to be added
            ;
        }
    }  
}


