var xmlHttp
$(document).ready(function() {
        var Dest="";
	if(Dest == ''){
		available();
		callAjax("Destination",'air_hotel','');

   		$("input[@name='booking_type']").click(function(event, section, DestCode, booking_type) {
			if(DestCode != 'undefined' && section == 'Nights'){
				callAjax(section,DestCode,booking_type);
			}else{
				available();
				var Option=$(this).val();
				$('#city_droplist').show();
				reset_destination();
				reset_nights();
				reset_DepartDate();
				reset_flight();
				$('#nights_droplist').show();
				callAjax("Destination",Option,'');
			}
   		});

   		$('#Destination').change(function(event) {
			available();
	   		reset_nights();
	   		reset_DepartDate();
	   		reset_flight();
           		$('#nights_droplist').show();
	   		var DestCode=$(this).val();
			if((DestCode == 'P7' || DestCode == 'P5') && $("input[@name='booking_type']").val() == 'air_hotel'){
				alert("Air Packages are not available to Panama City Beach, FL");
				$("input[@name='booking_type']").trigger('click', ['Nights', DestCode, 'hotel_only']);
			}else{
           		callAjax("Nights",DestCode,'');
			}
			if((DestCode == 'T4') && $("input[@name='booking_type']").val() == 'air_hotel'){
				alert("Air Packages are not available to South Padre Island, TX");
				$("input[@name='booking_type']").trigger('click', ['Nights', DestCode, 'hotel_only']);
			}else{
           		callAjax("Nights",DestCode,'');
			}
			$('INPUT[id=DestCode2]').attr({value: DestCode});
			$('INPUT[id=DestCode]').attr({value: DestCode});
   		});
      }else{
		$('#bodytext').hide();
 		$('#destination_droplist').hide();
		callAjax("Nights",Dest,'');
      }
   $('#Nights').change(function(event) {
		available();
        reset_DepartDate();
		reset_flight();
        var DestCode=$(this).val();
        $('INPUT[id=DestCode2]').attr({value: DestCode});
        $('INPUT[id=DestCode]').attr({value: DestCode});
        callAjax("DepartDate",DestCode,'');
		$('SELECT[id=DepartDate]').attr({value: '0'});
   });
   $('#DepartDate').change(function(event) {
	available();
           var DestCode=$(this).val();
	   reset_flight();
	   var bookingType = $("input[name=booking_type]:checked").val();
       	   $('INPUT[id=TourCode]').attr({value: DestCode});
	 
	   if(bookingType=='hotel_only')
	   {
		$('INPUT[id=hotel_only]').attr({value: '1'});
		callAjax("hotel_only",DestCode,bookingType);
	   }
           else
           {
	   	callAjax("Flight",DestCode,bookingType);
	   }
   });
   $('#Flight').change(function(event) {
	available();
           var DestCode=$(this).val();
           $('INPUT[id=FTID]').attr({value: DestCode});
	   var Tour=$('INPUT[id=TourCode]').val();	   
	   var sub=$('INPUT[id=hotel_only]').val();
	   if (sub)
	   {
	   	document.PriceBox.submit();
	   }
	   callAjax("checkDup",DestCode,Tour);
   });

});

