
function addTheItem(CookieName, itemID) {
// Note: "count" is a way to track the order in which images were picked.
// It is stored in its own cookie and incremented everytime a new image is saved.
// Then, the count value is stored with the imageID cookie and used as a sequencer.

    var count;
    count = getCount();
    if(!count || isNaN(count))
    {
      setCookie("BRBL00001",0,null);
      count=0;
    }
    count++;
    setCookie("BRBL00001",count,null);

    var itemArray = itemCookieToArray(getCookie(CookieName));
    if (-1 != FindItemInArray(itemID, itemArray)) {
    }
    else {
//      var newItem = new Array(CookieName,itemID+"~"+count);
      var newItem = itemID+"~"+count;
      itemArray = itemArray.concat(newItem);
      setCookie(CookieName, itemArray.join("|"), null);
    }
}

function getCount() {
 var cookiestring=""+document.cookie;
 var index1=cookiestring.indexOf("BRBL00001");
 if (index1==-1) return "0"; 
 var index2=cookiestring.indexOf(';',index1);
 if (index2==-1) index2=cookiestring.length; 
 return unescape(cookiestring.substring(index1+10,index2));
}
   
function removeTheItem(CookieName, itemID) {
    var itemArray = itemCookieToArray(getCookie(CookieName));
    var itemSlot = FindItemInArray(itemID, itemArray);
    if (-1 != itemSlot) {
      itemArray = itemArray.slice(0,itemSlot).concat(itemArray.slice(itemSlot+1));
      setCookie(CookieName, itemArray.join("|"), null);
      XCCollectIt();
    }
}

function removeGroupItem(CookieName, itemID, group) {
    var itemArray = itemCookieToArray(getCookie(CookieName));
    var itemSlot = FindItemInArray(itemID, itemArray);
    if (-1 != itemSlot) {
      itemArray = itemArray.slice(0,itemSlot).concat(itemArray.slice(itemSlot+1));
      setCookie(CookieName, itemArray.join("|"), null);
      window.location = "editSAVEDGROUP2.asp?fn="+group
    }
}

function getCookie(CookieName) {
   var search = CookieName + "="
   if (document.cookie.length > 0) { // if there are any cookies
      offset = document.cookie.indexOf(search) 
      if (offset != -1) { // if cookie exists 
         offset += search.length 
         // set index of beginning of value
         end = document.cookie.indexOf(";", offset) 
         // set index of end of cookie value
         if (end == -1) 
            end = document.cookie.length
         return unescape(document.cookie.substring(offset, end))
      } 
   }
   return "";
}

function itemCookieToArray(items) {
    var itemArray = new Array();
    for (i=0; i<items.length; i++) {
        var itemString = "";
        while ( (i < items.length) && ('|' != items.substring(i,i+1))) {
            itemString += items.substring(i,i+1);
            i++;
        }
        var newItem = new Array(itemString);
        itemArray = itemArray.concat(newItem);
    }
    return itemArray;
}


function FindItemInArray(itemID, itemArray) {
    var search = itemID + "~";
    var arrayString;
    for (i=0; i<itemArray.length; i++) {
//        if (itemArray[i] == itemID) {
      arrayString = itemArray[i];
      offset = arrayString.indexOf(search) 
      if (offset != -1) { // found the item 
            return i;
        }
    }
    return -1;
}

function setCookie(CookieName, value, expire) {
   var debug_me=0;
   var path="/";
   var domain="library.yale.edu";
   // Sets cookie values. Expiration date is optional
   document.cookie = CookieName + "=" + escape(value)
	+ ((expire == null) ? "" : ("; expires=" + expire.toGMTString()));
// NOTE THAT THE PATH AND DOMAIN ARGUMENTS ARE CAUSING PROBLEMS IF IIS_6 IS THE WEB SERVER
//        + ( (path == null)   ? "" : ("; path="    + path)   ) 
//        + ( (domain == null) ? "" : ("; domain="  + domain) ); 

 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!
 if ( (document.cookie.length > 0) && (debug_me > 0)) 
 { 
  begin = document.cookie.indexOf(CookieName+"="); 
  if (begin != -1) // Note: != means "is not equal to"
  { 
   begin += CookieName.length+1; 
   end = document.cookie.indexOf(";", begin);
   if (end == -1)
   {
     end = document.cookie.length;
   }
   alert(unescape(document.cookie.substring(begin,end))); 
  }
 }
 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!


}


