// JavaScript Document

/**
 * JavaScript: Dynamic Packaging's Available Packages page scripts for TBXOnline. 
 * @version: 1.0
 * @author: Buddhika Nalin Fernando
 * Date created: 07/08/2006
 * Date modified: 07/08/2006
 * Copyright: Codegen International (Pvt) Ltd.
 */
 
/**
 * Toggles the visibility of Flights that have the same price but different times.
 * This functionality will only be available only after checking availability.
 * @param   p_iLegIndex			Index of the leg (starting from 1) given for each page at 
 *								creation.<br>
 *          p_iBundleIndex		Index of the Name of the page with the relative path from 
 *								the Layout Page. Can be used with some parameters 
 *								as a GET request.<br>
 *          p_PostData			Use a string with parameters seperated with '&' sign.
 *								Each parameter should have Name value pairs seperated
 *								with '=' sign. 
 *								eg. 'Name1=Value1&Name2=Value2'<br>
 * @see         loadTabPage(),submitTabPage().
 */
function showDPFlightDifTime( p_iLegIndex )
{
	var p_oFlightTable = document.getElementById( 'DPDifTimeFlights' + p_iLegIndex );
	var p_oFlightsDifTimeImg = document.getElementById('DPFlightsDifTimeImg'  + p_iLegIndex );
	var p_oFlightsDifTimeLink = document.getElementById('DPFlightsDifTimeLink'  + p_iLegIndex );
	if( p_oFlightTable.style.display=='none' )
	{
		p_oFlightTable.style.display='';
		p_oFlightsDifTimeImg.src='images/dp/TreeMinus.gif';
		p_oFlightsDifTimeLink.innerHTML='Hide Flights in Different times'
	}
	else
	{
		p_oFlightTable.style.display='none';
		p_oFlightsDifTimeImg.src='images/dp/TreePlus.gif';
		p_oFlightsDifTimeLink.innerHTML='Show Flights in Different times'
	}
}

function selectDPFlightDifTime( p_iLegIndex, p_iBundleIndex , p_sBundleID)
{
	document.getElementById('DPFlightDifTimeSelectedBundleDiv' + p_iLegIndex ).innerHTML =
				document.getElementById('DPFlightDifTimeBundleDataDiv' + p_iLegIndex + '_' + p_iBundleIndex).innerHTML;
	document.getElementById('DPFlightLegSelectedBundleID' + p_iLegIndex ).value = p_sBundleID;
	
	/**
	 * Disabled the Select Button of selected Bundle.
	 */
	var iBundleCount=parseInt(document.getElementById('DPFlightBundleCount' + p_iLegIndex ).value, 10 );
	var iBundleIndex=1;
	var oSelectButton; 
	for(; iBundleIndex<=iBundleCount;iBundleIndex++)
	{
		oSelectButton = document.getElementById('DPFlightDifTimeSelectButton' + p_iLegIndex + '_' + iBundleIndex );
		if(iBundleIndex==p_iBundleIndex)
		{
			oSelectButton.disabled=true;
			oSelectButton.className='btnDisable';
		}
		else
		{
			oSelectButton.className='btn';
			oSelectButton.disabled=false;
		}
	}
}


function showDPHotelRoomTypes( p_iHotelIndex )
{
	var p_oHotelRoomTable = document.getElementById( 'DPHotelRoomTypeTable' + p_iHotelIndex );
	var p_oHotelRoomTypesActionImg = document.getElementById('DPHotelRoomTypesActionImg'  + p_iHotelIndex );
	var p_oHotelRoomTypesStatusImg = document.getElementById('DPHotelRoomTypesStatusImg'  + p_iHotelIndex );
	var p_oHotelRoomTypesLink = document.getElementById('DPHotelRoomTypesLink'  + p_iHotelIndex );
	if( p_oHotelRoomTable.style.display=='none' )
	{
		p_oHotelRoomTable.style.display='';
		p_oHotelRoomTypesActionImg.src='images/dp/TreeCollapse.gif';
		p_oHotelRoomTypesStatusImg.src='images/dp/TreeExpanded.gif';
		p_oHotelRoomTypesLink.innerHTML='Hide Room Types'
	}
	else
	{
		p_oHotelRoomTable.style.display='none';
		p_oHotelRoomTypesActionImg.src='images/dp/TreeExpand.gif';
		p_oHotelRoomTypesStatusImg.src='images/dp/TreeCollapsed.gif';
		p_oHotelRoomTypesLink.innerHTML='More Room Types'
	}
}

