var bag = null;
var bagActive = false;
var bagBusy = false;
var bagItemtracker = null;
var customerType = -1;
var ajaxReqid = 1;
var shopdomainqueue = new Array();
var shopdomainactive = false;
var _ajaxReq = new Array();
var rpc = null;
//alert("hi2");

function initShoppingBagClient() {
//	alert("hi");
//	alert(navigator.userAgent);
	if(! ajax) {
		return;
	}
	
	ajaxbag = false;

//	alert("initing easyXDM");
	rpc = new easyXDM.Rpc({
		local: protocol + thishost + "/etc/name.html",
		swf: protocol + shophost + "/etc/easyxdm.swf",
		remote: shopdomainurl
	},
	{
		local: {
			wakeup: function() {
//				alert("wakeup received");
				ajaxbag = true;
				shopdomainactive = true;
			},

			ajaxSuccess: function(resp) {
				eval(resp.req.success + "(resp.resp, resp.statusText)");
			},

			ajaxCustomerTypeSuccess: function(resp) {
				if(resp.resp.status === 0) {
					customerType = resp.resp.customerType;
				}
				eval(resp.req.success + "(resp.resp, resp.statusText)");
			}, 

			ajaxFailure: function(resp) {
				req.failure(resp.resp, resp.statusText);
			},
			
			cookiecallback: function(req) {
				if(typeof(req.cookiecb) !== "undefined") {
					eval(req.cookiecb + "(req)");
				}
			}
		},
		remote: {
			request: {},
			getcookie: {}
		}
	});

//	alert("easyXDM init complete");

	// setup receipt of message that shop. domain has loaded and is active
	var url = protocol + shophost + aburi + "/shopdomain";
	
}

function emulateSuccess(resp, statusText) {
	eval(resp.success + "(resp, statusText)");
}

function AjaxShoppingBag(params) {
	var d = new Date();
	params.tzoff = d.getTimezoneOffset();
	params._url = "shoppingbag";
	SendShopdomainMessage(params);
//	$.ajax({ type: "POST", url: shopurl("shoppingbag"), data: params, dataType: "json",
//			success: ajaxSuccess, error: ajaxFailure });
}

function ajaxSuccess(resp, statusText) {
	if(resp === null) {
		return;
	}
		
	var req = _ajaxReq[resp.reqid];
	if(typeof(req) == 'undefined') {
		return;
	}
	
	if(req === null) {
		return;
	}

	if(typeof(req.success) == 'string') {
		var r = new Object;
		r.req = req;
		r.resp = resp;
		r.statusText = statusText;
		var msg = typeof(req._success) === "undefined" ? "ajaxSuccess" : req._success;
		pm({ target: parent.window, type: msg, data: r });
	}
	_ajaxReq[req.reqid] = null;
}

function ajaxFailure(resp, statusText) {
	var req = _ajaxReq[resp.reqid];
	if(typeof(req) == 'undefined') {
		return;
	}
	
	if(typeof(req.failure) == 'function') {
		var r = new Object;
		r.req = req;
		r.resp = resp;
		r.statusText = statusText;
		var msg = typeof(req._success) === "undefined" ? "ajaxFailure" : req._success;
		pm({ target: parent.window, type: msg, data: r });
	}
	
	_ajaxReq[req.reqid] = null;
}

function AjaxQueryShoppingBag(params) {
	params._url = "shoppingbag";
	SendShopdomainMessage(params);

//	$.ajax({ type: "POST", url: shopurl("shoppingbag"), data: params, dataType: "json",
//			success: ajaxSuccess, error: ajaxFailure });
}

function shopurl(s) {
	var url = "http://" + shophost + aburi + "/" + s;
//	var url = aburi + "/" + s;
	return(url);
}

function AjaxQueryOrdersummary(req) {
	AjaxRequest(req, "ordersummary");
}

function AjaxQueryCustomerType(params) {
	params.name = "sid2";
	params.cookiecb = "AjaxQueryCustomerType2";
	params._type = "getcookie";
	SendShopdomainMessage(params);
}

