// JavaScript Document
var formHasBeenSubmitted=false;
var cmMonthArray = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
var currentStep = 1;
var quoteURL = new Array();
quoteURL[0] = '';
quoteURL[1] = '/content/29/de/';
quoteURL[2] = '/content/30/de/';
quoteURL[3] = '/content/31/de/';
quoteURL[4] = '/content/32/de/';
quoteURL[5] = '/content/33/de/';

var expiresInDays = 1;

$(document).ready(function(){
        
  //Start JQuery Code
  if(document.getElementById('alert') && ! $.cookie('alertMessage')) {
   
  var content = $('#alertContainer').html();
  
  $('#alertContainer').remove();
  
  $.nyroModalManual({     
     type: 'form',
     modal: false,
     content: content,
     minWidth: 635, // Minimum width
     minHeight: 505,
     width: 635,
     height: 505,
     resizable: false, // Indicate if the content is resizable. Will be set to false for swf
     autoSizable: false,
     padding: 0,
     endShowContent: bindEvents
  });  
 }

  $(".logo").click(function () { 
      xt_med('C', '2', 'Mondial Logo', 'A');
  });
  
  $(".quoteProgressControl").click(function () {
   currentStep = $.trim($(this).attr('title'));
   updateQuote();   
   return false;
  }); 
                    
  $("#departing_date1").change(function(){ 
  if(isDate($("#departing_date1").val())) {  
   if($("#homeGroupID").val() == '12' || $("#homeGroupID").val() == '13' || $("#homeGroupID").val() == '24' || $("#homeGroupID").val() == '25') {
  
   var tmp = $("#departing_date1").val().split(".");
   var day = tmp[0];
   var month = tmp[1]-1;
   var year = parseInt(tmp[2]);   
  

   if(day < 10) {
    day = '0'+day; 
   }
   if(month < 10) {
    month = '0'+month; 
   }  
   var formattedReturnDate = day+'.'+month+'.'+year;  
   $("#returning_date1").val(getHPAnnualEndDate());   
  }
  }
  });
  
  function getHPAnnualEndDate() {
 if(isDate($("#departing_date1").val())) {   
   var returnDate = new Date();
   var tmp = $("#departing_date1").val().split(".");
   var day = tmp[0];
   var month = tmp[1]-1;
   var year = parseInt(tmp[2]);   
   returnDate.setMonth(month);
   returnDate.setFullYear(year);
     returnDate.setDate(day*1+365);   
   day = returnDate.getDate();
   month = returnDate.getMonth()*1+1;
   year = returnDate.getFullYear();   
   if(day < 10) {
    day = '0'+day; 
   }
   if(month < 10) {
    month = '0'+month; 
   }  
   var formattedReturnDate = day+'.'+month+'.'+year;  
   //$("#returnDate").val();
   //$("#returnDate").val(formattedReturnDate);      
  }      
  return formattedReturnDate;
}
  
  // handles home page form
 $("#homeGroupID").change(function(){
  setDateDisplay($(this).val());
   if(isNaN($("#homeGroupID").val())) {
   $('#hpDateFields').css('display','none');  
   }
   else {
     $('#hpDateFields').css('display','block'); 
   }

 }); 
 
 $("#leftNavigationQuoteForm").submit(function() {
                         
  if(isNaN($("#leftNavGroupID").val())) {                         
  xt_med('C', '2', 'Start Quote Left Navigation', 'S');    
   window.open($("#leftNavGroupID").val());
   return false;
  }
  else {
  xt_med('C', '2', 'Start Quote Left Navigation', 'A');    
   return true;     
  }

 });
 
 $("#homepageQuoteForm").submit(function() {
                     
                     
   if(isNaN($("#homeGroupID").val())) {
     xt_med('C', '2', 'Start Quote', 'S');
   window.open($("#homeGroupID").val());
     return false;
   }
   else {
    xt_med('C', '2', 'Start Quot Home Page', 'A');    
   return true;     
   }

 });
 
  //Email/Thickbox Loading
  if(!$.browser.opera){
    $(window).load(function(){
      $("#printemail a.thickbox").css("display","inline");
    });
  } else {
      $("#printemail a.thickbox").css("display","inline");    
  };
  
  //Submit Button Hide
  $(":submit").addClass("hide");  
  if ($.browser.mozilla && (parseFloat($.browser.version) < 1.9)) {
    $("span.submit").css("display","-moz-inline-box");  
  } else {
    $("span.submit").css("display","inline-block");      
  };

  //Outside Links
  $("span.submit:not(.externalQuote,.hpquote)").click(function(){                
    if(!formHasBeenSubmitted) {        
     $(this).parents("form").submit();
    }
  });  
 
  
  //Outside Links
  $("a.target").click(function(){
    window.open($(this).attr("href"));
    return false;
  });    
  
  //Coverage Options 
  $("ul.options li div").addClass("hide");

  $("ul.options li a").toggle(  
    function() {      
      $(this).parents("li").addClass("selected");
      $(this).parent().siblings("div").removeClass("hide");
      return false;      
    },    
    function() {    
      $(this).parents("li").removeClass("selected");
      $(this).parent().siblings("div").addClass("hide");
      return false;
    }
  );


  //Calendars
  
  //Initization
  
  /* German initialisation for the jQuery UI date picker plugin. */
  /* Written by Milian Wolff (mail@milianw.de). */
  jQuery(function($){
    $.datepicker.regional['de'] = {
      clearText: 'l&#246;schen', clearStatus: 'aktuelles Datum l&#246;schen',
      closeText: 'schlie&#223;en', closeStatus: 'ohne &#196;nderungen schlie&#223;en',
      prevText: '&#x3c;zur&#252;ck', prevStatus: 'letzten Monat zeigen',
      prevBigText: '&#x3c;&#x3c;', prevBigStatus: '',
      nextText: 'Vor&#x3e;', nextStatus: 'n&#228;chsten Monat zeigen',
      nextBigText: '&#x3e;&#x3e;', nextBigStatus: '',
      currentText: 'heute', currentStatus: '',
      monthNames: ['Januar','Februar','M&#228;rz','April','Mai','Juni',
      'Juli','August','September','Oktober','November','Dezember'],
      monthNamesShort: ['Jan','Feb','M&#228;r','Apr','Mai','Jun',
      'Jul','Aug','Sep','Okt','Nov','Dez'],
      monthStatus: 'anderen Monat anzeigen', yearStatus: 'anderes Jahr anzeigen',
      weekHeader: 'Wo', weekStatus: 'Woche des Monats',
      dayNames: ['Sonntag','Montag','Dienstag','Mittwoch','Donnerstag','Freitag','Samstag'],
      dayNamesShort: ['So','Mo','Di','Mi','Do','Fr','Sa'],
      dayNamesMin: ['So','Mo','Di','Mi','Do','Fr','Sa'],
      dayStatus: 'Setze DD als ersten Wochentag', dateStatus: 'W&#228;hle D, M d',
      dateFormat: 'dd.mm.yy', firstDay: 1,
      initStatus: 'W&#228;hle ein Datum', isRTL: false};
  });

  var lang = "de";
  if ($("html").attr("lang") != "de") lang = $("html").attr("lang");
  
  $.datepicker.setDefaults($.datepicker.regional[lang]); //Default is German
  $.datepicker.setDefaults({ 
    showOn: 'button', 
    buttonImage: 'files/images/icons/icon-calendar.gif', 
    buttonImageOnly: true,
    numberOfMonths: 2,
    showButtonPanel: true,
    firstDay: 1,
    constrainInput: false
  });
     
  //Calendar Calls
  if ($("body").attr("class") == "homepage") {
    $("#departing_date1").datepicker();
    $("#returning_date1").datepicker();  
  };
  
  if ($("body").attr("class") == "purchase") {
    $("#departing_date").datepicker();
    $("#returning_date").datepicker();
    $("#deposit_date").datepicker();    
  };    
  
  //Sidebar Benefits
  $(".policyselection .coveragedetails").addClass("hide");  
  
  $("#tripinsured .coveragedetails").removeClass("hide");  
  $("#tripinsured .coveragedetails").siblings("a.viewbenefits").addClass("openbenefits");    
  
  $(".policyselection a.viewbenefits").toggle(  
    function() {      
      $(this).addClass("openbenefits");
      $(".policyselection .coveragedetails").removeClass("hide");
      return false;      
    },    
    function() {    
      $(this).removeClass("openbenefits");
      $(".policyselection .coveragedetails").addClass("hide");
      return false;
    }
  );  


  //IE6 Hacks
  if ($.browser.msie && (parseInt($.browser.version) == 6)) {

    $("table.producttable tr:first td").css("padding-top", "0");
    $("table.producttable tr:first td").css("border-top", "none");
    
    //Buttons
    $("#featured span.startquote").hover(function() {      
        $(this).children("span").css("color","#fce703");  
        $(this).css("background-position","100% -30px");
      },function() {    
        $(this).children("span").css("color","#ffffff");  
        $(this).css("background-position","100% 0");        
    });    
    
    $("#email span.startquote").hover(function() {      
        $(this).children("span").css("color","#fce703");  
        $(this).css("background-position","100% -30px");
      },function() {    
        $(this).children("span").css("color","#ffffff");  
        $(this).css("background-position","100% 0");        
    });    
    
    //BgFix - IE6/z-index
  if ($("body").attr("class") == "purchase") {
    $(".helpwindow").bgiframe({ width: 404 });
    $(".benefits").bgiframe({ width: 522 });
  };

    //Product Table Hovers
    $("#article .coveragedetails tbody tr, #sidebar .coveragedetails tbody tr").not(".selection").hover(function() {      
        $(this).children("td").css("background","#eeeeee none");  
        $(this).children("td").css("border-left","1px solid #ffffff");
        $(this).children("th").css("background","#eeeeee none");     
      },function() {    
        $(this).children("td").css("background","transparent url(/files/images/backgrounds/bkg-tdcell.gif) repeat-x 0 100%");  
        $(this).children("td").css("border-left","1px solid #ffffff");  
        $(this).children("th").css("background","transparent url(/files/images/backgrounds/bkg-coverage_rowth.gif) repeat-x 0 100%");        
    });  

    $("#article .gruppenschutz tbody tr").hover(function() { 
        $(this).children("td").css("background","transparent none");  
        $(this).children("td").css("border-left","1px solid #eeeeee");
        $(this).children("th").css("background","transparent none");      
      },function() {    
        $(this).children("td").css("background","transparent none");  
        $(this).children("td").css("border-left","1px solid #eeeeee");
        $(this).children("th").css("background","transparent none");            
    });    
                            
  };  
  
  //Old Gecko/Firefox 2 hack
  if ($.browser.mozilla && (parseFloat($.browser.version) < 1.9)) {
    
    //Product Tables
    $(".coveragedetails table.triple td").css("width","20%")
  
    //Sidenav/German Names
    $("#sidenav dd a, .package, .coveragedetails tbody th, #sidebar .policyselection h3 span").each(function (i) {
      var link = $(this).html().replace(/-/g,"-<wbr>");
      $(this).html(link);
    });      
  };

  
  //Equal Height - Coverage Details Table Header
  var heights = new Array();  
  var heights2 = new Array();   
  var popular = false;
  
  //Top
  $(".coveragedetails thead p.package").each(function (i) {
    heights[i] = $(this).height();
  });  
    
  test = heights.sort(function(a,b){return b - a});
  $(".coveragedetails thead p.package").height(test[0]);
  
  //Bottom
  $(".coveragedetails tr.selection th span.package").each(function (i) {
    heights2[i] = $(this).height();
  });  
    
  test2 = heights2.sort(function(a,b){return b - a});
  $(".coveragedetails tr.selection th span.package").height(test2[0]);  
  
  //Equalize First Header Cell
  $(".coveragedetails thead th.first div").height($(".coveragedetails thead th:last div").not(".pricelist").height());

  //Purchase Process Breadcrumb - Due to lack of IE6 support
  $("body.purchase #article ul#breadcrumb li:first span").css("background","transparent url(files/images/lists/list-breadcrumb_standby_first.gif) no-repeat 0 0");  
  $("body.purchase #article ul#breadcrumb li:first.stepdone span").css("background","transparent url(files/images/lists/list-breadcrumb_stepdone_first.gif) no-repeat 0 0");
  $("body.purchase #article ul#breadcrumb li:last.standby").css("background","#eeb40b url(files/images/lists/list-breadcrumb_standby_last.gif) no-repeat 100% 0");

  
  //Benefits
  $(".coveragedetails tbody th a").hover(function() {      
      $(this).siblings(".benefits").css("left", "auto");
    },function() {    
      $(this).siblings(".benefits").css("left", "-9999px");      
  });  
  
  /* Help Windows */
  var t;
  
  $("label a.helptips").mouseover(function(e) {    
      x = e.pageX-201;    
      $(this).parent().siblings("div.helpwindow").css("left", x + "px");        
  });
  
  $("label a.helptips").mouseout(function(e) {    
      t = setTimeout("closeHelpWindow()", 300);        
  });  

  $("div.helpwindow").hover(function(e){    
      clearTimeout(t);
    },function() {    
      t = setTimeout("closeHelpWindow()", 300);      
  });  
  
  $("#selectlanguage a").click(function(){
    var contentID = getID($(this).attr('id'));
    var languageFull = $(this).attr('title');   
    var languageShort = getLanguageShort($(this).attr('id'));
    $.ajax({
        url: "/ajax/get/_getContentLink.cfm?contentID="+contentID+"&languageFull="+languageFull+"&languageShort="+languageShort,
        cache: false,
        async: false,
        success: function(html){
         var link = $.trim(html);
         window.location=link;   
        }
      });
      return false;
     }); 
  
  $(".sqcomparisongrid").click(function(){
  document.getElementById('prodoctComparisonForm').submit();
  return false;
});
  
  setDateDisplay($("#homeGroupID").val());  
  
//End JQuery Code
});

