
/**
 * Holds methods that interact with the
 * Redeem box in the sidebar
 */
RedeemBox = function()
{
	/**
	 * Holds a pointer to the
	 * amount field
	 * @type jQuery
	 */
	var jAmountField = null
	
	/**
	 * Determines whether the box is
	 * in "show amount"-mode
	 * @type bool
	 */
	var inAmountMode = false

	/**
	 * Initialises pointers etc
	 */
	function init()
	{
		Debug.trace('RedeemBox.init')
		
		// check that the input box actually exists
		jAmountField = $('#redeemBoxAmount')
		
		// if the html of the field is "null", the field
		// is not visible, ergo no codes present in this order
		inAmountMode = jAmountField.html() != null
	}
	
	/**
	 * Tells the redeem box to update it's amount.
	 * The method makes sure that it is in amount-mode
	 * before performing the actual update.
	 */
	function updateAmount()
	{
		Debug.trace('RedeemBox.updateAmount')
	
		if (!inAmountMode)
		{
			// nothing to do as there
			// is are no codes in the order
			return
		}
		
		// perform update
		Ajax.sendPost({"cmd":"redeembox-updateamount"}, RedeemBox.updateAmountComplete, RedeemBox.updateAmountError)
	}
	
	/**
	 * Completed "update amount"-request
	 */
	function updateAmountComplete(http, textStatus)
	{
		Debug.trace('RedeemBox.updateAmountComplete')
		
		var s = Ajax.getStatus(http)
		
		if (s.code == 200)
		{
			Debug.debug(s)
			jAmountField.html(s.body)
		}
		
		else
		{
			Debug.debug(s.message)
		}
	}
	
	/**
	 * An error occured with the "update amount"-request
	 */
	function updateAmountError(httpRequest, textStatus, errorThrown)
	{
		Debug.trace('RedeemBox.updateAmountError')
		
		Debug.debug('An error occured while updating the redeem box amount')
	}

	return {
		"init" : init,
		"updateAmount" : updateAmount,
		"updateAmountComplete" : updateAmountComplete,
		"updateAmountError" : updateAmountError
	}
}()


$(document).ready(function()
{
	RedeemBox.init()
})
