//List countries and corresponding country domains
var countries = new Array(10);
countries[0] = 'canada';
countries[1] = 'hong kong';
countries[2] = 'iceland';
countries[3] = 'india';
countries[4] = 'malaysia';
countries[5] = 'pakistan';
countries[6] = 'philippines';
countries[7] = 'singapore';
countries[8] = 'south africa';
countries[9] = 'thailand';

var countryDomains = new Array(10);
countryDomains[0] = '/offices/NA/canada/';
countryDomains[1] = '/offices/asiapacific/hongkong/';
countryDomains[2] = '/offices/europe/iceland/';
countryDomains[3] = '/offices/asiapacific/india/';
countryDomains[4] = '/offices/asiapacific/malaysia/';
countryDomains[5] = '/offices/asiapacific/pakistan/';
countryDomains[6] = '/offices/asiapacific/philippines/';
countryDomains[7] = '/offices/asiapacific/singapore/';
countryDomains[8] = '/offices/africa/southafrica/';
countryDomains[9] = '/offices/asiapacific/thailand/';

//Function to test referrer for particular strings. If parts of referer match, set cookies accordingly
function checkReferrer () {
	var refer = document.referrer;
	//Checks to see if specific country domains are in the referrer string
	for (var i=0; i<countries.length; i++) {
		if (refer.indexOf(countryDomains[i]) == -1) {
			//alert("not from this country");
			continue;
		} else {
			createCookie("countrycookie", countries[i], 0);
			//Then check the page again
			checkPage();		
		}
	}
}

//Function to check for the correct cookie. If it's not there, check the referer and set if necessary.
function checkPage() {
	//Look for the cookie
	var countryCookie = readCookie("countrycookie");
	//If the cookie is there, adjust links based on the country
	if (countryCookie) {
		updateLinks(countryCookie);
	} else {
		checkReferrer();
	}
}

//Revised function to update links on the page based on the cookie
function updateLinks(country) {
	var strHref, strLogoLink, strHomeLink;

	//Update links according to list of countries
	switch (country) {
		case 'canada':
			strHref = '/offices/NA/canada/';
			break;
		case 'hong kong':
			strHref = '/offices/asiapacific/hongkong/';
			break;
		case 'iceland':
			strHref = '/offices/europe/iceland/';
			break;
		case 'india':
			strHref = '/offices/asiapacific/india/';
			break;
		case 'malaysia':
			strHref = '/offices/asiapacific/malaysia/';
			break;
		case 'pakistan':
			strHref = '/offices/asiapacific/pakistan/';
			break;
		case 'philippines':
			strHref = '/offices/asiapacific/philippines/';
			break;
		case 'singapore':
			strHref = '/offices/asiapacific/singapore/';
			break;
		case 'south africa':
			strHref = '/offices/africa/southafrica/';
			break;
		case 'thailand':
			strHref = '/offices/asiapacific/thailand/';
			break;
		default:
			strHref = '/index.html';
	}

	// Adjust the href in the logo...
	strLogoLink = "<a href='" + strHref + "' id='homePgLogo'><img src='http://www.sas.com/feature/images/sas_logo.gif' alt='SAS: The Power To Know' width='186' height='54' border='0' /></a>";

	// Adjust the href in the home button...
	strHomeLink = "<a href='" + strHref + "' id='homePgLink'><img src='http://www.sas.com/feature/images/sas_home.gif' alt='' width='117' height='37' border='0' /></a>";
	
	// Insert the adjusted html into the document...
	document.getElementById('logo').innerHTML = strLogoLink;
	document.getElementById('topButton').innerHTML = strHomeLink;

}

//Functions to handle the cookies
function createCookie(name,value,days) {
	//alert('creating test country cookie');
	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;
}