﻿var countryselect = "countryselect";
var unitylife = "unitylife";
var tellingourstory = "tellingourstory";


function MomentumOnLoad(){
	//hide empty right widgets
	var additionalRightContent = document.getElementById("additional-right-content");
	if($(additionalRightContent).children().children().length < 1 && $(additionalRightContent).children(".ms-rtestate-field").text() == "")
		$(additionalRightContent).hide();
		
	RemoveBreadcrumbVariation();
			
	try{
		replaceSearchQuery();
	}
	catch(ex){}	
	try{
		if((getQuerystring("referrer") == "www.unitylife.ca")||(getQuerystring("referrer") == "www.unite-vie.ca")){
			OpenUnityLife();
		}
		//else if(getQuerystring("CountrySelect") == "true")
		else if ((document.location.href.toLowerCase().indexOf("/ca-en/") > -1)&&(!(MSCheckCookie(countryselect))))
		{
			OpenCountrySelect();
		}		
		else
		{
			OpenTellingStory();
		}
	}
	catch(ex){alert(ex.message);}
}
function removeSearchText(searchInput){
	if (searchInput.value == "Search"){		
		searchInput.value = "";
	}
}
function addSearchText(searchInput){
	if (searchInput.value == ""){		
		searchInput.value = "Search";
	}
}
function removeSearchTextFR(searchInput){
	if (searchInput.value == "Recherche"){		
		searchInput.value = "";
	}
}
function addSearchTextFR(searchInput){
	if (searchInput.value == ""){		
		searchInput.value = "Recherche";
	}
}


function replaceSearchQuery(){
	var searchText = getQuerystring("k");
	if 	(searchText.length > 0){
		document.getElementById("search-field").value = searchText;
	}
} 
function search(searchInput, variation){
	window.location = "/search/pages/results(" + variation + ").aspx?k=" + searchInput.value;
}
function ensureSiteScope(variation){
	//cs=This%20Site&u=" + window.location.protocol + "//" + window.location.host + "/" + variation;
	var currentHref = window.location.href;
	if (currentHref.indexOf("u=") < 1){
		var newHref = currentHref += "&cs=This%20Site&u=" + window.location.protocol + "//" + window.location.host + "/" + variation;
		window.location = newHref;
	}
}
function TranslatePage(language){
	var typeSelect = document.getElementById("ctl00_m_g_500971f4_a7b1_4fce_aeb3_a872e698e75e_ASB_SS_rtlb");
	typeSelect.options[0].text = "Tous les résultats";	
	RemoveTypeChoices("ctl00_m_g_500971f4_a7b1_4fce_aeb3_a872e698e75e_ASB_SS_rtlb");
	document.getElementById("ctl00_m_g_500971f4_a7b1_4fce_aeb3_a872e698e75e_ASB_BS_SRCH_1").value = "Recherche";
}
function RemoveTypeChoices(id){
	var typeSelect = document.getElementById(id);
	typeSelect.options[4] = null;
	typeSelect.options[3] = null;
	typeSelect.options[2] = null;
}
function VariationRedirect(urlPart)
{
	var urlPath = window.location.pathname;
	window.location = urlPath.substring(0, 6) + urlPart;
}
function RemoveBreadcrumbVariation(){	


	var breadcrumbSpans = document.getElementById("breadcrumb").getElementsByTagName("span");
	RemoveBreadcrumbDuplicate(breadcrumbSpans);
	RemoveEmptyRightPanel();
	
	breadcrumbSpans[1].style.display = "none";
	breadcrumbSpans[2].style.display = "none";
	try
	{
		breadcrumbSpans[3].childNodes[0].innerText = "Foresters";
	}
	catch (ex) {}
	
	try
	{
		breadcrumbSpans[3].childNodes[0].textContent = "Foresters";
	}
	catch (ex) {}
}

