/* Schweppe Scripts */

var msgTimer ;	

function showLarge(itemNum) {
	var obj = doScrn();	

	var popDiv = top.document.getElementById('largeDiv');
	var imgLarge = top.document.getElementById('largeImg');
	
	imgLarge.src = '/_media/products/large/'+itemNum+'.jpg';	
	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';
	
	// alert ('div width: '+divWidth+' | div height: '+divHeight+' | caption width:'+capWidth+'');
}

function showNote(noteText) {
	var obj = doScrn();	
	var popDiv = top.document.getElementById('noteDiv');
	var noteBody = top.document.getElementById('noteText');
	
	noteBody.innerHTML = noteText;
	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 selectImage(id,img) {
	var option = document.getElementById(id).selectedIndex;
	var image = document.getElementById(img);
	
	if (option == 1 | option == 2) {
		image.src = "/_media/cvv2_visamc.gif";
	} else if (option == 3) {
		image.src = "/_media/cvv2_amex.gif";
	} else if (option == 4) {
		image.src = "/_media/cvv2_discover.gif";
	} else {
		image.src = "/_media/spacer.gif";
	}
	
}

function showAllFilters(obj)
{
	var items = $(obj).nextSiblings();

	if ($(obj).hasClassName('closed'))
	{
		$(obj).addClassName('open');
		$(obj).removeClassName('closed');
		//$(obj).down(1).src = "/_media/icons/plus.gif";
		//$(obj).down(2).update('More...');
		items.each(function(item){Element.addClassName(item, 'hide')});
	}
	else
	{
		$(obj).addClassName('closed');
		$(obj).removeClassName('open');
		//$(obj).down(1).src = "/_media/icons/minus.gif";
		//$(obj).down(2).update('Less...');
		items.each(function(item){Element.removeClassName(item, 'hide')});
		$(obj).hide();
	}
}

var popInfoCtr = 0;

function addToCart(partnum, fieldCase, fieldEach)
{
	try
	{
		var errormsg = 'Unable to add item to cart.';

		var popDiv = $('addDiv');

		var timestamp = (new Date()).valueOf();
		if (popDiv.lastpartnum && (popDiv.lastpartnum == partnum) &&
			popDiv.lastaddtime && ((timestamp - popDiv.lastaddtime) < 2000))
		{
			return false;
		}

		if (partnum == '')
		{
			alert('Invalid part number.');
			return false;
		}
		
		var quantityCase = 0;
		var quantityEach = 0;

		if (fieldCase)
		{
			if (fieldCase.value.length)
			{
				quantityCase = fieldCase.value;

				if (! checkQuantityValue(quantityCase))
				{
					alert('You have entered an invalid quantity value.');
					return false;
				}
			}
		}

		if (fieldEach)
		{
			if (fieldEach.value.length)
			{
				quantityEach = fieldEach.value;

				if (! checkQuantityValue(quantityEach))
				{
					alert('You have entered an invalid quantity value.');
					return false;
				}
			}
		}

		if (fieldEach && !fieldEach.value.length)
		{
			if (fieldCase == null || !fieldCase.value.length)
			{
				fieldEach.value = '1';
				quantityEach = 1;
			}
		}

		if (fieldCase && !fieldCase.value.length)
		{
			if (fieldEach == null || !fieldEach.value.length)
			{
				fieldCase.value = '1';
				quantityCase = 1;
			}
		}
		
		if ((quantityCase == 0) && (quantityEach == 0))
		{
			alert('You have entered an invalid quantity value.');
			return false;
		}

		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+'"><img class="loadingImg" align="absmiddle" src="/_media/loading_sm.gif" alt="" />Loading...</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;
			//var xPos = (obj.winWidth) - 455;
			//var yPos = (obj.winHeight + obj.scrollTop) - 110;

			popDiv.style.left = xPos+'px';
			popDiv.style.top = yPos+'px';
		}

		var req = new Ajax.Request('/_ajax/addtocart.cfm',
		{
			method: 'post',
			parameters: {partnum:partnum, quantityCase:quantityCase, quantityEach:quantityEach},
			onSuccess: function(data)
			{
				if (data.responseJSON)
				{
					if (data.responseJSON.success)
					{
						$(popInfoId).update('<img id="addImg" src="'+data.responseJSON.img+'" alt="" /> <div id="itemName"><strong>'+data.responseJSON.name+'</strong></div><br/><div id="itemDetails">Added <strong>'+data.responseJSON.qty+'</strong> item' + (data.responseJSON.qty != 1 ? 's' : '') + ' to cart</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);
					}
					else
					{
						showAddCartError(errormsg);
					}
				}
				else
				{
					showAddCartError(errormsg);
				}
			},
			onFailure: function(data)
			{
				if (data.responseJSON && data.responseJSON.message)
				{
					showAddCartError(data.responseJSON.message);
				}
				else
				{
					showAddCartError(errormsg);
				}
			}
		});
	}
	catch (err)
	{
		showAddCartError(errormsg);
	}
	
	return true;
}

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)
{
	$('cart-total').update(total);
	if (count == 1)
	{
		$('cart-count').update(count + ' item');
	}
	else
	{
		$('cart-count').update(count + ' items');
	}
}

function checkQuantityValue(value)
{
	if (value.length && (isNaN(value) || (value < 0) || (value != Math.round(value))))
	{
		return false;
	}
	
	return true;
}
