
(function($){var _itemID="";var _quoteID="";var _tradeID="0";$.initialiseAjax=function(){$.ajaxSetup({error:function(xhr){alert('Error: '+xhr.status+' '+xhr.statusText);}})}
$.itradesReviewSetup=function(){$('#submit_review').bind('click',function(event){if(validateAddReview()){document.theForm.submit();}else{$('#itrades_add_review_error').show();}});}
function validateAddReview(){var bValid=true;var s='';s=$('#txtName').val();if(s.length==0){$('#itrades_add_review_error').html('Please enter your name.');bValid=false;}
s=$('#txtEmail').val();if(s.length<3){$('#itrades_add_review_error').html('Please enter a valid email address.');bValid=false;}else{if(s.search('@')==-1){$('#itrades_add_review_error').html('Please enter a valid email address.');bValid=false;}}
s=$('#txtLocation').val();if(s.length==0){$('#itrades_add_review_error').html('Please enter your location.');bValid=false;}
if($('#lstPrice').val()=='-1'||$('#lstQuality').val()=='-1'||$('#lstTidyness').val()=='-1'||$('#lstOverall').val()=='-1'){$('#itrades_add_review_error').html('Please rate this company on all four categories.');bValid=false;}
return bValid;}
$.itradesSearchSetup=function(){$('#itrades_home_tab_1').bind('click',function(event){clearTabSelection();$('#itrades_home_tab_1').addClass("selected");$('#itrades_home_listbox1').show();});$('#itrades_home_tab_2').bind('click',function(event){clearTabSelection();$('#itrades_home_tab_2').addClass("selected");$('#itrades_home_listbox2').show();});$('#theForm').submit(function(){return false;});$('.itrades_search_selection').bind('click',function(event){var objID=$(event.target).attr("id");try{$('#opt'+_tradeID).css("background-color","#fefbda");$('#opt'+_tradeID).css("color","#000");}catch(e){}
_tradeID=objID.replace('opt','');$('#'+objID).css("background-color","#EF5A28");$('#'+objID).css("color","#FFF");});$('#itrades_search_btn').bind('click',function(event){if(validateiTradesSearch()){window.location="/itrades/search-results.aspx?t="+_tradeID+"&a="+$('#txtArea').val();}});}
function validateiTradesSearch(){var bValid=true;if(_tradeID=='0'){$('#itrades_search_error').html('You need to select a trade or service from the list.');$('#itrades_search_error').show();bValid=false;}else{var s='';s=$('#txtArea').val();if(s.length<2){$('#itrades_search_error').html('Enter your postcode or county.');$('#itrades_search_error').show();bValid=false;}else if(s.search('<')>-1||s.search('>')>-1){$('#itrades_search_error').html('Invalid entry.');$('#itrades_search_error').show();bValid=false;}}
return bValid;}
function clearTabSelection(){$('#itrades_home_tab_1').removeClass("selected");$('#itrades_home_tab_2').removeClass("selected");$('#itrades_home_listbox1').hide();$('#itrades_home_listbox2').hide();}
$.createCommentsTracker=function(){$('#txtComment').keyup(function(){limitChars('txtComment',500,'charlimitinfo');})}
function limitChars(textid,limit,infodiv){var text=$('#'+textid).val();var textlength=text.length;if(textlength>limit){$('#'+infodiv).html('You have exceeded the max '+limit+' characters limit.');$('#'+textid).val(text.substr(0,limit));return false;}else{$('#'+infodiv).html('You have '+(limit-textlength)+' characters left.');return true;}}
$.createDeleteLinks=function(){$('.deleteItem').bind('click',function(event){var objID=$(event.target).attr("id");var answer=confirm("Are you sure you want to delete this item?");if(answer){$.post('/deleteitem.aspx',{id:objID},function(data){document.theForm.submit();});}});}
$.createUnDeleteLinks=function(){$('.undeleteItem').bind('click',function(event){var objID=$(event.target).attr("id");$.post('/undeleteitem.aspx',{id:objID},function(data){document.theForm.submit();});});}
$.createBasketDeleteLinks=function(){$('.deleteItem').bind('click',function(event){var objID=$(event.target).attr("id");var answer=confirm("Are you sure you want to delete this item?");if(answer){$.post('/deletebasketitem.aspx',{id:objID},function(data){document.theForm.submit();});}});}
$.createDeleteQuoteLinks=function(){$('.deleteItem').bind('click',function(event){var objID=$(event.target).attr("id");var answer=confirm("Are you sure you want to delete this item?");if(answer){$.post('/controlpanel/deletequote.aspx',{id:objID},function(data){document.theForm.submit();});}});}
$.createAdvertLinks=function(){$('.deleteItem').bind('click',function(event){var objID=$(event.target).attr("id");var answer=confirm("Are you sure you want to suspend this advert?");if(answer){$.post('/controlpanel/deleteadvert.aspx',{id:objID},function(data){document.theForm.submit();});}});$('.approveItem').bind('click',function(event){var objID=$(event.target).attr("id");$.post('/controlpanel/approveadvert.aspx',{id:objID},function(data){document.theForm.submit();});});}
$.createDeleteUserLinks=function(){$('.deleteItem').bind('click',function(event){var objID=$(event.target).attr("id");var answer=confirm("Are you sure you want to delete this user?");if(answer){$.post('/controlpanel/deleteuser.aspx',{id:objID},function(data){document.theForm.submit();});}});}
$.createOrderLinks=function(){$('.deleteItem').bind('click',function(event){var objID=$(event.target).attr("id");var answer=confirm("Are you sure you want to delete this order?");if(answer){$.post('/controlpanel/deleteorder.aspx',{id:objID},function(data){document.theForm.submit();});}});$('.process').bind('click',function(event){var objID=$(event.target).attr("id");$.post('/controlpanel/processorder.aspx',{id:objID},function(data){document.theForm.submit();});});}
$.createAddLinks=function(){$('.addToBasket,.iproduct_addbtn_style1,.iproduct_addbtn_style2').bind('click',function(event){var objID=$(event.target).attr("id");var objQty="#"+objID.replace('btnAdd','txtQty');var itemcount=0;var iQuantifyArea=0;if(objID=='btnAddQ0I1'){iQuantifyArea=$('#txtIQuantifyArea').val();if(iQuantifyArea>0){itemcount=iQuantifyArea;$('#msgIQuantify').html('Service has been added to your basket.');}else{alert('Invalid floor area specified.');}}else{try{itemcount=$(objQty).val();}catch(e){}}
if(itemcount>0){$.post('/addtobasket.aspx',{id:objID,numitems:itemcount},function(data){if(data.length>4){var dataSplit=data.split(",");if(dataSplit[2]=='-1'){$('#msgQ'+dataSplit[0]+'I'+dataSplit[1]).html('! quantity too low');}else{if(dataSplit[0]=='0'){$('#msgQ'+dataSplit[0]+'I'+dataSplit[1]).html(dataSplit[2]+' item(s) added');}else{$('#msgQ'+dataSplit[0]+'I'+dataSplit[1]).html(dataSplit[2]+' item(s) in basket');}}}
$.post('/basketsummary.aspx',{i:0},function(data){if(data.length>2){var dataSplit=data.split("|");$('#basketItemCount').html('<a href="basket.aspx">'+dataSplit[0]+' item(s)</a>');$('#basketTotalPrice').html(dataSplit[1]);}});});}});}
$.createAddQuoteLinks=function(){$('.iproduct_quotebtn_style1,.iproduct_quotebtn_style2').bind('click',function(event){var objID=$(event.target).attr("id");var objQty="#"+objID.replace('btnQuote','txtQty');var itemcount=0;try{itemcount=$(objQty).val();}catch(e){}
if(itemcount>0){location.href='iplumb.aspx?id='+objID.replace('btnQuoteQ0I','')+'&qty='+itemcount;}});}
$.iquantifySetup=function(){$('#txtIQuantifyArea').keyup(function(event){this.value=this.value.replace(/[^0-9]/g,'');});}
$.createCopyAddress=function(){$('input#copy-details').change(function(){if($(this).attr("checked")){copyAddress();return;}
clearAddress();});$('input#copy-details').click(function(){if($(this).attr("checked")){copyAddress();return;}
clearAddress();});}
function copyAddress(){$('#del1').val($('#bill1').val());$('#del2').val($('#bill2').val());$('#del3').val($('#bill3').val());$('#del4').val($('#bill4').val());$('#del5').val($('#bill5').val());$('#del6').val($('#bill6').val());$('#del7').val($('#bill7').val());$('#del8').val($('#bill8').val());$('#del9').val($('#bill9').val());}
function clearAddress(){$('#del1').val('');$('#del2').val('');$('#del3').val('');$('#del4').val('');$('#del5').val('');$('#del6').val('');$('#del7').val('UK');$('#del8').val('');$('#del9').val('');}
$.refreshBasketSummary=function(){$.post('/basketsummary.aspx',{i:0},function(data){if(data.length>2){var dataSplit=data.split("|");$('#basketItemCount').html('<a href="/basket.aspx">'+dataSplit[0]+' item(s)</a>');$('#basketTotalPrice').html(dataSplit[1]);}});}
$.refreshQuoteItems=function(){var quoteid=null;quoteid=$.jqURL.get('id');if(quoteid!=null){if(quoteid.length>0){$.post('/refreshquoteitems.aspx',{id:quoteid},function(data){if(data.length>0){var dataSplit=data.split(",");var intCount=0;var strDiv="";for(i=0;i<dataSplit.length;i++){if(intCount==0){strDiv='#msgQ'+dataSplit[i];}else if(intCount==1){strDiv=strDiv+'I'+dataSplit[i];}
intCount++;if(intCount==3){try{$(strDiv).html(dataSplit[i]+' items in basket');}catch(e){}
intCount=0;strDiv="";}}}});}}}
$.start_database_cleanup=function(){$.post('/controlpanel/cleanup.aspx',{action:1},function(data){$('#loginWindow').append('<p>'+data+' quotation data files were removed.</p>');$.post('/controlpanel/cleanup.aspx',{action:2},function(data){$('#loginWindow').append('<p>'+data+' basket data files were removed.</p>');$('#loginWindow').append('<p>&nbsp;</p><p>Re-directing to <a href="iimprove-quotes.aspx">Control Panel</a>...</p>');var t=setTimeout("location.href='iimprove-quotes.aspx'",5000);});});}
$.createPriceLinks=function(){$('.itemprice').bind('click',function(event){var objID=$(event.target).attr("id");var intPos=0;intPos=objID.search("I");_quoteID=objID.substr(1,intPos-1);_itemID=objID.substr(intPos+1);$('#txtSupplier').val('');$.post('/controlpanel/pricedata.aspx',{itemID:_itemID},function(data){if(data.length>0){var dataSplit=data.split("|");if($.trim(dataSplit[1])==''||$.trim(dataSplit[1])=='0.00'){dataSplit[1]='20';}
if($.trim(dataSplit[0])=='0.00'){dataSplit[0]='';}
$('#overlaybox').show();$("#overlayDialog").center();$('#overlayDialog').show();document.theForm.txtBought.focus();$('#txtBought').val($.trim(dataSplit[0]));$('#txtMarkup').val($.trim(dataSplit[1]));$('#txtPrice').val($.trim(dataSplit[2]));$('#lstSupplier').val(dataSplit[3]).change();}});});}
$.createDescLinks=function(){$('.itemdesc').bind('click',function(event){var objID=$(event.target).attr("id");var intPos=0;intPos=objID.search("D");_quoteID=objID.substr(1,intPos-1);_itemID=objID.substr(intPos+1);$.post('/controlpanel/descdata.aspx',{itemID:_itemID},function(data){if(data.length>0){var dataSplit=data.split("|");$('#overlaybox').show();$("#overlayDialog2").center();$('#overlayDialog2').show();document.theForm.txtItemDescription.focus();$('#txtItemDescription').val($.trim(dataSplit[0]));$('#txtItemInfo').val($.trim(dataSplit[1]));}});});}
$.createPriceDialog=function(){$('#overlayDialogSave').bind('click',function(event){$.post('/controlpanel/saveitemprice.aspx',{itemid:_itemID,bought:$('#txtBought').val(),markup:$('#txtMarkup').val(),supplier:$('#lstSupplier').val(),newsupplier:$('#txtSupplier').val(),price:$('#txtPrice').val()},function(data){if(data.length==0){alert('Changes were not saved. Please check that your entries are valid.');}else if(data=='-1'){location.href='/controlpanel/index.aspx';}else if(data=='-2'){alert('Please specify a new supplier name.');}else{hideDialog();location.reload();}});});$('#overlayDialogCancel').bind('click',function(event){hideDialog();});$('#txtMarkup').keyup(function(event){this.value=this.value.replace(/[^0-9\.]/g,'');recalculatePrice();});$('#txtBought').keyup(function(event){this.value=this.value.replace(/[^0-9\.]/g,'');recalculatePrice();});}
$.createDescDialog=function(){$('#overlayDialogSave2').bind('click',function(event){$.post('/controlpanel/saveitemdesc.aspx',{itemid:_itemID,desc:$('#txtItemDescription').val(),info:$('#txtItemInfo').val()},function(data){if(data.length==0){alert('Changes were not saved. Please check that your entries are valid.');}else if(data=='-1'){location.href='/controlpanel/index.aspx';}else{hideDialog();location.reload();}});});$('#overlayDialogCancel2').bind('click',function(event){hideDialog();});}
function recalculatePrice(){var markupPct=0;var boughtPrice=0;var customerPrice=0;var vatAmount=0;try{markupPct=parseFloat($('#txtMarkup').val());}catch(e){markupPct=0;}
try{if($('#txtBought').val()==''){boughtPrice=0;}else{boughtPrice=parseFloat($('#txtBought').val());}
customerPrice=(boughtPrice+((boughtPrice/100)*markupPct));customerPrice=roundVal(customerPrice);$('#txtPrice').val(displayAsCurrency(customerPrice.toString()));}catch(e){$('#txtPrice').val('');}}
function roundVal(val){var dec=2;var result=Math.round(val*Math.pow(10,dec))/Math.pow(10,dec);return result;}
function displayAsCurrency(s){var i=-1;var sCurrency='';var sPence='';i=s.indexOf('.');if(i>-1){sPence=s.substr(i+1);if(sPence.length==1){sCurrency=s+"0";}else if(sPence.length==2){sCurrency=s;}else if(sPence.length>2){sCurrency=s.substr(0,i+3);}}else{sCurrency=s+".00";}
return sCurrency;}
function hideDialog(){$('#overlayDialog').hide();$('#overlayDialog2').hide();$('#overlaybox').hide();}
jQuery.fn.center=function(){this.css("position","absolute");this.css("top",($(window).height()-this.height())/2+$(window).scrollTop()+"px");this.css("left",($(window).width()-this.width())/2+$(window).scrollLeft()+"px");return this;}
$.createRevealLinks=function(){$('#reveal1').bind('click',function(event){if($('#iwindowsReveal1').is(':visible')){$('#iwindowsReveal1').hide('fast');if($('#iwindowsReveal2').is(':visible')==false){$('#iwindowsReveal3').hide('fast');}}else{$('#iwindowsReveal1').show('fast');if($('#iwindowsReveal3').is(':visible')==false){$('#iwindowsReveal3').show('fast');}}});$('#reveal2').bind('click',function(event){if($('#iwindowsReveal2').is(':visible')){$('#iwindowsReveal2').hide('fast');if($('#iwindowsReveal1').is(':visible')==false){$('#iwindowsReveal3').hide('fast');}}else{$('#iwindowsReveal2').show('fast');if($('#iwindowsReveal3').is(':visible')==false){$('#iwindowsReveal3').show('fast');}}});}})(jQuery);
