/**
 * Updates the image iframe with the specified probeset value.
 *  
 * @param frameid			The id of the frame to update
 * @param org				The organism id
 * @param symbol			The gene symbol 
 * @param array			The array internal name
 * @param probeset		The probeset filter
 */
function selectProbeset(frameid, org, symbol, array, probeset) {
	//Get the DIV object with our IFRAME form values
	var divId = ""+org+"."+symbol+"."+array;
	var divObj = document.getElementById(divId);
	if(divObj != null) {
		//Get the probeset element within the div
		var probeSets = divObj.getElementsByTagName("PROBESET");
		if(probeSets != null && probeSets.length > 0) {
			probeSets[0].setAttribute("name", probeset.value);
		}
		
		//Call the iframe update javascript
		updateGeneIFrame(frameid, org, symbol, array);
	}
}

/**
 * This function is used to update an IFRAME when a probeset filter has 
 * changed. It sets the iframe source to the gene image loading form, which
 * reads parameters from the current page and builds hidden form fields, then
 * submits this to the actual image JSP.
 * 
 * @param frameid			The id of the frame to update
 * @param org				The organism id
 * @param symbol			The gene symbol 
 * @param array			The array internal name
 */
function updateGeneIFrame(frameid, org, symbol, array) {
	//Build the URL to send to the IFRAME form page
	var url = "GeneIFrameForm.jsp?organism="+org+"&symbol="+symbol+"&array="+array;
	//alert("updating iframe with url="+url);
	
	//Get the IFRAME element and set the URL
	var f = document.getElementById(frameid);
	if(f != null) {
		f.src=url;
	}
}

/**
 * This function is called from within the IFRAME form page. It is responsible
 * for adding parameters to the form based upon elements within the 
 * corrisponding DIV within the parent document. The DIV is located using
 * the organism id, gene symbol, and array internal name data provided to
 * the function. From here, the function is looking for gene information,
 * probeset filter data, and primers. This data is added to the form and
 * then submitted.
 *  
 * @param org				The organism id
 * @param symbol			The gene symbol 
 * @param array			The array internal name
 */
function buildGeneIFrameForm(org, symbol, array) {
	//Get the form within this document
	var form = document.getElementById("GeneIFrameImageForm");	
	if(form != null) {
		//Add hidden fields to the form, starting with the basics
		addInput(form, "organism", org);
		addInput(form, "symbol", symbol);
		addInput(form, "array", array);
		
		//Get the parent DIV for this organism, symbol, and array
		var divId = ""+org+"."+symbol+"."+array;
		var divObj = parent.document.getElementById(divId);
		if(divObj != null) {
			//Attempt to get parent page values
			var probeSets = divObj.getElementsByTagName("PROBESET");
			if(probeSets != null && probeSets.length > 0) {
				addInput(form, "probeset", probeSets[0].getAttribute("name"));
			}
					
			//Get the image footer text
			var footers = divObj.getElementsByTagName("IMAGEFOOTER");
			if(footers != null && footers.length > 0) {
				addInput(form, "imageFooter", footers[0].getAttribute("value"));
			}
			
			//Get display options
			var includeDegens = divObj.getElementsByTagName("INCLUDEDEGEN");
			if(includeDegens != null && includeDegens.length > 0) { 
				addInput(form, "includeDegen", includeDegens[0].getAttribute("value"));
			}
				
			var includeDomain = divObj.getElementsByTagName("INCLUDEDOMAIN");
			if(includeDomain != null && includeDomain.length > 0) { 
				addInput(form, "includeDomain", includeDomain[0].getAttribute("value"));
			}
				
			var includeNMD = divObj.getElementsByTagName("INCLUDENMD");
			if(includeNMD != null && includeNMD.length > 0) { 
				addInput(form, "includeNMD", includeNMD[0].getAttribute("value"));
			}
				
			//Get a list of primer nodes and add the inputs for each
			var primers = divObj.getElementsByTagName("PRIMER");
			if(primers != null) {
				for(var i = 0; i < primers.length; i++) {
					var pname = primers[i].getAttribute("name");
					addInput(form, "primername"+i, pname);
					var text = primers[i].getAttribute("text");
					addInput(form, "primertext"+i, text);
					var st = primers[i].getAttribute("start");
					addInput(form, "primerstart"+i, st);
					var sp = primers[i].getAttribute("stop");
					addInput(form, "primerstop"+i, sp);
					var mismatches = primers[i].getAttribute("mismatches");
					addInput(form, "primermismatches"+i, mismatches);
				}
			}
		}
	}
}

/**
 * The following section contains private functions that support the public
 * interface javascript.
 */
 
 /**
  * This function adds  a hidden field to the form object specified.
  * 
  * @param formObj		The form object to add to
  * @param fieldName		The name of the input 
  * @param fieldValue	The field value
  */
function addInput(formObj, fieldName, fieldValue) {
	//Get the document element and create a new child
	var e = document.createElement("INPUT");
	
	//Set the name and value, etc, then add to the form object provided
	e.setAttribute("type", "hidden");
	e.setAttribute("name", fieldName); 
	e.setAttribute("value", fieldValue);
	formObj.appendChild(e);
}