$(function()
{
  $("#inlineDate").datepicker({
      onSelect: function() { validateStartDate(); },
      highlightWeek: true,
      altField: "#startDate",
      altFormat: 'dd MM yy',
	  minDate: new Date(2011, 12 - 1, 20),
	  maxDate: new Date(2012, 4 - 1, 30)   
  }).attr("readonly", "readonly");
  
  $("#startDate").show().keyup(setInlineDate);
  
  $(".basic_button").each(function()
  {
    buttonHover($(this));
  });
  
  $("#begin_order").click(function()
  {
    beginOrder();
  });

});


function validDate(strValue)
{ 
  var objRegExp = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/
 
  //check to see if in correct format
  if(!objRegExp.test(strValue))
    return false; //doesn't match pattern, bad date
  else{
    var strSeparator = strValue.substring(2,3) 
    var arrayDate = strValue.split(strSeparator); 
    //create a lookup for months not equal to Feb.
    var arrayLookup = { '01' : 31,'03' : 31, 
                        '04' : 30,'05' : 31,
                        '06' : 30,'07' : 31,
                        '08' : 31,'09' : 30,
                        '10' : 31,'11' : 30,'12' : 31}
    var intDay = parseInt(arrayDate[0],10); 

    //check if month value and day value agree
    if(arrayLookup[arrayDate[1]] != null) {
      if(intDay <= arrayLookup[arrayDate[1]] && intDay != 0)
        return true; //found in lookup table, good date
    }
    
    // Deal with February
    var intMonth = parseInt(arrayDate[1],10);
    if (intMonth == 2) { 
       var intYear = parseInt(arrayDate[2]);
       if (intDay > 0 && intDay < 29) {
           return true;
       }
       else if (intDay == 29) {
         if ((intYear % 4 == 0) && (intYear % 100 != 0) || 
             (intYear % 400 == 0)) {
              // year div by 4 and ((not div by 100) or div by 400) ->ok
             return true;
         }   
       }
    }
  }  
  return false; //any other values, bad date
}



function beginOrder()
{
  // Check a valid date has been entered
  var selectedDate = new Date($("#startDate").val());
  
  if (!validDate(selectedDate.asString()))   
  {
    alert("Please select a valid start date for your\nski hire using the calendar above.");
    return false;
  }
  
  else
    location.href = "bansko-ski-quote.php";
}


function buttonHover( obj )
{
  $(obj).hover(function()
  {
    $(obj).css({ cursor:"pointer" });
  }, function()
  {
    $(obj).css({ cursor:"auto" });
  });
}

function setInlineDate()
{
  try 
  { 
    var date = $.datepicker.parseDate("dd MM yy", $("#startDate").val()); 
    $("#inlineDate").datepicker("setDate", date); 
    } 
    catch (e) 
	{ 
      // Ignore 
    } 
}


function daysFromToday( futureDate )
{
  Date.format = 'dd/mm/yyyy';
  var futureDateStr = futureDate.asString();
  var today = new Date(); 
  var todayStr = new String( today.asString() );
  var one_day = 1000*60*60*24;
  
  var x = futureDateStr.split("/");
  var y = todayStr.split("/");
  
  var date1 = new Date( x[2], (x[1]-1), x[0] );
  var date2 = new Date( y[2], (y[1]-1), y[0] );
  var month1 = x[1]-1;
  var month2 = y[1]-1;
  
  daysDiff = Math.ceil( (date1.getTime() - date2.getTime()) / (one_day) );
  return daysDiff;
}


function validateStartDate()
{
  var selectedDate = new Date($("#inlineDate").datepicker("getDate"));   
  var days = daysFromToday(selectedDate);

  if (days < 21)
  {
    $("#startDate").val("");
    alert("We're sorry, but to pre-book any of our services,\n" +
          "your start date must be at least 21 days from today.");
  }
  else
  {
    // Commit valid date to server	
	Date.format = 'dd/mm/yyyy';	
	var startDateStr = selectedDate.asString();
		      
    $.post("../php/set_start_date.php",
        { StartDate: startDateStr }
    );     
  }
}



