SignUp = {
		initialize: function(){
			$('.date-pick').datePicker({clickInput: false,startDate: '01/01/1900'});
			$('input.numeric').numeric();
			$('#same_as_service').bind('change', function() {
				if ($('#same_as_service').attr('checked'))
				{
					$("input[name*='bill_addr']").each(function(i){
						$(this).attr('value', $('#serv_addr'+(i+1)).val()).attr('readonly', 'readonly').css({'background-color' : '#ddd'});
					});
				}
				else
				{
					$("input[name*='bill_addr']").each(function(){
						$(this).attr('value', '').removeAttr('readonly').css({'background-color' : '#fff'});
					});
				}
				return false;
			});

			$('#step01').bind('submit', function() {
				SignUp.check_valid(); return false;
			});

			$('.tooltipable').tooltip({
				position: 'center right',
				offset: [10,10],
				effect: 'slide',
				tip: '.tooltip'
			});

			$('#region_code').bind('focus', function() {
				$.fn.colorbox({
					onLoad: function() {
						$('#map_container').css({'display':'block'});
					},
					onCleanup: function() {
						$('#map_container').css({'display':'none'});
					},
					innerWidth:"851px",
					innerHeight:"759px",
					inline:true,
					href:"#map_container"
				});
			});

			$('#region_code_img').maphilight();

			$('#add-features').bind('click', function(){
				var ajax_url = base_url + 'ajax/get_features_menu/' + $("input[@name='customer_type']:checked").val() + '/' + $("input[name='features_count']").val();
				$.getJSON(ajax_url,
					function(data){
						var count = eval($("input[name='features_count']").val()) + 1;
						$('#additional-features-box').append(data.html);
						$("input[name='features_count']").attr('value', count);

						// bind the dropmenu for change
						$('#feature'+count).bind('change', function(){
							var ajax_url = base_url + 'ajax/get_product_details/' + $.trim($(this).val()) + '/' + count;
							$.getJSON(ajax_url,
								function(data){
									$("input[name='feature"+count+"_service_charge']").attr('value', data.service_charge);
									$("input[name='feature"+count+"_monthly_rental']").attr('value', data.monthly_rental);
									$('#feature'+count+'_equipment').text(data.equipment);
									$('#feature'+count+'_monthly_rental_display').text(data.monthly_rental).formatCurrency();
									SignUp.check_monthly_rental();
								});
						});

						// bind the quantity box
						$('#feature'+count+'_quantity').bind('blur', function(){
							var monthly_rental = eval($(this).val()) * eval($("input[name='feature"+count+"_monthly_rental_unit']").val());
							$("input[name='feature"+count+"_monthly_rental']").attr('value', monthly_rental);
							$('#feature'+count+'_monthly_rental_display').text(monthly_rental).formatCurrency();
							SignUp.check_monthly_rental();
						});

						// bind the delete button
						$('#feature'+count+'_delete').bind('click', function(){
							$('#feature'+count+'_row').slideUp('slow');
							setTimeout("$('#feature"+count+"_row').remove()", 2000);
							setTimeout("SignUp.check_monthly_rental()", 2000);
							return false;
						});

						SignUp.check_monthly_rental();
					});
				return false;
			});

			$('#package').bind('change', function(){
				var ajax_url = base_url + 'ajax/get_product_details/' + $.trim($('#package').val());
				$.getJSON(ajax_url,
					function(data){
						$('#equipment').html(data.equipment);
						$("input[name='package_service_charge']").attr('value', data.service_charge);
						$("input[name='package_monthly_rental']").attr('value', data.monthly_rental);
						SignUp.check_monthly_rental();
					});
				return false;
			});

			$('#coupon_code').bind('blur', function(){
				if ($.trim($('#coupon_code').val()) != '')
					SignUp.check_coupon(); return false;
			});

			this.check_monthly_rental();
		},

		check_valid: function() {
			if ($("input[@name='existing']:checked").val() == '1')
			{
				if ($.trim($('#accountno').val()) != '')
				{
					var accountno = $.trim($('#accountno').val());
					if (accountno.length == 12)
						this.check_account();
					else
						this.check_service();
				}
				else
				{
					$('#error_message').show()
						.css({'color' : 'red', 'font-weight' : 'bolder'})
						.text('The Account Number Field Cannot Be Blank.').fadeOut(5000);
				}
			}
			else
			{
				// will unbind the the submit function the display the rest of the form
				$('#step01').unbind('submit');
				// bind a new function to enable the radio button on leaving the page
				$('#step01').bind('submit', function(){
					$("input[@name='existing']:radio").removeAttr('disabled');
					return true;
				});
				$('#accountno').attr('readonly', 'readonly').attr('value', '').css({'background-color' : '#ddd'});
				$("input[@name='existing']:radio").attr('disabled', 'disabled');
				$('.hide').fadeIn("slow");
			}
		},

		check_account: function() {
			$('#error_message').show()
				.css({'color' : '#000', 'font-weight' : 'bolder'})
				.text('One moment while we validate your account number.').fadeOut(5000);
			var ajax_url = base_url + 'ajax/is_account/' + $.trim($('#accountno').val());
			$.getJSON(ajax_url,
					function(data) {
						if (data.status == 'valid') {
							if (!data.existing) {
								// will unbind the the submit function the display the rest of the form
								$('#step01').unbind('submit');
								// bind a new function to enable the radio button on leaving the page
								$('#step01').bind('submit', function(){
									$("input[@name='existing']:radio").removeAttr('disabled');
									return true;
								});
								$('#accountno').attr('readonly', 'readonly').css({'background-color' : '#ddd'});
								$("input[@name='existing']:radio").attr('disabled', 'disabled');
								$('.hide').fadeIn("slow");
							}	else {
									$('body').append('<p id="dialog" style="color:#222;display:none">There is already an application being processed for you.</p>');
									$('#dialog').dialog({
										modal: true,
										buttons: {
												Ok: function() {
													$(this).dialog('close');
													$('#dialog').remove();
												}
										},
										title: 'Existing Application'
									});
							}
						} else {
							$('#error_message').show()
								.css({'color' : 'red', 'font-weight' : 'bolder'})
								.text('The Account Number Entered Is Invalid.').fadeOut(5000);
						}
					});
		},

		check_service: function() {
			$('#error_message').show()
				.css({'color' : '#000', 'font-weight' : 'bolder'})
				.text('One moment while we validate your service number.').fadeOut(5000);
			var ajax_url = base_url + 'ajax/get_account/' + $.trim($('#accountno').val());
			$.getJSON(ajax_url,
					function(data) {
						if (data.status == 'valid') {
							if (!data.existing) {
								// will unbind the the submit function the display the rest of the form
								$('#step01').unbind('submit');
								// bind a new function to enable the radio button on leaving the page
								$('#step01').bind('submit', function(){
									$("input[@name='existing']:radio").removeAttr('disabled');
									return true;
								});
								$('#accountno').attr('value', data.account_no).attr('readonly', 'readonly').css({'background-color' : '#ddd'});
								$('#serviceno').attr('value', data.service_no);
								$("input[@name='existing']:radio").attr('disabled', 'disabled');
								$('.hide').fadeIn("slow");
							}	else {
									$('body').append('<p id="dialog" style="color:#222;display:none">There is already an application being processed for you.</p>');
									$('#dialog').dialog({
										modal: true,
										buttons: {
												Ok: function() {
													$(this).dialog('close');
													$('#dialog').remove();
												}
										},
										title: 'Existing Application'
									});
							}
						} else {
							$('#error_message').show()
								.css({'color' : 'red', 'font-weight' : 'bolder'})
								.text('The Service Number Entered Is Invalid.').fadeOut(5000);
						}
					});
		},

		check_monthly_rental: function() {
			var total_monthly_rental = 0;
			var vat = 1.15;
			$("input[name$='_monthly_rental']:hidden").each(function(){
				total_monthly_rental += eval($(this).val());
			});
			$("input[name='customer_value']").attr('value', total_monthly_rental);
			$('#price-total').text( Math.round(total_monthly_rental * vat * 100) / 100 ).formatCurrency();
		},

		check_coupon: function() {
			var ajax_url = base_url + 'ajax/validate_coupon/' + $.trim($('#coupon_code').val());
			$.getJSON(ajax_url,
					function(data) {
						if (data.status == 'invalid') {
							$('#coupon_code').attr('value', '');
							$('#error_message').show()
								.css({'color' : 'red', 'font-weight' : 'bolder'})
								.text('The Coupon Code Entered Is Invalid.').fadeOut(5000);
						} else {
							$('input#coupon_code').unbind('blur');
							$('input#coupon_code')
								.attr('value', data.items[0].coupon_code)
								.attr('readonly', 'readonly')
								.css({'background-color' : '#ddd'});
							$('#error_message').show()
									.css({'color' : 'green', 'font-weight' : 'bolder'})
									.text(data.items[0].description);
						}
					});
		},

		get_region: function(data) {
			$('#region_code').attr('value', data);
			$.fn.colorbox.close();
			return false;
		}
};

