// JavaScript Document
<!--
function changeHidden()
{
  var whichNum = $('ADDITIONALINFO').selectedIndex;
  if (whichNum == 0)
  {
	$('PRICE').value="20.00";
	$('NAME').value="Long Sleeve";
    $('ID_NUM').value="00000001";
    //order.SHIPPING.value="7.95";
  }
  if (whichNum == 1)
  {
	$('PRICE').value="15.00";
    $('NAME').value="Short Sleeve";
    $('ID_NUM').value="00000002";
    //order.SHIPPING.value="7.95";
  }
  if (whichNum == 2)
  {
	$('PRICE').value="12.00";
    $('NAME').value="Youth Sizes Med";
    $('ID_NUM').value="00000003";
    //order.SHIPPING.value="7.95";
  }
  if (whichNum == 3)
  {
	$('PRICE').value="12.00";
    $('NAME').value="Youth Sizes Large";
    $('ID_NUM').value="00000004";
    //order.SHIPPING.value="7.95";
  }
  if (whichNum == 4)
  {
	$('PRICE').value="18.00";
    $('NAME').value="Light Blue Large";
    $('ID_NUM').value="00000005";
    //order.SHIPPING.value="7.95";
  }
  if (whichNum == 5)
  {
	$('PRICE').value="18.00";
    $('NAME').value="Light Blue X-Large";
    $('ID_NUM').value="00000006";
    //order.SHIPPING.value="7.95";
  }
}


var jerseydevills = new Item("Long Sleeve",2000,0);
var jerseydevilss = new Item("Short Sleeve",1500,0);
var jerseydevilysm = new Item("Youth Sizes Med", 1200,0);
var jerseydevilysl = new Item("Youth Sizes Large",1200,0);
var jerseydevillbssl = new Item("Light Blue Short Sleeve Large",1800,0);
var jerseydevillbssxl = new Item("Light Blue Short Sleeve X-Large",1800,0);

function purchase() // OPENS WINDOW THAT CONFIRMS PURCHASE AND SAYS THANKS
{

var porder="";
// add order array and item array
var cart = Item;
alert("CART Length: " + cart.contents.length);
if (cart.contents.length == 0) alert("can't add for zero reasons");

for (i=0; i<cart.contents.length; i++)
{

// Here is the cookie setter functions
var porder = porder + cart.contents[i].name + "||" + cart.contents[i].price + "||" + cart.contents[i].ship;
if (i<cart.contents.length-1) porder = porder + "&&";
}
alert("PORDER: " + porder);

// End cookie



purchaseWin = window.open("cartthankyou.html","","width=350,height=350,scrollbars=1,resizable=1,menubar=0,status=0,directories=0,location=0,toolbar=1");
}

function getdisplay()
{
 if (dropform.s.selectedIndex == 1)
  {
	jerseydevills.display();
  }
  if (dropform.s.selectedIndex == 2)
  {
	jerseydevilss.display();
  }
  if (dropform.s.selectedIndex == 3)
  {
 	jerseydevilysm.display();
  }
  if (dropform.s.selectedIndex == 4)
  {
	jerseydevilysl.display();
  }
  if (dropform.s.selectedIndex == 5)
  {
	jerseydevillbssl.display();
  }
  if (dropform.s.selectedIndex == 6)
  {
	jerseydevillbssxl.display();
  }
  cart.iship.focus();
}  

function Item(name,price,ship) // THE ITEM OBJECT CONSTRUCTOR FUNCTION
{
this.name = name;
this.price = price;
this.ship = ship;
this.selected = false; // VARIABLE THAT IS TURNED ON WHEN ITEM IS SELECTED
}

function toCurrency(x) // SWICHES NUMERIC VALUES TO CURRENCY STRINGS
{
if (x == 0) return "$0.00";
x = x.toString();
var pre = x.substring(0,(x.length - 2));
var post = x.substring((x.length - 2),(x.length));
return "$" + pre + "." + post;
}

// INITIALIZE CLASS VARIABLES
Item.total = 0; // TOTAL PRICE
Item.contents = new Array(); // STORE CONTENTS OF CART
Item.counter = 0; // DESIGNATE A UNIQUE INTIGER FOR INITIALIZATION OF AN ELEMENT OF ITEM.CONTENTS


Item.prototype.display = function() // METHOD TO SET OBJECT VALUES IN DISPLAY FIELDS 
{
jerseydevills.selected = false;
jerseydevilss.selected = false;
jerseydevilysm.selected = false;
jerseydevilysl.selected = false;
jerseydevillbssl.selected = false;
jerseydevillbssxl.selected = false;
document.cart.iname.value = this.name;
document.cart.iprice.value = toCurrency(this.price);
//document.cart.iship.value = this.ship;
document.cart.iship.value = 0;
this.selected = true; // TURN ON THIS VARIABLE TO SELECT IT FOR INCLUSION IN THE CART
}

Item.prototype.add = function() // METHOD TO ADD AN ITEM OBJECT TO THE CART
{
Item.total = Item.total + (this.price * cart.iship.value); // ADD TO THE TOTAL PRICE
Item.contents[Item.counter] = this; // PLACE AN ITEM INTO A NEW ARRAY ELEMENT
Item.contents[Item.counter].ship = Math.round(parseInt(cart.iship.value));
document.cart.total.value = toCurrency(Item.total); // PURELY FOR DISPLAY
document.cart.iname.value = ""; // UPDATE DISPLAY FIELDS
document.cart.iprice.value = ""; // UPDATE DISPLAY FIELDS
document.cart.iship.value = ""; // UPDATE DISPLAY FIELDS
document.dropform.s.value = "selecta";
Item.counter++ // INCREMENT COUNTER FOR NEXT ARRAY ELEMENT
}

