// coolaff.js
function GetSizes(Frm_Num) {
	//make sure the cookies are there
	var itemspcf;
	for (var i=1; i < 6; i++) {
		itemspcf = "item" + i;
		if (GetCookie(itemspcf) === null) {
			SetCookie (itemspcf, 0,null,"/");
			//alert("set cookie" + itemspcf + " to zero!!!");
		}
		itemspcf = "size" + i;
		if (GetCookie(itemspcf) === null) {
			SetCookie (itemspcf, 0,null,"/");
			//alert("set cookie" + itemspcf + " to zero!!!");
		}
		itemspcf = "quantity" + i;
		if (GetCookie(itemspcf) === null) {
			SetCookie (itemspcf, 0,null,"/");
			//alert("set cookie" + itemspcf + " to zero!!!");
		}
	}
	var size_ch2 = " ";
	var size_ch1;
	size_ch1 = document.forms[Frm_Num].size1.options[document.forms[Frm_Num].size1.selectedIndex].text;
	if (document.forms[Frm_Num].size2 === undefined || document.forms[Frm_Num].size2.value == "") {
		//alert("one size");
		size_ch2 = " ";
	} else {
		//alert("two sizes");
		size_ch2 = "-" + document.forms[Frm_Num].size2.options[document.forms[Frm_Num].size2.selectedIndex].text;
	}

	//get the size(s) and set the cookie
	var curr_cart = GetCookie("cart_num");
	if (curr_cart === null) { curr_cart = 1; }

	var size_num = "";
	if (curr_cart >= 1 && curr_cart <= 5) {
		size_num = "size" + curr_cart;
		var my_size = size_ch1 + size_ch2;
		SetCookie(size_num, my_size, null,"/");
	}
}


function SetButtons() {
	//THIS IS NOT BE NEEDED NOW THAT NETSCAPE IS FIXED
	if(navigator.appName == "Netscape") {
		for (var i=0; i < document.images.length; i++) {
			var j = document.images[i].src;
			var k = document.images[i].src.length;

			if(document.images[i].src.lastIndexOf("buybutton.gif", k)> 1) {
				document.images[i].src ="netscape.gif";
			}
		}
	}
}


function ChkBrowser() {
	document.location = "prodcart.php";
}


function GtRef() {
	//get the referring document
	var now = new Date();
	FixCookieDate(now);
	now.setTime(now.getTime() + 1 * 24 * 60 * 60 * 1000);

	//added lines above for date
		var csdocref
		csdocref = "myrefdoc"
	if (GetCookie(csdocref)== null) {
		SetCookie (csdocref,document.referrer,now,"/");
		var mycookie3 = GetCookie(csdocref)
		//alert("You clicked from " + mycookie3)
	} else {
		var mycookie3 = GetCookie(csdocref)
	}
}


function GtCkies() {
	GtRef();
	//clear cc if user clicks back
	//took out 2-2-03
	//document.form1.cc_num.value=""
	//check for null cookies

	var itemspcf
	for (var i=1; i < 6; i++) {
		itemspcf = "item" + i
		if (GetCookie(itemspcf)== null){
			SetCookie (itemspcf, 0,null,"/");
		}
		itemspcf = "size" + i
		if (GetCookie(itemspcf)== null){
		SetCookie (itemspcf, 0,null,"/");
		}
		itemspcf = "quantity" + i
		if (GetCookie(itemspcf)== null){
		SetCookie (itemspcf, 0,null,"/");
		}
	}
	//check the browser
	//ChkBrowser()
	//now.....get the items
	get_items()
	for (var i=1; i < 6; i++) {
		update(i)
	}
}


