var gbpRate;
var eurRate = "1"; // base rate
var usdRate;
var errorRate;


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);
}

function viewCart()
{
	var url = window.parent.location.href;
	createCookie("pte_lasturl",url,1);
	window.location.href= "/basket/webscr.php";
}

function add_to_cart() {
	var url = window.parent.location.href;
	createCookie("pte_lasturl",url,1);
    document.addcartform.submit();
}

function loadCart()
{
	var setvalue = readCookie("pte_shipping");
	var x= document.getElementById('s1');
	//console.log(setvalue);
	if (setvalue == null)
	{
		var caption = null;
		var imageGroup = false;
		var url = "select.html?TB_iframe=true&height=300&width=500&modal=true";
		tb_ie7fix();
		tb_show(caption,url,imageGroup);
		//createCookie("pte_shipping","United Kingdom",2);
		//setvalue = "United Kingdom"
	}
	else
	{
	 	if(x != null)
	 	{
			for (i=0;i<x.length;i++)
			{
				if(x.options[i].text == setvalue) 
				{
					x.options[i].selected= true;
					break;
				}
			}
			UpdateValue();
    		UpdatePrice();
	 	}
	}
}

function loadHome()
{
	
	var setvalue = readCookie("pte_shipping");
	if (setvalue == null)
	{
		var caption = null;
		var imageGroup = false;
		var url = "select.html?TB_iframe=true&height=300&width=500&modal=true";
		tb_show(caption,url,imageGroup);
		//createCookie("pte_shipping","United Kingdom",2);
		//setvalue = "United Kingdom"
	}

}

function select(name)
{
	var x= document.getElementById('s1');
	var setvalue = readCookie("pte_shipping");

	createCookie("pte_shipping",name,365);
	setvalue = name;
	
	if(x != null)
	 	{
			for (i=0;i<x.length;i++)
			{
				if(x.options[i].text == setvalue) 
				{
					x.options[i].selected= true;
					break;
				}
			}
			UpdateValue();
    		UpdatePrice();
	 	}
	 	
	tb_remove()
	
}

function UpdateValue(mode)
{
	//var objHidden = null;
	var objHidden = 1;
	var objDdl = null;
	var objCc = null;
	var val = '';
	var shipping = readCookie("pte_shipping");
	//console.log(shipping);

	objDdl = document.getElementById('s1');
	objCc = document.getElementById('cc');
	//objHidden = document.getElementById('s2');
	objtax = document.getElementById('tax');

	if( (objDdl != null) && (objHidden != null) )
	{
		val = objDdl.options[objDdl.selectedIndex].value.toString();
		txt = objDdl.options[objDdl.selectedIndex].text.toString();
		//objHidden.value = val;
		
		if (txt == "United Kingdom") 
		{
			objtax.value = "17.5";
			objCc.value = "GBP";
			//console.log("hello");
		}
		if (txt == "Europe") 
		{
			objtax.value = "0";
			objCc.value = "EUR";
			//console.log("hello");
		}
		if (txt == "United States") 
		{
			objtax.value = "0";
			objCc.value = "USD";
			//console.log("hello");
		}
		
		if (txt == "Rest of the world") 
		{
			objtax.value = "0";
			objCc.value = "USD";
			//console.log("hello");
		}
		createCookie("pte_shipping",txt,365);
		//console.log('2' + shipping);
		UpdatePrice();
		destroySession(mode, shipping);

	}

}

function UpdatePrice()
{

	var objHidden = null;
	var objDdl = null;
	var val = '';

	objDdl = document.getElementById('o1');
	objHidden = document.getElementById('os0');

	if( objDdl != null )
	{
		val = objDdl.options[objDdl.selectedIndex].value.toString();
		txt = objDdl.options[objDdl.selectedIndex].text.toString();
		objHidden.value = txt;
		if( errorRate == "0" )
		{
			document.getElementById('txtPrice').innerHTML = setRate(val);
		} 
		else 
		{	
			document.getElementById('txtPrice').innerHTML = '£'+val+ ' ex vat';
		}
        document.getElementById('amount').value = val;
		
	}

}

function setRate(amount)
{
	var newRate = getNewRate()
	var cal = amount / gbpRate * newRate;
	var cal = cal.toFixed(2);
	var txt = readCookie("pte_shipping");
	var newPrice;
	
	if (txt == "United Kingdom") 
	{
		newPrice = '£'+cal+ ' GBP (Ex VAT)';
	}
	if (txt == "Europe") 
	{
		newPrice = '€'+cal+ ' EUR' ;
	}
	if (txt == "United States") 
	{
		newPrice = '$'+cal+ ' USD' ;
	}
	if (txt == "Rest of the world") 
	{
		newPrice = '$'+cal+ ' USD';
	}

	return newPrice;
}

function getNewRate()
{
	var txt = readCookie("pte_shipping");
	var newRate;
	
	if (txt == "United Kingdom") 
	{
		newRate = gbpRate;
	}
	if (txt == "Europe") 
	{
		newRate = eurRate;
	}
	if (txt == "United States") 
	{
		newRate = usdRate;
	}
	if (txt == "Rest of the world") 
	{
		newRate = usdRate;
	}
	return newRate;
}



function getRate()
{
	
	$.ajax({
	type: "GET",
	url: "basket/rates.xml",
	dataType: "xml",
	cache: false,
	error: function(xml) { errorRate = 1; loadCart(); },
	success: function(xml) {
			   
		 $(xml).find('Cube').each(function(){
			       
			    if ($(this).attr('currency') == "GBP") 
				{
			       gbpRate = $(this).attr('rate');
				}
				
				if ($(this).attr('currency') == "EUR") 
				{
			       eurRate = $(this).attr('rate');
				}
				
				if ($(this).attr('currency') == "USD") 
				{
			       usdRate = $(this).attr('rate');
				}

		});
		errorRate = 0;
		loadCart();
	}
	});
}

function destroySession(mode , lastshipping)
{	
	//console.log('ls' + lastshipping);
	var shipping = lastshipping;
	
	if (mode == 'dialog')
	{
		var caption = null;
		var imageGroup = false;
		var url = "warning.html?TB_iframe=true&height=90&width=350&modal=true";
		tb_ie7fix();
		tb_show(caption,url,imageGroup);
	}
	
	if (mode == 'yes')
	{
		//console.log("hello");
		var url = window.parent.location.href;
		createCookie("pte_lasturl",url,1);
		window.location.href= "/basket/webscr.php?session=destroy";	
	}
	
	if (mode == 'no')
	{
		//console.log(shipping);
		createCookie("pte_shipping",shipping,365);
		//console.log('cookie' + readCookie("pte_shipping"));
		var url = window.parent.location.href;
		createCookie("pte_lasturl",url,1);
		window.location.href= url;	
	}
}