function openAdWindow(iPortalId, iAdId, iCompanyId) 
{
	var thisURL = "flight_v3_click_through_ad.cfm?portalId=" + iPortalId + "&DM_SearchResultId=-1&adid=" + iAdId + "&companyid=" + iCompanyId + "&DM_SearchParamsId=" + nSearchID;
	window.open(thisURL,'_blank');
}

function openGoogleMapsAirports(sType)
{
	var sURL = "";
	var sWinName = "";
	var sFeatures = "";
	sURL = sGooglemapPath + '?nType=3&nSearchID=' + nSearchID + '&nWidth=' + nGooglemapWidth + '&nHeight=' + nGooglemapHeight + '&nSupplierID=' + nSupplierID + '&sAirportType=' + sType;
	sWinName = sType + "_airport";
	sFeatures = 'height=' + nGooglemapHeight + ',width=' + nGooglemapWidth + ',toolbar=no,titlebar=no,status=no,resizable=yes,menubar=no,location=no,directories=no';
	window.open(sURL,sWinName,sFeatures);
};

function trackFlightBehaviour(nTrackingElementId,sTrackingValue) 
{

	nFlightTrackingStepCounter = nFlightTrackingStepCounter + 1;
	if (bFlightTracking == true) {
		new Ajax.Request(sAjaxUrl, 
		{ 
			method:'post', 
			parameters: 
			{
				action: "SaveTracking",
				sSession: sClientFlightSession,
				nSupplierID: nSupplierID,
				nTrackingElementId: nTrackingElementId,
				nTrackingStep: nFlightTrackingStepCounter,
				sTrackingValue: sTrackingValue,
				uniqueValue: makeUniqueValue()
			}
		});
	}
};
 
 
function findCity(type) {

	if(type=='departure') {
		if (nDepartureCityOption == 0) {
			var form = $("tse_flight_search_form");
			var elem = document.createElement("input");
			elem.type = "hidden";
			elem.name = "departure";
			elem.id = "departure";
			elem.value = "";
			form.appendChild(elem);
			//$('departureTd').update('<input type="text" id="departureName" name="departureName" class="departureInputFaded" onBlur="$(\'departureName\').className=\'departureInput\';validateDepartureInput();" onFocus="$(\'departureName\').className=\'departureInputFocused\';" onclick="setFocus(\'departureName\');" onkeyup="getDeparture(this);"/><br/><div id="departure_airport_select_container" style="clear:both;display:none;"><\/div>');
			$('departureTd').update('<div id="departure_airport_select_container" style="clear:both;display:none;"><\/div>');
			$("departureName").show();
			nDepartureCityOption = 1;
			
		} else {
			nDepartureCityOption = 0;
			$("departure").remove();
			new Ajax.Request(sAjaxUrl,
			{
				method:'post',
				parameters: {
					action: "DropDownAirport",
					nSupplierID: nSupplierID,
			   		prefix: "departure",
			   		departureCountryId: sOptionDefaultCountry,
			   		defaultDepartureCity: sOptionDefaultCity
			   	}, 
			    onSuccess: function(transport){
					var response = transport.responseText;
					$("departureName").hide();
					$("departureTd").update(response);
			    }
			  });
		}
	}
	else if (type=='destination') {
		if (nDestinationCityOption == 0) {
			var form = $("tse_flight_search_form");
			var elem = document.createElement("input");
			elem.type = "hidden";
			elem.name = "destination";
			elem.id = "destination";
			elem.value = "";
			form.appendChild(elem);
			//$('destinationTd').update('<input type="text" id="destinationName" name="destinationName" class="destinationInputFaded"  onFocus="$(\'destinationName\').className=\'destinationInputFocused\';" onBlur="$(\'destinationName\').className=\'destinationInput\';validateDestinationInput();" onclick="setFocus(\'destinationName\');" onkeyup="getDestination(this);"/><br/><div id="destination_airport_select_container" style="clear:both;display:none;"><\/div>');
			$('destinationTd').update('<div id="destination_airport_select_container" style="clear:both;display:none;"><\/div>');
			$('destinationName').show();
			nDestinationCityOption = 1;
	 	} else {
			nDestinationCityOption = 0;
			$("destination").remove();
			new Ajax.Request(sAjaxUrl,
			{
				method:'post',
				parameters: {
					action: "DropDownAirport",
					nSupplierID: nSupplierID,
					prefix: "destination",
					destinationCountryId: sOptionDefaultDestinationCountry,
					defaultDestinationCity: sOptionDefaultDestinationCity
			}, 
		    onSuccess: function(transport){
		      var response = transport.responseText;
		      $("destinationName").hide();
		      $("destinationTd").update(response);
		    }
			});
		}
	}

};
	
		
function getCities(type,country) {
	if(type=='departure') {
		new Ajax.Request(sAjaxUrl,
		{
			method:'post',
			parameters: {
				action: "DropDownAirport",
				nSupplierID: nSupplierID,
				prefix: "departure",
				departureCountryId: country
			}, 
	    	onSuccess: function(transport) {
	      		var response = transport.responseText; 
	      		$("departureTd").update(response);
	    	}
		});
	}
	else if (type=='destination') {
		new Ajax.Request(sAjaxUrl,
		{
			method:'post',
			parameters: {
				action: "DropDownAirport",
				nSupplierID: nSupplierID,
				prefix: "destination",
				destinationCountryId: country
			}, 
			onSuccess: function(transport){
				var response = transport.responseText;
				$("destinationTd").update(response);
			}
		});
	}
}

function unClickAllOutAirports(iAirport) {
	for(i=0;i < $('nNumberOfDepartureAirports').value;i++)
	{
		$('bFilterOutAirport'+i).checked = false;
	}
	$("bFilterOutAirport" + iAirport).click();
};

function unClickAllHomeAirports(iAirport) {
	for(i=0;i < $('nNumberOfReturnAirports').value;i++)
	{
		$('bFilterHomeAirport'+i).checked = false;
	}
	$("bFilterHomeAirport" + iAirport).click();
};

function unClickAllCarriers(iCarrier) {
	for(i=0;i < $('nNumberOfCarriers').value;i++)
	{
		$('bFilterCarrier'+i).checked = false;
	}
	$("bFilterCarrier" + iCarrier).click();
};

function unClickAllStops(iStop) {
	for(i=0;i < $('nNumberOfStops').value;i++)
	{
		try
		{
			$('bFilterStop' + i).checked = false;
		} catch (err) { }
	}
	$("bFilterStop" + iStop).click();
};

function toggleStop(iStop)
{
	var nChecked = 0;
	currentPage = 1;
	if($('nNumberOfStops')) 
	{
		for(i=0;i < $('nNumberOfStops').value;i++)
		{
			try 
			{
				if ($('bFilterStop'+i).checked)
				{
					nChecked++;
				}
			} catch (err) {}
		}
	}
	if (nChecked == 0) 
	{
		$('bFilterStop'+iStop).checked = true;
	} 
	else 
	{
		getListFilter(1);
	}
};

function toggleCarrier(iCarrier)
{
	var nChecked = 0;
	currentPage = 1;
	if($('nNumberOfCarriers')) 
	{
		for(i=0;i < $('nNumberOfCarriers').value;i++)
		{
			if ($('bFilterCarrier'+i).checked)
			{
				nChecked++;
			}
				
		}
	}

	if (nChecked == 0) {
		$('bFilterCarrier'+iCarrier).checked = true;
	} else {
		getListFilter(1);
	}
};

function toggleReturnAirport(iAirport)
{
	var nChecked = 0;
	currentPage = 1;
	if($('nNumberOfReturnAirports')) 
	{
		for(i=0;i < $('nNumberOfReturnAirports').value;i++)
		{
			if ($('bFilterHomeAirport'+i).checked)
			{
				nChecked++;
			}
				
		}
	}

	if (nChecked == 0) {
		$('bFilterHomeAirport'+iAirport).checked = true;
	} else {
		getListFilter(1);
	}
};

function toggleDepartureAirport(iAirport)
{
	var nChecked = 0;
	currentPage = 1;
	if($('nNumberOfDepartureAirports')) 
	{
		for(i=0;i < $('nNumberOfDepartureAirports').value;i++)
		{
			if ($('bFilterOutAirport'+i).checked)
			{
				nChecked++;
			}
				
		}
	}

	if (nChecked == 0) {
		$('bFilterOutAirport'+iAirport).checked = true;
	} else {
		getListFilter(1);
	}
};


function getTimeTable(iDMSearchResultId,sType) 
{
	/* Load only when need needed */
	if ($(sType+'_sub_'+iDMSearchResultId).innerHTML == '') 
	{
		new Ajax.Request(sAjaxUrl,
		{
			parameters: 
			{ 
				action: 'TimeTable', 
				iDMSearchResultId: iDMSearchResultId, 
				sType: sType, nSupplierId: nSupplierID 	
			} ,
			method:'post',
			onSuccess: function(transport)
			{
			    var response = transport.responseText;
			    $(sType+'_sub_'+iDMSearchResultId).update(response);
			    $(sType+'_sub_'+iDMSearchResultId).toggle();
			    /* Reload Iframe - if needed */
			    if (bHasIframe == true)
			    {
			  		resizeIframe();
				}
			}
		});
	/* Just hide/show */
	} else {
		$(sType+'_sub_'+iDMSearchResultId).toggle();
	}
};

function getTimeTableType2(iDMSearchResultId) 
{
	

	/* Load only when need needed */
	if ($('details'+'_'+iDMSearchResultId).innerHTML == '') 
	{

		new Ajax.Request(sAjaxUrl,
		{
			parameters: 
			{ 
				action: 'TimeTableType2', 
				iDMSearchResultId: iDMSearchResultId, 
				nSupplierId: nSupplierID 	
			},
			method:'post',
			onSuccess: function(transport)
			{
			    var response = transport.responseText;
				$('details'+'_'+iDMSearchResultId).update(response);
				$('details'+'_'+iDMSearchResultId).toggle();
				/* Reload Iframe - if needed */
			    if (bHasIframe == true)
			    {
			  		resizeIframe();
				}
			}
		});
	/* Just hide/show */
	} else {
		$('details'+'_'+iDMSearchResultId).toggle();
	}
};


function doStartFirstPoll()
{
	startPoll();
	doGetList(1);
};
function startPoll()
{	
	$('searching_text_container').show();
	timeoutID = window.setTimeout(iscompletedOrPoll, nTimeBetweenPolls);
};

function setStopList()
{
	if($('nNumberOfStops')) 
	{
		lUncheckedStops = '';
		
		var nCountElements = 0;
		for(i=0;i < $('nNumberOfStops').value;i++)
		{
			try {
				if( !$('bFilterStop'+i).checked)
				{
					if(nCountElements == 0)
					{
						lUncheckedStops = $('bFilterStop'+i).value;	
					}
					else
					{
						lUncheckedStops = lUncheckedStops+","+ $('bFilterStop'+i).value;
					}		
					nCountElements = nCountElements+1;
				}
			} catch (err) {}		
		}
	}
	else
	{
		lUncheckedStops = '';
	}
};

