var deletion = 0;
function  updateorder(form,page)
{
 //add the item to the order and go to the display order page


   mycookies=new Array(20);
   var i=0;
   var startpoint=0;

   shortdesc=form.elements[0].value;
   price=form.elements[1].value;
   quantity=form.elements[2].value;
   productcode=form.elements[3].value;
 
	
   //  validate the contents of the quantity field (numeric and positive)
   
   qlen = quantity.length;
   ilen = qlen - 1;
   invalidq = 0;
   numbers = "0123456789";
   
   
   for (count=0; count<=ilen; count++)
   {
		digit = quantity.substr(count,1);
		if (numbers.indexOf(digit) == -1)
		{
			invalidq = 1;	
		}
   }
   

   
   
   if (invalidq )
   {
		location.reload();		
		if (invalidq)
		{
			alert('Invalid quantity, please input a positive integer');
		}
   }   
   else
   {
   deleteitem = 0;
   delquantity = 0;
   donothing = 0;
   	
   if (deletion)
   {
		if (quantity != 0)
		{
			quantity = quantity * (-1);
		}
   }
   
   workanswer = quantity;

   
   if (document.cookie == "")
   {
		if (quantity > 0)
		{
			
			
			$thisitem = quantity + "+" + shortdesc + "+" + price + "+" + productcode + "+" + "endofitem "; 
  
			$expiration = new Date((new Date()).getTime() + 240*3600000);

			document.cookie = "GE=" + $thisitem + "; expires=" + $expiration.toGMTString(); 

			location = "vieworder.htm";		
		}
   }
   else
   {	   

		whole_len=document.cookie.length;
		while(whole_len!=startpoint && i< 20)
		{
			if ((temlen=document.cookie.indexOf(";",startpoint))!=-1)
			{
				mycookies[i]=document.cookie.substring(startpoint,temlen);
				i++;
				startpoint=temlen+1
			}
			else if (startpoint != whole_len)
			{
			 mycookies[i]=document.cookie.substring(startpoint,whole_len);
			 i++;
			 startpoint=whole_len;
			}
		}

		for (j=0;j<i;j++)
		{
			//filter out cookies that are not used by GEo
			if((mycookies[j].substring(0,2)=="GE")||(mycookies[j].substring(0,3)==" GE"))
			{
				// if the item already exists adjust the quantity if not
				// add the new item onto the end of the cookie and replace it
				acookie = mycookies[j];
				clen = acookie.length;
				
				itemexists = 0;
				
				data= acookie.substring(3,clen);
    
   				items = data.split("endofitem");
		
				// how many items? store in count
		
				start = 0;
				end = data.length - 8;
				count = 0;
		
				while (items[count] != "")
				{
						count++;
				}	 
				position = 0;
				for (k=0; k<count; k++)
				{
					anitem = items[k].split("+");
					$itemquantity = anitem[0];
					$itemshortdesc = anitem[1];
					$itemprice = anitem[2];
					$productcode =	anitem[3];				
					
					
					if ($productcode == productcode)
					{
						position = k;
						itemexists = 1;
						workanswer = quantity - 0;
						workquantity = anitem[0] - 0;
						anitem[0] = workquantity + workanswer;
						items[k] = anitem.join("+");
					}
				}
	
	
				if (itemexists)
				{
					if (quantity == 0 || workanswer <= 0)
					{
					
						deletion = 1;
						// remove the item and display a warning
						items[position] = "";
						if (count == 1)
						{
							// delete the cookie as nothing left
							deleteitem = 1;	
							data = items.join("endofitem");
							$thisitem = data;
						}
						else
						{
							data = "";
							for (k=0; k<count; k++)
							{
								if (k != position)
								{
									data = data + items[k] + "endofitem";
								
								}						
							}
							$thisitem = data;
						}
					}
					else
					{
						data = items.join("endofitem");
						$thisitem = data;
					}
				}
				else
				{
					if (quantity == 0 || workanswer <= 0)
					{
						donothing = 1;
					}
					else
					{
						$thisitem = data + quantity + "+" +  shortdesc + "+" + price + "+" + productcode + "+" + "endofitem";
					}
				}
				 
				
				if (deleteitem)
				{
					$expiration = new Date((new Date()).getTime() - 10*3600000);
					document.cookie =  "GE=" + $thisitem + "; expires=" + $expiration.toGMTString(); 
					location="vieworder.htm";
					alert('The item has been removed from your order.');
				}
				else
				{
					if (!donothing)
					{ 
						$expiration = new Date((new Date()).getTime() + 10*3600000);
						document.cookie =  "GE=" + $thisitem + "; expires=" + $expiration.toGMTString(); 
						location="vieworder.htm";
						if (deletion)
						{
							alert('The item has been removed from your order.');
						}
					}
					else
					{
						location="vieworder.htm";
					}
				}
				

			}
        } 
    }  
    }  
}


function  delitem(form,page)
{ 
	//delete from order and go to a new page
	
	deletion = 1; 
	updateorder(form,page);
	    
}

