// Copyright 1996-2011 Information Builders, Inc. All rights reserved.
// $Revision: 1.7 $:
var ibicookie_js_Revision = "$Revision: 1.7 $:";

// Create a cookie with the specified name and value.
function SetCookie(sName, sValue, sPath, sDomain, sExpiration, bSecure)
{
   var sCookie = "";
   sCookie = sName + "=" + escape(sValue);
   if (sPath)        sCookie += "; path=" + sPath;
   if (sDomain)      sCookie += "; domain=" + sDomain;
   if (sExpiration)  sCookie += '; expires=' + sExpiration;
   if (bSecure)      sCookie += "; secure";
   document.cookie = sCookie;
 // alert(document.cookie);
}

// Retrieve the value of the cookie with the specified name.
function GetCookie(sName)
{
  // cookies are separated by semicolons
  //  alert(document.cookie);
  var aCookie = document.cookie.split("; ");
  for (var i=0; i < aCookie.length; i++)
  {
	// a name/value pair (a crumb) is separated by an equal sign
	var aCrumb = aCookie[i].split("=");
	if (sName == aCrumb[0])
	  return unescape(aCrumb[1]);
  }

  // a cookie with the requested name does not exist
  return null;
}
// Delete the cookie with the specified name.
function DelCookie(sName)
{
  document.cookie = sName + "=" + escape("gone") + "; expires=Fri, 31 Dec 1970 23:59:59 GMT;";
}

function ParseCookie(cookieValue)
{
   var result = cookieValue.split("^");
   for(var i=0; i<result.length; i++)
   {
      var pair = result[i].split("`");
      var nameCol = document.getElementsByName(pair[0]);
      if(nameCol && nameCol[0] && nameCol[0].tagName == "SELECT" && nameCol[0].options)
      {
         nameCol[0].focus();
         var opt = nameCol[0].options;
         for(var j=0; j<opt.length; j++)
         {
            if(opt[j].value == pair[1])
            {
               opt[j].selected = true;
               break;
            }
         }
      }
      else if(nameCol && nameCol[0] && nameCol[0].tagName == "INPUT")
      {
         for(var k=0; k<nameCol.length; k++)
         {
            if(nameCol[k].value == pair[1])
            {
               nameCol[k].checked = true;
               break;
            }
         }
      }
   }
}
// routines for ibi CGI cookies, eg WF_USER, etc.
function GetIBICookieElement( sCookieName, sElementName )
{
   var ibiCookie = GetCookie( sCookieName );
   if( ibiCookie )
      {
      //ibiCookie elements are separated by commas
      var elements = ibiCookie.split(",");
      // look backwards, b/c of cookie deletion delay issue in DelCookie() (deleted cookie may still be there at beginning of string).
      for (var i=elements.length-1; i >=0 ; i--)
         {
         // a name/value pair is separated by an equals sign
         var elementNameValue = elements[i].split("=");
         if( sElementName == unescape( elementNameValue[0] ) )
            return unescape(elementNameValue[1]);
         }
      }
   // a cookie with the requested name does not exist
   return null;
}
function SetIBICookieElement( sCookieName, sElementName, sElementValue, sPath, sDomain, sExpiration, bSecure)
{
   // this function only uses the first 3 parms.  the rest just get passed on to SetCookie() and DelIBICookieElement()
   DelIBICookieElement( sCookieName, sElementName, sPath, sDomain, sExpiration, bSecure);
   var newValue = "";
   // do this so that any remaining data from the old cookie will be preserved. 
   var remainder = GetCookie( sCookieName );
   if( remainder && remainder != "undefined" )
      {
      newValue += remainder;
      if( newValue.charAt(newValue.length-1) != ',' )  // this is  for protection, in case cookie was set wrong previously.
         newValue += ',';
      }
   newValue += escape(sElementName) + '=' + escape(sElementValue) + ',';
   SetCookie( sCookieName, newValue, sPath, sDomain, sExpiration, bSecure);
   return;
}
function DelIBICookieElement( sCookieName, sElementName, sPath, sDomain, sExpiration, bSecure)
{
   // this function only uses the first 2 parms.  the rest just get passed on to SetCookie().
   var ibiCookie = GetCookie( sCookieName );
   if( ibiCookie)
      {
      var begin = ibiCookie.indexOf( sElementName + "=" );
      if( begin > -1 )
         {
         var end = ibiCookie.indexOf( ',', begin );
         var remainder = ibiCookie.substring( 0, begin );
         remainder += ibiCookie.substr( end+1 );
         if( remainder )
            SetCookie( sCookieName, remainder, sPath, sDomain, sExpiration, bSecure);
         else DelCookie( sCookieName );
         }
      }
   return;
}

