/**
 * Product.js
 * 
 * This namespace contains functions associated
 * with the /Product.aspx file
 * 
 * @author Patrick Timm, patrick@isharp.dk
 * @author Nicklas Overgaard, nicklas@isharp.dk
 * @version 2010.08.04
 */
Product = function()
{
	
	// alert messages
	var alertCityNotFound // not actually an alert
	var alertValidateAmount
	var alertValidateElse
	var alertValidateSMS
	var alertValidateSMSDiff
	var alertValidateSMSCode
	var alertValidateEmail
	var alertValidateEmailDiff
	
	
	
	/**
	 * This function is called when the user selects
	 * a recipient group
	 *
	 * @param string The element ID of the group to show
	 * @return void
	 */
	function SetRecipient(element)
	{
		Debug.t('Product.SetRecipient')

		// hide all input groups
		document.getElementById('recipientBoxOther').style.display = 'none'
		document.getElementById('recipientBoxEmail').style.display = 'none'
		document.getElementById('recipientBoxSms').style.display = 'none'
		document.getElementById('recipientBoxSelf').style.display = 'none'

		// show the selected input group
		Debug.d(element)
		
		FixGiftwrap(element)
		
		if (element != 'none')
			document.getElementById(element).style.display = 'block'

		// show email/sms delivery dates
		if (element == 'recipientBoxEmail' || element == 'recipientBoxSms')
		{
			document.getElementById('inDeliveryDatePhysical').style.display = 'none'
			document.getElementById('inDeliveryDateSgk').style.display = 'block'
			return
		}

		// show the "normal" delivery dates
		document.getElementById('inDeliveryDatePhysical').style.display = 'block'
		document.getElementById('inDeliveryDateSgk').style.display = 'none'
		
		
	}

	
	/**
	 * Called when the user leaves the zipcode-input. The function looks up the
	 * zipcode in a city array and fills the city-input with the city name.
	 *
	 * @dependency /js/zipcode.js
	 *
	 * @param int The zipcode
	 * @return void
	 */
	function Zip2City(zip)
	{
		var elm = document.cardForm['product_' + this.productID + '_shippingCity']
		
		try
		{
			elm.value = cityArray[Number(zip.replace(' ', ''))]
		}

		catch(e)
		{
		}

		if (elm.value == 'undefined')
		{
			try
			{
				elm.value = this.alertCityNotFound
				elm.select()
				elm.focus()
			}

			catch(e)
			{
			}
		}
	}
	
	
	
	/**
	 * Set the value of a string that is
	 * shown to help the user
	 *
	 * @param string The string identifier (priceLow || priceHigh)
	 * @param string The new value
	 * @return void
	 */
	function SetAlert(key, txt)
	{
		switch (key)
		{
			case 'priceLow':
				this.alertPriceLow = txt
				break
				
			case 'priceHigh':
				this.alertPriceHigh = txt
				break
				
			case 'cityNotFound':
				this.alertCityNotFound = txt
				break
			
			case 'validateAmount':
				this.alertValidateAmount = txt
				break
			
			case 'validateElse':
				this.alertValidateElse = txt
				break
			
			case 'validateSMS':
				this.alertValidateSMS = txt
				break
			
			case 'validateSMSDiff':
				this.alertValidateSMSDiff = txt
				break
			
			case 'validateSMSCode':
				this.alertValidateSMSCode = txt
				break
				
			case 'validateEmail':
				this.alertValidateEmail = txt
				break
				
			case 'validateEmailDiff':
				this.alertValidateEmailDiff = txt
				break
				
			default:
				break
		}
	}
	
	function RestoreState(recipient)
	{
		Debug.t('Product.RestoreState')
		
		$('[value='+recipient+']').click()
	}
	
	/**
	 * Update UI regarding giftwrap elements based
	 * on delivery method
	 * @param string The element ID of the group to show
	 */
	function FixGiftwrap(groupId)
	{
		Debug.t('Product.FixGiftwrap')
		
		switch (groupId)
		{
			case "recipientBoxOther":
			case "recipientBoxSelf":
			{
				$('#giftwrap-options').show()
				$('#theNextButton').addClass("showproduct-next-giftwrap")
				break;
			}
			
			default:
			{
				$('#giftwrap-options').hide()
				$('#theNextButton').removeClass("showproduct-next-giftwrap")
				break;
			}
		}
	}

	return {
		"SetAlert" : SetAlert,
		"SetRecipient" : SetRecipient,
		"Zip2City" : Zip2City,
		"RestoreState" : RestoreState,
		"FixGiftwrap": FixGiftwrap
	}
}()
