// JavaScript Functions used by the following pages
//            tn3270/prices.htm page
//            tn3270/upgradeprices.htm                     
//            tn3270/weborder.aspx 
//
// computePrice - Calculates the price on the price form 
// computeUpgradePrice - computes the price on the upgrade price form 
//
// Amendments
// 12/01/2009 - Maintenance is required for all orders, not just orders
//              greater than or equal to 100 copies 
// 12/09/2009 - ComputePrice - Add unitprice, subtotal, maintenance amount and 
//                             procsessing fee field filling.
// 12/21/2009 - Implement smooth price curve
// 12/21/2009 - Add currency symbol

// Global Variables

// Feature Prices
currencySymbol = '$';
exchangeRate = 1;

telnetBasePrice = 25 * exchangeRate;
tn3270Price = 15 * exchangeRate;
tn5250Price = 15 * exchangeRate;
vtANSIPrice = 15 * exchangeRate;
sslPrice =    40 * exchangeRate;
sshPrice =    10 * exchangeRate;
printPrice =  10 * exchangeRate;
ftpPrice =    10 * exchangeRate;
fullPackagePrice = 95;

processingFee = 35;


// JavaScript Function used by the prices.htm page
//
// computePrice - Calculates the price on the price form 
// computeUpgradePrice - computes the price on the upgrade price form 
//
function computePrice(form)
{
	amount = telnetBasePrice
	quantity = eval(form.Quantity.value);
	
	var ERROR_NO_TERMINAL_TYPE = "You must select a least one terminal emulation."

	// Insure at least one terminal emulation is checked
	if 	(eval(form.TN3270.checked == false) &&
		 eval(form.TN5250.checked == false) &&
		 eval(form.VTANSI.checked == false) &&
		 eval(form.FullPackage.checked == false))
	{
	    // blank out all calculated fields
	    if (form.UnitPrice != null) form.UnitPrice.value = ""; 
        if (form.Subtotal != null) form.Subtotal.value = ""; 
        if (form.MaintenancePlan != null) form.MaintenancePlan.value = ""; 
	    if (form.ProcessingFee != null) form.ProcessingFee.value = "";
	    form.TotalPrice.value = "";
	    
	    // Do not issue error if quantity form field has focus. 
        if (document.activeElement)
             if (document.activeElement.name == "Quantity")
                 return(true);
        else
        {    
             // Error - You must select at least one terminal emulation. 
	         alert (ERROR_NO_TERMINAL_TYPE);
	         form.TN3270.focus();	         
	         return (false);
	   }
	}

	// If all features are checked, set full package on.
	if 	(eval(form.TN3270.checked == true) &&
		eval(form.TN5250.checked == true) &&
		eval(form.VTANSI.checked == true) &&	
		eval(form.Print.checked == true) &&
		eval(form.SSL.checked == true) &&
		eval(form.SSL.checked == true) &&		
		eval(form.FTP.checked == true)) 
	{
		form.FullPackage.checked = true;
	}


	if 	(eval(form.FullPackage.checked == true))
	{
		amount = fullPackagePrice;
		form.TN3270.checked = false;
		form.TN5250.checked = false;
		form.VTANSI.checked = false;
		form.Print.checked = false;
		form.SSH.checked = false;
		form.SSL.checked = false;
		form.FTP.checked = false;
		form.FullPackage.checked = true;
	}
	else
	{
		// Compute the price for selected features.	
		if (eval(form.TN3270.checked == true)) 	amount = amount + tn3270Price;
		if (eval(form.TN5250.checked == true)) 	amount = amount + tn5250Price;
		if (eval(form.VTANSI.checked == true)) 	amount = amount + vtANSIPrice;
		if (eval(form.Print.checked == true))	amount = amount + printPrice;
		if (eval(form.SSL.checked == true))	amount = amount + sslPrice;
		if (eval(form.SSH.checked == true))	amount = amount + sshPrice;
		if (eval(form.FTP.checked == true))	amount = amount + ftpPrice;
	}	
	
	// Compute total price with quantity discount
	subtotal = computeTotalPrice(quantity, amount);
	unitPrice = (subtotal / quantity);
	subtotal = Math.ceil(subtotal);                 // round up
	// unitPrice = Math.round((subtotal / quantity) * 10000)/10000;
	
	
	// Compute maintenance price
	maintenance = subtotal * .15;
	maintenance = Math.ceil(subtotal * .15);        // round up
	if (maintenance < 100)
		maintenance = 100;
		
	// Compute Total Price
	totalPrice = subtotal + maintenance;
	
	// Add processing fee
	if (form.ProcessingFee != null)
	{   
	    fee = 0;
	    if (totalPrice < 300) fee += processingFee;
	               
	    totalPrice += fee;
	    if (fee.toFixed) fee = fee.toFixed(2);
	    form.ProcessingFee.value = '$' + fee;
	}
	
	totalPrice = Math.ceil(totalPrice);             // round up
	// totalPrice = Math.round(totalPrice*100)/100; 
	
	if (totalPrice != parseFloat(totalPrice))
		totalPrice = 0;
	if (totalPrice.toFixed) 		//if browser supports toFixed() method
	{
		unitPrice = unitPrice.toFixed(2);
		subtotal = subtotal.toFixed(2);
		maintenance = maintenance.toFixed(2);
		totalPrice = totalPrice.toFixed(2);
	}
	
	// Update the form
	if (form.UnitPrice != null)
		form.UnitPrice.value = currencySymbol + unitPrice; 
    if (form.Subtotal != null)
        form.Subtotal.value = currencySymbol + subtotal; 
    if (form.MaintenancePlan != null)
        form.MaintenancePlan.value = currencySymbol + maintenance;   
	form.TotalPrice.value = currencySymbol + totalPrice; 
}


