


$(document).ready(function() {
   
   $("#cookiesno").click(function(){
      SetUseCookies('Y');  
   });  
   $("#cookiesyes").click(function(){
         SetUseCookies('N');   
   }); 
   
   
   $("#zonedd").change(function(){
      window.location.href = $(this).find(":selected").val() ; 
    }); 
    
   $("#currencydd").change(function(e){
     if( $("#orderform").length) {  
        e.preventDefault();
        $("#orderform").attr('action',$(this).find(":selected").val());
        $("#orderform").submit();   // in order to preserve entered address details 
        return false;  
     }
     else 
      window.location.href = $(this).find(":selected").val() ; 
    }); 
  
      
    
   $(".btn.search").click(function(){
             $(".product-search").toggle(50);
   });  
    
  $("#buybtn").click( function() {  // returns basketline for pc version 
    // first check if varprice was entered and that this is valid
     var varprice = 0.0;  
     $(".varprice").each(function(i, obj) {
        varprice = parseFloat($(this).val());  
        if ( !(varprice == 0))  
         if (isNaN(varprice)||(varprice < 5)||(varprice > 1000)) {
            alert('Please enter valid value ($5-$1000) for this item.');  
            $(this).focus(); 
            exit();
        }
      }); 
   
    var quant = 0;
    $(".val-quantity").each(function(i, obj) {
       quant = quant + parseInt($(this).val());  
    });
 
    if (quant > 0) {
      $.post( "/shop/basket/781873922/AC=ADDAJAX,REF=" ,  $("#shopOrderForm").serialize(),
              function(data) { 
                if (!data.trim()) {
                 $("#basketline").html("-- empty --");  
                 $("#mobilebasketitems").html("0");
                }
                else   {
                 var ln = data.trim();
                 $("#mobilebasketitems").html(ln.substr(0,ln.indexOf(" ")));
                 alert(data+ ' now in Basket');   
                 $("#basketline").html(data);
                 $("#basketheadline").html("<a href='/shop/basket/781873922.317'>Basket/Checkout</a>");
                }
     });
    }
    else           
        alert('Please enter Quantity');
  });
   
  $(".buyonebtn").click(function(){                         // id=bbtn  
     var qty = 1; 
     var ino =  this.id;
     ino = ino.substr(4,15);
     if($("#qty"+ino).length > 0) 
       qty =   +$("#qty"+ino).val();
     
       

     $.post( "/shop/basket/781873922/AC=ADDAJAX,REF="+ino , 'SHSINO1='+qty,    // SHSINO1  is case sensitive!!! for additemstocookie
              function(data) { 
                if (!data.trim()) {
                 $("#basketline").html("-- empty --");  
                 $("#basketheadline").html("Basket/Checkout");
                 $("#mobilebasketitems").html("0");
                }
                else   {
                 var ln = data.trim();
                 $("#mobilebasketitems").html(ln.substr(0,ln.indexOf(" ")));
                 alert(data+ ' now in Basket');   
                 $("#basketline").html(data);
                 $("#basketheadline").html("<a href='/shop/basket/781873922.2201'>Basket/Checkout</a>");
                }
                // now change button to in Basket
                $("#spanbbtn"+ino).html('<a  class="btn btn-xs btn-custom float-right" href="/shop/spi/781873922.2719/RTURN___'+ino+'">'+qty+' in Basket</a>');
              }
     ); 
  }); 
 
 $("#backbtn").click(function(){
 window.location.href="/shop/spi/781873922.1796/_#it_"; 
  }); 

 $("#printbtn").click(function(){   // print item 
   print();
 });
 
 
 $("#reviewbtn").click(function(){
     gotoreviews();
 });
 
 $("#stfbtn").click(function(){   // send item to friend popup
   $.get( "/shop/stf/781873922" , 
              function(data) { 
                      $("#modal-message").html(data);
              } 
    );
 $('#modalmsg').modal('show');
  });
  
 $('.nav-toggle').click(function(){
            //get collapse content selector
            var collapse_content_selector = $(this).attr('href');

            //make the collapse content to be shown or hide
            var toggle_switch = $(this);
            $(collapse_content_selector).toggle("fast", function(){
                if($(this).css('display')=='none'){
                    //change the button label to be 'Show'
                    toggle_switch.html('&#9776; Show Menu');
                }else{
                    //change the button label to be 'Hide'
                    toggle_switch.html('&#9776; Hide Menu');
                }
            });
         });
         
 // Load images after page content is loaded.
 // Loops img elements with attribute data-srcnm and adds src tag and sets it with value of data-srcnm 
 $('img[data-srcnm]').each(function(index){
                $(this).attr({
                    "src" :   $(this).data('srcnm')
                });
 });
 
       

}); 



