function initCurrency(strCurrency)
{var strStoredCurrency=$.cookie('VSCurrency');hideCurrencyPrices();if(!strStoredCurrency)
{toggleCurrency(strCurrency);}
else
{toggleCurrency(strStoredCurrency);}}
function hideCurrencyPrices()
{var arrCurrencies=$('#currency_options').children();for(var i=0;i<arrCurrencies.length;++i)
{$('#link_'+arrCurrencies[i].id).bind("click",function(){var strCurrency=this.id;strCurrency=strCurrency.replace(/link_currency_/,'');toggleCurrency(strCurrency);this.blur();return false;});}}
function toggleCurrency(strCurrencyCode)
{if($("#price_swap")&&$('#link_currency_'+strCurrencyCode).attr('name')=='N')
{$("#price_swap").css('visibility','hidden');}
else
{$("#price_swap").css('visibility','visible');}
if(strCurrencyCode==$.cookie('VSCurrency'))
{return;}
var arrCurrencies=$('#currency_options').children();for(var i=0;i<arrCurrencies.length;++i)
{if(document.getElementById('link_'+arrCurrencies[i].id))
{document.getElementById('link_'+arrCurrencies[i].id).className='';var strCurrency=arrCurrencies[i].id;strCurrency=strCurrency.replace(/currency_/,'');$("."+strCurrency).hide();$("#img_currency_active_"+strCurrency).hide();$("#img_currency_inactive_"+strCurrency).show();$("#extra_img_currency_active_"+strCurrency).hide();$("#extra_img_currency_inactive_"+strCurrency).show();}}
document.getElementById('link_currency_'+strCurrencyCode).className='active';if($('#link_currency_'+strCurrencyCode).attr('name')=='N')
{if($("#price_swap"))
{$("#price_swap").css('visibility','hidden');}}
if($.cookie('VSCurrency')!=strCurrencyCode)
{$.cookie('VSCurrency',strCurrencyCode,{expires:+365,path:'/',domain:window.location.hostname});}
$("."+strCurrencyCode).show();$("#img_currency_active_"+strCurrencyCode).show();$("#img_currency_inactive_"+strCurrencyCode).hide();$("#extra_img_currency_active_"+strCurrencyCode).show();$("#extra_img_currency_inactive_"+strCurrencyCode).hide();}
function refreshCurrency()
{hideCurrencyPrices();toggleCurrency($.cookie('VSCurrency'));}
function removeItem(iKey)
{if(confirm("Are you sure you want to remove this item from your basket?\n\nClick \"OK\" for Yes or \"Cancel\" for No"))
{return true;}
else
{return false;}}
function initQuickBuy()
{if($('.quickbuybox').length>0)
{
$('.quickbuybox').colorbox({onLoad:function(){$('#cboxClose').hide();},onClosed:function(){$('#cboxClose').show();},iframe:true,title:' ',width:'620',height:'500'});}}
var booleanEmailAsk = false;
function initEmailAsk()
{
  if($('.emailaskbox').length>0)
  {
  	if(booleanEmailAsk == false){

  	booleanEmailAsk = true;

	    $('.emailaskbox').colorbox(
	    {
	      onLoad: function(){$('#cboxClose').hide();},
	      onClosed: function(){$('#cboxClose').show();},
	      iframe:true,
	      title:' ',
	      innerWidth:'820',
	      innerHeight:'540'
	    });
	}
  }
}
function initVat()
{if($('#vat_options'))
{var arrVatOptions=$('#vat_options').children();for(var i=0;i<arrVatOptions.length;++i)
{$('#link_'+arrVatOptions[i].id).bind("click",function(){var strDisplayPrices=this.id;strDisplayPrices=strDisplayPrices.replace(/link_vat_options_/,'');refreshVat();this.blur();return false;});}}
if($('#price_swap'))
{$("#price_swap").bind("click",function(){toggleVat(false);this.blur();return false;});}
toggleVat($.cookie('VSVatPrices'));}
function refreshVat()
{var strDisplayPricesCookie=$.cookie('VSVatPrices');if(strDisplayPricesCookie=='')
{return;}
toggleVat($.cookie('VSVatPrices'));}
function toggleVat(strDisplayPrices)
{if(strDisplayPrices==false)
{if($.cookie('VSVatPrices')=='inc')
{strDisplayPrices='ex';}
else
{strDisplayPrices='inc';}}
if(strDisplayPrices=='ex')
{$("#link_vat_options_ex").addClass('active');$("#link_vat_options_inc").removeClass('active');$(".ex").show();$(".inc").hide();if($("#vat_basket_row"))
{$("#vat_basket_row").show();}}
else
{$("#link_vat_options_inc").addClass('active');$("#link_vat_options_ex").removeClass('active');$(".ex").hide();$(".inc").show();if($("#vat_basket_row"))
{$("#vat_basket_row").hide();}}
if($.cookie('VSVatPrices')!=strDisplayPrices)
{$.cookie('VSVatPrices',strDisplayPrices,{expires:+365,path:'/',domain:window.location.hostname});}}
$(document).ready(function()
{initQuickBuy();initEmailAsk();});
function updateMiniBasket(strJSON)
{
  if(strJSON==null)
  {
    $.getJSON('/ajax/getbasket/true',function(strJSON)
    {
      updateMiniBasket(strJSON);
     });
   }
   else
   {
     $('#basket_items').hide();
     $('#mini_basket_num_items,.mini_basket_num_items').text(strJSON.basket_num_items);

     $('#mini_basket_total,.mini_basket_total').text('');
     $('#mini_basket_total,.mini_basket_total').append(strJSON.product_total);
     $('#basket_items').fadeIn();
     $('#quick_buy_cancel').hide();

      if(strJSON.note!=undefined&&strJSON.note!='')
      {
        $('#add_note').remove();
        $('#added_to_basket').append('<span id="add_note"><strong>Please Note: '+strJSON.note+'</strong></span>');
      }
      $('#added_to_basket').fadeIn();
      window.setTimeout("$('#added_to_basket').fadeOut()",3500);
    }
  };

