//
// Albertsons
// by Jacque Istok
//    Professional Innovations, Inc.
//    http://www.piiglobal.com
//
// $Id: javascript.js,v 1.37 2004/11/02 16:45:15 dpsdjxi Exp $ 

var BasketUID = new Array();
var BasketName = new Array();
var BasketQty = new Array();
var BasketPrice = new Array();
var BasketSize = new Array();
var BasketUPC = new Array();
var BasketDesc = new Array();
var sizeID = "";

function showLargerImage(upc)
{
	document.getElementById('s_fullsize').style.display = 'block';
	document.getElementById('fullsize_image').src = "catalog/fullsize/" + upc + ".jpg";

	if(document.getElementById('fullsize_image').src.match(/cake_theme/))
	{
		parent.f_items.document.getElementById('c_theme').style.display = 'none';
		parent.f_items.document.getElementById('c_size').style.display = 'none';
		parent.f_items.document.getElementById('c_layer').style.display = 'none';
		parent.f_items.document.getElementById('c_filling').style.display = 'none';
		document.getElementById('fullsize_image').width = 200;
		document.getElementById('fullsize_image').height = 170;
	}
	return true;
}

function closeLargerImage()
{
	document.getElementById('s_fullsize').style.display = 'none';
	if(document.getElementById('fullsize_image').src.match(/cake_theme/))
	{
		parent.f_items.document.getElementById('c_theme').style.display = 'block';
		parent.f_items.document.getElementById('c_size').style.display = 'block';
		parent.f_items.document.getElementById('c_layer').style.display = 'block';
		parent.f_items.document.getElementById('c_filling').style.display = 'block';
	}
	return true;
}

function closeButton()
{
	window.close();
	return true;
}

function linkHover(status_text)
{
	window.status = status_text;
	document.body.style.cursor = POINTER;
	return true;
}

function linkOut(status_text)
{
	window.status = status_text;
	document.body.style.cursor = 'default';
	return true;
}

function SearchPlatter(which, div)
{
	var search_platter = document.getElementById("search_platter");
	if(which == "focus")
	{
		if(search_platter.value == "Enter key word(s)")
		{
			search_platter.value = "";
		}
	}
	else if (which == "blur")
	{
		if(search_platter.value == "")
		{
			search_platter.value = "Enter key word(s)";
		}
	}
	else if (which == "search")
	{
		if(search_platter.value == "Enter key word(s)")
		{
			alert("Please search for a word(s) contained within the description of a platter");
		}
		else
		{	
			var url = "item_list.asp?div=" + div + "&category_uid=0&search=" + search_platter.value;
			parent.f_items.location.href = url;
		}
	}
	return true;
}

function mapShow(m_sAddress, sZip)
{
	var url = "http://maps.yahoo.com/py/maps.py?Pyt=Tmap&newFL=Use+Address+Below&country=us&Get%A0Map=Get+Map&addr=" + m_sAddress + "&csz=" + sZip;
	winMap = window.open(url,"map","height=725,width=660,scrollbars=yes,titlebar=no,resizable");
	//,screenX="+x+",left="+x+",screenY="+y+",top="+y
}

function helpShow()
{
	winMap = window.open("help.asp","help","height=625,width=770,scrollbars=yes,titlebar=no,resizable=no");
}

function categoryShow(div)
{
	
	var category_uid = document.getElementById("category_uid");
	var url = "item_list.asp?div=" + div + "&category_uid=" + category_uid.options[category_uid.selectedIndex].value + "&category_name=" + category_uid.options[category_uid.selectedIndex].text;
	parent.f_items.location.href = url;

}