// Detail page calls this to add an item to the shopping cart.
// This takes you to the shopping cart page after.
function setdata(option_num, size_num) { // option_num is the product number, size_num is the form number
	//get and set the size value
	GetSizes(size_num);
	if(option_num <= 0) {
		alert("Please select valid styles/sizes from drop down menus, then click buy");
		return;
	}

	//get rid of the temporary value on the color cookie
	SetCookie ("col_cookie", 0,null,"/");
	var expdate = new Date();
	FixCookieDate (expdate); // Correct for Mac date bug
	expdate.setTime (expdate.getTime() + (24 * 60 * 60 * 1000)); // 24 hrs from now

	//find and add to the cart_num
	var curr_cart_num = GetCookie("cart_num")
	//alert("first item is " + curr_cart_num)
	if (curr_cart_num == null) {
		curr_cart_num = 1
		//alert("now the value is " + curr_cart_num)
	}
	if (curr_cart_num >0) {
		var ncart_num = eval(curr_cart_num) + eval(1)
		//alert("added 1 to num it is now " + ncart_num)
	}

	SetCookie ("cart_num", ncart_num,null,"/");
	//alert("from setdata current cart Number is " + ncart_num)

	for (var i=1; i < 6; i++) {
		if (curr_cart_num==i){
			var item_num = ("item" + i)
			var quant_num = ("quantity" + i)
		}
		// NOW add 1 to the cart number
		SetCookie ("cart_num", ncart_num,null,"/");
	}
	SetCookie(item_num, option_num,null,"/");

	//Check quantity if == 0 make it 1
	var quant_chk = GetCookie(quant_num)
	if (quant_chk == "0"){
		SetCookie(quant_num, 1,null,"/");
	}

	//go to order page
	ChkBrowser()
}


// Detail page calls this to add ALL SELECTED items to the shopping cart.
// We then take you to the shopping cart.
// option_num is the product id number, form_num is the HTML form number
// Code from GetSizes() integrated into here, because cart_num may change by more than 1 now.
function setdetail() {
	var qtyprocessed = 0;

	// Make sure the cookies are there
	var itemspcf;
	for (var i=1; i < 6; i++) {
		itemspcf = "item" + i;
		if (GetCookie(itemspcf) === null) {
			SetCookie (itemspcf, 0,null,"/");
			//alert("set cookie" + itemspcf + " to zero!!!");
		}
		itemspcf = "size" + i;
		if (GetCookie(itemspcf) === null) {
			SetCookie (itemspcf, 0,null,"/");
			//alert("set cookie" + itemspcf + " to zero!!!");
		}
		itemspcf = "quantity" + i;
		if (GetCookie(itemspcf) === null) {
			SetCookie (itemspcf, 0,null,"/");
			//alert("set cookie" + itemspcf + " to zero!!!");
		}
	}

	// Get next empty item slot in the cart
	var cart_num = GetCookie("cart_num");
	if (cart_num === null) { cart_num = 1; }

	// Each form on the page has a separate item to be added to the cart.
	for (var form_num=0; form_num<document.forms.length; form_num++) {
//alert("processing form " + form_num + ", selectedIndex=" + document.forms[form_num].size1.selectedIndex);

		// Read size of tops from web form
		// If there's a second size (i.e. bottoms), read that also
		var size1exists = (document.forms[form_num].size1 !== undefined);
		if (size1exists) {
			var size1index = document.forms[form_num].size1.selectedIndex;
			var size1value = document.forms[form_num].size1.options[size1index].text;
			var qty1index = document.forms[form_num].qty1.selectedIndex;
			var qty1value = document.forms[form_num].qty1.options[qty1index].text;
			if (size1index > 0 && (qty1value == '' || qty1value == 'Qty' || qty1value < 1)) {
				alert("Please select a quantity for this item.");
				document.forms[form_num].qty1.focus();
				return;
			}
//alert("size1 selectedIndex=" + size1index + ", value=" + size1value);
		}
		var size2exists = (document.forms[form_num].size2 !== undefined);
		if (size2exists) {
			var size2index = document.forms[form_num].size2.selectedIndex;
			var size2value = document.forms[form_num].size2.options[size2index].text;
//alert("size2 selectedIndex=" + size2index + ", value=" + size2value);
		}

		// If both sizes exist and one selected but not the other, error.
		if (size1exists && size2exists) {
			if (size1index == 0 && size2index != 0) {
				alert("Please select a top size to go with this bottom.");
				document.forms[form_num].size1.focus();
				return;
			}
			else if (size2index == 0 && size1index != 0) {
				alert("Please select a bottom size to go with this top.");
				document.forms[form_num].size2.focus();
				return;
			}
		}

		// If they chose this item, put it in cart.
		if (size1exists && size1index > 0) {
			qtyprocessed++;

			var option_num;
			option_num = document.forms[form_num].webitemid.value;
			if(option_num <= 0) { continue; }		// we can't process this one without knowing the WEBITEMID

			// Get rid of the temporary value on the color cookie
			SetCookie("col_cookie", 0,null,"/");

			// Does this really do anything?:
			var expdate = new Date();
			FixCookieDate (expdate); // Correct for Mac date bug
			expdate.setTime (expdate.getTime() + (24 * 60 * 60 * 1000)); // 24 hrs from now

			// store size(s)
			var sizevalue = size1value;
			if (size2exists) {
				sizevalue += "-" + size2value;
			}

			// Add item to cart, only if there's room for it
			if (cart_num >= 1 && cart_num <= 5) {
				var item_num = "item" + cart_num;
				SetCookie(item_num, option_num,null,"/");

				var size_num = "size" + cart_num;
				SetCookie(size_num, sizevalue,null,"/");

				// If quantity in cart is less than qty on form, set it to qty on form.
				var quant_num = "quantity" + cart_num;
				var quant_chk = GetCookie(quant_num);
				if (quant_chk < qty1value) {
					SetCookie(quant_num, qty1value,null,"/");
				}
			}

			cart_num++;
		}
	}

	if (qtyprocessed < 1) {
		alert("Please select styles/sizes to continue.");
		return;
	}

	// Remember how many line items we just used up
	SetCookie("cart_num", cart_num, null, "/");

	//go to order page
	ChkBrowser();
}