function sendstfbtnclick() {
    $.post( "/shop/stf/781873922/it=,s=y" ,  $("#stfform").serialize(),
              function(data) { 
                   $("#modal-message").html(data);
     });
};

function subtoembtnclick() {
    $.get( "/shop/emconfsub/781873922/STBcfint");
    $('#modalmsg').modal('hide');
};

function copylink() {
	    if (document.getElementById('linkdevurl').innerHTML == "")
           document.getElementById('linkdevurl').innerHTML = "<small>/shop/spi/781873922/RTURN_"+ document.getElementById('itemno').value;
		else
		   document.getElementById('linkdevurl').innerHTML = "";
};	

function gotoItem(ino) {  //not used ....
 window.location.href = "/shop/spi/781873922/RTURN___"+ino ; 
}

function gotoreviews()  {  // jumps to revies accordeon
 $('html, body').animate({
        scrollTop: $("#reviewspanel").offset().top
    }, 1000);
 $("#acc-review").collapse("show");  
};

function addreview(revno)  {                                // brings up form popup
    $.get('/shop/review/781873922/ac=start,rno='+revno+'', function(data) { 
                                     $("#modal-message").html(data);
     });
     $('#modalmsg').modal('show');
};

function updatereview() {  // updates reviews or adds new one
    $.post( "/shop/review/781873922/ac=upd" ,  $("#reviewform").serialize(),
              function(data) { 
                   $("#modal-message").html(data);
                //  location.reload();  
     });
};
function checkforreload() {   //modal if close butto should trigger page reload as for review 
  if (  $("#reviewthanksdiv").length > 0)
   location.reload();  
}

function togglediffdeladdr(isshowing) {
  if (isshowing)  
   $("#diffdeliveryaddress").removeClass('hidden');
  else
   $("#diffdeliveryaddress").addClass('hidden');
};


function setzoneandcurrency(targ,selObj,restore){ //v3.0
    eval(targ+".location.href='"+selObj.options[selObj.selectedIndex].value+"'");
    if (restore) 
         selObj.selectedIndex=0;
}
  

function showpagemodal(url) {
 $.get(url, function(data) { 
             $("#modal-message").html(data);
     });
     $('#modalmsg').modal('show');
}

var  SetUseCookies = function(yesno) {  
  var url = '/shop/basket/781873922/setUseCookie='+yesno+'';
  $.get(url, function(data) { 
         if (data.trim()  == 'ok')  
               $("#cookie-consent").hide(); 
     });
};


function openwin(fname) {
                         thewindow = window.open('HTTP://shop.theshop.co.nz/RTURN/'+fname , '_new',  config='height=500,width=600,toolbar=0,menubar=0,scrollbars=1,resizable=yes,location=0,directories=0,status=0,titlebar=0');
                         self.thewindow.focus();
}
    
function securitycodepp() 	{ //v2.0
	 thewindow = window.open('http://shop.theshop.co.nz/securitycodeinfo.htm', 'anew',  config='height=450,width=500,toolbar=no,menubar=no,scrollbars=yes,resizable=yes,location=center,directories=no,status=no,titlebar=no');
}
	
function shownzpostcodefinder() {
				pcwindow = window.open('http://www.nzpost.co.nz/Cultures/en-NZ/OnlineTools/PostCodeFinder', 'anew',  config='height=650,width=650,toolbar=no,menubar=no,scrollbars=yes,resizable=yes,location=center,d irectories=no,status=yes');
				window.pcwindow.focus();
}