function getID(str) {
 var id = str.split("_");
 id=id[1];
 return id;
}  
   
function getLanguageShort(str) {
  var languageShort = str.split("_");
  languageShort=languageShort[2];
  return languageShort;
}

function closeHelpWindow() {
  $("div.helpwindow").css("left", "-9999px");      
}

function isDate(dateStr) {
 var tmp = dateStr.split(".");  
 if(tmp.length != 3) {
  return false; 
 }
 else {   
  var month = tmp[1]-1;  
  var day = tmp[0]; 
  var year = tmp[2];   
  if(isValidDate(month,day,year) != true) {  
    if(isValidDay(day, month)) {
   return true;  
  }
  else {
     return false;
  }     
  }
  else {
  return true;  
  }
 }   
}

function isValidDate(month,day,year) { 
 if(year < 1900 || year > 9999 || isNaN(year)) { 
  return 'year';
 }
 if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {  
  cmMonthArray[1] = "29";
 }  
 if(month < 1 || month > 12 || isNaN(month)) { 
  return 'month';
 } 
 if(day < 1 || day > cmMonthArray[(month-1)] || isNaN(day)) {  
  return 'day';
 }
 //alert(cmMonthArray[(month-1)]);
 return true;
}

function isValidDay(day, month) {  
 if(isNaN(day) || day < 1 || day > cmMonthArray[month]) {  
  return false;
 }
 return true;
}


