
// REFERAL

	// Referal Earning
	var referalEarningCount = 0;
	function referalEarning(_formObject)
	{
		referalEarningCount=0;
		for (var i = 0; i < _formObject.length; i++) 
		{
			if (_formObject[i].value.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\..{2,2}))$)\b/gi) && _formObject[i].value != 'example@example.com')
				referalEarningCount++; 
		}	
		document.getElementById('potentialEarnings').innerHTML = '$'+(referalEarningCount*5);	
	}
	
	
	

// CART

	// Product - Add To Cart
	function cartAddProduct(_required,_group)
	{	
		// Vars.
		var count = 0;
		document.getElementById('addtocart_error').style.display = "none";
		document.getElementById('addtocart_error_group').style.display = "none";
		document.getElementById('addtocart_error_delivery').style.display = "none";	
		
		// Delivery Option
		if(document.getElementById('product_delivery') && document.getElementById('product_delivery').value == '')
		{
			document.getElementById('addtocart_error_delivery').style.display = "block";
			return;
		}	
		
		// Type
		document.addtocart.type.value = (_group)?'group':'product';
		
		// Verify
		for (var i = 0; i < document.addtocart.length; i++) 
			if(document.addtocart[i].name.match(/^products/))
				count += !isNaN(parseInt(document.addtocart[i].value))?parseInt(document.addtocart[i].value):0; 
				
		if(count%_required > 0 || count == 0)	
		{
			if(_group)
				document.getElementById('addtocart_error_group').style.display = "block";
			else
				document.getElementById('addtocart_error').style.display = "block";
		}
		else 
			document.addtocart.submit();	
	}
	
	// Programs - Add To Cart
	function cartAddPrograms(_id,_type,_submit)
	{	
		// Set
		document.getElementById('program_'+_id+'_'+_type+'_error').style.display = "none";	
	
		// Errors
		if(_submit)
		{
			if(document.getElementById('program_'+_id+'_'+_type+'_delivery').value == '')
				var error = '<strong>Sorry.</strong> Select Delivery Type';
			else if(document.getElementById('program_'+_id+'_'+_type+'_duration').value == '')
				var error = '<strong>Sorry.</strong> Select Duration';
			else if(document.getElementById('program_'+_id+'_'+_type+'_quantity').value == '')
				var error = '<strong>Sorry.</strong> Select Quantity';	
				
			if(error)	
			{
				document.getElementById('program_'+_id+'_'+_type+'_error').style.display = "block";		
				document.getElementById('program_'+_id+'_'+_type+'_error').innerHTML = error;
			}
		}
	
		// Price
		if(!isNaN(parseInt(document.getElementById('program_'+_id+'_price').value)) && !isNaN(parseInt(document.getElementById('program_'+_id+'_'+_type+'_duration').value)) && !isNaN(parseInt(document.getElementById('program_'+_id+'_'+_type+'_quantity').value)))
		{
			// Price
			var price = parseInt(document.getElementById('program_'+_id+'_price').value)*parseInt(document.getElementById('program_'+_id+'_'+_type+'_duration').value)*parseInt(document.getElementById('program_'+_id+'_'+_type+'_quantity').value);
			
			// Season
			if(_type == 'season')
			{
				price = price*4;	
				discount = parseFloat(document.getElementById('program_discount_season').value);
				price = price-(price*discount);	
			}
			
			// Discount
			if(document.getElementById('program_discount_'+_type+'_'+document.getElementById('program_'+_id+'_'+_type+'_duration').value))
			{
				var discount = parseFloat(document.getElementById('program_discount_'+_type+'_'+document.getElementById('program_'+_id+'_'+_type+'_duration').value).value);
				price = price-(price*discount);
			}			
			
			// Discount
			if(document.getElementById('program_'+_id+'_'+_type+'_delivery').value == 'pickup')
			{
				if(_type == 'week')
					price -= (parseFloat(document.getElementById('program_discount_pickup').value)*parseInt(document.getElementById('program_'+_id+'_'+_type+'_duration').value))*parseInt(document.getElementById('program_'+_id+'_'+_type+'_quantity').value);
				else if(_type == 'day')
					price -= (parseFloat(document.getElementById('program_discount_pickup').value)*(Math.ceil(parseInt(document.getElementById('program_'+_id+'_'+_type+'_duration').value)/3)))*parseInt(document.getElementById('program_'+_id+'_'+_type+'_quantity').value);
				else if(_type == 'season')
					price -= ((parseFloat(document.getElementById('program_discount_pickup').value)*(Math.ceil(parseInt(document.getElementById('program_'+_id+'_'+_type+'_duration').value)/3)))*4)*parseInt(document.getElementById('program_'+_id+'_'+_type+'_quantity').value);
			}
			
			
			//Set
			document.getElementById('program_'+_id+'_'+_type+'_total').innerHTML = formatCurrency('$'+price);
			
		}
		else
			document.getElementById('program_'+_id+'_'+_type+'_total').innerHTML = '---';
			
			
		// Form
		document.addtocart.id.value = _id
		document.addtocart.type.value =  _type
		document.addtocart.delivery.value = document.getElementById('program_'+_id+'_'+_type+'_delivery').value;
		document.addtocart.duration.value =  document.getElementById('program_'+_id+'_'+_type+'_duration').value;
		document.addtocart.quantity.value = document.getElementById('program_'+_id+'_'+_type+'_quantity').value;
		if(document.getElementById('program_'+_id+'_'+_type+'_coolerbag'))
			document.addtocart.coolerbag.value = document.getElementById('program_'+_id+'_'+_type+'_coolerbag').value;
		
		// Submit
		if(!error && _submit)
			document.addtocart.submit();	
	}
	
	// Cart
	function ajaxCart(_action,_id,_objectId) 
	{
		// Actions
		var actions = new Object();
		
		// Remove
		actions.remove = new Object();
		actions.remove.url = '/cart/action/remove/'+encodeURIComponent(_id);
		actions.remove.method = 'get';
		actions.remove.sucess = function()
		{
			var removeObject=document.getElementById(_objectId)
			removeObject.parentNode.removeChild(removeObject);				
		}
		
		// Post
		var cart = new httpRequest ();
		cart.url = actions[_action].url;
		cart.parameters = "ajax=1";
		cart.dataType = 'json';	
		cart.success = function(data) 
		{ 
			actions[_action].sucess();
			document.getElementById('cartSubTotal').innerHTML = data.subtotal;	
			document.getElementById('cartQunatity').innerHTML = data.size+' Items - $ '+data.subtotal;	
		}
		if(actions[_action].method == 'post')	
			cart.post();
		else if(actions[_action].method == 'get')	
			cart.get();		
	}	