function setCourier(iCountryID,iCourierId,blnRender)
{strPresonalisationQueryString=''
$(".courier_cost").hide();$("#courier_cost_"+iCountryID+'_'+iCourierId).show();$("#courier_vat_"+iCountryID+'_'+iCourierId).show();$("#courier_discounts_"+iCountryID+'_'+iCourierId).show();$("#courier_subtotal_"+iCountryID+'_'+iCourierId).show();$("#courier_total_"+iCountryID+'_'+iCourierId).show();if($('#top_container_basket_cost').length!=0)
{$('#top_container_basket_cost').html($("#courier_total_"+iCountryID+'_'+iCourierId).html());}
$(".courier_information").hide();if($("#courier_information_"+iCountryID+'_'+iCourierId).html()!=''){$("#courier_information_"+iCountryID+'_'+iCourierId).fadeIn();}
if(typeof(blnRender)!='undefined'&&blnRender)
{if($('#basket_total').length==0||$('#basket_sub_total').length==0)
{$.post('/ajax/setCourierID/'+iCourierId,function(strData)
{$('#shipping_cost_container').html(strData);});}
else
{$('#basket_page_loading').remove();$('#basket_page_right').append('<div id="basket_page_loading"></div>');$.getJSON('/ajax/setCourierID/'+iCourierId+'/'+iCountryID+'/true/true',function(strJSON)
{$('#basket_delivery_price').html(strJSON.delivery);$('#basket_discount_price').html(strJSON.discount);$('#basket_sub_total').html(strJSON.sub_total);$('#basket_total').html(strJSON.total);$('#basket_page_loading').remove();});}}
else
{$.post('/ajax/setCourierID/'+iCourierId);}};function changeQuantity(iKey,strDirection)
{if(strDirection=='plus')
{if(((arrBasket[iKey]['quantity']+1)>arrBasket[iKey]['stock'])&&arrBasket[iKey]['stock']!=-1)
{alert(arrBasket[iKey]['title']+'\n\nSorry, we currently only have '+arrBasket[iKey]['stock']+' of this item in stock');return false;}
else
{return true;}}
else if(strDirection=='minus')
{if((arrBasket[iKey]['quantity']==1))
{if(confirm("Are you sure you want to remove this item from your basket?\n\nClick \"OK\" for Yes or \"Cancel\" for No"))
{return true;}
else
{return false;}
return false;}
else
{return true;}}
return true;};function setupPopupLinks()
{$(".popup").unbind('click');$(".popup").click(function(){var iHeight=0;var iWidth=0;if(this.name!='')
{var arrBits=this.name.split('x');if(!isNaN(arrBits[0])&&!isNaN(arrBits[1]))
{iWidth=arrBits[0];iHeight=arrBits[1];}}
if(iHeight==0||iWidth==0)
{iHeight=780;iWidth=680;}
popupWindow(this.href,iHeight,iWidth);return false;});};function setRegistrationDetails(blnRepopulate)
{if($('#delivery_address_no').attr('checked'))
{$('.delivery').addClass('field_disabled');$('#delivery_full_name').val($('#full_name').val());$('#delivery_title').val($('#title').val());$('#delivery_firstname').val($('#firstname').val());$('#delivery_surname').val($('#surname').val());$('#delivery_postcode').val($('#postcode').val());$('#delivery_company_name').val($('#company_name').val());$('#delivery_house_number').val($('#house_number').val());$('#delivery_address1').val($('#address1').val());$('#delivery_address2').val($('#address2').val());$('#delivery_town').val($('#town').val());$('#delivery_county').val($('#county').val());$('#delivery_country').val($('#country').val());$('#delivery_telephone').val($('#telephone').val());}
else
{$('.delivery').removeClass('field_disabled');}}
function popupWindow(url,width,height)
{LeftPosition=(screen.width)?(screen.width-500)/2:0;TopPosition=(screen.height)?(screen.height-500)/2:0;settings='height='+height+',width='+width+',top='+TopPosition+',left='+LeftPosition+',toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1';win=window.open(url,null,settings);return false;}
function setCountry(iCountryID,blnUpdateDeliveryMethods,blnRender)
{if(iCountryID>0&&!isNaN(iCountryID))
{$.ajax({async:false,type:'POST',url:'/ajax/setCountryID/'+iCountryID});if(blnUpdateDeliveryMethods)
{$('#courier_id').attr('disabled',false);if($('#delivery_prompt').length>0)
{$('#delivery_prompt').hide();}
if($('#delivery_label').length>0)
{$('#delivery_label').show();}
if(arrCouriersForCountry[iCountryID].length>0)
{var arrCourierIDs=arrCouriersForCountry[iCountryID].split(',');blnSelect=false;if(typeof(document.getElementById('delivery_country_id').options)!='undefined')
{arrCountryOptions=document.getElementById('delivery_country_id').options;for(var i=0;i<arrCountryOptions.length;i++)
{if(arrCountryOptions[i].value=='')
{document.getElementById('delivery_country_id').remove(i);}}}
if($('#delivery_country_id').hasClass('error_border'))
{$('#delivery_country_id').removeClass('error_border');}
if(document.getElementById('courier_id').options&&document.getElementById('courier_id').options.length>0)
{document.getElementById('courier_id').options.length=0;}
for(var i=0;i<arrCourierIDs.length;i++)
{var newoption=document.createElement('option');newoption.text=arrCouriers[arrCourierIDs[i]];newoption.value=arrCourierIDs[i];if(typeof(arrCouriers[arrCourierIDs[i]])!='undefined'&&typeof(arrCourierIDs[i])!='undefined')
{document.getElementById('courier_id').add(newoption,newoption.selectedIndex);if(!blnSelect)
{if(typeof(blnRender)!='undefined'&&blnRender)
{setCourier(iCountryID,arrCourierIDs[i],true);}
else
{setCourier(iCountryID,arrCourierIDs[i]);}
blnSelect=true;}}}}
else
{$('#courier_id').attr('disabled',true);$(".courier_cost").hide();$(".courier_information").hide();if($(".courier_cost_noship"))
{$(".courier_cost_noship").show();}}}}}
function setCourier(iCountryID,iCourierId,blnRender)
{strPresonalisationQueryString='';if($('#top_container_basket_cost').length!=0)
{$('#top_container_basket_cost').html($("#courier_total_"+iCountryID+'_'+iCourierId).html());}
$(".courier_information").hide();if($("#courier_information_"+iCountryID+'_'+iCourierId).html()!='')
{$("#courier_information_"+iCountryID+'_'+iCourierId).fadeIn();}
if(typeof(blnRender)!='undefined'&&blnRender)
{$.ajax({async:false,dataType:'',url:'/ajax/setCourierID/'+iCourierId+'/'+iCountryID+'/true/true',success:function(strData)
{$.each(strData,function(i,response)
{switch(i)
{case"sub_total":$('#basket_sub_total').html(response);break;case"delivery":$('#shipping_cost_container').html(response);break;case"discount":$('#discount_total').html(response);break;case"vat":if(response==0)
{$('#vat_basket_row').hide();}
else
{$('#vat_basket_row').show();$('#vat_total').html(response);}
break;case"total":$('#basket_total').html(response);break;}});}});}
else
{$.ajax({async:false,type:'POST',url:'/ajax/setCourierID/'+iCourierId});}}
function getDeliveryDates(iCountryID,iCourierId)
{if(document.getElementById('delivery_date').options&&document.getElementById('delivery_date').options.length>0)
{if(document.getElementById('delivery_date').options.length!=0)
{}}
$("#delivery_date_row").hide();$('#delivery_date_row').attr('disabled',true);var i=0;$.post('/ajax/setShippingDate/');try
{$.ajax({async:true,dataType:'',url:'/ajax/getCouriersDeliveryDates/?courier_id='+iCourierId+'&country_id='+iCountryID,success:function(response)
{arrCourierDeliveryDates[iCourierId]=new Array();arrCourierDeliveryDates[iCourierId][iCountryID]=new Array();if(response)
{$.each(response.delivery_dates,function(key,value)
{arrCourierDeliveryDates[iCourierId][iCountryID][key]=value;});}}});if(arrCourierDeliveryDates[iCourierId][iCountryID]&&arrCourierDeliveryDates[iCourierId][iCountryID].length>0)
{var arrDeliveryDates=arrCourierDeliveryDates[iCourierId][iCountryID];$("#delivery_date_row").show();$('#delivery_date_row').attr('disabled',false);var keys=new Array();for(k in arrDeliveryDates)
{keys.push(k);}
keys.sort(function(a,b)
{return(a>b)-(a<b);});$('#delivery_date').attr('disabled',true);var options='';for(var i=0;i<keys.length;i++)
{var key=keys[i];var option;if(i==0)
{$.post('/ajax/setShippingDate/'+key);var setDate=new Date(key*1000);if($("#named_delivery_text").length)
{sDatePrefix='th';if(setDate.getDate()==1||setDate.getDate()==31)
{sDatePrefix='st';}
else if(setDate.getDate()==2)
{sDatePrefix='nd';}
else if(setDate.getDate()==3)
{sDatePrefix='rd';}
$("#named_delivery_text").text(setDate.getDayName()+', '+setDate.getDate()+sDatePrefix+' '+setDate.getMonthName());}}
$("#delivery_date_row").show();option=$("<option></option>").attr("value",key).text(arrDeliveryDates[key]);options+=$('<div>').append(option.clone()).remove().html();}
$('#delivery_date').html(options);$('#delivery_date').attr('disabled',false);$("#delivery_date_row").show();}}
catch(err)
{return false;}}
function setDeliveryDate(iDeliveryDate){if(iDeliveryDate!='null'){$.post('/ajax/setShippingDate/'+iDeliveryDate);}};function updateQuantity(iKey,iQuantity)
{if(((iQuantity)>arrBasket[iKey]['stock'])&&arrBasket[iKey]['stock']!=-1)
{alert(arrBasket[iKey]['title']+'\n\nSorry, we currently only have '+arrBasket[iKey]['stock']+' of this item in stock, your basket has been updated to reflect this.');$('#product_quantity_'+iKey).val(arrBasket[iKey]['stock']);return false;}
else
{return true;}}
var arrPausePromoUnits=Array();var arrCancelPromoUnits=Array();var arrCurrentBanners=Array();var iBannerIDOverride=null;function pausePromoUnitFade(strPromoUnitName)
{arrPausePromoUnits[parseInt(strPromoUnitName)]=0;}
function resumePromoUnitFade(strPromoUnitName,iInterval,iMouseoutInterval)
{arrPausePromoUnits[parseInt(strPromoUnitName)]=1;}
var promoUnitFadeTimer=null;function initPromoUnitFade(strPromoUnitName,iStartingBannerID,iInterval,iMouseoutInterval,blnPauseOnHover)
{if(!blnPauseOnHover){blnPauseOnHover='true';}
arrPausePromoUnits[parseInt(strPromoUnitName)]=1;arrCancelPromoUnits[parseInt(strPromoUnitName)]=0;promoUnitFadeTimer=window.setTimeout("triggerRunPromoUnitFade('"+strPromoUnitName+"', '"+iStartingBannerID+"', '"+iInterval*1000+"')",iInterval*1000);if(blnPauseOnHover=='true')
{$('.'+strPromoUnitName+'fade').attr('onmouseover','pausePromoUnitFade("'+strPromoUnitName+'");');$('.'+strPromoUnitName+'fade').attr('onmouseout','resumePromoUnitFade("'+strPromoUnitName+'", "'+iInterval*1000+'", "'+iMouseoutInterval+'");');}
$('.'+strPromoUnitName+'thumbs > a').bind('click',function()
{clearInterval(promoUnitFadeTimer);arrCancelPromoUnits[parseInt(strPromoUnitName)]=1;arrPausePromoUnits[parseInt(strPromoUnitName)]=1;runPromoUnitFade(strPromoUnitName,parseInt(this.name)-1,-1);return false;});}
function triggerRunPromoUnitFade(strPromoUnitName,iStartingBannerID,iInterval){if(arrCancelPromoUnits[parseInt(strPromoUnitName)]==0)
{promoUnitFadeTimer=window.setTimeout("runPromoUnitFade('"+strPromoUnitName+"', '"+iStartingBannerID+"', '"+iInterval+"');",iInterval);}
else
{arrCancelPromoUnits[parseInt(strPromoUnitName)]=0;}}
function runPromoUnitFade(strPromoUnitName,iStartingBannerID,iInterval)
{iNewBannerID=parseInt(iStartingBannerID)+parseInt(1);if(!$("#"+strPromoUnitName+iNewBannerID).length){iNewBannerID=0;}
if(arrPausePromoUnits[parseInt(strPromoUnitName)]==1){arrCurrentBanners[strPromoUnitName]=iNewBannerID;$("#"+strPromoUnitName+iStartingBannerID).fadeOut(Math.ceil(iInterval/(iInterval/1000)));$("."+strPromoUnitName+'fade').fadeOut(Math.ceil(iInterval/(iInterval/1000)));$("#"+strPromoUnitName+iNewBannerID).fadeIn(Math.ceil(iInterval/(iInterval/1000)));if(iStartingBannerID<0)iStartingBannerID=0;if($('#thumb_'+strPromoUnitName+iStartingBannerID).length>0)
{$("."+strPromoUnitName+'thumbs').removeClass('selected');$("#thumb_"+strPromoUnitName+iNewBannerID).addClass('selected');}}
if(iInterval>0){triggerRunPromoUnitFade(strPromoUnitName,iNewBannerID,iInterval);}}
var iLastPriceBreakPointer=0;function togglePriceBands(iPriceBreakQuantity,strElementID)
{while($('#'+strElementID+'_'+iPriceBreakQuantity).length==0&&iPriceBreakQuantity>0)
{iPriceBreakQuantity--;}
if(iPriceBreakQuantity>0)
{if(iPriceBreakQuantity!=iLastPriceBreakPointer)
{$('.'+strElementID).hide();$('#'+strElementID+'_'+iPriceBreakQuantity).show();}}
iLastPriceBreakPointer=iPriceBreakQuantity;}


