Calendar.setup=function(params){function param_default(pname,def){if(typeof params[pname]=="undefined"){params[pname]=def;}};param_default("inputField",null);param_default("displayArea",null);param_default("button",null);param_default("eventName","click");param_default("ifFormat","%Y/%m/%d");param_default("daFormat","%Y/%m/%d");param_default("singleClick",true);param_default("disableFunc",null);param_default("dateStatusFunc",params["disableFunc"]);param_default("dateText",null);param_default("firstDay",null);param_default("align","Br");param_default("range",[1900,2999]);param_default("weekNumbers",true);param_default("flat",null);param_default("flatCallback",null);param_default("onSelect",null);param_default("onClose",null);param_default("onUpdate",null);param_default("date",null);param_default("showsTime",false);param_default("timeFormat","24");param_default("electric",true);param_default("step",2);param_default("position",null);param_default("cache",false);param_default("showOthers",false);param_default("multiple",null);var tmp=["inputField","displayArea","button"];for(var i in tmp){if(typeof params[tmp[i]]=="string"){params[tmp[i]]=document.getElementById(params[tmp[i]]);}}if(!(params.flat||params.multiple||params.inputField||params.displayArea||params.button)){alert("Calendar.setup:\n  Nothing to setup (no fields found).  Please check your code");return false;}function onSelect(cal){var p=cal.params;var update=(cal.dateClicked||p.electric);if(update&&p.inputField){p.inputField.value=cal.date.print(p.ifFormat);if(typeof p.inputField.onchange=="function")p.inputField.onchange();}if(update&&p.displayArea)p.displayArea.innerHTML=cal.date.print(p.daFormat);if(update&&typeof p.onUpdate=="function")p.onUpdate(cal);if(update&&p.flat){if(typeof p.flatCallback=="function")p.flatCallback(cal);}if(update&&p.singleClick&&cal.dateClicked)cal.callCloseHandler();};if(params.flat!=null){if(typeof params.flat=="string")params.flat=document.getElementById(params.flat);if(!params.flat){alert("Calendar.setup:\n  Flat specified but can't find parent.");return false;}var cal=new Calendar(params.firstDay,params.date,params.onSelect||onSelect);cal.showsOtherMonths=params.showOthers;cal.showsTime=params.showsTime;cal.time24=(params.timeFormat=="24");cal.params=params;cal.weekNumbers=params.weekNumbers;cal.setRange(params.range[0],params.range[1]);cal.setDateStatusHandler(params.dateStatusFunc);cal.getDateText=params.dateText;if(params.ifFormat){cal.setDateFormat(params.ifFormat);}if(params.inputField&&typeof params.inputField.value=="string"){cal.parseDate(params.inputField.value);}cal.create(params.flat);cal.show();return false;}var triggerEl=params.button||params.displayArea||params.inputField;triggerEl["on"+params.eventName]=function(){var dateEl=params.inputField||params.displayArea;var dateFmt=params.inputField?params.ifFormat:params.daFormat;var mustCreate=false;var cal=window.calendar;if(dateEl)params.date=Date.parseDate(dateEl.value||dateEl.innerHTML,dateFmt);if(!(cal&&params.cache)){window.calendar=cal=new Calendar(params.firstDay,params.date,params.onSelect||onSelect,params.onClose||function(cal){cal.hide();});cal.showsTime=params.showsTime;cal.time24=(params.timeFormat=="24");cal.weekNumbers=params.weekNumbers;mustCreate=true;}else{if(params.date)cal.setDate(params.date);cal.hide();}if(params.multiple){cal.multiple={};for(var i=params.multiple.length;--i>=0;){var d=params.multiple[i];var ds=d.print("%Y%m%d");cal.multiple[ds]=d;}}cal.showsOtherMonths=params.showOthers;cal.yearStep=params.step;cal.setRange(params.range[0],params.range[1]);cal.params=params;cal.setDateStatusHandler(params.dateStatusFunc);cal.getDateText=params.dateText;cal.setDateFormat(dateFmt);if(mustCreate)cal.create();cal.refresh();if(!params.position)cal.showAtElement(params.button||params.displayArea||params.inputField,params.align);else cal.showAt(params.position[0],params.position[1]);return false;};return cal;};;var BundleConfigExt=Class.create({initialize:function(){this.finalPrice=$("product-final-price");this.cont=0;this.container=[];this.url="";this.cmUrl="";this.configurableProducts();},bindEvents:function(el){},_getParams:function(el){},configurableProducts:function(){Event.observe(window,'load',function(){$$('#product-options-wrapper dl dd.last select').each(function(el)
{if(el&&el.id)
{$(el).observe('change',function(ele){this.removeValidationMsg(ele);ele=ele.findElement();var inputEl=ele.next('input',1);var params="?id=";var text="";if(ele.id.indexOf('attribute')!=-1){var attrId=ele.id.split('attribute')[1];}
else{if(ele.id.indexOf('_')!=-1){var attrId=ele.id.split('_')[1];}}
var prodId=$("attr-"+attrId).value;if(typeof(el.options[el.selectedIndex])!="undefined")
{text=el.options[el.selectedIndex].text;}
params=params+prodId+"_"+attrId+"_"+text+"_"+el.selectedIndex;if(el.selectedIndex*1!=0)
{this.ajaxUpdate(inputEl,this.url,params);}
else
{inputEl.hide();$("label_"+inputEl.id).hide();}}.bind(this))}}.bind(this))}.bind(this))},ajaxUpdate:function(inputEl,url,params){if(inputEl!=null&&typeof(inputEl)!=="undefined"&&url!=""){new Ajax.Request(url,{method:'get',parameters:params,onComplete:function(resp)
{resp=resp.responseText.evalJSON();if(typeof(resp)!==undefined&&resp.msg!==null)
{$("label_"+inputEl.id).show();$('availability-question_'+inputEl.id).show();$(inputEl).value=resp.msg;$(inputEl).writeAttribute('readonly',true).show().addClassName("remove-styles");}}})}},updateStatusMessage:function(el){var params="?id=";var text=""
var inputEl=$(el).up(1).next('input');if(typeof(el.options[el.selectedIndex])!="undefined")
{text=el.options[el.selectedIndex].text.substr(0,1);}
params=params+el.id+"_"+text+"_"+el.selectedIndex;if(inputEl!=null&&typeof(inputEl)!=="undefined")
{this.ajaxUpdate(inputEl,this.cmUrl,params);}},removeValidationMsg:function(el){if(el==null){return;}
var adviceEl=$('advice-required-entry-'+el.id);if(adviceEl!=null){adviceEl.remove();}
$$('div.validation-advice').each(function(element){element.remove();})
$$('.validation-failed').each(function(element){element.removeClassName('validation-failed');})},createInputEl:function(){this.inputEl=$(this.el).up(1).next('input');if(this.inputEl)
{this.inputEl.value="";}},change:function(el,id,attrId){this.selectBoxes=$$("select.super-attribute-select");this.el=$(el);this.removeValidationMsg(el);this.createInputEl();if(this.el===null)
{return;}
if(this.index<=0||this.el.selectedIndex*1<=0){this.inputEl.hide();$("label_"+this.inputEl.id).hide();return;}
var value=el.selectedIndex*1;var params='?id=';this.upEl=$(el).up(4).previous().down('span.price');var text=""
if(typeof(el.options[value])!="undefined")
{text=el.options[value].text}
if(id!=null&&typeof(id)!=="undefined"){params=params+id+"_"+attrId+"_"+text+"_"+value;}else{params=params+el.id+"_"+text+"_"+value;}
var tmp=this.inputEl;new Ajax.Request(this.url,{parameters:params,method:'get',onSuccess:function(resp)
{resp=resp.responseText.evalJSON();if(typeof(resp.error)=="undefined")
{if(id!=null&&typeof(id)!=="undefined"){jQuery('.availability-question').show();jQuery('.remove-label-style').show();jQuery('input.product-custom-option').val(resp.msg).show().addClass('remove-styles');}else{$('availability-question_'+tmp.id).show();$("label_"+tmp.id).show();tmp.value=resp.msg;$(tmp).writeAttribute('readonly',true).show().addClassName("remove-styles");}}
else
{alert(resp.msg);}}.bind(this)});}});var bce=new BundleConfigExt()
Product.OptionsPrice.prototype.formatPrice=function(price){price=formatCurrency(price,this.priceFormat);if(price.indexOf(this.priceFormat.decimalSymbol)!=-1){var decimals=price.split(this.priceFormat.decimalSymbol);if(decimals.length>1&&decimals[1]*1==0){return decimals[0];}}
return price;};if(typeof Product=='undefined'){var Product={};}
var bconfInstances=$H({});Product.BConfig=Class.create();Product.BConfig.prototype={initialize:function(config){this.config=config;this.taxConfig=this.config.taxConfig;this.settings=$$('select[rel="conf-'+config.productId+'"]');this.state=new Hash();this.priceTemplate=new Template(this.config.template);this.prices=config.prices;this.settings.each(function(element){Event.observe(element,'change',this.configure.bind(this))}.bind(this));this.settings.each(function(element){var attributeId=element.id.replace(/[a-z]*/,'');if(attributeId&&this.config.attributes[attributeId]){element.config=this.config.attributes[attributeId];element.attributeId=attributeId;this.state[attributeId]=false;}}.bind(this))
var childSettings=[];for(var i=this.settings.length-1;i>=0;i--){if(this.checkEq(this.settings[i])){var prevSetting=this.settings[i-1]?this.settings[i-1]:false;var nextSetting=this.settings[i+1]?this.settings[i+1]:false;if(i==0){this.fillSelect(this.settings[i])}
else{this.settings[i].disabled=true;}
$(this.settings[i]).childSettings=childSettings.clone();$(this.settings[i]).prevSetting=prevSetting;$(this.settings[i]).nextSetting=nextSetting;childSettings.push(this.settings[i]);}}
var separatorIndex=window.location.href.indexOf('#');if(separatorIndex!=-1){var paramsStr=window.location.href.substr(separatorIndex+1);this.values=paramsStr.toQueryParams();this.settings.each(function(element){var attributeId=element.attributeId;element.value=this.values[attributeId];this.configureElement(element);}.bind(this));}},configure:function(event){var element=Event.element(event);if(this.checkEq(element)){this.configureElement(element);}},configureElement:function(element){this.reloadOptionLabels(element);if(element.value){this.state[element.config.id]=element.value;if(element.nextSetting&&this.checkEq(element)){element.nextSetting.disabled=false;this.fillSelect(element.nextSetting);this.resetChildren(element.nextSetting);}}
else{this.resetChildren(element);}
this.reloadPrice();},reloadOptionLabels:function(element){if(this.checkEq(element)){var selectedPrice;if(element.options[element.selectedIndex].config){selectedPrice=parseFloat(element.options[element.selectedIndex].config.price)}
else{selectedPrice=0;}
for(var i=0;i<element.options.length;i++){if(element.options[i].config){element.options[i].text=this.getOptionLabel(element.options[i].config,element.options[i].config.price-selectedPrice);}}}},resetChildren:function(element){if(element.childSettings&&this.checkEq(element)){for(var i=0;i<element.childSettings.length;i++){element.childSettings[i].selectedIndex=0;element.childSettings[i].disabled=true;if(element.config){this.state[element.config.id]=false;}}}},fillSelect:function(element){var attributeId=element.id.replace(/[a-z]*/,'');if(!this.checkEq(element)){return false;}
var options=this.getAttributeOptions(attributeId);this.clearSelect(element);element.options[0]=new Option(this.config.chooseText,'');var prevConfig=false;if(element.prevSetting){if(this.checkEq(element.prevSetting)){prevConfig=element.prevSetting.options[element.prevSetting.selectedIndex];}}
if(options){var index=1;for(var i=0;i<options.length;i++){var allowedProducts=[];if(prevConfig){for(var j=0;j<options[i].products.length;j++){if(prevConfig.config.allowedProducts&&prevConfig.config.allowedProducts.indexOf(options[i].products[j])>-1){allowedProducts.push(options[i].products[j]);}}}else{allowedProducts=options[i].products.clone();}
if(allowedProducts.size()>0){options[i].allowedProducts=allowedProducts;element.options[index]=new Option(this.getOptionLabel(options[i],options[i].price),options[i].id);element.options[index].config=options[i];index++;}}}},getOptionLabel:function(option,price){var price=parseFloat(price);if(this.taxConfig.includeTax){var tax=price/(100+this.taxConfig.defaultTax)*this.taxConfig.defaultTax;var excl=price-tax;var incl=excl*(1+(this.taxConfig.currentTax/100));}else{var tax=price*(this.taxConfig.currentTax/100);var excl=price;var incl=excl+tax;}
if(this.taxConfig.showIncludeTax||this.taxConfig.showBothPrices){price=incl;}else{price=excl;}
var str=option.label;if(price){if(this.taxConfig.showBothPrices){str+=' '+this.formatPrice(excl,true)+' ('+this.formatPrice(price,true)+' '+this.taxConfig.inclTaxTitle+')';}else{str+=' '+this.formatPrice(price,true);}}
return str;},checkEq:function(element){return(element.readAttribute('rel')=='conf-'+this.config.productId);},formatPrice:function(price,showSign){var str='';price=parseFloat(price);var inPrice=parseInt(price)
price=(price-inPrice)>0?price:inPrice;if(showSign){if(price<0){str+='-';price=-price;}
else{str+='+';}}
var roundedPrice=(Math.round(price*100)/100).toString();if(this.prices&&this.prices[roundedPrice]){str+=this.prices[roundedPrice];}
else{str+=this.priceTemplate.evaluate({price:price.toFixed(2)});}
return str;},clearSelect:function(element){for(var i=element.options.length-1;i>=0;i--){if(this.checkEq(element)){element.remove(i);}}},getAttributeOptions:function(attributeId){if(this.config.attributes[attributeId]){return this.config.attributes[attributeId].options;}},reloadPrice:function(){var price=0;for(var i=this.settings.length-1;i>=0;i--){var selected=this.settings[i].options[this.settings[i].selectedIndex];if(selected.config&&this.checkEq(this.settings[i])){price+=parseFloat(selected.config.price);}}
optionsPrice.changePrice('config',price);optionsPrice.reload();return price;if($('product-price-'+this.config.productId)){$('product-price-'+this.config.productId).innerHTML=price;}
this.reloadOldPrice();},reloadOldPrice:function(){if($('old-price-'+this.config.productId)){var price=parseFloat(this.config.oldPrice);for(var i=this.settings.length-1;i>=0;i--){var selected=this.settings[i].options[this.settings[i].selectedIndex];if(selected.config&&this.checkEq(this.settings[i])){price+=parseFloat(selected.config.price);}}
if(price<0)
price=0;price=this.formatPrice(price);if($('old-price-'+this.config.productId)){$('old-price-'+this.config.productId).innerHTML=price;}}}}
Product.Bundle=Class.create();Product.Bundle.prototype={initialize:function(config){this.config=config;this.reloadPrice();},changeSelection:function(selection){parts=selection.id.split('-');if(this.config['options'][parts[2]].isMulti){selected=new Array();if(selection.tagName=='SELECT'){for(var i=0;i<selection.options.length;i++){if(selection.options[i].selected&&selection.options[i].value!=''){selected.push(selection.options[i].value);}}}else if(selection.tagName=='INPUT'){selector=parts[0]+'-'+parts[1]+'-'+parts[2];selections=$$('.'+selector);for(var i=0;i<selections.length;i++){if(selections[i].checked&&selections[i].value!=''){selected.push(selections[i].value);}}}
this.config.selected[parts[2]]=selected;}else{try{var cnfPrId=this.config.options[parts[2]].selections[parts[3]].confProductId;var cnfGId=cnfPrId+'_'+parts[3];var confOpts=this.config.options[parts[2]].selections[parts[3]].configurableOptions[cnfGId];}catch(er){console.error(er);}
if($('config-container'+parts[3])){$('config-container'+parts[3]).remove();}
if(typeof confOpts!="undefined"){var atI=this.config.options[parts[2]].selections[parts[3]].confattributes[cnfGId].items;var html='';atI.each(function(i,j){var myT=new Template('<label><i>#{label}</i><select rel="conf-#{product_id}" onchange="bce.change(this)" name="super_attribute['+cnfPrId+'][#{attribute_id}] selectBox-dropdown" id="#{product_id}_#{attribute_id}" class="required-entry super-attribute-select selectBox"><option>Choose an Option...</option></select></label><a href="#" rel="orchidboutique/cart/add/product/#{product_id}/option/#{attribute_id}" class="simpleAddToCart">Add to Bag</a>');html+=myT.evaluate(i);});selection.next().insert({after:'<div class="bconf-cont" id="config-container'+parts[3]+'">'+html+'</div>'});bconfInstances.set(cnfGId,new Product.BConfig(confOpts));}
if(selection.value!=''){this.config.selected[parts[2]]=new Array(selection.value);}else{this.config.selected[parts[2]]=new Array();}
this.populateQty(parts[2],selection.value);}
this.reloadPrice();jQuery(".selectBox").selectBox();jQuery('a.simpleAddToCart').click(function(){if(!jQuery(this).parent().find('select.super-attribute-select').val()){if(!jQuery(this).parent().find('a.super-attribute-select').hasClass('validation-failed')){jQuery(this).parent().find('a.super-attribute-select').addClass('validation-failed');}
return false;}
var href=jQuery(this).attr('rel')+'/value/'+jQuery(this).parent().find('select').val();jQuery(this).attr('href',href);});},reloadPrice:function(){var calculatedPrice=0;var dispositionPrice=0;for(var option in this.config.selected){if(this.config.options[option]){for(var i=0;i<this.config.selected[option].length;i++){var prices=this.selectionPrice(option,this.config.selected[option][i]);calculatedPrice+=Number(prices[0]);dispositionPrice+=Number(prices[1]);}}}
if(this.config.specialPrice){var newPrice=(calculatedPrice*this.config.specialPrice)/100;calculatedPrice=Math.min(newPrice,calculatedPrice);}
optionsPrice.changePrice('bundle',calculatedPrice);optionsPrice.changePrice('nontaxable',dispositionPrice);optionsPrice.reload();return calculatedPrice;},selectionPrice:function(optionId,selectionId){if(selectionId==''||selectionId=='none'){return 0;}
if(this.config.options[optionId].selections[selectionId].customQty==1&&!this.config['options'][optionId].isMulti){if($('bundle-option-'+optionId+'-qty-input')){qty=$('bundle-option-'+optionId+'-qty-input').value;}else{qty=1;}}else{qty=this.config.options[optionId].selections[selectionId].qty;}
if(this.config.priceType=='0'){price=this.config.options[optionId].selections[selectionId].price;tierPrice=this.config.options[optionId].selections[selectionId].tierPrice;for(var i=0;i<tierPrice.length;i++){if(Number(tierPrice[i].price_qty)<=qty&&Number(tierPrice[i].price)<=price){price=tierPrice[i].price;}}}else{selection=this.config.options[optionId].selections[selectionId];if(selection.priceType=='0'){price=selection.priceValue;}else{price=(this.config.basePrice*selection.priceValue)/100;}}
var disposition=this.config.options[optionId].selections[selectionId].plusDisposition+
this.config.options[optionId].selections[selectionId].minusDisposition;var result=new Array(price*qty,disposition*qty);return result;},populateQty:function(optionId,selectionId){if(selectionId==''||selectionId=='none'){this.showQtyInput(optionId,'0',false);return;}
if(this.config.options[optionId].selections[selectionId].customQty==1){this.showQtyInput(optionId,this.config.options[optionId].selections[selectionId].qty,true);}else{this.showQtyInput(optionId,this.config.options[optionId].selections[selectionId].qty,false);}},showQtyInput:function(optionId,value,canEdit){},changeOptionQty:function(element,event){var checkQty=true;if(typeof(event)!='undefined'){if(event.keyCode==8||event.keyCode==46){checkQty=false;}}
if(checkQty&&(Number(element.value)==0||isNaN(Number(element.value)))){element.value=1;}
parts=element.id.split('-');optionId=parts[2];if(!this.config['options'][optionId].isMulti){selectionId=this.config.selected[optionId][0];this.config.options[optionId].selections[selectionId].qty=element.value*1;this.reloadPrice();}},validationCallback:function(elmId,result){if(typeof elmId=='undefined'){return;}
var container=$(elmId).up('ul.options-list');if(typeof container!='undefined'){if(result=='failed'){container.removeClassName('validation-passed');container.addClassName('validation-failed');}else{container.removeClassName('validation-failed');container.addClassName('validation-passed');}}}}