// CHECKOUT

	// Options
	var checkoutOptions = {
			pickupSelected:false,
			datesSelected:false,
			timesSelected:false,
			timesRequired:false,
			postalcodes:{
					'main':false,
					'alternative':false
					},
			alternativeAddress:false,
			alternativeAddressType:''
			};
			
				
	// Update cart
	function updateCartItems()
	{
		var cart = new httpRequest ();
		cart.url = '/cart/billing/items';
		cart.parameters = 'cart=1';
		cart.success = function(_html) 
		{	
			if(_html == 'refresh')
				location.href= '/cart/billing';
			else
				document.getElementById('ajaxCartOverview').innerHTML = _html;				
		}
		cart.post();		
	}
	
	// Remove
	function removeCartItem(_id)
	{
		var cart = new httpRequest ();
		cart.url = '/cart/action/remove';
		cart.parameters = 'id='+_id+'&ajax=1';
		cart.dataType = 'json';
		cart.success = function(data) 
		{	
			if(data.size == 0)
				location.href = "/cart";
			else
			{
				setAlternativeAddress(data.alternativeType,data.alternativeMessage);
				updateCartItems();
			}
		}
		cart.post();	
	}
	
	
	// Checkout options
	function setCheckoutOptions(_inputId, _id)
	{
		// Reset
		resetErrors();
		
		// Set
		document.getElementById(_inputId).value = _id;
			
		// Set Date/Time
		if(_inputId == 'delivery_location')
			setDeliveryDateTimeOption((_id=='1')?true:false);
		
		// Clear Billing
		if(_inputId == 'checkout_sameasshipping' && (_id == '1' || _id == '2'))	
			validateFormClear(document.checkoutform,'address[billing]'); 
			
	}		
	
	
	// Delivery date/time options
	function setDeliveryDateTimeOption(_pickup)
	{
		return;	
		// Section
		if(_pickup)
		{
			validateFormClear(document.checkoutform,'address[shipping]'); 
			validateFormClear(document.checkoutform,'address[alternative]'); 
			document.getElementById('delivery_datetime_html').style.display = "block";
			document.getElementById('main_postal_error_message_html').style.display = "none";
			document.getElementById('delivery_time_delivery').style.display = "none";
			document.getElementById('delivery_time_pickup').style.display = "block";			
		}
		
		else
		{
			document.getElementById('delivery_time_delivery').style.display = "block";
			document.getElementById('delivery_time_pickup').style.display = "none";		
		}
		
		if(!_pickup && !checkoutOptions.postalcodes['main'] && document.getElementById('checkout_type').value != 'session')
		{
			document.getElementById('delivery_date').value = '';
			document.getElementById('delivery_date_html').innerHTML = 'Choose a date...';
			checkoutOptions.alternativeAddress = false;
			document.getElementById('delivery_datetime_html').style.display = "none";
			document.getElementById('main_postal_error_message_html').style.display = "none";
		}
		
		if(!_pickup && checkoutOptions.postalcodes['main'])
		{
			document.getElementById('delivery_datetime_html').style.display = "block";
		}
		
		// Title
		if(_pickup)
		{
			document.getElementById('delivery_address_html_title').innerHTML = 'Select Pick Up Location';
			document.getElementById('delivery_date_html_title').innerHTML = 'Select Pick Up Date';
			if(document.getElementById('delivery_time_html_title'))
				document.getElementById('delivery_time_html_title').innerHTML = 'Select Pick Up Time';		
		}
		
		else 
		{
			document.getElementById('delivery_address_html_title').innerHTML = 'Select Delivery Location';
			document.getElementById('delivery_date_html_title').innerHTML = 'Select Delivery Date';
			if(document.getElementById('delivery_time_html_title'))
				document.getElementById('delivery_time_html_title').innerHTML = 'Select Delivery Time';
		}		
		
		// Same as shipping option
		sameAsAlternativeAddress();
		
		// Instruction
		if(_pickup)
			document.getElementById('special_delivery_instructions').style.display = "none";
		else
			document.getElementById('special_delivery_instructions').style.display = "block";
		
		// Error
		document.getElementById('error_message_html').style.display = "none";
		
		// Alternative address
		if(_pickup)
		{
			document.getElementById('address_alternative_html').style.display = "none";
		}
		else if(checkoutOptions.alternativeAddress)
			document.getElementById('address_alternative_html').style.display = "block";	
				
		// Pick up discount / Delivery shipping
		var action = new httpRequest ();
		action.url = '/cart/action/pickup';
		action.parameters = "option="+encodeURIComponent(((_pickup)?'1':'0'));		
		action.success = function(data) 
		{
			updateCartItems();							
		};
		action.post();	
	}
	
	
	// Postal Code
	function setPostalCode(_postalCode, _type)
	{
		if(!checkoutOptions.pickupSelected)
		{
			// Reset
			checkoutOptions.postalcodes[_type] = false;
			document.getElementById(_type+'_postal_error_message_html').style.display = "none";	
			
			if(_postalCode.replace(' ','').length >= 6 || _postalCode=='')
			{				
				var post = new httpRequest ();
				post.url = '/cart/action/deliveryoptions';
				post.parameters = "postalcode="+encodeURIComponent(_postalCode)+"&type="+_type;		
				post.success = function(data) 
				{					
					// Set			
					if(data == 'true')
					{		
						resetErrors();				
						checkoutOptions.postalcodes[_type] = true;
						if(_type == 'main')
							setDeliveryDateTimeOption(false);
					}
					else 
					{
						if(_postalCode != '')
							document.getElementById(_type+'_postal_error_message_html').style.display = "block";
					}
					
					// Update	
					sameAsAlternativeAddress();
					updateCartItems();	
				};
				post.post();			
			}
			else if(_type == 'main')
			{
				validateFormClear(document.checkoutform,'address[alternative]'); 	
				sameAsAlternativeAddress();			
			}
		}
	}
		
	
	// Address
	function getAddress(_htmlId, _addressId, _type)
	{
		// Set
		if(_addressId != '')
		{
			var address = new httpRequest ();
			address.url = '/myaccount/action';
			address.parameters = "addressjson="+encodeURIComponent(_addressId)
			address.dataType = 'json';		
			address.success = function(data) 
			{
				// Html
				var html = '<p>';
				html += ''+data.address1+' '+data.address2+'<br />';
				html += ''+data.city+', '+data.region+' '+data.postalcode+'';
				html += '</p>';			
				document.getElementById(_htmlId).innerHTML = html;
				
				// Set
				if(_type != '')
					setPostalCode(data.postalcode, _type);	
				
			};
			address.post();	
		}
		
		// Clear
		else
		{
			document.getElementById(_htmlId).innerHTML = '';
			if(_type != '')
				setPostalCode('', _type);		
		}
	}
	
	
	// Alternative address
	function setAlternativeAddress(_type, _message)
	{		
		if(!checkoutOptions.pickupSelected)
		{
			if(checkoutOptions.alternativeAddress && _type == '')
				setPostalCode('', 'alternative');
			
			// Set
			checkoutOptions.alternativeAddressType = _type;
			checkoutOptions.alternativeAddress = false;
			
			// Weekday/Weekend
			if(_type != '')
			{
				checkoutOptions.alternativeAddress = true;
				document.getElementById('address_alternative_html').style.display = "block";	
				document.getElementById('address_alternative_html_title').innerHTML =  _type;		
				document.getElementById('address_alternative_html_message').innerHTML =  _message;
				if(!checkoutOptions.pickupSelected)
					document.getElementById('address_alternative_html').style.display = "block";
				else
				{
					document.getElementById('address_alternative_html').style.display = "none";	
				}
			}
			else
			{
				document.getElementById('address_alternative_html').style.display = "none";
			}
			
			sameAsAlternativeAddress();
		}
	} 
	
	// Same as
	function sameAsAlternativeAddress()
	{
		if(document.getElementById('billingAddress-same'))
		{
			if(!checkoutOptions.postalcodes['main'])
				checkoutOptions.postalcodes['alternative'] = false;
			
			if(checkoutOptions.pickupSelected)
			{
				document.getElementById('billingAddress-same').style.display = "none";
				document.getElementById('billingAddress-alternative').style.display = "none";	
			}
			
			else
			{	
				
				// Rest
				document.getElementById('billingAddress-same').style.display = "none";
				if(checkoutOptions.postalcodes['main'])
				{
					document.getElementById('billingAddress-same').style.display = "block";
					document.getElementById('billing_sameas_1_html').innerHTML = "Same as delivery location?";
				}
				if(document.getElementById('billingAddress-alternative'))
					document.getElementById('billingAddress-alternative').style.display = "none";	
				
				// Set
				if(document.getElementById('billingAddress-alternative') && checkoutOptions.alternativeAddressType != '' && checkoutOptions.postalcodes['alternative'])
				{		
					document.getElementById('billing_sameas_1_html').innerHTML = "Same as "+((checkoutOptions.alternativeAddressType=='Weekday')?'weekend':'weekday')+" delivery location?";
					document.getElementById('billing_sameas_2_html').innerHTML = "Same as "+checkoutOptions.alternativeAddressType.toLowerCase()+" delivery location?";
					document.getElementById('billingAddress-alternative').style.display = "block";	
				} 
				else
					if(document.getElementById('checkout_sameasshipping').value == '2')
					{
						document.getElementById('checkout_sameasshipping').value == '1'; 
						document.getElementById('billingAddress-same').className = 'twoCols-sectionLeftWhite active';
						document.getElementById('billingAddress-alternative').className = 'twoCols-sectionLeftWhite';
					}				
			}
	}
	}
	
	
	// Set error
	function setError(_message)
	{
		// Valid
		if(_message == '')
		{
			document.getElementById('error_message_html').style.display = "none";
			return true;
		}
		
		// Invalid
		else 
		{
			document.getElementById('error_message_html').style.display = "block";
			document.getElementById('error_message_html_message').innerHTML = _message;
			return false;
		}
	}
	
	// Reset error
	function resetErrors()
	{
		if(document.getElementById('delivery_error_datehold'))
			document.getElementById('delivery_error_datehold').style.display = "none";	
		if(document.getElementById('error_message_html'))	
			document.getElementById('error_message_html').style.display = "none";
		if(document.getElementById('main_postal_error_message_html'))	
			document.getElementById('main_postal_error_message_html').style.display = "none";	
		if(document.getElementById('alternative_postal_error_message_html'))
			document.getElementById('alternative_postal_error_message_html').style.display = "none";		
	}
	
	
	// Checkout
	function checkout() 
	{
		// Reset
		var valid = setError('');
		resetErrors();		
		
		// Memebership
		if(document.getElementById('checkout_type').value == 'member' && !validateForm(document.memberform,'color','#FF0000','requiredAccount',''))
			valid = setError(checkoutErrorMessages.mebershipInformation);
		
		// Shipping address  
		if(valid && !document.getElementById('address_shipping_id') && !checkoutOptions.pickupSelected && !validateForm(document.checkoutform,'color','#FF0000','requiredShippingAddress',''))
			valid = setError(checkoutErrorMessages.shippingAddress);
			
		if(valid && !checkoutOptions.pickupSelected && document.getElementById('address_shipping_id') && document.getElementById('address_shipping_id').value == '')
			valid = setError(checkoutErrorMessages.shippingAddressDropDown);	
		
		// Postal code
		if(valid && !checkoutOptions.pickupSelected && !checkoutOptions.postalcodes['main'])
			valid = setError(checkoutErrorMessages.invalidPostalCode);
					
		// Delivery date
		if(valid && !checkoutOptions.datesSelected)
		{
			if(checkoutOptions.pickupSelected)
				valid = setError(checkoutErrorMessages.deliveryDate.replace('delivery','pick up'));
			else
				valid = setError(checkoutErrorMessages.deliveryDate);			
		}
		
		// Delivery time
		if(valid && checkoutOptions.timesRequired && !checkoutOptions.timesSelected)
		{
			if(checkoutOptions.pickupSelected)
				valid = setError(checkoutErrorMessages.deliveryTime.replace('delivery','pick up'));
			else
				valid = setError(checkoutErrorMessages.deliveryTime);
		}
		
		// Alternative address
		if(valid && !checkoutOptions.pickupSelected && checkoutOptions.alternativeAddress)
		{	
			var empty = true;
			for (var i = 0; i < document.checkoutform.length; i++)
				if(document.checkoutform[i].className.match(RegExp("^requiredAlternativeShippingAddress|\ requiredAlternativeShippingAddress")) && document.checkoutform[i].value != '') 
					empty = false;
						
			if(!empty && !validateForm(document.checkoutform,'color','#FF0000','requiredAlternativeShippingAddress',''))
				valid = setError(checkoutErrorMessages.alternativeShippingAddress);
			
			// Postal code	
			if(valid && document.getElementById('address_alternative_id') && document.getElementById('address_alternative_id').value != '' && !checkoutOptions.postalcodes['alternative'])
				valid = setError(checkoutErrorMessages.invalidPostalCode.replace('delivery','alternative delivery'));
			if(valid && document.getElementById('alternativePostalCode') && document.getElementById('alternativePostalCode').value != '' && !checkoutOptions.postalcodes['alternative'])
				valid = setError(checkoutErrorMessages.invalidPostalCode.replace('delivery','alternative delivery'));	
		}		
		
		// Billing address
		if(valid && ((document.getElementById('checkout_sameasshipping').value != '1' && document.getElementById('checkout_sameasshipping').value != '2') || checkoutOptions.pickupSelected) && !validateForm(document.checkoutform,'color','#FF0000','requiredBillingAddress',''))
			valid = setError(checkoutErrorMessages.billingAddress);
		
		// Credit card
		if(valid && !validateForm(document.checkoutform,'color','#FF0000','requiredCheckout',''))
			valid = setError(checkoutErrorMessages.creditCardInformation);
		
		// Terms
		if(valid && document.getElementById('checkout_termsofsale').value != '1')
			valid = setError(checkoutErrorMessages.terms);
		
		// Memeber email check
		if(valid && document.getElementById('checkout_type').value == 'member')
		{
			var days = new httpRequest ();
			days.url = '/myaccount/action';
			days.parameters = "verifyemail="+encodeURIComponent(document.getElementById('checkoutMemeberEmail').value);		
			days.success = function(data) 
			{
				if(data == 'exists')
				{	
					setError(checkoutErrorMessages.memebershipEmailExists);
				}
				else 
					document.checkoutform.submit();					
			};
			days.post();	
			valid = false;	
		}	
			
		// Submit
		else if(valid)
			document.checkoutform.submit();
	}
	

