
(function(){var global=this;global.AjaxRemove=function(){var store={currencySymbol:null,costDomPrivacyYearly:Number(0),currentParentRowTemp:null,currentSubRowsTemp:[],cssRemovingBgClass:"removing",cssFlashColor:"#fffcaa",regex:{querystring_id:/id=([0-9]+)/,querystring_type:/type=([a-zA-Z-]+)/,item_domainprivacy:/Domain Privacy for [0-9]+yrs/,rowclass_main:/top/,rowclass_sub:/extra/,rowclass_removed:/removed/}};var init=function(){store.currencySymbol=Common.cookie.get("currencySymbol");if(!store.currencySymbol){window.location="/";}
setup.getDomPrivacyItemCost();var trashAnchors=$(".os-tbl tr .trash .image-block");$(trashAnchors).click(function(){return false;});$.each(trashAnchors,function(){this.href+="&ajax=true";$(this).mouseup(function(){removalEvent(this);});});};var removalEvent=function(trashAnchor){$(trashAnchor).parent().parent().find("td").addClass(store.cssRemovingBgClass);var href=$(trashAnchor).attr("href");if(href){$.get(href,function(xml){removalCallback(xml);});}
return false;};var removalCallback=function(xml){if($(xml).find("order").attr("version")=="1.0"&&$(xml).find("services")&&$(xml).find("service")&&$(xml).find("serviceName")&&$(xml).find("orderItems")&&$(xml).find("orderItem")){utils.resetStoredInfo();var XMLOrderItemIds=utils.getXMLOrderItemIds(xml);var currentOrderItemIds=utils.getCurrentOrderItemIds();var removalOrderItemIds=jsCore.arraySubtract(currentOrderItemIds,XMLOrderItemIds);if(removalOrderItemIds.length>0){utils.updateBasketItems(xml,removalOrderItemIds);}else{utils.updateDomainPrivacyItems(xml);}
utils.updateBasketTotal(xml);utils.updateMessageBoxes("hide");}else{document.location=document.location.toString();}};var setup={getDomPrivacyItemCost:function(){$.each($(".os-tbl .extra"),function(){if($(this).find(".product").text().match(store.regex.item_domainprivacy)){itemCost=$(this).find(".price").text();itemCost=itemCost.replace(/(^\D|,)/g,'');if(itemCost!="FREE"){period=$(this).find(".product").text().match(/[0-9]+/);store.costDomPrivacyYearly=Number((itemCost/period).toFixed(2));}}});}};var jsCore={arrayUnique:function(iArray){dArray=[];uArray=iArray;iArrayLength=iArray.length;uArrayLength=uArray.length;for(i=0;i<iArrayLength;i++){var counter=0;for(j=0;j<iArrayLength;j++){if(iArray[i]==iArray[j]){counter++;}}
if(counter>1){if(dArray.length>0){var dcounter=0;for(d=0;d<dArray.length;d++){if(iArray[i]==dArray[d]){dcounter++;}}
if(dcounter===0){dArray.push(iArray[i]);}}else{dArray.push(iArray[i]);}}}
for(u=0;u<uArrayLength;u++){for(d=0;d<dArray.length;d++){if(uArray[u]==dArray[d]){uArray.splice(u,1);}}}
uArray=uArray.concat(dArray);return uArray;},arraySubtract:function(oArray,rArray){fArray=[];uArray=oArray.slice(0);oArrayLength=oArray.length;rArrayLength=rArray.length;for(o=0;o<oArrayLength;o++){var counter=0;for(r=0;r<rArrayLength;r++){if(oArray[o]==rArray[r]){counter++;}}
if(counter<1){if(fArray.length>0){var fcounter=0;for(f=0;f<fArray.length;f++){if(oArray[o]==fArray[f]){fcounter++;}}
if(fcounter===0){fArray.push(oArray[o]);}}else{fArray.push(oArray[o]);}}}
return fArray;},numberFormat:function(number,formatting){formatting=formatting||{};formatting.sign="";number=Number(number);if(number<0){number=number*-1;formatting.sign="-";}
if(formatting.decimals!==null){number=number.toFixed(formatting.decimals);}
if(formatting.delimiter!==null){number+='';var x=number.split('.');var x1=x[0];var x2=x.length>1?'.'+x[1]:'';var rgx=/(\d+)(\d{3})/;while(rgx.test(x1)){x1=x1.replace(rgx,'$1'+formatting.delimiter+'$2');}
number=x1+x2;}
if(formatting.prefix!==null){number=formatting.sign+formatting.prefix+number;}
return number;}};var utils={flashBasketTotal:function(){$(".os-tbl .final td").stop(true).attr("style","");var bgColor=$(".os-tbl .final .subtotal").css("background-color");$(".os-tbl .final td").animate({backgroundColor:store.cssFlashColor},250,function(){$(".os-tbl .final td").animate({backgroundColor:bgColor},500);});},getCurrentOrderItemIds:function(){currentOrderItemIds=[];$.each($(".os-tbl tr.top, .os-tbl tr.extra"),function(){if($(this).find(".trash a").attr("href")){var orderItemId=$(this).find(".trash a").attr("href").match(/id=([0-9]+)/)[1];}
if(orderItemId){currentOrderItemIds.push(orderItemId);}});currentOrderItemIds=jsCore.arrayUnique(currentOrderItemIds);return currentOrderItemIds;},getXMLOrderItemIds:function(xml){XMLOrderItemIds=[];$.each($(xml).find("orderItemId"),function(){XMLOrderItemIds.push($(this).text());});return XMLOrderItemIds;},getXMLDomainPrivateRegStatus:function(xml){var privateRegStatus=[];$.each($(xml).find("orderItem"),function(){var orderItem=this;if($(orderItem).find("type").text()=="domain"){domainName=$(orderItem).find("domainName").text();itemId=$(orderItem).find("orderItemId").text();domainPriv=$(orderItem).find("privateReg").text();privateRegStatus.push({domain:domainName,orderItemId:itemId,privacy:domainPriv});}});return privateRegStatus;},getRowByOrderId:function(orderId){anchor=$.grep($(".os-tbl tr .trash .image-block"),function(a){return $(a).attr("href").match(orderId);});if(anchor.length){return $(anchor).parent().parent();}else{return false;}},getParentRow:function(row){var prevRow=$(row).prev();if($(row).attr("class")&&$(row).attr("class").match(store.regex.rowclass_main)){store.currentParentRowTemp=row;return null;}else{if(($(prevRow).attr("class")&&$(prevRow).attr("class").match(store.regex.rowclass_main))){store.currentParentRowTemp=$(prevRow);}else if($(prevRow).attr("class")&&$(prevRow).attr("class").match(store.regex.rowclass_sub)){utils.getParentRow($(row).prev());}else{return null;}}},getSubRows:function(row){var nextRow=$(row).next();if($(nextRow).attr("class")&&$(nextRow).attr("class").match(store.regex.rowclass_sub)){store.currentSubRowsTemp.push(nextRow);utils.getSubRows(nextRow);}},removeRow:function(row,opts){if(opts&&opts.keepRowVis){$(row).addClass("remkeptvis");$(row).find(".trash a").remove();$(row).find(".price").html("");$(row).find(".desc").html("");}else{$(row).addClass("removed");$(row).children().addClass(store.cssRemovingBgClass);$(row).children().fadeOut("slow",function(){$(this).parent().remove();if($(".os-tbl tr.top, .os-tbl tr.extra").length<1){$(".os-tbl .final").html('<td colspan="4" class="empty">Your order is empty</td>');$(".os-tbl #vouchers, .os-tbl #clearproceed").hide();}});}
if($(row).attr("class").match(store.regex.rowclass_sub)){utils.removeNoTrashParentWithNoSubs(row);}},removeNoTrashParentWithNoSubs:function(row){utils.resetStoredInfo(row,"reset");var parentRow=store.currentParentRowTemp;var subRows=store.currentSubRowsTemp;var subRowsRemoved=$(subRows).filter(function(){return $(this).attr("class").match(store.regex.rowclass_removed);});if(!$(parentRow).find("td.trash").length||!$(parentRow).find("td.trash a").length){var subRowsLeft=subRows.length-subRowsRemoved.length;if(subRowsLeft<1){utils.removeRow(parentRow,null);}}},resetStoredInfo:function(row,action){switch(action){case"reset":store.currentParentRowTemp=null;store.currentSubRowsTemp=[];utils.getParentRow(row);utils.getSubRows(store.currentParentRowTemp);break;default:store.currentParentRowTemp=null;store.currentSubRowsTemp=[];break;}},updateBasketItems:function(xml,removalOrderItemIds){$.each($(xml).find("orderItem"),function(){utils.updateItemPrice($(this).find("orderItemId").text(),$(this).find("netAfterDiscount").text(),$(this).find("privateReg").text(),$(this).find("rebillPeriod").text(),$(this).find("offerId").text(),$(this).find("voucherId").text());utils.updateItemOfferText($(this).find("orderItemId").text(),$(this).find("privateReg").text(),$(this).find("offerId").text(),$(this).find("voucherId").text());});$.each(removalOrderItemIds,function(){var orderItemId=Number(this);var row=utils.getRowByOrderId(orderItemId);if($(row).find(".trash a").attr("href").match(store.regex.querystring_type)[1]=="domain"){var parentRow=null;$.each(row,function(){if($(this).attr("class").match(store.regex.rowclass_main)){parentRow=this;}});utils.getSubRows(parentRow);if(store.currentSubRowsTemp.length>0){var domainRow=null;$.each(row,function(){if($(this).find(".trash a").attr("href").match(store.regex.querystring_type)[1]=="domain"){domainRow=this;}});utils.removeRow(domainRow,{keepRowVis:true});$(domainRow).find("td").removeClass(store.cssRemovingBgClass);$.each(store.currentSubRowsTemp,function(){if($(this).find(".product").text().match(store.regex.item_domainprivacy)){utils.removeRow(this,null);}});}else{utils.removeRow(row,null);}}else{utils.removeRow(row,null);}});},updateBasketTotal:function(xml){var oldBasketTotal=Number($(".os-tbl .final .subtotal").text().replace(/(\s)+(\D|,)/g,''));var runningTotal=Number(0);$.each($(xml).find("orderItem"),function(){itemCost=Number($(this).find("netAfterDiscount").text());runningTotal+=itemCost;});var newBasketTotal=runningTotal;if(newBasketTotal!=oldBasketTotal){if(newBasketTotal===0){newBasketTotal=Number(0);}
$(".os-tbl .final .subtotal").html(jsCore.numberFormat(newBasketTotal,{prefix:store.currencySymbol,delimiter:",",decimals:2}));utils.flashBasketTotal();if(window.raf){if(raf.link_code){$.get("/order/api/raf/get_spendcheck_message.php",{net_total:newBasketTotal,currency_id:raf.currency_id,currency_symbol:raf.currency_symbol,vat_status:raf.vat_status,vat_rate:raf.vat_rate},function(data){$('.errorBox .error p, .infoBox .info p').html(data);});}}}},updateDomainPrivacyItems:function(xml){domainPrivStatuses=utils.getXMLDomainPrivateRegStatus(xml);$.each(domainPrivStatuses,function(){var domainPrivStatus=this;$.each($(".os-tbl tr.extra"),function(){var row=this;var subItemId=$(row).find(".trash a").attr("href").match(store.regex.querystring_id)[1];var subItemType=$(row).find(".trash a").attr("href").match(store.regex.querystring_type)[1];if(domainPrivStatus.orderItemId==subItemId){if(subItemType=="privateReg"&&domainPrivStatus.privacy=="n"){utils.removeRow(row,null);}}});});},updateMessageBoxes:function(action){switch(action){case"hide":$(".errorBox, .infoBox, .notification").slideUp("def");break;case"show":$(".errorBox, .infoBox, .notification").slideDown("def");default:break}},updateItemPrice:function(id,price,privacy,rebillPeriod,offerId,voucherId){price=Number(price);rebillPeriod=Number(rebillPeriod);offerId=Number(offerId);voucherId=Number(voucherId);var row=utils.getRowByOrderId(id);if(row.length>1&&privacy=="y"){$.each(row,function(){if($(this).attr("class").match(store.regex.rowclass_main)){row=$(this);}});price-=(store.costDomPrivacyYearly*rebillPeriod);}
if(price===0){$(row).find(".price").html('<span class="free">FREE</span>');}else{var priceHtml=jsCore.numberFormat(price,{prefix:store.currencySymbol,delimiter:",",decimals:2});if((offerId!==0||voucherId!==0)&&price>=0){priceHtml='<span class="free">'+priceHtml+'</span>';}
$(row).find(".price").html(priceHtml);}},updateItemOfferText:function(id,privacy,offerId,voucherId){if((!offerId||offerId===0)||(!voucherId||voucherId===0)){var row=utils.getRowByOrderId(id);if(row.length>1&&privacy=="y"){$.each(row,function(){if($(this).attr("class").match(store.regex.rowclass_main)){row=this;}});}
if($(row).find(".product").text().match(/\(SPECIAL OFFER\)/)){var newProductHtml=$(row).find(".product").html().replace(/SPECIAL\sOFFER/,"");newProductHtml=newProductHtml.replace(/\(/,"");newProductHtml=newProductHtml.replace(/\)/,"");$(row).find(".product").html(newProductHtml);}}}};return{init:init};}();})();$(document).ready(function(){AjaxRemove.init();});