function RemoveBreadcrumbDuplicate(breadcrumbSpans){
	var previousValueInner = "";
	var previousValueText = "";

	for (i=0; i < breadcrumbSpans.length; i++)
	{
		try
		{
			var innerTextValue = breadcrumbSpans[i].innerText;
			if (innerTextValue.indexOf(">") < 0) {
				if (innerTextValue == previousValueInner)
				{
					breadcrumbSpans[i].style.display = "none";
					breadcrumbSpans[i+1].style.display = "none";
					return;
				}
				previousValueInner = breadcrumbSpans[i].innerText;
			}
		}
		catch (ex){}
		
		try
		{
			var textContentValue = breadcrumbSpans[i].textContent;
			if (textContentValue.indexOf(">") < 0) {
				if (textContentValue == previousValueText)
				{
					breadcrumbSpans[i].style.display = "none";
					breadcrumbSpans[i+1].style.display = "none";
					return;
				}
				previousValueText = breadcrumbSpans[i].textContent;
			}
		}
		catch (ex){}
		
		
	}
}

function RemoveEmptyRightPanel(){
	var rightPanel = document.getElementById('');	
	
	
}

function printSelection(){
	  document.getElementById('compliance-code').style.display = 'inline';
	  var node = document.getElementById("ctl00_MSO_ContentDiv");
	  
	  var content=node.innerHTML
	  
	  // Remove all Images
	  var content = content.replace(/<img[^>]+\>/gi, "");
	  
	  var pwin=window.open('','print_content','width=700,height=400');
	
	  pwin.document.open();
	  pwin.document.write('<html><head><link rel="stylesheet" type="text/css" href="/_layouts/1033/styles/Themable/search.css?rev=9hpHCuePC8XDNq1maXxO6Q%3D%3D"/><link rel="stylesheet" type="text/css" href="/_layouts/1033/styles/Themable/corev4.css?rev=iIikGkMuXBs8CWzKDAyjsQ%3D%3D"/><link rel="stylesheet" type="text/css" href="/Style%20Library/StandardElements.css"/><link rel="stylesheet" type="text/css" href="/Style%20Library/Layouts.css"/><link rel="stylesheet" type="text/css" href="/Style%20Library/Footer.css"/><link rel="stylesheet" type="text/css" href="/Style%20Library/SP-Forms.css"/><link rel="stylesheet" type="text/css" href="/Style%20Library/Main.css"/></head><body onload="window.print()"><img alt="Foresters" src="/PublishingImages/print-logo.gif"/>' + content + '<div style="position:absolute; bottom:15px">' + document.location.href + '</div></body></html>');
	  pwin.document.close();
	 
	  setTimeout(function(){pwin.close();},3000);	
	  document.getElementById('compliance-code').style.display = 'none';
}

function emailLink(){
	var subject = "Interesting Foresters information"
	var body = "I thought you might find this information interesting: " + window.location
	window.location="mailto:?subject=" + subject + "&body=" + body;
}
	
function setEmailLink(){
	var node = document.getElementById("ctl00_MSO_ContentDiv");
  	var content=node.innerHTML
	var bodyHTML = '<html><head><link rel="stylesheet" type="text/css" href="/Style%20Library/Main.css"/><link rel="stylesheet" type="text/css" href="/Style%20Library/Layouts.css"/><link rel="stylesheet" type="text/css" href="/Style%20Library/Print.css"/></head><body onload="window.print()">'+content+'</body></html>'
	document.getElementById("email-page").href = "mailto: ?Subject=&Body=" + bodyHTML;
}

function toggle(obj) {

	var el = document.getElementById(obj);

	if ( el.style.display != 'none' ) {

		el.style.display = 'none';

	}

	else {

		el.style.display = '';

	}

}