function selectDPHotelRoomType( p_iHotelIndex, p_sTypeDescription, p_sTypeID, p_sValue )
{
	
	//alert("selectDPHotelRoomType() --> p_iHotelIndex = " + p_iHotelIndex);
	//alert("selectDPHotelRoomType() --> p_sTypeDescription = " + p_sTypeDescription);
	//alert("selectDPHotelRoomType() --> p_sTypeID = " + p_sTypeID);
	//alert("selectDPHotelRoomType() --> p_sValue = " + p_sValue);
	
	
	
	document.getElementById('DPHotelRoomTypeDescription' + p_iHotelIndex ).innerHTML = p_sTypeDescription;
	document.getElementById('DPHotelSelectedRoomTypeID' + p_iHotelIndex ).value = p_sTypeID;
	document.getElementById('DPHotelSelectedRoomTypePremium' + p_iHotelIndex ).value = p_sValue;

	if( document.getElementById('DPGrandTotal') == null )
	{
		//updateDPHotelGrandTotal( p_iHotelIndex );  // Nirasha
	}
	else
	{
		//updateDPGrandTotal();  // Nirasha
	}
}

function showDPHotelOccupancyTypes( p_iHotelIndex )
{
	var p_oHotelOccupancyTable = document.getElementById( 'DPHotelOccupancyTypeTable' + p_iHotelIndex );
	var p_oHotelOccupancyTypesActionImg = document.getElementById('DPHotelOccupancyTypesActionImg'  + p_iHotelIndex );
	var p_oHotelOccupancyTypesStatusImg = document.getElementById('DPHotelOccupancyTypesStatusImg'  + p_iHotelIndex );
	var p_oHotelOccupancyTypesLink = document.getElementById('DPHotelOccupancyTypesLink'  + p_iHotelIndex );
	if( p_oHotelOccupancyTable.style.display=='none' )
	{
		p_oHotelOccupancyTable.style.display='';
		p_oHotelOccupancyTypesActionImg.src='images/dp/TreeCollapse.gif';
		p_oHotelOccupancyTypesStatusImg.src='images/dp/TreeExpanded.gif';
		p_oHotelOccupancyTypesLink.innerHTML='Hide Occupancy Types'
	}
	else
	{
		p_oHotelOccupancyTable.style.display='none';
		p_oHotelOccupancyTypesActionImg.src='images/dp/TreeExpand.gif';
		p_oHotelOccupancyTypesStatusImg.src='images/dp/TreeCollapsed.gif';
		p_oHotelOccupancyTypesLink.innerHTML='More Occupancy Types'
	}
}

function selectDPHotelOccupancyType( p_iHotelIndex, p_sTypeDescription, p_sTypeID, p_sValue )
{
	document.getElementById('DPHotelOccupancyTypeDescription' + p_iHotelIndex ).innerHTML = p_sTypeDescription;
	document.getElementById('DPHotelSelectedOccupancyTypeID' + p_iHotelIndex ).value = p_sTypeID;
	document.getElementById('DPHotelSelectedOccupancyTypePremium' + p_iHotelIndex ).value = p_sValue;

	if( document.getElementById('DPGrandTotal') == null )
	{
		updateDPHotelGrandTotal( p_iHotelIndex );
	}
	else
	{
		updateDPGrandTotal();
	}
}

