// JavaScript Document

  // Get Quote

function toggleSI() {
  $('#shippinginformation').toggle();
}

function changePrice(optionid, productid) {
  var priceid = '.price'+productid;
  var checkboxid = '#checkbox'+optionid+productid;
  checkbox = $(checkboxid);
  var newprice = checkbox.attr('value');
  checkbox.attr('value', $(priceid).html() );
  $(priceid).html(newprice);

  optionlabel = checkbox.parent().siblings('.title').find('span').text();
  title = $('#select'+productid+'0').siblings('h5');
  title2 = $('#select'+productid+'1').siblings('h5');
  if(title.find('#labeloption'+optionid).html()) {
    title.find('#labeloption'+optionid).remove(); title2.find('#label2option'+optionid).remove();
  } else {
    title.html(title.html()+'<small id="labeloption'+optionid+'"><br/>+ '+optionlabel+'</small>');
    title2.html(title2.html()+'<small id="label2option'+optionid+'"><br/>+ '+optionlabel+'</small>');
  }

}

function generateLink(productid, nboption, selectid, uid, famille) {
  var options = '';
  for (var i = 0; i <= nboption; i++) {
    var checkboxid = '#checkbox'+i+productid;
    var n = $(checkboxid+":checked").length;
    if ( n == 1 ) {
      options += $(checkboxid).name();
      options += "/";
    }
  }
  url = $(selectid).attr('href');
  $(selectid).attr('href', url+"/"+uid+"/"+options+""+famille);
}

function disallowDate(date) {

  // date is a JS Date object
  today = new Date();
  max = new Date(today.getFullYear(),(parseInt(today.getMonth())+1),today.getDate());
  if (date>max) {
    return true;
  }
  return false; // enable other dates
};

$(document).ready(function(){
  // Construction

  /* Annual Start Date calendar */
  $("input.startdatejscalendar").each(function (){
        var id = $(this).attr('id');
        var form = this.form;
        var div = document.createElement('div');
        $(div)
          .html(' ')
          .attr('id', id + '-button')
          .addClass('jscalendar-icon');
        $(this)
          .after(div)
          .parent().addClass('jscalendar');

        var settings = [];
        settings['ifFormat'] = $('#' + id + '-jscalendar-ifFormat', form).size() > 0 ? $('#' + this.id + '-jscalendar-ifFormat', form).val() : '%Y-%m-%d %H:%M:%S';
        // We use eval() because the result is a boolean while our input is a string.
        settings['showsTime'] = $('#' + id + '-jscalendar-showsTime', form).size() > 0 ? eval($('#' + this.id + '-jscalendar-showsTime', form).val()) : true;
        settings['timeFormat'] = $('#' + id + '-jscalendar-timeFormat', form).size() > 0 ? $('#' + this.id + '-jscalendar-timeFormat', form).val() : '12';

        //mycalendar.setDisabledHandler(disallowDate);
        Calendar.setup({
          inputField  : id,
          ifFormat    : settings['ifFormat'],
          button      : id + '-button',
          showsTime   : settings['showsTime'],
          timeFormat  : settings['timeFormat'],
          dateStatusFunc : disallowDate
        });
  });

  //alert($('#edit-type-insurance option').val());
  if($('#edit-type-insurance').val() == "ANNUAL") {
    $('#departreturn').hide();
    $('#startdate').show();
    $('#price_field').hide();
  } else {
    $('#departreturn').show();
    $('#startdate').hide();
    $('#price_field').show();
  }

  // If history return
  $('table.insurance-coverage input[type="checkbox"]:checked').each(function () {
    $(this).click().attr("checked", "checked");
  });

  //$(".jqtraveller").hide();
  $('.jqtraveller:lt('+$('#edit-addtraveler').val()+')').show();
  $('.jqtraveller:gt('+$('#edit-addtraveler').val()+')').hide();
  $('.jqtraveller:eq('+$('#edit-addtraveler').val()+')').hide();


  $('#edit-addtraveler').change(updateTravellers).keyup(updateTravellers);

  $('#edit-type-insurance').change(updateSelectBox).keyup(updateSelectBox);


  //YourPrice

  //alert($("#edit-si-sameasbilling:checked").length);
  //Traveler info
  if($("#edit-si-sameasbilling:checked").length == 1) {
    $('#shippinginformation').hide();
  }
  //$('#edit-si-sameasbilling').attr('onclick', "toggleView('#shippinginformation')");
  $('#edit-si-sameasbilling').click(toggleSI);

  // Trip insured email sender !
  $('#sendEmailRecap').click(function () {
    url = $(this).attr('href');
    $('#sendEmailRecapResponse').show().load(url);
    $('#sendEmailRecap').attr('href', '#').unbind('click');
    return false;
  });
});

function updateTravellers() {
    $('.jqtraveller:lt('+$(this).val()+')').show();
    $('.jqtraveller:gt('+$(this).val()+')').hide();
    $('.jqtraveller:eq('+$(this).val()+')').hide();
 }

function updateSelectBox(){
    if($(this).val() == "ANNUAL") {
      $('#departreturn').hide();
      $('#startdate').show();
      $('#price_field').hide();
    } else {
      $('#departreturn').show();
      $('#startdate').hide();
      $('#price_field').show();
    }
  }