/**
* This function will take the parameters from requestParamters and add them to the original URL.
* We assume the parameters in requestParameters are formatted like param1=value1&param2=value2*...
* If any url parameters already exist in the originalURL, we don't add them again.
* 
*/
function addURLParameters(originalURL, requestParameters) {
    try {
        // newURL is the url we will be constructing.
        var newURL = originalURL;
        var parameters = requestParameters.split('&');
        
        for(i = 0; i < parameters.length; i++) {
            // Skip entries that have no size
            if( parameters[i].length == 0 ) {
                continue;
            }
            
            var indexOfEquals = parameters[i].indexOf('=');
            
            // If the equals begins the token, we should skip
            if(indexOfEquals == 0) {
                continue;
            }
            
            // Extract the parameter name and the parameter value from the parameter
            var parameterName;
            if(indexOfEquals == -1) {
                parameterName = parameters[i];  
            }
            else {
                parameterName = parameters[i].substr(0, indexOfEquals); 
            }
            var parameterValue;
            
            // Try to extract the parameterValue.  Check the case where there was no equals.
            if(parameters[i].length <= indexOfEquals || indexOfEquals == -1) {
                parameterValue = '';
            }
            else {
                parameterValue = parameters[i].substr(indexOfEquals + 1);
            }
            
            var nameValuePair;
            
            // Determines whether the resultant string should have an equals or not.
            if (indexOfEquals == -1) {
                nameValuePair = parameterName;
            }
            else {
                nameValuePair = parameterName + '=' + parameterValue;
            }

            // Check if the original URL had this parameter, if so, just skip. 
            // We don't want to overwrite existing parameters.
            if (newURL.indexOf('&' + parameterName + '=') == -1 &&
                    newURL.indexOf('?' + parameterName + '=') == -1) {
            
                // Determine whether we need to append a ? or a &
                var indexOfQuestionMark = newURL.indexOf('?');
                if (indexOfQuestionMark == -1) {
                    newURL = newURL + '?' + nameValuePair;
                }
                else {
                    newURL = newURL + '&' + nameValuePair;
                }
            }
        }

        return newURL;
    }
    catch (e) {
        alert('Failure:' + e.name + ':' + e.message);       
        
        alert('originalURL:' + originalURL);
        
        return originalURL;
    }
        
}

/**
* Adds the parameters from the URL parameters to each link.href and form.action on the page.
* This function uses JQuery to parse each 'a' tag and 'form' on the page.  This function will
* behave correctly with elements have existing URL parameters in the URL.
*/
function addURLParametersToLinksAndForms() {
    var indexOfQuestionMark = window.location.href.indexOf('?');
    var requestParameters;
    if(indexOfQuestionMark != -1) {
        requestParameters = window.location.href.substr(indexOfQuestionMark + 1);
    }
    else {
        requestParameters = ''; 
    }
    
    jQuery('form').each(function(i) { 
        var newURL = addURLParameters(this.action, requestParameters);
        this.action = newURL; 
    });
    
    jQuery('a').each(function(i) {
        // Only append URLs to anchor tags that are leading to web addresses, not javascript invocations.
    	if (!this.href.startsWith('javascript:')) {
    		var newURL = addURLParameters(this.href, requestParameters);
    		this.href = newURL;
    	}
    });
}

// We want to add the URL parameters to all links and forms present on the page.
// This function will execute each time the page loads.
jQuery(document).ready(addURLParametersToLinksAndForms);