function readCookie(cookieName) {
	var theCookie=""+document.cookie;
	var ind=theCookie.indexOf(cookieName);
	if (ind==-1 || cookieName=="") return ""; 
	var ind1=theCookie.indexOf(';',ind);
	if (ind1==-1) ind1=theCookie.length; 
	return unescape(theCookie.substring(ind+cookieName.length+1,ind1));
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

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 currencySelector(country_code, el)
{ 
    var xhr; 
    try {  xhr = new ActiveXObject('Msxml2.XMLHTTP');   }
    catch (e) 
    {
        try {   xhr = new ActiveXObject('Microsoft.XMLHTTP');    }
        catch (e2) 
        {
          try {  xhr = new XMLHttpRequest();     }
          catch (e3) {  xhr = false;   }
        }
     }
  
    xhr.onreadystatechange  = function()
    { 
         if(xhr.readyState  == 4)
         {
              if(xhr.status  == 200)  {
		  var myXML = xhr.responseText;
		  document.getElementById(el).innerHTML = myXML;
		  changeCurrency();
             }
         }
    }; 

   createCookie("country_code",country_code,30);
   var url="/inc/asp/getCurrency.asp?country=" + country_code
   xhr.open('GET', url,  true); 
   xhr.send(null); 
} 

function Left(str, n){
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}

function Right(str, n)
{
      if (n <= 0)
          return "";
      else if (n > String(str).length)
          return str;
      else
   {
          var iLen = String(str).length;
          return String(str).substring(iLen, iLen - n);
      }
}

function changeCurrency() {

	var oldPrice;
	var newPrice;
	var convertingFrom;

	var currencyIndex 	= document.getElementById('currencyChoice').selectedIndex;
	var currencyValue 	= document.getElementById('currencyChoice')[currencyIndex].value;
	var currencyText	= document.getElementById('currencyChoice')[currencyIndex].text;
	var currencySymbol 	= Left(currencyText, currencyText.indexOf(' '));

	if (currencyIndex == 0) {
		createCookie("country_code",'US',30);
	}
	if (currencyIndex == 1) {
		createCookie("country_code",'GB',30);
	}
	if (currencyIndex == 2) {
		createCookie("country_code",'AU',30);
	}
	if (currencyIndex == 3) {
		createCookie("country_code",'CA',30);
	}

	var arr_price = document.getElementsByTagName('span');

   	for(var i = 0; i < arr_price.length; i++) {
      		if (arr_price[i].getAttribute('class') === 'price' || arr_price[i].getAttribute('className') === 'price') {
			oldPrice = arr_price[i].getAttribute('data-price');

			convertingFrom = arr_price[i].getAttribute('data-currency');
			if (convertingFrom == '') {
				newPrice = (oldPrice / document.getElementById('currencyChoice')[0].value)*currencyValue;
			}
			if (convertingFrom == 'USD') {
				newPrice = (oldPrice / document.getElementById('currencyChoice')[0].value)*currencyValue;
			}
			if (convertingFrom == 'GBP') {
				newPrice = (oldPrice / document.getElementById('currencyChoice')[1].value)*currencyValue;
			}

			if (arr_price[i].getAttribute('data-advertiser') == 'CafePress') {
				if (currencyIndex == 0) {
				} else {
					
					// was 10%
					newPrice = parseFloat(newPrice * 1);
					newPrice = newPrice.toFixed(2);
					
					var newPriceCents = newPrice.toString();
					newPriceCents = "0" + newPriceCents.substring(newPriceCents.lastIndexOf("."));
					newPriceCents = parseFloat(newPriceCents).toFixed(2);
					
					var newPriceDollars = newPrice.toString();
					newPriceDollars =  parseFloat(Left(newPriceDollars, newPriceDollars.indexOf('.')));
					
					if (newPriceCents < 0.10) {
						newPriceCents = 0.00;
					} else {
						if (newPriceCents < 0.80) {
							newPriceCents = 0.50;
						} else {
							//was 1.00
							newPriceCents = 0.00;
						}
					}
					
					newPrice = newPriceDollars + newPriceCents;
									
				}
			}		
				
			arr_price[i].innerHTML = newPrice.toFixed(2);
			arr_price[i].style.visibility="visible";

  		}
	}

	var arr_currency = document.getElementsByTagName('span');

   	for(var i = 0; i < arr_currency.length; i++) {
      		if (arr_currency[i].getAttribute('class') === 'currency' || arr_currency[i].getAttribute('className') === 'currency') {
			arr_currency[i].innerHTML = currencySymbol;
			arr_currency[i].style.visibility="visible";
  		}
	}

}