function getTop() {
	// get the top of the content
	var top = $('#scroll-content').css('top');
	return trimPx(top);
}

function getHeight(id) {
	// get the height, including padding
	var height = $(id).height();
	var paddingTop = trimPx($(id).css("padding-top"));
	var paddingBottom = trimPx($(id).css("padding-bottom"));

	return height + paddingTop + paddingBottom;
}

function trimPx(value) {
	// remove "px" from values
	var pos = value.indexOf("px");
	if (pos != 0)
		return parseInt(value.substring(0, pos));
	else
		return 0;
}

var container;
var content;
var hidden;	// # of pixels hidden by the container

function setScrollerDimensions() {
	container = getHeight("#scroll-container");
	content = getHeight("#scroll-content");
	hidden = content - container;
}

function resetScroller() {
	setScrollerDimensions();
	$('#scroll-content').css('top', 0);
}

ScrollArea = {
	initialize: function() {
		setScrollerDimensions();

		$('#scroll-controls a.up-arrow').click(function() {
			return false;
		});

		$('#scroll-controls a.down-arrow').click(function() {
			return false;
		});

		$('#scroll-controls a.down-arrow').hover(
			function() {
				if (hidden > 0) {
					var current = getTop();
					$('#scroll-content').animate({ top: -hidden }, Math.abs(current - hidden) * 5);
				}
			},
			function() {
				$('#scroll-content').stop();
			}
		);

		$('#scroll-controls a.up-arrow').hover(
			function() {
				if (hidden > 0) {
					var current = getTop();
					$('#scroll-content').animate({ top: "0" }, Math.abs(current) * 5);
				}
			},
			function() {
				$('#scroll-content').stop();
			}
		);
	}
};

$(function() {
	$('a.lightbox').colorbox();

	$(".btn-slide").click(function(){
		$("#panel").slideToggle("slow");
		$(this).toggleClass("active"); return false;
	});

	if (!$('#step03').length) {
		$('form').focus_first();
	}

	Cufon.replace('h1');
	Cufon.replace('.cufon-text');

	if ($('#scroll-container').length) {
		ScrollArea.initialize();
	}

	SignUp.initialize();
});