Item.show = function() // DISPLAY WINDOW TO REVEAL CART CONTENTS
{
cartWin = window.open("showcart.html","cartWin","width=300,height=400,scrollbars=1,resizable=0,menubar=0,status=0,directories=0,location=0,toolbar=0,left=320,top=20");
cartWin.focus();
}

Item.removalOf = function(x) // REMOVE AN ITEM FROM THE CART, WHEW!
{
Item.total = Item.total - (Item.contents[x].price * Item.contents[x].ship); // REDUCE TOTAL
Item.contents[x] = ""; // VOID OUT THE ARRAY ELEMENT
document.cart.total.value = toCurrency(Item.total); // UPDATE DISPLAY
Item.contents.sort(function (a,b) // RESORT OUR ARRAY
   {
   if (a == "") a = 1; else a = 0;
   if (b == "") b = 1; else b = 0;
   return a - b;
   });
var holdPoint = Item.contents.length;
for (i=0; i<Item.contents.length; i++) // DESIGNATE WHERE EMPTY ELEMENTS BEGIN
   {
   if (Item.contents[i] == "") { holdPoint = i; break; }
   }
Item.contents = Item.contents.slice(0,holdPoint); // DISCARD EMPTY ELEMENTS
Item.counter--; // DECREMENT COUNTER TO MATCH BACK UP TO ARRAY
}

Item.addToCart = function(ship) // FIGURE OUT WHICH ITEM IS SELECTED
{

// Check for int or NaN
zerot = parseInt(ship);
//zerot = parseInt(cart.iship.value);
zerot = Math.round(zerot);

NotNum = isNaN(zerot);

if (zerot == "0" || NotNum) 
{
  alert("User must enter a numeric quantity to add item to cart");
  return true;
}  

// Get cookie 

if (jerseydevills.selected) { jerseydevills.add(zerot); jerseydevills.selected = false; }
if (jerseydevilss.selected) { jerseydevilss.add(zerot); jerseydevilss.selected = false; }
if (jerseydevilysm.selected) { jerseydevilysm.add(zerot); jerseydevilysm.selected = false; }
if (jerseydevilysl.selected) { jerseydevilysl.add(zerot); jerseydevilysl.selected = false; }
if (jerseydevillbssl.selected) { jerseydevillbssl.add(zerot); jerseydevillbssl.selected = false; }
if (jerseydevillbssxl.selected) { jerseydevillbssxl.add(zerot); jerseydevillbssxl.selected = false; }

// Set cookie + new order 
var cart = Item;
var porder = "";

// Here is the cookie setter functions 
var name = "test";
var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) )
	{
		return null;
	}
	if ( start == -1 ) return null;
	
	var end = document.cookie.indexOf( ";", len );
	if ( end == -1 ) end = document.cookie.length;
	cookie_stuff = unescape( document.cookie.substring( len, end ) );
	alert("CCOOKIE " + cookie_stuff);
	order = cookie_stuff.split("&&");
	for (i=0; i<order.length; i++)
    {
	  alert("Order: "+order [i]);
	}

if (i<cart.contents.length-1) porder = porder + "&&";
Set_Cookie( 'test', porder, '', '/', '', '' );
alert("PORDER Me: " + porder);

}

// this function gets the cookie, if it exists
function Get_Cookie( name ) {
	
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) )
	{
		return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ";", len );
	if ( end == -1 ) end = document.cookie.length;
	cookie_stuff = unescape( document.cookie.substring( len, end ) );
	order = cookie_stuff.split("&&");
	for (i=0; i<order.length; i++)
    {
	  alert("Order: "+order [i]);
	}  
}

/*
only the first 2 parameters are required, the cookie name, the cookie
value. Cookie time is in milliseconds, so the below expires will make the 
number you pass in the Set_Cookie function call the number of days the cookie
lasts, if you want it to be hours or minutes, just get rid of 24 and 60.

Generally you don't need to worry about domain, path or secure for most applications
so unless you need that, leave those parameters blank in the function call.
*/
function Set_Cookie( name, value, expires, path, domain, secure ) {
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );
	// if the expires variable is set, make the correct expires time, the
	// current script below will set it for x number of days, to make it
	// for hours, delete * 24, for minutes, delete * 60 * 24
	if ( expires )
	{
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );

	document.cookie = name + "=" +escape( value ) +
		( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + //expires.toGMTString()
		( ( path ) ? ";path=" + path : "" ) + 
		( ( domain ) ? ";domain=" + domain : "" ) +
		( ( secure ) ? ";secure" : "" );
}

// this deletes the cookie when called
function Delete_Cookie( name, path, domain ) {
	if ( Get_Cookie( name ) ) document.cookie = name + "=" +
			( ( path ) ? ";path=" + path : "") +
			( ( domain ) ? ";domain=" + domain : "" ) +
			";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}


// remember, these are the possible parameters for Set_Cookie:
// name, value, expires, path, domain, secure
function proc_cookie()
{ 
  var prorder = cart.iname.value + "&&" + cart.iprice.value + "&&" + cart.iship.value + "&&" + cart.total.value;
  Set_Cookie( 'test', prorder, '', '/', '', '' );
  if ( Get_Cookie( 'test' ) ) alert( Get_Cookie('test'));
  // and these are the parameters for Delete_Cookie:
  // name, path, domain
  // make sure you use the same parameters in Set and Delete Cookie.
  // Delete_Cookie('test', '/', '');
   ( Get_Cookie( 'test' ) ) ? alert( Get_Cookie('test')) : 
  alert( 'it is gone');
}

//-->