
/*	
	**************************************
	            onItemValueChange function
	**************************************

	Responds when user makes or changes a selection 
	or changes a value (amount).
	
	External Functions Called
	1. checkExpired (check TRANSACTION has expired)
	2. checkExclusive(changed item index)
	3. verifyRequired
	4. setupPayments
	5. RecordItemSelections
          6. deselectCheckedItem(pItemIndex)	
	
	Internal (in this file) Functions
	1. itemValuesSum()

*/

/*
	----------------------------------------
	                 Globals
	----------------------------------------
*/
var sInputFieldName = '';
var oInputField = null;
var dInput = 0;	

var sValueFieldName = '';
var oValueField = null;
var dValue = 0;

var sQtyFieldName = '';
var oQtyField = null;
var dQty = 0;

var sValue = '';

var bPaymentIsSetup = false;

var oCreditCardDIV = null;

//add REGEX string replace functionality
String.prototype.replaceAll=function(s1, s2) 
{ return this.replace(new RegExp(s1,"g"), s2); }

/*
	----------------------------------------
	         onItemValueChange
	----------------------------------------
*/	

function onItemValueChange(pItemIndex)
{
//alert('entering function onItemValueChange')
	var bErrMsg = true;
	
	var bTest = false;
	var iItemIndex = (1*pItemIndex);
	pItemIndex = pItemIndex;

//if(bErrMsg){alert('function setup ok')};	
/*	
	don't do it if the transaction has expired
	or if user has not provided her info, checked by 
	functions checkExpired and verifyRequired
	respectively.
*/
	if(checkExpired(true))
	{
		return false;
	}
	
//if(bErrMsg){alert('after expiration check')};	

//	make sure all personal info items are filled in
/**/	
	if( verifyRequired() == false )
	{
		bTest = deselectCheckedItem(iItemIndex);
		return false;
	}

//if(bErrMsg){alert('before price')};	

	sValueFieldName = 'Item' + pItemIndex + 'Price';
	
	oValueField = document.getElementById(sValueFieldName);
	dValue = oValueField.value;	
	
//if(bErrMsg){alert('get element by id ' + sValueFieldName + ': ' + dValue)};

//if(bErrMsg){alert('after price')};	

	sQtyFieldName = 'Item' + pItemIndex + 'Qty';
	oQtyField =  document.getElementById(sQtyFieldName);
	dQty = 0;	
	
//if(bErrMsg){alert('before input')};	

	sInputFieldName = 'Item' + pItemIndex + 'Input';
	oInputField = document.getElementById(sInputFieldName);
	dInput = oInputField.value; 
	
	var bDoCheckExclusive = false;

//if(bErrMsg){alert('after input')};	
	
//	handle checkbox status	
	if(dInput == "on")
	{
//		if it is checked then uncheck mutually exclusive items		
		dInput = oInputField.checked
		if(dInput)
		{
			bDoCheckExclusive = true;
		}
	} 
	else if(dValue == 0)
	{

//alert('found text box');	
		dInput = dInput.replaceAll("[^0-9,.]", "");
//alert('text box after replace, dInput = ' + dInput);		
		oInputField.value = dInput;
		
		if(dInput == '')
		{
			dInput = false;
		}
	} // end if checkbox or textbox

	if(dInput)
	{
		dQty = 1;
		oQtyField.value = dQty;
	}
	else
	{
		dQty = 0;
		oQtyField.value = dQty;			
	} // end if updating quantity

//if(bErrMsg){alert('before check exclusive')};

//alert('on item change,\n dInput = ' + dInput + '\n dValue = ' + dValue + '\n Qty ' + dQty + '\n  Check Exclusive = ' + bDoCheckExclusive);	
	if(bDoCheckExclusive)
	{
		bTest = checkExclusive(iItemIndex);
	}

//if(bErrMsg){alert('before items sum')};

//	sum items -- (price *Qty) + taxes	
	bTest = itemValuesSum();
	
//if(bErrMsg){alert('before RecordItemSelections')};	

//	makes ajax transaction with updated list of items and charges	
	bTest = RecordItemSelections();	

//	add payment buttons and links, if needed	
	if( bPaymentIsSetup == false)
	{
		bPaymentIsSetup = setupPayments();
	}
	else
	{
	
//	get/set link for click and pledge with current total	
	sInputFieldName = 'CreditCardLinkDIV';
	oCreditCardLinkDIV = document.getElementById(sInputFieldName);

	sValue = setupClickAndPledgeURL();

	oCreditCardLinkDIV.innerHTML = sValue;	
	}
	
} // end function onItemValueChange

