function setDispatchOperation(form, dispatchOperation)
{
    var operation = getOperation(form);
    if(operation == null)
    {
        var operation = document.createElement("input");
        operation.type = "hidden";
        operation.name = "operation";
        operation.value = dispatchOperation;
        form.appendChild(operation);
    }
    else operation.value = dispatchOperation;
}

function getDispatchOperation(form)
{
    var operation = getOperation(form);
    
    if(operation == null) return "";
    else return operation.value;
}

function getOperation(form)
{
    // IE has a bug not adding dynamically created field as named properties 
    //so we have to loop through the elements array to find the field
    var formElements = form.elements;
    
    for(var i = 0; i < formElements.length; i++ ){
        if(formElements[i].name == "operation") return formElements[i];
    }
    
    return null;
}

function submitForm(form, dispatchOperation)
{
    setDispatchOperation(form, dispatchOperation);
    
    form.submit();
}

//trims whitespace in start and end of string
function trim(stringToTrim)
{
    return stringToTrim.replace(/^\s*|\s*$/g,"");
}


function displayValidationErrors(errors, customErrorsSectionId, customErrorsListSectionId)
{
    var errorsString = "";
    
    for(var i = 0; i < errors.length; i++)
    {
        errorsString += "<br>" + errors[i];
    }
    
    //validationErrorsSection and validationErrorsListSection are the elements generated by the Struts validation framework
    
    var validationErrorsSection = document.getElementById("validationErrorsSection");
    var validationErrorsListSection = document.getElementById("validationErrorsListSection");
    
    if( validationErrorsSection == null ) validationErrorsSection = document.getElementById(customErrorsSectionId);
    if( validationErrorsListSection == null ) validationErrorsListSection = document.getElementById(customErrorsListSectionId);
    
    validationErrorsSection.style.display = "";
    validationErrorsSection.scrollIntoView(true);
    validationErrorsListSection.innerHTML = errorsString;
}

function hideValidationErrors(customErrorsSectionId)
{
    if(document.getElementById("validationErrorsSection") != null)
        document.getElementById("validationErrorsSection").style.display = "none";
    
    document.getElementById(customErrorsSectionId).style.display = "none";
}

function openContactInfoByRH(rhId) {
    openPopup( "contactInfo.do?rhId="+rhId,null,
               "height=250,width=430,status=no,toolbar=no,menubar=no,location=no");
}

function openContactInfoByRgt(rgtInst, dateOfUse) {
    openPopup( "contactInfo.do?rgtInst="+rgtInst+"&dateOfUse="+dateOfUse,null,
               "height=250,width=430,status=no,toolbar=no,menubar=no,location=no");
}

//append isPopup request parameter to url
function openPopup(url, name, features)
{
    var requestParameterPrefix = ( url.indexOf("?") >= 0 ) ? "&" : "?";   
    window.open( url + requestParameterPrefix + "com.copyright.ispopup=true", name, features );
}

/*
 * Returns the value of the provided URL parameter name, for the current URL, if available. 
 * Otherwise, it returns null. [lalberione]
 */
function getCurrentURLParameter( urlParamName ){
    var urlParamValue = null;
    
    var urlParamRegexString= "[?&]" + urlParamName + "=([^&$]*)";
    var urlParamRegex = new RegExp( urlParamRegexString );

    var currentURL = document.location.href;

    var match = currentURL.match( urlParamRegex );

    if( match ) urlParamValue = unescape( match[1] );
    
    return urlParamValue;
}