function SetNewsMaster(){
	//alert(document.referrer.toLowerCase());
	if (document.referrer.toLowerCase().indexOf("/ca-fr") > -1)
	{
		document.getElementById("advisor-panel(ca-en)").style.display = "none"
		document.getElementById("country-selector(ca-en)").style.display = "none"
		document.getElementById("search(ca-en)").style.display = "none"
		document.getElementById("right-container(ca-en)").style.display = "none"
		
		document.getElementById("advisor-panel(ca-fr)").style.display = "block"
		document.getElementById("country-selector(ca-fr)").style.display = "block"
		document.getElementById("search(ca-fr)").style.display = "block"
		document.getElementById("right-container(ca-fr)").style.display = "block"
	}
	else if (document.referrer.toLowerCase().indexOf("/us-en") > -1)
	{
		document.getElementById("advisor-panel(ca-en)").style.display = "none"
		document.getElementById("country-selector(ca-en)").style.display = "none"
		document.getElementById("search(ca-en)").style.display = "none"
		document.getElementById("right-container(ca-en)").style.display = "none"
		
		document.getElementById("advisor-panel(us-en)").style.display = "block"
		document.getElementById("country-selector(us-en)").style.display = "block"
		document.getElementById("search(us-en)").style.display = "block"
		document.getElementById("right-container(us-en)").style.display = "block"
	}
	else if (document.referrer.toLowerCase().indexOf("/uk-en") > -1)
	{
		document.getElementById("advisor-panel(ca-en)").style.display = "none"
		document.getElementById("country-selector(ca-en)").style.display = "none"
		document.getElementById("search(ca-en)").style.display = "none"
		document.getElementById("right-container(ca-en)").style.display = "none"
		
		document.getElementById("advisor-panel(uk-en)").style.display = "block"
		document.getElementById("country-selector(uk-en)").style.display = "block"
		document.getElementById("search(uk-en)").style.display = "block"
		document.getElementById("right-container(uk-en)").style.display = "block"
	}
}

function MSCheckCookie(name){
	try{		
		var cookie = MSGetCookie(name);
		if (cookie.length > 1){
			return true;
		}
		else{
			return false;
		}
	}
	catch (err){
	}
	return false
}
function OpenCountrySelect(){
	//if (MSCheckCookie('country-select'))
	//{		
	//	return false;
	//}
	//else
	//{
		OpenSplash();
		var splashContent = document.getElementById("country-select");
		splashContent.style.display = "block";
		CheckOffset();
		return false
	//}
}
function OpenUnityLife(){
	//if (MSCheckCookie('unity-life'))
	//{		
	//	return false;
	//}
	//else
	//{
		OpenSplash();
		var splashContent = document.getElementById("unity-life");
		splashContent.style.display = "block";		
		return false;
	//}
}
function OpenTellingStory(){

	//hiding right content
	/*var additionalRightContent = document.getElementById("additional-right-content");
	if($(additionalRightContent).children().children().length < 1 && $(additionalRightContent).children(".ms-rtestate-field").text() == "")
		$(additionalRightContent).hide();*/
	
	
	if (MSCheckCookie(tellingourstory))
	{		
		return false;
	}
	else
	{
		OpenSplash();
		var splashContent = document.getElementById("telling-our-story");
		splashContent.style.display = "block";
		return false
	}
}

function OpenSplash(){
	try{
		var splashContent = document.getElementById("country-select");
		splashContent.style.display = "none";
	}
	catch (err) {}
	try{	
		splashContent = document.getElementById("unity-life");
		splashContent.style.display = "none";
	}
	catch (err) {}
	splashContent = document.getElementById("telling-our-story");
	splashContent.style.display = "none";
	var thediv=document.getElementById('overlaybox');
	thediv.style.display = "block";	
	ShowSplashContent();
	return false;
}

function CheckOffset()
{	
	var splashBox = document.getElementById("splashBox");
	var splashHeight = splashBox.offsetHeight + 30;
	var windowHeight = (typeof window.innerHeight != 'undefined' ? window.innerHeight : document.body.offsetHeight)
	/*alert("splash height = " + splashHeight);
	alert("window height = " + windowHeight);*/
	if (splashHeight > windowHeight)
	{
		var overlayBox = document.getElementById("overlaybox");
		overlayBox.style.verticalAlign = "top";
		overlayBox.style.overflow = "scroll";
		try
		{
			//splashBox.setAttribute('style', 'position:absolute !important');
		}
		catch (err) {splashBox.style.position = "absolute";}
		splashBox.style.marginTop = "0px";
		splashBox.style.top = "30px";
	}
	else
	{
		var overlayBox = document.getElementById("overlaybox");
		overlayBox.style.verticalAlign = "middle";
		splashBox.style.position = "fixed";
		splashBox.style.marginTop = "-" + ((splashHeight/2)-10) + "px";
		splashBox.style.top = "50%";
	}
}