function AjaxQueryCustomerType2(params) {
	params.sid = params.val;
	params.cookiecb = "AjaxQueryCustomerType3";
	params.name = "wcust";
	params._type = "getcookie";
	SendShopdomainMessage(params);
}

function BagCheckDeal(callback) {
	var params = new Object();
	params.name = "dealcode";
	params.appcb = callback;
	params.cookiecb = "BagCheckDeal2";
	params._type = "getcookie";
	SendShopdomainMessage(params);
}

function BagCheckDeal2(params) {
	var dealcode = params.val;
	if(dealcode != null) {
		if(dealcode.length > 0) {
			params.name = dealcode;
			params.cookiecb = "BagCheckDeal3";
			params._type = "getcookie";
			SendShopdomainMessage(params);
		}
	}
}

function BagCheckDeal3(params) {
	var deal = params.val;
	if(deal != null) {
		if(deal.length > 0) {
			deal = eval('(' + deal + ')');
			if(deal.dealtype == 2) {
				var d = new Date();
				var seconds = (d.getTime() / 1000) - d.getTimezoneOffset();
				if(seconds >= deal.startdate && seconds <= (deal.expdate + 86400 + 28800)) {
					eval(params.appcb + "(deal)");
				}
			}
		}
	}
}

function AjaxQueryCustomerType3(params) {
	var sid = params.sid;
	var wcust = params.val;
	if(sid === null && wcust === null) {
		emulateSuccess(params, "");
		return;
	}
	
	if(sid !== null) {
		if(sid.length === 0) {
			emulateSuccess(params, "");
			return;
		}
	}
	
	if(wcust !== null) {
		if(wcust.length === 0) {
			emulateSuccess(params, "");
			return;
		}
	}
	
	if(wcust !== null && wcust !== '1') {
		params.customerType = 4;
		ajaxSuccess(params, "");
		return;
	}

	params._url = "customertype";
	params._success = "ajaxCustomerTypeSuccess";
	params._type = "request";
	SendShopdomainMessage(params);

//	$.ajax({ type: "POST", url: shopurl("customertype"), data: params, dataType: "json",
//		   success: ajaxCustomerTypeSuccess, error: ajaxFailure });
}

function ajaxCustomerTypeSuccess(resp, statusText) {
	if(resp.status === 0) {
		customerType = resp.customerType;
	}
		
	var req = _ajaxReq[resp.reqid];
	if(typeof(req) == 'undefined') {
		return;
	}
	
	if(typeof(req.success) == 'string') {
		var r = new Object;
		r.req = req;
		r.resp = resp;
		r.statusText = statusText;
		pm({ target: parent.window,  type: "ajaxCustomerTypeSuccess", data: r });
	}

	_ajaxReq[req.reqid] = null;
}

function AjaxVerifyDeal(params) {
	var d = new Date();
	params.tzoff = d.getTimezoneOffset();
	params._url = "verifydeal";
	SendShopdomainMessage(params);

//	$.ajax({ type: "POST", url: shopurl("verifydeal"), data: params, dataType: "json",
//			success: ajaxSuccess, error: ajaxFailure });
}

function AjaxAddToBag(params) {
	var d = new Date();
	params.tzoff = d.getTimezoneOffset();
	params._url = "shoppingbagadd";
	SendShopdomainMessage(params);

//	$.ajax({ type: "POST", url: shopurl("shoppingbagadd"), data: params, dataType: "json",
//			success: ajaxSuccess, error: ajaxFailure });
	
	return(1);
}

function AjaxAddress(params) {
	params._url = "addressbody";
	SendShopdomainMessage(params);

//	$.ajax({ type: "POST", url:  shopurl("addressbody"), data: params, dataType: "json",
//			success: ajaxSuccess, error: ajaxFailure });
	return(1);
}

function AjaxAddressBook(params) {
	params._url = "addressbook";
	SendShopdomainMessage(params);

//	$.ajax({ type: "POST", url: shopurl("addressbook"), data: params, dataType: "json",
//			success: ajaxSuccess, error: ajaxFailure });
	return(1);
}

