<!--

// Enable (true) or disable (false) the cookie persistence behaviour
//
var	enableCookiePersist = true;

//	readcookies==true -> update the location url with information from the cookie
//	readCookies==false -> just update cookie info without changing the url.
//	Note that url changes occur in retrieveCookie().
var 	readCookies = true;

function ie3split(mystring, delimiter) {
/*

   Purpose: Substitute for Javascript's split() function which is not supported by ie3.
   Input:
         mystring - the string to be split
         delimiter - the character to be delimited.
   Output:
         pairs - an array containing each section separated by the delimiter.

*/

   var pairs = new Object();
   var arrayidx = 1;
   var tempstring = '';

   pairs[0] = 0;

   for (var i = 0; i < mystring.length; i++) {
      if (mystring.charAt(i) != delimiter) {
         tempstring += mystring.charAt(i);
      } else {
         pairs[arrayidx++] = tempstring;
         tempstring = '';
      }
   }

   pairs[arrayidx++] = tempstring;
   pairs[0] = arrayidx;

   return (pairs);
}

function parameterExists(nmvalpairs, szMatch) {
/*

   Purpose: determine whether a name/value pair exists in the URL.
   Input:
      nmvalpairs - string containing the name/value pairs separated by '&'
      szMatch - the name of the variable to match

   Output:
      value - the value associated with the variable that matches szMatch
*/
//   var pairs = nmvalpairs.split("&");

   var pairs = new ie3split(nmvalpairs, '&');

   for (var i = 1; i < pairs[0]; i++) 
   {
      var pos = pairs[i].indexOf('=');
      if (pos == -1) continue;

      var argname = pairs[i].substring(0,pos);
      var value = pairs[i].substring(pos + 1);

      if (argname == szMatch) 
	{
         if (value.length == 0) 
	   {
            return '';          // this should be changed
         } 
	   else 
	   {
            return value;
         }
      }
   }

   return '';
}

function InsertInPlace( szHref, szValue, iIdx )
{
   var  szLeft  =  new String();
   var  szRight =	 new String();

   szLeft = szHref . substring(0, iIdx);
   szRight= szHref . substring( iIdx, szHref . length);

   szHref = szHref + escape(szValue) + szRight;

   return szHref;
}


/*

   Purpose: to retrieve the cookie value stored on the user machine and add it to the URL.

   Input: none
   Output: parameter string containing the values from the cookies

*/
function retrieveCookie() 
{
   var szReturn = '';
   var szHref   = window.location.href;

   //  way to read cookies only at beginning of MC session; avoids problem of reloading pages, trapping users unnecessarily
   var timestamp = GetCookieValue("nextread=");
   if( timestamp.length == 0)
	timestamp = 0;
   var theDate = new Date();
   var now = theDate.getTime();
   if( parseInt(now) < parseInt(timestamp) ){
	readCookies = false;
	return szReturn ;
   }	else{
	readCookies = true;
   }

   var isFrom   = false;
   var isTo     = false;

   // test to see if the cookie values exists
   //
   var destURLAddr2 = parameterExists(szHref, 'tocity');
   var destURLAddr3 = parameterExists(szHref, 'tostate');
   var origURLAddr2 = parameterExists(szHref, 'fromcity');
   var origURLAddr3 = parameterExists(szHref, 'fromstate');
   var visitorURLName = parameterExists(szHref, 'visitorname');

   if ( origURLAddr2.length == 0 )
   {
	var	fromCity;
	var	iIdx	= szHref . indexOf( "fromcity=" );

      	fromCity    = GetCookieValue( "fromcity=" );

	isFrom  	= ( fromCity . length != 0 );

	if ( fromCity . length != 0 && iIdx  == -1 )
	{
         szReturn  += '&fromcity=';
	   szReturn  += escape(fromCity);
	}
	else
	{
	   szHref = InsertInPlace( szHref, fromCity, iIdx + 9 );
	}
   }

   if ( destURLAddr2.length == 0 )
   {
	var 	toCity;
  	var	iIdx	=  szHref . indexOf( "tocity=" );

      toCity  	= GetCookieValue("tocity=");

	isTo		= ( toCity . length != 0 );

	if ( toCity . length != 0 && iIdx == -1)
	{
         szReturn  += '&tocity=';
	   szReturn  += escape(toCity);
	}
	else
	{
	   szHref = InsertInPlace( szHref, toCity, iIdx + 7 );
	}
   }

   if ( origURLAddr3.length == 0 )
   {
	var	fromState;

	fromState  	= GetCookieValue( "fromstate=" );

	if ( fromState . length != 0 && isFrom == true )
	{
	   szReturn  += '&fromstate=';
	   szReturn  += escape(fromState);
	}
   }

   if ( destURLAddr3.length == 0 ) 
   {
	var	toState;

	toState  	= GetCookieValue("tostate=");

	if ( toState . length != 0 && isTo == true )
	{
	   szReturn  += '&tostate=';
	   szReturn  += escape(toState);
	}
   }

   if ( visitorURLName.length == 0 ) 
   {
	var	visitorName;

	visitorName  	= GetCookieValue("visitorname=");

	if ( visitorName . length != 0 )
	{
	   szReturn  += '&visitorname=';
	   szReturn  += escape(visitorName);
	}
   }

   return szReturn;
}