function initReviewStarRater()
{if($('#review_star_1').length)
{var iPointer=1;while($('#review_star_'+iPointer).length)
{$('#review_star_'+iPointer).bind("click",function(){var strCurrentWidth=this.innerHTML;var strNewWidth=strCurrentWidth*20+'%';$('#review_star_user_rating').css('width',strNewWidth);$('#review_form_rating').focus();$('#review_form_rating').val(strCurrentWidth);$('#review_form_rating').blur();$('#review_form_subject').focus();$('#review_form_rating_errors').hide();var iMessagePointer=1;while($('#rating_message_'+iMessagePointer).length)
{$('#rating_message_'+iMessagePointer).hide();iMessagePointer++;}
$('#rating_message_'+strCurrentWidth).show();return false;});iPointer++;}}}

function initReviewMultiStarRater()
{
  if($('#rating_star_1').length)
  {
    var iPointer = 1;

    while($('#rating_star_' + iPointer).length)
    {
      $('#rating_star_'  + iPointer + ' li>a').bind("click", function()
      {
        //parent().parent() is fine here as the html won't change.
        $(this).parent().parent().removeClass('onestar twostar threestar fourstar fivestar');

        //Set the answer to this question
        var strAnswerInput = $(this).parent().parent().attr('id').replace('rating_star_','review_answer_');
        $('#'+strAnswerInput).val(this.innerHTML);

        switch(this.innerHTML)
        {
          case "1":
            $(this).parent().parent().addClass('onestar');
            break;

          case "2":
            $(this).parent().parent().addClass('twostar');
            break;

          case "3":
            $(this).parent().parent().addClass('threestar');
            break;

          case "4":
            $(this).parent().parent().addClass('fourstar');
            break;

          case "5":
            $(this).parent().parent().addClass('fivestar');
            break;
        }

        iPercent = this.innerHTML * 20;
        el = $(this).parent().parent();
        $('#' + $(el).attr('id') + ' li.current-rating').css('width', iPercent + '%');

        return false;
      });

      iPointer++;

    }
  }
}

