 var proxy = {
                 path: '/ajax.php',
                 host: 'xml.m-broker.de',
		 uri:  '/wrapper/index_cdata.jsp',
		 proto: 'ssl',
		 port:  '443'
             }


 function initPage()
 {     
     ////  Handhe the empty result event
     //
     if (typeof(flag_empty_result) != 'undefined')
     {
         handle_empty_result();
     }
     
     
     ////  Bind the events 
     //
     
     //  For the country select
     Event.observe(id_carCountry, 'change', initCitySelect);
     //  For the submit button
     Event.observe(id_submit, 'click', proceed);
     //  For the fromDate field
     Event.observe(id_carFromDate, 'change', fromdate_onchange);
     //  For the toDate field
     Event.observe(id_carToDate, 'change', todate_onchange);
     
     //Event.observe('logo-tuv'     , 'click', new Function('openLayer("Sicherheit", "...", "/layer-content-tuv/"     , "", "418", "253");'));
//     Event.observe('logo-tshops'  , 'click', new Function('openLayer("Sicherheit", "...", "/layer-content-tshops/"  , "", "418", "253");'));
     Event.observe('logo-geotrust', 'click', new Function('openLayer("Sicherheit", "...", "https://www.ferienauto.de/proxy.jsp?uri=/layer-content-geotrust/", "", "418", "253");'));
     
     
     
     
     ////  Initialize the country lists
     //
     initCountrySelect();
     
     
     if (!(use_country == false || use_country == null || use_country == -1))
     {
         initCitySelect(use_country);
     }

     if (use_fromdate == null || use_fromdate == false || use_fromdate == '')
     {
         var tmpDate = new Date();

         tmpDate.setTime(tmpDate.getTime() + fromdate_offset);
         use_fromdate = tmpDate.print(date_format);
     }

     $(id_carFromDate).value = use_fromdate;
     select_choose(id_carFromHour  , use_fromhour);
     select_choose(id_carFromMinute, use_fromminute);



     if (use_todate == null || use_todate == false || use_todate == '')
     {
         var tmpDate = new Date();

         tmpDate.setTime(tmpDate.getTime() + todate_offset);
         use_todate = tmpDate.print(date_format);
     }

     $(id_carToDate).value = use_todate;
     select_choose(id_carToHour  , use_tohour);
     select_choose(id_carToMinute, use_tominute);
     
     $(id_PromoCode).value = use_promo_code;
     $(id_partnerNo).value = use_partner_no;
     $(id_opId).value      = use_op_id;
 }
 
 
 ////  Handle the empty result event. Do stuff when it happens
 //
 function handle_empty_result()
 {
     openLayer('Ihre Mietwagen-Anfrage', '...', '/no-offers', '', '420', '255');
//     openLayer('Angebot anfordern', '...', 'https://www.ferienauto.de/buchungsanfrage.jsp', '', '500', '540');
 }
 
 
 
 ////  Init the country select list
 //
 var _first_run_country = false;
 function initCountrySelect(ajax_responce)
 {     
     var countrySelect = $(id_carCountry);

     ////  Disable the selece so the user's can't do anything while AJAX runs
     //
     countrySelect.disable();

     ////  Show the waiting image, if it exists
     // 
     if ((tmp=$(id_carCountry+'_loading')))   tmp.removeClassName('invisible');
     

     ////  First phase, request country list XML from the server
     //
     if (ajax_responce == null)
     {         
         var data = {
                        lang: 'de',
                        opId: use_op_id
                    };
         ajax_fetch_country_xml(initCountrySelect, proxy, data);
     }

     ////  Second phase, parse the received XML data
     //
     else
     {   
         ////  Parse the result XML into key => value pairs
	 //
         var arr = xml_parse_namevalue_list(ajax_responce.responseText, fix_umlauts);
	 
	 if (typeof(topCountry) !=' undefined')
	 {
	     var new_arr = new Array();
	     
	     new_arr[0] = new Option();
	     new_arr[0].value = '-1';
	     new_arr[0].name  = '-----------';
	     
	     var i, j;
	    
	     var tc_cnt = topCountry.length;
	     var ca_cnt = arr.length;
	     var k = new_arr.length;
	     
	     for(i=0; i<tc_cnt; i++)
	     {
	         for(j=0; j<ca_cnt; j++)
		 {
		     if(topCountry[i] == arr[j].value)
		     {
		         new_arr[k] = arr[j];
			 k++;
			 
			 break;
		     }
		 }
	     }  
	     if(new_arr.length != 0)
	     {
	         ////  Add a divider after the top countries
		 //
		 var q = new_arr.length;
		 new_arr[q]       = new Option();
		 new_arr[q].value = '-1';
		 new_arr[q].name  = '-----------';
	    
	         ////  copy over all the countries from the countries array
                 //
                 k = new_arr.length;
	         ca_cnt = arr.length;
                 for(i=0; i<ca_cnt; i++)
	         {
	             new_arr[k] = arr[i];
		     k++;
	         }
	     
	         arr = new_arr; 
             }
	  }

         select_clear(id_carCountry);
         select_build(id_carCountry, arr);
	 
	 ////  Hide hte waiting image, if it exists
	 //
         if ((tmp=$(id_carCountry+'_loading')))   tmp.addClassName('invisible');
	 
         countrySelect.enable();

         if (_first_run_country == false)
         {
             _first_run_country = true;
             select_choose(id_carCountry, use_country);
         }
     }
 }


 ////  Init the city select list
 //
 var _first_run_city = false;
 function initCitySelect(input_param)
 {
     var citySelect = $(id_carCity);
     
     if (typeof(input_param) == 'object' && typeof(input_param.type) == 'string' && input_param.type == 'change')
     {
         ////  Input param is an event.
	 //    We have to determine the VALUE of the currently selected list item
	 //
	 input_param = $F(Event.element(input_param));
     }
     
     
     if ((typeof(input_param) == 'number' || typeof(input_param) == 'string') && (input_param == "-1" || input_param == -1))
     {
         select_clear(id_carCity);
	 return;
     }

     citySelect.disable();
     
     if ((tmp=$(id_carCity+'_loading')))   tmp.removeClassName('invisible');

     ////  If we receive anything but an object, then this must be the
     //    country id to select cities for, and not AJAX responce object
     //    In this case, we make the AJAX request
     //
     if (typeof(input_param) == 'number' || typeof(input_param) == 'string')
     {         
         xml_data = {
                        lang: 'de',
			opId: use_op_id,
			countryId: input_param
                    };

         ajax_fetch_city_xml(initCitySelect, proxy, xml_data);
     }

     ////  Second phase, parse the received XML data
     //
     else
     {         
         var arr = xml_parse_namevalue_list(input_param.responseText, fix_umlauts);

         select_clear(id_carCity);
         select_build(id_carCity, arr);
	 
	 if ((tmp=$(id_carCity+'_loading')))   tmp.addClassName('invisible');
         citySelect.enable();

         if (_first_run_city == false)
         {
             _first_run_city = true;
             select_choose(id_carCity, use_city);
         }
     }
 }












 ////  Store the combination of country/city, when we submit the form
 //    so that we would be able to restore them once we return from page2
 //
 function store_form_data()
 {
     var expires    = new Date(today.getTime() + 5 * 60 * 1000); // 5 minutes

     var cookie_value = $F(id_carCountry)    + ';' +
                        $F(id_carCity)       + ';' +
                        $F(id_carFromDate)   + ';' +
                        $F(id_carFromHour)   + ';' +
                        $F(id_carFromMinute) + ';' +
                        $F(id_carToDate)     + ';' +
                        $F(id_carToHour)     + ';' +
                        $F(id_carToMinute)   + ';' +
			$F(id_PromoCode)     + ';' +
			$F(id_partnerNo)     + ';' + 
			$F(id_opId);

     setCookie (cookie_name, cookie_value, expires);
 }





 function proceed(evt)
 {
     var error = false;
     
     //  Check if the country was selected
     //
     if ($F(id_carCountry) == -1)
     {
         alert(ERR_MSG_NO_COUNTRY);
         error = true;
     }


     ////  Check that the city has been selected
     //
     if (!error && $F(id_carCity) == -1)
     {
         alert(ERR_MSG_NO_CITY);
	 error = true;
     }


     var currentDate = new Date();
     var suppliedFromDate = date_get_from_value ($F(id_carFromDate), $F(id_carFromHour), $F(id_carFromMinute));
     var suppliedToDate   = date_get_from_value ($F(id_carToDate)  , $F(id_carToHour)  , $F(id_carToMinute)  );


     ////  Make sure both dates are properly formatted
     //
     if (!error && !(suppliedFromDate || suppliedToDate))
     {
         alert(ERR_MSG_WRONG_DATEFORMAT);
	 error = true;
     }

     ////  Make sure we are not trying to use dates in the past
     //
     if (!error && (suppliedFromDate < currentDate.getTime() || suppliedToDate < currentDate.getTime()))
     {
         alert(ERR_MSG_NO_DATES_IN_PAST);
	 error = true;
     }

     ////  Make sure the "from" date is actually earlier then the "end" date
     //
     if (!error && (suppliedFromDate >= suppliedToDate))
     {
         alert(ERR_MSG_TO_EARLIER_FROM);
	 error = true;
     }

     ////  Make sure the user doesn't book a car for less then MIN_RENTAL_TIME period
     //
     if (!error && ((suppliedToDate - suppliedFromDate) < min_rental_time))
     {
         alert(ERR_MSG_INCORRECT_TIME);
	 error = true;
     }

     ////  Make sure the user doesn't book a car over the MAX_RENTAL_TIME period
     //
     if (!error && ((suppliedToDate - suppliedFromDate) > max_rental_time))
     {
         alert(ERR_MSG_BOOKTIME_TOO_LONG);
	 error = true;
     }


     ////  Make sure we are not trying to book from a date that is more then a yar in the future
     //
     var todayDate = new Date();
     var tmpMaxDate = todayDate.getTime() + Date.DAY*365*2;

     if (!error && (suppliedFromDate > tmpMaxDate || suppliedToDate > tmpMaxDate))
     {
         alert(ERR_MSG_BOOKTIME_TOO_LONG);
	 error = true;
     }

     if (!error)
     {
         $('PromoCode').value = umlaut_convert($('PromoCode').value).toUpperCase();
     
     
         store_form_data();          
         submit_form();
     }
     Event.stop(evt);
 }
 
 function submit_form()
 {
      var add_params = {
                          display: 'index_ferienauto',
			  p      : 'page2',
			  exec   : 'GetCars',
			  lang   : 'de'
                      };

     object_to_form_submit(add_params, id_form);
 }








 
 
 //
 ////  Date functions
 //
 function fromdate_onchange()
 {
     var tmpUnixtime = date_get_from_value($F(id_carFromDate), 0, 0);

     if (tmpUnixtime == false)
     {
         alert(ERR_MSG_WRONG_DATEFORMAT);

         return false;

     }

     var tempdate = new Date();

     tempdate.setTime(tmpUnixtime + (todate_offset - fromdate_offset));
     $(id_carToDate).value = tempdate.print(date_format);

 }


 function todate_onchange()
 {
     var tmpUnixtime = date_get_from_value ($F(id_carToDate), 0, 0);

     if (tmpUnixtime == false)
     {
         alert(ERR_MSG_WRONG_DATEFORMAT);

         return false;
     }
 }
 
 
 
 
 
 
 
 
 