function setCarrierList()
{
	if($('nNumberOfCarriers')) 
	{
		lUncheckedCarriers = '';
		
		var nCountElements = 0;
		for(i=0;i < $('nNumberOfCarriers').value;i++)
		{
			if( !$('bFilterCarrier'+i).checked)
			{
				if(nCountElements == 0)
				{
					lUncheckedCarriers = $('bFilterCarrier'+i).value;	
				}
				else
				{
					lUncheckedCarriers = lUncheckedCarriers+","+ $('bFilterCarrier'+i).value;
				}		
				nCountElements = nCountElements+1;
			}		
		}
	}
	else
	{
		lUncheckedCarriers = '';
	}
};

function setAirportList()
{
	if($('nNumberOfDepartureAirports')) 
	{
		lUncheckedDepartureAirports = '';
		
		var nCountElements = 0;
		for(i=0;i < $('nNumberOfDepartureAirports').value;i++)
		{
			if ($('bFilterOutAirport' + i)) 
			{
				if (!$('bFilterOutAirport' + i).checked) {
					if (nCountElements == 0) {
						lUncheckedDepartureAirports = $('bFilterOutAirport' + i).value;
					}
					else {
						lUncheckedDepartureAirports = lUncheckedDepartureAirports + "," + $('bFilterOutAirport' + i).value;
					}
					
					nCountElements = nCountElements + 1;
				}
			}
		}
	}
	else
	{
		lUncheckedDepartureAirports = '';
	}
	
	if($('nNumberOfReturnAirports')) 
	{
		lUncheckedReturnAirports = '';
		
		var nCountElements = 0;
		for(i=0;i < $('nNumberOfReturnAirports').value;i++)
		{
			if($('bFilterHomeAirport'+i))
			{
				if( !$('bFilterHomeAirport'+i).checked)
				{
					if(nCountElements == 0)
					{
						lUncheckedReturnAirports = $('bFilterHomeAirport'+i).value;	
					}
					else
					{
						lUncheckedReturnAirports = lUncheckedReturnAirports+","+ $('bFilterHomeAirport'+i).value;
					}
					nCountElements = nCountElements+1;
				}
			}
		}
	}
	else
	{
		lUncheckedReturnAirports = '';
	}
};

function sliderEndDepartureTime()
{
	bDepartureTimeSliderChanged = true;
	$('loading_layer').show();

	if (bFlightTracking == true) {
		doGetList(3);
		trackFlightBehaviour(253,'Out time filter changed');
	}
};


function sliderEndReturnTime()
{
	bReturnTimeSliderChanged = true;
	$('loading_layer').show();
	if (bFlightTracking == true) {
		doGetList(3);
		trackFlightBehaviour(254,'Home time filter changed');
	}
};

function sliderEndTravelTimeOut()
{
	bTravelTimeSliderChanged = true;
	$('loading_layer').show();
	doGetList(3);
};

function sliderEndTravelTimeHome()
{
	bTravelTimeSliderChanged = true;
	$('loading_layer').show();
	doGetList(3);
};
	
function sliderChangeDepartureTime()
{
	$('departureTimeFromLabelText').update($('departureTimeFromLabel').value);
	$('departureTimeToLabelText').update($('departureTimeToLabel').value);
	
	sDepartureTimeFrom = $('departureTimeFrom').value;
	sDepartureTimeTo = $('departureTimeTo').value;
};	

function sliderChangeReturnTime()
{
	$('returnTimeFromLabelText').update($('returnTimeFromLabel').value);
	$('returnTimeToLabelText').update($('returnTimeToLabel').value);
	
	sReturnTimeFrom = $('returnTimeFrom').value;
	sReturnTimeTo = $('returnTimeTo').value;
};	

function sliderChangeTravelTimeOut()
{
	$('travelTimeFromOutLabelText').update($('travelTimeFromOutLabel').value);
	$('travelTimeToOutLabelText').update($('travelTimeToOutLabel').value);
	nTravelTimeFromOut = $('travelTimeFromOut').value;
	nTravelTimeToOut = $('travelTimeToOut').value;
};

function sliderChangeTravelTimeHome()
{
	$('travelTimeFromHomeLabelText').update($('travelTimeFromHomeLabel').value);
	$('travelTimeToHomeLabelText').update($('travelTimeToHomeLabel').value);
	nTravelTimeFromHome = $('travelTimeFromHome').value;
	nTravelTimeToHome = $('travelTimeToHome').value;
};

function iscompletedOrPoll()
{
	
	try 
	{
		if (!searchCompleted) 
		{
			$('searching_text_container').show();
		}
		
		
		
			new Ajax.Request(sAjaxUrl, {
				method: 'post',
				parameters: {
					action: "isSearchCompleted",
					nSearchID: nSearchID,
					nSupplierID: nSupplierID,
					uniqueValue: makeUniqueValue()
				},
				onSuccess: function(transport){
					// Success
					
					var response = transport.responseText || "no response text";
					var splitArray = response.split("[TM_SPLIT]", 3);
					/* Stop poll */
					if (parseInt(splitArray[0], 10) == 1) {
						searchCompleted = 1;
						nRecordCount = splitArray[1];
						nFlightRecordCount = splitArray[2];
						stopPoll();
					}
					/* Do poll */
					else {
						if (parseInt(splitArray[1], 10) > nRecordCount) {
							nTimeBetweenPolls = parseInt(nTimeBetweenPolls * 1.2, 10);
							nRecordCount = splitArray[1];
							nFlightRecordCount = splitArray[2];
							poll();
						}
						else {
							nTimeBetweenPolls = parseInt(nTimeBetweenPolls * 1.2, 10);
							timeoutID = window.setTimeout(iscompletedOrPoll, nTimeBetweenPolls);
						}
					}
					
					if (parseInt(nFlightRecordCount, 10) > 0) {
						$('filters').show();
						
						if (!bTimeSliderWasDrawn && sTimeFilter != '') 
						{
							$('timeFilter').update(sTimeFilter);
							bTimeSliderWasDrawn = true;
						}
					}
					
					if (searchCompleted) {
						showHideSearchingText();
					}
					
				}
			});
		}
	 
	catch(err) 
	{
		
	}

};
		

function updateFlightCountTexts()
{
	var newFlightCountText = $('flight_dynamic_header_text').value;
	
	newFlightCountText = newFlightCountText.replace("[DEPARTURES]",nFilteredFlightRecordCount);
	newFlightCountText = newFlightCountText.replace("[PRICES]",nFilteredRecordCount);

	$('header_right').update(newFlightCountText);
};


function setSliderValues()
{
	bFlightTracking = false;
	try 
	{
		if (!bDepartureTimeSliderChanged && bTimeSliderWasDrawn) 
		{
			/*if ($('nDepartureTimeFromIndex')) 
			{
				YAHOO.flight_slider1.slider.setMinValue(parseInt($('nDepartureTimeFromIndex').value, 10), true, true, true);
				$('departureTimeFromLabelText').update($('sFormatedDepartureTimeFrom').value);
			}
			if ($('nDepartureTimeToIndex')) 
			{
				YAHOO.flight_slider1.slider.setMaxValue(parseInt($('nDepartureTimeToIndex').value, 10), true, true, true);
				$('departureTimeToLabelText').update($('sFormatedDepartureTimeTo').value);
			}*/
			if($('nDepartureTimeFromIndex') && $('nDepartureTimeToIndex'))
			{
				YAHOO.flight_slider1.slider.setValues(parseInt($('nDepartureTimeFromIndex').value, 10),parseInt($('nDepartureTimeToIndex').value, 10), true, true, true);
			}
		}
		
		if (!bReturnTimeSliderChanged && bTimeSliderWasDrawn ) 
		{
			if($('nReturnTimeFromIndex') && $('nReturnTimeToIndex'))
			{
				YAHOO.flight_slider2.slider.setValues(parseInt($('nReturnTimeFromIndex').value, 10),parseInt($('nReturnTimeToIndex').value, 10), true, true, true);
				
			}
			
			/*if ($('nReturnTimeFromIndex')) 
			{
				YAHOO.flight_slider2.slider.setMinValue(parseInt($('nReturnTimeFromIndex').value, 10), true, true, true);
				$('returnTimeFromLabelText').update($('sFormatedReturnTimeFrom').value);
			}
			if ($('nReturnTimeToIndex')) 
			{
				YAHOO.flight_slider2.slider.setMaxValue(parseInt($('nReturnTimeToIndex').value, 10), true, true, true);
				$('returnTimeToLabelText').update($('sFormatedReturnTimeTo').value);
			}*/
		}
		bFlightTracking = true;
		$('loading_layer').hide();
	} 
	catch (err) 
	{
		bFlightTracking = true;
	
		
		/*alert(err);
		 * 
		 */
		
		$('loading_layer').hide();
		
		
	}
};
	
function showHideSearchingText()
{
	if (parseInt(nRecordCount,10) > 0)
	{
		$('searching_text_container').hide();
//		$('searching_text_container_completed').show();
		try {
		if ($('top_elements')) 
		{
			$('top_elements').show();
		}
		} catch(err) {}
	} 
	else 
	{
		$('searching_text_container').hide();
		$('searching_text_container_none_found').show();
		saveSearchStatus();
	}
};
	
function stopPoll()
{
	bIsLastPoll = true;
	searchCompleted = 1;
	doSearch = false;
	getList(1);
	updatePriceCalendarPrices();
};
	
function getListPoll()
{
	doGetList(2);
};
	
