function dynamicContent(elementid, content) {
	// Add dynamic content to an element.
	// From http://www.javascriptkit.com/javatutors/dynamiccontent4.shtml
	// (slightly modified).
	// dsforna Oct 2009 Added this function.
	var el = null;
	if (document.all) {
		return;
	}
	if (document.getElementById) {
		el = document.getElementById(elementid);
	} else {
		return;
	}
	if (el == null) {
		return;
	}
	rng = document.createRange();
	rng.setStartBefore(el);
	htmlFrag = rng.createContextualFragment(content);
	while (el.hasChildNodes()) {
		el.removeChild(el.lastChild);
	}
	el.appendChild(htmlFrag);
}

//-----------------------------------------------------------------------------

function tdLink(widthPercent, href, txt) {
	// Creates a TD element with one standard Querator link.
	// INPUT:
	// - width_percent: value for the width attribute of the TD element.
	// - href: href of the link.
	// - txt: text of the link.
	// RETURN:
	// - the generated HTML code as string.
	// dsforna Oct 2009 Added this function.
	tdtxt = "<td width='" + widthPercent + "' align='center' bgcolor='#006699'>";
	tdtxt += "<a href='" + href + "'><font color='white' size='1'>";
	tdtxt += "<strong>" + txt + "</strong></font></a></td>";
	return tdtxt;
}

//-----------------------------------------------------------------------------

function writeLinks(linksId, hrefArray, txtArray, mailSubject) {
	// Dynamically writes a row of link in one of the current document's node.
	// INPUT:
	// - linksId: id of the node where to write. 
	// - hrefArray: array of hrefs (one for each link) 
	// - txtArray: array of textes (one for each link)
	// - mailSubject: for the "send comment" mail. Set to null if the send 
	//   comment is to be hidden. 
	// if the lengths of href and txt arrays differ, the missing href are 
	// replace by "#".
	// dsforna Oct 2009 Added this function.
	if (txtArray == null) {
		return;
	}
	if (txtArray.length == 0) {
		return;
	}

	if (hrefArray == null) {
		hrefArray = new Array();
	}
	var width = parseInt(100 / txtArray.length);
	var widthPercent = "" + width + "%";
	txtlink = "<table border='0' cellpadding='2' cellspacing='5'><tr>";
	for ( var i = 0; i < txtArray.length; i++) {
		var txt = txtArray[i];
		var href = "#";
		if (i < hrefArray.length) {
			href = hrefArray[i];
		}
		txtlink += tdLink(widthPercent, href, txt);
	}
	txtlink += "</tr>"
	if (mailSubject != null) {
		// Send comment mail:
		txtlink +="<tr><td colspan='"+txtArray.length+"' align='center'>";
		txtlink +="<a href='mailto:hst-archive@eso.org?subject="+mailSubject+"'>";
		txtlink +="<font size='1'><strong>Send comments to hst-archive@eso.org";
		txtlink +="</strong></font></a></td></tr>";
	}
	txtlink +="</table>";
	dynamicContent(linksId, txtlink);
}

//-----------------------------------------------------------------------------

function writeFooterLinks(linksId, mailSubject) {
    // Just a wrapper around writeLinks for the most common row of links.
    // dsforna Oct 2009 added this function. 
  var hrefArray = new Array("http://archive.eso.org/", 
		  "http://www.eso.org/", 
		  "http://www.stecf.org/", 
		  "/search/");
  var txtArray =  new Array("Archive Facility HOME", 
		  "ESO HOME", 
		  "ST-ECF HOME", 
		  "Search");
  writeLinks(linksId, hrefArray, txtArray, mailSubject);

}

// -----------------------------------------------------------------------------

function OpenAscii(txt) {
	// Input text in a textarea.
	var newWin;
	newWin = window.open("", "AsciiWin",
			"scrollbars,resizable,menubar,status,heigth=950,width=1000");
	newWin.document.writeln("<html><head><title>Querator Ascii Output");
	newWin.document.write("</title></head>");
	newWin.document.write("<body><form action=''>");
	newWin.document.write("<input type='button' value='select all' ");
	newWin.document
			.write("onClick='javascript:this.form.data.focus();this.form.data.select();'>");
	newWin.document.write("<br>");
	newWin.document.write("<textarea readonly name='data' rows=30 cols=80 >");
	newWin.document.write(unescape(txt));
	newWin.document.write("</textarea><br>");
	newWin.document.write("</form></body></html>");
	newWin.document.close();
}

// ------------------------------------------------------------------------------