function AjaxRequest(params, url) {
	params._url = url;
	SendShopdomainMessage(params);
//	$.ajax({ type: "POST", url: url, data: params, dataType: "json",
//			success: ajaxSuccess, error: ajaxFailure });
	return(1);
}

function AjaxAddToWishlist(params) {
	params.importance = 3;

	params._url = "wishlistadd";
	SendShopdomainMessage(params);
//	$.ajax({ type: "POST", url: shopurl("wishlistadd"), data: params, dataType: "json",
//			success: ajaxSuccess, error: ajaxFailure });
	return(1);
}

function AjaxQueryRecommendations(params) {
	params._url = "recommend";
	SendShopdomainMessage(params);
	
//	$.ajax({ type: "POST", url: shopurl("recommend"), data: params, dataType: "json",
//			success: ajaxSuccess, error: ajaxFailure });
}

function AjaxUpdateBag(params) {
	if(bagBusy) {
		return;
	}

	bagBusy = true;

	var d = new Date();
	params.tzoff = d.getTimezoneOffset();
	params._url = "shoppingbagupdate";
	SendShopdomainMessage(params);

//	$.ajax({ type: "POST", url: shopurl("shoppingbagupdatebyitemid"), dataType: "json", data: params,
//			success: ajaxSuccess, error: ajaxFailure });
}

function AjaxRemoveBag(params) {
	if(bagBusy) {
		return;
	}

	bagBusy = true;

	var d = new Date();
	params.tzoff = d.getTimezoneOffset();
	params._url = "shoppingbagremove";
	SendShopdomainMessage(params);

//	$.ajax({ type: "POST", url: shopurl("shoppingbagremove"), dataType: "json", data: params,
//			success: ajaxSuccess, error: ajaxFailure });
}

function AjaxVote(params) {
	params._url = "vote";
	SendShopdomainMessage(params);

//	$.ajax({ type: "POST", url: shopurl("vote"), data: params, dataType: "json",
//			success: ajaxSuccess, error: ajaxFailure, timeout: 7000 });
}

function AjaxVoteretrieve(params) {
	params._url = "voteretrieve";
	SendShopdomainMessage(params);
	
//	$.ajax({ type: "POST", url: shopurl("voteretrieve"), data: params, dataType: "json",
//			success: ajaxSuccess, error: ajaxFailure });
}

var _vcs;

function AjaxVotecount(params) {
	_vcs = params.success;

	$.getJSON(aburi + "/votecount", function(data) { if(typeof(_vcs) == 'function') { _vcs(data); } });
}

var sdtimer = false;

function SendShopdomainMessage(req) {
	if(typeof(req.success) === "function" || typeof(req.failure) === "function") {
		alert("Ajax call with code ref error");
		return;
	}
	
	if(shopdomainactive === false) {
		shopdomainqueue[shopdomainqueue.length] = req;
		if(! sdtimer) {
			sdtimer = true;
			window.setTimeout("ClearShopdomainQueue()", (InternetExploderSix || InternetExploderSeven) ? 500 : 100);
		}
		return;
	}
	
	var t = "request";
	if(typeof(req._type) !== "undefined") {
		t = req._type;
	}

	if(t === "request") {
		rpc.request(req);
	}
	
	if(t === "getcookie") {
		rpc.getcookie(req);
	}
}

function ClearShopdomainQueue() {
	if(shopdomainactive === false) {
		window.setTimeout("ClearShopdomainQueue()", (InternetExploderSix || InternetExploderSeven) ? 500 : 100);
		return;
	}

//	window.clearInterval(sdtimer);
	sdtimer = null;
	var i;
	for(i = 0; i < shopdomainqueue.length; i++) {
		var req = shopdomainqueue[i];
		SendShopdomainMessage(req);
	}
}

function addBagException(r, e) {
	alert(e.name + ": " + e.message);
}

var itemsWithRecommendations;

