
// General site layout and interactivity functions.

function finalizeLayout()
{
	var buttonElems = $$('a.on_off');
	bindEvents(buttonElems);
	
	function bindEvents(elements)
	{
		for ( i = 0; i < elements.length; i++ ) {
			Event.observe(elements[i], 'mouseover', swapImage.bindAsEventListener(elements[i], 1));
			Event.observe(elements[i], 'mouseout', swapImage.bindAsEventListener(elements[i], 0));
		}
	}
}

function swapImage(e, on)
{
	var element = Event.element(e);
	
	if ( on ) {
		element.src = element.src.replace(/_off./, '_on.');
	}
	else {
		element.src = element.src.replace(/_on/, '_off');
	}

}

// Order form functions.

function ShowShippingAddress()
{
	$('shipping_address').show();
}

function registerQuantityChange() 
{
	var quantityElems = $$('#cart_view .quantity_tag');
	bindEvents(quantityElems);
	
	function bindEvents(elements)
	{
		for ( i = 0; i < elements.length; i++ ) {
			Event.observe(elements[i], 'keyup', calculatePricing.bindAsEventListener(elements[i], 1));
		}
	}
}

function calculatePricing (e, triggerAction)
{
	var element = Event.element(e);
	
	var prodID = element.id.replace('Quantity_', '');
	var qty = element.value;
	
	
	var Req = new Ajax.Request('/store/update_cart_item.json.php',
			{
				method:'post',
				parameters: { 	ProductID: prodID, 
								Quantity: qty
							},
				onSuccess: function(transport){ displayNewPricing(transport) },
				onFailure: function(transport){ alert('Something went wrong...' + transport.responseText) }
			});
	
	return false;
	
	function displayNewPricing(transport)
	{
		var info = transport.headerJSON;
		
		if (info.UpdatedItem.Price != info.UpdatedItem.FullPrice) {
			$('fullprice_'+ info.UpdatedItem.ProductID +'_container').innerHTML = '$<strike>'+info.UpdatedItem.FullPrice+'</strike><br/>';
			$('discount_'+ info.UpdatedItem.ProductID +'_container').innerHTML = '('+info.UpdatedItem.Discount+'%)';
		}
		else {
			$('fullprice_'+ info.UpdatedItem.ProductID +'_container').innerHTML = '';
			$('discount_'+ info.UpdatedItem.ProductID +'_container').innerHTML = '';
		}

		$('price_'+ info.UpdatedItem.ProductID +'_container').innerHTML = info.UpdatedItem.Price;
		$('total_'+ info.UpdatedItem.ProductID +'_container').innerHTML = info.UpdatedItem.Total;
		$('grand_total_container').innerHTML = info.Cart.Subtotal;
	}
}

function subscriberChanged()
{
	var Req = new Ajax.Request('/store/set_subscriber.json.php',
			{
				method:'post',
				parameters: { 	IsSubscriber: $F('IsSubscriber') },
				onSuccess: function(transport){ subscriberPricingUpdate(transport) },
				onFailure: function(transport){ alert('Something went wrong...' + transport.responseText) }
			});
			
	return false;
	
	function subscriberPricingUpdate(transport)
	{
//		var info = transport.headerJSON;
//		
//		if (info.UpdatedItem.Price != info.UpdatedItem.FullPrice) {
//			$('fullprice_'+ info.UpdatedItem.ProductID +'_container').innerHTML = '$<strike>'+info.UpdatedItem.FullPrice+'</strike><br/>';
//			$('discount_'+ info.UpdatedItem.ProductID +'_container').innerHTML = '('+info.UpdatedItem.Discount+'%)';
//		}
//		else {
//			$('fullprice_'+ info.UpdatedItem.ProductID +'_container').innerHTML = '';
//			$('discount_'+ info.UpdatedItem.ProductID +'_container').innerHTML = '';
//		}
//
//		$('price_'+ info.UpdatedItem.ProductID +'_container').innerHTML = info.UpdatedItem.Price;
//		$('total_'+ info.UpdatedItem.ProductID +'_container').innerHTML = info.UpdatedItem.Total;
//		$('grand_total_container').innerHTML = info.Cart.Subtotal;
	}			
}

function productShowSection(sectionContainerId)
{
	$$('.product_info_section').invoke('hide');
	$(sectionContainerId).show();
	
	$$('.product_nav_container a').invoke('removeClassName', 'selected');
	$(sectionContainerId + '_link').addClassName('selected');
	
	
	return false;
}

function selectDiscountRadioOption(event)
{
	var element = event.element();
	
	switch ( element.name ) {
		case 'SV_CustomerName':
			$('discount1').click();
			break;	
		case 'SV_CustomerNumber':
			$('discount2').click();
			break;
		case 'SV_PromoCode':
			$('discount3').click();
			break;
	}
}

function verifySubscription()
{
	if ( $F('AgreeToTerms') ) {
		return true;	
	}	
	else {
		alert("You must agree to the terms and conditions.");
		return false;	
	}
}

function toggleProductDisplay(groupingNumber)
{
	var elementId = 'ProdGrouping' + groupingNumber;
	var ctvId = 'ProdCtv' + groupingNumber;
	
	if ( $(elementId).visible() ) {
		$(elementId).hide();
		$(ctvId).show();
	}
	else {
		$(elementId).show();
		$(ctvId).hide();
	}
	
	return false;
}

function initializeProductDisplay()
{
	var referringProductId = getQueryVariable('ReferringProductID');
	
	var tbodyElems = $$('#cart_view tbody.product_grouping');
	for ( i = 0; i < tbodyElems.length; i++ ) {
		
		var groupingRegex = new RegExp(/ProdGrouping([0-9]+)/)
		matches = groupingRegex.exec(tbodyElems[i].id);
		
		var inputElems = $$('#' + tbodyElems[i].id + ' input.quantity_tag');
		var itemsToShow = 0;
		for ( j = 0; j < inputElems.length; j++ ) {
			
			if ( referringProductId > 0 && inputElems[j].id == 'Quantity_' + referringProductId ) {
				itemsToShow++
			}
			
			if ( inputElems[j].value ) {
				itemsToShow += inputElems[j].value;
			}
		}
		
		if ( itemsToShow == 0 ) {
			toggleProductDisplay(matches[1]);
		}
	}
}
