var index1 = -1;
var index2 = -1;

/**
	This function takes the selected indexes of 2 radio button lists.
	It keeps track of previously selected indexes, so radioIndex1 or
	radioIndex2 can be updated by themselves or together.
	If both indexes are updated together, pass them both through (eg TotalPackageCost(2,1)),
	however if only one index is to be updated, pass through TotalPackageCost(2,-1) or
	TotalPackageCost(-1,1). 
	
	This method came into existance through needing to calculate a total cost from selections
	made to two different RadioButtonLists. It was designed to be used in conjunction with the 
	CustomRadioButtonList.cs class, to allow ListItems within a RadioButtonList to trigger javascript
	depending on which ListItem was clicked on.
	
	This method is also tightly linked to the javascript code written to the page from all the 
	Package Details pages.
	
	It looks for an array called 'costArray'. The structure of the costArray is as follows:
		costArray = { [innerCostArray]*, [innerCostArray] }
		
		innerCostArray = {travellers, index1, index2, cost}
			where:
				travellers	=	If the package cost can easily be determined by (number of travellers * cost), this is set to -1
								If the package cost is not determined by this way, this value is set to the number of travellers that matches the cost below
				index1		=	The required selected index of the first RadioButtonList
				index2		=	The required selected index of the second RadioButtonList (if there is only one RadioButtonList this is set to -2)
				cost		=	The totalCost when the above indexes have been selected (multipled by the number of travellers if the above travellers variable is not -1)
**/
function TotalPackageCost(radioIndex1, radioIndex2, monthYearSelect, daySelect, priceRollOverMonth){

	nArrivalDateStatus = GetArrivalDateStatus(monthYearSelect, daySelect, priceRollOverMonth);
	
	if (radioIndex1 != -1){
		this.index1 = radioIndex1;
	}
	
	if (radioIndex2 != -1){
		this.index2 = radioIndex2;
	}
	
	var agent = window['agentLoggedIn'];
	var rentalCar = window['rentalCarUnconfirmed'];
	
	var totalCostArray = window['costArray'];
	var totalTravellers = window['numTravellers'];
	
	var tempCost;
	var totalCost;
	var costFound = false;
	
	var i;
	
	if ((index1 != -1) && (index2 != -1) && (index2 != -2))
	{
		for (i = 0; i < totalCostArray.length; i++)
		{
			tempCost = totalCostArray[i];
			
			//tempCost[0]: if the package cost is based on the number of travellers (not easiliy multipliable), then this is set to the number of travellers, otherwise set to -1
			//tempCost[1]: index1
			//tempCost[2]: index2
			//tempCost[3]: current cost
			//tempCost[4]: next cost
			
			if (tempCost[0] == -1)
			{
				
				if ((tempCost[1] == index1) && (tempCost[2] == index2))
				{
					costFound = true;
					//totalCost = totalTravellers * tempCost[3];
					if (nArrivalDateStatus == 1)
					{
						totalCost = totalTravellers * tempCost[3];
					}
					else if (nArrivalDateStatus == 2)
					{
						totalCost = totalTravellers * tempCost[4];
					}
					else 
					{
						totalCost = 0;
					}
					
					if (totalCost > 0)
					{
						DisplayTotalCost(agent, true, totalCost);					
					}
					else
					{
						DisplayTotalCost(agent, false, 0);
					}
				}
			}
			else
			{
				if ((tempCost[0] == totalTravellers) && (tempCost[1] == index1) && (tempCost[2] == index2))
				{
					costFound = true;
					//totalCost = tempCost[3];
					if (nArrivalDateStatus == 1)
					{
						totalCost = tempCost[3];
					}
					else if (nArrivalDateStatus == 2)
					{
						totalCost = tempCost[4];
					}
					else 
					{
						totalCost = 0;
					}
					
					if (totalCost > 0)
					{
						DisplayTotalCost(agent, true, totalCost);					
					}
					else
					{
						DisplayTotalCost(agent, false, 0);
					}
				}
			}
		}
		
		if (!costFound)
		{
			DisplayTotalCost(agent, false, 0);
		}
		
	}
	else if ((index1 != -1) && (index2 == -2))
	{
		
		for (i = 0; i < totalCostArray.length; i++)
		{
			tempCost = totalCostArray[i];
			
			//tempCost[0]: if the package cost is based on the number of travellers (not easiliy multipliable), then this is set to the number of travellers, otherwise set to -1
			//tempCost[1]: index1
			//tempCost[2]: index2
			//tempCost[3]: current cost
			//tempCost[4]: next cost
			
			if (tempCost[0] == -1)
			{
				if (tempCost[1] == index1)
				{
					
					//totalCost = totalTravellers * tempCost[3];
					if (nArrivalDateStatus == 1)
					{
						totalCost = totalTravellers * tempCost[3];
					}
					else if (nArrivalDateStatus == 2)
					{
						totalCost = totalTravellers * tempCost[4];
					}
					else 
					{
						totalCost = 0;
					}
					
					if (totalCost > 0)
					{
						DisplayTotalCost(agent, true, totalCost);					
					}
					else
					{
						DisplayTotalCost(agent, false, 0);
					}
				}
			}
		}
	}
	else if ((index1 == -1) && ((index2 == -1) || (index2 == -2)))
	{
		DisplayTotalCost(agent, true, 0);		
	}
	else if ((index1 == -1) || (index2 == -1))
	{
		DisplayTotalCost(agent, true, 0);		
	}
	else
	{
		DisplayTotalCost(agent, false, 0);
	}
}