function computeUpgradePrice(form)
{
	
	// Set quantity discount levels
	quantity = eval(form.Quantity.value);
			
	// amount = telnetBasePrice/2;
	amount = 0;

	/*		
	// Validate selections
	if (eval(form.TN3270.checked == true) && eval(form.TN3270Upgrade.checked == true))
		form.TN3270Upgrade.checked = false;
	if (eval(form.TN5250.checked == true) && eval(form.TN5250Upgrade.checked == true))
		form.TN5250Upgrade.checked = false;
	if (eval(form.VTANSI.checked == true) && eval(form.VTANSIUpgrade.checked == true))
		form.VTANSIUpgrade.checked = false;
	if (eval(form.SSL.checked == true) && eval(form.SSLUpgrade.checked == true))
		form.SSLUpgrade.checked = false;
	if (eval(form.SSH.checked == true) && eval(form.SSHUpgrade.checked == true))
		form.SSHUpgrade.checked = false;		
	if (eval(form.Print.checked == true) && eval(form.PrintUpgrade.checked == true))
		form.PrintUpgrade.checked = false;
	if (eval(form.FTP.checked == true) && eval(form.FTPUpgrade.checked == true))
		form.FTPUpgrade.checked = false;
	*/

	// Enable selected prices
   	// form.TelnetBasePrice.disabled = !form.TelnetBase.checked;
	// form.TN3270Price.disabled = !form.TN3270.checked;
	// form.TN5250Price.disabled = !form.TN5250.checked;
	// form.VTANSIPrice.disabled = !form.VTANSI.checked;
	// form.SSLPrice.disabled =    !form.SSL.checked;
	// form.SSHPrice.disabled =    !form.SSH.checked;
	// form.PrintPrice.disabled =  !form.Print.checked;
	// form.FtpPrice.disabled =    !form.FTP.checked;
	
	// form.TN3270UpgradePrice.disabled = !form.TN3270Upgrade.checked;
	// form.TN5250UpgradePrice.disabled = !form.TN5250Upgrade.checked;
	// form.VTANSIUpgradePrice.disabled = !form.VTANSIUpgrade.checked;	
	// form.SSLUpgradePrice.disabled =    !form.SSLUpgrade.checked;
	// form.SSHUpgradePrice.disabled =    !form.SSHUpgrade.checked;
	// form.PrintUpgradePrice.disabled =  !form.PrintUpgrade.checked;
	// form.FtpUpgradePrice.disabled =    !form.FTPUpgrade.checked;
	
	// Compute update amounts
	/*
	if (eval(form.TN3270.checked == true)) amount = amount + tn3270Price/2;
	if (eval(form.TN5250.checked == true)) amount = amount + tn5250Price/2;
	if (eval(form.VTANSI.checked == true)) amount = amount + vtANSIPrice/2;
	if (eval(form.SSL.checked == true))    amount = amount + sslPrice/2;
	if (eval(form.SSH.checked == true))    amount = amount + sshPrice/2;
	if (eval(form.Print.checked == true))  amount = amount + printPrice/2;
	if (eval(form.FTP.checked == true))    amount = amount + ftpPrice/2;
	*/
	
	// Compute additional feature amounts.
	if (eval(form.TN3270Upgrade.checked == true)) amount = amount + tn3270Price;
	if (eval(form.TN5250Upgrade.checked == true)) amount = amount + tn5250Price;
	if (eval(form.VTANSIUpgrade.checked == true)) amount = amount + vtANSIPrice;
	if (eval(form.SSLUpgrade.checked == true))    amount = amount + sslPrice;		
	if (eval(form.SSHUpgrade.checked == true))    amount = amount + sshPrice;
	if (eval(form.PrintUpgrade.checked == true))  amount = amount + printPrice;
	if (eval(form.FTPUpgrade.checked == true))    amount = amount + ftpPrice;
	
	/*		
	// Compute Full Package Discount
	if 	(eval(form.TN3270.checked == true) &&
		eval(form.TN5250.checked == true) &&
		eval(form.SSL.checked == true) &&	
		eval(form.SSH.checked == true) &&	
		eval(form.Print.checked == true) &&	
		eval(form.FTP.checked == true))
	{
		amount = fullPackagePrice/2;
	}
	*/
	
	// Compute feature prices with quantity discount
	subtotal = computeTotalPrice(quantity, amount);
	
	/*
	tn3270UpgradePrice = discount * tn3270Price;
	tn5250UpgradePrice = discount * tn5250Price;
	vtANSIUpgradePrice = discount * vtANSIPrice;
	sslUpgradePrice =    discount * sslPrice;
	sshUpgradePrice =    discount * sshPrice;
	printUpgradePrice =  discount * printPrice;	
	ftpUpgradePrice =    discount * ftpPrice;

	telnetBasePrice = discount * (telnetBasePrice/2);
	tn3270Price = discount * (tn3270Price/2);
	tn5250Price = discount * (tn5250Price/2);
	vtANSIPrice = discount * (vtANSIPrice/2);
	printPrice =  discount * (printPrice/2);
	sslPrice =    discount * (sslPrice/2);
	sshPrice =    discount * (sshPrice/2);
	ftpPrice =    discount * (ftpPrice/2);

	// Compute Unit Price
	unitPrice = amount * discount
		
	// Compute Subtotal
	subTotal = amount * quantity * discount;
	if (subTotal != parseFloat(subTotal)) subTotal = 0;
	*/
	
	// Compute Total Price
	totalPrice = subtotal;
	totalPrice = Math.round(totalPrice*100)/100;
	// unitPrice = Math.round(unitPrice*1000)/1000;
	
	// Determine the number of decimal digits to display.
	/*
	significantDigits = 2
	if (Math.floor(telnetBasePrice * 100) != Math.ceil(telnetBasePrice * 100))
		significantDigits = 3;
	if (Math.floor(tn3270Price * 100) != Math.ceil(tn3270Price * 100))
		significantDigits = 3;
	if (Math.floor(printPrice * 100) != Math.ceil(printPrice * 100))
		significantDigits = 3;
	*/
		
	if (totalPrice.toFixed) 		//if browser supports toFixed() method
	{
	    /*
		telnetBasePrice = telnetBasePrice.toFixed(significantDigits);
		tn3270Price =     tn3270Price.toFixed(significantDigits);
		tn5250Price =     tn5250Price.toFixed(significantDigits);
		vtANSIPrice =     vtANSIPrice.toFixed(significantDigits);
		sslPrice =        sslPrice.toFixed(significantDigits);
		sshPrice =        sshPrice.toFixed(significantDigits);	 	 
		printPrice =      printPrice.toFixed(significantDigits);
		ftpPrice =        ftpPrice.toFixed(significantDigits);

		tn3270UpgradePrice = tn3270UpgradePrice.toFixed(significantDigits);
		tn5250UpgradePrice = tn5250UpgradePrice.toFixed(significantDigits);
		vtANSIUpgradePrice = vtANSIUpgradePrice.toFixed(significantDigits);
		sslUpgradePrice =    sslUpgradePrice.toFixed(significantDigits);
		sshUpgradePrice =    sshUpgradePrice.toFixed(significantDigits);
		printUpgradePrice =  printUpgradePrice.toFixed(significantDigits);
		ftpUpgradePrice =    ftpUpgradePrice.toFixed(significantDigits);
	
		unitPrice = unitPrice.toFixed(significantDigits)
		*/
		totalPrice = totalPrice.toFixed(2);
	}
	// Display Prices
	/*
	form.TelnetBasePrice.value = '$' + telnetBasePrice;
	form.TN3270Price.value =     '$' + tn3270Price;
	form.TN5250Price.value =     '$' + tn5250Price;
	form.VTANSIPrice.value =     '$' + vtANSIPrice;
	form.SSLPrice.value =        '$' + sslPrice;
	form.SSHPrice.value =        '$' + sshPrice;
	form.PrintPrice.value =      '$' + printPrice;
	form.FtpPrice.value =        '$' + ftpPrice;

	form.TN3270UpgradePrice.value = '$' + tn3270UpgradePrice;
	form.TN5250UpgradePrice.value = '$' + tn5250UpgradePrice;
	form.VTANSIUpgradePrice.value = '$' + vtANSIUpgradePrice;
	form.SSLUpgradePrice.value =    '$' + sslUpgradePrice;
	form.SSHUpgradePrice.value =    '$' + sshUpgradePrice;
	form.PrintUpgradePrice.value =  '$' + printUpgradePrice;
	form.FtpUpgradePrice.value =    '$' + ftpUpgradePrice;
	form.FullPackagePrice.value ='$' + fullPackagePrice;
	
 	form.UnitPrice.value = '$' + unitPrice;
 	*/
 	
	form.TotalPrice.value = '$' + totalPrice; 
}


