//------------------------------------------------------------------------------
// Project:       Structural Design of Stainless Steel
// Copyright:     © 2003, The Steel Construction Institute
// File version:  1.00.027
// File name:     _Scripts/Main.js
// Description:   Main page scripts
// Developed by:  FreeSTYLE Software Ltd.
// Author:        Rezo Mesarkishvili
// Last revision: 6 August 2005
//------------------------------------------------------------------------------

   var CookieEMail        = "SCIUserEMail";
   var CookieNewsSubscr   = "SCINewsSubscr";
   var CookieStoreData    = "CalcUKStoreData";
   var CookieSessionStart = "CalcUKSStart";
   var CookieSessionEnd   = "CalcUKSEnd";
   var CookieSessionDur   = "CalcUKSDur";
   var CookieCalcCounter  = "CalcUKCounter";

   var CookieExpires  = GetExpDate ();
   var UserEMail      = GetCookie ( CookieEMail );
   var SaveValues     = false;
   var ShowCalcVerErr = false;
   var StoreData      = GetCookie ( CookieStoreData ) == "Yes" && SaveValues;

//-----------------------------------------------------------/ GetExpDate /-----
// Function:    GetExpDate
// Description: Creates cookie expiration date as a string
// Returns:     Date string in UTC string format
//------------------------------------------------------------------------------
function GetExpDate ()
{
   var CurrDate  = new Date ();

   var CurrMonth = CurrDate.getMonth ();
   if (    CurrMonth           ==  1
        && CurrDate.getDate () == 29
      )
   {
      CurrDate.setDate ( 28 );
   }
   else
   if ( CurrDate.getDate () == 31 )
   {
      CurrDate.setDate ( 30 );
   }
   if ( CurrMonth == 11 )
   {
      CurrDate.setFullYear ( CurrDate.getYear () + 1 );
      CurrDate.setMonth ( 0 );
   }
   else
   {
      CurrDate.setMonth ( CurrMonth + 1 );
   }
   return CurrDate.toUTCString ();
}

//------------------------------------------------------------/ SetCookie /-----
// Function:    SetCookie
// Description: Creates a cookie with the specified name and value
// Arguments:   Name  - cookie name
//              Value - cookie value
// Returns:     nothing
//------------------------------------------------------------------------------
function SetCookie ( Name, Value )
{
   document.cookie = Name + "=" + escape ( Value ) + "; expires=" + CookieExpires;
}

//------------------------------------------------------------/ GetCookie /-----
// Function:    GetCookie
// Description: Retrieves cookie value
// Arguments:   Name - cookie name
// Returns:     Cookie value or null if cookie with specified name does not exist
//------------------------------------------------------------------------------
function GetCookie ( Name )
{
   var Cookie = document.cookie.split ( "; " );
   for ( var i = 0; i < Cookie.length; i ++ )
   {
      var Prop = Cookie [i].split ( "=" );
      if ( Prop [0] == Name )
      {
         return unescape ( Prop [1] );
      }
   }
   return "";
}

//------------------------------------------------------------/ DelCookie /-----
// Function:    DelCookie
// Description: Deletes cookie (sets expiration date in the past)
// Arguments:   Name - cookie name
// Returns:     nothing
//------------------------------------------------------------------------------
function DelCookie ( Name )
{
   var ExpDate = new Date ( 1999, 11, 31, 0, 0, 0, 0 );
   document.cookie = Name + "=expired; expires=" + ExpDate.toUTCString ();
}

//--------------------------------------------------------/ SetCookieColl /-----
// Function:    SetCookieColl
// Description: Creates a cookie with the specified name and array of named values
// Arguments:   Name - cookie name
//              Coll - collection of named values
// Returns:     nothing
//------------------------------------------------------------------------------
function SetCookieColl ( Name, Coll )
{
   var IsFirst = true;
   var Values  = "";
   for ( var PropName in Coll )
   {
      if ( IsFirst )
      {
         IsFirst = false;
      }
      else
      {
         Values += "&";
      }
      Values += ( PropName + "=" + escape ( Coll [PropName] ));
   }
   document.cookie = Name + "=" + Values + "; expires=" + CookieExpires;
}

