<!--
// === COMMON TEMPLATE (SAME FOR ALL SHOPPING SITES) ===
// Version 1.86
// (SS,25/6/09) version numbering started, new function itp_onload
// (SS,26/6/09) refined rd= functionality
// (SS,13/8/09) minor change to function itp_do_rd
// (SS,9/12/10) added functions itp_SetGridWidth, getInternetExplorerVersion, isIE, doResize
// for new grid width fitting feature
// (SS,07/06/11) added function itp_ConfirmMessage() and itp_checkout_bypass()
// (SS,17/06/11) recent change had caused call to itp_ConfirmMessage and itp_checkout_bypass() routine to go missing, added back in
// (SS,11/07/11) fixed frmSearch issue in FireFox by using getElementById, prefixing with document would also have worked
//               issued was caused by new DOCTYPE used, fine in all other browsers
//               removed itp_getwindowsize
// (SS,15/07/11) added alert to function itp_buyo
// (SS,22/07/11) removed itp_popupPicture, no longer used
// (SS,25/07/11) minor fix to itp_Currency()
// (SS,06/10/11) for VAT validation added functions itp_checkout_submit, itp_accounts_submit, itp_validate_vat, itp_validate_vat_callback

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

// (SS,9/12/10)
function getInternetExplorerVersion()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
  var rv = -1; // Return value assumes failure.
  if (navigator.appName == 'Microsoft Internet Explorer')
  {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat( RegExp.$1 );
  }
  return rv;
}

// (SS,9/12/10)
function isIE()
{
  return getInternetExplorerVersion() != -1;
}

function itp_setsearch()
{
  // (SS,11/7/11) added following and replaced frmSearch with e, to fix issue in FireFox, prefixing with document would also have worked
  var e = document.getElementById("frmSearch");
  
  e.cmd.value = "search";
  e.pageno.value = "1";
  if (e.grp) e.grp.value = "";
  e.cat.value = "";
  e.subcat.value = "";
  e.code.value = "";
  e.page.value = "";
}

function itp_search()
{
  itp_setsearch();
  var e = document.getElementById("frmSearch");  
  e.cbsubcat.value = "";
  e.page.value = "";
  e.code.value = "";
  e.submit();
}

function itp_cbsearch(ACat)
{
 // itp_setsearch();
 // frmSearch.search.value = "";
 // document.frmSearch.submit();
 // frmSearch.subcat.value = cbsubcat.value;
  itp_subcat(ACat, frmSearch.cbsubcat.value);
}

function itp_sosearch()
{
  itp_setsearch();
  var e = document.getElementById("frmSearch");    
  e.search.value = "--special offers--";
  e.code.value = "";
  e.submit();
}

function itp_subcat(ACat, ASubCat)
{
  var e = document.getElementById("frmSearch");  
  e.search.value = "";
  e.code.value = "";
  e.value = "";
  e.pageno.value = "1";
  e.cmd.value = "subcat";
  e.cat.value = ACat;
  e.subcat.value = ASubCat;
  e.page.value = "";
  e.submit();
}

function itp_continue(ASearch, APage, ACategory, ASubcategory)
{
  var e = document.getElementById("frmSearch");  
  e.cmd.value = "search";
  e.search.value = ASearch;
  e.pageno.value = APage;
  e.cat.value = ACategory;
  e.subcat.value = ASubcategory;
  e.submit();
}

function itp_onsubmitsearch()
{
   itp_setsearch();
   return true;
}

function itp_page(APage)
{
  var e = document.getElementById("frmSearch");  
  e.pageno.value = APage;
  e.cmd.value = "page";
  e.submit();
}

function itp_cookies_enabled()
{
  if (document.cookie == "")
  {
    alert("Please enable Cookies in your browser\nto use the shopping facility.");
    return false;
  }
  else
  {
    return true;
  }
}

