//
//Get country code
//Check referrer first, and if not there check cookie
//Cookie is only written for 'main' countries

//get country from referrer 
function getReferrerCountry() {
	var previousUrl = document.referrer.split("/");
		//previousUrl[0] is http or https
		//previousUrl[1] is blank
		//previousUrl[2] is host
		//previousUrl[3] is travel
		//previousUrl[4] is jsp
		//previousUrl[5] is pageid
		//previousUrl[6] is country
		//previousUrl[7] is login
		//previousUrl[8] is language
		//previousUrl[8] is scheme
	
		if (previousUrl != "" && previousUrl[3]=="travel") 	{
			var country = previousUrl[6];
			return country;
		} else {
			return "";
		}
}
	
	
//get country from cookie 
function readCountryCookie() {
			if (document.cookie == '') { 
				// if no cookie return blank;
				return "";
			} else {
    			//bring back BA cookie
				var BACookie = document.cookie;
				var startCookie, startCountry, endCountry;
				startCookie = BACookie.indexOf("BA_COUNTRY_CHOICE_COOKIE");
				if(startCookie != -1)  {
					// 24 letters in BA_COUNTRY_CHOICE_COOKIE
					startCountry = startCookie + 24 +1;
					//get the next ; for the end of the name
					endCountry = BACookie.indexOf(';', startCookie);
					// Find the end of the value string (i.e. the next ';').
					if(endCountry == -1) endCountry = BACookie.length;
					return unescape(BACookie.substring(startCountry, endCountry));
				} else {
				// if no country return blank;
				return "";
				}
			}
		}

//main function starts here
//get country from referrer, and if blank from cookie
function getMorphCountry() {
	var refCountryCode = getReferrerCountry().toUpperCase();
	var cookieCountryCode = readCountryCookie().toUpperCase();

	if (refCountryCode !="") {
		return refCountryCode;
	} else if (cookieCountryCode !="") {
		return cookieCountryCode;
	} else {
		return "";
	}

}


