/*
 * This is the function that actually highlights a text string by
 * adding HTML tags before and after all occurrences of the search
 * term. You can pass your own tags if you'd like, or if the
 * highlightStartTag or highlightEndTag parameters are omitted or
 * are empty strings then the default <font> tags will be used.
 */
function doReplace(bodyText, searchTerm, replaceWith) 
{
  
	// find all occurences of the search term in the given text, and add some "highlight" tags to them (we're not using a
	// regular expression search, because we want to filter out matches that occur within HTML tags and script blocks, so
	// we have to do a little extra validation)
	
	var newText = "";
	var i = -1;
	var lcSearchTerm = searchTerm.toLowerCase();
	var lcBodyText = bodyText.toLowerCase();

	while (bodyText.length > 0) {
		
		//Get index of search term
		i = lcBodyText.indexOf(lcSearchTerm, i+1);
		
		//if we can't fine it, replace the newText with the BodyText and return
		if (i < 0) {
			newText += bodyText;
			bodyText = "";
		} else {
		
			// skip anything inside an HTML tag
			if (bodyText.lastIndexOf(">", i) >= bodyText.lastIndexOf("<", i)) {
				// skip anything inside a <script> block
				if (lcBodyText.lastIndexOf("/script>", i) >= lcBodyText.lastIndexOf("<script", i)) {
					
					//Get Ascii Representation
					var	charCode = bodyText.charAt(i).charCodeAt(0);
					
					//Is this uppercase
					var isUpper = (charCode >= 65 && charCode <= 90);
					
					//Do replacing
					if(isUpper){
						newText += bodyText.substring(0, i) +  replaceWith.charAt(0).toUpperCase() + replaceWith.substr(1) + " ";
					}else{
						newText += bodyText.substring(0, i) +  replaceWith + " ";
					}
					bodyText = bodyText.substr(i + searchTerm.length);
					lcBodyText = bodyText.toLowerCase();
					i = -1;
				}
			}
		}
	}

	return newText;
}


/*
 * This function replaces search terms that are in the array
 */
function replaceTerms()
{	
	var searchArray = new Array("favors","labors","colors","favor","labor","color");
	var replaceArray = new Array("favours","labours","colours","favour","labour","colour");

	if (!document.body || typeof(document.body.innerHTML) == "undefined") {
		//alert("Sorry, for some reason the text of this page is unavailable. Searching will not work.");
		return false;
	}

	var bodyText = document.body.innerHTML;
	for (var i = 0; i < searchArray.length; i++) {
		bodyText = doReplace(bodyText, searchArray[i], replaceArray[i]);
	}
	
	document.body.innerHTML = bodyText;
	return true;
}

function checkForReplace(){

	//Get the Cookie
	var geoValue = $.cookie('Geo');// Cookie.read('Geo');
	
	//If it's not in the US
	if(geoValue != "USA"){
	}
}

function switchLocale(Country){
	
	//Write the country to the cookie
	//Cookie.write("Geo", Country,{path:"/"});
	$.cookie("Geo", Country, {path :'/'});
	
	//alert("Set to " + Country);
	//Set this cookie to prevent switching - lasts 45 minutes
	$.cookie("GeoSwitch", "", {expires:0.042, path:"/"});
	
	//Reload the page
	location.reload(true);
}