// TIME PICKER
function timePicker(_mouseEvent,_id) 
{				
	// Close
	closeCalandar();
	
	// Position
	_mouseEvent = (_mouseEvent) ? _mouseEvent : ((window.event) ? event : null);
				
	var scrollPostion = getScrollingPosition();		
	
	// Layout	
	document.getElementById('selectTime-popup').style.display = "block";	
	var posX = _mouseEvent.clientX-(document.getElementById('selectTime-popup').offsetWidth/2)+scrollPostion[0];			
	var posY = _mouseEvent.clientY-(document.getElementById('selectTime-popup').offsetHeight+20)+scrollPostion[1];	
	document.getElementById('selectTime-popup').style.top =  posY+"px";
	document.getElementById('selectTime-popup').style.left =  posX+"px";	
	
	// Set
	var times = document.getElementById('selectTime-popup').getElementsByTagName('div');
	for(i=0; i<times.length; i++)
		if(times[i].className.match('select-time twoCols-sectionLeftWhite'))
		{
			// Active
			if(document.getElementById('item_selected_time_display_'+_id).innerHTML == document.getElementById(times[i].id+'_display').value)
				times[i].className = 'select-time twoCols-sectionLeftWhite active';
			else
				times[i].className = 'select-time twoCols-sectionLeftWhite';
			
			// Function
			times[i].onclick = function() 
			{
				var cart = new httpRequest ();
				cart.url = '/cart/action/deliverytimes';
				cart.parameters = 'id='+_id+'&time='+encodeURIComponent(document.getElementById(this.id+'_display').value);
				cart.dataType = 'json';
				cart.success = function(data)  
				{	
					checkoutOptions.timesSelected = data.timesSelected;
					document.getElementById('item_select_time_'+data.id).style.display = "none";
					document.getElementById('item_selected_time_'+data.id).style.display = "block";
					document.getElementById('item_selected_time_display_'+data.id).innerHTML = data.time;	
					document.getElementById('selectTime-popup').style.display = "none";	
					updateCartItems();	
				}
				cart.post();		
			}
		}
}


