/*

	Author:			Jay Dobson
	Date:			Jan 28, 2008
	Description:	Provides general helper methods

*/

// Returns trimmed version of a given string
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };

// Shows not available message (used when disabling functions for i:Create side)
function showNAMessage() {
	alert('This function is not available in i:Create.');
}

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';
	

}

// Used for sector section of home page
function MM_jumpMenu(targ, selObj, restore) {

	var url = selObj.options[selObj.selectedIndex].value;
	
	if (url == '') {
		selObj.selectedIndex = 0;
		return;
	}
	
	eval(targ + ".location='" + url + "'");
	if (restore) selObj.selectedIndex = 0;
  
}

// Handles autoTab functionality for fields
// pcID = Previous Control ID, ccID = Current Control ID, ncID = Next Control ID
function autoTab(event, pcID, ccID, ncID) 
{

	var isBack = (event.keyCode == 8);
	
	var pc = document.getElementById(pcID);
	var cc = document.getElementById(ccID);
	var nc = document.getElementById(ncID);
	
	if ( isBack && cc.value.length == 0 && pc != null ) {
		pc.focus();
		pc.select();
	}
	
	if ( cc.value.length >= cc.getAttribute("maxlength") && nc != null ) {
		nc.focus();
		nc.select();
	}
	
}

// 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 );

}