// ------------------------------------------------------------------------- 
// Helper functions
// ------------------------------------------------------------------------- 
function computeTotalPrice(quantity, amount)
{
	subtotal = 0;
	currentQuantity = quantity;    
	if (currentQuantity > 9999) 
	{
		subtotal += (currentQuantity - 9999) * (amount * .05);
		currentQuantity = 9999;
	}
	if (currentQuantity > 999)
	{
		subtotal += (currentQuantity - 999) * (amount * .1);
		currentQuantity = 999;
	}
	if (currentQuantity > 249) 
	{
		// subtotal += (currentQuantity - 249) * (amount * .4);
		subtotal += (currentQuantity - 249) * (amount * .35);
		currentQuantity = 249;
	}
	if (currentQuantity > 99) 
	{
		// subtotal += (currentQuantity - 99) * (amount * .75);		
		subtotal += (currentQuantity - 99) * (amount * .50);
		currentQuantity = 99;
	}
	if (currentQuantity > 49)  
	{
		subtotal += (currentQuantity - 49) * (amount * .85);
		currentQuantity = 49;
	}
	if (currentQuantity > 14) 
	{
		subtotal += (currentQuantity - 14) * (amount * .93);
		currentQuantity = 14;
	}
	
	subtotal += (currentQuantity * amount);
	// alert ("amount = "  + amount + " quantity = " + quantity + " subtotal = " + subtotal);	
	return subtotal;
}