// DATE PICKER

	// Vars.
	var suggestionsDetailsData = new Object();

	// Set
	var deliveryDatePicker = new monthlyCalendar('deliveryDatePicker','popupCalandar');
	deliveryDatePicker.availableDuration = 12;
	deliveryDatePicker.width = 850;
	deliveryDatePicker.onClickClose = false;
	deliveryDatePicker.onClickClose = false;
	deliveryDatePicker.setEmptyInput = false;
	deliveryDatePicker.appendHtml = '<div id="deliveryDateSuggestionsButtonClose" class="buttonRight buttonPurple"><a href="javascript:void(0)" onclick="closeCalandar()">X</a></div><div id="deliveryDateSuggestions"><div id="deliveryDateSuggestionsText"></div><div id="deliveryDateSuggestionsButton" class="buttonRight buttonPurple"><a href="javascript:void(0)" onclick="updateDeliverDate()">Update</a></div><div id="deliveryDateSuggestionsButtonOk" class="buttonRight buttonPurple"><a href="javascript:void(0)" onclick="closeCalandar()">Ok</a></div></div><div class="clear"></div><div id="deliveryDateSuggestionsPreloader"><img src="/images/deliveryDate-Preloader.gif" width="35" height="35" /></div>';
	deliveryDatePicker.hideExcludedDays = true;		

		// Exlude days
		deliveryDatePicker.setExcludedDays = function (_month, _year, _arguments) 
		{
			if(document.getElementById('selectTime-popup'))
				document.getElementById('selectTime-popup').style.display = "none";				
			document.getElementById('deliveryDateSuggestionsPreloader').style.display = "block";
			document.getElementById('popupCalandar_calendar').style.visibility = "hidden";
			var post = new httpRequest ();
			post.url = '/cart/action/deliverydates';
			post.parameters = "excludeddates="+_year+'/'+_month+'&id='+_arguments.id;
			post.dataType = 'json';
			post.success = function(data) {	
			
				    // Dates
					deliveryDatePicker.excludeDays = data.days.split(',');
					deliveryDatePicker.insertHTML();
					document.getElementById('deliveryDateSuggestionsPreloader').style.display = "none";
					document.getElementById('popupCalandar_calendar').style.visibility = "visible";
					
					// Error
					if(data.html)
						setCalendarInformation(data.html,'');					
				};
			post.post();
		}
	
		// Select		
		deliveryDatePicker.onSelected = function(_day, _month, _year, _arguments) 
		{
			resetCalendar();
			var post = new httpRequest ();
			post.url = '/cart/action/deliverydates';
			post.parameters = "verificaiton="+pad(_month,2)+'/'+_day+'/'+_year+'&id='+_arguments.id;
			post.dataType = 'json';
			post.success = function(data) 
			{
					suggestionsDetailsData = data.details;		
					checkoutOptions.timesSelected = data.timesSelected;	
						
					if(data.available)
					{						
						checkoutOptions.datesSelected = data.datesSelected;
						
						if(checkoutOptions.datesSelected)
							updateCartItems();						
						
						document.getElementById('item_select_date_'+_arguments.id).style.display = "none";
						document.getElementById('item_selected_date_'+_arguments.id).style.display = "block";
						document.getElementById('item_selected_date_display_'+_arguments.id).innerHTML = data.fullDate;						
						
						if(data.exemptions)
							setCalendarInformation(data.html,'ok');						
						else
							closeCalandar();						
						setAlternativeAddress(data.alternativeType,data.alternativeMessage);					
					}
					
					else 
					{
						checkoutOptions.datesSelected = data.datesSelected;
						
						document.getElementById('item_select_date_'+_arguments.id).style.display = "block";
						document.getElementById('item_selected_date_'+_arguments.id).style.display = "none";
						document.getElementById('item_selected_date_display_'+_arguments.id).innerHTML = '';
						
						setCalendarInformation(data.html,(data.updateButton)?'update':'');
					}
			};
			post.post();			
		}
	
	
	// Reset
	function resetCalendar()
	{
		document.getElementById('deliveryDateSuggestionsText').innerHTML = '';
		document.getElementById('deliveryDateSuggestionsPreloader').style.display = "block";
		document.getElementById('deliveryDateSuggestionsButton').style.display = "none";
		document.getElementById('deliveryDateSuggestionsButtonOk').style.display = "none";		
		if($('#popupCalandar').css('width') != '800px')
			document.getElementById('popupCalandar_calendar').style.visibility = "hidden";		
	}
	
	
	// Information
	function setCalendarInformation(_html,_button)
	{
		// Set
		function set()
		{
			document.getElementById('deliveryDateSuggestionsPreloader').style.display = "none";
			document.getElementById('deliveryDateSuggestionsPreloader').style.left = "574px";
			document.getElementById('deliveryDateSuggestionsText').innerHTML = _html;
			if(_button == 'update')
				document.getElementById('deliveryDateSuggestionsButton').style.display = "block";
			if(_button == 'ok')
				document.getElementById('deliveryDateSuggestionsButtonOk').style.display = "block";
			document.getElementById('popupCalandar_calendar').style.visibility = "visible";					
		}
		
		// Set/Animate	
		if($('#popupCalandar').css('width') == '800px')
			set();
		else 
			$('#popupCalandar').animate({width:800}, 300,function(){ set(); });	
	}
		
	
	// Close
	function closeCalandar()
	{
		if(document.getElementById('popupCalandar'))
		{
			document.getElementById('popupCalandar').style.display = "none";
			document.getElementById('deliveryDateSuggestionsPreloader').style.left = "182px";
			document.getElementById('popupCalandar').style.width = "400px";	
			document.getElementById('deliveryDateSuggestionsText').innerHTML = '';
			document.getElementById('deliveryDateSuggestionsPreloader').style.display = "block";
			document.getElementById('deliveryDateSuggestionsButton').style.display = "none";
			document.getElementById('deliveryDateSuggestionsButtonOk').style.display = "none";	
		}
	}
	
	// Close Time
	function closeTime()
	{
		document.getElementById('selectTime-popup').style.display = "none";
	}
	
	// Details
	function suggestionsDetails(_id)
	{
		var i = 1;
		while(document.getElementById('suggestions_details_'+i))	
		{		
			document.getElementById('suggestions_details_'+i).innerHTML = '';
			i++;
		}
		if(document.getElementById('suggestions_details_'+_id))
			document.getElementById('suggestions_details_'+_id).innerHTML = suggestionsDetailsData[_id];		
	}
	
	// Update delivery date
	function updateDeliverDate()
	{
			var setHold = '1';	
			var i = 1;
			while(document.getElementById('deliveryDateSuggestionOptions_'+i))	
			{		
				if(document.getElementById('deliveryDateSuggestionOptions_'+i).checked) 
					setHold = document.getElementById('deliveryDateSuggestionOptions_'+i).value;
				i++;
			}
		
			resetCalendar();
			var post = new httpRequest ();
			post.url = '/cart/action/deliverydates';
			post.parameters = 'suggestion='+setHold+'&id='+deliveryDatePicker.arguments.id;
			post.dataType = 'json';
			post.success = function(data) 
			{	
				suggestionsDetailsData = data.details;
				checkoutOptions.timesSelected = data.timesSelected;	
				
				if(data.available)
				{	
					checkoutOptions.datesSelected = data.datesSelected;
				
					if(data.available && data.exemptions)
						setCalendarInformation(data.html,'ok');	
					else
						closeCalandar();
					
					setAlternativeAddress(data.alternativeType,data.alternativeMessage);					
					
					document.getElementById('item_select_date_'+deliveryDatePicker.arguments.id).style.display = "none";
					document.getElementById('item_selected_date_'+deliveryDatePicker.arguments.id).style.display = "block";
					document.getElementById('item_selected_date_display_'+deliveryDatePicker.arguments.id).innerHTML = data.fullDate;
					document.getElementById('item_selected_date_input_'+deliveryDatePicker.arguments.id).value = data.date;					
					
					updateCartItems();						
				}
					
				else 
				{
					checkoutOptions.datesSelected = data.datesSelected;
					setCalendarInformation(data.html,(data.updateButton)?'update':'');
					document.getElementById('delivery_date').value = '';
					document.getElementById('delivery_date_html').innerHTML = 'Choose a date...';
				}
			};
			post.post();		
	}
	
	