function doGetList(type)
{
	
	setCarrierList();
	setAirportList();
	setStopList();
	if(type == 3)
	{
		currentPage  = 1;
	}
	
	if (!bGettingList || searchCompleted || type == 3) 
	{
		new Ajax.Request(sAjaxUrl, {
			method: 'post',
			parameters: {
				action: "getList",
				nPage: currentPage,
				nSupplierID: nSupplierID,
				nSearchID: nSearchID,
				nTDIID: nTDIID,
				nSort: nSort,
				sDepartureTimeFrom: sDepartureTimeFrom,
				sDepartureTimeTo: sDepartureTimeTo,
				sReturnTimeFrom: sReturnTimeFrom,
				sReturnTimeTo: sReturnTimeTo,
				nTravelTimeFromOut: nTravelTimeFromOut,
				nTravelTimeToOut: nTravelTimeToOut,
				nTravelTimeFromHome: nTravelTimeFromHome,
				nTravelTimeToHome: nTravelTimeToHome,
				lUncheckedCarriers: lUncheckedCarriers,
				lUncheckedDepartureAirports: lUncheckedDepartureAirports,
				lUncheckedReturnAirports: lUncheckedReturnAirports,
				lUncheckedStops: lUncheckedStops,
				bCalculateFees: bShowFeeGuideInfoBox,
				nFeeGuideInfoBoxLuggage: nFeeGuideInfoBoxLuggage,
				nFeeGuideInfoBoxSeating: nFeeGuideInfoBoxSeating
			},
			onLoading: function(transport){
				bGettingList = 1;
			},
			onComplete: function(transport){
				bGettingList = 0;
				
			},
			onSuccess: function(transport){
				bGettingList = 0;
				// Success
				
				var response = transport.responseText || "no response text";
				if (response != 'no response text') 
				{
					var splitArray = response.split("[TM_SPLIT]", 9);
					
					nFilteredFlightRecordCount = splitArray[6];
					
					if (parseInt(nFilteredFlightRecordCount, 10) == 0) 
					{
						nFilteredRecordCount = 0;
					}
					else 
					{
						nFilteredRecordCount = splitArray[7];
					}
					$('list_container').update(splitArray[0]);
					sIndexTop = splitArray[4];
					
					$('index_top_container').update(splitArray[4]);
					sTravelTimeFilter = splitArray[3];
					
					$('index_bottom_container').update(splitArray[8]);
					
				}
				if (bIsFirstPoll) 
				{
					bIsFirstPoll = false;
					sTimeFilter = splitArray[2];
					if (bShowTSEAdsInResult) 
					{
						showTSEAds();
					}
					if (bHasCrossSellingBannerInResult == true) {
						doUpdateCrossSellingBanner();
					}
					
				}
				if (type == 1) {
					$('loading_layer').hide();
				}
				if (type != 3 && type != 4) {
					var splitArrayFilter = splitArray[1].split("[TM_FILTER_SPLIT]");
					if ($('stopsFilter')) {
						$('stopsFilter').update(splitArrayFilter[3]);
					}
					if ($('airportFilter')) {
						$('airportFilter').update(splitArrayFilter[5]);
					}
					if ($('carrierFilter')) {
						$('carrierFilter').update(splitArrayFilter[1]);
					}
				}
				if (type != 4) {
					updateFlightCountTexts();
				}
				if (type == 5) {
					$("fee_guide_loading_layer").hide();
				}
				$('loading_layer').hide();
				
				if (bIsLastPoll && !bTravelTimeSliderWasDrawn) 
				{
					writeDepartureReturnTimeFilters();
					$('travelTimeFilter').update(sTravelTimeFilter);
					bTravelTimeSliderWasDrawn = 1;
				}
				if (bIsLastPoll) 
				{
					$("header_live_search").show();
					$('index_top_container').show();
					if (parseInt(nRecordCount, 10) > 0) 
					{
						$('index_bottom_container').show();
						if (bHasBannerInResult == true) 
						{
							doUpdateBannerTags();
						}
					}
					if (bDepartureTimeSliderChanged == false || bReturnTimeSliderChanged == false) 
					{
						setSliderValues();
					}
					if (bShowTextAdsInResult) 
					{
						showTextAds();
					}
				}
				if (bShowTSEAdsInResult) {
					if (sTSEAdsHTML != "") {
						$("tse_ads_1").update(sTSEAdsHTML);
					}
				}
				/*if (bShowTextAdsInResult) {
					if (sTextAdsHTML != "") {
						$("list_text_ads_1").update(sTextAdsHTML);
					}
				}*/
				if (bHasCrossSellingBannerInResult == true) {
					if (sCrossSellingHTML != '') {
						$("cross_selling_1").update(sCrossSellingHTML);
					}
				}
				if (bHasIframe == true) {
					resizeIframe();
				}
			}
		});
		
	}
	
};
	
function poll()
{	
	var nowTime =  new Date();
	if (doSearch) 
	{
		pollCount++;
		getListPoll();
		if (nowTime >= endTime) 
		{
			stopPoll();
		}
		if (doSearch) 
		{
			startPoll();
		}
	}
};

function getListPaging(page)
{
	$('loading_layer').show();
	$('loading_layer').style.top = getScrollTop() + 50 ;
	$('loading_layer').style.left = 240;

	currentPage = page;
	DataFetcher1 = window.setTimeout("doGetList(4)",0);
};

function getList(page)
{
	if (!bIsLastPoll) 
	{
		$('loading_layer').show();
		$('loading_layer').style.top = getScrollTop() + 50;
		$('loading_layer').style.left = 240;
		currentPage = page;
		DataFetcher1 = window.setTimeout("doGetList(1)",0);
	}
	else
	{
		currentPage = page;
		doGetList(1);
	}

};


function getListFilter(page)
{
	$('loading_layer').show();
	$('loading_layer').style.top = getScrollTop() + 50 ;
	$('loading_layer').style.left = 240;

	currentPage = page;
	DataFetcher1 = window.setTimeout("doGetList(3)",200);
};


function changeSortType() {
	$('loading_layer').show();
	$('loading_layer').style.top = getScrollTop() + 50 ;
	$('loading_layer').style.left = "240px";

	nSort = ($("OrderBy").selectedIndex+1);
	currentPage = 1;
	DataFetcher1 = window.setTimeout("doGetList(4)",0);
};

function getScrollTop()
{
	var ScrollTop = document.body.scrollTop;
	if (ScrollTop == 0)
	{
	    if (window.pageYOffset)
	        ScrollTop = window.pageYOffset;
	    else
	        ScrollTop = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
	}
	return ScrollTop;
};


function setFocus(field) {

	if (field == 'departureName' || field == 'destinationName') 
	{
		pos = -1;
		posDest = -1;
	}
	$(field).value= '';
	$(field).focus();

	if (field == 'departureName') 
	{
		$("departureNameTmp").value= '';
		$("departure").value='';
		$("departureIdTmp").value= '';	
	} 
	else 
	{
		$("destinationNameTmp").value= '';
		$("destination").value='';
		$("destinationIdTmp").value= '';	
	}

};


function validateDepartureInput() 
{
	
	if (document.getElementById("departure_airport_select_container").getElementsByTagName("div")) 
	{		
		if (pos > 0) 
		{
			if (document.getElementById("departure_airport_select_container").getElementsByTagName("div")[pos]) 
			{
				var _tmpId = document.getElementById("departure_airport_select_container").getElementsByTagName("div")[pos].id;
				setdeparture(getCrossBrowserInnerText(document.getElementById("departure_airport_select_container").getElementsByTagName("div")[pos]),_tmpId.substring(4,_tmpId.length));
				setTmpdeparture(getCrossBrowserInnerText(document.getElementById("departure_airport_select_container").getElementsByTagName("div")[pos]),_tmpId.substring(4,_tmpId.length));
			}
		} 
		else 
		{
			if (document.getElementById("departure_airport_select_container").getElementsByTagName("div")[0]) 
			{
				var _tmpId = document.getElementById("departure_airport_select_container").getElementsByTagName("div")[0].id;
				setdeparture(getCrossBrowserInnerText(document.getElementById("departure_airport_select_container").getElementsByTagName("div")[0]),_tmpId.substring(4,_tmpId.length));
				setTmpdeparture(getCrossBrowserInnerText(document.getElementById("departure_airport_select_container").getElementsByTagName("div")[0]),_tmpId.substring(4,_tmpId.length));
			}
		}
	}

};

function validateDestinationInput() 
{
	
	if (document.getElementById("destination_airport_select_container").getElementsByTagName("div")) 
	{
		if (posDest > 0) 
		{
			if (document.getElementById("destination_airport_select_container").getElementsByTagName("div")[posDest]) 
			{
				var _tmpId = document.getElementById("destination_airport_select_container").getElementsByTagName("div")[posDest].id;
				setdestination(getCrossBrowserInnerText(document.getElementById("destination_airport_select_container").getElementsByTagName("div")[posDest]),_tmpId.substring(4,_tmpId.length));
				setTmpdestination(getCrossBrowserInnerText(document.getElementById("destination_airport_select_container").getElementsByTagName("div")[posDest]),_tmpId.substring(4,_tmpId.length));
			}
		} 
		else 
		{
			if (document.getElementById("destination_airport_select_container").getElementsByTagName("div")[0]) 
			{
				var _tmpId = document.getElementById("destination_airport_select_container").getElementsByTagName("div")[0].id;
				setdestination(getCrossBrowserInnerText(document.getElementById("destination_airport_select_container").getElementsByTagName("div")[0]),_tmpId.substring(4,_tmpId.length));
				setTmpdestination(getCrossBrowserInnerText(document.getElementById("destination_airport_select_container").getElementsByTagName("div")[0]),_tmpId.substring(4,_tmpId.length));
			}
		}
	}
};
	
function getCrossBrowserEvent(event) 
{
	if(!event) 
	{
		var myEvent = window.event 
	} 
	else 	
	{
		myEvent = event;
	}
	return myEvent;
};
	
function getDeparture(e) 
{
	if (e.value.length > 2) 
	{
		try 
		{
			clearTimeout(DepartureFetcher);
		} 
		catch(err) 
		{
			
		}
        DepartureFetcher = setTimeout("getDepartureWithDelay('"+ e.value +"')",200);
	}
};