function PlatterLoad(uid)
{
	var category_uid = document.getElementById("category_uid");
	var step_desc = top.document.getElementById("step_desc");
	for(var loop=0; loop < category_uid.options.length; loop++)
	{
		if(category_uid.options[loop].value == uid)
		{
			category_uid.selectedIndex = loop;
		}

		//holiday dinner exceptions
		if(category_uid.options[category_uid.selectedIndex].text == "Holiday Dinners")
		{
			var mydate=new Date();
			var month = mydate.getMonth() + 1;
			var day = mydate.getDate();
			//holiday text
			if( ! (month == 11 && ((day >= 11 && day <= 22) || day >= 29)) || (month == 12 && (day <= 22)) )
			{
				step_desc.innerHTML = "Step one: add products to your order<br> <font color='red'>Holiday Dinners are available for on-line ordering from Nov.11th through Nov. 22nd and Nov. 29th through Dec. 22nd.  Please call or visit your local store for dinner availability beyond these specified dates.</font>"; 
			}
			step_desc.innerHTML = step_desc.innerHTML + "<br><br><font color ='black'>Note: Dinner components and entrees are fully cooked, but will require reheating. Heating instructions are included. Please allow a minimum of 2-3 hours to heat <br>before serving.</font>";
		}
	}

	//check for invalid division
	if(category_uid.options.length == 1)
	{
		step_desc.innerHTML = "<font color='red'>Sorry - The Shop 'n Save Online Party Planner is not yet available in your area.  Please call your store to arrange for a party tray.</font>"; 
	}
}

function stepOne(address1, address2, store_num, zip_code, division, phonenum)
{
	var unit_num = parent.f_store.document.getElementById("unit_num");
	var addr1 = parent.f_store.document.getElementById("addr1");
	var addr2 = parent.f_store.document.getElementById("addr2");
	var zip = parent.f_store.document.getElementById("zip_code");
	var div = parent.f_store.document.getElementById("div");
	var phone_num = parent.f_store.document.getElementById("phone_num");

	unit_num.value = store_num;
	addr1.value = address1;
	addr2.value = address2;
	zip.value = zip_code;
	div.value = division;
	phone_num.value = phonenum;

	parent.f_store.document.getElementById("f_step2").submit();
}

function stepTwo()
{
	if(BasketUID.length == 0)
	{
		alert("Please add some items to your basket to order.");
	}
	else
	{
		var url = "step2.asp";
		var category_uid = document.getElementById("category_uid");
	 	category_uid.selectedIndex = 0;

		parent.f_items.location.href = url;
		var d_platter_note = parent.document.getElementById("d_platter_note");
		d_platter_note.style.display = 'none';
	}
}

function itemLoad(uid)
{
	var step_desc = top.document.getElementById("step_desc");
	step_desc.innerHTML = "Step one: add products to your order.<br><font color='#CF5D1E' font-family: verdana> Note: Chicken Trays are only available from 10.00 am - 6.00 pm</br>"; 

	var d_platter_note = top.document.getElementById("d_platter_note");
	if(uid != -1)
	{
		d_platter_note.style.display = 'block';
	}
	else
	{
		d_platter_note.style.display = 'none';
	}

	PlatterLoad(uid);
	basketRefresh();
}

function basketLoad(uid)
{
	var qty = parent.f_items.document.getElementById('qty_' + uid).value;
	var stackpush = 1;

	if(qty == 0)
	{
		alert("Please add at least 1 item to your basket.");
	}
	else
	{
		var j_name = parent.f_items.document.getElementById('d_name_' + uid);
		var current_price = parent.f_items.document.getElementById('current_price_' + uid);
		var current_size = parent.f_items.document.getElementById('current_size_' + uid);
		var current_upc = parent.f_items.document.getElementById('current_upc_' + uid);
		var current_desc = parent.f_items.document.getElementById('current_desc_' + uid);

		for(var loop=0; loop < BasketUID.length; loop++)
		{
			if(BasketUID[loop] == uid && BasketSize[loop] == current_size.value && uid != "customcake")
			{
				stackpush = 0;
				BasketQty[loop] = (BasketQty[loop] * 1) + (qty * 1);
			}
		}

		if(stackpush)
		{
			BasketUID.push(uid);
			BasketName.push(j_name.innerHTML);
			BasketPrice.push(current_price.value);
			BasketQty.push(qty);
			BasketSize.push(current_size.value);
			BasketUPC.push(current_upc.value);
			BasketDesc.push(current_desc.value);
		}

		basketRefresh();
	}
}

