 //
 //
 ////  Parse the country/city list XML and return an array of objects
 //
 //
 /*! VARIABLE INDEPENDENT */
 /*! FUNCTION INDEPENDENT */
 function xml_parse_namevalue_list(xml_string, text_parser_callback)
 {

     var _result       = new Array();
     //  XML document to parse, genereated off the XML string
     var doc           = xml_load(xml_string);

     xml_parse_messages(doc);

     var all_results   = doc.getElementsByTagName('Record');


     ////  Return "false" in case there are no results found
     //
     if (all_results == null)
     {
         return null;
     }

     var cnt         = all_results.length;
     var dom_node    = null;
     for (var i=0; i<cnt; i++)
     {
         dom_node  = all_results[i];

         node_name = dom_node.getElementsByTagName('Name')[0].childNodes[0].nodeValue;
         node_id   = dom_node.getElementsByTagName('Id')[0].childNodes[0].nodeValue;

	 node_name = fix_umlauts(node_name);

         if (text_parser_callback)
             node_name = text_parser_callback(node_name);

         _result[i] = $H({
                             name:   node_name,
                             value:  node_id
                         });
     }

     return _result;
 }


 //
 //
 ////  Requests country list XML data from the server and invokes a callback on
 //    the received results
 //
 /*! VARIABLE INDEPENDENT */
 /*! FUNCTION INDEPENDENT */
 var myCountryTemplate = null;
 function ajax_fetch_country_xml(parser_callback, proxy, xml_data)
 {
     if (myCountryTemplate == null)
     {
         ////  Initilize the XML template
         //
         countryRequest_xml_template = '<START>'+
                                           '<Administration>'+
                                               '<ObjectType>2</ObjectType>'+
                                               '<DemandedObjects>1001</DemandedObjects>'+
                                               '<Lang_id>#{lang}</Lang_id>'+
                                               '<Op_id>#{opId}</Op_id>'+
                                           '</Administration>'+
                                       '</START>';


         myCountryTemplate = new Template(countryRequest_xml_template);
     }


     if (!xml_data)
         xml_data = {
                        lang: 'en',
			opId: '100000'
                    };


     var countryRequest_xml = myCountryTemplate.evaluate(xml_data);


     var params = {
                      request: countryRequest_xml,
                      host:    proxy.host,
                      uri:     proxy.uri
                  };

     var opts   = {
                      method:     'post',
                      parameters:  params,
                      onSuccess:   parser_callback
                  };
     var myAjax = new Ajax.Request(proxy.path, opts);
 }






 //
 //
 ////  Requests city list XML data from the server and invokes a callback on
 //    the received results
 //
 /*! VARIABLE INDEPENDENT */
 /*! FUNCTION INDEPENDENT */
 var myCityTemplate = null;
 function ajax_fetch_city_xml(parser_callback, country_id, proxy, xml_data)
 {
     if (myCityTemplate == null)
     {
         ////  Initilize the XML template
         //
         cityRequest_xml_template = '<START>'+
                                        '<Administration>'+
                                            '<ObjectType>2</ObjectType>'+
                                            '<DemandedObjects>1000</DemandedObjects>'+
                                            '<Lang_id>#{lang}</Lang_id>'+
                                            '<Op_id>#{opId}</Op_id>'+
                                            '<Country_id>#{countryId}</Country_id>'+
                                        '</Administration>'+
                                    '</START>';


         myCityTemplate = new Template(cityRequest_xml_template);
     }


     if (!xml_data)
         xml_data = {
                        lang: 'en',
			opId: '100000',
                        countryId: country_id
                    };
     else
         xml_data = Object.extend(xml_data, {countryId: country_id});


     var cityRequest_xml = myCityTemplate.evaluate(xml_data);




     var params = {
                      request: cityRequest_xml,
                      host:    proxy.host,
                      uri:     proxy.uri
                  };

     var opts   = {
                      method:     'post',
                      parameters:  params,
                      onSuccess:   parser_callback
                  };
     var myAjax = new Ajax.Request(proxy.path, opts);
 }




 function xml_parse_user_data(xml_string, text_parser_callback)
 {
     var _result       = new Array();
     //  XML document to parse, genereated off the XML string
     var doc           = xml_load(xml_string);

     var data;

     xml_parse_messages(doc);

     var all_results   = doc.getElementsByTagName('UserData');

     ////  Return "false" in case there are no results found
     //
     if (all_results == null)
     {
         return 0;
     }

     data  = all_results[0];

     var tmp;

     _use_driverSalutation    = ((tmp = data.getElementsByTagName('adress')[0].childNodes      ).length != 0) ? tmp[0].nodeValue : '';
     _use_driverName          = ((tmp = data.getElementsByTagName('name')[0].childNodes        ).length != 0) ? tmp[0].nodeValue : '';
     _use_firstname           = _use_driverName;
     _use_driverFirstname     = ((tmp = data.getElementsByTagName('firstname')[0].childNodes   ).length != 0) ? tmp[0].nodeValue : '';
     _use_name                = _use_driverFirstname;
     _use_driverDobDay        = ((tmp = data.getElementsByTagName('dobDay')[0].childNodes      ).length != 0) ? tmp[0].nodeValue : '';
     _use_driverDobMonth      = ((tmp = data.getElementsByTagName('dobMonth')[0].childNodes    ).length != 0) ? tmp[0].nodeValue : '';
     _use_driverDobYear       = ((tmp = data.getElementsByTagName('dobYear')[0].childNodes     ).length != 0) ? tmp[0].nodeValue : '';
     _use_street              = ((tmp = data.getElementsByTagName('street')[0].childNodes      ).length != 0) ? tmp[0].nodeValue : '';
     _use_zip                 = ((tmp = data.getElementsByTagName('zip')[0].childNodes         ).length != 0) ? tmp[0].nodeValue : '';
     _use_city                = ((tmp = data.getElementsByTagName('city')[0].childNodes        ).length != 0) ? tmp[0].nodeValue : '';
     _use_thisCountry         = ((tmp = data.getElementsByTagName('thisCountry')[0].childNodes ).length != 0) ? tmp[0].nodeValue : '';
     _use_telCountry          = ((tmp = data.getElementsByTagName('telCountry')[0].childNodes  ).length != 0) ? tmp[0].nodeValue : '';
     _use_telAreaCode         = ((tmp = data.getElementsByTagName('telAreaCode')[0].childNodes ).length != 0) ? tmp[0].nodeValue : '';
     _use_telExtension        = ((tmp = data.getElementsByTagName('telExtension')[0].childNodes).length != 0) ? tmp[0].nodeValue : '';
     _use_email               = ((tmp = data.getElementsByTagName('email')[0].childNodes       ).length != 0) ? tmp[0].nodeValue : '';
     _use_faxCountry          = ((tmp = data.getElementsByTagName('faxCountry')[0].childNodes  ).length != 0) ? tmp[0].nodeValue : '';
     _use_faxAreaCode         = ((tmp = data.getElementsByTagName('faxAreaCode')[0].childNodes ).length != 0) ? tmp[0].nodeValue : '';
     _use_faxExtension        = ((tmp = data.getElementsByTagName('faxExtension')[0].childNodes).length != 0) ? tmp[0].nodeValue : '';
     _use_ccHolder            = ((tmp = data.getElementsByTagName('ccHolder')[0].childNodes    ).length != 0) ? tmp[0].nodeValue : '';
     _use_ccId                = ((tmp = data.getElementsByTagName('ccId')[0].childNodes        ).length != 0) ? tmp[0].nodeValue : '';
     _use_ccNo                = ((tmp = data.getElementsByTagName('ccNo')[0].childNodes        ).length != 0) ? tmp[0].nodeValue : '';
     _use_ccExpMonth          = ((tmp = data.getElementsByTagName('ccExpMonth')[0].childNodes  ).length != 0) ? tmp[0].nodeValue : '';
     _use_ccExpYear           = ((tmp = data.getElementsByTagName('ccExpYear')[0].childNodes   ).length != 0) ? tmp[0].nodeValue : '';

     return 1;
 }



 ////  xml_parse_error_messages(document_object)
 //
 function xml_parse_messages(document_object)
 {
     var tmp;

     ////  Parse out the error messages and warnings
     //
     tmp = document_object.getElementsByTagName('ERROR_MESSAGE');
     if (tmp.length!=0) error_msg = tmp[0].childNodes[0].nodeValue.replace("\\n", "\n");

     tmp = document_object.getElementsByTagName('SUCCESS_MESSAGE');
     if (tmp.length!=0) success_msg = tmp[0].childNodes[0].nodeValue.replace("\\n", "\n");

     tmp = document_object.getElementsByTagName('BUSINESS_MESSAGE');
     if (tmp.length!=0) business_msg = tmp[0].childNodes[0].nodeValue.replace("\\n", "\n");
 }







 //
 //
 ////  Requests car extras XML data from the server and invokes a callback on
 //    the received results
 //
 function ajax_fetch_extras_xml(parser_callback, data)
 {
    var add_params = {
			display		:  "wrapper",
			p		:  "wrapper.jsp",
			action          :  "get_extras",
			exec		:  "GetCarExtras",
			cmd		:  ""
                     };

    add_params = Object.extend(data, add_params);


    var opts   = {
                     method:     'post',
                     parameters:  add_params,
                     onSuccess:   parser_callback
                 };



    var myAjax = new Ajax.Request("/MBServlet", opts);
 }




 function xml_parse_extras_list(xml_string, text_parser_callback)
 {
     var _result       = new Array();
     //  XML document to parse, genereated off the XML string
     var doc           = xml_load(xml_string);

     var all_results   = doc.getElementsByTagName('extra');

     ////  Return "false" in case there are no results found
     //
     if (all_results == null)
     {
         return null;
     }

     var cnt       = all_results.length;
     var dom_extra = null;
     for (var i=0; i<cnt; i++)
     {
         dom_extra  = all_results[i];

         var extra_name;
	 var extra_id;
	 var extra_checked;
	 var extra_amount;

         extra_name    = dom_extra.getElementsByTagName('name')[0].childNodes[0].nodeValue;
         extra_id      = dom_extra.getElementsByTagName('id')[0].childNodes[0].nodeValue;
	 extra_checked = dom_extra.getElementsByTagName('checked')[0].childNodes[0].nodeValue;
	 extra_amount  = dom_extra.getElementsByTagName('amount')[0].childNodes[0].nodeValue;

         if (text_parser_callback)
             extra_name = text_parser_callback(extra_name);

         _result[i] = {
                          name   :  extra_name,
                          id     :  extra_id,
			  amount :  extra_amount,
			  checked:  extra_checked
                      };
     }

     return _result;
 }




 function ajax_fetch_statinfo_xml(parser_callback, data)
 {
     var add_params = {
                          display : 'wrapper',
                          p       : 'wrapper.jsp',
                          action  : 'get_statinfo'
                      };

     add_params = Object.extend(data, add_params);

     var opts = {
                    method     : 'post',
                    parameters : add_params,
                    onSuccess  : parser_callback
                };

     var myAjax = new Ajax.Request("/MBServlet", opts);
 }




 function xml_parse_statinfo_list(xml_string, text_parser_callback)
 {
     var _result = new Array();
     var doc = xml_load(xml_string);

     var pickup_station = doc.getElementsByTagName('pickup_station')[0];
     var return_station = doc.getElementsByTagName('return_station')[0];

     _result = {
                   pickup_location : pickup_station.getElementsByTagName('pickup_location')[0].childNodes[0].nodeValue,
                   pickup_phone    : pickup_station.getElementsByTagName('pickup_phone')[0].childNodes[0].nodeValue,
                   pickup_fax      : pickup_station.getElementsByTagName('pickup_fax')[0].childNodes[0].nodeValue,
                   pickup_open     : pickup_station.getElementsByTagName('pickup_open')[0].childNodes[0].nodeValue,
                   return_location : return_station.getElementsByTagName('return_location')[0].childNodes[0].nodeValue,
                   return_phone    : return_station.getElementsByTagName('return_phone')[0].childNodes[0].nodeValue,
                   return_fax      : return_station.getElementsByTagName('return_fax')[0].childNodes[0].nodeValue,
                   return_open     : return_station.getElementsByTagName('return_open')[0].childNodes[0].nodeValue
               };

     return _result;
 }











 function fix_umlauts(text)
 {
     var result = text;

     result = result.replace(/&#228;/g, String.fromCharCode(228) ); // &auml;
     result = result.replace(/&#196;/g, String.fromCharCode(196) ); // &Auml;
     result = result.replace(/&#252;/g, String.fromCharCode(252) ); // &uuml;
     result = result.replace(/&#220;/g, String.fromCharCode(220) ); // &Uuml;
     result = result.replace(/&#246;/g, String.fromCharCode(246) ); // &ouml;
     result = result.replace(/&#214;/g, String.fromCharCode(214) ); // &Ouml;
     result = result.replace(/&#180;/g, String.fromCharCode(180) ); // &acute;
     result = result.replace(/&#224;/g, String.fromCharCode(224) ); // &agrave;
     result = result.replace(/&#242;/g, String.fromCharCode(242) ); // &ograve;

     return result;
 }