function BagModify(itemid) {
	var s = GetCookie("itemtracker");

	if(s === null) { return; }
	if(s.length < 1) { return; }

	bagItemtracker = eval('(' + s + ')');
	var quantity = bagItemtracker[itemid];
	var e = ge("updqty_" + itemid);
	if(e) { e.value = quantity; }
	
	bagItemtracker = eval('(' + s + ')');
	displayState[itemid] = "change";
	e = ge("bag" + itemid + "_display");
	if(e) { e.style.display = "none"; }
	e = ge("bag" + itemid + "_change");
	if(e) { e.style.display = "block"; }

	e = ge("updqty_" + itemid);
	if(e) {
		e.focus();
	}
	if(e) {
		e.select();
	}
}

function BagDisplaySummary(bag) {
	var html = '<a class="bagmenubar" id="bagmenubarlink" href="javascript:go(\'bag\')" onmouseover="this.style.color=\'' + siteprofile[siteid].bagHighlight + '\'; ge(\'bagmenubarextra\').style.color=\'' + siteprofile[siteid].bagHighlight + '\';" onmouseout="this.style.color=\'#fff\'; ge(\'bagmenubarextra\').style.color=\'#fff\';"><span style="font-size: 8pt" id="bagmenubarextra">Shopping Bag<br />is empty</span></a>';

	if(bag === null)	{ $("#bagmenubar").html(html); return; }
	if(bag.count < 1)	{ $("#bagmenubar").html(html); return; }

	if(bag.itemtracker.subtotal.toString == 'undefined' || bag.itemtracker.quantity.toString() == 'undefined') {
		html = '<a class="bagmenubar" id="bagmenubarlink" href="javascript:go(\'bag\')" onmouseover="this.style.textDecoration=\'' + siteprofile[siteid].bagHighlight + '\';" onmouseout="this.style.textDecoration=\'#fff\';">Items: Unknown<br />Click to update</a>';
		$("#bagmenubar").html(html);
		return;
	}
	
	html = '<a class="bagmenubar" id="bagmenubarlink" href="javascript:go(\'bag\')" onmouseover="this.style.color=\'' + siteprofile[siteid].bagHighlight + '\'; ge(\'bagmenubarextra\').style.color=\'' + siteprofile[siteid].bagHighlight + '\';" onmouseout="this.style.color=\'#fff\'; ge(\'bagmenubarextra\').style.color=\'#fff\';">' + bag.itemtracker.quantity + ' Item' + (bag.itemtracker.quantity > 1 ? 's' : '') + ' $' + parseFloat(bag.itemtracker.subtotal).toFixed(2) + '<br /><span style="font-size: 8pt" id="bagmenubarextra">View Bag</span></a>';

	$("#bagmenubar").html(html);
}

function BagSetViewlink() {
	var params = new Object();
	
	params.name = "bag";
	params.cookiecb = "BagSetViewlink2";
	params._type = "getcookie";
	SendShopdomainMessage(params);
}

function BagSetViewlink2(params) {
	var bagjson = params.val;
	
	var html = '<a class="bagmenubar" id="bagmenubarlink" href="javascript:go(\'bag\')" onmouseover="this.style.color=\'' + siteprofile[siteid].bagHighlight + '\'; ge(\'bagmenubarextra\').style.color=\'' + siteprofile[siteid].bagHighlight + '\';" onmouseout="this.style.color=\'#fff\'; ge(\'bagmenubarextra\').style.color=\'#fff\';">';
	
	if(bagjson == null) {
		html += '<span style="font-size: 8pt" id="bagmenubarextra">Shopping Bag<br />is empty</span></a>';
	}
	else {
		var bag = eval('(' + bagjson + ')');
		if(bag.quantity == 0) {
			html += '<span style="font-size: 8pt" id="bagmenubarextra">Shopping Bag<br />is empty</span></a>';
		}
		else {
			html += bag.quantity + ' Item' + (bag.quantity > 1 ? 's ' : ' ') + bag.formattedsubtotal + '<br /><span style="font-size: 8pt" id="bagmenubarextra">View Bag</span></a>';
		}
	}
	$("#bagmenubar").html(html);
}

