/*
* $Id: cs_config.js,v 1.38.2.1 2010/11/24 16:51:12 dyck Exp $ 
*/
$(document).ready(function(){ 

/* Prevent DW-header navigation from being locked to https */
  $('#Header a').each(function(){
    var url = $(this).get(0).href;
    $(this).get(0).href = url.replace(/https/, "http");
  });
  
if(document.getElementById('ibeRegion'))
       updateRegionList('ibeDestination', document.getElementById('ibeRegion'));              
		
		// SelectBoxen
		//$('.select').selectbox({debug: true});
		
		//initialize date pickers
		var standalone = false;
		standalone = $('#standalone').text();
		
Date.format = 'dd.mm.yyyy';	
$(function()
{
	
    if(standalone){
      $('#ibeStartDate').datePicker({createButton:false});
      $('#ibeEndDate').datePicker({createButton:false});
      $('#ibeStartDate').dpSetOffset(-20, -11);
      $('#ibeEndDate').dpSetOffset(-20, -11);
      $('#ibeStartDate').css('width','173px');
      $('#ibeEndDate').css('width','173px');
      $('.IBECarRental #ibeStartDate').css('width','143px');
      $('.IBECarRental #ibeEndDate').css('width','143px');
      
    }
    else{
      $('#ibeStartDate').datePicker();
  		$('#ibeEndDate').datePicker();
		}
		$('#dp-popup').bgiframe();
});		
		// set zebra-stripes for flight result tables
		$(".resultTable tr:nth-child(even)").not(".selected").addClass("alternate");
		// set initial highlighting for flight result tables
		$(".resultTable input:radio:checked").parents('tr').removeClass("alternate").addClass("selected");
		
		// set highlighting for flight result tables on click
    $(this).find(".resultTable input:radio").click(function() {
      $(".resultTable input:radio").parents('tr').removeClass("selected");
		  $(".resultTable tr:nth-child(even)").addClass("alternate");
		  $(".resultTable input:radio:checked").parents('tr').removeClass("alternate").addClass("selected");
    });
    // set zebra-stripes for datePrice tables
		$("#hotelResultsList tr:nth-child(even)").addClass("gray");
		$(".relevanceHeader").removeClass('gray');
		$(".relevanceHeader td").css('backgroundImage','none');
		$(".alternative").css('color','#6f6f6f');
	
    
	/* display wait animation on datePrice.jsp
	   setTimeOut() reloads the img so IE6 won't stop displaying the animation.
	*/
    function displayAni(thisHref){ 
      var checkButtonTd = '#buttonTd' + thisHref.id.substr(1);
      
      var aniImgTd = '#aniTd' + thisHref.id.substr(1);
      $(checkButtonTd).hide();
      $(aniImgTd).show();
    	var aniImg = 'ani' + thisHref.id.substr(1);
      
    		window.setTimeout(function(){changeSource(aniImg)},10);
    }
		 $("#hotelResultsList .check").click(function(e) {
      displayAni(this);
    });
    /*  helper function for displayAni() because setTimeout
        cannot deal with variables
    */
    function changeSource(aniImg){
      progressImg = document.getElementById(aniImg);
      progressImg.src = progressImg.src;
    }
    /*
    $("#hotelResultsList .refreshOfferView").click(function(e) {
      displayAni(this);
    });
    */
   
     $("#hotelResultsList .enterPersonalData").click(function(e) {
      displayAni(this);
      });
      
      $.fn.clearForm = function() {
        return this.each(function() {
          var type = this.type, tag = this.tagName.toLowerCase();
          if (tag == 'form')
            return $(':input',this).clearForm();
          if (type == 'text' || type == 'password' || tag == 'textarea')
            this.value = '';
          else if (type == 'checkbox' || type == 'radio')
            this.checked = false;
          else if (tag == 'select')
            this.selectedIndex = -1;
        });
      };
      
/* display number of hotel results on page */      
    function changeNumResults(){
		document.forms['paging'].submit();
		/* ajax
		maxHitsPerPage = document.getElementById("maxHitsPerPage");
		maxHitsPerPageSelected = maxHitsPerPage.options[maxHitsPerPage.selectedIndex].value;
		$("#hotelResults").html('').load("<gb:rewrite action="updateNumHotelResults.do" appended="\"+'maxHitsPerPage='+maxHitsPerPageSelected"/>);
		*/
	}	
  $('#maxHitsPerPage').change(function(e){
    changeNumResults();
  });  
// checks if rental car driver has the required age      
      function checkDriverAge(){
      if(document.getElementById('pickupDate')!= undefined && document.getElementById('BirthdayYear') != undefined && document.getElementById('minAge') != undefined ){
      
  			 var thePickupDate = document.getElementById('pickupDate').innerHTML;
  			 thePickupDateInput = thePickupDate.split('.');
  			 var thePickupDate = new Date(thePickupDateInput[2],thePickupDateInput[1]-1,thePickupDateInput[0]);
  			 var theBirthdate = new Date(document.getElementById('BirthdayYear').value,parseInt(document.getElementById('BirthdayMonth').value, 10) -1,document.getElementById('BirthdayDay').value);
  			 if(document.getElementById('minAge')!= undefined){
 				 var theMinAge = document.getElementById('minAge').innerHTML;
 			 }else{
 				var theMinAge = 25;
 			 }
  			 var minAgeBirthDate = new Date ((thePickupDateInput[2]-theMinAge),thePickupDateInput[1]-1,thePickupDateInput[0]);
  			 
  			 if(minAgeBirthDate < theBirthdate){
  			   document.getElementById('ageGuideline').style.color = "#990000";
  			   document.getElementById('ageGuideline').style.fontWeight = "bold";
  			   $('#driverBirthday').addClass('error');
  			   return false;
  			 }
  			 else{
          return true;
         }
      } else
        return true;
  			 
  	}
  	if(document.getElementById('ibePerson')){
      if(document.getElementById('ibePerson').selectedIndex == 3)
        $('#ibeThirdChild').hide();
    }
  	
    $('#ibePerson').change(function(){
      if(this.selectedIndex == 3){
        if($('#ibeThirdChild').get(0).selectedIndex != 0)
          $('#maxPersonHint').show();
        $('#ibeThirdChild').get(0).selectedIndex = 0;
        $('#ibeThirdChild').hide();
      }
      if(this.selectedIndex < 3){
      $('#ibeThirdChild').get(0).selectedIndex = 0;
      $('#ibeThirdChild').show();
      $('#maxPersonHint').hide();
      }
    });
    function openThisSSLInfo(){
      $("#InnerDimensions").html($('#ssl').html());
      openPopup();
    }
  
    $('#sslLink').click(function(){
    openThisSSLInfo();
    });
    $('#sslLogo').click(function(){
    openThisSSLInfo();
    });
    $('#sslHeadline').click(function(){
    openThisSSLInfo();
    });
    /* validate personalData and paymentData*/
    
		if($("#persData").get(0)){

//	removes formatting if driver age has been highlighted before on personalDataCarrental.jsp
      $('.birthday').click(function(){
        document.getElementById('ageGuideline').style.color = "#313131";
        document.getElementById('ageGuideline').style.fontWeight = "normal";
        $('#driverBirthday').removeClass('error');
      });
      $('.remove').change(function(){
        $(this).next().hide();
      });
     $("#submitButton").click(function() {
                  
        
        //$("#persData").submit();
         if($(this).parent().attr('id') == 'bindingBooking' && $('#CheckTerms').is(':checked')){
          //alert('Buchung');
            $("#persData").submit();
            $(this).remove();
            $('#busyButton').show();
            $('html').css('cursor','wait');
            
          } else
            $("#persData").submit();
      });  
		
    	/*$("#persData #Bill").click(function() {
    		    $('table.formDebit').find('input').clearForm();
            $('div.formCreditCard').find('input').clearForm();
            $('div.formCreditCard').find('select').clearForm();
        });
      $("#persData #CreditCard").click(function() {
        $('table.formDebit').find('input').clearForm();
    });
      $("#persData #Debit").click(function() {
        $('div.formCreditCard').find('input').clearForm();
        $('div.formCreditCard').find('select').clearForm();
    });
   */
/*    $("#persData #InsuranceCreditCard").click(function() {
        $('table.formDebit').find('input').clearForm();
    });
      $("#persData #InsuranceDebit").click(function() {
        $('div.formCreditCard').find('input').clearForm();
        $('div.formCreditCard').find('select').clearForm();
    });
*/  
  

    }
    $("#CardNumber").attr({ autocomplete:"off" });
    $("#Bank").attr({ autocomplete:"off" });
    $("#BankCode").attr({ autocomplete:"off" });
    $("#InsuranceAccountNumber").attr({ autocomplete:"off" });
    $("#InsuranceCardNumber").attr({ autocomplete:"off" });
    $("#InsuranceBank").attr({ autocomplete:"off" });
    $("#InsuranceBankCode").attr({ autocomplete:"off" });
    $("#InsuranceAccountNumber").attr({ autocomplete:"off" });
    $("#CVV").clearForm();
    // sliding effects for paymentData
    if($('#insurancePaymentHeadline').text().length > 0) {
    	var Bill = $("#Bill");
			var Debit = $("#InsuranceDebit");
			var CreditCard = $("#InsuranceCreditCard");
    }
      
    else {
    	var Bill = $("#Bill");
			var Debit = $("#Debit");
			var CreditCard = $("#CreditCard");
		}
			var iniBill = Bill.is(":checked");
			var iniDebit = Debit.is(":checked");
			var iniCreditCard = CreditCard.is(":checked");
			
			var showDebit = $(".formDebit");
			var showCreditCard = $(".formCreditCard");
		 
       jQuery('#insuranceTable input').click(function(){
          jQuery('.payment *').css('color','#313131');
          jQuery('#CVV').clearForm();
          jQuery('.payment input').each(function(){
            jQuery(this).attr('disabled','');
        //    jQuery(this).attr('checked','');
          });
        });
        function disableInsurancePayment(){
          $('.payment *').css('color','#b5b5b5');
          $('.payment input').each(function(){
            $(this).attr('disabled','disabled');
            $(this).attr('checked','');
          });
          	showCreditCard.slideUp("slow");
          	showDebit.slideUp("slow");
          	$('.insurance .payment').find('input').clearForm();
          	$('.insurance .payment').find('select').clearForm();
          	$('.insurance .payment *').removeClass('error');
          	$('#insuranceHeadline').removeClass('error');
          	$('#insuranceHeadline').text('Reiseversicherung:')
        }
        
        if($('#radioNoIns').is(':checked'))
          disableInsurancePayment();
          
       
        
      if(Debit.is(':checked'))
        showDebit.show();
        
      if(CreditCard.is(':checked'))
        showCreditCard.show();  
			
			Bill.click(function() {
				showDebit.slideUp("slow");
				showCreditCard.slideUp("slow"); 
			});
			
			$('#radioNoIns').click(function(){
          disableInsurancePayment();
      });
			Debit.click(function() {
				showDebit.slideDown("slow");
				showCreditCard.slideUp("slow");
				jQuery('#CVV').clearForm();
				
				//var topicInputs = topics.find("input")
				//topicInputs.attr("disabled", !this.checked);
			});
			
			
			CreditCard.click(function() {            
				showCreditCard.slideDown("slow");
				showDebit.slideUp("slow");
				
				//var topicInputs = topics.find("input");
				//topicInputs.attr("disabled", this.checked);
				//topicInputs.removeClass("error");
			});			
			 $('#printBookingConfirmation').click(function(){
			 	window.print();
			 });
		
		
		// Toggle Elements, e.g. flightResults
    $(".toggleContainer").find("a").click(function () {																	
      if ($(this).parent().parent().find(".toggleContent").is(":hidden")) {
        $(this).parent().parent().find(".toggleContent").slideDown("slow");
        $(this).parent().parent().addClass("active");
      } else {
        $(this).parent().parent().find(".toggleContent").slideUp("slow");
        $(this).parent().parent().removeClass("active");
      }
      return false;
    });  
}); 