function keyHandler(theEvent) 
{
	var myChild;
	var event = theEvent;
	key = event.keyCode;

	if (document.getElementById("departure_airport_select_container").style.display == 'block') {
		/* Key arrow down */					
		if(document.all) {
			var departurelength = document.getElementById("departure_airport_select_container").childNodes.length;
		} else {
			var departurelength = document.getElementById("departure_airport_select_container").getElementsByTagName("div").length;
		}
		/* key arrow down */
		if (key == 40 && (departurelength > 1 || pos == -1)) {
			if (pos == -1) {
				pos = 0;
				myChild = document.getElementById("departure_airport_select_container").getElementsByTagName("div")[pos];
				myChild.className='choserMouseOver';
				var _tmpId = document.getElementById("departure_airport_select_container").getElementsByTagName("div")[pos].id;
				setdeparturenohide(getCrossBrowserInnerText(document.getElementById("departure_airport_select_container").getElementsByTagName("div")[pos]),_tmpId.substring(4,_tmpId.length));
				setTmpdeparture(getCrossBrowserInnerText(document.getElementById("departure_airport_select_container").getElementsByTagName("div")[pos]),_tmpId.substring(4,_tmpId.length));
				
			} else if (pos == document.getElementById("departure_airport_select_container").getElementsByTagName("div").length-1) 	{	
				myChild = document.getElementById("departure_airport_select_container").getElementsByTagName("div")[pos];
				myChild.className='choserMouseOver';
				var _tmpId = document.getElementById("departure_airport_select_container").getElementsByTagName("div")[pos].id;
				setdeparturenohide(getCrossBrowserInnerText(document.getElementById("departure_airport_select_container").getElementsByTagName("div")[pos]),_tmpId.substring(4,_tmpId.length));
				setTmpdeparture(getCrossBrowserInnerText(document.getElementById("departure_airport_select_container").getElementsByTagName("div")[pos]),_tmpId.substring(4,_tmpId.length));
				
			} else if (pos > -1) {
				myChild = document.getElementById("departure_airport_select_container").getElementsByTagName("div")[pos];
				myChild.className='choserMouseOut';
				pos += 1;
				myChild = document.getElementById("departure_airport_select_container").getElementsByTagName("div")[pos];
				myChild.className='choserMouseOver';

				var _tmpId = document.getElementById("departure_airport_select_container").getElementsByTagName("div")[pos].id;
				setdeparturenohide(getCrossBrowserInnerText(document.getElementById("departure_airport_select_container").getElementsByTagName("div")[pos]),_tmpId.substring(4,_tmpId.length));
				setTmpdeparture(getCrossBrowserInnerText(document.getElementById("departure_airport_select_container").getElementsByTagName("div")[pos]),_tmpId.substring(4,_tmpId.length));
			}
		}		
		/* Key arrow up */
		else if (key == 38 && departurelength > 1) {
			if (pos == 0) {
				pos = 0;
				myChild = document.getElementById("departure_airport_select_container").getElementsByTagName("div")[pos];
				myChild.className='choserMouseOver';
				var _tmpId = document.getElementById("departure_airport_select_container").getElementsByTagName("div")[pos].id;
				setdeparturenohide(getCrossBrowserInnerText(document.getElementById("departure_airport_select_container").getElementsByTagName("div")[pos]),_tmpId.substring(4,_tmpId.length));
				setTmpdeparture(getCrossBrowserInnerText(document.getElementById("departure_airport_select_container").getElementsByTagName("div")[pos]),_tmpId.substring(4,_tmpId.length));
			} else if (pos > 0) {
				myChild = document.getElementById("departure_airport_select_container").getElementsByTagName("div")[pos];
				myChild.className='choserMouseOut';
				pos = pos -1;
				myChild = document.getElementById("departure_airport_select_container").getElementsByTagName("div")[pos];
				myChild.className='choserMouseOver';
				var _tmpId = document.getElementById("departure_airport_select_container").getElementsByTagName("div")[pos].id;
				setdeparturenohide(getCrossBrowserInnerText(document.getElementById("departure_airport_select_container").getElementsByTagName("div")[pos]),_tmpId.substring(4,_tmpId.length));
				setTmpdeparture(getCrossBrowserInnerText(document.getElementById("departure_airport_select_container").getElementsByTagName("div")[pos]),_tmpId.substring(4,_tmpId.length));
			}
		}
		else if (key == 37 || key == 39) {
		}			
		/* Character return/enter */
		else if (key == 13) {
			if (pos == -1) {
				pos = 0;
			}
			var _tmpId = document.getElementById("departure_airport_select_container").getElementsByTagName("div")[pos].id;
			setdeparture(getCrossBrowserInnerText(document.getElementById("departure_airport_select_container").getElementsByTagName("div")[pos]),_tmpId.substring(4,_tmpId.length));
			setTmpdeparture(getCrossBrowserInnerText(document.getElementById("departure_airport_select_container").getElementsByTagName("div")[pos]),_tmpId.substring(4,_tmpId.length));
		}
	} else if (document.getElementById("destination_airport_select_container").style.display == 'block') {
		/* Key arrow down */					
		if(document.all) {
			var destinationlength = document.getElementById("destination_airport_select_container").childNodes.length;
		} else {
			var destinationlength = document.getElementById("destination_airport_select_container").getElementsByTagName("div").length;
		}
		/* key arrow down */
		if (key == 40 && (destinationlength > 1 || pos == -1)) 	{ 
			if (posDest == -1) {
				posDest = 0;
				myChild = document.getElementById("destination_airport_select_container").getElementsByTagName("div")[posDest];
				myChild.className='choserMouseOver';
				var _tmpId = document.getElementById("destination_airport_select_container").getElementsByTagName("div")[posDest].id;
				setdestinationnohide(getCrossBrowserInnerText(document.getElementById("destination_airport_select_container").getElementsByTagName("div")[posDest]),_tmpId.substring(4,_tmpId.length));
				setTmpdestination(getCrossBrowserInnerText(document.getElementById("destination_airport_select_container").getElementsByTagName("div")[posDest]),_tmpId.substring(4,_tmpId.length));
			} else if (posDest == document.getElementById("destination_airport_select_container").getElementsByTagName("div").length-1) {	
				myChild = document.getElementById("destination_airport_select_container").childNodes[document.getElementById("destination_airport_select_container").childNodes.length-1];
				myChild.className='choserMouseOver';
				var _tmpId = document.getElementById("destination_airport_select_container").getElementsByTagName("div")[posDest].id;
				setdestinationnohide(getCrossBrowserInnerText(document.getElementById("destination_airport_select_container").getElementsByTagName("div")[posDest]),_tmpId.substring(4,_tmpId.length));
				setTmpdestination(getCrossBrowserInnerText(document.getElementById("destination_airport_select_container").getElementsByTagName("div")[posDest]),_tmpId.substring(4,_tmpId.length));
			} else if (posDest > -1) {
				myChild = document.getElementById("destination_airport_select_container").getElementsByTagName("div")[posDest];
				myChild.className='choserMouseOut';
				posDest += 1;
				myChild = document.getElementById("destination_airport_select_container").getElementsByTagName("div")[posDest];
				myChild.className='choserMouseOver';
				var _tmpId = document.getElementById("destination_airport_select_container").getElementsByTagName("div")[posDest].id;
				setdestinationnohide(getCrossBrowserInnerText(document.getElementById("destination_airport_select_container").getElementsByTagName("div")[posDest]),_tmpId.substring(4,_tmpId.length));
				setTmpdestination(getCrossBrowserInnerText(document.getElementById("destination_airport_select_container").getElementsByTagName("div")[posDest]),_tmpId.substring(4,_tmpId.length));
				
			} 
		/* Key arrow up */
		} else if (key == 38 && destinationlength > 1) {
			if (posDest == 0) {
				posDest = 0;
				myChild = document.getElementById("destination_airport_select_container").getElementsByTagName("div")[posDest];
				myChild.className='choserMouseOver';
				var _tmpId = document.getElementById("destination_airport_select_container").getElementsByTagName("div")[posDest].id;
				setdestinationnohide(getCrossBrowserInnerText(document.getElementById("destination_airport_select_container").getElementsByTagName("div")[posDest]),_tmpId.substring(4,_tmpId.length));
				setTmpdestination(getCrossBrowserInnerText(document.getElementById("destination_airport_select_container").getElementsByTagName("div")[posDest]),_tmpId.substring(4,_tmpId.length));
			} else if (posDest > 0) {
				myChild = document.getElementById("destination_airport_select_container").getElementsByTagName("div")[posDest];
				myChild.className='choserMouseOut';
				posDest = posDest -1;
				myChild = document.getElementById("destination_airport_select_container").getElementsByTagName("div")[posDest];
				myChild.className='choserMouseOver';
				var _tmpId = document.getElementById("destination_airport_select_container").getElementsByTagName("div")[posDest].id;
				setdestinationnohide(getCrossBrowserInnerText(document.getElementById("destination_airport_select_container").getElementsByTagName("div")[posDest]),_tmpId.substring(4,_tmpId.length));
				setTmpdestination(getCrossBrowserInnerText(document.getElementById("destination_airport_select_container").getElementsByTagName("div")[posDest]),_tmpId.substring(4,_tmpId.length));
			}
		}
		else if (key == 37 || key == 39) {
		}			
		/* Character return/enter */
		else if (key == 13) {
			if (posDest == -1) {
				posDest = 0;
			}
			var _tmpId = document.getElementById("destination_airport_select_container").getElementsByTagName("div")[posDest].id;
			setdestination(getCrossBrowserInnerText(document.getElementById("destination_airport_select_container").getElementsByTagName("div")[posDest]),_tmpId.substring(4,_tmpId.length));
			setTmpdestination(getCrossBrowserInnerText(document.getElementById("destination_airport_select_container").getElementsByTagName("div")[posDest]),_tmpId.substring(4,_tmpId.length));
		}
	}	
};

function setdestinationnohide(name,id) 
{
	name = name.replace("<b>","");
	name = name.replace("<\/b>","");
	$("destinationName").value = name;
	$("destination").value = id;
};

function setdeparturenohide(name,id) {
	name = name.replace("<b>","");
	name = name.replace("<\/b>","");
	$("departureName").value = name;
	$("departure").value = id;
}

	
function getDepartureWithDelay(sDepartureValue) {
	new Ajax.Request(sAjaxUrl,
	{	
		method:'post',
		parameters: 
		{
				action: "searchAirport", 
				prefix: "departure", 
				destination: sDepartureValue, 
				nSupplierID: nSupplierID, 
				uniqueValue: makeUniqueValue()
		}
		,onSuccess: function(transport)
		{
	   		var response = transport.responseText;
	    	$("departure_airport_select_container").update(response);
		    if (response != "") 
			{
	    		$("departure_airport_select_container").style.display = 'block';
	    	}
		}
	});
}


function setTmpdeparture(completeDestination,airportCode) {
	if ($("departureNameTmp").value!='' && $("departureIdTmp").value!='') {
		$("departureName").value = $("departureNameTmp").value;
		$("departure").value = $("departureIdTmp").value;	
	}
}	


function setTmpdestination(completeDestination,airportCode) {
	if ($("destinationNameTmp").value!='' && $("destinationIdTmp").value!='') {
		$("destinationName").value = $("destinationNameTmp").value;
		$("destination").value = $("destinationIdTmp").value;	
	}
}


function setdeparture(name,id) 
{
	name = name.replace("<b>","");
	name = name.replace("<\/b>","");
	$("departureName").value = name;
	$("departure").value = id;
	setTimeout("hideDropDownDeparture()",200);
}

function setdestination(name,id) 
{
	name = name.replace("<b>","");
	name = name.replace("<\/b>","");
	$("destinationName").value = name;
	$("destination").value = id;
	setTimeout("hideDropDownDestination()",200);
}

function getCrossBrowserInnerText(obj) 
{
	if(!obj.innerText) 
	{
		var myInnerText = obj.textContent;
	} 
	else 
	{
		var myInnerText = obj.innerText;
	}
	return myInnerText;
}


function hideDropDownDeparture() 
{
	$("departure_airport_select_container").hide();
}

function hideDropDownDestination(obj) 
{
	$("destination_airport_select_container").hide();
}


function getDestination(e) 
{
	if (e.value.length > 2) 
	{
		try 
		{
			clearTimeout(DestinationFetcher);
		} 
		catch(err) 
		{
			
		}
        DestinationFetcher = setTimeout("getDestinationWithDelay('"+ e.value +"')",200);
	}
}
	
function getDestinationWithDelay(sDestinationValue) 
{
	new Ajax.Request(sAjaxUrl,
	{
		method:'post',
		parameters: 
		{
			action: "searchAirport", 
			prefix: "destination", 
			destination: sDestinationValue, 
			nSupplierID: nSupplierID
		}, 
		onSuccess: function(transport)
		{
		    var response = transport.responseText;
		    $("destination_airport_select_container").update(response);
		    if (response != "") 
			{
		    	$("destination_airport_select_container").style.display = 'block';
		    }
		}
	});	
}

function searchFlightFromLiveSearch()
{
	bLiveSearch = true;
	setTimeout("doFlightSearch()",500);
}
	
function searchFlightFromList() 
{
	setTimeout("doFlightSearch()",500);
	if (bSaveCookie == true) {
		SaveCookie();
	}
};	

function searchFlightFromQS() {
	
	setTimeout("doFlightSearch()",500);
	if (bSaveCookie == true) {
		SaveCookie();
	}
};


function setDepartureDate(day,month,year)
{
	$('departure_date').value = year + "-"+ month +"-" + day;
	
}

function setReturnDate(day,month,year)
{
	$('return_date').value = year + "-"+ month +"-" + day;
	
}