function callAjax(type,str,bookingType)
{ 
 	xmlHttp=GetXmlHttpObject()
 	if (xmlHttp==null)
  	{
  		alert ("Browser does not support HTTP Request")
  		return
  	}
	if (type == "Nights")
	{
		var url="/pricebox/pricebox.php"
		url=url+"?q="+str
		url=url+"&type="+type
		url=url+"&sid="+Math.random()
		xmlHttp.onreadystatechange=findNights
		xmlHttp.open("GET",url,true)
		xmlHttp.send(null)
	}
	else if (type == "Destination")
	{
                var url="/pricebox/pricebox.php"
                url=url+"?q="+str
                url=url+"&type="+type
                url=url+"&sid="+Math.random()
                xmlHttp.onreadystatechange=findDestinations
                xmlHttp.open("GET",url,true)
                xmlHttp.send(null)
        }
	else if (type == "DepartDate")
        {
                var url="/pricebox/pricebox.php"
                url=url+"?q="+str
                url=url+"&type="+type
                url=url+"&sid="+Math.random()
                xmlHttp.onreadystatechange=findDepartDate
                xmlHttp.open("GET",url,true)
                xmlHttp.send(null)
        }
        else if (type == "Flight")
        {
                var url="/pricebox/pricebox.php"
                url=url+"?q="+str
                url=url+"&type="+type
		url=url+"&btype="+bookingType
                url=url+"&sid="+Math.random()
                xmlHttp.onreadystatechange=findFlights
                xmlHttp.open("GET",url,true)
                xmlHttp.send(null)
	}
	else if (type == "hotel_only")
	{
                var url="/pricebox/pricebox.php"
                url=url+"?q="+str
                url=url+"&type="+type
                url=url+"&btype="+bookingType
                url=url+"&sid="+Math.random()
                xmlHttp.onreadystatechange=findHotel
                xmlHttp.open("GET",url,true)
                xmlHttp.send(null)
	}
	else if (type == "checkDup")
	{
        	var url="/pricebox/pricebox.php"
        	url=url+"?q="+str
        	url=url+"&type="+type
		url=url+"&btype="+bookingType
        	url=url+"&sid="+Math.random()
        	xmlHttp.onreadystatechange=checkDuplicates
        	xmlHttp.open("GET",url,true)
        	xmlHttp.send(null)
	}
}
function findHotel()
{
        if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
        {
                xmlDoc=xmlHttp.responseXML;
                var number=xmlDoc.getElementsByTagName("count")[0].childNodes[0].nodeValue;
                var tour=xmlDoc.getElementsByTagName("tour")[0].childNodes[0].nodeValue;
                var count=0;
			
                while (count < number){
                        var id=xmlDoc.getElementsByTagName("id")[count].childNodes[0].nodeValue;
                        var value=xmlDoc.getElementsByTagName("value")[count].childNodes[0].nodeValue;
                        say(id,value,"Flight");
                        count++;
                }
		if(count<1)
		{
                        unavailable('city');
		}
                $('SELECT[id=Flight]').attr({value: '0'});
        }
}

function findFlights()
{
        if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")	
        {
                xmlDoc=xmlHttp.responseXML;
                var number=xmlDoc.getElementsByTagName("count")[0].childNodes[0].nodeValue;
		var tour=xmlDoc.getElementsByTagName("tour")[0].childNodes[0].nodeValue;

		var count=0;		
		var ftid=xmlDoc.getElementsByTagName("hotel")[count].childNodes[0].nodeValue;
		
		/*if(number == 0){
			$("#PriceBox").attr("action","http://www.sunsplashtours.com/price/choose_land_only.php");
			document.PriceBox.submit();
		}*/

		while (count < number){
       	                var id=xmlDoc.getElementsByTagName("id")[count].childNodes[0].nodeValue;
       	                var value=xmlDoc.getElementsByTagName("value")[count].childNodes[0].nodeValue;
       	                say(id,value,"Flight");
       	                count++;
	        }
		if(count == 0)
		{
			unavailable('city');
		}
		if(ftid > 0)
		{
		     $('INPUT[id=FTID]').attr({value: ftid});
		     document.PriceBox.submit();		
		}
   		$('SELECT[id=Flight]').attr({value: '0'});
        }
}
function findDepartDate()
{
        if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
        {
                xmlDoc=xmlHttp.responseXML;
                var number=xmlDoc.getElementsByTagName("count")[0].childNodes[0].nodeValue;
		var count=0;
                while (count < number){

                        var id=xmlDoc.getElementsByTagName("id")[count].childNodes[0].nodeValue;
                        var value=xmlDoc.getElementsByTagName("value")[count].childNodes[0].nodeValue;
                        say(id,value,"DepartDate");
                        count++;
                }
		if (count == 0)
		{
			unavailable('depart_date');
		}
                $('SELECT[id=DepartDate]').attr({value: '0'});
        }
}

function findDestinations()
{
        if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
        {
                xmlDoc=xmlHttp.responseXML;
                var number=xmlDoc.getElementsByTagName("count")[0].childNodes[0].nodeValue;
                var count=0;
                while (count < number)
		{

                        var id=xmlDoc.getElementsByTagName("id")[count].childNodes[0].nodeValue;
                        var value=xmlDoc.getElementsByTagName("value")[count].childNodes[0].nodeValue;
                        say(id,value,"Destination");
                        count++;
                }
                $('SELECT[id=Destination]').attr({value: '0'});
        }
}

