/**
 * InfoAspx
 * 
 * This namespace contains functions associated
 * with the /info.aspx file
 * 
 * @author Patrick Timm, patrick@isharp.dk
 * @version 2009.09.27
 */
InfoAspx = function()
{
	// the product ID (used in the id/name of all elements... stupid!)
	var productID
	
	// price configuration
	var priceMin
	var priceMax
	var priceStep
	
	// alert messages
	var alertPriceLow
	var alertPriceHigh
	var alertCityNotFound // not actually an alert
	var alertValidateAmount
	var alertValidateElse
	var alertValidateSMS
	var alertValidateSMSDiff
	var alertValidateSMSCode
	var alertValidateEmail
	var alertValidateEmailDiff
	
	
	
	
	/**
	 * Called when a default price is selected
	 *
	 * @param int The new amount/price 
	 * @return void
	 */
	function UpdatePrice(newPrice)
	{
		ClearCustomPrice()
		document.getElementById('product_'+ this.productID +'_qtyPrice').value = newPrice
	}
	
	
	
	
	/**
	 * Called when a default price is selected
	 *
	 * @return void
	 */
	function ClearCustomPrice()
	{
		var elm = document.getElementById('customTxt')
		if (elm != undefined)
			elm.style.visibility = 'hidden'
	}
	
	
	
	
	/**
	 * Called when the user has changed the value of
	 * the custom price box
	 *
	 * @return void
	 */
	function SetCustomPrice()
	{
		var value = document.getElementById('customPriceValueField').value
		var outPrice = ''

		try
		{
			value = value.replace(',', '').replace('.', '')
		}

		catch(e)
		{
		}

		if (!isNaN(value))
		{
			if(value>0)
			{
				if (value < parseInt(this.priceMin))
				{
					alert(this.alertPriceLow + ' ' + this.priceMin)
					return
				}
				
				else if (value > parseInt(this.priceMax))
				{
					alert(this.alertPriceHigh + ' ' + this.priceMax)
					return
				}
				
				else
				{
					outPrice = value - (value % this.priceStep)
					document.getElementById('dummyPriceRadio').checked = 'on'
				}
			}

			// if value is negative, empty the box
			else
				outPrice = ''
		}

		// if value is not a number, empty the box
		else
			outPrice = ''

		document.getElementById('customPriceValueField').value = outPrice
		document.getElementById('product_' + this.productID + '_qtyPrice').value = outPrice
	}
	
	
	
	
	/**
	 * 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(elementID)
	{
		// reset values
		document.getElementById('product_'+ this.productID + '_shippingSmsName').value = ''
		document.getElementById('product_'+ this.productID + '_shippingEmailName').value = ''
		document.getElementById('product_'+ this.productID + '_shippingName').value = ''
		document.getElementById('product_'+ this.productID + '_shippingAddress1').value = ''
		document.getElementById('product_'+ this.productID + '_shippingZip').value = ''
		document.getElementById('product_'+ this.productID + '_shippingCity').value = ''
		document.getElementById('product_'+ this.productID + '_shippingEmail').value = ''
		document.getElementById('product_'+ this.productID + '_shippingEmail2').value = ''
		document.getElementById('product_'+ this.productID + '_shippingCellphone').value = ''
		document.getElementById('product_'+ this.productID + '_shippingCellphone2').value = ''
		document.getElementById('product_'+ this.productID + '_cellphoneGreeting').value = ''

		// hide all input groups
		document.getElementById('recipientbox').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
		if (elementID != 'none')
			document.getElementById(elementID).style.display = 'block'

		// show email/sms delivery dates
		if (elementID == 'recipientboxEmail' || elementID == 'recipientboxSms')
		{
			document.getElementById('product_' + this.productID + '_deliveryDate').style.display = 'none'
			document.getElementById('product_' + this.productID + '_deliveryDateEmail').style.display = 'block'
			return
		}

		// show the "normal" delivery dates
		document.getElementById('product_' + this.productID + '_deliveryDate').style.display = 'block'
		document.getElementById('product_' + this.productID + '_deliveryDateEmail').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 custom pricing variables.
	 *
	 * @param int The minimum value
	 * @param int The maximum value
	 * @param int The step value
	 * @return void
	 */
	function SetPrices(pMin, pMax, pStep)
	{
		this.priceMin = pMin
		this.priceMax = pMax
		this.priceStep = pStep
	}
	
	
	
	
	/**
	 * 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
		}
	}
	
	
	
	
	/**
	 * Defines the value of the productID
	 *
	 * @param int The product ID
	 * @return void
	 */
	function SetProductID(x)
	{
		this.productID = x
	}
	
	
	
	
	/**
	 * Called when the user clicks the "next/go" button. The method
	 * validates the different inputs and shows a helpfull message to
	 * the user if an error is found.
	 *
	 * @dependency /js/oo/Phone.js
	 *
	 * @param string The current language ID (used to check SMS number)
	 * @return bool True if input is valid, false otherwise
	 */
	function Validate(languageID)
	{
		// validate amount
		// amount must be positive
		if (document.getElementById('product_'+ this.productID +'_qtyPrice').value <= 0)
		{
			alert(this.alertValidateAmount)
			return false
		}


		// validate SELF
		if (document.getElementById('who0').checked)
		{
			document.cardForm['product_'+ this.productID +'_emailDelivery'].value = 0
			document.cardForm['product_'+ this.productID +'_smsDelivery'].value = 0
			return true
		}
	
		// validate SOMEONE ELSE
		else if (document.getElementById('who1').checked)
		{
			if (
				document.getElementById('product_'+ this.productID +'_shippingName').value == ''
				||
				document.getElementById('product_'+ this.productID +'_shippingAddress1').value == ''
				||
				document.getElementById('product_'+ this.productID +'_shippingZip').value == ''
				||
				document.getElementById('product_'+ this.productID +'_shippingCity').value == ''
			   )
			{
				alert(this.alertValidateElse)
				return false
			}
		
			else
			{
				document.cardForm['product_'+ this.productID +'_emailDelivery'].value = 0
				document.cardForm['product_'+ this.productID +'_smsDelivery'].value = 0
				return true
			}
		}

		// validate SMS
		else if (document.getElementById('who3') != null && document.getElementById('who3').checked) 
		{ 
			var sName = document.getElementById('product_'+ this.productID +'_shippingSmsName')
			var sCell1 = document.getElementById('product_'+ this.productID +'_shippingCellphone')
			var sCell2 = document.getElementById('product_'+ this.productID +'_shippingCellphone2')

			// check sms-delivery data
			if ( sName.value == '' || sCell1.value == '')
			{
				// input is missing
				alert(this.alertValidateSMS)
				return false;
			}
			
			else if (sCell1.value != sCell2.value)
			{
				alert(this.alertValidateSMSDiff)
				return false
			}
			
			else
			{
				// input seems ok

				// get an estimate of the enterede number's language
				var glang = Phone.GuessLang(Phone.Clean(sCell1.value))
	
				// if languageId != number.lang, the user might have forgotten to
				// add the language code
				if (languageID != glang.lang && !glang.set && glang.lang != '?')
				{
					// did you forget to add the language code?
					// ok = yes
					// cancel = no
					if (!confirm(this.alertValidateSMSCode))
					{
						document.cardForm['product_'+ this.productID +'_emailDelivery'].value = 0
						document.cardForm['product_'+ this.productID +'_smsDelivery'].value = 1
						return true
					}
					
					return false
				}
				
				document.cardForm['product_'+ this.productID +'_emailDelivery'].value = 0
				document.cardForm['product_'+ this.productID +'_smsDelivery'].value = 1
				return true;
			}
		}

		// validate EMAIL
		else
		{
			regEx = new RegExp(/^([0-9a-zA-Z]+[-._+&])*[0-9a-zA-Z_]+@([-0-9a-zA-Z]+[.])+[a-zA-Z]{2,4}$/)

			if (
				document.getElementById('product_'+ this.productID +'_shippingEmailName').value == ''
				||
				document.getElementById('product_'+ this.productID +'_shippingEmail').value == ''
				||
				!(document.getElementById('product_'+ this.productID +'_shippingEmail').value.match(regEx))
			   )
			{
				alert(this.alertValidateEmail)
				return false
			}
			
			else if (document.getElementById('product_'+ this.productID +'_shippingEmail').value != document.getElementById('product_'+ this.productID +'_shippingEmail2').value)
			{
				alert(this.alertValidateEmailDiff)
				return false
			}
		
			else
			{
				document.cardForm['product_'+ this.productID +'_emailDelivery'].value = 1
				document.cardForm['product_'+ this.productID +'_smsDelivery'].value = 0
				return true
			}
		}
		return false
	}

	
	
	
	
	return {
		UpdatePrice:UpdatePrice,
		ClearCustomPrice:ClearCustomPrice,
		SetPrices:SetPrices,
		SetCustomPrice:SetCustomPrice,
		SetAlert:SetAlert,
		SetRecipient:SetRecipient,
		Zip2City:Zip2City,
		SetProductID:SetProductID,
		Validate:Validate
	}
}();
