// JavaScript Document
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);
}


var userLang = readCookie('language')

//check if cookie is set
//COOKIE IS SET
if(userLang) {
	//Prefferd languagr --> USA
	//rewrite URL
	if(userLang == "USA") {
		currentUrl=document.location.href;
    	currentUrl = currentUrl.replace('\(UK\)','\(USA\)');  
		window.location = currentUrl;
	}
	else {
	//Prefferd languagr --> UK	
	//do nothing
		exit();
	}
	
}
else {
	//COOKIE NOT SET
	//Display confirm box
	var answer = confirm('The web page you are about to view is not intended for the USA.\nWould you like to transfer to our USA site?');
    if (answer){
    //user said YES - go to USA site    
    //replace UK to USA
	//get current URL
	currentUrl=document.location.href;
    currentUrl = currentUrl.replace('\(UK\)','\(USA\)');    
    //go to UK site
    window.location = currentUrl;
	//create cookie language --> USA
	createCookie('language','USA',365)
}
	else
{
        //client said NO - stay on UK site
		//create cookie --> language --> UK
		createCookie('language','UK',365)
		exit();
    }


}