function openWindow(url,name,w,h,features) {
  if(screen.width) {
      var winl = (screen.width-w)/2;
      var wint = (screen.height-h)/2;
  }
  else {
      winl = 0;wint =0;
  }
  if (winl < 0) winl = 0;
  if (wint < 0) wint = 0;
  var settings = 'height=' + h + ',';
  settings += 'width=' + w + ',';
  settings += 'top=' + wint + ',';
  settings += 'left=' + winl + ',';
  settings += 'scrollbars=1' + ',';
  settings += features;
  win = window.open(url,name,settings);
  win.window.focus();
}

function openWindowAndSetOpener(url,name,w,h,features) {
  if(screen.width) {
      var winl = (screen.width-w)/2;
      var wint = (screen.height-h)/2;
  }
  else {
      winl = 0;wint =0;
  }
  if (winl < 0) winl = 0;
  if (wint < 0) wint = 0;
  var settings = 'height=' + h + ',';
  settings += 'width=' + w + ',';
  settings += 'top=' + wint + ',';
  settings += 'left=' + winl + ',';
  settings += 'scrollbars=1' + ',';
  settings += features;
  win = window.open(url,name,settings);
  win.opener = self;
  win.window.focus();
}

function populateForm(theForm,fieldName,fieldValue,message) {
  theForm.elements[fieldName].value = fieldValue;
  if (message != null && message != "") {
    alert(message);
  }
}

function populateDiv(divId,value) {
  var divElement = document.getElementById(divId);
  if (divElement) {
    divElement.innerHTML = value;
  }
}

function join_field(selectionId) {
    var selection = document.getElementById(selectionId);
    var value = "";
    var length = selection.options.length;
    for (i = 0; i < length; i++) {
        var candidateOption = selection.options[i];
        if (candidateOption.selected) {
            if (value != "") value = value + ",";
            value = value + candidateOption.value;
        }
    }
    selection.value = value;
    //alert(value);
    return false;
}

function updateElementValue(elementId, newValue) {
    var element = document.getElementById(elementId);
    if (element) {
        element.value = newValue;
    }
}

// Used to block or hide an element, using the + & - button images
function f_expand(imgPath, fieldName) {
    var imgRef = document.getElementById(fieldName+'ImgExpand');
    var fieldRef = document.getElementById(fieldName);

    if (imgRef.src.indexOf('btnShrink') != -1) {
        imgRef.src = imgPath + 'btnExpand.gif';
        fieldRef.style.display='none';
    } else {
        imgRef.src = imgPath + 'btnShrink.gif';
        fieldRef.style.display=''; // block causes problems
    }
}

// If the element is hidden, will show it, and vice versa
function f_showHide(fieldName) {
    var fieldRef = document.getElementById(fieldName);

    if (fieldRef.style.display=='none') {
        fieldRef.style.display='';
    } else {
        fieldRef.style.display='none';
    }
}

// If the element is hidden, will show it, and vice versa
function f_switchClass(fieldName, class1, class2) {
    var fieldRef = document.getElementById(fieldName);
    
    if (fieldRef.className==class1) {
        fieldRef.className=class2;
    } else {
        fieldRef.className=class1;
    }
}

function getNumericToken(node, matchNumber) {
    var nodeId = node.id;
    var tokens = nodeId.split("-");
    var currentMatchNumber = 0;
    for (var i=0; i<tokens.length; i++) {
        var exp = /^(\d*)$/;
        var result = tokens[i].match(exp);
        if (result) {
            if (currentMatchNumber == matchNumber) {
                return tokens[i];
            }
            else {
                currentMatchNumber = currentMatchNumber + 1;
            }
        }

    }
    return "";
}

function showNotImplementedAlert() {
    alert("This feature has not yet been implemented");
    return false;
}

function noImpl() {

}

function popupApplicationContactUsForm(context, applicationId, details) {
    var path = context + '/email/edit.do?';
    if (applicationId) {
        path += 'applicationId=' + applicationId  + "&";
    }
    if (details) {
        path += 'details=' + details + "&";
    }
    path += new Date().getTime(); // ensures browser caching doesn't occur
    openWindowAndSetOpener(path, 'contactUsForm', 600, 400, '');
    return false;
}

function popupGenericContactUsForm(context, subject, details) {
    var path = context + '/email/edit.do?';
    if (subject) {
        path += 'subject=' + subject + "&";
    }
    if (details) {
        path += 'details=' + details + "&";
    }
    path += new Date().getTime(); // ensures browser caching doesn't occur
    openWindowAndSetOpener(path, 'contactUsForm', 600, 400, '');
    return false;
}