function doFlightSearch() 
{
	if($('oneway').checked)
	{
		nRoundtrip = 0;
	}
	else
	{
		nRoundtrip = 1;
	}

	new Ajax.Request(sAjaxUrl,
	{
		method:'post',
		parameters:
		{
			action: 'StartSearch',
			tdiid: nTDIID,
			departure: $('departure').value,
			destination: $('destination').value,
			departuredate: $('departure_date').value,
			returndate: $('return_date').value,
			roundtrip: nRoundtrip,
			nSupplierID: nSupplierID,
			bLiveSearch: bLiveSearch
		},
		onSuccess: function(transport)
		{
			// Success
			var response = transport.responseText;
			response = response.replace(/^\s+|\s+$/g,"");
			
			if (!isNaN(response)) {
				/* Departure is same as destination */
				if (response == 1) {
					$('quick_search_button').disabled = false;
					alert(sTokenDepartureDestinationEqual);
				/* Error in dates */
				} else if (response == 2) {
					$('quick_search_button').disabled = false;
					alert(sTokenErrorDateFields);
				/* Error in input */	
				} else if (response == 3) {
					$('quick_search_button').disabled = false;
					alert(sTokenErrorFields);
				/* Banned route */
				} else if (response == 4) {
					$('quick_search_button').disabled = false;
					alert(sBannedFlightAlert);
				} else {
				/*	if (sUTMTracking != "")
					{
						var sUrl = sFormAction +"?nSearchID="+ response + "&nTDIId=" + nTDIID + sUTMTracking;
						window.open(sUrl+sExtraParameters,'_blank');
					}
					else
					{*/
						var sUrl = sFormAction +"?nSearchID="+ response + "&nTDIId=" + nTDIID;
						window.open(sUrl+sExtraParameters,'_top');
					//}
				}
			}
		}
	});
	bLiveSearch = false;
}
	

function hidePriceInfo()
{
	$('price_info').hide();
	$('price_info_sub_providers').hide();
	$('price_info_best_price').hide();
	$('price_info_sub_providers').update('');
	$('price_info').update('');
	$('price_info_best_price').update('');
}

function showPriceInfo(searchResultID,iCompanyId,iCarrierCompanyId,index,type)
{
	hidePriceInfo();
	if(type == 0)
	{
		$('price_info').style.left = getX($('fee_guide_icon_'+searchResultID))+"px";
		$('price_info').style.top = getY($('fee_guide_icon_'+searchResultID))+"px";
		$('price_info').show();
	}
	else if(type == 1)
	{
		$('price_info_sub_providers').style.left = getX($('fee_guide_less_icon_'+searchResultID+'_'+index))+"px";
		$('price_info_sub_providers').style.top = getY($('fee_guide_less_icon_'+searchResultID+'_'+index))+"px";
		$('price_info_sub_providers').show();
	}
	else if(type == 2)
	{
		$('price_info_sub_providers').style.left = getX($('fee_guide_more_icon_'+searchResultID+'_'+index))+"px";
		$('price_info_sub_providers').style.top = getY($('fee_guide_more_icon_'+searchResultID+'_'+index))+"px";
		$('price_info_sub_providers').show();
	}
	else if(type == 3)
	{
		$('price_info_best_price').style.left = (getX($('fee_guide_best_price_icon_'+searchResultID+'_'+index))-120)+"px";
		$('price_info_best_price').style.top = getY($('fee_guide_best_price_icon_'+searchResultID+'_'+index))+"px";
		$('price_info_best_price').show();
	}
	
	new Ajax.Request(sAjaxUrl,
	{
		method:'post',
		parameters: 
		{
			action: 'FeeGuideForProvider',
			iCompanyId: iCompanyId,
			iCarrierCompanyId: iCarrierCompanyId,
			nSupplierID: nSupplierID,
			bShowFeeGuideInfoBox: bShowFeeGuideInfoBox,
			nDMSearchResultId: searchResultID,
			iRoundtrip: 1,
			nFeeGuideInfoBoxLuggage: nFeeGuideInfoBoxLuggage,
			nFeeGuideInfoBoxSeating: nFeeGuideInfoBoxSeating
		},
		onSuccess: function(transport)
		{
		    var response = transport.responseText;
			if(type == 1 || type == 2)
			{
				$('price_info_sub_providers').update(response);
			}
			else if(type == 3)
			{
				$('price_info_best_price').update(response);
			}
			else
			{
				$('price_info').update(response);
			}
		}
	});
};

function hideAllFeatureBoxes() {
	try {
		if ($("open_send_email_form_container")) {
			$("open_send_email_form_container").hide();
		}
	} catch(err) {
	}
	try {
		if ($("fee_guide_form_container")) {
			$("fee_guide_form_container").hide();
		}
	} catch(err) {
	}
	try {
		if ($("price_agent_form_container")) {
			$("price_agent_form_container").hide();
		}
	} catch(err) {
	}
	try {
		if ($("price_calendar_v2_container")) {
			$("price_calendar_v2_container").hide();
		}
	} catch(err) {
	}
};

function showPriceCalendar()
{
	hideAllFeatureBoxes();
	
	$("price_calendar_v2_container").show();
	
	new Ajax.Request(sAjaxUrl,
	{
		method:'post',
		parameters: 
		{ 
			action: 'PriceCalendar', 
			nSearchId: nSearchID, 
			nSupplierId: nSupplierID,
			sCurrency: sPriceCalendarCurrency 	
		},
		onSuccess: function(transport)
		{
		    var response = transport.responseText;

		    $("price_calendar_v2_container").update(response);
		    $("price_calendar_v2_container").show();
		    /* Reload Iframe - if needed */
		    if (bHasIframe == true)
		    {
				resizeIframe();
			}
		}
	});
};

function showPriceCalendarBox()
{
	var sDepartureDate = nPriceCalendarDepartureYear + "-" + nPriceCalendarDepartureMonth + "-" + nPriceCalendarDepartureDay;
	var sReturnDate = nPriceCalendarReturnYear + "-" + nPriceCalendarReturnMonth + "-" + nPriceCalendarReturnDay;
	
	new Ajax.Request(sAjaxUrl,
	{
		method:'post',
		parameters: 
		{ 
			action: 'PriceCalendarBox', 
			nSearchId: nSearchID, 
			nSupplierId: nSupplierID,
			currency: sPriceCalendarCurrency,
			departure: sPriceCalendarDeparture,
			destination: sPriceCalendarDestination,
			departuredate: sDepartureDate,
			returndate: sReturnDate,
			roundtrip: bPriceCalendarRoundtrip
		},
		onSuccess: function(transport)
		{
		    var response = transport.responseText;
		    $("PriceCalendarPrice").update(response);
		    if (response == "")
		    {
		    	$("PriceCalendarCurrency").update("");
		    }
		    else {
		    	$("PriceCalendarCurrency").update(sPriceCalendarCurrencyText);
		    }
		    
		}
	});
};

function updatePriceCalendarPrices()
{
	new Ajax.Request(sAjaxUrl,
	{
		method:'post',
		parameters: 
		{ 
			action: 'PriceCalendarUpdatePrices', 
			nSearchId: nSearchID, 
			nSupplierId: nSupplierID
		},
		onSuccess: function(transport)
		{
		}
	});
};


function showPriceAgentForm() 
{
	hideAllFeatureBoxes();
	
	new Ajax.Request(sAjaxUrl,
	{
		method:'post',
		parameters: 
		{ 
			action: 'PriceAgentForm', 
			nSearchId: nSearchID, 
			nSupplierId: nSupplierID,
			nocache: "true"
		},
		onSuccess: function(transport)
		{
		    var response = transport.responseText;

		    $("price_agent_form_container").update(response);
		    $("price_agent_form_container").show();
		    /* Reload Iframe - if needed */
		    if (bHasIframe == true)
		    {
		  		resizeIframe();
			}
		}
	});

};
function showPriceAgentFormPostBack() 
{
	var nMaxPriceChoice = 1;
	var nPriceAgentSendingType = 1;
	if ($("price_agent_sending_type_period").checked) {
		nPriceAgentSendingType = 1;
	}
	if ($("price_agent_sending_type_daily").checked) {
		nPriceAgentSendingType = 2;
	}
	/*if ($("price_agent_max_price_choice_cheaper").checked) {
		nMaxPriceChoice = 1;
	}
	if ($("price_agent_max_price_choice_price").checked) {
		nMaxPriceChoice = 2;
	}*/
	if (IsEmail($("price_agent_email").value) == true && IsNumeric($("price_agent_max_price").value) == true) {
		new Ajax.Request(sAjaxUrl,
		{
			method:'post',
			parameters: 
			{ 
				action: 'PriceAgentFormPostBack', 
				nSearchId: nSearchID,
				nSupplierId: nSupplierID,
				sPriceAgentEmail: $("price_agent_email").value,
				nPriceAgentMaxPriceChoice: nMaxPriceChoice,
				nPriceAgentMaxPrice: $("price_agent_max_price").value,
				nPriceAgentSendingType: nPriceAgentSendingType
				 	
			},
			onSuccess: function(transport)
			{
			    var response = transport.responseText;
			    $("price_agent_form_container").update(response);
			    $("price_agent_form_container").show();
			    /* Reload Iframe - if needed */
			    if (bHasIframe == true)
			    {
			  		resizeIframe();
				}
			}
		});
	} else {
		alert(sPriceAgentErrorInEmail);
	}
};

function IsNumeric(sText)
{
	var sChars = "0123456789";
	var IsNumber = true;
	var sChar;
	
	for (i = 0; i < sText.length && IsNumber == true; i++) 
	{ 
		sChar = sText.charAt(i); 
		if (sChars.indexOf(sChar) == -1) 
		{
			IsNumber = false;
		}
	}
	return IsNumber;
};

function showFeeGuideForm() {
	
	hideAllFeatureBoxes();
	
	$("fee_guide_form_container").show();
	
	new Ajax.Request(sAjaxUrl,
	{
		method:'post',
		parameters: 
		{ 
			action: 'FeeGuideForm',
			nSupplierId: nSupplierID
			
		} ,
		onSuccess: function(transport)
		{
		    var response = transport.responseText;
		    $("fee_guide_form_container").update(response);
		    $("fee_guide_form_container").show();
		    $("nFeeGuideLuggagePieces").selectedIndex = nFeeGuideInfoBoxLuggage;
			$("nFeeGuideSeating").selectedIndex = nFeeGuideInfoBoxSeating;
		    /* Reload Iframe - if needed */
		    if (bHasIframe == true)
		    {
		  		resizeIframe();
			}
		}
	});
	
};

function showFeeGuideFormPostBack()
{
	$("fee_guide_loading_layer").show();
	
	bShowFeeGuideInfoBox = true;
	nFeeGuideInfoBoxLuggage = $("nFeeGuideLuggagePieces").value;
	nFeeGuideInfoBoxSeating = $("nFeeGuideSeating").value;

	if (parseInt(nFeeGuideInfoBoxLuggage,10) == 0 && parseInt(nFeeGuideInfoBoxSeating,10) == 0) 
	{
		nFeeGuideInfoBoxLuggage = 0;
		nFeeGuideInfoBoxSeating = 0;
	}
	
	new Ajax.Request(sAjaxUrl,
	{
		method:'post',
		parameters: 
		{ 
			action: 'FeeGuideFormPostBack',
			nSupplierId: nSupplierID
		} ,
		onSuccess: function(transport)
		{
		    var response = transport.responseText;
		    $("fee_guide_form_container").update(response);
		    /* Reload Iframe - if needed */
		    if (bHasIframe == true)
		    {
		  		resizeIframe();
			}
		}
	});
	doGetList(5);
};

function showTellAFriend() {

	hideAllFeatureBoxes();
	$("open_send_email_form_container").show();
	
	new Ajax.Request(sAjaxUrl,
		{
			method:'post',
			parameters: 
			{ 
				action: 'TellAFriend', 
				nSearchID: nSearchID,
				nTDIID: nTDIID,
				nSupplierId: nSupplierID
			} ,
			onSuccess: function(transport)
			{
			    var response = transport.responseText;
			    $("open_send_email_form_container").update(response);
			    /* Reload Iframe - if needed */
			    if (bHasIframe == true)
			    {
			  		resizeIframe();
				}
			}
		});
};