function GetCookieValue( variablename )
{
   var szValue = new String;
   var idx;
   var start, end;

   var allcookies = document.cookie;

   if ( (idx = allcookies.indexOf(variablename)) != -1) 
   {
      start = idx + variablename.length;
   
      end = allcookies.indexOf(";", start);

      if (end == -1) end = allcookies.length;

      szValue = unescape(allcookies.substring(start, end));

	return szValue;
   }

   return '';
}

function RunMeFirst() 
{

   // test to see if the client accept cookies.
   if (!AcceptCookie()) 
   {
      // the following should be a redirection.
      //
      window.location.href="mc.dll?page=home&file=require_cookies.htm";
      return false;
   }

   var vHref = window.location.href;

   if ( vHref . indexOf( 'mc.dll' ) == -1 )
   {
	return false;
   }

   // check for cookie
   //
   var szToFrom = retrieveCookie();

   if ( szToFrom . length == 0 ) 
   {
	var todaydate = new Date();
	//	set a timestamp in the cookie; used to read the cookie only at the beginning of the user's MC session
	//	assume that the user will wait at least 3 minutes before starting a brand new session
	var timestamp = todaydate.getTime() + 180000 ; 
	todaydate.setMonth( todaydate.getMonth() + 6 );

   	var destURLAddr2 = parameterExists(window.location.href, 'tocity');
   	var destURLAddr3 = parameterExists(window.location.href, 'tostate');
   	var origURLAddr2 = parameterExists(window.location.href, 'fromcity');
   	var origURLAddr3 = parameterExists(window.location.href, 'fromstate');
   	var visitorURLName = parameterExists(window.location.href, 'visitorname');

	if ( enableCookiePersist )
	{
      	document.cookie = 'tocity=' + destURLAddr2 + "; expires= " + todaydate.toGMTString();
      	document.cookie = 'fromcity=' + origURLAddr2 + "; expires= " + todaydate.toGMTString();
      	document.cookie = 'tostate=' + destURLAddr3 + "; expires= " + todaydate.toGMTString();
      	document.cookie = 'fromstate=' + origURLAddr3 + "; expires= " + todaydate.toGMTString();
      	document.cookie = 'visitorname=' + visitorURLName + "; expires= " + todaydate.toGMTString();
		if( readCookies ){
			document.cookie = 'nextread=' + timestamp + "; expires= " + todaydate.toGMTString();
		}
	}
	else
	{
      	document.cookie = 'tocity=' + destURLAddr2;
      	document.cookie = 'fromcity=' + origURLAddr2;
      	document.cookie = 'tostate=' + destURLAddr3;
      	document.cookie = 'fromstate=' + origURLAddr3;
      	document.cookie = 'visitorname=' + visitorURLName;
	}   
      return true;
   } 
   else 
   {
      location += szToFrom;
   }

   return false;
}

function AcceptCookie() {

/*

   Purpose:  This function tests whether the client accept cookies.

   Output: true - accepts, false - do not accept.

*/
   var todaydate = new Date();
   var strCookie = 'testcookie=' + todaydate;

   document.cookie = strCookie;

   var allcookies = document.cookie;
   var acceptCookie = (allcookies.indexOf(strCookie) != -1) ? true : false;

   return(acceptCookie);

}

RunMeFirst();

// -->