var item_cookie = "item=";
var cookie_separator = ";";
var item_value_separator = ":";
var item_value_pair_separator = "&";
var old_cookie;
var item;
var arrCookie = new Array();
var ima = new Image();
var wind;
var twind;

/*function displaypic(entry)
{
   var im = new Image();
   im.src = entry;
   var hi = im.height;
   var wi = im.width;
   var nw = window.open('', '', 'toolbar=no, status=no, resizable=yes, scrollbars=no, height=' + hi + ', width=' + wi);
   nw.document.write("<body leftmargin=0 topmargin=0 marginwidth=0 marginheight=0>");
   nw.document.write("<img src=\"" + entry + "\">");
   nw.document.write("</body>");
}*/

function resizeImg(pic)
{
   var im;
   var pic_height;
   var pic_width;
   var frame_width = 150;
   var frame_height = 120;
	
   pic_width = eval(pic).width;
   pic_height = eval(pic).height;
   if( (pic_height > frame_height) && (pic_width > frame_width) ) // both exceeds the size of the frame
   {
      if( (pic_height / pic_width) >= frame_ratio) // taller than or equal to the frame ratio
      {
	      eval(pic).height = frame_height;
	   }
      else // wider than the frame ratio
      {
	     eval(pic).width = frame_width;
      }
   }
   else
   {
      if(pic_height > frame_height) // height exceeds the frame height
      {
         eval(pic).height = frame_height;
      }
      else
      {
 	      if(pic_width > frame_width) // width exceeds the frame width
	      {
	         eval(pic).width = frame_width;
    		}
      }
   } 
   eval(pic).style.visibility = "visible";
}				   

function displaypic(entry)
{
   ima.src = entry;
   twind = window.open('','', 'toolbar=no, status=no, resizable=no, scrollbars=no, height=30, width=200');
   twind.document.write("<body leftmargin=0 topmargin=0 marginwidth=0 marginheight=0>");
   twind.document.write("&nbsp;<p><center>Loading...");
   twind.document.write("</body>");
   setTimeout('display()', 500);
}

function display()
{
   twind.close();
   var hi = ima.height;
   var wi = ima.width;
   wind = window.open('', '', 'toolbar=no, status=no, resizable=yes, scrollbars=no, height=' + hi + ', width=' + wi);
   wind.document.write("<body leftmargin=0 topmargin=0 marginwidth=0 marginheight=0>");
   wind.document.write("<img src=\"" + ima.src + "\">");
   wind.document.write("</body>");
}

// item cookie string: "item=[i_id]:[qty]:[price]&[i_id]:[qty]:[price]&...;[session id]=[sessionid value]"

function readCart()
{
   if(document.cookie)
   {
      if(document.cookie.match(item_cookie))
      {                                                                          //alert("readCart: match");
         old_cookie = document.cookie.split(cookie_separator); // -> item=... | jsessionid=...
         var i = 0;
         for(i = 0; i < old_cookie.length; i++)
         {
            if(old_cookie[i].match(item_cookie)) break;
         }
         old_cookie = old_cookie[i].split(item_cookie); // -> item= | item id:qty:price&item id:qty:price&...
         old_cookie = old_cookie[1].split(item_value_pair_separator); // -> item id:qty:price | item id:qty:price | ...
         var j = 0;
         arrCookie.length = 0;                                                     //alert(old_cookie);
         for(i = 0; i < old_cookie.length; i++)
         {
            item = old_cookie[i].split(item_value_separator);
            arrCookie[j] = item[0];                                             //alert("arrCookie[" + j + "]: " + arrCookie[j]);
            j++;
            arrCookie[j] = item[1];                                              //alert("arrCookie[" + j + "]: " + arrCookie[j]);
            j++;
            arrCookie[j] = item[2];                                              //alert("arrCookie[" + j + "]: " + arrCookie[j]);
            j++;
         }                                                                      // alert("arrCookie.length: " + arrCookie.length);
      } // if(document.cookie.match(item_id)
   } // if(document.cookie)
}