function showDPTransferTypes( p_iHotelIndex )
{
	var p_oTransferTable = document.getElementById( 'DPTransferTypeTable' + p_iHotelIndex );
	var p_oTransferTypesActionImg = document.getElementById('DPTransferTypesActionImg' + p_iHotelIndex );
	var p_oTransferTypesStatusImg = document.getElementById('DPTransferTypesStatusImg' + p_iHotelIndex );
	var p_oTransferTypesLink = document.getElementById('DPTransferTypesLink' + p_iHotelIndex );
	if( p_oTransferTable.style.display=='none' )
	{
		p_oTransferTable.style.display='';
		p_oTransferTypesActionImg.src='images/dp/TreeCollapse.gif';
		p_oTransferTypesStatusImg.src='images/dp/TreeExpanded.gif';
		p_oTransferTypesLink.innerHTML='Hide Transfer Types'
	}
	else
	{
		p_oTransferTable.style.display='none';
		p_oTransferTypesActionImg.src='images/dp/TreeExpand.gif';
		p_oTransferTypesStatusImg.src='images/dp/TreeCollapsed.gif';
		p_oTransferTypesLink.innerHTML='More Transfer Types'
	}
}

function selectDPTransferType( p_iTransferIndex, p_sTypeDescription, p_sTypeID, p_sValue )
{
	document.getElementById('DPTransferTypeDescription' + p_iTransferIndex ).innerHTML = p_sTypeDescription;
	document.getElementById('DPTransferSelectedTypeID' + p_iTransferIndex ).value = p_sTypeID;
	document.getElementById('DPTransferSelectedTypePremium' + p_iTransferIndex ).value = p_sValue;
	updateDPGrandTotal();
}

function updateDPGrandTotal()
{
	var fNewTotal = 0;
	var fSuggestedTotal = parseFloat(document.getElementById('DPSuggestedTotal').value,0);
	var iHotelCount = parseInt(document.getElementById('DPHotelCount').value,0);
	var iTransferCount = parseInt(document.getElementById('DPTransferCount').value,0);
	var iHotelIndex;
	var iTransferIndex;

	if(document.getElementById('DPFlightTable').style.display=='')
	{
		fNewTotal += parseFloat(document.getElementById('DPFlightPrice').value,0);
	}

	for( iHotelIndex = 1; iHotelIndex <= iHotelCount; iHotelIndex++ )
	{
		if(document.getElementById('DPHotelTable' + iHotelIndex).style.display=='')
		{
			fNewTotal += parseFloat(document.getElementById('DPHotelPrice' + iHotelIndex).value,0);
			fNewTotal += parseFloat(document.getElementById('DPHotelSelectedRoomTypePremium' + iHotelIndex).value,0);
			fNewTotal += parseFloat(document.getElementById('DPHotelSelectedOccupancyTypePremium' + iHotelIndex).value,0);
		}
	}
	
	for( iTransferIndex = 1; iTransferIndex <= iTransferCount; iTransferIndex++ )
	{
		if(document.getElementById('DPTransferTable' + iTransferIndex).style.display=='')
		{
			fNewTotal += parseFloat(document.getElementById('DPTransferPrice' + iTransferIndex).value,0);
			fNewTotal += parseFloat(document.getElementById('DPTransferSelectedTypePremium' + iTransferIndex).value,0);
		}
	}

	fNewTotal += fSuggestedTotal;
	document.getElementById('DPGrandTotal').innerHTML = getCurrencyString(fNewTotal);
}

function checkDPFlightAvailability()
{
	document.getElementById('DPAvailabilityCheckButton').style.display='none';
	document.getElementById('DPFlightAvailabilityMsg').innerHTML='Checking Availability ';
	document.getElementById('DPFlightAvailabilityAnimation').style.display='';
	
	requestDPFlightAvailability();
}

function gotoDPIndexPageFromAvailablePackages()
{
	if(document.getElementById('DPProceedButton').disabled)
	{
		return;
	}
	document.getElementById('DPProceedButton').disabled = true;
	//submitTabPage( TAB_ID_DP,'travelpaths/presentation/DPIndexPage.php', '' );
}

// commented Nirasha

//function showFlightAlternatives()
//{
//	if(document.getElementById('DPProceedButton').disabled)
	//{
		//return;
	//}
	//if(document.getElementById('DPFlightAvailabilityAnimation').style.display=='')
	//{
		//return;
	//}
	//document.getElementById('DPProceedButton').disabled = true;
	//submitTabPage( TAB_ID_DP,'travelpaths/presentation/DPUpdateSelectedPackage.php', getPostData()+'&DirectTo=FlightAlt' );
	