function deleteItem(which)
{
	var basket_qty = parent.document.getElementById('basket_qty_' + which);
	basket_qty.value = 0;
	basketUpdate();
}

function basketUpdate()
{
	var strValidChars = "0123456789";

	var basket_qty;
	for(var loop=BasketUID.length - 1; loop >= 0; loop--)
	{
		basket_qty = parent.document.getElementById('basket_qty_' + loop).value;
		if(!checkChars(basket_qty, strValidChars))
		{
			alert("Invalid qty change");
		}
		else
		{
			if(basket_qty == 0)
			{
				BasketUID.splice(loop,1);
				BasketName.splice(loop,1);
				BasketQty.splice(loop,1);
				BasketPrice.splice(loop,1);
				BasketSize.splice(loop,1);
				BasketUPC.splice(loop,1);
				BasketDesc.splice(loop,1);
			}
			else
			{
				BasketQty[loop] = basket_qty;
			}
		}
	}

	basketRefresh();
}

function basketRefresh()
{
	var subtotal = 0;

	var d_basket = parent.document.getElementById('d_basket');
	var s_subtotal = parent.document.getElementById('s_subtotal');
	var html;
	var tmpnum;

	if(BasketUID.length == 0)
	{
		html = '<span class="orangeDark2_reg">Your order summary is currently empty.</span>';
	}
	else
	{
		html = '<table width="200" class="basket" border="0" cellspacing="0" cellpadding="0">';

		for(var loop=0; loop < BasketUID.length; loop++)
		{
			tmpnum = Math.round(BasketPrice[loop] * BasketQty[loop] * 100) / 100;
			tmpnum = Math.round(tmpnum*100).toString();
			tmpnum = tmpnum.substring(0,tmpnum.length-2)+'.'+ tmpnum.substring(tmpnum.length-2,tmpnum.length);

			html += '<tr><td align="left" width="40" height="5" class="black_bold">Item:</td><td align="left" style="padding-left:5px;" class="blue">' + BasketName[loop] + '</td></tr>';
			html += '<tr><td align="left" width="40" height="5" class="black_bold">Quantity:</td><td align="left" style="padding-left:5px;" class="reg_text"><input maxlength="2" id="basket_qty_' + loop + '" type="text" size="2" value="' + BasketQty[loop] + '" style="font-size:10px; font-family: verdana;"></td></tr>';
			html += '<tr><td align="left" width="40" height="5" class="black_bold">Size:</td><td align="left" style="padding-left:5px;" class="reg_text">' + BasketSize[loop] + '</td></tr>';
			html += '<tr><td align="left" width="40" height="5" class="black_bold">Price:</td><td align="left" style="padding-left:5px;" class="reg_text">$' + tmpnum + '</td></tr>';
			html += '<tr><td align="center" colspan="2"><img id="basket_delete_button_' + loop + '" onmouseover="this.style.cursor = POINTER;" src="images/button_delete.gif" onclick="deleteItem(' + loop + ');"></td></tr>';
			html += '<tr><td colspan="2" height="2" style="border-bottom:1px dashed #c2c2c2;">\&nbsp;</td></tr>';
	
			subtotal += (BasketPrice[loop] * BasketQty[loop]);
		}

		html += '<table>';
	}
	d_basket.innerHTML = html;

	if(subtotal == 0)
	{
		s_subtotal.innerHTML = "Order Total: $0.00";
	}
	else
	{
		tmpnum = Math.round(subtotal * 100) / 100;
		tmpnum = Math.round(tmpnum*100).toString();
		tmpnum = tmpnum.substring(0,tmpnum.length-2)+'.'+ tmpnum.substring(tmpnum.length-2,tmpnum.length);
		s_subtotal.innerHTML = "Order Total: $" + tmpnum;
	}
}

