var CFT = function()
{

    // Private variables

    // Private objects

    // Private methods

    return {

        // Public properties

        // Public methods
		
		Form: {
			init: function () {
				$('#contactform input, #contactform textarea').focus(function () {
					$(this).parent().addClass('focused');
				}).blur(function () {
					$(this).parent().removeClass('focused');
				});
				$('#contactform').submit(function (e) {
					if ($('#name').val() == '' || $('#email').val() == '') {
						alert("You must enter your name and email address");
						e.preventDefault();
						return false;
					} else {
						return;
					}
				});
				$('#bookingform').submit(function (e) {
					var blank_fields = false;
					$('#attendees input').each(function () {
						if ($(this).attr('name') == 'attendee-name[]' && ($(this).val() == '' || $(this).val() == 'name')) {
							blank_fields = true;
						}
					});
					if (blank_fields) {
						alert("Please complete the names/positions of all course attendees");
						e.preventDefault();
						return false;
					} else {
						var payment_method_checked = false;
						$('input[type=radio]').each(function () {
							if ($(this)[0].checked) {
								payment_method_checked = true;
							}
						});
						if (payment_method_checked) {
							var message = 'You are about to book ' + $('#attendees input:odd').length + ' delegate';
							if ($('#attendees input:odd').length > 1) {
								message += 's';
							}
							message += ' on "' + $('#course option').eq($('#course')[0].selectedIndex).text() + '" at ' + $('#course option').eq($('#course')[0].selectedIndex).val().replace(/,/, ' on') + '.\n\nClick OK to proceed.';
							if (confirm(message)) {
								return;
							} else {
								e.preventDefault();
								return false;
							}
						} else {
							alert('Please select a payment method before proceeding');
							e.preventDefault();
							return false;
						}
					}
				});
			},
			workshop: function () {
				$('#attendees h2').after('<a href="#" id="add-attendee">Additional attendee</a>');
				$('#add-attendee').click(function () {
					$('#attendees').append('<label><input type="text" value="name" onkeyup="CFT.Form.update_workshop_form();" name="attendee-name[]" maxlength="250" size="20" /></label><label><input type="text" value="position" name="attendee-position[]" maxlength="250" size="20" /></label>');
					return false;
				});
				$('#course').change(function () {
//					if ($(this).val() == 'Glasgow, 17 March') {
//						// $('#per-person').text('110');
//						$('#company').parent().html('Company:<span>Your company/organisation\'s name</span><input type="text" name="company" id="company" maxlength="250" size="20" />');
//					} else {
//						// $('#per-person').text('199');
//						$('#company').parent().html('Council:<span>Your council name</span><input type="text" name="company" id="company" maxlength="250" size="20" />');
//					}
					document.getElementById('course_name').selectedIndex = document.getElementById('course_price').selectedIndex = document.getElementById('course').selectedIndex;
					// Update the form in case the course price has changed
					CFT.Form.update_workshop_form();
				})
			},
			update_workshop_form: function () {
				var number_of_attendees = 0;
				$('#attendees input').each(function () {
					if ($(this).attr('name') == 'attendee-name[]' && $(this).val() != '' && $(this).val() != 'name') {
						number_of_attendees++;
					}
				});
				var net_cost = (number_of_attendees * $('#course_price').val());
				
				// Apply 2-for-1 discount
				var free_seats = Math.floor(number_of_attendees / 2);
				net_cost -= (free_seats * $('#course_price').val());

				var discounted = false;

				/* Apply bulk booking discounts
				if (number_of_attendees >= 7) {
					net_cost = (net_cost * 0.8);
					discounted = 20;
				} else if (number_of_attendees >= 5) {
					net_cost = (net_cost * 0.85);
					discounted = 15;
				} else if (number_of_attendees >= 3) {
					net_cost = (net_cost * 0.9);
					discounted = 10;
				}
				//*/
					
				var vat = (net_cost * 0.175);
				// Check if rounding up
				if (vat.toFixed(2) != (vat + 0.005).toFixed(2)) {
					vat += 0.01;
				}
				vat = vat.toFixed(2);
				var total_payable = (parseFloat(net_cost) + parseFloat(vat)).toFixed(2);
				$('#net-cost').text(net_cost);
				$('#vat').text(vat);
				$('#total').text(total_payable);
				if (discounted) {
					$('#total').addClass('discounted');
					$('#discounted').html('<span class="discount">' + discounted + '% discount<\/span>');
				} else {
					$('#total').removeClass('discounted');
					$('#discounted').html('');
				}
				$('#amount').val(total_payable);
			}
		},

		BoxHeights: {
			maxh: 0,
			boxes: Array(),
			num: 0,
			equalise: function() {
				this.num = arguments.length;
				for (var i=0;i<this.num;i++) if (!document.getElementById(arguments[i])) return;
				this.boxes = arguments;
				this.maxheight();
				for (var i=0;i<this.num;i++) $('#'+arguments[i]).css({ height: this.maxh+"px" });
			},
			maxheight: function() {
				var heights = new Array();
				for (var i=0;i<this.num;i++) {
					if (navigator.userAgent.toLowerCase().indexOf('opera') == -1) {
						$('#'+this.boxes[i]).css({ height: 'auto' });
						heights.push(document.getElementById(this.boxes[i]).scrollHeight);
					} else {
						heights.push(document.getElementById(this.boxes[i]).offsetHeight);
					}
				}
				heights.sort(this.sortNumeric);
				this.maxh = heights[this.num-1];
			},
			sortNumeric: function(f,s) {
				return f-s;
			}
		}

    };

}();

$(document).ready(function () {
	CFT.BoxHeights.equalise('previous','services','contact');
	CFT.Form.init();
	$('#courses li').eq(0).addClass('first-child');
	$('#courses h3 + p').addClass('first');
});

$(window).resize(function () {
	CFT.BoxHeights.equalise('previous','services','contact');
});

function checkPaid() {
	alert('Now make your credit card payment online using PayPal - click the "Pay Online" button to proceed');
}
