/** Whether to hide crappy urls or not (for bookmarking) */
var hiddenParameters = false;

/** Used to automatically scroll to an anchor/id when performing an action or going to a view */
function getParamPagePosition() {return '_navPagePosition';}

/** Set of safe shortcuts for common dom operations */
function getElement(id) {return document.getElementById(id);}
function getElementName(id) {return getElement(id) != null ? getElement(id).name : '';}
function getElementValue(id) {return getElement(id) != null ? getElement(id).value : '';}
function getElementChecked(id) {return getElement(id) != null ? getElement(id).checked : false;}
function getElementSelected(id) {return getElement(id) != null ? getElement(id).selected : false;}
function getElementInnerHTML(id) {return getElement(id) != null ? getElement(id).innerHTML : '';}
function setElementName(id, name) {if (getElement(id) != null) getElement(id).name = name;}
function setElementValue(id, value) {if (getElement(id) != null) getElement(id).value = value;}
function setElementChecked(id, checked) {if (getElement(id) != null) getElement(id).checked = checked;}
function setElementSelected(id, selected) {if (getElement(id) != null) getElement(id).selected = selected;}
function setElementInnerHTML(id, html) {if (getElement(id) != null) getElement(id).innerHTML = html;}

/** private function used by doView and doAction **/
function addForm(formName)
{
    var hform;
    var existed = false;

    try
    {
        hform = getElement(formName);
        existed = hform != null;

        hform = existed ? hform : document.createElement('form');
        hform.setAttribute('method', 'post');
        hform.setAttribute('action', getAppLocal() + '/');
        hform.setAttribute('id', formName);
    }
    catch(e)
    {
        hform = document.createElement('<form method="post" action="' + getAppLocal() + '/" id="' + formName + '"/>');
    }

    if (!existed) document.body.appendChild(hform);
    return hform;
}

/** private function used by doView and doAction **/
function addInput(formName, name, value)
{
    var hform = (getElement(formName) != null) ? getElement(formName) : addForm(formName);
    var hinput;

    try
    {
        hinput = (getElement(name) != null) ? getElement(name) : document.createElement('input');
        hinput.setAttribute('type', 'hidden');
        hinput.setAttribute('name', name);
        hinput.setAttribute('value', value);
    }
    catch(e)
    {
        hinput = document.createElement('<input type="hidden" name="' + name + '" value="' + value + '"/>');
    }

    hform.appendChild(hinput);
    return hinput;
}

/** Always submit to the default page, regardless of the current page **/
function getNavigationURL()
{
    var url = window.location.pathname;
    try {url = url.substring(0, url.lastIndexOf("/") + 1);} catch(e) {}
    return url;
}

/** Transition to a new page **/
function doView()
{
    var formName = '_temp';
    var view = arguments[0];
    var url = getNavigationURL() + "?";
    var scrollTo = '';

    addForm(formName);

    if (hiddenParameters)
    {
        addInput(formName, getParamView(), view);
        addInput(formName, getParamTemplate(), getTemplate());
    }
    else
    {
        url = url + getParamView() + "=" + view + "&";
        url = url + getParamTemplate() + "=" + getTemplate() + "&";
    }

    for (i = 1; arguments != null && i < arguments.length; i = i + 2)
    {
        if (arguments[i] == getParamPagePosition())
        {
            scrollTo = arguments[i + 1];
        }
        else
        {
            if (hiddenParameters) addInput(formName, arguments[i], arguments[i + 1]);
            else url = url + arguments[i] + "=" + arguments[i + 1] + "&";
        }
    }

    // handle preservation of locale
    if (getDefaultLocale() != '' && getCurrentLocale() != '' && getDefaultLocale() != getCurrentLocale())
    {
        url = url + getParamLocale() + "=" + getCurrentLocale();
    }
    
    var form = getElement(formName);
    if (!hiddenParameters) form.action = url;
    if (scrollTo != '') form.action += ('#' + scrollTo);
    form.submit();
    return false;
}

