/*
  Programmer:  Michael Thomas, michael@michael-thomas.com
  Orig Date.:  11/03/00

  For your reference:
    Book:  "Designing with JavaScript, Creating Dynamic Web Pages" by Nick Heinle, published
           by O'Reilly.  Chapter 8 is a great resource for cookies.  This book inspired me
           to create this javascript code.  Some of the functions originally originated 
           from his design.

*/
//********************************************************************************
function isCookiesEnabled() {
//********************************************************************************
	document.cookie = "isCookiesEnabled=true"

  return ( document.cookie.indexOf("isCookiesEnabled=true")>=0 ? true : false );
}
//********************************************************************************
function setCookie(strName, strValue) {
//********************************************************************************
  //Use this function to customize a default cookie setting.
  //Use fsetCookie() for access to all settings.
  
  //Current settings:
  // 90 days to expire.  Cookie saved in the root. 
  dateExpire = new Date();
  dateExpire = fsetCookieDateTime( dateExpire, 90, "day")
  fsetCookie(strName, strValue, dateExpire, "/", null, null);

}
//********************************************************************************
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(strName){
//-----------------------------------------------------------------------------
//Retrieve the cookie.

  strReturn = null;

  var arrCookieInfo = document.cookie.split("; ");
  var arrCookieItem
  var intStart
  var intStop
  
  for ( var i = 0; i < arrCookieInfo.length ; i++ ) {
    if ( arrCookieInfo[i].indexOf(strName+"=") > -1 ) {
      intStart = arrCookieInfo[i].indexOf("=") + 1; 
      intStop = arrCookieInfo[i].length;
      strReturn = unescape(arrCookieInfo[i].substring(intStart, intStop));
    }
  }

  return strReturn;
}

//-----------------------------------------------------------------------------
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>');
  
}

//-----------------------------------------------------------------------------
function fHtmlCookieDemoA() {
//-----------------------------------------------------------------------------

 var dateExpire;

 
 document.write('<table border="1" width="100%">');
 document.write("<tr><td width=20%><b>Cookie Name</b></td><td width=20%><b>Cookie Value</b></td><td><b>Notes</b></td></tr>");

 document.write("<tr><td>n/a<br><b>Before</b> cookies have been created.</td><td>" + "n/a" + "</td><td>Value of document.cookie:<br><b>" + unescape(document.cookie) + "</b><br>" +
                "<br>Note:<ol>" +
                "<li>If this is the first time you've loaded this page or if you have cleared/deleted the cookies and closed the browser, then the cookie will be empty." + 
                "<li>Once this page has loaded, if you refresh/reload the page you will see the cookie file has cookies." +
                "</ol></td></tr>"); 
 
 fsetCookie("Cookie_01");
 fsetCookie("Cookie_02", "Cookie #2");

 dateExpire = new Date();
 dateExpire = fsetCookieDateTime( dateExpire, 32, "day")
 fsetCookie("Cookie_03", "Cookie #3-expire in 32 days", dateExpire);
 
 fsetCookie("CookieNameTest", "Name Test");
 fsetCookie("CookieNameTest2", "Name Test 2");
 fsetCookie("CookieSemicolonTest1", "Semicolon Test;1");
 fsetCookie("CookieSemicolonTest2", "Semicolon Test; 2"); //Testing the split() function's separator value.
 fsetCookie("CookieEqualTest", "Semicolon=Test"); //Testing getting of the values of a cookie if it has an equal sign.
 
 
 document.write("<tr><td>n/a<br><b>After</b> cookies have been created.</td><td>" + "n/a" + "</td><td>Value of document.cookie:<br><b>" + unescape(document.cookie) + "</b><br>" +
                "<br>Note:<ol>" +
                "<li>If this is the first time you've loaded this page or if you have cleared/deleted the cookies and closed the browser, then \"mytest\" will not appear here." + 
                "<li>Once this page has loaded, if you refresh/reload the page you will see the cookie \"mytest\"." +
                "</ol></td></tr>"); 
 
 document.write("<tr><td>Cookie_01</td><td>" + fgetCookie("Cookie_01") + 
                "</td><td>fsetCookie(\"Cookie_01\") (note:  no value sent.)</td></tr>");
 document.write("<tr><td>Cookie_02</td><td>" + fgetCookie("Cookie_02") + 
                "</td><td>fsetCookie(\"Cookie_02\", \"Cookie #2\")</td>");
 document.write("<tr><td>Cookie_03</td><td>" + fgetCookie("Cookie_03") + 
                "</td><td>fsetCookie(\"Cookie_03\", \"Cookie #3\", dateExpire)</td>");
 document.write("<tr><td>mytest</td><td>"  + fgetCookie("mytest")  + 
                "</td><td>Checking for a cookie that has not been created.</td></tr>");

 fsetCookie("mytest","Hello");
 document.write("<tr><td>mytest</td><td>"  + fgetCookie("mytest")  + 
                 "</td><td>Now, create the cookie with value=\"Hello\".<br>fsetCookie(\"mytest\",\"Hello\")</td></tr>");
 
 fsetCookie("mytest","Hello World");
 document.write("<tr><td>mytest</td><td>"  + fgetCookie("mytest")  + 
                "</td><td>Now, change value to \"Hello World\"<br>fsetCookie(\"mytest\",\"Hello World\");</td></tr>");

 fsetCookie("mytest",null);
 document.write("<tr><td>mytest</td><td>"  + fgetCookie("mytest")  + 
                "</td><td>fsetCookie(\"mytest\",null);<br><b>Notice:  IE=null, Netscape=null also.</b></td></tr>");

 fsetCookie("mytest","");
 document.write("<tr><td>mytest</td><td>"  + fgetCookie("mytest")  + "</td><td>fsetCookie(\"mytest\",\"\");<br><b>Notice:  Normally IE=null & Netscape is blank. However, I have JavaScript code that converts \"\" to a null to make IE & Netscape work the same..</b></td></tr>");

 fsetCookie("mytest"," ");
 document.write("<tr><td>mytest</td><td>"  + fgetCookie("mytest")  + "</td><td>fsetCookie(\"mytest\",\" \");</td></tr>");

 fdelCookie("mytest");
 document.write("<tr><td>mytest</td><td>"  + fgetCookie("mytest")  + "</td><td>Now, delete the cookie.<br>fdelCookie(\"mytest\");</td></tr>");

 document.write("<tr><td>n/a</td>" +
                "<td>" + "n/a" + "</td>" + 
                "<td>Value of document.cookie:<br><b>" + unescape(document.cookie) + "</b></td></tr>"); 

 dateExpire = new Date();
 dateExpire = fsetCookieDateTime(dateExpire, 10, "sec");

 fsetCookie("myexpire","Expire in 10 secs", dateExpire);
 document.write("<tr><td>myexpire</td><td>"  + fgetCookie("myexpire")  + "</td><td>fsetCookie(\"myexpire\",\"Expire Example\", dateExpire);</td></tr>");

 document.write("</table>");

}
//-----------------------------------------------------------------------------
function fDisplayCurrentCookie() {
//-----------------------------------------------------------------------------
  
  document.frmCookie.CookieText.value = unescape(document.cookie);
  }