function showTellAFriendPostBack() 
{
	new Ajax.Request(sAjaxUrl,
	{
		method:'post',
		parameters: 
		{ 
			action: 'TellAFriend', 
			nSearchID: nSearchID,
			nSupplierId: nSupplierID,
			nTDIID: nTDIID,
			gogo: 1,
			tip_a_friend_reciever_email: $("tip_a_friend_reciever_email").value,
			tip_a_friend_sender_name: $("tip_a_friend_sender_name").value,
			tip_a_friend_sender_email: $("tip_a_friend_sender_email").value,
			tip_a_friend_comment: $("tip_a_friend_comment").value,
			tip_a_friend_reciever_email: $("tip_a_friend_reciever_email").value
		} ,
		onSuccess: function(transport)
		{
		    var response = transport.responseText;
		    $("open_send_email_form_container").update(response);
		    /* Reload Iframe - if needed */
		    if (bHasIframe == true)
		    {
		  		resizeIframe();
			}
		}
	});
};
	
	
	
function doCalendar2Check(day,month,year)
{
	var dDepartureInt = year + '/' + month + '/' + day;
	var dReturnInt = $('return_date').value;
	dReturnInt = dReturnInt.replace(/-/gi, "/");
	
	var oDepartureDate = new Date(dDepartureInt);
	var oReturnDate = new Date(dReturnInt);
	var diff = oDepartureDate.getTime()- oReturnDate.getTime();	
	
	if (diff < 0) {
	}
	else
	{
		if (day) {
			var myDate = new Date();
			var newDay = parseInt(day,10) ;
			myDate.setFullYear(year, month - 1, day);
			myDate.setDate(myDate.getDate()+7);
			myDateday = myDate.getDate();
			myDatemonth = myDate.getMonth()+1;
			
			YAHOO.travelmarket.return_date_calendar.cal1return_date_calendar.setMonth(myDatemonth-1);
			YAHOO.travelmarket.return_date_calendar.cal1return_date_calendar.setYear(myDate.getFullYear());
			YAHOO.travelmarket.return_date_calendar.cal1return_date_calendar.select(myDate.getDate() + "." + myDatemonth + "." + myDate.getFullYear());
			
			YAHOO.travelmarket.return_date_calendar.cal1return_date_calendar.render();
		}
	}
};

// Get y coordinate
function getY( oElement ) {
	var iReturnValue = 0;
	while( oElement != null ) {
		iReturnValue += oElement.offsetTop;
		oElement = oElement.offsetParent;
	}
	return iReturnValue;
}


// Get x coordinate
function getX( oElement ) {
	var iReturnValue = 0;
	while( oElement != null ) {
		iReturnValue += oElement.offsetLeft;
		oElement = oElement.offsetParent;
	}
	return iReturnValue;
}

function IsEmail(sEmail) {
	var reg = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if(reg.test(sEmail)) {
		return true;
	} else {
		return false;
	}
};

/* Function one destination form input */
function changeDestinationDeparture(theValue, sIATA, sName) {
	
	if (theValue == "departure") {
		
		$('departure').value = sIATA;
		$('departureName').value = sName;
		$('destination').value = "";
		$('destinationName').value = "";
		
		$('destination_form_container').show();
		$('departure_form_container').hide();
		
	} else {
		
		$('destination').value = sIATA;
		$('destinationName').value = sName;
		$('departure').value = "";
		$('departureName').value = "";
		
		$('departure_form_container').show();
		$('destination_form_container').hide();
	}
};

function saveSearchStatus()
{
	new Ajax.Request(sAjaxUrl,
	{
		method:'post',
		parameters: 
		{ 
			action: 'NoResultsStatus',
			nSupplierID: nSupplierID,
			nSearchID: nSearchID,
			sPortalServer: sPortalServer
		}
	});
};

function writeDepartureReturnTimeFilters()
{
	if(parseInt(nFlightRecordCount,10) > 0)
	{
		$('filters').show();
		
		if(!bTimeSliderWasDrawn && sTimeFilter !='')
		{
			$('timeFilter').update(sTimeFilter);	
			bTimeSliderWasDrawn = true;
		}
		
		
		
	}
};

function SaveCookie()
{
	var sDate = new Date();
	sDate.setDate(sDate.getDate() + 14);
	sDate = sDate.toGMTString();
	
	if ($("departureName")) { 
		document.cookie = "cookie_tse_departurename=" + escape($('departureName').value) + ";expires=" + sDate + ";path=/";
		document.cookie = "cookie_tse_departure=" + escape($('departure').value) + ";expires=" + sDate + ";path=/";
	} else {
		document.cookie = "cookie_tse_departurename=;expires=" + sDate + "path=/";
		document.cookie = "cookie_tse_departure=;expires=" + sDate + "path=/";
	}
	if ($("destinationName")) { 
		document.cookie = "cookie_tse_destinationname=" + escape($('destinationName').value) + ";expires=" + sDate + ";path=/";
		document.cookie = "cookie_tse_destination=" + escape($('destination').value) + ";expires=" + sDate + ";path=/";
	} else {
		document.cookie = "cookie_tse_destinationname=;expires=" + sDate + ";path=/";
		document.cookie = "cookie_tse_destination=;expires=" + sDate + ";path=/";
	}
	document.cookie = "cookie_tse_departuredate=" + escape($('departure_date').value) + ";expires=" + sDate + ";path=/";
	document.cookie = "cookie_tse_returndate=" + escape($('return_date').value) + ";expires=" + sDate + ";path=/";
	if ($("return").checked == true) {
		document.cookie = "cookie_tse_roundtrip=" + escape(1) + ";expires=" + sDate + ";path=/";		
	} else {
		document.cookie = "cookie_tse_roundtrip=" + escape(0) + ";expires=" + sDate + ";path=/";
	}
};

function LoadCookie()
{
	var sDepartureDate = "";
	var sNowDate = new Date();
	var sMonth = "";
	var sDay = "";
	if (parseInt(sNowDate.getMonth(),10) < 9)
	{
		sMonth = "0" + (sNowDate.getMonth() + 1);
	}
	else
	{
		sMonth = (sNowDate.getMonth() + 1);
	}
	if (parseInt(sNowDate.getDate(),10) < 10)
	{
		sDay = "0" + sNowDate.getDate();
	}
	else
	{
		sDay = sNowDate.getDate();
	}

	sNowDate = sNowDate.getFullYear() + "" + sMonth + "" + sDay;
	sDepartureDate = getCookie('cookie_tse_departuredate').replace('-','').replace('-','');
	
	if (sDepartureDate > sNowDate)
	{
		try
		{
			if (location.href.split("/").length <= 4) {
				if (getCookie('cookie_tse_destination') != "") {
					$('destination').value = getCookie('cookie_tse_destination');
				}
				if (getCookie('cookie_tse_destinationname') != "") {
					$('destinationName').value = getCookie('cookie_tse_destinationname');
				}
			}
		} catch (err) {}
		try
		{
			if (location.href.split("/").length <= 6) {
				if (getCookie('cookie_tse_departure') != "") {
					$('departure').value = getCookie('cookie_tse_departure');
				}
				if (getCookie('cookie_tse_departurename') != "") { 
					$('departureName').value = getCookie('cookie_tse_departurename');
				}
			}	
		} catch (err) {}
		try
		{
			if (getCookie('cookie_tse_roundtrip') != "") {
				if (getCookie('cookie_tse_roundtrip') == "1" ) {
					$("return").click();
				} else {
					$("oneway").click();
				}
			}
		} catch (err) {}
		try
		{
			if (getCookie('cookie_tse_departuredate') != "" ) {
				var nYear = getCookie('cookie_tse_departuredate').substring(0,4);
				var nMonth = getCookie('cookie_tse_departuredate').substring(5,7);
				var nDay = getCookie('cookie_tse_departuredate').substring(8,10);
				var nMonthFormated = parseInt(getCookie('cookie_tse_departuredate').substring(5,7),10);
				nMonthFormated = parseInt(nMonthFormated,10);
				nMonthFormated = nMonthFormated - 1;
				 
				YAHOO.travelmarket.departure_date_calendar.cal1departure_date_calendar.setMonth(nMonthFormated);
				YAHOO.travelmarket.departure_date_calendar.cal1departure_date_calendar.setYear(nYear);
				YAHOO.travelmarket.departure_date_calendar.cal1departure_date_calendar.select(nDay + "." + nMonth + "." + nYear);
				YAHOO.travelmarket.departure_date_calendar.cal1departure_date_calendar.render();
				$("departure_date").value = getCookie('cookie_tse_departuredate');
			}
		} catch (err) { }
		try
		{
			if (getCookie('cookie_tse_returndate') != "" ) {
				var nYear = getCookie('cookie_tse_returndate').substring(0,4);
				var nMonth = getCookie('cookie_tse_returndate').substring(5,7);
				var nDay = getCookie('cookie_tse_returndate').substring(8,10);
				var nMonthFormated = parseInt(getCookie('cookie_tse_returndate').substring(5,7),10);
				nMonthFormated = parseInt(nMonthFormated,10);
				nMonthFormated = nMonthFormated - 1;
				
				YAHOO.travelmarket.return_date_calendar.cal1return_date_calendar.setMonth(nMonthFormated);
				YAHOO.travelmarket.return_date_calendar.cal1return_date_calendar.setYear(nYear);
				YAHOO.travelmarket.return_date_calendar.cal1return_date_calendar.select(nDay + "." + nMonth + "." + nYear);
				YAHOO.travelmarket.return_date_calendar.cal1return_date_calendar.render();
				$("return_date").value = getCookie('cookie_tse_returndate');
			}
		} catch (err) { }
	}
};

function getCookie(c_name) {
	if (document.cookie.length>0) {
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1) { 
		    c_start=c_start + c_name.length+1; 
		    c_end=document.cookie.indexOf(";",c_start);
		    if (c_end==-1) c_end=document.cookie.length;
		    	return unescape(document.cookie.substring(c_start,c_end));
		}
	}
	return "";
};

function doUpdateBanner728x60() {
	if ($("tse_banner_placement_728x60")) {
		$("tse_banner_placement_728x60").innerHTML = tse_banner_string_728x60;
		$("tse_banner_placement_728x60").className = "tse_banner_container_shown_728x60";
	}
};

function doUpdateCrossSellingBanner()
{
	new Ajax.Request(sAjaxUrl,
	{
		method:'post',
		parameters: 
		{ 
			action: 'CrossSelling', 
			nSearchID: nSearchID,
			nSupplierId: nSupplierID
		} ,
		onSuccess: function(transport)
		{
		    var response = transport.responseText;
		    sCrossSellingHTML = response;
		    $("cross_selling_1").update(sCrossSellingHTML);
		}
	});
};