function setDateDisplay(groupID) {  
  switch(parseInt(groupID)) {
   case 12:
   $('label[for=departing_date1]').html('Starting'); 
   $('label[for=returning_date1]').html('Ending');
   $('img.ui-datepicker-trigger').eq(1).css('display','none');
   $("#departing_date1").val('TT.MM.JJJJ');
   $("#returning_date1").val('TT.MM.JJJJ');
   break;
   case 13:
   $('label[for=departing_date1]').html('Starting');
   $('label[for=returning_date1]').html('Ending');
   $('img.ui-datepicker-trigger').eq(1).css('display','none');
   $("#departing_date1").val('TT.MM.JJJJ');
   $("#returning_date1").val('TT.MM.JJJJ');
   break;
   case 24:
   $('label[for=departing_date1]').html('Versicherungsbeginn');
   $('label[for=returning_date1]').html('Versicherungsende');
   $('#returning_date1').removeClass('hasDatepicker');
   $('img.ui-datepicker-trigger').eq(1).css('display','none');
   $("#departing_date1").val('TT.MM.JJJJ');
   $("#returning_date1").val('TT.MM.JJJJ');
   break;
   case 25:
   $('label[for=departing_date1]').html('Versicherungsbeginn');
   $('label[for=returning_date1]').html('Versicherungsende');   
   $('img.ui-datepicker-trigger').eq(1).css('display','none');
   $("#departing_date1").val('TT.MM.JJJJ');
   $("#returning_date1").val('TT.MM.JJJJ');   
   break;
   default:
   $('label[for=departing_date1]').html('Reisebeginn');
   $('label[for=returning_date1]').html('Reiseende');
   $('img.ui-datepicker-trigger').eq(1).css('display','inline');
   break;
   }  
}

function updateQuote() {  
 $('body').append('<form id="tmpUpdateForm" action="'+quoteURL[currentStep]+'" method="post"><input type="hidden" name="nToken" id="nToken" value="1" /><input type="hidden" name="qtoken" id="qtoken" value="update" /></form>');
 document.getElementById('tmpUpdateForm').submit();   
}

function previousStep() {
 window.location=$('#startQuoteURL').val();
}
                                    
function bindEvents() {   
  $.cookie('alertMessage', 1,{expires: expiresInDays, path: '/' });   
  $('#nyroModalWrapper').css('border','none');
  $('#nyroModalWrapper').css('background','transparent');
  $("#closewindow").click(function () {
    $.nyroModalRemove();
    return false;
  });
} 
