/*

	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+$/, ''); };

function str_replace(haystack, needle, replacement) {
    var r = new RegExp(needle, 'g');
    return haystack.replace(r, replacement);
}

// Sets lW3C CSS Validation link to current URL
function SetCSSValidationLink() {

	var url = "http://jigsaw.w3.org/css-validator/validator?uri=" + escape(document.location.href) + "&profile=css21&usermedium=all&warning=1&lang=en";
	document.getElementById('CSSValidator').href = url;
	
}

// 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 );

}

/*

	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 ) {
		
		var elements = document.getElementsByTagName("select");
		
		for (var i = 0; i < elements.length; i++) {
			elements[i].style.visibility = 'hidden';
		}		
		
	}

}

/*

	Shows inputs
	
*/
function ShowInputs() {

	var ver = navigator.appVersion;
	var isIE6 = ver.indexOf("MSIE 6.0") != -1;
	
	if ( isIE6 ) {
		
		var elements = document.getElementsByTagName("select");
		
		for (var i = 0; i < elements.length; i++) {
			elements[i].style.visibility = 'visible';
		}		
		
	}
	
}