/**
 * This JavaScript library provides e.g. TYPO3 HTML templates to be able to contain
 * hard-coded links while maintaining the ability to provide multi-language support.
 *
 * @author Markus L. Dechert <md@weaveit.de>
 * @date 04/13/2007
 */

/**
 * Consider this function a private helper to the addQueryString(anchorTag) function.
 * It parses the document.URL with all its GET parameters and returns an associative
 * array consisting of all the parameters.
 */ 
function getParamStringArray() {
    var result = new Object();
    var paramString = document.URL.replace(/.+\?/, '').split('&'); // get the whole parameter string
    for (var i=0; i<paramString.length; i++) {
        var paramSplit = paramString[i].split('=');
        result[paramSplit[0]] = paramSplit[1];
    }
    return result;
}

/**
 * Use this function to append the (GET) parameters from the current string to
 * the href attribute of the passed anchor tag.
 * Note: Parameters which are already set in the href tag are preserved.
 */
function addQueryString(anchorTag) {
    if (anchorTag && anchorTag.href) {
        var paramArray = getParamStringArray();
        var hrefParamString = anchorTag.href.replace(/.+\?/, '');

        for (paramName in paramArray) {
            if (hrefParamString.indexOf(paramName+"=") == -1) { // don't overwrite parameters set in the href of the tag
                anchorTag.href += (anchorTag.href.indexOf('?') == -1) ? '?' : '&';
                anchorTag.href += paramName+"="+paramArray[paramName];
            }
        }
    }
}