function strCheck(which)
{
	var strValidChars;
	var str;

	if(which == "zip")
	{
		strValidChars = "0123456789";
		str = document.getElementById('zip');
		maxlen = 5;
	}
	else if(which == "city")
	{
		strValidChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ";
		str = document.getElementById('city');
		maxlen = 100;
	}


//check length
	if(str.value.length > maxlen)
	{
		alert(which + " cannot be longer than " + maxlen + " digits");
		str.value = "";
		str.focus();
	}

//check for numeric values
	if(!checkChars(str.value, strValidChars))
	{
		alert("Invalid characters in " + which + " field");
		str.value = "";
		str.focus();
	}
}

function storeSearch()
{
	var zip = document.getElementById("zip");
	var city = document.getElementById("city");
	var state = document.getElementById("state").options[document.getElementById("state").selectedIndex];

	strCheck('zip');
	strCheck('city');
	if(zip.value == "" && city.value == "" && state.value == "")
	{
		alert("Please enter a city and state, or a zip code to search on.");
	}
	else if(zip.value == "" && city.value == "" && state.value != "")
	{
		alert("Please enter a city in your state to search on.");
	}
	else if(zip.value == "" && city.value != "" && state.value == "")
	{
		alert("Please enter the state of your city to search on.");
	}
	else
	{
		var url = "store_list.asp?zip_code=" + zip.value + "&city=" + city.value + "&state=" + state.value;
		parent.f_store.location.href = url;
	}
}

function completeOrder()
{
	var validSubmission = true;
	var strValidChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 ";
	var reg = new RegExp(/^.+[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_]\@.+\.\w{2,3}$/i);

	top.f_items.document.getElementById("div").value = top.document.getElementById("div").value;
	top.f_items.document.getElementById("unit_num").value = top.document.getElementById("unit_num").value;
	top.f_items.document.getElementById("addr1").value = top.document.getElementById("addr1").value;
	top.f_items.document.getElementById("addr2").value = top.document.getElementById("addr2").value;
	top.f_items.document.getElementById("phone_num").value = top.document.getElementById("phone_num").value;

	var fname = parent.f_items.document.getElementById('fname');
	var lname = parent.f_items.document.getElementById('lname');
	var email = parent.f_items.document.getElementById('email');
	var vemail = parent.f_items.document.getElementById('vemail');
	var phone = parent.f_items.document.getElementById('phone');

	if(!checkChars(fname.value, strValidChars) || fname.value.length == 0)
	{
		validSubmission = false;
		//alert("First name must be between 1 and 30 alphanumeric characters");
		alert("Please enter information for first name");
	}

	if(!checkChars(lname.value, strValidChars) || lname.value.length == 0)
	{
		validSubmission = false;
		//alert("Last name must be between 1 and 40 alphanumeric characters");
		alert("Please enter information for last name");
	}

	strValidChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789@_.";
	if(!checkChars(email.value, strValidChars) || email.value.length == 0)
	{
		validSubmission = false;
		//alert("Last name must be between 1 and 40 alphanumeric characters");
		alert("Please enter information for email");
	}
	
	if(!checkChars(email.value, strValidChars) || !checkChars(vemail.value, strValidChars))
	{
		validSubmission = false;
		//alert("Email must be between 1 and 50 alphanumeric characters");
		alert("Please enter information for email");
	}

	if(email.value != vemail.value)
	{
		validSubmission = false;
		alert("Email verification does not match");
	}

	var reg = new RegExp(/^.+[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_]\@.+\.\w{2,3}$/i);
	if(!reg.test(email.value) && email.value != "")
	{
		validSubmission = false;
		alert("Please enter a valid email address");
	}

	strValidChars = "0123456789";
	if(!checkChars(phone.value, strValidChars) || phone.value.length < 10)
	{
		validSubmission = false;
		if(phone.value.length == 0)
		{
			alert("Please enter information for phone number");
		}
		else
		{
			alert("Phone Number should be of minimum 10 numeric digits");
		}
	}

//get basket items
	var uid = parent.f_items.document.getElementById('BasketUID');
	var name = parent.f_items.document.getElementById('BasketName');
	var qty = parent.f_items.document.getElementById('BasketQty');
	var price = parent.f_items.document.getElementById('BasketPrice');
	var size = parent.f_items.document.getElementById('BasketSize');
	var upc = parent.f_items.document.getElementById('BasketUPC');
	var desc = parent.f_items.document.getElementById('BasketDesc');

	uid.value = "";
	name.value = "";
	qty.value = "";
	price.value = "";
	size.value = "";
	upc.value = "";
	desc.value = "";

	for(var loop=0; loop < BasketUID.length; loop++)
	{
		uid.value += BasketUID[loop] + "|";
		name.value += BasketName[loop] + "|";
		qty.value += BasketQty[loop] + "|";
		price.value += BasketPrice[loop] + "|";
		size.value += BasketSize[loop] + "|";
		upc.value += BasketUPC[loop] + "|";
		desc.value += BasketDesc[loop] + "|";
	}

	if(uid.value == "")
	{
		validSubmission = false;
		alert("Please add some items to your basket to order.");
	}

	if(validSubmission)
	{
		BasketUID.length = 0;
		BasketName.length = 0;
		BasketQty.length = 0;
		BasketPrice.length = 0;
		BasketSize.length = 0;
		BasketUPC.length = 0;
		BasketDesc.length = 0;

		parent.f_items.document.getElementById('f_step3').submit();
	}
}