//***************SET AND GET THE VALUES*********************************************
function setsize(ans_num) {
	tf = document.form1;
	//set set all the initial cookie values here
	//set quantity
	SetCookie("quantity1", tf.quantity1.selectedIndex,null,"/");
	SetCookie("quantity2", tf.quantity2.selectedIndex,null,"/");
	SetCookie("quantity3", tf.quantity3.selectedIndex,null,"/");
	SetCookie("quantity4", tf.quantity4.selectedIndex,null,"/");
	SetCookie("quantity5", tf.quantity5.selectedIndex,null,"/");
	//set item
	SetCookie("item1", tf.menu1.options.selectedIndex,null,"/");
	SetCookie("item2", tf.menu2.options.selectedIndex,null,"/");
	SetCookie("item3", tf.menu3.options.selectedIndex,null,"/");
	SetCookie("item4", tf.menu4.options.selectedIndex,null,"/");
	SetCookie("item5", tf.menu5.options.selectedIndex,null,"/");
	//set size
	SetCookie("size1", tf.size1.value,null,"/");
	SetCookie("size2", tf.size2.value,null,"/");
	SetCookie("size3", tf.size3.value,null,"/");
	SetCookie("size4", tf.size4.value,null,"/");
	SetCookie("size5", tf.size5.value,null,"/");


	//also update cart_num
	tf = document.form1;
	//check out the cart num
	var cart_tag = 0;
	if (tf.menu1.options.selectedIndex > 0) {
		cart_tag = eval(cart_tag + 1);
	}
	if (tf.menu2.options.selectedIndex > 0) {
		cart_tag = eval(cart_tag + 1);
	}
	if (tf.menu3.options.selectedIndex > 0) {
		cart_tag = eval(cart_tag + 1);
	}
	if (tf.menu4.options.selectedIndex > 0) {
		cart_tag = eval(cart_tag + 1);
	}
	if (tf.menu5.options.selectedIndex > 0) {
		cart_tag = eval(cart_tag + 1);
	}

	var ncart_num = eval(cart_tag) + eval(1);
	SetCookie ("cart_num", ncart_num,null,"/");

	if (ans_num == 1) {
		history.back();
	}
}

