function ProductQtyChange(iItemNum) {
	UpdateProductLineTotals(iItemNum);
	UpdateTotals();
}//ProductQtyChange()

function UpdateProductLineTotals(iItemNum) {
	//get values
		var iPrice = document.getElementById('Price_Hidden_' + iItemNum).value;
		var iPV = document.getElementById('BasePV_Hidden_' + iItemNum).value;
		var iCV = document.getElementById('BaseCV_Hidden_' + iItemNum).value;
		var iRewards = document.getElementById('BaseRewards_Hidden_' + iItemNum).value;
		var iQty = document.getElementById('frmSel_Qty_' + iItemNum).value;
	
	//calculate values
		var iFreeQty = iQty / 2;
		var iTotalPrice = iQty * iPrice;
		var iTotalPV = iQty * iPV;
		var iTotalCV = iQty * iCV;
		var iTotalRewards = ( parseInt(iQty) + parseInt(iFreeQty) ) * iRewards;
	
	//display values
		//free qty
			if (iFreeQty == 0) {
				document.getElementById('FreeQty_' + iItemNum).innerHTML = '--';
			} else {
				document.getElementById('FreeQty_' + iItemNum).innerHTML = iFreeQty;
			}
		//total price
			if (iTotalPrice == 0) {
				document.getElementById('TotalPrice_' + iItemNum).innerHTML = '--';
				document.getElementById('TotalPrice_Curr_' + iItemNum).style.display = 'none'; //hide $
			} else {
				document.getElementById('TotalPrice_' + iItemNum).innerHTML = (iTotalPrice*1).toFixed(2);
				document.getElementById('TotalPrice_Curr_' + iItemNum).style.display = 'inline'; //show $
			}
		//total PV & total CV
			if (iTotalPV == 0 && iTotalCV == 0) {
				document.getElementById('TotalVolume_Values_' + iItemNum).style.display = 'none';	//hide PV/CV
				document.getElementById('TotalVolume_Blank_' + iItemNum).style.display = 'inline';	//show --
				document.getElementById('TotalPV_' + iItemNum).innerHTML = '';
				document.getElementById('TotalCV_' + iItemNum).innerHTML = '';
			} else {
				document.getElementById('TotalPV_' + iItemNum).innerHTML = iTotalPV;
				document.getElementById('TotalCV_' + iItemNum).innerHTML = iTotalCV;
				document.getElementById('TotalVolume_Blank_' + iItemNum).style.display = 'none';	//hide --
				document.getElementById('TotalVolume_Values_' + iItemNum).style.display = 'inline'; //show PV/CV
			}
		//total rewards
			if (iTotalRewards == 0) {
				document.getElementById('TotalRewards_' + iItemNum).innerHTML = '--';
				document.getElementById('TotalRewards_Points_' + iItemNum).style.display = 'none'; //hide Points
			} else {
				document.getElementById('TotalRewards_' + iItemNum).innerHTML = iTotalRewards;
				document.getElementById('TotalRewards_Points_' + iItemNum).style.display = 'inline'; //show Points
			}
			
}//UpdateProductLineTotals()