function CloseSplash(){
	var today = new Date();
	var expiry = new Date(today.getTime() + 365 * 24 * 60 * 60 * 1000);
	try{
		var splashContent = document.getElementById("country-select");
		if (splashContent.style.display == "block")
		{
			MSSetCookie(countryselect, "true", expiry);		
		}
	}
	catch (err) {}		
	/*splashContent = document.getElementById("unity-life");
	if (splashContent.style.display == "block")
	{
		
		MSSetCookie(unitylife, "true", expiry);
	}*/	
	splashContent = document.getElementById("telling-our-story");
	if (splashContent.style.display == "block")
	{
		MSSetCookie(tellingourstory, "true", expiry);
	}
	var thediv=document.getElementById('overlaybox');
	thediv.style.display = "none";
	HideSplashContent();
}

function ShowSplashContent(){
	try{	
		var splashBox = document.getElementById("splashBox");			
		splashBox.style.display = "block";
	}
	catch (err){
		alert(err.description);
	}
}

function HideSplashContent() {
	var splashBox = document.getElementById("splashBox");		
	splashBox.style.display = "none";
	return;	
}

function LocationFilter(index){
	switch(index)
	{
		case 1:
			SetFilterParams("Canada (EN)");
			break;
		case 2:
			SetFilterParams("Canada (FR)");
			break;
		case 3:
			SetFilterParams("United States");
			break;
		case 4:
			SetFilterParams("United Kingdom");
			break;
		default:
			SetFilterParams("All Countries");
			break;			
	}
}		
function SetFilterParams(country){
	var newLocation;
	var URL = window.location.href.split("?")[0];
	var date = document.getElementById("byDateTxt").value;
	if (date.length < 1) { newLocation = URL + "?location=" + country; } 
	else { newLocation = URL  + "?location=" + country + "&date=" + date; }			
	window.location = newLocation;
}		
function getQuerystring(key, default_)
{
  if (default_==null) default_=""; 
  key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
  var qs = regex.exec(unescape(window.location.href));
  if(qs == null)
    return default_;
  else
    return qs[1];
}
function SetFields()
{
	var country = getQuerystring("location");
	var dateFilter = getQuerystring("date");
	var countryDD = document.getElementById("byCountryDD");

	if (country.length > 1) {
		try {
			if (country == "Canada (EN)"){ countryDD.selectedIndex = 1; }
			else if (country == "Canada (FR)"){ countryDD.selectedIndex = 2; }
			else if (country == "United States"){ countryDD.selectedIndex = 3; }
			else if (country == "United Kingdom"){ countryDD.selectedIndex = 4; }
			else{ countryDD.selectedIndex = 0; }
		} catch (ex){}
		try {
			if (country == "Canada (EN)"){ countryDD.options.selectedIndex = 1; }
			else if (country == "Canada (FR)"){ countryDD.options.selectedIndex = 2; }
			else if (country == "United States"){ countryDD.options.selectedIndex = 3; }
			else if (country == "United Kingdom"){ countryDD.options.selectedIndex = 4; }
			else{ countryDD.options.selectedIndex = 0; }
		} catch (ex){}
	}
	if (dateFilter.length > 1) {
		document.getElementById("byDateTxt").value = dateFilter;		
	}
}



// Derived from the Bill Dortch code at http://www.hidaho.com/cookies/cookie.txt

function msgetCookieVal(offset) {
    var endstr = document.cookie.indexOf(";", offset);
    if (endstr == -1) { endstr = document.cookie.length; }
    return unescape(document.cookie.substring(offset, endstr));
}

function MSGetCookie(name) {
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    while (i < clen) {
        var j = i + alen;
        if (document.cookie.substring(i, j) == arg) {
            return msgetCookieVal(j);
        }
        i = document.cookie.indexOf(" ", i) + 1;
        if (i == 0) break;
    }
    return null;
}

function MSDeleteCookie(name, path, domain) {
    if (MSGetCookie(name)) {
        document.cookie = name + "=" +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}

function MSSetCookie(name, value, expires) {
    document.cookie = name + "=" + escape(value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    "; path=/";
}

