//////////////////////////////////////////////////////////////////////////
// CookieTin.js * How to use a cookie to store/retrieve records with fields
// My first attempt ... :-)). Usefull in creating order forms and so on.
//////////////////////////////////////////////////////////////////////////
// In your html source, put the following line in the header
// <head>
// <script language="javascript" src="cookietin.js"></script>
// // rest of header
// </head>
// Make sure this file is where src is pointing to!!
// From now on you can reference the global variables (indicated below) and 
// use all functions in your whole html file.
//////////////////////////////////////////////////////////////////////////
// These (!) cookies use a fixed layout, as follows:
// Cookie overall:  "CookieName||Record1||Record2||....||RecordN"
// Each record in it: "Identifyer^^Field1^^Field2^^Field3^^....^^FieldN"
// Of course never use || or ^^ within a field or identifyer. 
// If you must do so, then change the global variables below.
//////////////////////////////////////////////////////////////////////////
// Read the function comments to figure out how to use everything.
// Note: cookies can be up to 4kb, no more. Sufficient for 1 order form.
//////////////////////////////////////////////////////////////////////////


//////////////////////////////////////////////////////////////////////////
// Global variables which you can use in your html file as well. This is handy,
// if for some reason you need to change these variables, change them here and
// your html page can remain unaltered.

var gCookieCRLF = "{CRLF}";
var gCookieTAB = "{TAB}";


function CookieCreate(NameOfCookie)
//Creates the simplest cookie, which is the name only.
// Code: MyCookie = CookieCreate("NewCookieName");
{
 return NameOfCookie 
}

function CookieLoad(NameOfCookie)
// Loads an existing cookie, or returns ""
// Code: Mycookie = CookieLoad("ThisIsWhatICalledItWhenIMadeIt");
{ 
if (document.cookie.length > 0) 
 {
 //alert(unescape(document.cookie));
 begin = document.cookie.indexOf(NameOfCookie+"="); 
 if (begin != -1) 
  {begin += NameOfCookie.length+1; 
  end = document.cookie.indexOf(";", begin);
  if (end == -1) {end = document.cookie.length};
  return unescape(document.cookie.substring(begin, end));
  } 
 }
//alert("No cookie") 
return ""; 

} 

function CookieSave(NameOfCookie, sCookie, iDays, sPath)
// Saves the cookie to browser memory. It is a session-only cookie, 
// i.e. when you close your browser, the cookie will be lost.
// Code: CookieSave("ThisIsWhatICallIt", MyCookie);
// iDays: Expiry after n days. 0 is session cookie, <0 is remove now.
{ 
	var sExpires = '';
	if (iDays)
	{
		var date = new Date();
		date.setTime(date.getTime()+(iDays*24*60*60*1000));
		var sExpires = "; expires="+date.toGMTString();
	}
	if (!sPath) var sPath = "/";
	sPath = "; " + sPath;
 	document.cookie = NameOfCookie + "=" + escape(sCookie) + sExpires + sPath;
}

function CookieKill (NameOfCookie)
// Remove a cookie from memory
// Code: CookieKill ("ThisIsWhatICalledIt");
{ 
 if (CookieLoad(NameOfCookie))
 {
 document.cookie = NameOfCookie + "=" + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
 }
}

function CookieBreak(sCookie)
// Break the cookie into individual records in an array
// Code on how to use it: var a = CookieBreak(MyCookie);
// This is how to retrieve records 1 to the last one in the array (element [0] is the cookie name)
// for (i=0; i<a.length; i++) {alert(a[i])}; //use alert to show each record for now
{
 return sCookie.split(gCookieCRLF);
}


function CookieCrumble(sPart)
// Crumble each cookie record into individual fields in an array
// Code on how to use it: var a = CookieCrumble(MyCookiePart3);
// This is how to retrieve fields 1 to the last one in the array (element [0] is the record ID)
// for (i=0; i<a.length; i++) {alert(a[i])}; //use alert to show each field for now
{
 return sPart.split(gCookieTAB);
}

// This works to get field 3 out of record 2, but you must know the upper dimensions of the arrays:
// alert(CookieCrumble(CookieBreak(MyCookie)[2])[3]); 
// If you go beyond array boundaries, it comes back as the string "undefined"


function CookieAdd(sCookie, sPart)
// Add sPart to the cookie (you must be sure it is not there yet!)
// Code: MyCookie = CookieAdd(MyCookie, sPart)
// sPart must be made manually in the format "Identifyer^^Field1^^Field2^^Field3^^....^^FieldN"
{
 return sCookie + gCookieCRLF + sPart
}


function CookiePart(sCookie, sID)
// Return the wanted record identified by sID from the total cookie
// Code: alert(CookiePart(MyCookie, "Record12"));
{
 begin = sCookie.indexOf(gCookieCRLF + sID + gCookieTAB)
 if (begin != -1) 
  {begin += gCookieCRLF.length; 
  end = sCookie.indexOf(gCookieCRLF, begin);
  if (end == -1) {end = sCookie.length};
  return sCookie.substring(begin, end)
  }
}

function CookieUpdate(sCookie, sPart)
// Replace or add the wanted record identified by sID in the total cookie
// Code: alert(CookieUpdate(MyCookie, sPart));
// sPart must be made manually in the format "Identifyer^^Field1^^Field2^^Field3^^....^^FieldN"
// sPart is found in the existing cookie by finding the ID of that record
{
 end = sPart.indexOf(gCookieTAB);
 if (end == -1)  {end = sPart.length};
 var sID = sPart.substring(0, end);
 begin = sCookie.indexOf(gCookieCRLF + sID + gCookieTAB);
 if (begin != -1) 
  {begin += gCookieCRLF.length; 
  end = sCookie.indexOf(gCookieCRLF, begin);
  if (end == -1) {end = sCookie.length};
  return sCookie.substring(0, begin) + sPart + sCookie.substring(end, sCookie.length);
  }
 return CookieAdd(sCookie, sPart);

}
// That's all folks!