function qtyCheck(qty)
{
	var strValidChars = "0123456789";

	if(qty.value == 0)
	{
		alert("Please enter a quantity of 1 or greater.");
		qty.value = 1;
	}

	if(!checkChars(qty.value, strValidChars))
	{
		alert("Please enter a numeric quantity.");
		qty.value = 1;
	}
}

function checkChars(str, strValidChars)
{
	var blnResult = true;
	for (var loop=0; loop < str.length && blnResult == true; loop++)
	{
		strChar = str.charAt(loop);
		if (strValidChars.indexOf(strChar) == -1)
		{
			blnResult = false;
		}
	}

	return blnResult;
}
	
function cancelOrder()
{
	if(BasketUID.length > 0 && confirm("Are you sure you want to cancel your order and empty the items in your basket?"))
	{
		BasketUID.length = 0;
		BasketName.length = 0;
		BasketQty.length = 0;
		BasketPrice.length = 0;
		BasketSize.length = 0;
		BasketUPC.length = 0;
		BasketDesc.length = 0;
	
		basketRefresh();
		PlatterLoad(-1);
		categoryShow(-1);
	}
}

function priceAdjust(uid, price, size, cnt, serves, upc)
{
	var h_price = parent.f_items.document.getElementById('price_' + uid);
	var d_price = parent.f_items.document.getElementById('d_price_' + uid);
	var c_div = parent.f_items.document.getElementById('count_' + uid);
	var s_div = parent.f_items.document.getElementById('serves_' + uid);
	var img = parent.f_items.document.getElementById('img_' + uid);
	var d_viewlarger = parent.f_items.document.getElementById('d_viewlarger_' + sizeID);

	var current_price = parent.f_items.document.getElementById('current_price_' + uid);
	var current_size = parent.f_items.document.getElementById('current_size_' + uid);
	var current_upc = parent.f_items.document.getElementById('current_upc_' + uid);

	var new_price = Math.round( ((Math.round(h_price.value * 100) / 100) + (Math.round(price * 100) / 100)) * 100) / 100;

	current_price.value = new_price;
	current_size.value = size;
	current_upc.value = upc;

	d_price.innerHTML = "$" + new_price;
	c_div.innerHTML = cnt;
	s_div.innerHTML = serves;
	img.src = "catalog/" + upc + ".jpg";

	d_viewlarger.innerHTML = "<span class=\"blue\" onmouseover=\"javascript:this.style.cursor = POINTER;\" onmouseout=\"javascript:parent.linkOut('');\" onclick=\"javascript:parent.showLargerImage('" + upc + "');\">View Larger Image</span>";
}