function addItem(item_id, quantity, price)
{
   var cookie;
   var bFound = false;

   if(document.cookie)
   {
      if(document.cookie.match(item_cookie))
      {                                                                         //alert("item cookie exists: " + document.cookie);
         old_cookie = document.cookie.split(cookie_separator); // -> item=... | jsessionid=...
         var i = 0;                                                             //alert("old_cookie: " + old_cookie);
         for(i = 0; i < old_cookie.length; i++)
         {                                                                      //alert("old_cookie[" + i + "]: " + old_cookie[i]);
            if(old_cookie[i].match(item_cookie)) break;
         }                                                                      //alert("item cookie is found at " + i);
         old_cookie = old_cookie[i].split(item_cookie); // -> item= | item id:qty:price&item id:qty:price&...
                                                                                //alert("old_cookie: " + old_cookie);
         old_cookie = old_cookie[1].split(item_value_pair_separator); // -> item id:qty:price | item id:qty:price | ...
                                                                                //alert("old_cookie.length: " + old_cookie.length);
                                                                                //alert("old_cookie[0]: " + old_cookie[0]);
         var j = 0;
         arrCookie.length = 0;
         for(i = 0; i < old_cookie.length; i++)
         {
            item = old_cookie[i].split(item_value_separator);
            if(item[0] == item_id)
            {
               bFound = true;
               if(quantity > 0) // someone might enter a "0" in the textbox
               {
                  arrCookie[j] = item_id;
                  j++;
                  arrCookie[j] = quantity;
                  j++;
                  arrCookie[j] = price;
                  j++;
               }
            }
            else
            {
               arrCookie[j] = item[0];
               j++;
               arrCookie[j] = item[1];
               j++;
               arrCookie[j] = item[2];
               j++;
            }
         }
         if(!bFound)
         {
            arrCookie[j++] = item_id;
            arrCookie[j++] = quantity;
            arrCookie[j] = price;
         }
         cookie = item_cookie;
         if(arrCookie.length > 0)
         {
            cookie = cookie + arrCookie[0] + item_value_separator + arrCookie[1] + item_value_separator + arrCookie[2];
         }
         i = 3;
         while(arrCookie[i])
         {
             cookie = cookie + item_value_pair_separator + arrCookie[i++] + item_value_separator + arrCookie[i++] + item_value_separator + arrCookie[i++];
         }
         document.cookie = cookie;                                              //alert("document.cookie:  " + document.cookie);
      } // if(document.cookie.match(item_id)
      else
      {
                                                                                // alert("no item cookie: " + document.cookie);
         cookie = item_cookie;
         cookie = cookie + item_id + item_value_separator + quantity + item_value_separator + price;
         document.cookie = cookie;                                              //alert("first item added: " + document.cookie);
      }
   } // if(document.cookie)
   else
   {
                                                                                //alert("no cookie");
      cookie = item_cookie;
      cookie = cookie + item_id + item_value_separator + quantity + item_value_separator + price;
      document.cookie = cookie;
   }
   cart=window.open('step1.htm', 'cart', 'width=840, height=250, menubar=no, toolbar=no, status=no, resizable=yes, scrollbars=yes');
   cart.focus();
}

function removeItem(item_id)
{
   var cookie;

   if(document.cookie)
   {
      if(document.cookie.match(item_cookie))
      {                                                                         //alert("item cookie exists: " + document.cookie);
         old_cookie = document.cookie.split(cookie_separator); // -> item=... | jsessionid=...
         var i = 0;                                                             //alert("old_cookie: " + old_cookie);
         for(i = 0; i < old_cookie.length; i++)
         {                                                                      //alert("old_cookie[" + i + "]: " + old_cookie[i]);
            if(old_cookie[i].match(item_cookie)) break;
         }                                                                      //alert("item cookie is found at " + i);
         old_cookie = old_cookie[i].split(item_cookie); // -> item= | item id:qty:price&item id:qty:price&...
                                                                                //alert("old_cookie: " + old_cookie);
         old_cookie = old_cookie[1].split(item_value_pair_separator); // -> item id:qty:price | item id:qty:price | ...
                                                                                //alert("old_cookie.length: " + old_cookie.length);
                                                                                //alert("old_cookie[0]: " + old_cookie[0]);
         var j = 0;
         arrCookie.length = 0;
         for(i = 0; i < old_cookie.length; i++)
         {
            item = old_cookie[i].split(item_value_separator);
            if(item[0] != item_id)
            {
               arrCookie[j] = item[0];
               j++;
               arrCookie[j] = item[1];
               j++;
               arrCookie[j] = item[2];
               j++;
            }
         }
         cookie = item_cookie;
         if(arrCookie.length > 0) // could be deleted all; no item exists
         {                                                                       //alert("arrCookie.length > 0");
            cookie = cookie + arrCookie[0] + item_value_separator + arrCookie[1] + item_value_separator + arrCookie[2];
         }
         i = 3;
         while(arrCookie[i])
         {
             cookie = cookie + item_value_pair_separator + arrCookie[i++] + item_value_separator + arrCookie[i++] + item_value_separator + arrCookie[i++];
         }
         document.cookie = cookie;                                             //alert("document.cookie:  " + document.cookie);
      } // if(document.cookie.match(item_id)
   } // if(document.cookie)
}
