/**
 * This function will calculate the amex fee according to the percentage given as argument
 * and the price selected in the form.
 * It should be called when there is a change in the selection of the price drop down menu.
 */

function calculateAmexFee (AmexPercentage)
{

	var PriceAndPeriod = document.paymentform.PriceAndPeriod.value;

	var _AmexFee;
	var _TotalTransac;

	var priceArray = PriceAndPeriod.split("|");
	var price = parseFloat (priceArray[0]);


	_AmexFee = (price * AmexPercentage) / 100;
	_TotalTransac = price + _AmexFee;

	// This is a trick to round this numbers to two decimal digits:
	_AmexFee = Math.round (_AmexFee * 100) / 100.00;
	_TotalTransac = Math.round (_TotalTransac * 100) / 100.00;

	document.paymentform.AmexCharge.value = _AmexFee;
	document.paymentform.TotalTransaction.value = _TotalTransac;

}


/**
 * This function will display a layer which  prints the Amex Fee fields
 * when Amex is selected.
 */

function AmexFee (AmexPercentage)
{

	var paymenttype = document.paymentform.paymenttype;
	var creditcard = document.paymentform.cboCreditCard.value;
	var defcctype = (document.paymentform.defcctype) ? document.paymentform.defcctype.value : '0';

	var iState = 0;

	if (((paymenttype.type == "hidden" || (paymenttype.type != "hidden" && paymenttype.value == 1)) && creditcard == 4) || (paymenttype.value == 0 && defcctype == 4))
	{
		iState = 1;
		alert ("Please note you will be charged a " + AmexPercentage + "% fee when paying with Amex");
	}

	if (blToggleBoxes != null && blToggleBoxes == 1)
	{
		ToggleAmexBoxes (iState, "AmexCharge1");
		ToggleAmexBoxes (iState, "AmexCharge2");
		ToggleAmexBoxes (iState, "AmexCharge3");
		ToggleAmexBoxes (iState, "AmexCharge4");
	}

}


/**
 * Function to display or hide layers according to current status (iState).
 */

function ToggleAmexBoxes (iState, _DivID)
{
	if (document.layers)	   //NN4+
	{
		document.layers[_DivID].visibility = iState ? 'show' : 'hide';
	}
	else if (document.getElementById)	  //gecko(NN6) + IE 5+
	{
		var obj = document.getElementById (_DivID);
		obj.style.visibility = iState ? 'visible' : 'hidden';
	}
	else if(document.all)		// IE 4
	{
		document.all[szDivID].style.visibility = iState ? 'visible' : 'hidden';
	}
}


/**
 * Main function to check date of payment form.
 */

function PaymentChecker()
{

	var PriceAndPeriod = document.paymentform.PriceAndPeriod;

	if (PriceAndPeriod.type == "select-one")
	{
		if (PriceAndPeriod.value == "#")
		{
			alert("You must select a value for the order here.\nPlease try again.");
			document.paymentform.PriceAndPeriod.focus();
			return false;
		}
		else
		{
			var price;
			price = PriceAndPeriod.value.substring(0,PriceAndPeriod.value.indexOf("|"));

			if (parseInt(price) == 0)
			{
				document.paymentform.cboCreditCard.value = "#";
				document.paymentform.txtCcName.value = null;
				document.paymentform.txtCcNumber.value = null;
				document.paymentform.txtCVV.value = null;
				document.paymentform.ccmonth.value = "#";
				document.paymentform.ccyear.value = "#";

				alert ("You have selected a free product.\nThere is no need to enter your credit card details.\nYour order will be entered now.");
				return;
			}
		}
	}

	var paymenttype = document.paymentform.paymenttype;
	var creditcard = document.paymentform.cboCreditCard.value;
	var ccname = document.paymentform.txtCcName.value;
	var ccnumber = document.paymentform.txtCcNumber.value;
	var cvv = document.paymentform.txtCVV.value;
	var ccmonth = document.paymentform.ccmonth.value;
	var ccyear = document.paymentform.ccyear.value;

	if (paymenttype.value == "#")
	{
		alert ("You must select a type of payment.\nPlease try again.");
		document.paymentform.paymenttype.focus();
		return false;
	}

	/* If the payment type field is hidden, the credit card details must be entered.
	From the Domain Shop there will only be one payment option for the end user (credit card),
	but the paymenttype for the order will be either "invoice" or "prepayment".
	From TPP's OMS there will be more than one payment option:
	credit card and something else ("invoice" or "prepayment") for resellers and corporate clients.
	In this case, we only need to ensure the credit card details are entered if the end user selected "credit card".
	From TPP's website and OMS there will always be only "credit card" payment option.
	*/

	if (paymenttype.value != 0 && (paymenttype.type == "hidden" || (paymenttype.type != "hidden" && paymenttype.value == 1)))
	{

		if (creditcard == "#")
		{
			alert("You must select a Credit Card.\nPlease try again.");
			document.paymentform.cboCreditCard.focus();
			return false;
		}

		if (isNaN(ccnumber) || (ccnumber.length!=16 && creditcard!="4") || (ccnumber.length!=15 && creditcard=="4"))
		{

			alert("You must enter a Valid Credit Card Number (16 digits or 15 digits for Amex).\nPlease try again.");
			document.paymentform.txtCcNumber.focus();
			return false;
		}

		if (ccname == "")
		{
			alert("You must enter a Name.\nPlease try again.");
			document.paymentform.txtCcName.focus();
			return false;
		}


		if (cvv == "" || isNaN(cvv) || ((cvv.length!=4 && creditcard=="4")) || ( cvv.length != 3 && creditcard != "4") )
		{
			if (creditcard=="4")
			{
				strCorrectRange = "4";
			}
			else
			{
				strCorrectRange = "3";
			}
    		alert("You must enter a Valid Card Verification Value (" + strCorrectRange + " digits).\nPlease try again.");
    		document.paymentform.txtCVV.focus();
    		return false;
		}

		if (ccmonth=="#")
		{
			alert("You must select a Month.\nPlease try again.");
			document.paymentform.ccmonth.focus();
			return false;
		}

		if (ccyear=="#")
		{
			alert("You must select a Year.\nPlease try again.");
			document.paymentform.ccyear.focus();
			return false;
		}
	}

}