function findNights() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{
		xmlDoc=xmlHttp.responseXML;
		var number=xmlDoc.getElementsByTagName("count")[0].childNodes[0].nodeValue;
		var DestCode=xmlDoc.getElementsByTagName("destcode")[0].childNodes[0].nodeValue;
		if(number == 1)
		{
		     hide_nights_droplist();
		     callAjax("DepartDate",DestCode);
		}
		else
		{
			show_nights_droplist();
			var count=0;
			while (count < number){

				var id=xmlDoc.getElementsByTagName("id")[count].childNodes[0].nodeValue;
				var value=xmlDoc.getElementsByTagName("value")[count].childNodes[0].nodeValue;
				say(id,value,"Nights");
				count++;
			}
			$('SELECT[id=Nights]').attr({value: '0'});
		    //$('INPUT[id=DestCode2]').attr({value: DestCode});
		}
 	}
} 
function GetXmlHttpObject()
{ 
 	var objXMLHttp=null
 	if (window.XMLHttpRequest)
  	{
  		objXMLHttp=new XMLHttpRequest()
  	}
 	else if (window.ActiveXObject)
  	{
  		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
  	}
 	return objXMLHttp
}
function say(id,text,choice) {
       	$('SELECT[id='+choice+']').append('<Option value='+id+'>'+text+'</Option>');
}
function hide_flights_droplist()
{
	$('#city_droplist').hide();
}
function show_flights_droplist()
{
	Element.show('city_droplist');
}
function hide_nights_droplist()
{
	$('#Nights').hide();
}
function show_nights_droplist()
{
	$('#Nights').show();
}
function reset_nights()
{
  var obj_nights = document.PriceBox.Nights;
  obj_nights.options.length=0;
  obj_nights.options[obj_nights.options.length] = new Option('Select Nights','0');
}
function reset_flight()
{
	var obj_nights = document.PriceBox.Flight; 
	obj_nights.options.length=0;
	obj_nights.options[obj_nights.options.length] = new Option('Select Departure City','0');
}

function reset_destination()
{
  var obj_nights = document.PriceBox.Destination;
  obj_nights.options.length=0;
  obj_nights.options[obj_nights.options.length] = new Option('Select Destination','0');

}
function reset_DepartDate()
{
  var obj_nights = document.PriceBox.DepartDate;
  obj_nights.options.length=0;
  obj_nights.options[obj_nights.options.length] = new Option('Select Departure Date','0');

}
function unavailable(spanID)
{
	if(spanID == 'depart_date')
	{
		$('#depart_date_message').show();
		$('#depart_date_message').html('<font color="red">Not Available</font>');
		$('#depart_date_droplist').hide();
		$('#city_droplist').hide();
	}
	else if(spanID == 'city')
	{
		$('#city_message').show();
		$('#city_message').html('<font color="red">Not Available</font>');
		$('#city_droplist').hide();
	}
}
function available()
{
	$('#depart_date_message').hide();
	$('#city_message').hide();
	$('#depart_date_droplist').show();
	$('#city_droplist').show();
}
function checkDuplicates()
{
 	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
        {
                xmlDoc=xmlHttp.responseXML;
                var number=xmlDoc.getElementsByTagName("count")[0].childNodes[0].nodeValue;
		if(number > 1)
		{      
			var ftid=xmlDoc.getElementsByTagName("ftid")[0].childNodes[0].nodeValue;
			var departcode=xmlDoc.getElementsByTagName("departcode")[0].childNodes[0].nodeValue;
			var tourcode=xmlDoc.getElementsByTagName("tourcode")[0].childNodes[0].nodeValue;	
			$('INPUT[id=DepartureCode]').attr({value: departcode});
			$('INPUT[id=TourCode]').attr({value: tourcode});
			$('INPUT[id=FTID]').attr({value: ftid});
			//$('INPUT[id=DepartureCode]').attr({value: "BOS"});
			//$("#PriceBox").attr("action","http://www.sunsplashtours.com/price/FlightInventory.cgi?FTID="+ftid+"&TourCode="+tourcode+"&DepartureCode="+departcode+"&DepartureCode="+DestCode+"");
			$("#PriceBox").attr("action","http://www.cstravel.org/price/FlightInventory_w.cgi")
		}
		document.PriceBox.submit();
        }
}