// Function to actually set the visual Grand Total controls.
//  IsAgent		-	Specifies if the user is an agent or not.
//  PriceFound	-	Whether we found a price or not. 
//					In the case of a future booking period not known this will be false
//  TotalCost	-	The total cost to display - will be 0 if priceFound is false
function DisplayTotalCost(isAgent, priceFound, totalCost)
{
	if (!priceFound)
	{
		if (!isAgent)
		{
			document.getElementById("GrandTotal").childNodes[0].nodeValue = window['priceNotFoundMessage'];
		}
		else
		{
			document.getElementById("GrandTotal").childNodes[0].nodeValue = window['priceNotFoundMessage'];
			document.getElementById("AgentCommission").childNodes[0].nodeValue = window['priceNotFoundMessage'];
		}
	}
	else
	{
		if (!isAgent)
		{
			document.getElementById("GrandTotal").childNodes[0].nodeValue = "NZ$" + FormatCurrency(totalCost);
		}
		else
		{
			document.getElementById("GrandTotal").childNodes[0].nodeValue = "NZ$" + FormatCurrency(totalCost * .8);
			document.getElementById("AgentCommission").childNodes[0].nodeValue = "NZ$" + FormatCurrency(totalCost * .2);
		}
	}
}

function FormatCurrency(num) {
	num = num.toString().replace(/\$|\,/g,'');
	
	if(isNaN(num))
		num = "0";
	
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num * 100 + 0.50000000001);
	cents = num%100;
	num = Math.floor(num / 100).toString();
	
	if(cents < 10)
		cents = "0" + cents;
	
	for (var i = 0; i < Math.floor((num.length - (1+i)) / 3); i++)
		num = num.substring(0, num.length-(4*i+3)) + ',' + num.substring(num.length - (4*i+3));
	
	return num + '.' + cents;
}

function CheckHiddenControlAndComputeTotalCost(radioIndex1, radioIndex2)
{
	if (radioIndex1 != -1)
		index1 = radioIndex1;
	
	if (radioIndex2 != -1)
		index2 = radioIndex2;

	var TempIsCurrentDatePeriod = document.getElementById('IsCurrentDatePeriod');
	
	var agent = window['agentLoggedIn'];
	
	if (TempIsCurrentDatePeriod != null)
	{
		if ((TempIsCurrentDatePeriod.value == "true") ||
			(TempIsCurrentDatePeriod.value == "True"))
			TotalPackageCost(radioIndex1, radioIndex2);
		else
		{
			if ((index1 != -1) && (index2 != -1))
			{
				if (!agent)
				{			
					document.getElementById("GrandTotal").childNodes[0].nodeValue = window['outsideDateRangeMessage'];
				}
				else
				{
					document.getElementById("GrandTotal").childNodes[0].nodeValue = window['outsideDateRangeMessage'];
					document.getElementById("AgentCommission").childNodes[0].nodeValue = window['outsideDateRangeMessage'];				
				}
			}
			else
			{
				if (!agent)
				{
					document.getElementById("GrandTotal").childNodes[0].nodeValue = window['outsideDateRangeMessage'];
				}
				else
				{
					document.getElementById("GrandTotal").childNodes[0].nodeValue = window['outsideDateRangeMessage'];
					document.getElementById("AgentCommission").childNodes[0].nodeValue = window['outsideDateRangeMessage'];				
				}
			}
		}
	}
}

