/* FeeTax Javascript (Used by Pages that contain Fee Related Elements */

// Select/Deselect one Fee
function selectFee(isSelected, feeQty)
{
	if (feeQty) 
	{
		if (isSelected) 
		{
			if (feeQty.value=='0')
			{
				feeQty.value='1';
			}
		} 
		else 
		{
			feeQty.value='0';
		}
	}
}

// Select/Deselect all Fees
// feeIds = all Fees including self; feeQtys = all feeQtys including self
function selectAllFees(isSelected, feeIds, feeQtys)
{
	// Check and UnCheck all Fees
	if (feeIds != null)
	{
		for (var i=0; i<feeIds.length; i++)
		{
			feeIds[i].checked = isSelected;
		}
	}				
	
	// Set Qty to 1 if Fees are checked; or set Qty to 0 if Fees are unChecked
	if (feeQtys != null)
	{
		if (isSelected)
		{
			for (var j=0; j<feeQtys.length; j++)
			{
				feeQtys[j].value='1';
			}
		}
		else
		{
			for (var j=0; j<feeQtys.length; j++)
			{
				feeQtys[j].value='0';
			}						
		}
	}
}

// Change Qty of one Fee
function changeQty(feeId, feeQty)
{
	if (!isNumeric(feeQty.value)) 
	{
		alert('Please enter numbers only.');
		replaceRememberedValue(feeQty);
	} 
	else 
	{
		if (feeQty.value>0) 
		{
			feeId.checked=true;
		} 
		else 
		{
			feeId.checked=false;
		}
	}					
}

// Change Qty of one Fee and can not be below of the limitedQtyValue
// Won't allow to uncheck fee
function changeQtyLimited(feeId, feeQty, limitedQtyValue)
{
	if (!isNumeric(feeQty.value)) 
	{
		alert('Please enter numbers only.');
		replaceRememberedValue(feeQty);
	} 
	else if (feeQty.value < limitedQtyValue)
	{
		alert('You are only allowed to select more than ' + limitedQtyValue + ' fees.');
		replaceRememberedValue(feeQty);
	}
}

// Change Qty of all Fees
// feeQty is the current feeQty; feeIds = all Fees including self; feeQtys = all Qtys excluding self
function changeAllQtys(feeQty, feeIds, feeQtys)
{
	if (!isNumeric(feeQty.value)) 
	{
		alert('Please enter numbers only.');
		replaceRememberedValue(feeQty);
	} 
	else 
	{
		if (feeQty.value>0) 
		{
			if (!feeIds[0].checked)
			{
				// Check All Fees (Only if not checked b4)
				for (var i=0; i<feeIds.length; i++)
				{
					feeIds[i].checked = true;
				}							
				
				// Increase Qty of All Fees (Only if not set to > 0 b4)
				if (feeQtys != null)
				{
					for (var j=0; j<feeQtys.length; j++)
					{
						feeQtys[j].value = '1';
					}										
				}
			}
		} 
		else 
		{
			// Uncheck All Fees
			for (var i=0; i<feeIds.length; i++)
			{
				feeIds[i].checked = false;
			}								
			
			// Descrease Qty of All Fees
			if (feeQtys != null)
			{
				for (var j=0; j<feeQtys.length; j++)
				{
					feeQtys[j].value = '0';
				}										
			}								
		}
	}					
}					

// Decrease Qty of one Fee
function decreaseQty(feeId, feeQty)
{
	if (feeQty.value>0) 
	{
		feeQty.value--;
	} 
	if (feeQty.value<=0) 
	{
		feeId.checked=false;
	}
	return false;
}

// Decrease Qty of one Fee and can not be below of the limitedQtyValue
// Won't allow to uncheck fee
function decreaseQtyLimited(feeId, feeQty, limitedQtyValue)
{
	if (feeQty.value>limitedQtyValue) 
	{
		feeQty.value--;
	} 
	return false;
}

// Decrease Qty of all Fees
// feeQty is the current feeQty; feeIds = all Fees including self; feeQtys = all Qtys excluding self
function decreaseAllQtys(feeQty, feeIds, feeQtys)
{
	// Decrease Fee Qty of the selected Fee
	if (feeQty.value>0) 
	{
		feeQty.value--;
	} 
	
	// Uncheck All Fees and Decrease Qty of All Fees if necessary
	if (feeQty.value<=0) 
	{
		// Uncheck All Fees
		for (var i=0; i<feeIds.length; i++)
		{
			feeIds[i].checked = false;
		}								
		
		// Descrease Qty of All Fees
		if (feeQtys != null)
		{
			for (var j=0; j<feeQtys.length; j++)
			{
				feeQtys[j].value = '0';
			}										
		}								
	}
	return false;
}

// Increase Qty of one Fee
function increaseQty(feeId, feeQty)
{
	feeQty.value++;
	feeId.checked=true;
	return false;
}