function createSize(size, price_offset, cnt, serves, upc)
{
	var s_type = "";
	var c_div = parent.f_items.document.getElementById('count_' + sizeID);
	var s_div = parent.f_items.document.getElementById('serves_' + sizeID);
	var r_div = parent.f_items.document.getElementById('radio_' + sizeID);
	var upc_tmp = parent.f_items.document.getElementById('current_upc_' + sizeID);
	var d_viewlarger = parent.f_items.document.getElementById('d_viewlarger_' + sizeID);
	var img = parent.f_items.document.getElementById('img_' + sizeID);
	var current_size = parent.f_items.document.getElementById('current_size_' + sizeID);

	size = size.replace(/"/, " in.");
	size = size.replace(/'/, " ft.");

	if(r_div.innerHTML == "&nbsp;" || !r_div.innerHTML.match(/radio/))
	{
		s_type = "checked";
		current_size.value = size;
		c_div.innerHTML = cnt;
		s_div.innerHTML = serves;
		upc_tmp.value = upc;
		img.src = "catalog/" + upc + ".jpg";
		img.width = 120;
		img.height = 100;
	}

	r_div.innerHTML += "<br><input value=\"" + size + "\" name=\"radio_" + sizeID + "\" type=\"radio\" " + s_type + " onclick=\"parent.priceAdjust('" + sizeID + "','" + price_offset + "', '" + size + "','" + cnt + "','" + serves + "', '" + upc + "');\">" + size;
	d_viewlarger.innerHTML = "<span class=\"blue\" onmouseover=\"javascript:this.style.cursor = POINTER;\" onmouseout=\"javascript:parent.linkOut('');\" onclick=\"javascript:parent.showLargerImage('" + upc + "');\">View Larger Image</span>";
}

function holidayAlert()
{
	alert("Sorry - Holiday Dinners can only be ordered between Nov 11th and Nov 22nd or Nov 29th and Dec 22nd.");
}

function step2Load()
{
	basketRefresh();
	for(var loop=0; loop < BasketUID.length; loop++)
	{
		document.getElementById('basket_qty_' + loop).disabled = true;
		document.getElementById('basket_delete_button_' + loop).style.display = 'none';
	}
}

function orderDateChk(msg)
{
	var curdate = new Date();
	var hours = curdate.getHours();

	pickup_date = parent.f_items.document.getElementById('pickup_date');
	pickup_time = parent.f_items.document.getElementById('pickup_time');

	if(pickup_date.selectedIndex == 0) //tomorrow
	{
		if(hours <= 8)
		{
			pickup_date.selectedIndex = 0;
			hours = 8;
			msg++;
		}
		else if(hours >= 20)
		{
			pickup_date.selectedIndex = 1;
			hours = 8;
			msg++;
		}
		else
		{
			//pickup_date.selectedIndex = 1;
			if(pickup_time.selectedIndex < hours - 8)
			{
				pickup_time.selectedIndex = hours - 8 + 1;
				msg++;
			}
		}
	}

	if(msg == 2)
	{
		alert("We require 24 hour advanced notice.  If needed sooner, please contact the store.");
	}
}

//duplicate of the cake.js one - should consolidate
function step2commentRestrict(comment)
{
        var comment_cnt = parent.f_items.document.getElementById('comment_cnt');
        var maxLength = 500;

        if(comment.value.length + 1 > maxLength)
        {
                comment.value = comment.value.substring(0, maxLength - 1);
        }

        comment_cnt.innerHTML = (comment.value.length + 1) + " of " + maxLength;
}

