/*
 * Apex Module
 * @LastRevised $LastChangedDate: 2009-02-02 16:30:08 +0800 (Mon, 02 Feb 2009) $ 
 * @version $Id: PcCustomizer.js 78 2009-02-02 08:30:08Z eugim $ 
 *  
 *  
 * This code is licensed under BSD license. Use it as you wish, but keep this copyright intact.
 *  
 * Note: If you feel that your extension/plugin or code snippet was in this project, 
 * but was not duly recognized or referred to, please do not hesitate to contact 
 * eugim_migue@yahoo.com. As you see, I am using  JSBuilder, and I am a noob 
 * on automating such things.
 */


ApexModulePcCustomizer=Class.create({totalRegularPrice:0,totalDiscountedCashPrice:0,_orderData:{items:[],installmentSummary:{},pricesSummary:{}},initialize:function(config){var optionsEl=$$('select.apex-pccustomizer-mode-option'),attribs={};for(var i=0,len=optionsEl.length;i<len;i++){new Form.Element.Observer(optionsEl[i],.2,this._handleOptionChange.bindAsEventListener(this));attribs=optionsEl[i].options[optionsEl[i].selectedIndex].attributes;this.totalRegularPrice=this.totalRegularPrice+parseFloat(attribs.getNamedItem('apex:product_regular_price').value||0);this.totalDiscountedCashPrice=this.totalDiscountedCashPrice+parseFloat(attribs.getNamedItem('apex:product_discounted_price').value||0);this._handleOptionImage(optionsEl[i]);}
this._initSummaryButtonsEvents();this._initCustomizerFormEvents();this._initInstllmntSelectionCache();this._handleTotalsSummary(false);},_installmentSelectionCache:{},_initInstllmntSelectionCache:function(){var instOpts=$('INSTALLMENT_PLAN');for(var i=0,len=instOpts.length;i<len;i++){this._installmentSelectionCache[instOpts.options[i].value]={el:instOpts[i],text:instOpts[i].text+'      -        '};}},_initCustomizerFormEvents:function(){var instance=this;$('apex-pccustomizer-form').observe('submit',function(event){instance.recalculate();$('CUSTOMIZER_ORDER_DATA').setValue(Object.toJSON(instance._orderData));});},_initSummaryButtonsEvents:function(){var instance=this;$('apex-pccustomizer-recompute').observe('click',function(event){event.stop();instance.recalculate();});$('apex-pccustomizer-restoredefaults').observe('click',function(event){event.stop();instance.restoreDefaults();});$('apex-pccustomizer-finish-order').observe('click',function(event){event.stop();Element.scrollTo($('pccustomizer-form-visible-area'));$('EMAIL_ADDRESS').focus();});},_handleOptionChange:function(el,value){this._handleOptionImage(el);this._handleTotalsSummary(true);},_handleOptionImage:function(optionEl){var optionElId=optionEl.identify(),imgEl=$('image-'+optionElId);if(!imgEl){return;}
var optionAttribs=optionEl.options[optionEl.selectedIndex].attributes,imgAttribs=imgEl.attributes,newImageSrc='';if(imgEl&&(newImageSrc=optionAttribs.getNamedItem('apex:product_image').value||imgAttribs.getNamedItem('apex_default_image').value)){imgEl.src=imgAttribs.getNamedItem('apex_base_url').value+newImageSrc;}},_handleTotalsSummary:function(recomputeTotals){if(false!==recomputeTotals){var optionsEl=$$('select.apex-pccustomizer-mode-option'),attribs={},option={},regPrice=0,discPrice=0;this.totalRegularPrice=this.totalDiscountedCashPrice=0;this._orderData.items=[];for(var i=0,len=optionsEl.length;i<len;i++){option=optionsEl[i].options[optionsEl[i].selectedIndex];attribs=option.attributes;regPrice=parseFloat(attribs.getNamedItem('apex:product_regular_price').value||0);discPrice=parseFloat(attribs.getNamedItem('apex:product_discounted_price').value||0);this._orderData.items.push({ITEM_ID:option.value,ITEM_LABEL:optionsEl[i].id,ITEM_NAME:attribs.getNamedItem('apex:product_name').value,ITEM_PRICE:regPrice,ITEM_DISCOUNTED_PRICE:discPrice});this.totalRegularPrice=this.totalRegularPrice+regPrice;this.totalDiscountedCashPrice=this.totalDiscountedCashPrice+discPrice;}}
this._orderData.pricesSummary={TOTAL_PRICE:ApexUtil.format.currency(this.totalRegularPrice),TOTAL_DISCOUNTED_PRICE:ApexUtil.format.currency(this.totalDiscountedCashPrice)};$('apex:pccustomizer-total-regular-price').update(this._orderData.pricesSummary.TOTAL_PRICE);$('apex:pccustomizer-total-discounted-cash-price').update(this._orderData.pricesSummary.TOTAL_DISCOUNTED_PRICE);var installmentTotalLabels=$$('span.apex-pccustomizer-installments'),installmentType='',installmentFormula='',installmentAmount='',frmttedAmount='';this._orderData.installmentSummary={};for(var i=0,len=installmentTotalLabels.length;i<len;i++){installmentType=installmentTotalLabels[i].identify();installmentFormula=String(ApexModulePcCustomizer.INSTALLMENT_PLANS[installmentType].formula).replace(/{TOTAL_DISC_CASH_PRICE}/gi,this.totalDiscountedCashPrice);if(installmentFormula){try{installmentAmount=eval(installmentFormula);frmttedAmount=ApexUtil.format.currency(installmentAmount);installmentTotalLabels[i].update(frmttedAmount);this._installmentSelectionCache[installmentType].el.text=this._installmentSelectionCache[installmentType].text+frmttedAmount;this._orderData.installmentSummary[installmentType]={INSTALLMENT_ID:installmentType,INSTALLMENT_NAME:this._installmentSelectionCache[installmentType].el.label,INSTALLMENT_TOTAL:installmentAmount};}catch(e){}}}},recalculate:function(){this._handleTotalsSummary(true);},restoreDefaults:function(){var optionsEl=$$('select.apex-pccustomizer-mode-option');for(var i=0,len=optionsEl.length;i<len;i++){optionsEl[i].selectedIndex=0;}
this.recalculate();}});ApexModulePcCustomizer.INSTALLMENT_PLANS={'pccustomizer_3_month_installment':{formula:'( ({TOTAL_DISC_CASH_PRICE} * 0.0730) + ({TOTAL_DISC_CASH_PRICE} * 0.005) + {TOTAL_DISC_CASH_PRICE} ) / 3'},'pccustomizer_6_month_installment':{formula:'( ({TOTAL_DISC_CASH_PRICE} * 0.08) + ({TOTAL_DISC_CASH_PRICE} * 0.005) + {TOTAL_DISC_CASH_PRICE} ) / 6'},'pccustomizer_9_month_installment':{formula:'( ({TOTAL_DISC_CASH_PRICE} * 0.14) + ({TOTAL_DISC_CASH_PRICE} * 0.005) + {TOTAL_DISC_CASH_PRICE} ) / 9'},'pccustomizer_12_month_installment':{formula:'( ({TOTAL_DISC_CASH_PRICE} * 0.14) + ({TOTAL_DISC_CASH_PRICE} * 0.005) + {TOTAL_DISC_CASH_PRICE} ) / 12'}};