/*

	Author:			Jay Dobson
	Date:			Jan 28, 2008
	Description:	Provides general helper methods

*/

// Variables
var url = document.location.href;

// 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.');
}

// Replaces single quote and returns resulting string
function repSingleQuote(str) {
	return str.replace("'", "\'");
}			

// Switches the language of the page by changing the URL
function switchLanguage() {

    var str = document.location.href;
//    var test = str.contains("newsId");
 
    if (str.indexOf("newsId") >= 0) {
        url = url.substring(0, url.indexOf("newsId"));
    }
    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;
	}
	
}

// Hides inputs - This is used for Internet Explorer 6, as they show through the drop-down menu	
function HideInputs() {
	
	var ver = navigator.appVersion;
	var isIE6 = ver.indexOf("MSIE 6.0") != -1;
	
	if ( isIE6 ) {

	    jQuery('#printArea input').css('visibility', 'visible');
	    jQuery('#printArea select').css('visibility', 'visible');
	    jQuery('#printArea textarea').css('visibility', 'visible');			
		
	}

}

// Shows inputs
function ShowInputs() {

	var ver = navigator.appVersion;
	var isIE6 = ver.indexOf("MSIE 6.0") != -1;
	
	if ( isIE6 ) {

	    jQuery('#printArea input').css('visibility', 'visible');
	    jQuery('#printArea select').css('visibility', 'visible');
	    jQuery('#printArea textarea').css('visibility', 'visible');					
		
	}
	
}

function GotoSearch(src)
{
	window.location.href = src + "?strSearch=" + escape(document.getElementById("searchField").value);	
}

// 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';
	

}

// 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 );

}