/** Perform a server side action **/
function doAction()
{
    var method = arguments[0];
    var formName = arguments[1];
    var url = getNavigationURL() + "?";
    var scrollTo = '';
    var myfestTabCapable = false;
    var myfestTab = '';

    if (formName == null || formName == '') formName = '_temp';
    addForm(formName);

    var form = getElement(formName);
    // temporary hack to prevent double-submitting for showHideReview (MK - 9/24/2006)
    if (method == getActionShowHideReview()) hiddenParameters = true;
    if (form.disabled) return false;
    if (form.enctype == "multipart/form-data" || form.encoding == "multipart/form-data") hiddenParameters = true;
    if (window.getParamMyfestTab) myfestTabCapable = true;

    var templateOverride = false;
    var viewOverride = false;

    for (i = 2; arguments != null && i < arguments.length; i = i + 2)
    {
        if (arguments[i] == getParamTemplate()) templateOverride = true;
        if (arguments[i] == getParamView()) viewOverride = true;
        if (myfestTabCapable && arguments[i] == getParamMyfestTab()) myfestTab = arguments[i + 1];

        if (arguments[i] == getParamPagePosition())
        {
            scrollTo = arguments[i + 1];
        }
        else
        {
            if (hiddenParameters) addInput(formName, encodeURIComponent(arguments[i]), encodeURIComponent(arguments[i + 1]));
            else url = url + encodeURIComponent(arguments[i]) + "=" + encodeURIComponent(arguments[i + 1]) + "&";
        }
    }

    if (hiddenParameters)
    {
        addInput(formName, getParamAction(), method);
        if (!templateOverride) addInput(formName, getParamTemplate(), getTemplate());
        if (!viewOverride) addInput(formName, getParamView(), getView());
    }
    else
    {
        url = url + getParamAction() + "=" + method + "&";
        if (!templateOverride) url = url + getParamTemplate() + "=" + getTemplate() + "&";
        if (!viewOverride) url = url + getParamView() + "=" + getView() + "&";

        var inputs = document.getElementById(formName).elements;

        for (i = 0; i < inputs.length; i++)
        {
            if (url.indexOf(inputs[i].name) == -1 && inputs[i].value.length < 32)
            {
                // if the element is a checkbox only submit it if it is checked
                if ( (inputs[i].type != 'checkbox') ||
                    (inputs[i].checked == true) )
                {
                    url = url + inputs[i].name + "=" + inputs[i].value + "&";
                }
            }
        }
    }

    if (!hiddenParameters) form.action = url;
    if (form.enctype == "multipart/form-data" || form.encoding == "multipart/form-data")
    {
        form.action = url + getParamView() + "=" + getView();
        if (myfestTabCapable) form.action += ("&" + getParamMyfestTab() + "=" + myfestTab);
    }
    if (scrollTo != '') form.action += ('#' + scrollTo);
    form.submit();
    form.disabled = true;
    return false;
}

/** Standard popups **/
function doPopup()
{
    window.open(arguments[0], '', 'width=800,height=600,scrollbars=yes');
    return false;
}

/** Links to an external site */
function doLink(a)
{
    if (!a.getAttribute('redirected'))
    {
        a.href = getAppLocal() + '/redirect.jsp?' + getParamRedirectUrl() + '=' + encodeURIComponent(a.href);
        a.setAttribute('redirected', true);
    }

    return true;
}

/** Gets the path to the app as it is in the client's browser (the path returned will end with a '/') */
function getClientAppPath()
{
    appPath = window.location.href.substring(0, window.location.href.length - window.location.search.length);
    if (appPath.charAt(appPath.length - 1) != '/') appPath += '/';

    return appPath;
}

/** Sets the locale in the url to the given value */
function setLocale(loc)
{
    var regexp = new RegExp("([\\?&])" + getParamLocale() + "=[^&]*");
    var newSearch = window.location.search;
    if (newSearch.search(regexp) >= 0)
    {
        newSearch = newSearch.replace(regexp, "$1" + getParamLocale() + "=" + loc);
    }
    else
    {
        if (newSearch.length == 0) newSearch += '?';
        else if (newSearch.search(/&$/) < 0) newSearch += '&';
        newSearch += getParamLocale() + "=" + loc;
    }

    window.location.search = newSearch;
}