/*
	general definitions
*/

URL_BASE 	= new String;
URL_BASE 	= "/cadcbin/";
URL_HOST 	= new String;
URL_HOST 	= "";

windowName 	= new Array();
windowList 	= new Array();

/*
                CADC popup windows: help only for now
*/

function wCADC( name,url ) {		// root popup window
  var fullURL;
  /* Must determine the full URL */
  if (url.indexOf('://') >= 0) { fullURL = url }	
  else { if (url.substr(0,1) == '/') { fullURL = URL_HOST + url }
  else { fullURL = URL_BASE + url }}
  doPopup(name, fullURL);
}

function doPopup( name,url ) {		// root popup window
  var n = windowName.length ; var wn = name;
  var tmp, w, i, x, y;
  x = 640; y = 320;			// Default size
  i = name.indexOf('_');
  if (i >= 0) { 		// e.g. Window_500x500
      wn = name.substr(0, i);
      tmp = name.substr(i+1);
      i = tmp.indexOf('x') ;
      if (i >= 0) { x =  Number(tmp.substr(0, i)); y = Number(tmp.substr(i+1)) }
      else { y = Number(tmp) }
  }
  if (wn == "") { wn = "W" + windowName.length; i = n }
  else for (i=0; i<n; i++) {
      if (windowName[i] != wn) continue ;
      w = windowList[i];
      if (w.closed) w = 0;
      break ;
  }
  if (w) {	// Window already exists
    if ((y > 100) && (y <= 640)) w.innerHeight = y ;
    if ((x > 100) && (x <= 640)) w.innerWidth  = x ;
    w = window.open(url, wn) ;
  }
  else { 	// New window -- create it
    tmp= 'scrollbars=yes,resizable=yes,toolbar=0,location=0,status=0,menubar=0';
    tmp = tmp + ',height=' + y ;
    tmp = tmp + ',width=' + x ;
    w = window.open(url, wn, tmp) ;
    windowName[i] = wn; windowList[i] = w ;
  }
  w.focus();
}


/*
  Generic CADC archiveHelp popup
  
  a 	= archive name (e.g. FUSE, HST, GEMINI)
  d	= data descriptor
  l	= language (EN)
*/
 
function cadcHelp( a, d, l ) {
  if ( l == "" ) { l = "EN" }
//  wCADC('wHelp_700x320','cadc/cadcHelp.pl?archive='+a+'&attribute='+d+'&language='+l);
  doPopup('wHelp_700x320','/jcmt/helplabel?archive='+a+'&attribute='+d+'&lang='+l);  
}


/*
  Original gsaHelp window
*/

function gsaHelp(q) {
  var w = wCADC('wHelp_700x320', 'gsaHelp.pl?'+q) ;
}


/*
  Open a window to display GSA weather images...
*/

function gsaEnv(q) {
  var w = wCADC('gsaEnv_800x640', 'wdbi.cgi/gsa/gsa_environment/more?data_superset_id='+q) ;
}

function check(field)
{
  for (i = 0; i < field.length; i++)
  {
    var currField = field[i];

    if (currField.type == "checkbox")
    {
      currField.checked = true;
    }
  }
}


// Reset a form's fields, including putting the checkboxes back to the defaults.
function resetForm(form)
{
  // To set the checkboxes back...
  form.reset();

  // To clear the text fields.
  clearFormFields(form);
}

// Clear all the fields from the first form.  This will almost always apply to
// those pages that have a single form.
function clearFormFields(form)
{
  clearForm(form.id);
}

// Clear all the elements from the form field.  Special cases, such as the
// Search Box element are handled here too.  This will apply to the form in the
// document whose ID matches the given one.
function clearForm(formIdent)
{
  var form, elements, i, elm;
  form = document.getElementById
    ? document.getElementById(formIdent)
    : document.forms[formIdent];

  if (document.getElementsByTagName)
  {
    elements = form.getElementsByTagName('input');
    for( i=0, elm; elm=elements.item(i++); )
    {
      if (elm.type == "text")
      {
        if (elm.name == "searchBox")
        {
          elm.value = '00:10:00';
        }
        else
        {
          elm.value ='';
        }
      }
    }

    elements = form.getElementsByTagName('select');
    for( i=0, elm; elm=elements.item(i++); )
    {
      if (elm.name != "orderBy")
      {
        elm.options[0].selected = true;
      }
    }
  }

  // Actually looking through more elements here
  // but the result is the same.
  else
  {
    elements = form.elements;
    for( i=0, elm; elm=elements[i++]; )
    {
      if (elm.type == "text")
      {
        if (elm.name == "searchBox")
        {
          elm.value = '00:10:00';
        }
        else
        {
          elm.value ='';
        }
      }
      else if (elm.type == "select")
      {
        if (elm.name != "orderBy")
        {
          elm.options[0].selected = true;
        }
      }
    }
  }
}

function allUnchecked(currForm, field) {
//  if( document.forms[0].uri.length == null )
//  {
//    document.forms[0].uri.checked = false;
//  }
    
  if (currForm.elements[field].length == null)
  {
    currForm.elements[field].checked = false;
  }

  for(var i=0; i < currForm.elements[field].length; i++)
  {
    currForm.elements[field][i].checked = false;
  }
}


function allChecked(currForm, field)
{
  if (currForm.elements[field].length == null)
  {
    currForm.elements[field].checked = true;
  }

  for(var i=0; i < currForm.elements[field].length; i++)
  {
    currForm.elements[field][i].checked = true;
  }
}

function Preview(label) {
popup = window.open(label,"PreviewWindow",toolbar=no,
location=no,status=yes,scrollbars=yes,resizable=yes)
popup.document.close()
print
}
function Other(label) {
popup = window.open(label,"OtherWindow",toolbar=no,
location=no,status=yes,scrollbars=yes,resizable=yes)
popup.document.close()
}
function OtherEncode(label) {
ValueString = escape(label)
popup = window.open(ValueString,"OtherWindow",toolbar=no,
location=no,status=yes,scrollbars=yes,resizable=yes)
popup.document.close()
}
function saveSelected()
{
for(var i=0; i < document.forms[0].uri.length; i++)
{
if(document.forms[0].uri[i].checked)
{
document.forms[0].selected[i].value="Y";
}
else
{
document.forms[0].selected[i].value="N";
}
}
}
function openNewWindow(form) {
var name = 'jcmt_search_download_popup';
win=window.open('',name,'resizable=1,scrollbars=1,width=630,height=770');
form.target=name;
return true;
}

