/* Code to populate American and UK selectors with correct links - inc ISBN no. */
var centralfront = "http://www.centralbooks.co.uk/cgi-bin/ss000001.pl?ACTINIC_REFERRER=http://www.centralbooks.co.uk/acatalog/&SS=";
var centralback = "&PR=-1&TB=A";
var chicagofront = "http://www.press.uchicago.edu/presssite/metadata.epl?isbn=";
var isbn = readCookie('isbn');
var getuslink = document.getElementById('usLink');
getuslink.setAttribute("href", chicagofront + isbn);
var getuklink = document.getElementById('ukLink');
getuklink.setAttribute("href", centralfront + isbn + centralback);



/* Code to check geolocation cookie and populate button with initial choice */
var countries = new Array ("AI","AG","AW","BS","BB","BZ","BM","VG","CA","KY","CR","CU","DM","SV","GL","GD","GP","GT","HT","HN","JM","MQ","MX","MY","MS","AN","NI","PA","PR","KN","LC","PM","VC","TT","TC","US","AR","BO","BR","CL","CO","EC","FK","GF","GY","PY","PE","SR","UY","VE","SV");
var getuslink = document.getElementById('usLink');
var getuklink = document.getElementById('ukLink');
var button = document.getElementById("goButton");
var geolocation = readCookie('geolocation');
var crosscheck = countries.indexOf(geolocation);
if (crosscheck >= 0) {
	button.setAttribute("href", chicago + isbn);
	button.setAttribute("name", "US");
	getuslink.setAttribute("class", "on");
	getuslink.setAttribute("className", "on");
  } else {
	button.setAttribute("href", centralfront + isbn + centralback);
	button.setAttribute("name", "UK");
	getuklink.setAttribute("class", "on");
	getuklink.setAttribute("className", "on");
  }


if(!Array.indexOf){
    Array.prototype.indexOf = function(obj){
        for(var i=0; i<this.length; i++){
            if(this[i]==obj){
                return i;
            }
        }
        return -1;
    }
}


var expDays = 30;
var exp = new Date();
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));

function getCookieVal (offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}
function GetCookie (name) {
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen) {
var j = i + alen;
if (document.cookie.substring(i, j) == arg)
return getCookieVal (j);
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
}
return null;
}

function SetCookie (name, value) {
var argv = SetCookie.arguments;
var argc = SetCookie.arguments.length;
var expires = (argc > 2) ? argv[2] : null;
var path = "/";
var domain = (argc > 4) ? argv[4] : null;
var secure = (argc > 5) ? argv[5] : false;
document.cookie = name + "=" + escape (value) +
((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
((path == null) ? "" : ("; path=" + path)) +
((domain == null) ? "" : ("; domain=" + domain)) +
((secure == true) ? "; secure" : "");
}

function DeleteCookie (name) {
var exp = new Date();
exp.setTime (exp.getTime() - 1);
var cval = GetCookie (name);
document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}

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)
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function moveuklink(whichuklink) {
	var centralfront = "http://www.centralbooks.co.uk/cgi-bin/ss000001.pl?ACTINIC_REFERRER=http://www.centralbooks.co.uk/acatalog/&SS=";
	var centralback = "&PR=-1&TB=A";
	var isbn = readCookie('isbn');
	var getuslink = document.getElementById("usLink");
	var name = whichuklink.getAttribute("name");
	var source = whichuklink.getAttribute("href");
	var placeholder = document.getElementById("goButton");
	placeholder.setAttribute("href", centralfront + isbn + centralback);
	placeholder.setAttribute("name",name);
	whichuklink.setAttribute("class" , "on");
	whichuklink.setAttribute("className", "on");
	getuslink.setAttribute("class" , "off");
	getuslink.setAttribute("className", "off");
	}
	
	

function moveuslink(whichuslink) {
	var chicagofront = "http://www.press.uchicago.edu/presssite/metadata.epl?isbn=";
	var isbn = readCookie('isbn');
	var getuklink = document.getElementById("ukLink");
	var name = whichuslink.getAttribute("name");
	var source = whichuslink.getAttribute("href");
	var placeholder = document.getElementById("goButton");
	placeholder.setAttribute("href",chicagofront + isbn);
	placeholder.setAttribute("name",name);
	whichuslink.setAttribute("class" , "on");
	whichuslink.setAttribute("className", "on");
	getuklink.setAttribute("class" , "off");
	getuklink.setAttribute("className", "off");
	}