//}

// added Nirasha - 14-03-2007

function showFlightAlternatives()
{
	document.DPSearchResultPage.directToFlightAlternativePage.value = "FlightAlt";
	//alert("document.DPSearchResultPage.directToFlightAlternativePage.value  == " + document.DPSearchResultPage.directToFlightAlternativePage.value);
	
	formattedParamList = new String(getPostData());
	formattedParamList = formattedParamList.replace("&","?");	
	
	//document.DPSearchResultPage.action ="DPUpdateSelectedPackage.php" + formattedParamList + "&DirectTo=FlightAlt";
	document.DPSearchResultPage.action ="DPUpdateSelectedPackage.php?DirectTo=FlightAlt";
	//alert("URL when select alternative flights  == " + document.DPSearchResultPage.action);	
    document.DPSearchResultPage.submit();
}


function showHotelAlternatives(p_sItenaryNumber)
{
	if(document.getElementById('DPProceedButton').disabled)
	{
		return;
	}
	if(document.getElementById('DPFlightAvailabilityAnimation').style.display=='')
	{
		return;
	}
	document.getElementById('DPProceedButton').disabled = true;
	//submitTabPage( TAB_ID_DP,'travelpaths/presentation/DPUpdateSelectedPackage.php', getPostData()+'&DirectTo=HotelAlt&IteOrder=' + p_sItenaryNumber );
}

function addAddon( p_sAddOnId )
{
	if(document.getElementById('DPProceedButton').disabled)
	{
		return;
	}
	if(document.getElementById('DPFlightAvailabilityAnimation').style.display=='')
	{
		return;
	}
	document.getElementById('DPProceedButton').disabled = true;
	//submitTabPage( TAB_ID_DP,'travelpaths/presentation/DPUpdateSelectedPackage.php', getPostData()+'&DirectTo=AddOns&AddOnId='+p_sAddOnId );
}

function selectDPOption( p_sOptionName )
{
	document.getElementById(p_sOptionName).checked=true;		
}

function removeDPFlight()
{
	if(document.getElementById('DPProceedButton').disabled)
	{
		return;
	}
	if(confirm('You are tring to remove the flight. Are you sure?'))
	{
//		updateDPGrandTotal();
		if(document.getElementById('DPFlightAvailabilityAnimation').style.display=='')
		{
			alert('Please wait for the availabity.');
			return;
		}
		document.getElementById('DPFlightTable').style.display='none';
		var sPostData = getPostData();
		if( sPostData == '' )
		{
			alert('No item remains in the package. Please start a new search.');
			return;
		}
		sPostData = sPostData + '&DirectTo=RemoveItem' ;
		//alert('POSTDATA = ' + sPostData );
		
		document.getElementById('DPProceedButton').disabled = true;
		//submitTabPage( TAB_ID_DP,'travelpaths/presentation/DPUpdateSelectedPackage.php', sPostData );
	}
}

function removeDPHotel( p_sHotelTableName )
{
	if(document.getElementById('DPProceedButton').disabled)
	{
		return;
	}

	if(confirm('You are trying to remove the Hotel. Are you sure?')) 
	{
//		updateDPGrandTotal();
		if(document.getElementById('DPFlightAvailabilityAnimation').style.display=='')
		{
			alert('Please wait for the availabity.');
			return;
		}
		document.getElementById(p_sHotelTableName).style.display='none';
		var sPostData = getPostData();
		if( sPostData == '' )
		{
			alert('No item remains in the package. Please start a new search.');
			return;
		}
		
		sPostData = sPostData + '&DirectTo=RemoveItem' ;
		//alert('POSTDATA = ' + sPostData );
		
		document.getElementById('DPProceedButton').disabled = true;
		//submitTabPage( TAB_ID_DP,'travelpaths/presentation/DPUpdateSelectedPackage.php', sPostData );
	}
}

