/*

	Author:			Jay Dobson
	Date:			Jan 28, 2008
	Description:	Provides general helper methods

*/

// Hides quiclinks dropdown box (for IE 6 hovering) so that when user hovers
// over the 4th and 5th menu items, the drop-down list will not overlap.
function HideQuickLinks(ele) {

/* Not used anymore
var nVer = navigator.appVersion;
var nAgt = navigator.userAgent;
var browserName  = '';
var fullVersion  = 0; 

if ( (verOffset=nAgt.indexOf("MSIE")) != -1 ) {
 browserName  = "IE";
 fullVersion  = parseFloat(nAgt.substring(verOffset+5));
}

	if (browserName == 'IE' && fullVersion == 6) {
		if (ele.id == 'navtab4' || ele.id == 'navtab4on' || ele.id == 'navtab5' || ele.id == 'navtab5on')
			document.getElementById('jumpMenu').style.visibility = 'hidden';
	}
*/
		
}

function ShowQuickLinks(ele) {

/* Not used anymore
var nVer = navigator.appVersion;
var nAgt = navigator.userAgent;
var browserName  = '';
var fullVersion  = 0; 

if ( (verOffset=nAgt.indexOf("MSIE")) != -1 ) {
 browserName  = "IE";
 fullVersion  = parseFloat(nAgt.substring(verOffset+5));
}

	if (browserName == 'IE' && fullVersion == 6) {
		if (ele.id == 'navtab4' || ele.id == 'navtab4on' || ele.id == 'navtab5' || ele.id == 'navtab5on')
			document.getElementById('jumpMenu').style.visibility = 'visible';
	}
*/
	
}

// Returns trimmed version of a given string
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };

var url = document.location.href;

// Switches the language of the page by changing the URL
function switchLanguage() {
	
	var str = document.location.href;

	if(str.indexOf("/en/") >= 0){
		url = url.replace("/en/", "/fr/");
		document.location = url;
	}
	
	else if(str.indexOf("lang=en") >= 0){
		url = url.replace("lang=en", "lang=fr");
		document.location = url;
	}
	
	else if(str.indexOf("/fr/") >= 0){
		url = url.replace("/fr/", "/en/");
		document.location = url;
	}
	
	else if(str.indexOf("lang=fr") >= 0){
		url = url.replace("lang=fr", "lang=en");
		document.location = url;
	}
	
}

// Used for entering and leaving search textbox so the user doesn't have to clear the 'Search' text
function Search_Enter(searchTextbox) {

	if ( searchTextbox.value.toLowerCase() == 'recherche' || searchTextbox.value.toLowerCase() == 'search' )
		searchTextbox.value = '';

}

function Search_Leave(searchTextbox) {

	var url = document.location.href;

	if( url.indexOf("/en/") >= 0 && searchTextbox.value.trim() == '' )
		searchTextbox.value = 'Search';
	
	if( url.indexOf("/fr/") >= 0 && searchTextbox.value.trim() == '' )
		searchTextbox.value = 'Recherche';
	

}

function MM_jumpMenu(targ,selObj,restore){ //v3.0

  var selURL = selObj.options[selObj.selectedIndex].value;
  
  if (selURL == '')
  {
	// nothing need to do
  }
  
  else if (selURL.indexOf('~') == 0)
  {
  	window.open(selURL.substr(1, selURL.length), "_blank");
  }
  
  else
  {
   eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  }
  
  if (restore) selObj.selectedIndex=0;
  
}

function checkPageSecurity() {

    var currentUrl = document.location.href;
    var secured = currentUrl.indexOf("https://") == 0;
    
    if (typeof(_page_need_secure_) != 'undefined' && _page_need_secure_) {

        if (!secured)
            document.location.href = "https://" + currentUrl.substr(7);

    } 
    
    else {

        if (secured)
            document.location.href = "http://" + currentUrl.substr(8);

    }
    
}

// check security before any other scripts
var oldonload = window.onload;

if (typeof window.onload != 'function') {
    window.onload = checkPageSecurity;
} 

else {

	window.onload = function() {
		//checkPageSecurity();
		//oldonload();
	};
	
}
// Takes in a textbox reference, a label ID and the max # of characters that can go into the textbox
// When the input reaches the maxChars limit any further typing is 'cut-off' and the 
// label (displaying the number of characters left) turns red
function Counter(textbox, label, maxChars) {

	lbl = document.getElementById(label);
	
	if (textbox.value.length >= maxChars) {
		lbl.style.color = 'red';
		textbox.value = (textbox.value).substring(0, maxChars);
		textbox.scrollTop = textbox.scrollHeight;		
	}
	
	else {
		lbl.style.color = 'black';
	}
	
	lbl.innerHTML = maxChars - ( textbox.value.length );

}