//***************************************************************


function get_items(){
tf = document.form1

//SET PRODUCT
tf.menu1.options[GetCookie("item1")].selected = true
tf.menu2.options[GetCookie("item2")].selected = true
tf.menu3.options[GetCookie("item3")].selected = true
tf.menu4.options[GetCookie("item4")].selected = true
tf.menu5.options[GetCookie("item5")].selected = true

//SET QUANTITY
//wm = window.parent.hiddenfra.frmhide

tf.quantity1.options[GetCookie("quantity1")].selected = true
tf.quantity2.options[GetCookie("quantity2")].selected = true
tf.quantity3.options[GetCookie("quantity3")].selected = true
tf.quantity4.options[GetCookie("quantity4")].selected = true
tf.quantity5.options[GetCookie("quantity5")].selected = true
//SET SIZE

tf.size1.value = GetCookie("size1")
tf.size2.value = GetCookie("size2")
tf.size3.value = GetCookie("size3")
tf.size4.value = GetCookie("size4")
tf.size5.value = GetCookie("size5")

		}

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function del_item1(item_num){

//first set all the values that are there
setsize(0)
//change the cart number
var currcart= GetCookie("cart_num")
var nccart = eval(currcart -1)
SetCookie ("cart_num", nccart,null,"/");

//del item 1 move others up
for (var j=1; j < 6; i++) {
if(item_num==j){
for (var i=j; i < 6; i++) {
var gval = "item" + i, sval = "item" + eval(i+1), nval = GetCookie(sval)
SetCookie (gval,nval ,null,"/");
var gval = "size" + i, sval = "size" + eval(i+1), nval = GetCookie(sval)
SetCookie (gval,nval ,null,"/");
var gval = "quantity" + i, sval = "quantity" + eval(i+1), nval = GetCookie(sval)
SetCookie (gval,nval ,null,"/");
if (i==5){
SetCookie (gval,0 ,null,"/");
}
}
}
if(j==1){
document.form1.menu1.options[0].selected = true
}
if(j==2){
document.form1.menu2.options[0].selected = true
}
if(j==3){
document.form1.menu3.options[0].selected = true
}
if(j==4){
document.form1.menu4.options[0].selected = true
}
if(j==5){
document.form1.menu5.options[0].selected = true
}

}

//*********************************
setsize(0)
get_items()
update(item_num)
}

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//DELETE ITEMS
function del_item(item_num){
//I know it ain't pretty but it works :)
//first set all the values that are there
setsize(0)


//change the cart number
var currcart= GetCookie("cart_num")
var nccart = eval(currcart -1)
SetCookie ("cart_num", nccart,null,"/");


//del item 1 move others up
if(item_num==1){
document.form1.menu1.options[0].selected = true
SetCookie("item1", GetCookie("item2"),null,"/");
SetCookie("quantity1", GetCookie("quantity2"),null,"/");
SetCookie("size1", GetCookie("size2"),null,"/");
SetCookie("item2", GetCookie("item3"),null,"/");
SetCookie("quantity2", GetCookie("quantity3"),null,"/");
SetCookie("size2", GetCookie("size3"),null,"/");
SetCookie("item3", GetCookie("item4"),null,"/");
SetCookie("quantity3", GetCookie("quantity4"),null,"/");
SetCookie("size3", GetCookie("size4"),null,"/");
SetCookie("item4", GetCookie("item5"),null,"/");
SetCookie("quantity4", GetCookie("quantity5"),null,"/");
SetCookie("size4", GetCookie("size5"),null,"/");
SetCookie("item5", "0",null,"/");
SetCookie("quantity5", "0",null,"/");
SetCookie("size5", "0",null,"/");

}
//del item 2 move others up
document.form1.menu2.options[0].selected = true
if(item_num==2){
SetCookie("item2", GetCookie("item3"),null,"/");
SetCookie("quantity2", GetCookie("quantity3"),null,"/");
SetCookie("size2", GetCookie("size3"),null,"/");
SetCookie("item3", GetCookie("item4"),null,"/");
SetCookie("quantity3", GetCookie("quantity4"),null,"/");
SetCookie("size3", GetCookie("size4"),null,"/");
SetCookie("item4", GetCookie("item5"),null,"/");
SetCookie("quantity4", GetCookie("quantity5"),null,"/");
SetCookie("size4", GetCookie("size5"),null,"/");
SetCookie("item5", "0",null,"/");
SetCookie("quantity5", "0",null,"/");
SetCookie("size5", "0",null,"/");
}
//del item 3 move others up
document.form1.menu3.options[0].selected = true
if(item_num==3){
SetCookie("item3", GetCookie("item4"),null,"/");
SetCookie("quantity3", GetCookie("quantity4"),null,"/");
SetCookie("size3", GetCookie("size4"),null,"/");
SetCookie("item4", GetCookie("item5"),null,"/");
SetCookie("quantity4", GetCookie("quantity5"),null,"/");
SetCookie("size4", GetCookie("size5"),null,"/");
SetCookie("item5", "0",null,"/");
SetCookie("quantity5", "0",null,"/");
SetCookie("size5", "0",null,"/");
}
//del item 4 move others up
document.form1.menu4.options[0].selected = true
if(item_num==4){
SetCookie("item4", GetCookie("item5"),null,"/");
SetCookie("quantity4", GetCookie("quantity5"),null,"/");
SetCookie("size4", GetCookie("size5"),null,"/");
SetCookie("item5", "0",null,"/");
SetCookie("quantity5", "0",null,"/");
SetCookie("size5", "0",null,"/");
}
//del item 5 move others up
document.form1.menu5.options[0].selected = true
if(item_num==5){
	//FIXED THIS ERROR ON 5-28-01
SetCookie("item5", "0",null,"/");
SetCookie("quantity5", "0",null,"/");
SetCookie("size5", "0",null,"/");
				}
				//update what we did
		tf = document.form1
tf.menu1.options[GetCookie("item1")].selected = true
tf.menu2.options[GetCookie("item2")].selected = true
tf.menu3.options[GetCookie("item3")].selected = true
tf.menu4.options[GetCookie("item4")].selected = true
tf.menu5.options[GetCookie("item5")].selected = true
//QTY
tf.quantity1.options[GetCookie("quantity1")].selected = true
tf.quantity2.options[GetCookie("quantity2")].selected = true
tf.quantity3.options[GetCookie("quantity3")].selected = true
tf.quantity4.options[GetCookie("quantity4")].selected = true
tf.quantity5.options[GetCookie("quantity5")].selected = true
//SIZE
tf.size1.value =GetCookie("size1")
tf.size2.value =GetCookie("size2")
tf.size3.value =GetCookie("size3")
tf.size4.value =GetCookie("size4")
tf.size5.value =GetCookie("size5")

//*********************************
setsize(0)
get_items()
for (var i=1; i < 6; i++) {
update(i)
}
}


//***************COOKIE STUFF HERE
function getCookieVal (offset) {
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1)
    endstr = document.cookie.length;
  return unescape(document.cookie.substring(offset, endstr));
}
function FixCookieDate (date) {
  var base = new Date(0);
  var skew = base.getTime();
  if (skew > 0)
    date.setTime (date.getTime() - skew);
}
function GetCookie (name) {
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  while (i < clen) {
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg)
      return getCookieVal (j);
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break;
  }
  return null;
}
function SetCookie (name,value,expires,path,domain,secure) {
  document.cookie = name + "=" + escape (value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
}

function DeleteCookie (name,path,domain) {
  if (GetCookie(name)) {
    document.cookie = name + "=" +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}
//*************END COOKIE STUFF