/*	
	--------------------------------------------------
	            itemValuesSum function
	-------------------------------------------------

	External Variables Used by function itemValuesSum
	dTax1RateCategory1
	dTax1RateCategory2
	
	dTax2RateCategory1
	dTax2RateCategory2	
*/
function itemValuesSum()
{
	var dTotal = 0;
	var dTotalOther = 0;
 
//	variables for SEQUENTIAL item naming	
	var iItemIndex = 0;

	var dBeforeTaxCategory1 = 0;
	var dBeforeTaxCategory2 = 0;
	var dBeforeTaxTotal = 0;	
	
	var dTaxType1Category1 = 0;
	var dTaxType1Category2 = 0;
	var dTaxType1Total = 0;
	
	var dTaxType2Category1 = 0;
	var dTaxType2Category2 = 0;
	var dTaxType2Total = 0;
	
	var dTotalWithTaxCategory1 = 0;
	var dTotalWithTaxCategory2 = 0;
	var dTotalWithTax = 0;		

	var dValueCategory1 = 0;
	var dValueCategory2 = 0;
	
	var bTest = false;

//alert('done setting up, in itemValuesSum');

// 	setup for looping items	
	iItemIndex = 1;
	sValueFieldName = 'Item1Price';
	oValueField = document.getElementById(sValueFieldName);
	
	while(oValueField)
	{

//sMsg = 'loop # '+ iItemIndex;
//alert(sMsg);

		sQtyFieldName = 'Item' + iItemIndex + 'Qty';
		oQtyField = document.getElementById(sQtyFieldName);
		dQty = oQtyField.value;
		
		if(dQty > 0)
		{
//sMsg = 'found item @ #' + iItemIndex;
//alert(sMsg);
/*			
			If check box value is in the value (hidden) input ItemNValue
			If a textbox value is in the input (text type)  ItemNInput
			
			Detect latter by looking at the PRICE field.  
			If = 0 then the value is in the INPUT (text type) field.
*/
			dValue = oValueField.value;
			if(dValue == 0)
			{
				sInputFieldName = 'Item' + iItemIndex + 'Input';
				oInputField = document.getElementById(sInputFieldName);
				dValue = (1 * oInputField.value);
				dTotalOther = dTotalOther + dValue;
			}
			else
			{
				sValueFieldName = 'Item' + iItemIndex + 'PriceCategory1';
				oValueField = document.getElementById(sValueFieldName);
				
				// portion in category one? if so the field exists for category 1 part
				if(oValueField) 
				{	
					dTmp = (1 * oValueField.value);
					dBeforeTaxCategory1 = dBeforeTaxCategory1 + (dTmp * dQty);
				}
				else // otherwise all ofthe subtotal goes into in category one
				{
					dBeforeTaxCategory1 = dBeforeTaxCategory1 + (dValue * dQty);
				}
				
				sValueFieldName = 'Item' + iItemIndex + 'PriceCategory2';
				oValueField = document.getElementById(sValueFieldName);
				
				if(oValueField)
				{	
					dTmp = (1 * oValueField.value);
					dBeforeTaxCategory2 = dBeforeTaxCategory2 + (dTmp * dQty);
				}				
			}

			dBeforeTaxTotal = dBeforeTaxTotal + ( dValue * (1*dQty) );
			
		} // end if

//		look-ahead to next possible item, for do next loop check		
		++iItemIndex;		
		sValueFieldName = 'Item' + iItemIndex + 'Price';
		oValueField = document.getElementById(sValueFieldName);
		
	} // end while loop
	
//sMsg = 'found items count ' + iItemIndex;
//alert(sMsg);		

	--iItemIndex;	
	
//	>>> calculate category taxes <<<
	dTaxType1Category1 = dBeforeTaxCategory1  * dTax1RateCategory1;
	//dTaxType1Category1 = dTaxType1Category1.toFixed(2);
		
	dTaxType2Category1 = dBeforeTaxCategory1 * dTax2RateCategory1;
	//dTaxType2Category1 = dTaxType2Category1.toFixed(2);
	
	dTaxType1Category2 = dBeforeTaxCategory2 * dTax1RateCategory2;
	//dTaxType1Category2 = dTaxType1Category2.toFixed(2);
	
	dTaxType2Category2 = dBeforeTaxCategory2 * dTax2RateCategory2;
	//dTaxType2Category2 = dTaxType2Category2.toFixed(2);

//	>>> taxes totals <<<	
	dTaxType1Total = dTaxType1Category1 + dTaxType1Category2;
	dTaxType2Total = dTaxType2Category1 + dTaxType2Category2;
	
//	>>> calculate totals with taxes <<<	
	dTotalWithTaxCategory1 = dBeforeTaxCategory1 + dTaxType1Category1 + dTaxType2Category1;
	
	dTotalWithTaxCategory2 = dBeforeTaxCategory2 + dTaxType1Category2 + dTaxType2Category2;

//	>>> final total <<<
	dTotalWithTax = dTotalWithTaxCategory1 + dTotalWithTaxCategory2 + dTotalOther;	

//	>>> onscreen labels reflecting values <<<

//sMsg = 'before handling labels ' + iItemIndex;
//alert(sMsg);	

//	-- category 1 ---
	sValueFieldName = 'Category1SubtotalValue';
	oValueField = document.getElementById(sValueFieldName);
	oValueField.value = '$' + dBeforeTaxCategory1.toFixed(2);

//sMsg = 'in category 1';
//alert(sMsg);

	sValueFieldName = 'Category1Tax1Value';
	oValueField = document.getElementById(sValueFieldName);
	oValueField.value = '$' + dTaxType1Category1.toFixed(2);

	sValueFieldName = 'Category1Tax2Value';
	oValueField = document.getElementById(sValueFieldName);
	oValueField.value = '$' +  dTaxType2Category1.toFixed(2);

	sValueFieldName = 'Category1TotalValue';
	oValueField = document.getElementById(sValueFieldName);
	oValueField.value = '$' + dTotalWithTaxCategory1.toFixed(2);
	
//	-- category 2 ---

	sValueFieldName = 'Category2SubtotalValue';
	oValueField = document.getElementById(sValueFieldName);
	oValueField.value = '$' + dBeforeTaxCategory2.toFixed(2);
	
	sValueFieldName = 'Category2Tax1Value';
	oValueField = document.getElementById(sValueFieldName);
	oValueField.value = '$' + dTaxType1Category2.toFixed(2);

	sValueFieldName = 'Category2Tax2Value';
	oValueField = document.getElementById(sValueFieldName);
	oValueField.value = '$' + dTaxType2Category2.toFixed(2);

	sValueFieldName = 'Category2TotalValue';
	oValueField = document.getElementById(sValueFieldName);
	oValueField.value = '$' + dTotalWithTaxCategory2.toFixed(2);	

//	--- other items totals ---
	
	sValueFieldName = 'OtherItemsValueLabel';
	oValueField = document.getElementById(sValueFieldName);
	oValueField.value = '$' + dTotalOther.toFixed(2);	

	
//	--- pretax subtotal and tax types totals ---

//	sutotal before tax	
	sValueFieldName = 'SelectionsTotalLabel';
	oValueField = document.getElementById(sValueFieldName);
	
	oValueField.value = '$' + dBeforeTaxTotal.toFixed(2);	

//	tax type 1 total		
	sValueFieldName = 'Tax1TotalLabel';
	oValueField = document.getElementById(sValueFieldName);
	
	oValueField.value = '$' + dTaxType1Total.toFixed(2);		

//	tax type 2 total		
	sValueFieldName = 'Tax2TotalLabel';
	oValueField = document.getElementById(sValueFieldName);
	
	oValueField.value = '$' + dTaxType2Total.toFixed(2);

//	final total
	sValueFieldName = 'TotalChargesLabel';
	oValueField = document.getElementById(sValueFieldName);
	
	oValueField.value = '$' + dTotalWithTax.toFixed(2);

//sMsg = 'before filling hidden variables ';
//alert(sMsg);
	
//	--- put values into hidden variables usedforprocessing ---
//	(the others set above are primarly read only or labels) 

	sValueFieldName = 'SelectionsTotalValue';
	oValueField = document.getElementById(sValueFieldName);
	oValueField.value = dBeforeTaxTotal.toFixed(2);	

	sValueFieldName = 'Tax1TotalValue';
	oValueField = document.getElementById(sValueFieldName);
	oValueField.value = dTaxType1Total.toFixed(2);

	sValueFieldName = 'Tax2TotalValue';
	oValueField = document.getElementById(sValueFieldName);
	oValueField.value = dTaxType2Total.toFixed(2);

	sValueFieldName = 'TotalChargesValue';
	oValueField = document.getElementById(sValueFieldName);
	oValueField.value = dTotalWithTax.toFixed(2);
	
sMsg ='';	
//sMsg = 'found ' + iItemIndex + ' items \n';
//sMsg = sMsg +  'Before Tax: ' + dBeforeTaxTotal + '\n';
//sMsg = sMsg +  'Category 1: ' + dBeforeTaxCategory1 + '\n';
//sMsg = sMsg +  'Category 2: ' + dBeforeTaxCategory2 + '\n';
//sMsg = sMsg +  'Tax Type 1: ' + dTaxType1Total + '\n';
//sMsg = sMsg +  'Tax Type 2: ' + dTaxType2Total + '\n';
//sMsg = sMsg +  'Other Total: ' + dTotalOther + '\n';
//sMsg = sMsg +  'Final Total: ' + dTotalWithTax + '\n';
//alert(sMsg);

} // end function itemValuesSum