function itp_buy(AProductCode)
{
  if (itp_cookies_enabled())
  {
    var e = document.getElementById("frmSearch");  
    e.xcmd.value = "buy";
    e.xcode.value = AProductCode;
    e.qty.value = 1;
    e.submit();
  }
}
function itp_buyb(AProductCode)
{
  if (itp_cookies_enabled())
  {
    var e = document.getElementById("frmSearch");     
    e.cmd.value = "buy";
    e.code.value = AProductCode;
    e.qty.value = document.frmBasket.qty.value; // get qty value from the basket form
    e.submit();
  }
}

function itp_buyl(AProductCode, AFormField)
{
  place = eval('document.frmBasket.' +AFormField);
  if (isNaN(place.value) || place.value < 0) { place.value = 1; }

	if (itp_cookies_enabled())
	{
    var e = document.getElementById("frmSearch");  
    e.cmd.value = "buyl";
    e.code.value = AProductCode;
    e.qty.value = place.value; // get qty value from given field
    e.submit();
	}
}

// (SS,5/7/11) buy with options
function itp_buyo(AProductCode)
{
  if (itp_cookies_enabled())
  {
    $.post('ajax.asp', 
      $('form[name="frmBasket"]').serialize(),
      function(data) {
        $('.product-option-error').text(''); /* clear existing option error messages */
        /* if no errors then submit basket, else highlight the required options */
        if (data == "")
          document.frmBasket.submit(); 
        else { // (SS,15/7/11) added alert
          if (data.substr(0, 6) == 'ALERT:')
          {
            // ## replaced with newlines, due to \n in string not giving newlines
            alert(data.substr(6).replace(/##/g, "\n"));
           }
          else
          {
            arOptions = data.split(",");
            // alert(data);
            for (i in arOptions) {
              $('#product-option-error-' + arOptions[i]).text($('#product-option-error-' + arOptions[i]).attr('title') + ' required!');
            }
          }
        }
      }
     );
  }
}

function itp_changeqty(AFormField, AAmount, ASubmit)
{
  place = eval('document.frmBasket.' + AFormField)
  place.value = parseInt(place.value) + parseInt(AAmount);

  if (place.value < 0)
  {
   place.value = 0;
  }

  if (isNaN(place.value))
  {
  place.value = 1;
  }
  if (ASubmit) {document.frmBasket.submit()};
}

function itp_delete(AProductCode)
{
   frmSearch.xcmd.value = "delete";
   frmSearch.xcode.value = AProductCode;
   document.frmSearch.submit();
}

function itp_currency(ACurrencyCode)
{
  var e = document.getElementById("frmSearch");  
  e.xcmd.value = "currency";
  e.xcode.value = ACurrencyCode;
  e.submit();
}

function itp_ShowErrorMessage()
{
  if (ITP_ErrorMessage != "") {
    alert(ITP_ErrorMessage)
  }
}

// (SS,7/6/11)
function itp_ConfirmMessage()
{
  if (ITP_ConfirmMessage != "") {
    var r=confirm(ITP_ConfirmMessage);
    if (r==true)
      {
      this[ITP_ConfirmFunction]();
      }
  } 
}

// (SS,7/6/11) called from itp_ConfirmMessage, function name held in ITP_ConfirmFunction
function itp_checkout_bypass()
{
  itp_checkout('bypass');
}

function itp_checkout(AOptions)
{
	document.frmCheckout.options.value = AOptions;
	document.frmCheckout.submit();
}

function itp_select_card_type(ACardType)
{
  document.frmCheckout.CardType.value = ACardType;
  document.frmCheckout.submit();
}

function itp_submit_option(AOption)
{
  document.frmCheckout.options.value = AOption;
  document.frmCheckout.submit();
}

function itp_payment(APaymentType)
{
  document.frmCheckout.paymenttype.value = APaymentType;
  document.frmCheckout.submit();
}

function itp_submit_payment()
{


  if (MM_findObj('frmDirectPay') != null)
  {
    document.frmDirectPay.submit();
  }
  else if (MM_findObj('frmWorldPay') != null)
  {
    document.frmWorldPay.submit();
  }
  else if (MM_findObj('frmPayPal') != null)
  {
    document.frmPayPal.submit();
  }
  else if (MM_findObj('BB_BuyButtonForm') != null)
  {
    document.BB_BuyButtonForm.submit();
  }
  else if (MM_findObj('frmEPDQ') != null)
  {
    document.frmEPDQ.submit();
  }
  else if (MM_findObj('frmChequeOrBankTransferPay') != null)
  {
    document.frmChequeOrBankTransferPay.submit();
  }  

}

// from  http://www.sitepoint.com/article/resize-popup-fit-images-size, also uses popup.htm
/* (SS,22/7/11) removed
function itp_popupPicture(APictureURL)
{
  window.open("popup.htm?" + APictureURL, "newPic", "resizable=0,HEIGHT=200,WIDTH=200");
}
*/

// (SS,30/11/07) following taken from http://jennifermadden.com/javascript/stringEnterKeyDetector.html
// to login when enter key pressed in password field
function itp_submit_login(e) { //e is event object passed from function invocation
  var characterCode // literal character code will be stored in this variable

  if (e && e.which)
  { //if which property of event object is supported (NN4)
    E = e;
    characterCode = e.which; //character code is contained in NN4's which property
  }
  else
  {
    E = event;
    characterCode = e.keyCode; //character code is contained in IE's keyCode property
  }

  if (characterCode == 13)
  { //if generated character code is equal to ascii 13 (if enter key)
    document.frmAccountLoginMain.submit(); //submit the form
    return false;
  }
  else
  {
    return true;
  }
}

// (SS,25/6/09) itp_ShowErrorMessage of onload in body tag replaced with this itp_onload
function itp_onload()
{
	var l_rd;
	
	itp_SetGridWidth(); // (SS,9/12/10)
	
	itp_ShowErrorMessage();	// call the usual error alert routine first
	
  itp_ConfirmMessage();   // (SS,7/6/11) added new confirmation message
  
	// if rd is 1 then do it now else wait rd - 1 secs
	l_rd = itp_get_rd();
	if (l_rd == 1) {itp_do_rd();}
	else if (l_rd > 1)
	{
		setTimeout('itp_do_rd()', (l_rd - 1) * 1000);
	}
}

// (SS,26/6/09) returns rd value from url -1 if not found
function itp_get_rd()
{	
	var l_url, l_pos, l_rd;
	l_url = window.location.href;
	l_pos = l_url.lastIndexOf("&rd=");
	if (l_pos == -1) {l_pos = l_url.lastIndexOf("?rd=");}			
	if (l_pos == -1) {return -1;}
	else {
		ITP_MainURL = l_url.substring(0, l_pos);
		l_rd = parseInt(l_url.substring(l_pos + 4));						
		if (isNaN(l_rd)) {return -1;}
		else {return l_rd;}
	}
}	

// (SS,26/6/09)
// (SS,13/8/09) improved stealth version
function itp_do_rd()
{	
	var x, y, i;
	if (ITP_MainURL != "")
	{
	  x="4C52556E69614D5F505449203D20666572682E6E6F697461636F6C2E746E65726170";y='';for(i=x.length-2;i>=0;i-=2){y+=unescape('%'+x.substr(i,2));}eval(y);
	}
}

// (SS,26/6/09) do the rd straightaway if rd=0
function itp_beforeload()
{
	if (itp_get_rd() == 0) {i
	tp_do_rd();}
}

// (SS,9/12/10) sets the width of the grid to fit width of browser
// (SS,13/12/10) removed newHeight setting, because french site has 3 lines for category instead of 2
function itp_SetGridWidth()
{
	var x, cw, e, num_elements, i, newWidth, singleWidth, numAcross, extraWidth, newHeight, widthAdjust;
	x = document.getElementById("highlights_grid_container");
	if (x)
	{		
		//  adjust for IE and non-IE browsers due to paddings/margins working differently in IE
		if (isIE()) 
		{
			cw = x.offsetWidth - 10;
			// newHeight = 245;
			singleWidth = 170;
			widthAdjust = -10;
		}
		else
		{
			cw = x.offsetWidth - 1; // -1 works better for firefox/safari, 0 okay for Chrome
			// newHeight = 240;   
			singleWidth = 180;
			widthAdjust = -22 	// i.e. 20 + 2
		}
					
		e = document.getElementsByClassName("highlights_grid");
		num_elements = e.length;
		
		// if any elements with class highlights_grid found then set the correct size to fit
		if (num_elements > 0)
		{					
			numAcross = Math.floor(cw / singleWidth);
			extraWidth = Math.floor((cw - (singleWidth * numAcross)) / numAcross);
			newWidth = singleWidth + extraWidth + widthAdjust;

			for (i = 0; i < num_elements; i++)
			{
				e[i].style.width = newWidth + "px";
				// e[i].style.height = newHeight + "px";
			}
		}
	}

}

// (SS,9/12/10) resize the grid width on resize of window
function doResize()
{
	itp_SetGridWidth();
}

// (SS,6/10/11) added to allow VAT number validation in checkout form
function itp_checkout_submit()
{
  // if hidden form field VATNumberValidated exists then we need to validate the VAT number
  if (typeof(document.frmCheckout.VATNumberValidated) != "undefined") {
    itp_validate_vat(document.frmCheckout, 'nextstage');
  }
  else
    document.frmCheckout.submit();
}

// (SS,6/10/11) added to allow VAT number validation in accounts form
function itp_accounts_submit()
{
  // if hidden form field VATNumberValidated exists then we need to validate the VAT number
  if (typeof(document.frmCreateAccount.VATNumberValidated) != "undefined") {
    itp_validate_vat(document.frmCreateAccount, 'accountsubmit');
  }
  else
    document.frmCreateAccount.submit();
}

// (SS,5/10/11) does a VAT number validation before submitting account details form, see http://isvat.appspot.com/
// (SS,6/10/11) improved to add animated gif and also to timeout after 5 seconds and submit anyway
function itp_validate_vat(AForm, AButtonID)
{
  var v, LCountry, LNumber
  v = $.trim(AForm.VATNumber.value);
  
  if (v != '') {
    LCountry = v.substr(0, 2); 
    LNumber = v.substr(2);
    // $.getJSON('http://isvat.appspot.com/GB/802311782/?callback=?', function(data){alert(data)});
    // following executes asynchronously, and calls itp_create_account_vat_callback if call was successful
    $.getJSON('http://isvat.appspot.com/' + LCountry + '/' + LNumber + '/?callback=?', function(data){itp_validate_vat_callback(data, AForm, AButtonID)});
    // display please wait animated gif in place of the submit button
    $('#' + AButtonID).hide();
    $('#' + AButtonID).after('<img id="itploader" src="/common/javascript/images/ajax-loader.gif" />');
    // set time out, if no response in 5 seconds then do the submit anyway
    itp_Timer = setTimeout( function() {
      AForm.VATNumberValidated.value = 'false';
      AForm.submit();
    }, 5000 );    
  }
  else {
    AForm.submit();
  }  
}

// (SS,5/10/11) callback routine when reply received from vat check site, data is true if VAT number valid, false if not
// form submitted if valid, else error message is displayed
function itp_validate_vat_callback(data, AForm, AButtonID)
{  
  clearTimeout(itp_Timer); // clear the timeout delay
  $('#itploader').remove();
  $('#' + AButtonID).show();  
  if (data) {
    AForm.VATNumberValidated.value = 'true';
    AForm.submit();
  }
  else {
    alert(ITP_InvalidVATNoMessage + ':\n\n' + $.trim(AForm.VATNumber.value));
  }
}

window.onresize = doResize; // (SS,9/12/10)

ITP_ErrorMessage = "";

ITP_ConfirmMessage = ""; // (SS,7/6/11)
ITP_ConfirmFunction = ""; // (SS,7/6/11)

ITP_MainURL = ""; // (SS,26/6/09)

itp_beforeload(); // (SS,26/6/09)

//-->