function UpdateBag(n) {
	var e = ge("bagbsk_" + n);
	if(e) {
		var basketid = e.value;
		var quantity = ge("bagqty_" + n).value;
		
		if(customerType == 4 && quantity < 6) {
			alert("There is minimum quantity of 6 units for wholesale customers.\n\nPlease increase your quantity to 6 or more.");
			return;
		}

		updatingBag();

		var d = new Date();
		var params = new Object();
		params.basketid = basketid;
		params.quantity = quantity;
		params.tzoff = d.getTimezoneOffset();
		params.success = "bagSuccess";
		AjaxUpdateBag(params);
	}
}

function RemoveBag(n) {
	var e = ge("bagbsk_" + n);
	if(e) {
		var basketid = e.value;

		updatingBag();

		var params = new Object();
		params.basketid = basketid;
		params.success = "bagSuccess";
		AjaxRemoveBag(params);
	}
}

function UndoAdd(itemid) {
	var basketid = e.value;
	bagBusy = true;

	var params = new Object();
	params.basketid = basketid;
	params.reqid = ajaxReqid;
	ajaxReqid += 1;

	_ajaxReq[params.reqid] = params;
	$.ajax({ type: "POST", url: shopurl("shoppingbagremove"), data: params, dataType: "json",
			success: bagSuccess });
}


var delayBag;

function updatingBag() {
	$("#updatediv").html('<div class="nlnk" style="color: #ff1cac; float: right; height: 29px;"><table cellpadding="0" cellspacing="0"><tr><td><img style="margin-right: 4px;" src="/images/loadarrows.gif" /></td><td><p class="body" style="margin: 0; color: #ff1cac;"><b>Updating...</b></p></td></tr></table></div>');
	delayBag = true;
}

function setCheckoutButtons(req) {
	var cob = true;
	if(pagetype) {
		if(pagetype === "checkout") {
			cob = false;
		}
	}
	req.checkoutbuttons = cob;
	req.closebutton = ! cob;
}

var theBagHTML;

function bagSuccess(bagdata, statusText) {
	bag = bagdata;
	bagBusy = false;
//	if(bag.count > 0) {
//		SetCookie("itemtracker", $.toJSON(bag.itemtracker));
//	}
//	else {
//		SetCookie("itemtracker", "");
//	}
		
	BagDisplaySummary(bag);
	if(bag.status === 0) {
		var req = new Object();
		req.bag = bag;
		req.buttons = true;
		req.checkoutbuttons = true;
		req.closebutton = false;
		setCheckoutButtons(req);

		var html = renderBagContents(req);
		bagActive = true;
		displayBag(html);
//		window.setTimeout("displayBag()", (delayBag ? 500 : 1));
		delayBag = false;
	}
}

function displayBag(html) {
	modalClose();
	modalDialog("Shopping Bag", html, 592, "bagdisplay", "CloseAjaxShoppingBag()");
	pngifyTable($("#sbg" + sbgcount));
	bagActive = false;
}

function CloseAjaxShoppingBag() {
	if(bagBusy) {
		return;
	}
	bagBusy = false;
	bagActive = false;
	modalClose();
	if(typeof(BagCloseNotification) == 'function') {
		eval("BagCloseNotification();");
	}
}

var sbgcount = 0;

function renderBag(req) {
	var div;

	var html = new Array();
	sbgcount += 1;
	html.push('<table class="lb" cellpadding="0" cellspacing="0" id="sbg' + sbgcount + '">');
	html.push('<tr>');
	html.push('<td class="lbtl"></td>');
	html.push('<td class="lbtop" style="width: 592px;"><table class="bare" cellspacing="0" cellpadding="0"><tr><td style="width: 300px;">Shopping Bag</td><td style="text-align: right; width: 292px;"><a class="titlelink"  href="javascript:CloseAjaxShoppingBag()">X</a></td></tr></table></td>');
	html.push('<td class="lbtr"></td>');
	html.push('</tr>');
	html.push('<tr>');
	html.push('<td class="lbl"></td>');
	html.push('<td class="lbmid" style="width: 592px;">');

	req.buttons = true;
	html.push(renderBagContents(req));

	html.push('</td>');
	html.push('<td class="lbr"></td>');
	html.push('</tr>');

	
	html.push('<tr>');
	html.push('<td class="lbbl"></td>');
	html.push('<td class="lbbot"></td>');
	html.push('<td class="lbbr"></td>');
	html.push('</tr>');
	html.push('</table>');

	return(html.join(''));
}