function ViewAscii(txt) {
	// Input text as document body.
	var newWin;
	newWin = window.open("", "AsciiWin",
			"scrollbars,resizable,menubar,status,heigth=950,width=1000");
	newWin.document.writeln("<html><head><title>Querator Ascii Output");
	newWin.document.write("</title></head>");
	newWin.document.write("<body>");
	newWin.document.write(unescape(txt));
	newWin.document.write("</body></html>");
	newWin.document.close();
}

// ------------------------------------------------------------------------------

function allChecked(f) {
	for ( var i = 0; i < f.length; i++) {
		f.elements[i].checked = true;
	}
	return;
}

// ------------------------------------------------------------------------------

function allUnChecked(f) {
	for ( var i = 0; i < f.length; i++) {
		f.elements[i].checked = false;
	}
	return;
}

// ------------------------------------------------------------------------------

function goBack() {
	history.back();
	return false;
}

// ------------------------------------------------------------------------------

function globalAllChecked() {
	// Checks all the checkbox in all the forms of this documents.
	// dsforna Oct 2009 - added this fuction.

	doc = window.document;
	for ( var i = 0; i < doc.forms.length; i++) {
		f = doc.forms[i];
		allChecked(f);
	}
}

// ------------------------------------------------------------------------------

function globalAllUnChecked() {
	// Unchecks all the checkbox in all the forms of this documents.
	// dsforna Oct 2009 - added this fuction.

	doc = window.document;
	for ( var i = 0; i < doc.forms.length; i++) {
		f = doc.forms[i];
		allUnChecked(f);
	}
}

// ------------------------------------------------------------------------------

function removeHidden(f) {
	// Removes the hidden inputs for datasets which can be left over from a
	// browser
	// back button. This must be done in two steps (first collect the list of
	// the
	// children to remove, and then actually remove them) because it seems that
	// removing a child invalidates the children collection.
	// INPUT:
	// - f: form element from which the hidden inputs are to be removed.
	// dsforna Oct 2009 - added this fuction.

	toremove = new Array();
	for ( var i = 0; i < f.elements.length; i++) {
		if ((f.elements[i].name == "dataset")
				&& (f.elements[i].type == "hidden")) {
			toremove.push(f.elements[i]);
		}
	}
	for ( var i = 0; i < toremove.length; i++) {
		for ( var j = 0; j < f.elements.length; j++) {
			if (f.elements[j] == toremove[i]) {
				f.removeChild(f.elements[j]);
				break;
			}
		}
	}
}

// ------------------------------------------------------------------------------

function all2NGRH(f, link) {
	// Submits to the NGRH all the marked datasets (both HST and ESO).
	// Scan all the forms in the document looking for checked checkboxes
	// named "dataset" and add a hidden input to this form for each
	// found checkbox with the checkbox name and value.
	// INPUT:
	// - f: HTML form where the submit button of this action lives.
	// RETURN:
	// - true (i.e. perfom submission) if there is at least a dataset selected.
	// false otherwise.
	// dsforna Oct 2009 - added this fuction.

	// There are multiple submit buttons in this form which dynamically change
	// the action and method, so it is better to reset them to the default here
	// because it can have been changed (it happens if a user clicks on another
	// submit and then hit the back button of the browser).
	f.action = link;
	f.method = 'POST';
	doc = window.document;

	// Remove the (potentially) left over hidden inputs:
	removeHidden(f);

	// Now add to f all the currently marked datasets. It is assumend that
	// each dataset is unique in the document, no check is done here.
	var orilen = f.elements.length; // the non-dataset elelements.
	for ( var i = 0; i < doc.forms.length; i++) {
		cf = doc.forms[i];
		for ( var ei = 0; ei < cf.length; ei++) {
			if ((cf.elements[ei].name == "dataset")
					&& (cf.elements[ei].checked == true)) {
				var hinput = document.createElement('input');
				hinput.type = "hidden";
				hinput.name = "dataset";
				hinput.value = cf.elements[ei].value;
				f.appendChild(hinput);
			}
		}
	}
	if (f.elements.length == orilen) {
		alert("No dataset is selected: Archive Request will not be submitted.");
		return false;
	} else {
		return true;
	}
}

// ------------------------------------------------------------------------------

function setAction(f, link) {
	// Changes the action of this form as: GET to link.
	// It is used to have submit buttons behaving as links.
	// dsforna Oct 2009 - added this function.

	f.action = link;
	f.method = 'GET'; // (the eso main query form does not allow post)
	// Remove the (potentially) left over hidden inputs:
	removeHidden(f);
	return true;
}

