$().ready(function(){
  if($("#start")[0]) {
	  $("#start").datepick({ 
		   minDate: new Date(),
		   dateFormat: "dd/mm/yy", 
		   showOn: "both", 
		   buttonImage: "/assets/calendar.gif", 
		   buttonImageOnly: true,
		   yearRange: '-0:+1'
		});
   }

   $("a.roll img").hover(function() {
		this.src = this.src.replace("-off","-on");
	},
	function()
	{
		this.src = this.src.replace("-on","-off");
	});

   $("a.BossImagePop").fancybox({
		'hideOnContentClick': true
   });

   $("a.BossImagePop").fancybox({
		'hideOnContentClick': true
   });
	
  $("dl.rates input.button").click(function () {
	   if ($("#start").val().length > 0 && $("#amount").val().length > 0) {
		   var start = $("#start").datepick("getDate"), amount = parseFloat($("#amount").val().replace("$", "")), end;

		   // Determine the year for the next month of June.
			end = (start.getMonth() >= 5) ?
			   new Date(start.getFullYear() + 1, 5, 30):
		      new Date(start.getFullYear(), 5, 30);
		    
			// Can't use this method - result changes depending on which day of week we start payments on (vs. 30th June day of week).
		   // weeks = ( 53 - $.datepicker.iso8601Week(end) ) + ( 53 - $.datepicker.iso8601Week(start) );

			// Instead, convert to ms, then to days. Derive no. of weeks and fortnights from this.
			start_ms = Date.parse(start);
			end_ms   = Date.parse(end);

			// 86400000ms in one day. Add 1 to include the week we're in.
			days = (( end - start ) / 86400000 );
			weeks = Math.floor(( days / 7 ) + 1 );
			fnights = Math.floor(( days / 14 ) + 1 );

			// If the start payment date is after the 30th, there's one less payment month available. No payment in last month (June).
			if (start.getDate() > 30)
				months = Math.floor( (11  - start.getMonth()) + (11 - end.getMonth()) );
			else
				months = Math.floor( (11  - start.getMonth()) + (11 - end.getMonth()) + 1 );

		   $("#weekly").val($.boss.formatCurrency( amount / weeks ));
		   $("#fortnightly").val($.boss.formatCurrency( amount / fnights ));
		   $("#monthly").val($.boss.formatCurrency( amount / months ));
		}
	});
	
	/* Form validation */
	if (typeof $.validator !== undefined) $.validator.messages.required = " *";
	
	// validate signup form on keyup and submit
	$(".ReturnForm").validate({
        
        //As soon as a key within a form field in “myform” is release then start 
		event: "keyup",
		
		//Here the rules for the individual inputs are defined.
		rules: {
			text: "required"
		}
	});

});