function getAllCookies() {
 XCCollectIt();
}
 
function XCCollectItORIG() {
 var items0="";
 var items1="";
 var items2="";
 var items5="";
    var itemArray0 = itemCookieToArray(getCookie("PHONEGItems"));
    var itemArray1 = itemCookieToArray(getCookie("MLFItems"));
    var itemArray2 = itemCookieToArray(getCookie("BRBLDLItems"));
    var itemArray5 = itemCookieToArray(getCookie("RMNVItems"));
// round-about way to set array because Netscape was having trouble here.
    if ( (itemArray0.length == 0)&&(itemArray1.length == 0)&&(itemArray2.length == 0)&&(itemArray5.length == 0) ) {
      alert (" You have not selected any images yet...");
      return;
    }
    else {
      if (itemArray0.length != 0) {
        items0="Y"
      }          
      if (itemArray1.length != 0) {
        items1="Y"
      }         
      if (itemArray2.length != 0) {
        items2="Y"
      }          
      if (itemArray5.length != 0) {
        items5="Y"
      }          
      newURL = "CollexMGRXC.asp?xc0="+items0+"&xc1="+items1+"&xc2="+items2+"&xc5="+items5;
      var windowparms = "toolbar=no,location=no,menubar,status,scrollbars,resizable,top=10,left=10";
      remote = window.open(newURL,"Beinecke",windowparms);
      remote.focus();
      return;
    }
}

function XCCollectIt() {
 var items0="";
 var items1="";
 var items2="";
 var items5="";
    var itemArray0 = itemCookieToArray(getCookie("PHONEGItems"));
    var itemArray1 = itemCookieToArray(getCookie("MLFItems"));
    var itemArray2 = itemCookieToArray(getCookie("BRBLDLItems"));
    var itemArray5 = itemCookieToArray(getCookie("RMNVItems"));
// round-about way to set array because Netscape was having trouble here.
    if ( (itemArray0.length == 0)&&(itemArray1.length == 0)&&(itemArray2.length == 0)&&(itemArray5.length == 0) ) {
      alert (" You have not selected any images yet...");
      return;
    }
    else {
      newURL = "CollexMGRXC.asp";
      var windowparms = "toolbar=no,location=no,menubar,status,scrollbars,resizable,top=10,left=10";
      remote = window.open(newURL,"Beinecke",windowparms);
      remote.focus();
      return;
    }
}


function tickleCookie() {
    var itemArray0 = itemCookieToArray(getCookie("PHONEGItems"));
    var itemArray1 = itemCookieToArray(getCookie("MLFItems"));
    var itemArray2 = itemCookieToArray(getCookie("BRBLDLItems"));
    var itemArray5 = itemCookieToArray(getCookie("RMNVItems"));
}

function XCCollectSave() {
      var newURL = "saveGROUP.asp?overwrite=Y";
      var windowparms = "toolbar=no,location=no,menubar,status,scrollbars,resizable,top=10,left=10";
      remote = window.open(newURL,"Beinecke",windowparms);
      remote.focus();
      return;
}


function openZOOM(zoomURL,windowname) {
 var theurl = zoomURL;
//var newwindow = windowname;
 var newwindow = "sidview";
 var theparms = "toolbar=no,location=no,menubar=no,status=yes,scrollbars=yes,resizable=yes,top=20,left=50;"
   remote = window.open(theurl,newwindow,theparms);
   remote.focus();
}


function cookieBomb()
{
	makeCookie("PHONEGItems","",-1);
	makeCookie("MLFItems","",-1);
	makeCookie("BRBLDLItems","",-1);
	makeCookie("RMNVItems","",-1);
}
function makeCookie(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}