function UpdateTotals() {
	//get & calculate values
		var iTotalPrice = 0;
		var iTotalPV = 0;
		var iTotalCV = 0;
		var iTotalRewards = 0;
		var iTempVar;
		
		var arrProductList_Ids = document.getElementById('ProductList_Ids').value.split(',')
		for (var i = 0; i < arrProductList_Ids.length; i++) {
			//price
				iTempVar = document.getElementById('TotalPrice_' + arrProductList_Ids[i]).innerHTML;
				if (iTempVar != '--') {
					iTotalPrice = iTotalPrice*1 + iTempVar*1;
					var oOrder_Subtotal = document.getElementById('order_subtotal');
					oOrder_Subtotal.value = iTotalPrice;
				}
			//PV
				iTempVar = document.getElementById('TotalPV_' + arrProductList_Ids[i]).innerHTML;
				if (iTempVar != '') {
					//iTotalPV = parseInt(iTotalPV) + parseInt(iTempVar);
					iTotalPV = iTotalPV*1 + iTempVar*1;
				}
			//CV
				iTempVar = document.getElementById('TotalCV_' + arrProductList_Ids[i]).innerHTML;
				if (iTempVar != '') {
					//iTotalCV = parseInt(iTotalCV) + parseInt(iTempVar);
					iTotalCV = iTotalCV*1 + iTempVar*1;
				}
			//Rewards
				iTempVar = document.getElementById('TotalRewards_' + arrProductList_Ids[i]).innerHTML;
				if (iTempVar != '--') {
					iTotalRewards = parseInt(iTotalRewards) + parseInt(iTempVar);
				}
		}
/*
		//Tax
			var iSalesTax = document.getElementById('SalesTax').innerHTML;	
		//Shipping
			//get select bax
				var oShipping_Method = document.getElementById('frm_Shipping_Method');
			//get selected option text
				var sShippingMethod = oShipping_Method.options[oShipping_Method.selectedIndex].text;
			//get $ to )
				var iShipPrice = sShippingMethod.substring(sShippingMethod.indexOf('$') + 1, sShippingMethod.indexOf(')'))
				if (iShipPrice == '') {
					iShipPrice = 0;
				}
*/		
	//display values
		//PV
			if (iTotalPV == 0) {
				document.getElementById('TotalPV').innerHTML = '--';
			} else {
				document.getElementById('TotalPV').innerHTML = iTotalPV.toFixed(2);
			}
		//CV
			if (iTotalCV == 0) {
				document.getElementById('TotalCV').innerHTML = '--';
			} else {
				document.getElementById('TotalCV').innerHTML = iTotalCV.toFixed(2);
			}
		//Rewards
			if (iTotalRewards == 0) {
				document.getElementById('TotalRewards').innerHTML = '--';
			} else {
				document.getElementById('TotalRewards').innerHTML = iTotalRewards;
			}			
		//Order Total
			document.getElementById('OrderTotal').innerHTML = (iTotalPrice*1).toFixed(2);
/*			
		//Sales Tax
			document.getElementById('SalesTax').innerHTML = parseInt(iSalesTax).toFixed(2);
		//Grand Total
			document.getElementById('GrandTotal').innerHTML = (iShipPrice*1 + iSalesTax*1 + iTotalPrice*1).toFixed(2);			
*/		
}//UpdateTotals()

function Next() {
	//alert('Next()');
	if ( ! Validate()) {
		return false;
	}
	
	//submit form
	var oForm = document.getElementById('formRedeem');
	oForm.submit();
}//Next()

