/* foxboats.js */
function boat_details(boat_id,avl_id,sof_id)
{
	document.booking_step2_form.boat_id.value = boat_id;
	document.booking_step2_form.avl_id.value = avl_id;
	document.booking_step2_form.TR_Special_Offer_Number.value = sof_id;
	document.booking_step2_form.action = "booking/sof_boat_details.php";
	document.booking_step2_form.submit();
}

function book_boat(boat_id,avl_id,price)
{
	document.booking_step2_form.boat_id.value = boat_id;
	document.booking_step2_form.avl_id.value = avl_id;
	document.booking_step2_form.sof_price.value = price;
	document.booking_step2_form.action = "booking/booking_sof_step1.php";
	document.booking_step2_form.submit();
}

function setYearsMonths(monthsFor){
	if(!document.step1Form){
		return;
	}
  var nextYearFrom = 8 // month (0-11) to start showing next year's dates; default : 8 (September)
    , bookingMonths = new Array(false,false,false,'April','May','June','July','August','September',true,true,true) // months 0-11
    , curdate = new Date()
    , month = curdate.getMonth()
    , year = curdate.getFullYear()
    , yearSelect = document.step1Form.year_selected
    , monthSelect = document.step1Form.month_selected
    , mCt = 0
    ;
  if(!monthsFor){
    monthsFor = bookingMonths[month] ? (bookingMonths[month] === true ? (year+1) : year) : year;
    var yrCt = month < nextYearFrom || monthsFor != year ? 1 : 2;
    for(var y = 0; y < yrCt; y++){
      yearSelect.options.length = y+1;
      yearSelect.options[y].value = yearSelect.options[y].text = monthsFor+y;
    }
  }
  for(var m = (monthsFor == year ? month : 0); m < bookingMonths.length; m++){
    if(bookingMonths[m] && bookingMonths[m] !== true){
      monthSelect.options.length = mCt+1;
      monthSelect.options[mCt].value = ('00'+(m+1)).slice(-2);
      monthSelect.options[mCt++].text = bookingMonths[m];
    }
  }
}
jQuery(function($){
	setYearsMonths();
	// DAYBOAT HIRE
	var dbc = $('#dayBoatCalendars')
		, dbm = dbc.prepend('<ul></ul>').find('ul')
		, now = new Date()
		, nowd1 = new Date( now.getFullYear() + '/' + ('00'+(now.getMonth()+1)).slice(-2) + '/01' )
		, mnth = []
		;
	$('table', dbc).filter(function(){
			var m = $.trim($(this).find('th').text()), d = new Date(Date.parse('1 ' + m)), r = false;
			if(d < nowd1){
				$(this).parent('div').remove();
			}else{
				mnth.push( {m:m, d1:d} );
				r = true;
			}
			return r;
		}).each(function(i){
		  var idx = i;
		  dbm.append('<li><a href="#">' + mnth[idx].m.replace(/^(\w{3}).*?(\d+)$/, '$1 <span>$2</span>') + '</a></li>')
					.find('a').click(function(){
							var d = dbc.children('div'), t = d.eq(dbm.find('a').index(this));
							if(!t.is(':visible')){
								d.filter(':visible').hide();
								t.show();
							}
							return false;
						});
			$(this).find('tbody td').filter(function(){ return !$(this).attr('bgcolor'); }).each(function(){
				  var c = $(this), dt = new Date(Date.parse(c.text()+' '+mnth[idx].m));
				  if(dt > now){
				  	var ymd = dt.getFullYear() + '/' + ('00'+(dt.getMonth()+1)).slice(-2) + '/' + ('00'+dt.getDate()).slice(-2);
			 			c.addClass('dbcSelectable')
								.prepend('<input type="radio" name="cust_dateYMD" value="'+ymd+'">')
								.hover(function(){ $(this).addClass('dbcSelectHover'); }, function(){ $(this).removeClass('dbcSelectHover'); });
				  }
				});
		}).click(function(ev){
				var t = $(ev.target).filter('.dbcSelectable').find(':radio');
				if(t.length && !t.is(':checked')){
					$('td.dbcSelected', dbc).removeClass('dbcSelected').find(':radio').each(function(){ this.checked = false; });
					t.each(function(){ this.checked = true; }).closest('td').addClass('dbcSelected');
					$('#dayBoatStartDate').val(new Date(t.val()).toLocaleString().replace(/\s+\d+:.*$/, ''));
				}
			});
	var i = mnth.length;
	while(i && mnth[--i].d1 > now){ ; }
	dbm.find('a').eq(i).trigger('click');
	if(dbc.length){
		$('#contactForm').ajaxForm(
		  { dataType:'json'
			, beforeSubmit:function(data, fm, opt){
		  	  $('#contactSuccess,#contactError').hide();
				  var adults = 1*$('#dbcNumAdults').val(), kids = 1*$('#dbcNumChildren').val(), msg = '';
				  fm.find('.reqd_field').each(function(){
							if(!$(this).val()){
								msg = 'Please complete all required fields';
								$(this).addClass('dbcMissingField');
							}else{
								$(this).removeClass('dbcMissingField');
							}
						});
					if(!msg){
					 	if($('#dbcIsDayHire:checked').length){
							if(adults > 6){ msg = 'The maximum number of Adults on a Day Hire boat is 6'; }
							else if(adults + kids > 8){ msg = 'The maximum number of people on a Day Hire boat is 8'; }
						}else{
							if(adults > 4){ msg = 'The maximum number of Adults on a Mini Break boat is 4'; }
							else if(adults + kids > 6){ msg = 'The maximum number of people on a Mini Break boat is 6'; }
						}
					}
					if(msg){
						window.alert(msg);
					}
					return !msg;
				}
			, success:function(resp){
				  if(resp.ok){
						$('#contactForm').resetForm();
						$('td.dbcSelected', dbc).removeClass('dbcSelected').find(':radio').each(function(){ this.checked = false; });
						$('#contactSuccess').show();
					}else{
						$('#contactError').html(resp.error||'Failed to send').show();
					}
				}
			});
	}
});

