/*

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+$/, ''); };

// Place functions to be called on the window's load event here
Event.observe(window, 'load', function() {

    SetCSSValidationLink();

})

// 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;
    }

	else if (str.indexOf("5796669c-d893-41ba-ba5c-f615ce358f58") >= 0) {
		url = url.replace("5796669c-d893-41ba-ba5c-f615ce358f58", "ac6bea4d-e082-4bb7-a869-9939a07b4f92");
		document.location = url;
	}

	else if (str.indexOf("ac6bea4d-e082-4bb7-a869-9939a07b4f92") >= 0) {
		url = url.replace("ac6bea4d-e082-4bb7-a869-9939a07b4f92", "5796669c-d893-41ba-ba5c-f615ce358f58");
		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);

}

//to merge pages to dropdown otions on template_careerprofile_interior
function selectCareer() {

    //    document.location = "/230542_MIHR" + document.getElementById('careerProfile').value;
    document.location = document.getElementById('careerProfile').value;

}

window.onload = function() {
	var pageTracker=_gat._getTracker('UA-17569025-1');
	var el = jQuery("a[href^='http']").each(function(index) {
		jQuery(this).click(function() {
			var href = jQuery(this).attr('href');
			
			pageTracker._trackEvent('Outbound Links', href);
			
			setTimeout('document.location = "' + href + '"', 500);

			return false;
		} );
	} );
}