var delayReq;

function renderBagContents(req)
{
	var html = new Array();
	if(req.bag.count > 0)
	{
		if(typeof(req.updatefunc) === 'undefined') { req.updatefunc = 'UpdateBag'; }
		if(typeof(req.removefunc) === 'undefined') { req.removefunc = 'RemoveBag'; }
		if(typeof(req.tag) === 'undefined') { req.tag = "bag"; }
		
		html.push('<table class="bare" cellspacing="0" cellpadding="0" ');
		if(req.buttons)
		{
			html.push('style="margin-top: 15px;');
		}
		html.push('">');
		html.push('<tr>');
		html.push('<td class="bskhd0" style="width: 52px;">Item</td>');
		html.push('<td class="bskhd" style="width: 250px;">Description</td>');
		html.push('<td class="bskhd" style="width: 70px;">Quantity</td>');
		html.push('<td class="bskhd" style="width: 70px;">Price Ea.</td>');
		html.push('<td class="bskhd" style="width: 70px;">Total</td>');
		html.push('</tr>');
		
		var hasbackorder = false;
		for(i = 0; i < req.bag.contents.length; i++)
		{
			var item = req.bag.contents[i];
			
			html.push('<tr>');
			
			// thumbnail
			html.push('<td class="bskthumb"><span class="bagshadow"><img style="display: block; border: 0px; width: 50px; height: 50px; border: 1px solid #ddd;" src="' + item.imguri + '" alt="" /></span></td>');
			
			// description
			html.push('<td class="bsk">' + item.detaildescription + ' (Item ' + item.itemid + ')');

			if(item.removed)
			{
				html.push('<p style="color: #1600FF">Sorry, this item is no longer avaiable and has been removed from your basket</p>');
			}

			if(item.onorder)
			{
				html.push('<p style="color: #1600FF">This item is currently on backorder.</p>');
				hasbackorder = true;
			}
			
			html.push('</td>');
			
			// quantity and input areas
			html.push('<td class="bsk" style="text-align: center">');
			
			if(! item.removed)
			{
				if(req.buttons)
				{
					html.push('<input id="' + req.tag + 'qty_' + i + '" value="' + item.objquantity + '" style="width: 37px; margin-bottom: 3px; text-align: center;" type="text" maxlength="3" size="2" /><br /><div class="smbutton"><a class="smbutton" id="updlnk_' + i + '" href="javascript:' + req.updatefunc + '(' + i + ')"><span>Update</span></a></div><div class="smbutton"><a class="smbutton" id="rmlnk_' + i + '" href="javascript:' + req.removefunc +'(' + i + ')"><span>Remove</span></a></div><input id="' + req.tag + 'bsk_' + i +'" value="' + item.basketid + '" type="hidden" />');
				}
				else
				{
					html.push(item.objquantity);
				}
			}
			
			html.push('</td>');
			
			// price each
			html.push('<td class="bsk" style="text-align: right">' + item.formattedprice + '</td>');
			
			// cost
			html.push('<td class="bsk" style="text-align: right">' + item.formattedcost + '</td>');

			// all done
			html.push('</tr>');
		}

		html.push('<tr><td class="bsk0" colspan="4" style="text-align: right;">Merchandise (Sales tax and shipping may apply)</td>');
		html.push('<td class="bsk" style="text-align: right">' + req.bag.formattedsubtotal + '</td></tr>');

		html.push('</table>');
		
		if(hasbackorder)
		{
			html.push('<p style="color: #1600FF; font-family: arial narrow, helvetica narrow, arial, helvetica, sans-serif; font-size: 10pt; font-weight: bold; text-align: left; marign-top: 10px; margin-bottom: 4pt;">Your order contains one or more backordered items that may cause a delay in shipping your order.</p><p style="color: #1600FF; font-family: arial narrow, helvetica narrow, arial, helvetica, sans-serif; font-size: 10pt; font-weight: bold; text-align: left; margin-top: 0px; margin-bottom: 4pt;">We typically ship any items we have in-stock, then ship any backordered items in additional standard-rate shipment(s) as they become available in our warehouse, usually (but not always) within 1-3 weeks.  In some cases, we may delay shipment of your entire order if backordered items are scheduled to arrive in our warehouse within 5 business days.</p><p style="color: #1600FF; font-family: arial narrow, helvetica narrow, arial, helvetica, sans-serif; font-size: 10pt; font-weight: bold; text-align: left; margin-top: 0px; margin-bottom: 4pt;">Of course, there will be no additional shipping charges.</p>');
		}

		if(req.checkoutbuttons)
		{
			html.push('<div class="button" id="updatediv"><a class="button" href="https://' + shophost + aburi + '/checkout"><span>Checkout</span></a><a class="button" href="javascript:CloseAjaxShoppingBag()"><span>Continue Shopping</span></a></div>');
		}

		if(req.closebutton)
		{
			html.push('<div class="button"><a class="button" href="javascript:CloseAjaxShoppingBag()"><span>Close</span></a></div>');
		}
	}
	else
	{
		html.push('<table class="bare" cellspacing="0" cellpadding="0" style="margin-top: 15px;"><tr><td width="20">&nbsp;</td><td><img style="border: 0px; width: 100px; height: 154px;" src="/images/largebag.png" alt="" /></td><td width="80">&nbsp;</td><td><p class="bigmsg" style="text-align: center; font-size: 18pt; font-weight: normal;">Shopping Bag is empty</p></td></tr></table>');
		html.push('<div class="button"><a class="button" href="javascript:CloseAjaxShoppingBag()"><span>Continue Shopping</span></a></div>');
	}
	
	return(html.join(''));
}