function Validate() {
	var oForm = document.getElementById('formRedeem');
	
	//Buy Qty.
		var bQtySelected
		/*
		for (var i = 0; i < oForm.elements.length; i++) {
			if(oForm.elements[i].getAttribute('name')) {
				if (oForm.elements[i].getAttribute('name').indexOf('frmSel_Qty_') == 0 ) {
				//frmSel_Qty_501
					if ( oForm.elements[i].selectedIndex > 0 ) {
						bQtySelected = true;
					}
				}
			}
		}
		*/
		var arrProductList_Ids = document.getElementById('ProductList_Ids').value.split(',')
		for (var i = 0; i < arrProductList_Ids.length; i++) {
			if (document.getElementById('frmSel_Qty_' + arrProductList_Ids[i]).selectedIndex > 0) {
				bQtySelected = true;
			}
		}
		if (! bQtySelected) {
			alert('Please select the Buy Qty. of an item you would like to purchase.');
			return false;
		}
	
	//First Name
		fnTrimElement(oForm.frm_Shipping_FName);
		
		if (oForm.frm_Shipping_FName.value.length <= 0 || oForm.frm_Shipping_FName.value.length > 50) {
			oForm.frm_Shipping_FName.focus();
			oForm.frm_Shipping_FName.select();
			alert('Please enter a valid First Name, between 1 and 50 characters in length.');
			return false;
		}
	//Last Name
		fnTrimElement(oForm.frm_Shipping_LName);
		
		if (oForm.frm_Shipping_LName.value.length <= 0 || oForm.frm_Shipping_LName.value.length > 50) {
			oForm.frm_Shipping_LName.focus();
			oForm.frm_Shipping_LName.select();
			alert('Please enter a valid Last Name, between 1 and 50 characters in length.');
			return false;
		}
	//Shipping Address
		fnTrimElement(oForm.frm_Shipping_Address);
		
		if (oForm.frm_Shipping_Address.value.length <= 0 || oForm.frm_Shipping_Address.value.length > 50) {
			oForm.frm_Shipping_Address.focus();
			oForm.frm_Shipping_Address.select();
			alert('Please enter a valid Shipping Address, between 1 and 50 characters in length.');
			return false;
		}
	//Apt./Suite Number
		fnTrimElement(oForm.frm_Shipping_AptNumber);
		/*
		if (oForm.frm_Shipping_AptNumber.value.length <= 0 || oForm.frm_Shipping_AptNumber.value.length > 50) {
			oForm.frm_Shipping_AptNumber.focus();
			oForm.frm_Shipping_AptNumber.select();
			alert('Please enter a valid Apt./Suite Number, between 1 and 50 characters in length.');
			return false;
		}
		*/
	//City
		fnTrimElement(oForm.frm_Shipping_City);
		
		if (oForm.frm_Shipping_City.value.length <= 0 || oForm.frm_Shipping_City.value.length > 20) {
			oForm.frm_Shipping_City.focus();
			oForm.frm_Shipping_City.select();
			alert('Please enter a valid City, between 1 and 20 characters in length.');
			return false;
		}
	//State/Province
		if (oForm.frm_Shipping_State.selectedIndex == 0) {
			oForm.frm_Shipping_State.focus();
			alert('Please enter a select a State/Province.');
			return false;
		}
	//Postal Code
		var rxPostalCodeUS = /^\d{5}(-\d{4})?$/; //55555-4444 or 55555
		var rxPostalCodeCA = /^[A-Za-z]\d[A-Za-z][ -]?\d[A-Za-z]\d$/; //ANA NAN
		
		fnTrimElement(oForm.frm_Shipping_Zip);
		
		if (oForm.frm_Shipping_Zip.value.length <= 0 || oForm.frm_Shipping_Zip.value.length > 20) {
			oForm.frm_Shipping_Zip.focus();
			oForm.frm_Shipping_Zip.select();
			alert('Please enter a valid Postal Code, between 1 and 20 characters in length.');
			return false;
		}
		else if ( rxPostalCodeUS.test(oForm.frm_Shipping_Zip.value) == false && rxPostalCodeCA.test(oForm.frm_Shipping_Zip.value) == false ) {
			oForm.frm_Shipping_Zip.focus();
			oForm.frm_Shipping_Zip.select();
			alert('Please enter a valid Postal Code.\nUS: 12345-1234\nUS: 123456\nCA: A1B 2C3');
			return false;
		}
	//Country
		if (oForm.frm_Shipping_Country.options[oForm.frm_Shipping_Country.selectedIndex].value == -1) {
			oForm.frm_Shipping_Country.focus();
			alert('Please enter a select a Country.');
			return false;
		}
		
	//alert('Validate success');
	return true;
}//Validate()

//Remove Leading, Trailing, or Just Spaces of Element
function fnTrimElement(oElement) {
	var sOutputString;
	sOutputString = oElement.value;
	    
	var rxTrim = /^\s+|\s+$/g;
	    
	sOutputString = sOutputString.replace(rxTrim,"");
	oElement.value = sOutputString;
	    
	return sOutputString;
}//fnTrimElement()

function BodyOnload() {	
	//calcualte totals on page load
		var arrProductList_Ids = document.getElementById('ProductList_Ids').value.split(',')
		for (var i = 0; i < arrProductList_Ids.length; i++) {
			//alert(arrProductList_Ids[i]);
			ProductQtyChange(arrProductList_Ids[i]);
		}
}//BodyOnload()