//------ parent window functions 
// This function opens a popup window.
function showPopup(url, width, height){
    window.open(url, 'popup', 'toolbar=0,scrollbars=1,location=0,statusbar=1,menubar=0,resizable=1,width='+ width +',height='+ height +',left = 262,top = 134');
}

// This function adds a parameter to a query string for a given URL. The parameter name and value are the name and value of an HTML input field.
function addFieldToUrl( url, field ){
    if ( url.indexOf('?') < 0 ){
        url += '?';
    }
    else{
        url += '&';
    }
    
    url += field.name +'='+ field.value;
    
    return url;
}

// This function adds a parameter to a query string for a given URL.
function addParameterToUrl( url, name, value ){
    if ( url.indexOf('?') < 0 ){
        url += '?';
    }
    else{
        url += '&';
    }

    url += name +'='+ value;

    return url;
}

// This function opens a popup and passes the name and value of the calling field and puts the name of the calling field in a parameter called 'field'.
function popup(url, displayField, width, height){
    url = addParameterToUrl(url, 'field', displayField.name);
    url = addFieldToUrl(url, displayField);
    showPopup(url, width, height);
}

// This function opens a popup and passes the name and value of the calling display field and
// puts the name of the calling display field in a parameter called 'fieldName'. It does the same for the calling id field,
// but the parameter is called 'secondaryFieldName'.
function popup(url, displayField, idField, width, height){
    url = addParameterToUrl(url, 'fieldName', displayField.name);
    url = addParameterToUrl(url, 'secondaryFieldName', idField.name);
    url = addFieldToUrl(url, displayField);
    url = addFieldToUrl(url, idField);
    showPopup(url, width, height);
}


//------ popup window functions 
// This function sets the value of a field in the caller page to a specified value.
function setParentValue(value, targetId){
    window.opener.document.getElementById(targetId).value = value;
} 

// This function sets the value of the caller field to a specified value.
function returnPopupValue(targetId, value){
    setParentValue(value, targetId);
    window.close();
}

// This function sets the value of the caller display field and the caller id field to specified values.
function returnPopupValues(firstTargetId, secondTargetId, firstValue, secondValue){
    setParentValue(firstValue, firstTargetId);
    setParentValue(secondValue, secondTargetId);
    window.close();
}