//********************************************************************************
function setCookie(strName, strValue) {
//********************************************************************************

 dateExpire = new Date();
 dateExpire = fsetCookieDateTime( dateExpire, 90, "day")
 fsetCookie(strName, strValue, dateExpire);

}
//********************************************************************************
function getCookie(strName) {
//********************************************************************************
  return fgetCookie(strName);
}
//********************************************************************************
function delCookie(strName) {
//********************************************************************************
  fdelCookie(strName);
}

//********************************************************************************
function fCookieHtmlPage() {
//********************************************************************************
  var strMsg = "";

  var winMsg = window.open();

  var cname = name + "=";               
  var dc = document.cookie;             
  var aCookie = dc.split(";"); //Create an array of cookies.
  
  strMsg += "<html>" + "\n";
  strMsg += "<head>" + "\n";
  strMsg += "<title>Display Cookie Info</title>" + "\n";
  strMsg += "</head>" + "\n";
  strMsg += "<body>" + "\n";
  strMsg += "<p><h1>Display Cookie Info</h1></p>" + "\n";
  strMsg += "<p>This web page takes the cookie info, parses it into an array and displays the information. </p>" + "\n";
  winMsg.document.writeln( strMsg );

  if ( aCookie.length == 0 ) {
  	//Warning:  Even no cookie info may return a lenth of 1!
    winMsg.document.writeln( "No cookie data available." );
  } else {
    for (var i=0; i < aCookie.length ; i++) {
      winMsg.document.writeln( unescape( aCookie[i]) + "<br>" );
    }
  }

  strMsg = "";
  strMsg += "</body>" + "\n";
  strMsg += "</html>" + "\n";
  winMsg.document.writeln( strMsg );

  winMsg.document.close();  
  winMsg.focus();
}
//-----------------------------------------------------------------------------
function fgetCookie(name){
//-----------------------------------------------------------------------------
//Retrieve the cookie.

var cname = name + "=";               
var dc = document.cookie;             
    if (dc.length > 0) {              
    begin = dc.indexOf(cname);       
        if (begin != -1) {           
        begin += cname.length;       
        end = dc.indexOf(";", begin);
            if (end == -1) end = dc.length;
            return unescape(dc.substring(begin, end));
        } 
    }
return null;
}

//-----------------------------------------------------------------------------
function fsetCookie(strNameParm, strValueParm, dateExpiresParm, strPathParm, strDomainParm, strSecureParm) {
//-----------------------------------------------------------------------------
//Sets a cookie to a value.

var strName;
var strValue;
var strNameValue;
var strExpires;
var strPath;
var strDomain;
var strSecure;

  if ( typeof(strNameParm) == "string" ) {   //manditory
    strName = strNameParm;
  } else {
    strName = "default";  //Normally this value will always be passed to the function.
  }

  if ( typeof(strValueParm) == "string" ) {   //manditory
    if ( strValueParm == "" ) {
      strValue = null;  //This code makes IE & Netscape work the same.
    } else {
      strValue = escape(strValueParm);
    }
  } else {
    strValue = null;  //Didn't use "" because of difference between Netscape & IE!
  }
  
  strNameValue = strName + "=" + strValue;
  
  if ( typeof(dateExpiresParm) == "object" ) {  //optional
    strExpires = "expires=" + dateExpiresParm.toGMTString();
  } else {
    strExpires = "";
  }
    
  if ( typeof(strPathParm) == "string" ) {  //optional
    strPath = "path="+strPathParm;
  } else {
    strPath = "";
  }

  if ( typeof(strDomainParm) == "string" ) {  //optional
    strDomain = "domain=" + strDomainParm;
  } else {
    strDomain = "";
  }

  if ( typeof(strSecureParm) == "string" && strSecureParm.toLowerCase() == "secure" ) {  //optional
    strSecure = strSecureParm;
  } else {
    blnSecure = "";
  }
    

  document.cookie = strNameValue   + ";" +
                    strExpires + ";" +
                    strPath    + ";" +
                    strDomain  + ";" +
                    strSecure;
  
}
//-----------------------------------------------------------------------------
function fdelCookie(strName) {
//-----------------------------------------------------------------------------
// Use this function to delete a cookie.

  //Used value=null, not "", because of the difference in IE & Netscape!
  //This is the safest expiration parameter to delete a cookie.
  fsetCookie(strName,null, "Thu, 01-Jan-70 00:00:01 GMT");  
}

//-----------------------------------------------------------------------------
function fsetCookieDateTime(dateOrig, intValue, strType) {
//-----------------------------------------------------------------------------
// Pass a date object and change the value by the "intValue" and the "strType".

  var dateNew = dateOrig;

  if ( typeof(strType) != "string" ) strType = "day";
  if ( strType == "" ) strType = "day";

  if ( strType  == "day" || strType == "days" ) {
      dateNew.setTime( dateNew.getTime() + (1000*60*60*24*intValue) );
  } else if ( strType == "hr" || strType == "hours" ) {
      dateNew.setTime( dateNew.getTime() + (1000*60*60*intValue) );
  } else if ( strType == "min" || strType == "minuets" ) {
      dateNew.setTime( dateNew.getTime() + (1000*60*intValue) );
  } else if ( strType == "sec" || strType == "seconds" ) {
      dateNew.setTime( dateNew.getTime() + (1000*intValue) );
  } else if ( strType == "mil" || strType == "milsec" ) {
      dateNew.setTime( dateNew.getTime() + (intValue) );
  } else { 
    alert("Error in parameter passed.  Use: day, hr, min, sec, mil, etc...");
  } 

  return dateNew
}

//-----------------------------------------------------------------------------
function fListAllCookies() {
//----------------------------------------------------------------------------- 

  var cname = name + "=";               
  var strCookie = document.cookie; 
  
  document.write('<table border="1" width="100%">');
  document.write('<tr><td width="40%"><b>Cookie Name</b></td><td width="60%"><b>Value</b></td></tr>');

  document.write('<tr><td>Value of document.cookie<br>(string)</td><td>' + strCookie + '</td><tr>')
  document.write('<tr><td>Value of unescape(document.cookie)<br>(string)</td><td>' + unescape(strCookie) + '</td><tr>')

  for (var intBegin=0, intEnd=0; intBegin <= strCookie.length; intBegin++) {
    var strNameValue = "";
    var strName = "";
    var strValue = "";
    var intStop;
    
    intEnd = strCookie.indexOf(";", intBegin);
    if (intEnd == -1) intEnd = strCookie.length;  //The last cookie does not have a ";" at the end.
    strNameValue  = strCookie.substring(intBegin, intEnd);
    intBegin      = intEnd;  
 
    intStop = strNameValue.indexOf("=",0);
    if ( intStop == -1 ) {
      intStop = strNameValue.length;
    }
    
    strName = strNameValue.substring(0, intStop);
    strValue = unescape(strNameValue.substring( (intStop + 1) ) );  
    
    strHtml =  '';
    strHtml += '<tr>';
    strHtml += '<td>' + strName + '</td>';
    strHtml += '<td>' + strValue + '</td>';
    strHtml += '</tr>';
    
    document.write(strHtml);
    
  }
  
  document.write('</table>');
  
}
