
function validateMultipleQuantityFields(formName){var formActionField=document.forms[formName].elements[formName+':_idcl'];if(formActionField&&formActionField.value.indexOf(':deleteQuantity')!=-1){return;}
var numberOfItems=document.getElementById(formName+':'+'ItemList:0:numberOfItems');var quantity=formName+':'+'ItemList:';var errorMsg="Please correct the error below: The quantity field may consist only of 0 or positive numbers.";for(i=0;i<numberOfItems.value;i++){var quantityField=document.getElementById(quantity+i+':itemqty');if(!cf_isQuantity(quantityField.value)){quantityField.focus();displayErrorMessage(quantityField,errorMsg,formName);return false;}}}
function calculateTotalPrice(index){var tmp="collectionForm:ItemList:";var errorMsg="Please correct the error below: The quantity field may consist only of 0 or positive numbers.";cleanUpErrorMessage('errorMessage');cleanUpErrorImages('collectionForm');var quantityField=document.getElementById(tmp+index+':itemqty');if(quantityField!=null){if(!cf_isQuantity(quantityField.value)){quantityField.focus();displayErrorMessage(quantityField,errorMsg,'collectionForm');return false;}}
var priceIndex=tmp+index+":price";var salePriceIndex=tmp+index+":salePrice";var price=document.getElementById(priceIndex);var salePrice=document.getElementById(salePriceIndex);var indexQty=tmp+index+":itemqty";var qty=document.getElementById(indexQty);var totalPrice=0.0;var totalRegularPrice=price.value*qty.value;if(salePrice.value!=0){totalPrice=salePrice.value*qty.value;}else{totalPrice=price.value*qty.value;}
var totalPriceIndex=tmp+index+":totalPrice";document.getElementById(totalPriceIndex).value=totalPrice;var totalRegularPriceIndex=tmp+index+":totalRegularPrice";document.getElementById(totalRegularPriceIndex).value=totalRegularPrice;if(totalPrice==0){totalPrice='0.00';}else{totalPrice=formatPrice(totalPrice);}
var outputPriceIndex=tmp+index+":outputPrice";var displayPrice="Total Price: $"+totalPrice;document.getElementById(outputPriceIndex).firstChild.nodeValue=displayPrice;}
function calculateSelectionAndRegularPrice(){var selectionPrice=0.00;var regularPrice=0.00;var numberOfItems=document.getElementById('collectionForm:ItemList:0:numberOfItems');for(i=0;i<numberOfItems.value;i++){var totalPrice=document.getElementById('collectionForm:ItemList:'+i+':totalPrice');var totalPricetoNumber=totalPrice.value*1;selectionPrice=selectionPrice+totalPricetoNumber;var totalRegularPrice=document.getElementById('collectionForm:ItemList:'+i+':totalRegularPrice');var totalRegularPricetoNumber=totalRegularPrice.value*1;regularPrice=regularPrice+totalRegularPricetoNumber;}
var saveAmount=regularPrice-selectionPrice;var percentSaved=0.00;if(saveAmount!=0){percentSaved=saveAmount/regularPrice;percentSaved=percentSaved*100;percentSaved=formatPercent(percentSaved);}
saveAmount=formatPrice(saveAmount);var displaySaved=saveAmount;if((saveAmount)!=0){displaySaved=displaySaved+' ('+percentSaved+'%)';}else{displaySaved='0.00';}
var saveAmountIndex="collectionForm:saveAmount";document.getElementById(saveAmountIndex).firstChild.nodeValue=displaySaved;if(selectionPrice!=0){selectionPrice=formatPrice(selectionPrice);}else{selectionPrice='0.00';}
var selectionPriceIndex="collectionForm:selectionPrice";document.getElementById(selectionPriceIndex).firstChild.nodeValue=selectionPrice;if(regularPrice!=0){regularPrice=formatPrice(regularPrice);}else{regularPrice='0.00';}
var regularPriceIndex="collectionForm:regularPrice";document.getElementById(regularPriceIndex).firstChild.nodeValue=regularPrice;}
function formatPercent(amount){amount=amount.toString();var index=amount.indexOf('.');amount=amount.substring(0,index);return amount;}
function formatPrice(price){price=price.toString();var index1=price.length-2;var index2=price.length;var firstString=price.substring(0,index1);var secondString=price.substring(index1,index2);price=firstString+"."+secondString;return price;}
function cf_isQuantity(str){return str.match(/^[\d]{1,2}$/);}
function displayErrorMessage(field,errorMsg,formName){var errorAlt='Error';var errorClass='formError'
var errorTitle='This field has an error!';var errorIndicator=document.createElement('img');var errorImg='/images/global/alert.gif';var errorID='errorMessage';cleanUpErrorMessage(errorID);cleanUpErrorImages(formName);errorIndicator.alt=errorAlt;errorIndicator.src=errorImg;errorIndicator.title=errorTitle;field.className=errorClass;field.parentNode.insertBefore(errorIndicator,field);if(!document.getElementById(errorID)){var em=document.createElement('div');em.id=errorID;var newp=document.createElement('p');newp.appendChild(document.createTextNode(errorMsg));em.appendChild(newp);var sb=document.getElementById('required');if(sb){sb.parentNode.insertBefore(em,sb);}}}
function cleanUpErrorMessage(errorID){if(document.getElementById(errorID)){var em=document.getElementById(errorID);em.parentNode.removeChild(em);}}
function cleanUpErrorImages(formName){var numberOfItems=document.getElementById(formName+':'+'ItemList:0:numberOfItems');var quantity=formName+':'+'ItemList:';var reqfields=document.getElementById('required').value.split(',');for(var i=0;i<numberOfItems.value;i++){var f=document.getElementById(quantity+i+':itemqty');if(!f){continue;}
if(f.previousSibling&&/img/i.test(f.previousSibling.nodeName)){f.parentNode.removeChild(f.previousSibling);}
f.className='';}}
function changeQuantity(){var inputName='input';var checkBoxName='checkbox';var quantityName='Quantity';var inputListLength=document.getElementsByTagName(inputName).length;for(var i=0;i<inputListLength;i++){if(document.getElementsByTagName(inputName)[i].type==checkBoxName&&document.getElementsByTagName(inputName)[i].checked){var checkBoxId=document.getElementsByTagName(inputName)[i].id;var checkBoxIdList=checkBoxId.split(':');var quantityId=checkBoxIdList[0]+':'+checkBoxIdList[1]+':'+checkBoxIdList[2]+':'+quantityName;document.getElementById(quantityId).value=0;}}
return true;}
function checkQuantitiesForZero(){var numberOfItems=document.getElementById('collectionForm:ItemList:0:numberOfItems');var atLeastOnePositiveInteger=false;var allValidQuantities=true;for(i=0;i<numberOfItems.value;i++){var quantityField=document.getElementById('collectionForm:ItemList:'+i+':itemqty');if(quantityField!=null){var qty=quantityField.value*1;if(isNaN(qty)||qty<0){allValidQuantities=false;break;}
if(!atLeastOnePositiveInteger&&qty>0)atLeastOnePositiveInteger=true;}}
if(!allValidQuantities||!atLeastOnePositiveInteger){alert('Please enter a quantity from 1 to 99 for an item.');return false;}}