// Increase Qty of all Fees
// feeQty is the current feeQty; feeIds = all Fees including self; feeQtys = all Qtys excluding self
function increaseAllQtys(feeQty, feeIds, feeQtys)
{
	// Increase Fee Qty of the selected Fee
	feeQty.value++;
	
	// Check All Fees and Increase Qty of All Fees if necessary
	if (!feeIds[0].checked)
	{
		// Check All Fees (Only if not checked b4)
		for (var i=0; i<feeIds.length; i++)
		{
			feeIds[i].checked = true;
		}							
		
		// Increase Qty of All Fees (Only if not set to > 0 b4)
		if (feeQtys != null)
		{
			for (var j=0; j<feeQtys.length; j++)
			{
				feeQtys[j].value = '1';
			}										
		}
	}
	return false;
}

// When user checks changes the base amount, we need to update the total amount
// In case of negative fee amount, we disable taxes
// Precondition: user input is a valid fee amount
function UpdateFee(baseamountParam,calculatedamountParam,taxoptionParam,taxRateArray,taxOptionPayableAmountArray)
{
	// Check if the basefee is a negative fee
	var isNegativeFee = false;
	if (baseamountParam.value.charAt(0) == "-")
	{
		isNegativeFee = true;
	}
	
	// update baseamount
	var baseamount = parseFloat(baseamountParam.value);
	baseamountParam.value = Number(baseamount).toFixed(2);
	
	// recalculate taxes and update taxoptionPayableAmount
	var totalTaxAmount = 0;
	var numTaxOptions = taxoptionParam.length;
	
	// there is only 1 tax
	if (numTaxOptions == undefined)
	{
		// Handle Negative Fee: Disable Tax
		//if (isNegativeFee)
		//{
		//	taxoptionParam.checked = false;
		//	taxoptionParam.disabled = true;
		//	taxOptionPayableAmountArray[0].value = "";
		//}
		// Handle Positive Fee: Update Taxes
		//else
		//{
			// undisable previously disabled tax opion
			//taxoptionParam.disabled = false;
			
			if (taxoptionParam.checked)
			{
				var taxAmount = baseamount * taxRateArray[0];
				totalTaxAmount = taxAmount;
				taxOptionPayableAmountArray[0].value = Number(taxAmount).toFixed(2);
				
			}
		//}
	}
	
	// there is multiple tax
	else
	{
		// Handle Negative Fee: Disable Tax
		//if (isNegativeFee)
		//{
		//	for (var i=0; i<numTaxOptions; i++)
		//	{
		//		taxoptionParam[i].checked = false;
		//		taxoptionParam[i].disabled = true;
		//		taxOptionPayableAmountArray[i].value = "";
		//	}
		
		//}	
		// Handle Positive Fee: Update Taxes	
		//else
		//{
			for (var i=0; i<numTaxOptions; i++)
			{
				// undisable previously disabled tax opion			
				//taxoptionParam[i].disabled = false;
				
				if (taxoptionParam[i].checked)
				{
					var taxAmount = baseamount * taxRateArray[i];
					totalTaxAmount = totalTaxAmount + taxAmount;
					taxOptionPayableAmountArray[i].value = Number(taxAmount).toFixed(2);
				}
			}
		//}
	}
	
	// calculate and update calculatedamount
	var calculatedamount = baseamount + totalTaxAmount;
	calculatedamountParam.value = Number(calculatedamount).toFixed(2);
}	

// When user checks or unchecks a tax option, we need to calculate the tax amount and calculate the total amount
// Precondition: user input is a valid fee amount
function CalculateTax(baseamountParam,calculatedamountParam,taxOption,taxRate,taxamountParam)
{
	// Handle negative fee, disable all taxes (just in case browser doesn't support disabled checkbox
	//if (baseamountParam.value.charAt(0) == "-")
	//{
	//	taxOption.checked = false;
	//	calculatedamountParam.value = "-" + baseamountParam.value;
	//}
	
	// Handle Positive Fee: Calculate Tax and update CalculatedAmount
	//else
	//{
		// update baseamount
		var baseamount = parseFloat(baseamountParam.value);
		baseamountParam.value = Number(baseamount).toFixed(2);
		
		// calculate and update taxamount
		var taxamount = taxRate * baseamount;
		taxamountParam.value = Number(taxamount).toFixed(2);
	
		// calculate and update calculatedamount
		var calculatedamount = parseFloat(calculatedamountParam.value);	
		if (taxOption.checked)
		{
			calculatedamount = calculatedamount + taxamount;
			calculatedamountParam.value = Number(calculatedamount).toFixed(2);
		}
		else
		{
			calculatedamount = calculatedamount - taxamount;
			calculatedamountParam.value = Number(calculatedamount).toFixed(2);
			taxamountParam.value = "";
		}
	//}
}
	