function doUpdateBannerTags() {
	try {
		if ($("tse_banner_placement_1")) {
			$("tse_banner_placement_1").innerHTML = tse_banner_string_first;
			$("tse_banner_placement_1").className = "tse_banner_container_shown";
		}
	} catch (err)
	{
	}
	try {
		if ($("tse_banner_placement_2")) {
			$("tse_banner_placement_2").innerHTML = tse_banner_string;
			$("tse_banner_placement_2").className = "tse_banner_container_shown";
		}
	} catch (err)
	{
	}
	try {
		if ($("tse_banner_placement_3")) {
			$("tse_banner_placement_3").innerHTML = tse_banner_string;
			$("tse_banner_placement_3").className = "tse_banner_container_shown";
		}
	} catch (err)
	{
	}
	try {
		if ($("tse_banner_placement_4")) {
			$("tse_banner_placement_4").innerHTML = tse_banner_string;
			$("tse_banner_placement_4").className = "tse_banner_container_shown";
		}
	} catch (err)
	{
	}
};

function price_out_event(chart_id)
{

	try {
		nPriceCalendarDepartureDay = chart_id + 1;
		if (parseInt(nPriceCalendarDepartureDay,10) < 10) {
			if (parseInt(nPriceCalendarDepartureDay,10) <= 0) { nPriceCalendarDepartureDay = 1; }
			nPriceCalendarDepartureDay = "0" + parseInt(nPriceCalendarDepartureDay,10);
		}
		if (parseInt(nPriceCalendarDepartureMonth,10) < 10) {
			if (parseInt(nPriceCalendarDepartureMonth,10) <= 0) { nPriceCalendarDepartureMonth = 1; }
			nPriceCalendarDepartureMonth = "0" + parseInt(nPriceCalendarDepartureMonth,10);
		}
		if (parseInt(nPriceCalendarReturnDay,10) < 10) {
			if (parseInt(nPriceCalendarReturnDay,10) <= 0) { nPriceCalendarReturnDay = 1; }
			nReturnDay = "0" + parseInt(nPriceCalendarReturnDay,10);
		}
		if (parseInt(nPriceCalendarReturnMonth,10) < 10) {
			if (parseInt(nPriceCalendarReturnMonth,10) <= 0) { nPriceCalendarReturnMonth = 1; }
			nPriceCalendarReturnMonth = "0" + parseInt(nPriceCalendarReturnMonth,10);
		}
		oJSONDataOut = "%2Ftmcomponents%2Fmodules%2FTM_Flight_TSE%2Fpublic%2Ftse%5Fprice%5Fcalendar%5Fv2%5Fdata%5Fout%2Ecfm";
		oJSONDataOut = oJSONDataOut + "%3Fdeparturedate%3D" + nPriceCalendarDepartureYear + "%2D" + nPriceCalendarDepartureMonth + "%2D" + nPriceCalendarDepartureDay;
		oJSONDataOut = oJSONDataOut + "%26returndate%3D" + nPriceCalendarReturnYear + "%2D" + nPriceCalendarReturnMonth + "%2D" + nPriceCalendarReturnDay;
		oJSONDataOut = oJSONDataOut + "%26roundtrip%3D" + bPriceCalendarRoundtrip;
		oJSONDataOut = oJSONDataOut + "%26departure%3D" + sPriceCalendarDeparture;
		oJSONDataOut = oJSONDataOut + "%26destination%3D" + sPriceCalendarDestination;
		oJSONDataOut = oJSONDataOut + "%26currency%3D" + sPriceCalendarCurrency;
		oJSONDataOut = oJSONDataOut + "%26supplierid%3D" + nSupplierID;
		get_ofc_graph("out_chart", 720, 160,  oJSONDataOut, "");
		
		oJSONDataHome = "%2Ftmcomponents%2Fmodules%2FTM_Flight_TSE%2Fpublic%2Ftse%5Fprice%5Fcalendar%5Fv2%5Fdata%5Fhome%2Ecfm";
		oJSONDataHome = oJSONDataHome + "%3Fdeparturedate%3D" + nPriceCalendarDepartureYear + "%2D" + nPriceCalendarDepartureMonth + "%2D" + nPriceCalendarDepartureDay;
		oJSONDataHome = oJSONDataHome + "%26returndate%3D" + nPriceCalendarReturnYear + "%2D" + nPriceCalendarReturnMonth + "%2D" + nPriceCalendarReturnDay;
		oJSONDataHome = oJSONDataHome + "%26roundtrip%3D" + bPriceCalendarRoundtrip;
		oJSONDataHome = oJSONDataHome + "%26departure%3D" + sPriceCalendarDeparture;
		oJSONDataHome = oJSONDataHome + "%26destination%3D" + sPriceCalendarDestination;
		oJSONDataHome = oJSONDataHome + "%26currency%3D" + sPriceCalendarCurrency;
		oJSONDataHome = oJSONDataHome + "%26supplierid%3D" + nSupplierID;
		get_ofc_graph("home_chart", 720, 160,  oJSONDataHome, "");
		
		DoUpdatePriceCalendarBox();
	} 
	catch (err)
	{
	}
};

function price_home_event(chart_id)
{
	try {
		nPriceCalendarReturnDay = chart_id + 1; 
		if (parseInt(nPriceCalendarDepartureDay,10) < 10) {
			if (parseInt(nPriceCalendarDepartureDay,10) <= 0) { nPriceCalendarDepartureDay = 1; }
			nPriceCalendarDepartureDay = "0" + parseInt(nPriceCalendarDepartureDay,10);
		}
		if (parseInt(nPriceCalendarDepartureMonth,10) < 10) {
			if (parseInt(nPriceCalendarDepartureMonth,10) <= 0) { nPriceCalendarDepartureMonth = 1; }
			nPriceCalendarDepartureMonth = "0" + parseInt(nPriceCalendarDepartureMonth,10);
		}
		if (parseInt(nPriceCalendarReturnDay,10) < 10) {
			if (parseInt(nPriceCalendarReturnDay,10) <= 0) { nPriceCalendarReturnDay = 1; }
			nPriceCalendarReturnDay = "0" + parseInt(nPriceCalendarReturnDay,10);
		}
		if (parseInt(nPriceCalendarReturnMonth,10) < 10) {
			if (parseInt(nPriceCalendarReturnMonth,10) <= 0) { nPriceCalendarReturnMonth = 1; }
			nPriceCalendarReturnMonth = "0" + parseInt(nPriceCalendarReturnMonth,10);
		}
		
		oJSONDataOut = "%2Ftmcomponents%2Fmodules%2FTM%5FFlight%5FTSE%2Fpublic%2Ftse%5Fprice%5Fcalendar%5Fv2%5Fdata%5Fout%2Ecfm";
		oJSONDataOut = oJSONDataOut + "%3Fdeparturedate%3D" + nPriceCalendarDepartureYear + "%2D" + nPriceCalendarDepartureMonth + "%2D" + nPriceCalendarDepartureDay;
		oJSONDataOut = oJSONDataOut + "%26returndate%3D" + nPriceCalendarReturnYear + "%2D" + nPriceCalendarReturnMonth + "%2D" + nPriceCalendarReturnDay;
		oJSONDataOut = oJSONDataOut + "%26roundtrip%3D" + bPriceCalendarRoundtrip;
		oJSONDataOut = oJSONDataOut + "%26departure%3D" + sPriceCalendarDeparture;
		oJSONDataOut = oJSONDataOut + "%26destination%3D" + sPriceCalendarDestination;
		oJSONDataOut = oJSONDataOut + "%26currency%3D" + sPriceCalendarCurrency;
		oJSONDataOut = oJSONDataOut + "%26supplierid%3D" + nSupplierID;
		get_ofc_graph("out_chart", 720, 160,  oJSONDataOut, "");
		
		oJSONDataHome = "%2Ftmcomponents%2Fmodules%2FTM%5FFlight%5FTSE%2Fpublic%2Ftse%5Fprice%5Fcalendar%5Fv2%5Fdata%5Fhome%2Ecfm";
		oJSONDataHome = oJSONDataHome + "%3Fdeparturedate%3D" + nPriceCalendarDepartureYear + "%2D" + nPriceCalendarDepartureMonth + "%2D" + nPriceCalendarDepartureDay;
		oJSONDataHome = oJSONDataHome + "%26returndate%3D" + nPriceCalendarReturnYear + "%2D" + nPriceCalendarReturnMonth + "%2D" + nPriceCalendarReturnDay;
		oJSONDataHome = oJSONDataHome + "%26roundtrip%3D" + bPriceCalendarRoundtrip;
		oJSONDataHome = oJSONDataHome + "%26departure%3D" + sPriceCalendarDeparture;
		oJSONDataHome = oJSONDataHome + "%26destination%3D" + sPriceCalendarDestination;
		oJSONDataHome = oJSONDataHome + "%26currency%3D" + sPriceCalendarCurrency;
		oJSONDataHome = oJSONDataHome + "%26supplierid%3D" + nSupplierID;
		get_ofc_graph("home_chart", 720, 160,  oJSONDataHome, "");
		
		DoUpdatePriceCalendarBox();
	} 
	catch(err)
	{
	}
};

/* render new graph */
function get_ofc_graph(oElement, nWidth, nHeight, oJSONData, sLoadingText)
{
	swfobject.embedSWF("/tmcomponents/modules/TM_Flight_TSE/public/open-flash-chart-mod.swf", oElement, nWidth, nHeight, "9.0.0", "/tse/expressInstall.swf", { "data-file": oJSONData, "loading": ""}, {"wmode" : "transparent"});
};