function initOrderReviewMultiStarRater()
{
	$('#review_items ul').each(function(i,e)
	{

	//console.log(e);

	$(e).each(function()
	{
     strTempId = $(this).attr('id').replace('rating_star_','');
     strProductId = $(this).attr('id').replace('rating_star_','');
   	 strProductId = strProductId.substring(0, strProductId.length - 2);

	if($('#rating_star_' + strTempId).length)
	{
	    while($('#rating_star_' + strTempId).length)
    	{
    		$('#rating_star_'  + strTempId + ' li > a').each(function(i,e)
			{
				$( e ).bind("click", function()
				{
					//parent().parent() is fine here as the html won't change.
					$(this).parent().parent().removeClass('onestar twostar threestar fourstar fivestar');

					//Set the answer to this question
					var strAnswerInput = $(this).parent().parent().attr('id').replace('rating_star_'+strProductId+'_','review_answer_'+strProductId+'_');
					$('#'+strAnswerInput).val(this.innerHTML);

					switch(this.innerHTML)
			        {
			          case "1":
			            $(this).parent().parent().addClass('onestar');
			            break;

			          case "2":
			            $(this).parent().parent().addClass('twostar');
			            break;

			          case "3":
			            $(this).parent().parent().addClass('threestar');
			            break;

			          case "4":
			            $(this).parent().parent().addClass('fourstar');
			            break;

			          case "5":
			            $(this).parent().parent().addClass('fivestar');
			            break;
			        }

					iPercent = this.innerHTML * 20;
					el = $(this).parent().parent();
					$('#' + $(el).attr('id') + ' li.current-rating').css('width', iPercent + '%');

					return false;

				});
			});
			return false;
		}
	}
	});
	});
}

function alterQuantity(iIncrement,iMinQuantity,iMaxQuantity,strElementID)
{simpleAlterQuantity(iIncrement,iMinQuantity,iMaxQuantity,strElementID);togglePriceBands(iNewQuantity,'price_break');}
function simpleAlterQuantity(iIncrement,iMinQuantity,iMaxQuantity,strElementID)
{iCurrentQuantity=parseInt($("#"+strElementID).val());iNewQuantity=parseInt(iCurrentQuantity)+parseInt(iIncrement);if(iNewQuantity<iMinQuantity)
{iNewQuantity=iMinQuantity;}
if(iMaxQuantity>0&&iNewQuantity>iMaxQuantity)
{alert('Sorry, we currently only have '+iMaxQuantity+' of this item in stock.');iNewQuantity=iMaxQuantity;}
$("#"+strElementID).val(iNewQuantity);}
