
//
// main program node1 htdocs
// 
cartID = getCookie();

if( cartID == null )
 {
  cartID = makeSessionID();
  setCookie( cartID );
 }


//
// basic cookie access routines
//

function getCookie()
{
 var cookie = document.cookie;
 var search = "CART_ID=";
 var setStr = null;
 var offset = 0;
 var end = 0;

// alert( "existing cookie in raw form: '" + document.cookie + "'" );

 if( cookie.length > 0 )
  {
   offset = cookie.indexOf( search );
   if( offset != -1 )
    {
     offset += search.length;
     end = cookie.indexOf( ";", offset )
     if( end == -1 )
      {
       end = cookie.length;
      }
     setStr = unescape( cookie.substring( offset, end ) );

     // treat cookies of zero length as non-existent
     if( setStr.length == 0 )
      {
       setStr = null;
      }
    }
  }

// alert( "existing cookie in refined form: " + setStr );

 return( setStr );
}


function setCookie( value )
{
 var exp = new Date();
 var cookie = "";

 if( ( name == null ) ||
     ( value == null ) )
  {
   return false;
  }
 
 exp.setTime( exp.getTime() + 28 * 86400000 );
 document.cookie = "CART_ID=" + value + "; "
        + "expires=" + exp.toGMTString() + "; "
        + "path=/; "
        + "domain=.swd.de";

// alert( "after setting it: " + document.cookie );

 return true;
} 


function deleteCookie()
{
 document.cookie = "CART_ID=; "
                 + "expires=Thu, 01-Jan-70 00:00:01 GMT; "
                 + "path=/; "
                 + "domain=.swd.de";
 return true;
} 


//
// high level functions
//

// make an ID that is 20 digits long
function makeSessionID()
{
 var sessionID = "";

 for( i = 0;
      i < 20;
      i ++ )
  {
   sessionID += String( Math.round( ( Math.random() -0.05 ) * 10 ) );
  }

 return sessionID;
}