//------------------------------------------------------/ SetCookieValues /-----
// Function:    SetCookieValues
// Description: Creates a cookie with the specified name and array of indexed values
// Arguments:   Name - cookie name
//              Coll - collection of named values
// Returns:     nothing
//------------------------------------------------------------------------------
function SetCookieValues ( Name, Coll )
{
   var Values  = "";
   for ( var PropName in Coll )
   {
      Values += Coll [PropName] + ";";
   }

   document.cookie = Name + "=" + escape ( Values ) + "; expires=" + CookieExpires;
}

//--------------------------------------------------------/ GetCookieColl /-----
// Function:    GetCookieColl
// Description: Retrieves collection of named values for specified cookie
// Arguments:   Name - cookie name
//              Coll - collection of named values
// Returns:     true on success or false if cookie with specified name does not exist
//------------------------------------------------------------------------------
function GetCookieColl ( Name, Coll )
{
   var Cookie = document.cookie.split ( "; " );
   for ( var i = 0; i < Cookie.length; i ++ )
   {
      var Eq = /=/.exec ( Cookie [i] ).index;
      if (    Eq > -1
           && Cookie [i].substr ( 0, Eq ) == Name
         )
      {
         Crumb = ( Cookie [i].substr ( Eq + 1 )).split ( "&" );
         for ( var j = 0; j < Crumb.length; j ++ )
         {
            var Prop = Crumb [j].split ( "=" );
            Coll [Prop [0]] = unescape ( Prop [1] );
         }
         return true;
      }
   }
   return false;
}

//------------------------------------------------------/ GetCookieValues /-----
// Function:    GetCookieValues
// Description: Retrieves collection of indexed values for specified cookie
// Arguments:   Name - cookie name
//              Coll - collection of named values
// Returns:     true on success or false if cookie with specified name does not exist
//------------------------------------------------------------------------------
function GetCookieValues ( Name, Coll )
{
   var Cookie = document.cookie.split ( "; " );
   for ( var i = 0; i < Cookie.length; i ++ )
   {
      var Eq = /=/.exec ( Cookie [i] ).index;
      if (    Eq > -1
           && Cookie [i].substr ( 0, Eq ) == Name
         )
      {
         Crumb = ( Cookie [i].substr ( Eq + 1 )).split ( "&" );
         for ( var j = 0; j < Crumb.length; j ++ )
         {
            var Prop = Crumb [j].split ( "=" );
            Coll [Prop [0]] = unescape ( Prop [1] );
         }
         return true;
      }
   }
   return false;
}

//----------------------------------------------------------/ GetDuration /-----
// Function:    GetDuration
// Description: Calculates time span between two Date objects
// Arguments:   StartDt - starting date object
//              EndDt - ending date object
// Returns:     Time span as a string: HH:MM:SS
//------------------------------------------------------------------------------
function GetDuration ( oStart, oEnd )
{
   var TimeSpan = parseInt (( oEnd.getVarDate () - oStart.getVarDate () ) / 1000 );
   if ( TimeSpan > 0 )
   {
      var MinSec   = 60;           // Number of seconds in a minute
      var HrSec    = MinSec * 60;  // Number of seconds in an hour
      var DaySec   = HrSec  * 24;  // Number of seconds in a day
      var Interval = "";

      var DaysPassed = parseInt ( TimeSpan / DaySec );
      if ( DaysPassed )
      {
         Interval += DaysPassed + " day(s)";
      }
      TimeSpan -= DaysPassed * DaySec;
      var Hours = parseInt ( TimeSpan / HrSec );
      if ( Hours )
      {
         if ( Interval != "" )
         {
            Interval += ", ";
         }
         TimeSpan  -= Hours * HrSec;
         Interval += Hours + " h";
      }
      var Minutes = parseInt ( TimeSpan / MinSec );
      if ( Minutes )
      {
         if ( Interval != "" )
         {
            Interval += ", ";
         }
         TimeSpan  -= Minutes * MinSec;
         Interval += Minutes + " min";
      }
      if ( Interval != "" )
      {
         Interval += ", ";
      }
      Interval += TimeSpan + " sec";
      return Interval;
   }
   return "-";
}

//____________________________________________________________ End of File _____
