//////////////////////////////////////////////////////////////////
// Ajax code: initially just for maps                           //
//															    // 
//////////////////////////////////////////////////////////////////

function makeRequest( url, this_element_name, changewhat )
  {
  var httpRequest;
    
  if( window.XMLHttpRequest ) 
    { // Mozilla, Safari, ...
    httpRequest = new XMLHttpRequest();
    
	if( httpRequest.overrideMimeType ) 
	  {
      httpRequest.overrideMimeType('text/xml');
      // See note below about this line
      }
    } 
  else if( window.ActiveXObject ) 
    { // IE
    try 
	  {
      httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
      } 
    catch( e ) 
	  {
      try 
	    {
        httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
        } 
       catch( e ) 
	     {
	     }
       }
     }

   if( !httpRequest ) 
     {
     //alert('Giving up :( Cannot create an XMLHTTP instance');
     return false;
     }
   
   httpRequest.onreadystatechange = function() { alertContents( httpRequest, this_element_name, changewhat ); };
   httpRequest.open('GET', url, true);
   httpRequest.send('');
   }

function alertContents( httpRequest, this_element_name, changewhat ) 
  {
  if( httpRequest.readyState == 4 )  
    {
    if( httpRequest.status == 200 ) 
	  {
	  switch( changewhat )
	    {
		case '0': 	
	    document.getElementById( this_element_name ).innerHTML = httpRequest.responseText;
		break;
		case '1': 	
	    document.getElementById( this_element_name ).style.backgroundImage = httpRequest.responseText;
		break;
		}
	  } 
	else 
	  {
      //alert('There was a problem with the request.');
      }
    }
  }
 
// RENAME TO SOMETHING SENSIBLE 
function ajax_farup( nameOfDiv, lang, daterange, accom, n_person, n_nights )
    { 	
    var div = document.getElementById( nameOfDiv );

    if( div )
      {
	  makeRequest('priser-farup-pakke-calculate.php?zzz_lang=' + lang + '&zzz_daterange='  +  daterange + '&zzz_accom='  +  accom + '&zzz_npeople='  +  n_person + '&zzz_nnights='  +  n_nights, nameOfDiv, '0' );  
      }
	}
	
	
function ajax_basic_price_calculate( nameOfDiv, lang, n_adult, p_adult, n_child, p_child, n_night )
    { 	
    var div = document.getElementById( nameOfDiv );
    
    if( div )
      {
	  // incase server is slow we write out a dashed line so old price disapears
      div.innerHTML = "----";
	  // Simulate a slow server here alert( "delay" );
	  makeRequest('priser-basic-calculate.php?zzz_lang=' + lang + '&zzz_n_adult='  +  n_adult + '&zzz_p_adult='  +  p_adult + '&zzz_n_child='  +  n_child + '&zzz_p_child='  +  p_child + '&zzz_n_night='  +  n_night, nameOfDiv, '0' );  
      }
	}	


function ajax_calendar( nameOfDiv, month )
    { 	
    var div = document.getElementById( nameOfDiv );

    if( div )
      { 
	  makeRequest('calendar.php?zzz_month=' + month, nameOfDiv, '0' );  
      }
	}
	
	
	
	
function ajax_special_messages( nameOfTitleDiv, nameOfTextDiv, nameOfIndex, lang )
    { 	
    var div_title = document.getElementById( nameOfTitleDiv );
    var div_text  = document.getElementById( nameOfTextDiv );
	var index  = document.getElementById( nameOfIndex );
	
    // TODO we need to pass in the name of the slash div - it is assumed here
	  
	if( div_title && div_text && index )
      {
	  var index_val = index.value;
	  
      makeRequest('special-messages-title.php?zzz_lang=' + lang + '&zzz_index=' + index_val, nameOfTitleDiv, '0' );
	  makeRequest('special-messages-text.php?zzz_lang=' + lang + '&zzz_index=' + index_val, nameOfTextDiv, '0' );
      makeRequest('special-messages-slash.php?zzz_lang=' + lang + '&zzz_index=' + index_val, 'slash', '1' );
	  }  
	}	
