$(document).ready(function(){
  
  //when any 'datetime' field gets clicked it will open the datatime dropdown
  $(".datetime").datepicker({
    duration: '',
    showTime: true,
    constrainInput: true
  });
  
  //when any 'date' field gets clicked it will open the date dropdown
  $(".date").datepicker({
    duration: '',
    showTime: false,
    constrainInput: true
  });
  
  //when any ajax form get submitted, handle the request
  $(".ajax").click(function(){
    var form = $(this).parents("form");
    var action = form.attr("action");
    //this will update CKEditor's content so the correct data is carried accross
    for(instance in CKEDITOR.instances){
      CKEDITOR.instances[instance].updateElement();
    }
    form_data = form.serialize();
    $.ajax({
      type: "post",
      url: action,
      data: form_data,
      success: function(returnText){
        if(returnText.indexOf("error") > 0){
          $.fn.colorbox({html:returnText});
        }
        else{
          document.location = returnText;
        }
      },
      error: function(XMLHttpRequest, textStatus, errorThrown){
        $.fn.colorbox({html:"There was an error while trying to submit your form, please try again. " + errorThrown + ' ' + textStatus + ' ' + XMLHttpRequest});
      }
    });
    return false;
  });
  
  //when any 'colorbox' link gets clicked open a colorbox modal window
  $(".colorbox").colorbox();
  
  //when the article category changes the sub category needs to change
  $("#article_category").change(function(){
    $.ajax({
      type: "POST",
      url: "/modules/article/default/ajax/change_category.php",
      data: "article_category=" + $(this).val(),
      success: function(returnText){
        $("#article_sub_category").html(returnText);
      },
      error: function(){
        $.fn.colorbox({html:"There was an error while trying to update the list of sub categories, please try again."});
      }
    });
  });
  
  $("#request_a_quote").click(function(){
    $.fn.colorbox({href:"/_extra/quote/step_1.php",iframe:true,width:"932px",height:"650px"});
  });
  
  $("#calculate_now").click(function(){
    $.fn.colorbox({href:"/_extra/premium_calculate_now/step_1.php",iframe:true,width:"934px",height:"725px"});
  });
  
  $("#pre_planning").click(function(){
    $.fn.colorbox({href:"/_extra/planning/step_1.php",iframe:true,width:"957px",height:"680px"});
  });
  
  $(".funeral_policy_premium_calculator").click(function(){
    $.fn.colorbox({href:"/_extra/premium_calculator/step_1.php",iframe:true,width:"957px",height:"680px"});
  });
  
  //any table that has the 'alternating' class must have alternating colors
  $(".alternating tr:even").addClass("even");
  $(".alternating tr:odd").addClass("odd");
});
