//--------------------------------------------------------------
// Code from http://www2.volstate.edu/dba/CIS172/cookie.htm
//--------------------------------------------------------------
function DeleteCookie(name)
{
   var oneDay = 24 * 60 * 60 * 1000 //number of ms in one day
   var expireDate = new Date() //variable to hold date

   //set expire date equal to yesterday
   expireDate.setTime(expireDate.getTime() - oneDay)

   //write the cookie with the new expiration date...browser will delete cookie
   document.cookie = name + "=dummy; expires=" + expireDate.toGMTString()+ ";"
}

//----------------------------------------------------------------
// Currency - Returns passed number as string in $xxx,xxx.xx 
//            format. 
//            This JavaScript code written by Alan Simpson - 
//            www.coolnerds.com.
//----------------------------------------------------------------
function Currency(anynum) {
   //-- Returns passed number as string in $xxx,xxx.xx format.
   anynum=eval(anynum)
   workNum=Math.abs((Math.round(anynum*100)/100));workStr=""+workNum
   if (workStr.indexOf(".")==-1){workStr+=".00"}
   dStr=workStr.substr(0,workStr.indexOf("."));dNum=dStr-0
   pStr=workStr.substr(workStr.indexOf("."))
   while (pStr.length<3){pStr+="0"}

   //--- Adds comma in thousands place.
   if (dNum>=1000) {
      dLen=dStr.length
      dStr=parseInt(""+(dNum/1000))+","+dStr.substring(dLen-3,dLen)
   }

   //-- Adds comma in millions place.
   if (dNum>=1000000) {
      dLen=dStr.length
      dStr=parseInt(""+(dNum/1000000))+","+dStr.substring(dLen-7,dLen)
   }
   retval = dStr + pStr 
   //-- Put numbers in parentheses if negative.
   if (anynum<0) {retval="("+retval+")"}
   return "$"+retval
}

//----------------------------------------------------------------
// GetCookie - Returns the value of the specified cookie or null
//             if the cookie doesn't exist.
//             (See page 819 of JavaScript Unleashed)
//----------------------------------------------------------------
function GetCookie (name) {
    var result = null;
    var myCookie = " " + document.cookie + ";";
    var searchName = " " + name + "=";
    var startOfCookie = myCookie.indexOf(searchName);
    var endOfCookie;
    if (startOfCookie != -1) {
    startOfCookie += searchName.length; // skip past cookie name
    endOfCookie = myCookie.indexOf(";", startOfCookie);
    result = unescape(myCookie.substring(startOfCookie, endOfCookie));
    }
    return result;
}

//----------------------------------------------------------------
// Adapted from DeleteCookie code
//----------------------------------------------------------------
function SetCookie(name,value)
{
   var oneDay = 30 * 24 * 60 * 60 * 1000 //number of ms in 30 days
   var expireDate = new Date() //variable to hold date

   //set expire date equal to 30 days from now
   expireDate.setTime(expireDate.getTime() + oneDay)

   //write the cookie with the new expiration date
   document.cookie = name + "=" + escape(value) + "; expires=" + expireDate.toGMTString() + ";"
}

//----------------------------------------------------------------
// SetCookieEZ - Quickly sets a cookie which will last until the
//               user shows down his browser.
//               (See page 819 of JavaScript Unleashed)
//----------------------------------------------------------------
function SetCookieEZ (name, value) 
{
  document.cookie = name + "=" + escape(value)
}

//----------------------------------------------------------------
// StringReplace - Replace all instances of text in string with 
//                 new text.
//                 (See page 259 of JavaScript Unleashed)
//
// Could this be done more efficiently using replace() method 
// discussed on page 255.
//----------------------------------------------------------------
    function StringReplace(originalString, findText, replaceText) {
      var pos = 0;
      var len = findText.length;
      pos = originalString.indexOf(findText);
      
      while (pos != -1) {
        preString = originalString.substring(0, pos);
        postString = originalString.substring(pos + len,
             originalString.length);
        originalString = preString + replaceText + postString;
        pos = originalString.indexOf(findText);
      }
      return originalString;
    }

/*
I got this from http://www.vermontsoftware.com/Javascript/trim.html
on March 14, 2004, Vince
==================================================================
LTrim(string) : Returns a copy of a string without leading spaces.
==================================================================
*/
function LTrim(str)
/*
   PURPOSE: Remove leading blanks from our string.
   IN: str - the string we want to LTrim
*/
{
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(0)) != -1) {
      // We have a string with leading blank(s)...

      var j=0, i = s.length;

      // Iterate from the far left of string until we
      // don't have any more whitespace...
      while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
         j++;

      // Get the substring from the first non-whitespace
      // character to the end of the string...
      s = s.substring(j, i);
   }
   return s;
}

/*
==================================================================
RTrim(string) : Returns a copy of a string without trailing spaces.
==================================================================
*/
function RTrim(str)
/*
   PURPOSE: Remove trailing blanks from our string.
   IN: str - the string we want to RTrim

*/
{
   // We don't want to trip JUST spaces, but also tabs,
   // line feeds, etc.  Add anything else you want to
   // "trim" here in Whitespace
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
      // We have a string with trailing blank(s)...

      var i = s.length - 1;       // Get length of string

      // Iterate from the far right of string until we
      // don't have any more whitespace...
      while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
         i--;


      // Get the substring from the front of the string to
      // where the last non-whitespace character is...
      s = s.substring(0, i+1);
   }

   return s;
}

/*
=============================================================
Trim(string) : Returns a copy of a string without leading or trailing spaces
=============================================================
*/
function Trim(str)
/*
   PURPOSE: Remove trailing and leading blanks from our string.
   IN: str - the string we want to Trim

   RETVAL: A Trimmed string!
*/
{
   return RTrim(LTrim(str));
}
 
/*
=============================================================
Use this code to see if cookies are enabled.
=============================================================
*/
function cookieEnabled()
{
    var cookieEnabledTmp=(navigator.cookieEnabled)? true : false

    //if not IE4+ nor NS6+
    if (typeof navigator.cookieEnabled=="undefined" && !cookieEnabledTmp){ 
    document.cookie="testcookie"
    cookieEnabledTmp=(document.cookie=="testcookie")? true : false
    document.cookie="" //erase dummy value
    }
    
    return cookieEnabledTmp;
}    

/*
=============================================================
Use this code to see if cookies are enabled.
=============================================================
*/
function cookieEnabled2()
{
    var On = false;
    SetCookie("CookiesOn", "Y");
    if (GetCookie("CookiesOn")=="Y")
    {
        On = true;                
    }
    
    return On;
} 
