﻿function ChangeLanguage() {
	/* Get the current URL, or the one of the parent if in a IFrame */
	var curURL = parent.document.location.href;
	var newURL = '';
	var foundLang = false;
	var curLang = '';
	
	/* Loop through the complete URL and find the current language */
	for (i = 0; i < curURL.length && foundLang == false; i++) {
		
		/* If the current character is a front slash (/), we check the next pair to see if it's the language */
		if (curURL.charAt(i) == '\/') {
			
			/* Parse the current language following with a front slash to make sure it's not the start of a word */			
			curLang = curURL.substring(i + 1, i + 4);
			if (curLang == 'en/' || curLang  == 'fr/')
			{
					/* Build the new URL based on the opposite language */
					switch(curLang)
					{
						case 'en/':
							newURL = curURL.substring(0, i + 1) + 'fr' + curURL.substring(i + 3, curURL.length);
							break;
						case 'fr/':
							newURL = curURL.substring(0, i + 1) + 'en' + curURL.substring(i + 3, curURL.length);
							break;
						default:
							newURL = curURL.substring(0, i + 1) + curLang + curURL.substring(i + 3, curURL.length);
							break;
					}
					
					foundLang = true;
			}
			else
			{
				foundLang = false;
			}
		}
	} 
	
	/* Redirect the user to the new page */
	window.location = newURL;
}