/*
+--------------------------------------
|   Wiccle v1.00
|   ==================================
|   by Markus Loponen & Keith Killilea
|   (c) 2009-2010 Wiccle Ltd.
|   http://www.www.wiccle.com
|   Email: info@wiccle.com
|   ==================================
|   Time: 2010-04-16 | File version: 1.00
+-------------------------------------
|  	Please read the Wiccle license 
|   Available at docs/license.txt
+-------------------------------------
*/

function check_options()
{

	if (document.getElementById('delivery_option').value.length < 1)
	{
		alert('Please select a delivery option!');
		return false;
	}
	
	if (document.getElementById('method_of_payment').value.length < 1)
	{
		alert('Please select a method of payment!');
		return false;
	}
}

function check_delivery(delOption)
{
	var paySelect = document.getElementById('method_of_payment');

	paySelect.selectedIndex=0;
	
	var i = paySelect.options.length;

	while( i-- ) 
	{
		
		if ( delOption == 'shipping' && paySelect.options[i].value == 'cash')
		{
			paySelect.options[i].disabled=true;
		}
		else
		{
			paySelect.options[i].disabled=false;
		}
		
	}
}

function check_discount(discOption)
{
	var discSelect = document.getElementById('discount_option');

	discSelect.selectedIndex=0;
	
	var i = discSelect.options.length;
	
	while( i-- ) 
	{
		
		var thisValue = discSelect.options[i].value;
		
		thisValue = thisValue.replace('_discount', '');
		
		// enable option if payment method matches discount option, or if it's not defined as payment-specific discount
		
		if ( thisValue == discOption || !paymentMethods[thisValue] )
		{
			discSelect.options[i].disabled=false;
		}
		
		else
		{
			discSelect.options[i].disabled=true;
		}
	}
}

function fadeTooltip(itemId)
{
	var toolTip = 'item_' + itemId + '_ajax';
	if (toolTip)
		hideToolTip(toolTip);
		
	blankInner('add_item_message');
	
}

function collectCart(itemId)
{
	var itemExtras = '&extra=';
	
	var divList = document.getElementsByTagName('select');
	var i = divList.length;
	var divPrefix = 'extra_'+itemId+'_';
	
	while( i-- ) 
	{
		/* Tag column interiors */
		if( divList[i].id.indexOf(divPrefix) === 0 ) 
		{
			var tgtField = document.getElementById(divList[i].id);
					
			var extraType = divList[i].id.replace("extra_"+itemId+"_", "");
			itemExtras = itemExtras + extraType+'__'+tgtField.options[tgtField.selectedIndex].value + ";";
		}
	}
	
	
	submitAjaxGet('store_cart', 'cart&action=add_item&item_id='+itemId + itemExtras);
}

function expandCart()
{
	
}

function updateCart(itemId, action)
{
	if (action == "recount")
	{
		var qty = document.getElementById('qty').value;
		showLoader('store_cart', 'ajax_free_loader'); 
		submitAjaxGet('store_cart', 'cart&action=recount_item&item_id=' + itemId + '&' + 'qty=' + qty);
	}
	
	if (action == "drop_item")
	{
		showLoader('store_cart', 'ajax_free_loader'); 
		submitAjaxGet('store_cart', 'cart&action=recount_item&item_id=' + itemId + '&' + 'qty=0');
	}
	if (action == "undo_add")
	{
		showLoader('store_cart', 'ajax_free_loader'); 
		submitAjaxGet('store_cart', 'cart&action=undo_add&item_id=' + itemId);
	}
}



function itemActions(itemId, itemURL)
{

	var itemDivId = "item_actions_" + itemId;
	
	var divList = document.getElementsByTagName('div');

	var i = divList.length; 

	while( i-- ) 
	{
		/* Tag column interiors */
		
		var delItemDivId = divList[i].id;
		
		if( divList[i].id.indexOf('item_actions_') === 0 ) {
			document.getElementById(delItemDivId).style.display = "none";
		}
	}
	
	
	
	document.getElementById(itemDivId).style.display = "block";
	
	var itemQtyDiv = "item_"+itemId+"_qty";
	var itemQty = Number(document.getElementById(itemQtyDiv).innerHTML);
	document.getElementById(itemDivId).innerHTML = '<form onsubmit="updateCart(' + itemId + ', \'recount\'); return false;"><input name="qty" id="qty" value="' + itemQty + '" style="width: 20px; font-size: 100%;" class="input_field input_field_sub" onload /> <input type="button" value="Save" accesskey="s" class="button input_field_sub" onclick="updateCart(' + itemId + ', \'recount\');" /> <input type="button" value="Drop" accesskey="d" class="button input_field_sub" onclick="updateCart(' + itemId + ', \'drop_item\');" />  <input type="button" value="View" accesskey="v" class="button input_field_sub" onclick="goTo(\'' + itemURL +'\');" /> <input type="button" value="Cancel" accesskey="z" class="button input_field_sub" onclick="justHide(\'' + itemDivId + '\'); blankInner(\'' + itemDivId + '\');" /> <img src="templates/default/images/spacer.png" alt="" onload="document.getElementById(\'qty\').focus(); document.getElementById(\'qty\').select();" /></form>';
	

}