function discountedPrice(price, discount) {
	var array = price.split(' ');
	price = array[0];
	array = price.split('/');
	price = array[0];
	array = price.split('&nbsp;');
	price = array[0].substr(1);
	if(price === "9¢") {
		price = 0.99;
	}
	var n = (price - price * discount) * 1000;
	n = parseInt(n, 10);
	var round = n % 10 > 0 ? 10 : 0;
	n += round;
	n -= n % 10;
	n /= 1000;
	return(n.toFixed(2));
}

function exceptionHandler(r, e) {
	if(e.lineNumber !== null && e.fileName !== null) {
		var args = "?msg=" + escape(e.message) + "&url=" + escape(e.fileName) + "&line=" + escape(e.lineNumber);
	
		var img = new Image();
		img.src = "http://" + thishost + "/images/ajaxexception.jpg" + args;
	}
}

function BagGenerateDiscountedPrices(deal) {
	var i = 0;

	while(ge("pp" + i) !== null) {
		var price = $("#pp" + i).html();
		var n = price.indexOf("/");
		var pair = false;
		if(n == -1) {
			n = price.indexOf("&");
		}
		else {
			pair = true;
		}
		if(n != -1) {
			var m = price.indexOf("$");
			if(m == -1) {
				m = 0;
			}
			else {
				m += 1;
				n -= 1;
			}
			price = price.substr(m, n);
			price = parseFloat(price);
		} 
		price = price.toFixed(2);
		var html = '<span class="regprice">$' + price + '</span>';
		price = price - (price * deal.dealdiscount / 100);
		price += 0.01;
		html += '&nbsp;$' + price.toFixed(2);
		if(pair) {
			html += "/pair ";
		}
		else {
			html += " ";
		}
		$("#pp" + i).html(html);
 		
		i = i + 1;
	}
}

function BagGenerateWholesalePrices() {
	var i = 0;

	while(ge("pp" + i) !== null) {
		var price = $("#pp" + i).html();

		if(price.indexOf("dz") == -1) {
			var dprice = discountedPrice(price, 0.5) * 12;
			var hprice = discountedPrice(price, 0.45) * 6;
			var html = "$" + dprice.toFixed(2) + " dz $" + hprice.toFixed(2) + " &frac12;dz<br />";
			$("#pp" + i).html(html);
		}
 		
		i = i + 1;
	}
}


