/*

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() {

//    alert("document locatoin" + document.location);
//    alert("parent locatoin" + parent.location);
    var str = document.location.href;
    var newUrl = str;

    if (str.indexOf("/en/") >= 0) {
        newUrl = newUrl.replace("/en/", "/fr/");
    }

    if (str.indexOf("lang=en") >= 0) {
        newUrl = newUrl.replace("lang=en", "lang=fr");
    }

    if (str.indexOf("=en") >= 0) {
        newUrl = newUrl.replace("=en", "=fr");
        var index = newUrl.indexOf("=en");

        while (index != -1) {
            newUrl = newUrl.replace("=en", "=fr");
            index = newUrl.indexOf("=en");
        }
    }

    if (str.indexOf("/fr/") >= 0) {
        newUrl = newUrl.replace("/fr/", "/en/");
        document.location = newUrl;
    }

    if (str.indexOf("=fr") >= 0) {
        newUrl = newUrl.replace("=fr", "=en");
        var index = newUrl.indexOf("=fr");

        while (index != -1) {
            newUrl = newUrl.replace("=fr", "=en");
            index = newUrl.indexOf("=fr");
        }
    }

    parent.location = newUrl;
}

// 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', 'hidden');

    }

}

// Shows inputs
function ShowInputs() {

    var ver = navigator.appVersion;
    var isIE6 = ver.indexOf("MSIE 6.0") != -1;

    if (isIE6) {

        jQuery('#printArea input').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);

}
