﻿(function($j){

	Code.registerNamespace('Website.Pages');

	Website.Pages.BookingForm = {
	
		content: null,
		US: 'USD',
		GB:	'GBP',
		USPrice:	'360',
		GBPrice:	'235',
		
		_changeBookingCurrency: function(){
			var self = this;
				self._checkCurrency('.payment-currency');
				
			$j('.payment-currency').change(function(){
				self._checkCurrency($j(this));
				return false;
			});
			
		},
		
		_checkPaymentAmount: function(){
			var self = this;
				
				var paymentType = $j('.payment-currency');
				var paymentTypeVal = paymentType.val();
				
				var currency = $j('.currency-change');
				
				
				//alert(paymentTypeVal + ' ' + self.US);
				
				currency.blur(function(){
					if(paymentTypeVal == self.US){
						var currentCurrency = currency.val();
						if(Math.round(currentCurrency) < Math.round(self.USPrice)){
							currency.addClass('validation-failed');
							//currency.val(self.USPrice + '.00');
							currency.focus();
						}else{
							currency.removeClass('validation-failed');
						}
					}else if(paymentTypeVal == self.GB){
						var currentCurrency = currency.val();
						if(Math.round(currentCurrency) < Math.round(self.GBPrice)){
							currency.addClass('validation-failed');
							//currency.val(self.UKPrice + '.00');
							currency.focus();
						}else{
							currency.removeClass('validation-failed');
						}
					}	
					
				});
			
		},
		
		_checkCurrency: function(obj){
			var self = this;
			if($j(obj).val() == self.GB){
				$j('.currency-change').val(self.GBPrice + '.00');
				$j('.deposit-change').text('£' + self.GBPrice);
				$j('.deposit-change-alt').text('$' + self.USPrice);
			}
			if($j(obj).val() == self.US){
				$j('.currency-change').val(self.USPrice + '.00');
				$j('.deposit-change').text('$' + self.USPrice);
				$j('.deposit-change-alt').text('£' + self.GBPrice);
			}
		},
		
		initValidation: function(){
			if($j('.booking-form').size()){
				var myRules = jQuery.validationAide.getDefaultValidationRules();
				myRules.add('select-validator-required', '', function(fieldValue, fieldObj) {
				if (fieldValue == "0")	return false;
				return true;
			});
				$j('#aspnetForm').validationAideEnable(myRules);
			}
		},
		
		initDatePicker: function(firstDay, dateFormat){
				
				
					
				if($j('.booking-form').size()){
				
					Date.firstDayOfWeek = firstDay;
					Date.format = dateFormat;
				
					$j('.date-pick').datePicker({clickInput:true})
					$j('#CheckIn').bind(
						'dpClosed',
						function(e, selectedDates)
						{
							var d = selectedDates[0];
							if (d) {
								d = new Date(d);
								$j('#CheckOut').dpSetStartDate(d.addDays(1).asString());
							}
						}
					);
					$j('#CheckOut').bind(
						'dpClosed',
						function(e, selectedDates)
						{
							var d = selectedDates[0];
							if (d) {
								d = new Date(d);
								$j('#CheckIn').dpSetEndDate(d.addDays(-1).asString());
							}
						}
					);
				};
				
				if($j('.testimonials-container').size()){
					$j('.tdate-pick').datePicker({clickInput:true, startDate:'01/01/1996'})
				}
			},
			
			activateCardDetails: function(){
			var self = this;
			if($j('.payment-type', '.booking-form').val() == '3'){
					$j('.card-details').show();
					
				}
			
			$j('.payment-type', '.booking-form').change(function(evt){
				//var myTarget = $j(evt.target);
				
				if($j(this).val() == '3'){
					$j('.card-details').slideDown();
					
				}else{
					$j('.card-details').slideUp();
				}
				
			});
			
		},
		
		_checkCountry: function(){
			var self = this;
			$j('.country-details select').blur(function(){
				if($j(this).val() == 'USA'){
					$j('.payment-currency').val(self.US);
					self._changeBookingCurrency();
				}else{
					$j('.payment-currency').val(self.GB);
					self._changeBookingCurrency();
				}
			});
		},
		
		onReady: function(){
			var self = this;
			self.initValidation();
			self.initDatePicker(0, 'dd/mmm/yyyy');
			self.activateCardDetails();
			self._changeBookingCurrency();
			self._checkPaymentAmount();
			self._checkCountry();
		}

	};

	$j().ready(function(){
		Website.Pages.BookingForm.onReady();
	});


})(jQuery);	


