// File update 2010/01/06 to include some new lottery types
// File update 2009/09/25 to include Lotto Max
// File update 2007/01/09 to include celebration 2007 and qu_collection_souvenir
// File updated 2007/01/24 to include millionaire_life
// File updated 2007/02/02 to include new Partner Brand URL
// File updated 2007/02/20 to include French version of Millionaire Life
// The following two arrays contain the keys that we search the url for, along with their respective code
// There are two lists, english and french, and each element in each array contains a string delimited by the "|" character.
// The delimiter separates the key and code e.g. for "na_super_7|203", the key is "na_super_7", and the code is 203
var enCodeValues = new Array("na_lotto_649|202", "na_lotto_max|203", "na_max_millions|203", "na_super_7|203", "national_lotteries|201", "oct_bonus_2003|201", "celebration_2008|201", "celebration_2009|201", "celebration_2010|201", "celebration_2004|201", "celebration_2007|201", "celebration_2003|201", "celebration_2002|201", "na_celebration_2001|201", "na_celebration_99|201", "na_celebration_98|201", "na_mega_millions|201", "atlantic_canada|688", "at_|688", "quebec|687", "qc_|687", "celebration_2006_results|687", "celebration_2005_results|687", "qu_collection_souvenir|687", "qu_|687", "mega_millions_2005|687", "mega_millions_2004|687", "Mega_millions_2003|687", "ontario|686", "on_|686", "ont_big_ticket|686", "western_canada|685", "we_|685", "british_columbia|684", "bc_|684", "frequency_chart|690", "numbers_ever_won|691", "how_current|692", "lottery_schedules|693", "lottery_news|696", "lottery_offices|695", "ExtraGoodies|689", "provincial_lotteries|683", "numbers_ever_won_results|691", "millionaire_life|201", "millionaire_life_fr_2008|201", "millionaire_life_2009|201");
var frCodeValues = new Array("na_lotto_649|699", "na_lotto_max|700", "na_max_millions|700", "na_super_7|700", "national_lotteries|698", "oct_bonus_2003|698", "celebration_2008|698", "celebration_2009|698", "celebration_2010|698", "celebration_2004|698", "celebration_2007|698", "celebration_2003|698", "celebration_2002|698", "na_celebration_2001|698", "na_celebration_99|698", "na_celebration_98|698", "na_mega_millions|698", "atlantic_canada|702", "at_|702", "quebec|703", "qc_|703", "celebration_2006_fr_resu|703", "celebration_2005_fr_resu|703", "qu_collection_souvenir|703", "qu_|703", "mega_millions_2005|703", "mega_millions_2004|703", "mega_millions_2003|703", "ontario|711", "on_|711", "ont_big_ticket|711", "western_canada|712", "we_|712", "british_columbia|713", "bc_|713", "frequency_chart|715", "numbers_ever_won|717", "how_current|719", "lottery_schedules|716", "lottery_offices|718", "EnSavoirPlus|714", "provincial_lotteries|701", "numbers_ever_won_results|717", "millionaire_life_fr|698", "millionaire_life_fr_2008|698", "millionaire_life_2009|698");

// When we extract the key from a URL, we will first check if it begins with one of the following prefixes
// If it does, then we will consider the prefix as the key, and not the actual key from the URL
var prefixValues = new Array("at_", "bc_", "on_", "qc_", "we_");

// Build two maps that will be used in order to find the code for the key found in the URL
function buildHashMap(codeValues) {
	var aHashMap = new Object();
	for (var i in codeValues) {
		var anElement = codeValues[i];
		var sepCharLoc = anElement.indexOf("|");
		var key = anElement.substring(0, sepCharLoc);
		var code = anElement.substring(sepCharLoc + 1);
		aHashMap[key.toLowerCase()] = code;
	}
	return aHashMap;
}
var enList = buildHashMap(enCodeValues);
var frList = buildHashMap(frCodeValues);

function getCodeFromURL(theURL) {
	var listToUse = null, key = null, code = null;
	// Check for english or french
	var isEnglish = theURL.indexOf("/cgi-bin/english?") != -1 ? true : false;
	if(isEnglish) {
		listToUse = enList;
	} else {
		listToUse = frList;
	}
	
	// We'll first check for a couple of known exceptions to the format of the URLs
	if(theURL.indexOf("job=frequency_chart") != -1) {
		key = "frequency_chart";
	} else if(theURL.indexOf("job=numbers_ever_won_results") != -1) {
		key = "numbers_ever_won_results";
	} else if(theURL.indexOf("job=lottery_news") != -1) {
		key = "lottery_news";		
	} else {
		// Check if the "lottery" param exists in the URL
		var idx = theURL.indexOf("&lottery=");
		if(idx != -1) {
			// Check if there's another param after this one
			var nextParamAt = theURL.indexOf("&", idx + 9);
			if(nextParamAt == -1) {
				key = theURL.substring(idx + 9);
			} else {
				key = theURL.substring(idx + 9, nextParamAt);
			}
		} else {
			// Check if the "job" param exists in the URL
			var idx = theURL.indexOf("?job=");
			if(idx != -1) {
				// Check if there's another param after this one
				var nextParamAt = theURL.indexOf("&", idx + 5);
				if(nextParamAt == -1) {
					key = theURL.substring(idx + 5);
				} else {
					key = theURL.substring(idx + 5, nextParamAt);
				}
			}
		}
	}

	if (key != null) {
		// Check against the prefix values
		for (var i in prefixValues) {
			if (key.indexOf(prefixValues[i]) == 0) {
				key = prefixValues[i];
				break;
			}
		}

		code = listToUse[key.toLowerCase()];
		// FOR TESTING --- REMOVE ONCE THE SCRIPT IS IN PLACE AND WORKING
//		alert(code);
	}
	return code;
}

var theCode = getCodeFromURL(window.location.href);
var cobrandURL = "http://cobrand.sympatico.ca/Bell.Sympatico.CMS/Cobranding/PartnerCobranding.aspx?id=" + theCode + "&Preview=true";
document.write('<sc'+'ript language="JavaScript"');
document.write(' src="' + cobrandURL + '">');
document.write('</sc'+'ript>');