function removeDPTransfer( p_sTransferTableName )
{
	if(document.getElementById('DPProceedButton').disabled)
	{
		return;
	}

	if(confirm('You are trying to remove the Transfer. Are you sure?')) 
	{
//		updateDPGrandTotal();
		if(document.getElementById('DPFlightAvailabilityAnimation').style.display=='')
		{
			alert('Please wait for the availabity.');
			return;
		}
		document.getElementById(p_sTransferTableName).style.display='none';
		var sPostData = getPostData();
		if( sPostData == '' )
		{
			alert('No item remains in the package. Please start a new search.');
			return;
		}
		
		sPostData = sPostData + '&DirectTo=RemoveItem' ;
		//alert('POSTDATA = ' + sPostData );
		
		document.getElementById('DPProceedButton').disabled = true;
		//submitTabPage( TAB_ID_DP,'travelpaths/presentation/DPUpdateSelectedPackage.php', sPostData );
	}
}

function getPostData()
{
	var sPostData = '';
	
	/**
	 * Add Flights to PostData.
	 */
	if(document.getElementById('DPFlightTable').style.display=='')
	{
		var iBundleCount;
		var iLegIndex;

		iLegCount = parseInt(document.getElementById('DPFlightLegCount').value);
		for(iLegIndex=1;iLegIndex<=iLegCount;iLegIndex++)
		{
		   //alert("document.getElementById('DPFlightLegID' + iLegIndex).value  == " + document.getElementById('DPFlightLegID' + iLegIndex).value);
		   //alert("document.getElementById('DPFlightLegSelectedBundleID' + iLegIndex).value  == " + document.getElementById('DPFlightLegSelectedBundleID' + iLegIndex).value);
		   
		
		
			sPostData = sPostData + '&FlightLeg' + document.getElementById('DPFlightLegID' + iLegIndex).value + '=' + 
						document.getElementById('DPFlightLegSelectedBundleID' + iLegIndex).value;
		}
	}

	/**
	 * Add Hotels to PostData.
	 */
	/*  commented Nirasha
	var iHotelCount = parseInt(document.getElementById('DPHotelCount').value,10);
	var iHotelIndex;

	for(iHotelIndex=1;iHotelIndex<=iHotelCount;iHotelIndex++)
	{
		if(document.getElementById('DPHotelTable' + iHotelIndex).style.display=='')
		{
			sPostData = sPostData + '&HotelRoomType' + document.getElementById('DPHotelID' + iHotelIndex).value + '=' + 
						document.getElementById('DPHotelSelectedRoomTypeID' + iHotelIndex).value;
			sPostData = sPostData + '&HotelOccupancyType' + document.getElementById('DPHotelID' + iHotelIndex).value + '=' + 
						document.getElementById('DPHotelSelectedOccupancyTypeID' + iHotelIndex).value;
		}
	} */
	
	
	
	/**
	 * Add Transfers to PostData.
	 */
	 
	/*  commented Nirasha 
	var iTransferCount = parseInt(document.getElementById('DPTransferCount').value,10);
	var iTransferIndex;

	for(iTransferIndex=1;iTransferIndex<=iTransferCount;iTransferIndex++)
	{
		if(document.getElementById('DPTransferTable' + iTransferIndex).style.display=='')
		{
			sPostData = sPostData + '&TransferType' + document.getElementById('DPTransferID' + iTransferIndex).value + '=' + 
						document.getElementById('DPTransferSelectedTypeID' + iTransferIndex).value;
		}
	} */
     
    //alert("sPostData  :: " + sPostData);
	return sPostData;
}
/*
function submitDPPage()
{
	document.getElementById('DPProceedButton').disabled=true;
	if(document.getElementById('DPFlightAvailabilityAnimation').style.display=='')
	{
		alert('Please wait for the availabity.');
		document.getElementById('DPProceedButton').disabled=false;
		return;
	}
	var sPostData = getPostData();
	if( sPostData == '' )
	{
		alert('No item remains in the package. Please start a new search.');
		document.getElementById('DPProceedButton').disabled=false;
		return;
	}
	alert('POSTDATA = ' + sPostData );
 	submitTabPage( TAB_ID_DP,'travelpaths/presentation/DPUpdateSelectedPackage.php', sPostData );
}
*/
function submitDPPage()
{
	var sPostData = getPostData();
	if( sPostData == '' )
	{
		alert('No item remains in the package. Please start a new search.');		
		return;
	}
	
	unFormattedPostData = new String(sPostData); 
	formattedPostData = unFormattedPostData.replace("&","");
	
	alert("sumbit URL = DPUpdateSelectedPackage.php?" + formattedPostData ); 	
	document.DPSearchResultPage.action ="DPUpdateSelectedPackage.php?" + formattedPostData ;	
    document.DPSearchResultPage.submit();
}





