/*
* Aux functions for 1st page from site
* 
*/

function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	//alert("RadioLength: " + radioLength );

	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
  	//alert( i + " checked: " + radioObj[i].checked + " value: " + radioObj[i].value);
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}


function createParamsAux(paramName, isRadioButton) {
  var returnValue = "&" + paramName + "=";
  
  if (isRadioButton) {
    returnValue = returnValue + getCheckedValue( document.forms['initial_booking_form'].elements[paramName] );
  }
  else {
    returnValue = returnValue + document.getElementById(paramName).value;
  }
  
  //alert(returnValue);
  
  return returnValue;
}

function gotoNextStep() {    
  document.getElementById("nextStep").value = '1';
  //alert( "In gotoNextStep" );
  var noOfPassengers = parseInt(document.getElementById("no_of_grown_ups").value) + 
       parseInt(document.getElementById("no_of_children").value)  ;

  if (noOfPassengers >= 1) {

    var flightType = getCheckedValue( document.forms['initial_booking_form'].elements["flight_type"] );

    var params = "nextStep=1" +
                 createParamsAux("departure_id",false) +
                 createParamsAux("arrival_id",false) +
                 createParamsAux("flight_type",true) +
                 createParamsAux("outbound_time_filter",false) +
                 createParamsAux("outbound_date_flight",false)
                 ;
    if ( flightType == '2' ) {
      params = params  +
                 createParamsAux("inbound_time_filter",false) +
                 createParamsAux("inbound_date_flight",false)
                 ;
    }
    
    params = params +
                 createParamsAux("no_of_grown_ups",false) +
                 createParamsAux("no_of_children",false) +
                 createParamsAux("no_of_infants",false)
                 ;

    //alert( params );

    fhc_booking_window = window.open("../site/form1.php?" + params, "",
    ",type=fullWindow,fullscreen,scrollbars=yes"
    );
  }
  else {
    alert( 'Please choose number of Passengers.' );
  }
  
  return true;    
}	

function handleResponse( response, status, xhr) {
  // load posts the parameters
  //alert( 'HTML: ' + response );
  if (status == "error") {
    var msg = "Sorry but there was an error: ";
    alert( msg + xhr.status + " " + xhr.statusText );
  }
  else {
    if ( response == 'RELOAD') {
      window.location.href=window.location.href + '&departure_id=' + $("select#departure_id").val() + '&no_of_grown_ups=1';
    }
    if ( response.substring(0,5) == 'ERROR' ) {
      alert( response );
    }
  }
}

function fbh_initOnLoad() {
  // Define onChange events that will auto-populate next field.
  
  // alert("Start of ready function");
  
  // change on departure -> get Arrivals
	$("select#departure_id").change(function(){
	  if ( $(this).val() == '-1' ) {
	   $("select#arrival_id").html("");
	  }
	  else {
      $("select#arrival_id").load("form1_from_site_ajax_requests.php", 
        {departure_id:$(this).val(), type: "ARRIVALS"}, 
      handleResponse);
    }
    
    $("select#outbound_time_filter").html("");
    $("select#outbound_date_flight").html("");
    $("select#inbound_time_filter").html("");
    $("select#inbound_date_flight").html("");
	}); // change on departure -> get Arrivals


  // change on arrival -> get Outbound months
	$("select#arrival_id").change(function(){
	  if ( $(this).val() == '-1' ) {
	   $("select#outbound_time_filter").html("");
	  }
	  else {
      $("select#outbound_time_filter").load("form1_from_site_ajax_requests.php", 
        {arrival_id:$(this).val(), departure_id:$("select#departure_id").val(), type: "OUTBOUND_FILTER"}, 
      handleResponse);
    }
    
    $("select#outbound_date_flight").html("");
    $("select#inbound_time_filter").html("");
    $("select#inbound_date_flight").html("");
	}); // change on arrival -> get Outbound months


  // change on outbound filter -> get Outbound date
	$("select#outbound_time_filter").change(function(){
	  if ( $(this).val() == '-1' ) {
	   $("select#outbound_date_flight").html("");
	  }
	  else {
      $("select#outbound_date_flight").load("form1_from_site_ajax_requests.php", 
        {outbound_filter:$(this).val(),
         arrival_id:$("select#arrival_id").val(), 
         departure_id:$("select#departure_id").val(), 
         type: "OUTBOUND_DATE"}, 
      handleResponse);
    }

    $("select#inbound_time_filter").html("");
    $("select#inbound_date_flight").html("");
	}); // change on outbound filter -> get Outbound date


  // change on outbound date -> get Inbound filter  			
	$("select#outbound_date_flight").change(function(){
	  if ( $(this).val() == '-1' ) {
	   $("select#inbound_time_filter").html("");
	  }
	  else {
      $("select#inbound_time_filter").load("form1_from_site_ajax_requests.php"
      , 
        {outbound_date_id     : $(this).val(), 
         outbound_filter      : $("select#outbound_time_filter").val(),
         arrival_id           : $("select#arrival_id").val(), 
         departure_id         : $("select#departure_id").val(), 
         type                 : "INBOUND_FILTER"
        }
      , 
        handleResponse);
    }
    $("select#inbound_date_flight").html("");
	}); // change on outbound date -> get Inbound filter


  // change on Inbound filter -> get Inbound date  			
	$("select#inbound_time_filter").change(function(){
	  if ( $(this).val() == '-1' ) {
	   $("select#inbound_date_flight").html("");
	  }
	  else {
      $("select#inbound_date_flight").load("form1_from_site_ajax_requests.php"
      , 
        {
          type                 : "INBOUND_DATE",
          departure_id         : $("select#departure_id").val(), 
          arrival_id           : $("select#arrival_id").val(), 
          outbound_date_id     : $("select#outbound_date_flight").val(),
          inbound_filter       : $(this).val()
        }
      , 
        handleResponse);
    }
	}); // change on Inbound filter -> get Inbound date
}

