
var bIsFirebugReady = (!!window.console && !!window.console.log);

	$(document).ready(
		function (){
			
	checkCurrency();
			
		}
	);
	
			
	function checkCurrency()
	{
	    var c, exRate, v;
        c = readCookie('currency');
        v = readCookie('vatCookie');
        //if cookie is empty default to pound 
        if (c == "" )
        {
        createCookie('currency', 'pound', 365);
           
        }   
        else if (c == "euro")
        {
        //if cookie is set to euro then do euro pricing
            europricing();
            //createCookie("currency",euro,30);
        }
        if (v == "")
        {
        //default not to include VAT on prices
            createCookie('vatCookie', 'no', 365);
            
        }
        else if (v == "yes")
        {
        //include vat to prices
            includeVAT();
        }
        else{
           
        }
    }
	
	
	function europricing()
	{
	    if (readCookie('vatCookie') == "yes")
	    {
	        removeVAT();
	    }
	   
	       var cActive =$("#euro").attr("class");
    	    
    	   
            $(".VatPrice1").text("€0.00");
            $(".poundPrice").css("display", "none");
            $(".euroPrice").css("display", "block");
	       if (cActive != 'active')
	       { 
	        var exrate,price;
	        var newPrice = 0;
	        exRate = document.getElementById("exchangeRate");
    	   
	       
	       $(".price").each(function(i,domEle){
	          price = $(this).text();
	       //price = $("[@id^=price_]").parseNumber();
	       	  
	        price = removeFormatLong(price); 
	        price = parseFloat(price);  
	        newPrice = (((price*100)/exRate.value)/100).toFixed(2);
	        newPrice = addSpaces(newPrice);
        
            $(this).text("€" + newPrice);});

    	    //pricing on basket
    	    $(".baskprice").each(function(i,domEle){
	         price = $(this).text();
	         price = removeFormatLong(price); 
	         price = parseFloat(price);  
	         newPrice = (((price*100)/exRate.value)/100).toFixed(2);
	         newPrice = addSpaces(newPrice);
             $(this).text("€" + newPrice);});
    	    
    	    
    	    $("#euro").addClass("active");
    	    $("#pound").removeClass("active");
	        createCookie('currency', 'euro', 365);
	        }
	    
	}
	
	function poundpricing()
	{
	var cActive =$("#pound").attr("class");
	
	  $(".poundPrice").css("display", "block");
      $(".euroPrice").css("display", "none");  
	  $(".VatPrice1").text("£0.00");
	   if (cActive != 'active')
	   { 
	var exrate,price;
	var newPrice = 0;
	exRate = document.getElementById("exchangeRate");
	  $(".price").each(function(i,domEle){
	  price = $(this).text();
	  price = removeFormat(price);
	  price = parseFloat(price);
	  newPrice =(price*exRate.value).toFixed(2);
	  newPrice = addCommas(newPrice);
	  
	    
	    $(this).text("£" + newPrice);});
    	
    	//pricing on basket
    	 $(".baskprice").each(function(i,domEle){
	     price = $(this).text();
	     price = removeFormat(price);
	     price = parseFloat(price);
	     newPrice =(price*exRate.value).toFixed(2);
	     newPrice = addCommas(newPrice);
	     $(this).text("£" + newPrice);});
	    
	    
      $("#euro").removeClass("active");
      $("#pound").addClass("active");
    	createCookie('currency', 'pound', 365);
    	}
	}
	
	function includeVAT()
	{
	   if (readCookie('currency') != 'euro')
	   {
	       var cActive =$("#withVAT").attr("class");
	       if (cActive != 'active')
	       { 
	        var vatrate,price,curr;
	        var newPrice = 0;
	        vatrate = document.getElementById("vatRate");
	       if (isEuro()) {curr ='€';}else{curr='£';}
	       $(".price").each(function(i,domEle){
	        price = $(this).text();
	       
	        price = removeFormatLong(price);
	        price = parseFloat(price);
	        newPrice = (price + (price * ((vatrate.value)/100))).toFixed(2);
	        newPrice = addCommas(newPrice);
	       
	         //newPrice = parseFloat(newPrice);
	        $(this).text(curr + newPrice);});
            $("#withoutVAT").removeClass("active");
    	    $("#withVAT").addClass("active");   
    	    $(".vatText").text("(inc.vat)");   
    	    $(".poundPrice").css("display", "block");
            $(".euroPrice").css("display", "none");
    	    createCookie('vatCookie', 'yes', 365);
	        }
	    }
	}
	function removeVAT()
	{
	     
	   var cActive =$("#withoutVAT").attr("class");
	   if (cActive != 'active')
	   { 
	    var vatrate,price, curr;
	    var newPrice = 0;
	    vatrate = document.getElementById("vatRate");
	    if (isEuro()) {curr ='€';}else{curr='£';}
	   $(".price").each(function(i,domEle){
	     price = $(this).text();
         price = removeFormatLong(price);	  
         price = parseFloat(price);
	     newPrice = (price/((1+vatrate.value)/100)).toFixed(2);
	     if (isEuro()) { newPrice = addSpaces(newPrice);}else{ newPrice = addCommas(newPrice);}
	     
	    $(this).text(curr + newPrice);});
        $("#withVAT").removeClass("active");
    	$("#withoutVAT").addClass("active"); 
    	$(".vatText").text("(exc.vat)");	
    	$(".VatPrice1").css("display", "block");
    	$(".VatPrice").css("display", "none");
    	//$(".basketTcost").text(curr + ($(".basketTcost").sum()/((1+vatrate.value)/100)));
    	createCookie('vatCookie', 'no', 365);
	    }
	}
	function hideVAT()
	{
	 //  $(".VatPrice").css("display", "none"); 
	  // $(".basketTcost").text(curr + ($(".basketTcost").sum()/((1+vatrate.value)/100)));
	}
	function getOrigUnitPrice(uPrice)
	{
	 var v, c, exrate, vatrate;
	 exRate = document.getElementById("exchangeRate");
	 vatrate = document.getElementById("vatRate");
	 c = readCookie('currency');
	 v = readCookie('vatCookie');
	  uPrice = removeFormat(uPrice);
	    if ( v == "yes")
	    {
	      uPrice = (uPrice/((1+vatrate.value)/100)).toFixed(2);
	       
	    }
	    
	    if ( c == "euro")
	    {
	     uPrice = (uPrice*exRate.value).toFixed(2);
	    }
	    
        return uPrice;
	}
	function isEuro()
	{
	var csymbol;
	    csymbol = readCookie('currency');
	if (csymbol == 'euro')return true;
	else return false;
	}
	
	
function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function addSpaces(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ' ' + '$2');
	}
	x2 = x2.replace(".",",");
	return x1 + x2;
}
function removeFormat(nStr)
{   
  
	nStr += '';
	x = nStr.split(',');
	x1 = x[0];
	x2 = x.length > 1 ? ',' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ' ' + '$2');
		
	}
	x1 = x1.replace(/[^\d\.-]/g,'');
	x2 = x2.replace(",",".");
	return x1 + x2;
}
function removeFormatLong(nStr)
{   
  
	nStr += '';
	x = nStr.split(',');
	x1 = x[0];
	x2 = x.length > 1 ? ',' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ' ' + '$2');
		
	}
	x1 = x1.replace(/[^\d\.-]/g,'');
	x2 = x2.replace(",","");
	return x1 + x2;
}
// cookie functions http://www.quirksmode.org/js/cookies.html
function createCookie(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
function eraseCookie(name)
{
	createCookie(name,"",-1);
}
// /cookie functions