function SelectDateAndUpdateHiddenControl(monthYearSelect, daySelect)
{
	selectDate(monthYearSelect, daySelect, null);
	
	var dateResult = IsDateWithinCurrentTimePeriod(monthYearSelect, daySelect);
	
	var TempIsCurrentDatePeriod = document.getElementById('IsCurrentDatePeriod');
		
	if (TempIsCurrentDatePeriod != null)
		TempIsCurrentDatePeriod.value = dateResult;
}
	
function IsDateWithinCurrentTimePeriod(monthYearSelect, daySelect)
{
	var sSelectedMonth = monthYearSelect[monthYearSelect.selectedIndex].value;
	
	var nDay = daySelect[daySelect.selectedIndex].value;
	var nMonth = sSelectedMonth.split('/')[0];
	var nYear = sSelectedMonth.split('/')[1];
	
	var selectedDate = new Date(nYear, nMonth - 1, nDay);
	var currentDate = new Date();
	var firstOfOctCurrentYearDate = new Date(currentDate.getFullYear(), 10 - 1, 1);
	
	var a = firstOfOctCurrentYearDate.valueOf() > currentDate.valueOf();
	var b = selectedDate.valueOf() >= firstOfOctCurrentYearDate.valueOf();

	if ((a == true) && (b == true))
		return false;
	else
		return true;
}

function GetArrivalDateStatus(monthYearSelect, daySelect, priceRollOverMonth)
{
	var sSelectedMonth = monthYearSelect[monthYearSelect.selectedIndex].value;
	
	var nDay = daySelect[daySelect.selectedIndex].value;
	var nMonth = sSelectedMonth.split('/')[0];
	var nYear = sSelectedMonth.split('/')[1];
	
	var selectedDate = new Date(nYear, nMonth - 1, nDay);
	var currentDate = new Date();
	
	var interval1 = new Date(currentDate.getFullYear(), priceRollOverMonth -1, 1)
	var interval2 = new Date(currentDate.getFullYear() + 1, priceRollOverMonth-1, 1);
	var interval3 = new Date(currentDate.getFullYear() + 2, priceRollOverMonth-1, 1);

	// 0 = none, 1 = current, 2=next
	var nPricingPeriod = 0;

	if (currentDate.valueOf() < interval1.valueOf())
	{
		if (selectedDate.valueOf() < interval1.valueOf())
		{
			//current pricing period
			nPricingPeriod = 1;
		}
		else if (selectedDate.valueOf() < interval2.valueOf())
		{
			//next pricing period
			nPricingPeriod = 2;
		}
		else
		{
			//no pricing period
			nPricingPeriod = 0;
		}
	}
	else if (currentDate.valueOf() < interval2.valueOf())
	{
		if (selectedDate.valueOf() < interval2.valueOf())
		{
			//current pricing period
			nPricingPeriod = 1;
		}
		else if (selectedDate.valueOf() < interval3.valueOf())
		{
			//next pricing period
			nPricingPeriod = 2;
		}
		else
		{
			//no pricing period
			nPricingPeriod = 0;
		}
	}
	else
	{
		//no pricing period
		nPricingPeriod = 0;
	}
	
	
	return nPricingPeriod;
}

function ClearRadioGroupSelection(groupName)
{
	var radioArray = document.getElementsByName(groupName)
	
	for (i=0; i < radioArray.length; i++)
	{
		radioArray[i].checked = false;
	}
}