
SK.Applications.LocalPromo.Display.GiftCertificate=SK.Applications.LocalPromo.Display.GiftCertificate||new Class({Extends:SK.Applications.LocalPromo.Display,type:'gift_card',service_certificates:null,init:function(){this.parent();this.presets=JSON.decode(this.env.properties.preset_amount_cards)||[];this.getGiftCertificatesTemplate();},collectElements:function(){this.parent();$extend(this.el,{buy_now_button:this.el.wrapper.getElement('.sk-gd-button-buynow'),gift_card_types:this.el.wrapper.getElement('.sk-gift-card-types'),gift_card_type:this.el.wrapper.getElement('.sk-gift-card-type')});},getGiftCertificatesTemplate:function(){new Request({method:'get',url:this.template_dir+'gift_card_types.htm',onSuccess:function(template_html){new SK.Sync([this.getServiceBasedCertificates.bind(this)],this.displayGiftCertificates.bind(this,[template_html])).run();}.bind(this)}).send();},getServiceBasedCertificates:function(sync){if(this.env.properties.service_based_cards_on!='1'){sync.ready();return;}
this.api.execute('db.query.get_filtered_rows',{index_only:0,filter:JSON.encode({where:[[this.getColumnId('ID','Services'),'=',JSON.decode(this.env.properties.active_services)]]})},function(success,message,result){if(!success){alert(message);return;}
this.mgr.service_certificates=result.rows;this.sync.ready();}.bind({mgr:this,sync:sync}));},displayGiftCertificates:function(template_html){var html=[];var properties=this.env.properties;var currency=properties.paypal_currency;var name=this.env.placeholder;var title='';var radio_id='';var currency_prefix,currency_suffix;if(this.currencies[currency].shortcode){currency_prefix=this.currencies[currency].shortcode;currency_suffix=null;}else{currency_prefix=null;currency_suffix=currency;}
if(this.env.properties.service_based_cards_on=='1'){var service_cards=[];var result={rows:this.service_certificates||[]};var column_id_title=this.getColumnId('Name','Services');var column_id_balance=this.getColumnId('Price','Services');var column_id_description=this.getColumnId('Description','Services');var column_id_service=this.getColumnId('ID','Services');for(var i=0,l=result.rows.length;i<l;i+=1){var match=result.rows[i].cells[column_id_balance].match(/\d+\.\d+|\d+/);var price=0;if(match){price=parseFloat(match[0]);}
service_cards.push({title:result.rows[i].cells[column_id_title],description:result.rows[i].cells[column_id_description],service:result.rows[i].cells[column_id_service],currency_prefix:currency_prefix,currency_suffix:currency_suffix,price:price,radio_id:this.generateId(),name:name});}
html.push(new SK.TemplateEngine(template_html,{card_types:service_cards}).init());}
if(this.env.properties.preset_amount_cards_on=='1'){var preset_cards=[];for(var i=0,l=this.presets.length;i<l;i+=1){if(this.presets[i]){title=currency_prefix?currency_prefix+this.presets[i]:this.presets[i]+' '+currency;preset_cards.push({title:title+' Gift Certificate',currency_prefix:currency_prefix,currency_suffix:currency_suffix,price:this.presets[i],radio_id:this.generateId(),name:name});}}
html.push(new SK.TemplateEngine(template_html,{card_types:preset_cards}).init());}
if(this.env.properties.custom_amount_card_on=='1'){var min_max_text=[];if(this.env.properties.custom_amount_card){var min_max_values=JSON.decode(this.env.properties.custom_amount_card);if(min_max_values.min){min_max_text.push('minimum amount: '+(currency_prefix||'')
+min_max_values.min+(currency_suffix?' '+currency_suffix:''));}
if(min_max_values.max){min_max_text.push('maximum amount: '+(currency_prefix||'')
+min_max_values.max+(currency_suffix?' '+currency_suffix:''));}}
html.push(new SK.TemplateEngine(template_html,{card_types:[{custom_amount:true,title:'Custom Amount Gift Certificate',description:'('+min_max_text.join(', ')+')',currency_prefix:currency_prefix,currency_suffix:currency_suffix,radio_id:this.generateId(),name:name}]}).init());}
this.el.gift_card_types.set('html',html.join(''));delete html;this.attachRadioEvents();this.attachCustomAmountEvents();this.hideLoading();},attachEvents:function(){this.parent();this.el.buy_now_button.addEvent('click',this.displayForm.bind(this));},attachRadioEvents:function(){this.el.gift_card_types.getElements('input[type="radio"]').addEvent('click',function(e){var target=$(e.target);var item_wrapper=target.getParent('.sk-gd-option');var amount_field=item_wrapper.getElement('.sk-gift-card-amount');$extend(this.data,{item_name:item_wrapper.getElement('.sk-gd-option-title').get('text'),service_id:item_wrapper.getElement('.sk-gift-card-service').getValue(),amount:amount_field.getValue()});this.el.gift_card_type.setValue(JSON.encode(this.data));this.el.purchase_form.getElement('.sk-gd-cardtitle').set('text',target.get('value'));var custom_amount_field=this.el.properties.getElement('input[type="text"].sk-gift-card-amount');if(custom_amount_field){if(custom_amount_field==amount_field){custom_amount_field.set('validate_as','custom_amount');custom_amount_field.addEvent('blur',function(e){$extend(this.data,{amount:$(e.target).getValue()});}.bind(this));}else{custom_amount_field.erase('validate_as');custom_amount_field.removeEvents('blur');}}}.bind(this));},attachCustomAmountEvents:function(){var custom_amount_field=this.el.properties.getElement('input[type="text"].sk-gift-card-amount');if(custom_amount_field){var custom_amount_radio=custom_amount_field.getParent('.sk-gd-option').getElement('input[type="radio"]');custom_amount_radio.addEvent('click',function(custom_amount_field){custom_amount_field.focus();}.bind(this,[custom_amount_field]));if(Browser.Engine.trident){custom_amount_field.addEvent('click',function(custom_amount_radio){custom_amount_radio.click();}.bind(this,[custom_amount_radio]));}}},initValidator:function(){this.parent({gift_card_type:{anti_pattern:/^$/,error_message:'You have to choose a gift certificate first'},custom_amount:{validate:function(value){var min_max_values=JSON.decode(this.env.properties.custom_amount_card);var number_value=Number(value);var validates=value!=''&&!isNaN(number_value)&&number_value>0;if(min_max_values.min&&number_value<min_max_values.min){validates=false;}
if(min_max_values.max&&number_value>min_max_values.max){validates=false;}
return validates;}.bind(this),error_message:'You must enter a valid custom amount'}});},displayForm:function(){if(this.validate(this.el.properties)){this.parent();}},displaySubtitleDescription:function(){var elements=this.el.wrapper.getElements('.sk-gd-cardprice');var text='(price '+this.formatPrice(this.data.amount)+')';for(var i=0,l=elements.length;i<l;i+=1){elements[i].set('html',text);}}});