// MY ACCOUNT

	// Postal
	function verifyFormPostalCode(_form,_postal,_messageId,_valid)
	{
		if(_valid)
		{			
			var verify = new httpRequest ();
			verify.url = '/cart/action/deliveryoptions';
			verify.parameters = "postalcode="+encodeURIComponent(_postal);		
			verify.success = function(data) 
			{
				if(data == 'true')
					_form.submit();
				else 					
					document.getElementById(_messageId).style.display = "block";	
			};
			verify.post();	
		}
		return false;
	}

// GLOBAL

	// Open window
	function openWindow(loadUrl,pageWidth,pageHeight,toolBar,menuBar,locationBar,scrollBar,resize) 
	{
		var intWidth = pageWidth;
		var intHeight = pageHeight;
		var intLeft = (screen.availWidth / 2) - (intWidth / 2 );
		var intTop = (screen.availHeight / 2) - (intHeight / 2 );
		var setToolBar = (toolBar)?'yes':'no';
		var setMenuBar = (menuBar)?'yes':'no';
		var setLocationBar = (locationBar)?'yes':'no';	
		var setScrollBar = (scrollBar)?'yes':'no';
		var setResize = (resize)?'yes':'no';
		var openWin = window.open(loadUrl, "contentWindow", 'toolBar='+setToolBar+',menubar='+setMenuBar+',locationBar='+setLocationBar+',scrollbars='+setScrollBar+',resizable='+setResize+',width='+pageWidth+',height='+pageHeight+', screenX=' + intLeft + ',screenY=' + intTop + ',left=' + intLeft + ',top= ' + intTop + ''); 
		openWin.focus();
	}
	
	// Filter numbers
	function numbersOnly(_object) 
	{
		var cleanup = _object.value.replace (/[~|%|^|*|(|)|{|}|:|;|\"|\'<|>|\.|,|\/|`|\||\\|]/gi,''); 
		cleanup = cleanup.replace (/[a-zA-Z]/gi,''); 
		_object.value = cleanup;
	}
	
	// Append
	function appendElement(_objectId)
	{
		// Create
		var newObject = document.createElement('div');	
		newObject.id = document.getElementById(_objectId).id;
		newObject.className = document.getElementById(_objectId).className;
		newObject.innerHTML = document.getElementById(_objectId).innerHTML;	
		
		// Set Id
		var oldObject = document.getElementById(_objectId);
		document.getElementById(_objectId).id = '';
		
		// Add
		var parentObject=oldObject.parentNode
		parentObject.insertBefore(newObject,oldObject.nextSibling);		
	}
	
	// CURRENCT FORMAT
	function formatCurrency(Num) 
	{
		Num = Num.toString().replace(/\$|\,/g,'');
		if(isNaN(Num))
		Num = "0";
		sign = (Num == (Num = Math.abs(Num)));
		Num = Math.floor(Num*100+0.50000000001);
		cents = Num%100;
		Num = Math.floor(Num/100).toString();
		if(cents<10)
		cents = "0" + cents;
		for (var i = 0; i < Math.floor((Num.length-(1+i))/3); i++)
		Num = Num.substring(0,Num.length-(4*i+3))+','+
		Num.substring(Num.length-(4*i+3));
		return (((sign)?'':'-') + '$' + Num + '.' + cents);
	}
	
	// Pad
	function pad(_number, _length) 
	{	   
		var str = ''+_number;
		while (str.length < _length)
			str = '0'+str;
		return str;	
	}
	
	// Limit lenght
	function LimitLength (_object,_limit) 
	{
		if(_object.value.length >= _limit) 
			_object.value = _object.value.substring(0, _limit);
	}	
	
	// Hidden forms
	function setHiddenForm(_inputId, _id, _mutiple)
	{
		if(_mutiple && document.getElementById(_inputId).value != '')
			document.getElementById(_inputId).value += ','+_id;
		else
			document.getElementById(_inputId).value = _id;
	}
