//cookie code http://www.w3schools.com/JS/js_cookies.asp
function setCookie(c_name,value,expiredays, expireminutes)
{
  var exdate=new Date();    
  exdate.setDate(exdate.getDate()+expiredays);
  exdate.setMinutes(exdate.getMinutes()+expireminutes);
  
  document.cookie=c_name+ "=" +escape(value)+ ((expiredays==null) ? "" : ";expires="+exdate.toUTCString()+";path=/");
}

function getCookie(c_name)
{
  if (document.cookie.length>0)
    {
    c_start=document.cookie.indexOf(c_name + "=");
    if (c_start!=-1)
      {
      c_start=c_start + c_name.length+1;
      c_end=document.cookie.indexOf(";",c_start);
      if (c_end==-1) c_end=document.cookie.length;
      return unescape(document.cookie.substring(c_start,c_end));
      }
    }
  return "";
}


// Javascript File
// Creates the drop down list for the Search & Book widget based on current date
function departMonths(today){
    today.setDate(today.getDate() + 7); //add a week to default search
    departDays(today);
    
    var currentDate = new Date();
        currentDate.setDate(currentDate.getDate() + 7); //add a week to default search
        
    var currentYear = currentDate.getFullYear();
    var currentYearString = currentDate.getFullYear().toString();
    
    //if adding 7 days to currentDate pushes the month over ie 23rd Feb plus 7 days will bring it to 2nd of March, no longer be able to select Feb as a search option, do a check if adding 7 has pushed it over, if it has add the (original)month in first
    var testDate = new Date();
    var testYear = testDate.getFullYear();
    var testYearString = testDate.getFullYear().toString();    
    if (currentDate.getMonth() > testDate.getMonth())
    {
        i=testDate.getMonth()
        var monthFix = i + 1;
        if(monthFix.toString().length == 1){monthFix = "0" + monthFix};
        var optionValue = monthFix + testYearString.substring(2,4);
        var optionText = getMonthName(i) + " " + testYearString.substring(2,4);
        try {document.getElementById("depmonth").add(new Option(optionText,optionValue),null)}
        catch(e){document.getElementById("depmonth").add(new Option(optionText,optionValue),(testDate.getMonth() + testDate.getFullYear()))}
    }
    
    //finish off the current year
    for(i=currentDate.getMonth();i<=11;i++){
        var monthFix = i + 1;
        if(monthFix.toString().length == 1){monthFix = "0" + monthFix};
        var optionValue = monthFix + currentYearString.substring(2,4);
        var optionText = getMonthName(i) + " " + currentYearString.substring(2,4);
        try {
          document.getElementById("depmonth").add(new Option(optionText,optionValue),null);
          if (i == currentDate.getMonth())  //set the first option added by this for loop as the selected one (incase the testDate and currentDate months don't match, and the if statement adds a month in (we still want the default selected month to be the plus 7 days one)
          {
            document.getElementById("depmonth").options[document.getElementById("depmonth").options.length - 1].selected = true;  //set the last item added as the selected one (minus 1 from length, zero based indexing)
          }
        }
        catch(e){
          document.getElementById("depmonth").add(new Option(optionText,optionValue),(currentDate.getMonth() + currentDate.getFullYear()));
          if (i == currentDate.getMonth())  //set the first option added by this for loop as the selected one (incase the testDate and currentDate months don't match, and the if statement adds a month in (we still want the default selected month to be the plus 7 days one)
          {
            document.getElementById("depmonth").options[document.getElementById("depmonth").options.length - 1].selected = true;  //set the last item added as the selected one (minus 1 from length, zero based indexing)
          }
        }
    }
    

    //add few more months of the next year in
    for(i=0;i<=currentDate.getMonth()+5;i++){
    //FOR NOW HARD CODE A SEARCH LIMIT UP TO 30 April 2012
    //FOR NOW HARD CODE A SEARCH LIMIT UP TO 31 October 2012 - S12 now on sale
    //FOR NOW HARD CODE A SEARCH LIMIT UP TO 30 April 2013 - W13 now on sale
      if (i < 4)
      {
        
        var monthFix = i + 1;
        if (monthFix != 12) //for any month except december, work out its modulus, eg 2 is feb, so is 14
        {
          monthFix = monthFix%12;
        }
        
        if(monthFix.toString().length == 1){monthFix = "0" + monthFix};
        var optionValue = monthFix +""+(parseFloat(currentYearString.substring(2,4)) + 1);
        var optionText = getMonthName(i) + " " + (parseFloat(currentYearString.substring(2,4)) + 1);
        try{document.getElementById("depmonth").add(new Option(optionText,optionValue),null)}
        catch(e){document.getElementById("depmonth").add(new Option(optionText,optionValue),(currentDate.getMonth() + currentDate.getFullYear()))}
        
      }
    }
}

function departDays(today){
    var daySelect = document.getElementById("depday");
    for(i=1;i<=31;i++){
        var currentDay;
        if(i.toString().length == 1){currentDay = "0" + i.toString()}
        else{currentDay = i.toString()}
        try{daySelect.add(new Option(currentDay,currentDay),null)}
        catch(e){daySelect.add(new Option(currentDay,currentDay),i)}
    }

    var currentDay = today.getDate();
    //if(currentDay.toString().length == 1){currentDay = "0" + currentDay.toString()}
    //daySelect.options[parseInt(currentDay)].selected = true;
    //need to use Zero indexing
    daySelect.options[parseInt(currentDay)-1].selected = true;
    
}

function getMonthName(month){
    month = month%12;  //incase +5 in second loop goes over 12, then loop all the way around
    var monthName = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");                                
    return monthName[month];
}
