//1item2.js--modified to hold ship_pts
//use add2 function in htm page to call add, then recall page
//fixed the ship pt adding problem 1/02
//modified to drop cookie in a particular path for all browser pages

//script to add an item to the cart
function add2( what, bkmrk)
{
//not adding bookmark at this time
curdoc = ""
curdoc1 = ""
num1= 0
num2 = 0
curdoc = location.href
if (curdoc.length > 0)
{
 num1 = curdoc.indexOf("#",1)
}
//see if there's already a book mark
if (num1 > 0)
{ 
    curdoc1 = curdoc.substring(0,num1)
    curdoc = curdoc1
//   curdoc = curdoc1 + "#" + bkmrk
}
//else
//{  curdoc1 = curdoc
//curdoc = curdoc1 + "#" + bkmrk 
//window.alert("new curdoc is: " + curdoc)
//}
str1=what;
add(str1);
window.alert("Item has been added to cart. Thank You");
location.href = curdoc
}


var qq=0;
var parc=new Array();
var j=0;
var crud=""
var car=0; 
var begin=0;
var end=0;
var price=0;
var stock;
var howmany;
var booky;
var shippts;

start();

function person(booktit,price,stock,quantity,shippts)
{
this.quantity=quantity
this.booktit=booktit
this.stock=stock
this.price=price
this.shippts=shippts
}

var i=0;
var book="|";

//looks like it's initializing the cookie; adding the specified string to the cookie

function add(what)
{
//changed these lines to set path and domain
//original line below
//document.cookie="cook=yay; "+"expires= "+new Date();
//here we set the string called yay to the entire value, which is called cook
//the cookie will not expire until 2050
//document.cookie="cook=yay; "+"expires=31-Dec-2050 00:00:00 GMT;  "+"domain=mandarava.com; "+"path=/retail; " ;
document.cookie="cook=yay; expires=31-Dec-2050 00:00:00 GMT; domain=mandarava.com; path=/retail; " ;
//"secure "
//document.write()

 book+=what
//this takes the entire string yay (or cook, really) above and concatenates the order values to it
 document.cookie="cook="+book+";"
 	cookie=document.cookie 
 	while (crud!="true")
	{
//moved the line below outside of while loop
//	cookie=document.cookie
//the while loop is unnecessary because it never loops
	
	begin=cookie.indexOf("|",end);
	end=cookie.indexOf("|",begin+1)
//item title
	booky=cookie.substring(begin+1,end)

	  if (end==-1 || end=="")
	  crud="true"

	begin=cookie.indexOf("|",end);
	end=cookie.indexOf("|",begin+1)
//price per item
	price=cookie.substring(begin+1,end)
	
	begin=cookie.indexOf("|",end);
	end=cookie.indexOf("|",begin+1)
//item id
	stock=cookie.substring(begin+1,end)

	begin=cookie.indexOf("|",end);
	end=cookie.indexOf("|",begin+1)
//quantity of item
	howmany=cookie.substring(begin+1,end)

	begin=cookie.indexOf("|",end);
	end=cookie.indexOf("|",begin+1)
//shippts per item
	shippts=cookie.substring(begin+1,end)

	  if (crud!="true")
	   {
	    parc[j]=new person(booky,price,stock,howmany,shippts)
	    j++
           }
        } //end of while

//looking for matching stock ids to combine identical ones
//NOTE:  different items MUST have different stock ids

 for (i=0; i<j; i++)
 {
  for (z=i+1; z<j; z++)
   {
    if (parc[i].stock==parc[z].stock)
     {
      var temp=parseInt(parc[z].quantity)
      var temp2=parseInt(parc[i].quantity)
     var temp3=parseInt(parc[z].shippts)
      var temp4=parseInt(parc[i].shippts)
      temp2+=temp;
      temp4+=temp3;
      parc[z].quantity=temp2;
//changed line below from temp4 to temp3--THIS FIXED IT
      parc[z].shippts=temp3;
      parc[i].stock="zip";
//    parc[i].shippts="zip";
     }
   }
 }

document.cookie="cook=";
 book="|"

 for (i=0; i<j; i++)
 {
  if (parc[i].stock!="zip")
   book+=parc[i].booktit+"|"+parc[i].price+"|"+parc[i].stock+"|"+parc[i].quantity+"|" +parc[i].shippts+"|"
 }

if (document.cookie!="") document.cookie="cook="+book+" path='/';"
}
function start()
{
i=0;
var cookie=document.cookie;
var crud="";
var end=0;

while (crud!="true")
{
begin=cookie.indexOf("|",end);
end=cookie.indexOf("|",begin+1)
//item title
book=cookie.substring(begin+1,end)

if (end==-1 || end=="")
crud="true"

begin=cookie.indexOf("|",end);
end=cookie.indexOf("|",begin+1)
//price
price=cookie.substring(begin+1,end)

begin=cookie.indexOf("|",end);
end=cookie.indexOf("|",begin+1)
//item id
stock=cookie.substring(begin+1,end)

begin=cookie.indexOf("|",end);
end=cookie.indexOf("|",begin+1)
//quantity
howmany=cookie.substring(begin+1,end)

begin=cookie.indexOf("|",end);
end=cookie.indexOf("|",begin+1)
//shipping pts
shippts=cookie.substring(begin+1,end)

if (crud !="true")
{
 parc[j]=new person(book,price,stock,howmany,shippts);
 j++;
}

} //end of while
}