function stateChangedDPFlightAvailability()
{
	if (XmlHttpDP.readyState==4 || XmlHttpDP.readyState=="complete")
	{ 
		document.getElementById("DPFlightDiv").innerHTML=XmlHttpDP.responseText 
		runBodyOnloadScript(XmlHttpDP.responseText); 
	} 
} 

function requestDPFlightAvailability()
{
	XmlHttpDP=GetXmlHttpObject(stateChangedDPFlightAvailability);
	XmlHttpDP.open("POST", getNoCacheURL( 'travelpaths/presentation/DPShowPackagesPage.php' ), true)
	XmlHttpDP.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	XmlHttpDP.send('PageToDisplay=FlightAvail');
}


/**
 * Start of functions of DPHotelAlternativesPage
 */ 
function getHotelPostData( p_iHotelIndex )
{
	
	//alert("getHotelPostData() -->  p_iHotelIndex  :: " + p_iHotelIndex);
	//alert("getHotelPostData() -->  document.getElementById('DPHotelID' + p_iHotelIndex).value  == " + document.getElementById('DPHotelID' + p_iHotelIndex).value);
	//alert("getHotelPostData() -->  DPHotelID"+p_iHotelIndex+"  =  "+document.getElementById('DPHotelID' + p_iHotelIndex).value);
	//alert("getHotelPostData() -->  DPHotelSelectedRoomTypeID"+p_iHotelIndex+"  =  "+document.getElementById('DPHotelSelectedRoomTypeID' + p_iHotelIndex).value);
	//alert("getHotelPostData() -->  DPHotelSelectedOccupancyTypeID"+p_iHotelIndex+"  =  "+document.getElementById('DPHotelSelectedOccupancyTypeID' + p_iHotelIndex).value);
	
	var sPostData = 'AlternativeHotelID=' + document.getElementById('DPHotelID' + p_iHotelIndex).value;
	sPostData = sPostData + '&HotelRoomTypeID=' + document.getElementById('DPHotelSelectedRoomTypeID' + p_iHotelIndex).value;
	sPostData = sPostData + '&HotelOccupancyTypeID=' + document.getElementById('DPHotelSelectedOccupancyTypeID' + p_iHotelIndex).value;
	//alert(' PostData = ' + sPostData );
	return sPostData;
}

function updateDPHotelGrandTotal( p_iHotelIndex )
{
	var fNewTotal = parseFloat(document.getElementById('DPSuggestedTotal' + p_iHotelIndex).value,0);
	fNewTotal += parseFloat(document.getElementById('DPHotelSelectedRoomTypePremium' + p_iHotelIndex).value,0);
	fNewTotal += parseFloat(document.getElementById('DPHotelSelectedOccupancyTypePremium' + p_iHotelIndex).value,0);
	document.getElementById('DPGrandTotal' + p_iHotelIndex).innerHTML = getCurrencyString(fNewTotal);
}

// commented Nirasha
//function selectHotelAlternative( p_iHotelIndex )
//{
	//submitTabPage(TAB_ID_DP,'travelpaths/presentation/DPUpgradeHotel.php',getHotelPostData(p_iHotelIndex));	
//}

// written by Nirasha

function selectHotelAlternative( p_iHotelIndex )
{	
    //alert("selectHotelAlternative() :: p_iHotelIndex = " + p_iHotelIndex);
	formattedParamList = new String(getHotelPostData(p_iHotelIndex));
	//alert("selectHotelAlternative() :: DPUpgradeHotel.php?" + formattedParamList);	
	document.DPSearchResultPage.action ="DPUpgradeHotel.php?" + formattedParamList ;	
    document.DPSearchResultPage.submit();	
}
