/**
 * Function openWindow
 *
 * Makes a popup with defined width/height attributes.
 *
 * @author Adrian Sobotta
 *
 */
function openWindow(URL) {
  var newWindow = window.open(URL,'','width=590,height=480,scrollbars=yes,resizable=yes');
}
function openBlankWindow(URL) {
  var newWindow = window.open(URL,'','width=500,height=300,scrollbars=yes,resizable=yes');
}

/**
 * Function printpage
 *
 * spawns the print dialog box on the client.
 *
 * @author Adrian Sobotta
 */
function PrintPage() {
  window.print();
}


/**
 * Function checkRatioForm
 *
 * Just validates that the user has selected atleast one item to graph. If not, then
 * it displays a little error message.
 *
 * @author Adrian Sobotta
 *
 */
function checkRatioForm(formIn) {
  var elArr = formIn.elements;

  var filledIn = false
  for(var i = 0; i < elArr.length; i++) {
    if(elArr[i].checked == true){
      filledIn = true;
    }
  }

  if (filledIn == false){
    alert('You must provide at least one item to graph. Please try again.');
    return false;
  }

  return true;

}

/**
 * Function DownloadScreeningResults
 *
 * Lets the user download the results of a query
 *
 */
function DownloadScreeningResults(){
  location.href+="&download=csv&filetype=csv";
}

/**
 * Function glossaryId
 * Displays a Glossary term from the id given
 *
 */
function glossaryId(id) {
  
  filename = "/af/glossary?glossaryId="+id;
 window.open(filename, "Glossary", "width=275,height=300,resizable=no,scrollbars=yes");
}

/**
 * Function replace
 * Find and replace some text in a string.
 *
 * text = the text to search for.
 * by = what to replace it with.
 */
function replace(string,text,by) {
    var strLength = string.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) return string;

    var i = string.indexOf(text);
    if ((!i) && (text != string.substring(0,txtLength))) return string;
    if (i == -1) return string;

    var newstr = string.substring(0,i) + by;

    if (i+txtLength < strLength)
        newstr += replace(string.substring(i+txtLength,strLength),text,by);

    return newstr;
}


/**
 * Function glossaryId
 * Displays a Glossary term from the id given
 *
 */
function openDocument( filename) {
  
  window.open( filename, 'pdf',"width=800,height=600,resizable=yes,scrollbars=yes");    
  
}