function DoUpdatePriceCalendarBox()
{
	var sTempnPriceCalendarDepartureDay = "";
	var sTempnPriceCalendarDepartureMonth = "";
	var sTempnPriceCalendarReturnDay = "";
	var sTempnPriceCalendarReturnMonth = "";
	
	if (parseInt(nPriceCalendarDepartureDay,10) < 10) {
		sTempnPriceCalendarDepartureDay = "0" + parseInt(nPriceCalendarDepartureDay,10);
	} else {
		sTempnPriceCalendarDepartureDay = nPriceCalendarDepartureDay;
	}
	if (parseInt(nPriceCalendarDepartureMonth,10) < 10) {
		sTempnPriceCalendarDepartureMonth = "0" + parseInt(nPriceCalendarDepartureMonth,10);
	} else {
		sTempnPriceCalendarDepartureMonth = nPriceCalendarDepartureMonth;
	}
	if (parseInt(nPriceCalendarReturnDay,10) < 10) {
		sTempnPriceCalendarReturnDay = "0" + parseInt(nPriceCalendarReturnDay,10);
	} else {
		sTempnPriceCalendarReturnDay = nPriceCalendarReturnDay;
	}
	if (parseInt(nPriceCalendarReturnMonth,10) < 10) {
		sTempnPriceCalendarReturnMonth = "0" + parseInt(nPriceCalendarReturnMonth,10);
	} else {
		sTempnPriceCalendarReturnMonth = nPriceCalendarReturnMonth;
	}
	
	$("PriceCalendarDepartureDate").update(sTempnPriceCalendarDepartureDay + "-" + sTempnPriceCalendarDepartureMonth + "-" + nPriceCalendarDepartureYear);
	if ($("PriceCalendarReturnDate"))
	{
		$("PriceCalendarReturnDate").update(sTempnPriceCalendarReturnDay + "-" + sTempnPriceCalendarReturnMonth + "-" + nPriceCalendarReturnYear);
	}
	showPriceCalendarBox();
};
function DoUpdatePriceCalendarLink()
{
	var nTmpPriceCalendarDepartureMonth = "";
	var nTmpPriceCalendarDepartureDay = "";
	var nTmpPriceCalendarReturnMonth = "";
	var nTmpPriceCalendarReturnDay = "";

	if (parseInt(nPriceCalendarDepartureDay,10) <10) {
		nTmpPriceCalendarDepartureDay = "0" + parseInt(nPriceCalendarDepartureDay,10);
	} else {
		nTmpPriceCalendarDepartureDay = nPriceCalendarDepartureDay;
	}
	if (parseInt(nPriceCalendarDepartureMonth,10) <10) {
		nTmpPriceCalendarDepartureMonth = "0" + parseInt(nPriceCalendarDepartureMonth,10);
	} else {
		nTmpPriceCalendarDepartureMonth = nPriceCalendarDepartureMonth;
	}
	if (parseInt(nPriceCalendarReturnDay,10) <10) {
		nTmpPriceCalendarReturnDay = "0" + parseInt(nPriceCalendarReturnDay,10);
	} else {
		nTmpPriceCalendarReturnDay = nPriceCalendarReturnDay;
	}
	if (parseInt(nPriceCalendarReturnMonth,10) <10) {
		nTmpPriceCalendarReturnMonth = "0" + parseInt(nPriceCalendarReturnMonth,10);
	} else {
		nTmpPriceCalendarReturnMonth = nPriceCalendarReturnMonth;
	}
	$("price_calendar_link").href = "/flight_v3_list.cfm?choice=searchResult&departureDate=" + nPriceCalendarDepartureYear + "-" + nTmpPriceCalendarDepartureMonth + "-" + nTmpPriceCalendarDepartureDay + "&returnDate=" + nPriceCalendarReturnYear + "-" + nTmpPriceCalendarReturnMonth + "-" + nTmpPriceCalendarReturnDay + "&destination=" + sPriceCalendarDestination + "&departure=" + sPriceCalendarDeparture + "&currency=" + sPriceCalendarCurrency + "&roundtrip=" + bPriceCalendarRoundtrip + "&nTDIID=" + nTDIID;
};
function DoChangeCalendarMonth(oChanged,nValue)
{
	if (oChanged == 'out') {
		var bChangeHomeCal = false;
		if (nValue == 'prev') {
			nPriceCalendarDepartureMonth = nPriceCalendarDepartureMonth - 1;
			if (nPriceCalendarDepartureMonth < 1) {
				nPriceCalendarDepartureMonth = 12;
				nPriceCalendarDepartureYear = parseInt(nPriceCalendarDepartureYear,10) - 1;
			}
		} else {
			nPriceCalendarDepartureMonth = parseInt(nPriceCalendarDepartureMonth,10) + 1;
			if (nPriceCalendarDepartureMonth > 12) {
				nPriceCalendarDepartureMonth = 1;
				nPriceCalendarDepartureYear = parseInt(nPriceCalendarDepartureYear,10) + 1;
			}
		}
		
		if (bPriceCalendarRoundtrip == '0') {
			/* Need Home Cal to change */
			if (parseInt(nPriceCalendarReturnMonth,10) < parseInt(nPriceCalendarDepartureMonth,10)) {
				bChangeHomeCal = true;
				nPriceCalendarReturnMonth = nPriceCalendarDepartureMonth;
				nPriceCalendarReturnYear = nPriceCalendarDepartureYear;
			}
			if (parseInt(nPriceCalendarReturnYear,10) < parseInt(nPriceCalendarDepartureYear,10)) {
				bChangeHomeCal = true;
				nPriceCalendarReturnMonth = nPriceCalendarDepartureMonth;
				nPriceCalendarReturnYear = nPriceCalendarDepartureYear;
			}
			if (parseInt(nPriceCalendarReturnDay,10) < 10) {
				nPriceCalendarReturnDay = "0" + parseInt(nPriceCalendarReturnDay,10);
			}
			if (bChangeHomeCal == true) {
				if (IsDate( nPriceCalendarReturnDay + "-" + nPriceCalendarReturnMonth + "-" + nPriceCalendarReturnYear ) == true) {
				} else {
					if (IsDate("28-" + nPriceCalendarReturnMonth + "-" + nPriceCalendarReturnYear ) == true) {
						nPriceCalendarReturnDay = 28;
					}
				}
			}
		}
		if (parseInt(nPriceCalendarDepartureDay,10) < 10) {
			nPriceCalendarDepartureDay = "0" + parseInt(nPriceCalendarDepartureDay,10);
		}
		/* Make sure it is a valid date */ 
		if (IsDate( nPriceCalendarDepartureDay + "-" + nPriceCalendarDepartureMonth + "-" + nPriceCalendarDepartureYear ) == true) {
			price_out_event(parseInt(nPriceCalendarDepartureDay,10)-1);
		} else {
			if (IsDate("28-" + nPriceCalendarDepartureMonth + "-" + nPriceCalendarDepartureYear ) == true) {
				nPriceCalendarDepartureDay = 28;
				price_out_event(parseInt(nPriceCalendarDepartureDay,10)-1);
			}
		}
	}
	if (oChanged == 'home') {
		var bChangeOutCal = false;
		if (nValue == 'prev') {
			nPriceCalendarReturnMonth = nPriceCalendarReturnMonth - 1;
			if (nPriceCalendarReturnMonth < 1) {
				nPriceCalendarReturnMonth = 12;
				nPriceCalendarReturnYear = parseInt(nPriceCalendarReturnYear,10) - 1;
			}
		} else {
			nPriceCalendarReturnMonth = parseInt(nPriceCalendarReturnMonth,10) + 1;
			if (nPriceCalendarReturnMonth > 12) {
				nPriceCalendarReturnMonth = 1;
				nPriceCalendarReturnYear = parseInt(nPriceCalendarReturnYear,10) + 1;
			}
		}
		/* Need Out Cal to change */
		if (parseInt(nPriceCalendarDepartureMonth,10) > parseInt(nPriceCalendarReturnMonth,10)) {
			bChangeOutCal = true;
			nPriceCalendarDepartureMonth = nPriceCalendarReturnMonth;
			nPriceCalendarDepartureYear = nPriceCalendarReturnYear;
		}
		if (bChangeOutCal == true) {
			if (IsDate( nPriceCalendarDepartureDay + "-" + nPriceCalendarDepartureMonth + "-" + nPriceCalendarDepartureYear ) == true) {
				if (parseInt(nPriceCalendarDepartureDay,10) < 10) {
					nPriceCalendarDepartureDay = "0" + parseInt(nPriceCalendarDepartureDay,10);
				}
			} else {
				if (IsDate("28-" + nPriceCalendarDepartureMonth + "-" + nPriceCalendarDepartureYear ) == true) {
					nPriceCalendarDepartureDay = 28;
				}
			}
		}
		if (parseInt(nPriceCalendarReturnDay,10) < 10) {
			nPriceCalendarReturnDay = "0" + parseInt(nPriceCalendarReturnDay,10);
		}
		/* Make sure it is a valid date */ 
		if (IsDate( nPriceCalendarReturnDay + "-" + nPriceCalendarReturnMonth + "-" + nPriceCalendarReturnYear ) == true) {
			price_home_event(parseInt(nPriceCalendarReturnDay,10)-1);
		} else {
			if (IsDate("28-" + nPriceCalendarReturnMonth + "-" + nPriceCalendarReturnYear ) == true) {
				nPriceCalendarReturnDay = 28;
				price_home_event(parseInt(nPriceCalendarReturnDay,10)-1);
			}
		}
	}
	
	$("out_date_container").update(oPriceCalendarMonthName[nPriceCalendarDepartureMonth-1] + " " + nPriceCalendarDepartureYear);
	if (bPriceCalendarRoundtrip == 1) {
		$("home_date_container").update(oPriceCalendarMonthName[nPriceCalendarReturnMonth-1] + " " + nPriceCalendarReturnYear);
	}
};

function IsDate(sDate)
{
	// format: dd.mm.yyyy or dd/mm/yyyy or dd-mm-yyyy
	var s = String(sDate).split(/[-\/., ]/);
	var dd = parseInt(s[0],10);
	var mm = parseInt(s[1],10);
	var yyyy = parseInt(s[2],10);
	var dateStr = mm + '/' + dd + '/' + yyyy;
	// mm-dd-yyyy yyyy/mm/dd mm/dd/yyyy mmm dd, yyyy mm dd, yyyy ... for Date().parse(..)
	var dt = new Date(dateStr);
	if (dt.getDate() == dd && dt.getMonth() + 1 == mm && dt.getFullYear() == yyyy) {
		return true;
	} else {
		return false;
	}
};

function resizeIframe()
{
	var path = resizeIframepath;
	var height1 = 0;
	var height2 = 0;
	var height3 = 0;
	var height4 = 0;
	var height5 = 0;
	var height = 0;
	if(path != '') 
	{
		try 
		{
			if($('tse'))
			{
				height1 = getElementHeight('tse');
				if(isNaN(height1))
				{
					height1 = 0;
				}
			}
			if($('hotel_v1'))
			{
				height2 = getElementHeight('hotel_v1');
				if(isNaN(height2))
				{
					height2 = 0;
				}
			}
			if($('list_container'))
			{
				height3 = getElementHeight('list_container');
				if(isNaN(height3))
				{
					height3 = 0;
				}
			}
			if($('hotel_tse_module')) 
			{
				height4 = getElementHeight('hotel_tse_module');
				if(isNaN(height4))
				{
					height4 = 0;
				}
			}
			if($('searchResult'))
			{
				height5 = getElementHeight('searchResult');
				if(isNaN(height5))
				{
					height5 = 0;
				}
			}
			
			height = Math.max( height1 ,height2 );
			
			height = Math.max( height ,height3 );
			height = Math.max( height ,height4 );
			height = Math.max( height ,height5 );
			
			if (isNaN(height)) 
			{
				height=10000;
			}
			
			height = height +resizeExtraPixels;
	
			//this loads the hidden page into the iFrame hidden inside the inner page, with height as a url parameter.
			var iframe = document.getElementById('flight_resize_iframe');
			iframe.src = path + '?height=' + height;
			iframe.style.position = "relative";
			iframe.style.top = "auto";
			iframe.style.left = "auto";
			iframe.setAttribute("width", "1");
			iframe.setAttribute("height", "1");
		} 
		catch (error) {
		}
	}
};

function getElementHeight(element) 
{
	if($(element).clientHeight) 
	{
		return $(element).clientHeight;
	}
	else 
	{
		if($(element).offsetHeight) 
		{
			return $(element).offsetHeight;
		}
	}
};

function showTextAds()
{
	if ($('list_text_ads_1')) 
	{
		new Ajax.Request(sAjaxUrl, {
			method: 'post',
			parameters: {
				action: "showTextAds",
				nSearchID: nSearchID,
				nSupplierID: nSupplierID,
				sDestinationID: sDestinationID,
				uniqueValue: makeUniqueValue()
			},
			onSuccess: function(transport){
				var response = transport.responseText || "no response text";
				sTextAdsHTML = response;
				$("list_text_ads_1").update(sTextAdsHTML);
			}
		});
	}
};

function showTSEAds()
{
	new Ajax.Request(sAjaxUrl, {
		method: 'post',
		parameters: {
			action: "showTSEAds",
			nSearchID: nSearchID,
			nSupplierId: nSupplierId,
			uniqueValue: makeUniqueValue()
		},
		onSuccess: function(transport){
			var response = transport.responseText;
			sTSEAdsHTML = response;
		}
	});
};


