/// Redirect to previous page.
function goBack() {
    history.go(-1);
}

/// Redirect to location.
function redirect(location)
{
    window.location = location;
}

/// Alerts message.
function popAlert(message) {
    alert(message);
}

/// Popup a new window.
function popUp(id, URL, w, h) 
{
    if ((w == '0') || (w == 0) || (h == '0') || (h == 0))
    {
        eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0');");
    }
    else
    {
        eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=" + w + ",height=" + h + "');");
    }
}

/// Trim string.
function trim(value) 
{
	return value.replace(/^\s+|\s+$/g,"");
}

/// Set value to an element.
function setValue(elementID, value)
{
    var element = document.getElementById(elementID);
    
    if (element)
    {
        element.value = value;
    }
    else
    {
        alert('error at setValue(' + elementID + ', ' + value + ')');
    }
}

/// Append value into an element.
function addToElement(elementID, value, delimiter)
{
    var element = document.getElementById(elementID);
    
    if (element)
    {
        element.value = addToString(element.value, value, delimiter);
    }
}

/// Remove value from an element.
function removeFromElement(elementID, value, delimiter)
{
    var element = document.getElementById(elementID);
    
    if (element)
    {
        element.value = removeFromString(element.value, value, delimiter);
    }
}

/// Append value into string delimited by delimiter.
function addToString(string, value, delimiter)
{
    if (string)
    {
        var arr = string.split(delimiter);
    
        //return string if value existed in string.
        for (var i=0; i<arr.length; i++)
        {
            if (arr[i] == value) 
            {
                return string;
            }
        }
        
        //if value does not exist in string, append to string.
        if (string != '')
        {
            string += delimiter;
        }
        string += value;
    }
    else
    {
        string = value;
    }
    
    return string;
}

/// Remove value from string splited by delimiter.
function removeFromString(string, value, delimiter)
{                                                
    var arr = string.split(delimiter);
    
    string = '';
    //appends all elements (except value) in arr back into string.
    for (var i=0; i<arr.length; i++)
    {
        if ((arr[i] != value) && (arr[i] != '')) 
        {
            if (string != '')
            {
                string += delimiter;
            }
            string += arr[i];
        }
    }
    
    return string;
}

function portalLanguage_OnChange(sender) {
    var selectedValue = sender.value.toLowerCase();

    if (selectedValue == 'ct') {
        //redirect to Taiwan site if language selected is 'Chinese Traditional'
        window.location = 'https://www.training-partners.com.tw/';
    }
    else {
        //redirect to other sites.
        var currentPortal = window.location.toString().toLowerCase();

        if (currentPortal.lastIndexOf('/') == (currentPortal.length - 1)) {
            //location ends with '/'.
            currentPortal = currentPortal.substr(0, currentPortal.length - 1);
        }
        else {
            //location ends with a page name.
            currentPortal = currentPortal.substr(0, currentPortal.lastIndexOf('/'));
        }

        currentPortal = currentPortal.substr(0, currentPortal.lastIndexOf('/') + 1);
        currentPortal += selectedValue + '/'

        window.location = currentPortal;
    }
}

function portalLanguage_OnLoad() {
    var select = document.getElementById('select');
    var currentPortal = window.location.toString().toLowerCase();
    
    if (currentPortal.lastIndexOf('/') == (currentPortal.length - 1)) {
        //location ends with '/'.
        currentPortal = currentPortal.substr(0, currentPortal.length - 1);
        currentPortal = currentPortal.substr(currentPortal.lastIndexOf('/') + 1);
    }
    else {
        //location ends with a page name.
        currentPortal = currentPortal.substr(0, currentPortal.lastIndexOf('/'));
        currentPortal = currentPortal.substr(currentPortal.lastIndexOf('/') + 1);
    }

    select.value = currentPortal;
}
