// Javascript source file for Add-To-Order / Cookie functions
function swap_img(image) {
	document.all.big_image.src="pictures/"+image;
}
function AddToOrder(item,qty,price,name,partnum,cat,subcat) {
	if (name.indexOf("(Select)") > -1) { alert("Please Select an Option before ordering !");  }
	else {
		var Data = GetCookie("OrderData");
		var Lines = new Array;
		var Fields  = new Array;
		var Items = new Array;
		var Qtys = new Array;	
		var Prices = new Array;
		var Names = new Array;
		var Parts = new Array;
		var Count = 0;
		var l = 0, q = 0;
		var exists = false; 
		//alert("AddToOrder(A): cat="+cat+", subcat="+subcat+", opt="+name+"");
		var MyLine = "", MyItem = "", MyQty = 0, MyPrice = 0, MyName="";
		if (Data != null && Data != "") { // Yes, There's Data
			var i = Data.indexOf("path=/")
			if (i > -1) { Data = Data.substring(0,i); }
			//alert("AddToOrder(): Existing Data = '" + Data + "'");
			Lines = Data.split("~"); // Entries Tilde Separated
			for (l=0;l<Lines.length;l++) {
				MyLine = Lines[l];
				Fields = MyLine.split(",")
				if (Fields.length > 2) {
					Items[Count] = Fields[0];
					Qtys[Count] = Fields[1];
					Prices[Count] = Fields[2];
					Names[Count] = Fields[3];
					Parts[Count] = Fields[4];
					if (item == Items[Count] && name == Names[Count] && partnum == Parts[Count]) {
						//qty = prompt("You already have " + Qtys[Count] + " in your cart\nHow many would you like to order?",Qtys[Count]);
						alert("Adding another " + qty + " to existing Qty of " + Qtys[Count] + " for Item# " + item + " (Line " + Count + ")");
						q = parseInt(Qtys[Count]) + parseInt(qty);
						Qtys[Count] = q; exists = true;
					}
					Count ++;
				} // end of if there's enough fields
			}	// end of for each data line
			//alert("" + Count + " Existing Shopping Cart Items !");
		} else { // end of if there's data
			//alert("No Existing Shopping Cart Data !");
		}
		if (exists == false) { // not already in cart, Add it
			//alert("Adding New Line " + Count + ": " + qty + "  of Item# " + item + " at $" + price);
			//qty = prompt("How many would you like to order?",qty);
			Items[Count] = item;
			Qtys[Count] = qty;
			Prices[Count] = price;
			Names[Count] = name;
			Parts[Count] = partnum;
			Count ++;
		} 
		// Now Re-Write the Cookie Data
		Data = ""
		for (l=0;l<Count;l++) {
			Data += Items[l] + "," + Qtys[l] + "," + Prices[l] + "," + Names[l] + "," + Parts[l] ;
			if ((l + 1) < Count) { Data += "~"; }
		}
		//alert("AddToOrder(): New Data = '" + Data + "'");
		SetCookie("OrderData",Data);
		SetCookie("Category", cat);
		SetCookie("Subcategory", subcat);
		//alert("Shopping Cart Updated: " + Data);
		window.status = "Shopping Cart Updated !";
		//alert("AddToOrder(B): cat="+cat+", subcat="+subcat+"");
		var url="index.php?page=cart&cat=" + cat + "&subcat=" + subcat + "&id=" + item + "&Random=" + Math.random();
		window.open(url, TARGET="_top");
	}
}
//-------------------------------------------------------
function get_options(index) {
	var name1="", name2="", opt1="", opt2="", rval="";
	if (index == undefined) {
		//alert("get_options(): No Index");
		name1="options1";
		name2="options2";
	} else {
		//alert("get_options(): Index=" + index);
		name1="options1_"+index+"";
		name2="options2_"+index+"";
	}
	//alert("name1="+name1+", name2="+name2+"");
	try {
		var sel1 = document.getElementById(name1);
		opt1=sel1.options[sel1.selectedIndex].value;
		rval = opt1;
	} catch(e) { ; }
	try {
		var sel2 = document.getElementById(name2);
		opt2=sel2.options[sel2.selectedIndex].value;
		rval = rval + ", " + opt2;
	} catch(e) { ; }
	return rval;
}
//-------------------------------------------------------
//-------------------------------------------------------
// cookie-handling functions 
//-------------------------------------------------------
function GetCookie (name) {
	var arg = name + "=",alen=arg.length,clen=document.cookie.length,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;
} // end of GetCookie() function
//---------------------------------------------------
function SetCookie (name, value) {
	  var argv = SetCookie.arguments,argc = SetCookie.arguments.length;
	  var expires = (argc > 2) ? argv[2] : null,path = (argc > 3) ? argv[3] : null;
	  var domain = (argc > 4) ? argv[4] : null,secure = (argc > 5) ? argv[5] : false;
	  document.cookie = name + "=" + escape (value) +
	  ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
	  ((path == null) ? "path=/" : ("; path=" + path)) +
	  ((domain == null) ? "" : ("; domain=" + domain)) +
	  ((secure == true) ? "; secure" : "");
} // end of SetCookie () function
//---------------------------------------------------
function getCookieVal (offset) {
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1)    endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
} // end of getCookieVal ()
//---------------------------------------------------
function DeleteCookie (name) {
	var exp = new Date();
	exp.setTime (exp.getTime() - 1000);
	// This cookie is history
	var cval = GetCookie (name);
	document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
} // end of function DeleteCookie()

//-----------------------------------------------------------------------
function Wait4Cookie(name,count) { // This function waits (until wait_max) for cookie.
	var wait_time=1000,wait_max=60,wait_count=0;
	var line=GetCookie(name);
	var num=0,str="",str2="";
	var debug=false;

	if (count==0) wait_count=0; 
	if  ((line == null || line == "") && wait_count < wait_max) {
		wait_count++;count++;
		if (debug) self.status="Waiting for " + name + " (" + wait_count + " of " + wait_max + ") (" + line + ") ...";
		str="Wait4Cookie(\"" + name + "\"," + count + ")";
		str2="alert(\"" + name + "," + count + "\")";
		window.setTimeout(str,1000);
	} 
	else {
		//alert("Update Arrived !");
		if (wait_count < wait_max) {
			if (debug) self.status="Update Signal Arrived !";
			// OK, do whatever we were waiting for
		}
	}
} // end of function Wait4Cookie()
//-------------------------------------------------------

