/* toolweb javascript library */

var popInfoCtr = 0;

function showAddMsg(type,itemImg,itemName,itemQty,itemPrice)
{
	var popDiv = $('addDiv');
	var popInfoId = 'popInfo-' + (++popInfoCtr);
	var popList = $('addList');
	
	if (type == "cart") {
		$('addHdr').update('Item Added to Cart');
		popList.insert('<div class=\"cart-add\" id=\"'+popInfoId+'\"><div style="padding: 10px;"><img id=\"addImg\" src=\"'+itemImg+'\" alt=\"\" /> <div class=\"itemName\"><strong>'+itemName+'</strong></div><br/><div class=\"itemDetails\">Added <strong>'+itemQty+'</strong> at <strong>'+itemPrice+'</strong> each</div></div></div>');
	} else if (type == "compare") {
		$('addHdr').update('Item Added to Compare');
		popList.insert('<div class=\"compare-add\" id=\"'+popInfoId+'\"><div style="padding: 10px;"><img id=\"addImg\" src=\"'+itemImg+'\" alt=\"\" /> <strong>'+itemName+'</strong></div></div>');
	}

	setTimeout(function(){Effect.BlindUp($(popInfoId),{afterFinish:function(){onPopInfoEffectComplete($(popInfoId));}});}, 2000);

	if (!popDiv.style.display || popDiv.style.display == 'none')
	{
		var obj = doMeasure();	
	
		popDiv.style.display = 'block';
		divWidth = popDiv.offsetWidth;
		divHeight = popDiv.offsetHeight;
		
		var xPos = (obj.winWidth - divWidth)/2;
		var yPos = (obj.winHeight - divHeight)/2 + obj.scrollTop;
	
		popDiv.style.left = xPos+'px';
		popDiv.style.top = yPos+'px';
	}
}

function onPopInfoEffectComplete(element)
{
	var popList = $('addList');

	popList.removeChild(element);

	if (popList.childNodes.length == 0)
	{
		$('addDiv').hide();
	}
}

function returnCheck(id) {
	var box = eval("document.returnform.return"+id);
	var reason = eval("document.returnform.reason"+id);
	var qty = eval("document.returnform.qty"+id);
	
	if (box.checked == true) {
		reason.disabled = false;
		qty.disabled = false;
	} else {
		reason.disabled = true;
		qty.disabled = true;
	}
}

function returnReason(id) {
	var qty = eval("document.returnform.qty"+id);
	var reason = eval("document.returnform.reason"+id);
	
	if (qty.value > 0) {
		reason.disabled = false;
	} else {
		reason.disabled = true;
	}
}

var popInfoCtr = 0;

function addToCart(partnum, field, quantity)
{
	try
	{
		var popDiv = $('addDiv');

		/* Check to see how long it has been since this item was added (protect from double click, etc.) */
		var timestamp = (new Date()).valueOf();
		if (popDiv.lastpartnum && (popDiv.lastpartnum == partnum) &&
			popDiv.lastaddtime && ((timestamp - popDiv.lastaddtime) < 2000))
		{
			return;
		}

		if (partnum == '')
		{
			alert('Invalid part number.');
			return false;
		}

		if (! quantity)
		{
			quantity = field.value;

			if (quantity == '')
			{
				field.value = '1';
				quantity = 1;
			}
		}

		if(isNaN(quantity) || (quantity < 1) || (quantity != Math.round(quantity)))
		{
			alert('You have entered an invalid quantity value.');
			return;
		}

		var popInfoId = 'popInfo-' + (++popInfoCtr);
		var popList = $('addList');
		
		popDiv.lastpartnum = partnum;
		popDiv.lastaddtime = timestamp;
		
		$('addHdr').update('Adding Item to Cart');
		popList.insert('<div class="cart-add" id="'+popInfoId+'"><div style="padding: 10px;"><img class="loadingImg" align="absmiddle" src="/_media/loading_sm.gif" alt="" />Loading...</div></div>');

		if (!popDiv.style.display || popDiv.style.display == 'none')
		{
			var obj = doMeasure();

			popDiv.style.display = 'block';
			divWidth = popDiv.offsetWidth;
			divHeight = popDiv.offsetHeight;

			var xPos = (obj.winWidth - divWidth)/2;
			var yPos = (obj.winHeight - divHeight)/2 + obj.scrollTop;

			popDiv.style.left = xPos+'px';
			popDiv.style.top = yPos+'px';
		}

		var errormsg = 'Unable to add item to cart.';

		var req = new Ajax.Request('/_ajax/addtocart.cfm',
		{
			method: 'post',
			parameters: {partnum:partnum, quantity:quantity},
			onSuccess: function(data)
			{
				if (data.responseJSON)
				{
					if (data.responseJSON.success)
					{
						$(popInfoId).update('<div style="padding: 10px;"><img id="addImg" src="'+data.responseJSON.img+'" alt="" /> <div class="itemName"><strong>'+data.responseJSON.name+'</strong></div><br/><div class="itemDetails">Added <strong>'+data.responseJSON.qty+'</strong> at <strong>'+data.responseJSON.price+'</strong> each</div></div>');

						updateCartTotals(data.responseJSON.total, data.responseJSON.count);

						setTimeout(function(){Effect.BlindUp($(popInfoId),{duration:0.75,afterFinish:function(){
							var popList = $('addList');
							popList.removeChild($(popInfoId));
							if (popList.childNodes.length == 0){$('addDiv').hide();}
						}});}, 2000);
					}
					else if (data.responseJSON.message)
					{
						showAddCartError(data.responseJSON.message);
						$('addDiv').hide();
					}
					else
					{
						showAddCartError(errormsg);
						$('addDiv').hide();
					}
				}
				else
				{
					showAddCartError(errormsg);
					$('addDiv').hide();
				}
			},
			onFailure: function(data)
			{
				if (data.responseJSON && data.responseJSON.message)
				{
					showAddCartError(data.responseJSON.message);
					$('addDiv').hide();
				}
				else
				{
					showAddCartError(errormsg);
					$('addDiv').hide();
				}
			}
		});
	}
	catch (err)
	{
		showAddCartError(errormsg);
	}
}

function showAddCartError(errormsg)
{
	alert(errormsg);
	$('addList').update();
	$('addDiv').hide();
}

function hideAddCartPopop()
{
	var list = $('addList');
	if (list.hasChildNodes())
	{
		while (list.childNodes.length >= 1)
		{
			list.removeChild(list.firstChild);
		}
	}
	$('addDiv').hide();
}

function updateCartTotals(total, count)
{
	$('carttotal').update(total);
	if (count == 1)
	{
		$('cartcount').update(count + ' Item');
	}
	else
	{
		$('cartcount').update(count + ' Items');
	}
	$('hdrbox').style.width = $('topmodule').offsetWidth + 18 + 'px';
}

function followNav(url)
{
	document.location.href = url;
}

function showLarge(path)
{
	var obj = doScrn();	

	var popDiv = document.getElementById('largeDiv');
	var imgLarge = document.getElementById('largeImg');
	
	imgLarge.style.backgroundImage = 'url(' + path + ')';	
	popDiv.style.display = 'block';
	divWidth = popDiv.offsetWidth;
	divHeight = popDiv.offsetHeight;
	
	var xPos = (obj.winWidth - divWidth)/2;
	var yPos = (obj.winHeight - divHeight)/2 + obj.scrollTop;

	popDiv.style.left = xPos+'px';
	popDiv.style.top = yPos+'px';
}

