/* javascript version of the multi-species redirector */
function changeDatabase( obj ) {
    window.location = obj.options[obj.selectedIndex].value;}

/* Used by sequence logo thumbnails */
function Start(page) {
       OpenWin = this.open(page, "_blank", "height=317, width=660, toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes");
}

/* Used by iupac help */
function StartHelp(page) {
       OpenWin = this.open(page, "_blank", "height=450, width=300, toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes");
}

/* for the red_view location_form dropdown elements*/
var location_form_dropdowns = [
	["all chromosomes",["as a list of search regions (sorted alphabetically)","as a genome browser image of search region locations"]],
	["otherwise",["whole chromosome","chromosome sub-region"]]
	];


function populateChromosomesListBox(thebox,chromosomeNames) {
	var optn = document.createElement("OPTION")
	optn.text = "all chromosomes"
	optn.value = "all chromosomes"
	thebox.options.add(optn);
	
	for (var i=0; i < chromosomeNames.length;i++)
	{
		optn = document.createElement("OPTION")
		optn.text = ('chromosome '+chromosomeNames[i]).replace('chromosome CHROMOSOME_','CHROMOSOME_');
		optn.value = ('chromosome '+chromosomeNames[i]).replace('chromosome CHROMOSOME_','CHROMOSOME_');
		thebox.options.add(optn);
	}
}

function checkToEnableSubregionFields(selectionBox,elementsToEnable,enableIfWeSeeThisString) {
	if (selectionBox.value == enableIfWeSeeThisString)
	{
		for (var i=0; i < elementsToEnable.length; i++)
		{
			setDisabledStatus(elementsToEnable[i],'enabled')
		}
	}
	else
	{
		for (var i=0; i < elementsToEnable.length; i++)
		{
			setDisabledStatus(elementsToEnable[i],'disabled')
		}
	}
}

function setDisabledStatus(id,target)
{
    // client-side script that toggles the specified
    // input control's disabled status
    var elem = document.getElementById(id);
    if (elem != null)
    {
        // toggle the disabled status
        if (elem.onfocus != null && target=='enabled')
        {
            // there is a client-side focus event handler
            // assumption: this element is disabled, so enable it!                    
            elem.onfocus = null;
            elem.style.backgroundColor = 'white';
        }
        else
        {
        	if (elem.onfocus == null && target=='disabled')
        	{
	            // assumption: this element is enabled, so disable it!
	            elem.onfocus = function preventFocus(e) { this.blur(); };
	            elem.style.backgroundColor = '#CCCCCC';
            }
        }
    }
}

function updateDropdownWithDefault(box1,box2,optionArray, elementsToEnable,enableIfWeSeeThisString) {
	//clear the current values
	for (var i=box2.options.length-1;i>=0;i--)
	{
		box2.remove(i);
	}
	//find the subarray in optionArray that matches box1.value
	for (var i = 0; i < optionArray.length; i++)
	{
		if (optionArray[i][0] == box1.value)
		{
			//load the contents of the nested array
			for (var j=0; j < optionArray[i][1].length; j++)
			{
				var optn = document.createElement("OPTION")
				optn.text = optionArray[i][1][j];
				optn.value = optionArray[i][1][j];
				box2.options.add(optn);
			}
			break
		}
		else
		{
			if (i == optionArray.length-1) //this is the last pass, so use the default
			{			
				//load the contents of the nested array
				for (var j=0; j < optionArray[i][1].length; j++)
				{
					var optn = document.createElement("OPTION")
					optn.text = optionArray[i][1][j];
					optn.value = optionArray[i][1][j];
					box2.options.add(optn);
				}
			}
		}
	}
	
	for (var i=0;i < elementsToEnable.length;i++)
	{
		if (box2.value == enableIfWeSeeThisString)
		{
			setDisabledStatus(elementsToEnable[i],'enabled')
		}
		else
		{
			setDisabledStatus(elementsToEnable[i],'disabled')
		}
	}
}


/* cookie is used to store p-value threshold */

function setCookie(name, value, path, domain, secure) {
    expires = new Date();
    expires.setFullYear(expires.getFullYear()+20);
    var curCookie = name + "=" + escape(value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
    document.cookie = curCookie;
    window.location.reload();
    return false;
}


/* Java web start for launching Sockeye application */  
var javawsInstalled = 0; 
isIE = "false"; 
if (navigator.mimeTypes && navigator.mimeTypes.length) { 
  x = navigator.mimeTypes['application/x-java-jnlp-file']; 
  if (x) { 
    javawsInstalled = 1; 
  } 
} 
else { 
  isIE = "true"; 
}

var webstartURL = "http://coast.bcgsc.bc.ca:8080/webstart/sockeyelauncher?";

function launchSockeye(database, chromosome, start, end, connection){
  var targetURL = webstartURL + "database=" + database + "&chromosome=" + chromosome + "&start=" + start + "&end=" + end + "&conn=" + connection;

if (javawsInstalled == 1) {
    location.href=targetURL;
}  
else if (navigator.userAgent.indexOf("Gecko") !=-1) {
    var message = (" <br>If you have Java Runtime Environment installed, ");
    message += ("click <a href='" + targetURL + "'>here</a> to launch Sockeye");
    message += ("<br>or<br>");
    message += ("Click ");
    message += ("<a href='http://java.sun.com/j2se/1.4.2/download.html' target='_new'>here</a> to download Java Runtime Environment 1.4.2<br>");
    var win = window.open("","LaunchSockeye","toolbar=no,scrollbars=yes,width=300,height=300");
    win.document.open();
    win.document.write(message);
    win.document.close();

}
else {
    var message = ("<br><B>Steps for installing Java JRE 1.4.2</B><br>");
    message += ("To launch Sockeye, please follow steps below to install Java Runtime Environment 1.4.2");
    message += ("\n<BR><BR>\n");
    message += ("(For <B>Mac Os X</B> Users, please refer to OS X Java Web Start Installtion section ");
    message += ("<a href='http://coast.bcgsc.bc.ca:8080/sockeyehelp/1.2/welcome/webstart_config.html' target='_new'>here</a>.)");
    message += ("\n<BR>\n");
    message += ("<UL><LI><b>Step One: </b>Click ");
    message += ("<a href='http://java.sun.com/j2se/1.4.2/download.html' target='_new'>here</a> ");
    message += ("to Java JRE download page.");
    message += ("</LI></UL>");
    message += ("<UL><LI><B>Step Two: </b>On the Download Java 2 Platform page, Click ");
    message += ("<i>Download J2SE JRE</I> link");
    message += ("<br><br><img src='http://coast.bcgsc.ca:8080/webstart/img/javascreenshot.jpg'>");
    message += ("</LI></UL>");
    message += ("<UL><LI><B>Step Three: </b>Click <I>Accept</I> and <I>Continue</I> to pass Term of Use page.");
    message += ("</LI></UL>");
    message += ("<UL><LI><B>Step Four: </B>Click and save installer file depending on the operating system you are using.");
    message += ("<BR><I>Note: For Windows users, click on either Windows Offline Installation or Windows Installation.</I>");
    message += ("</LI></UL>");
    message += ("<UL><LI><B>Step Five: </B>After download completed, run the installer and follow the direction");
    message += (" to complete installation.  You should be able to launch Sockeye after successful JRE installtion.");
    message += ("</LI></UL>");
    message += ("<UL><LI>If you encounter any difficulties installing JRE, please refer to <A href='http://coast.bcgsc.bc.ca:8080/s\
ockeyehelp/1.2/welcome/webstart_config.html' target='_new'>Java Web Start Troubleshooting</A> page.");
    message += ("</LI></UL>");
    var win = window.open("","LaunchSockeye","toolbar=no,scrollbars=yes,width=800,height=600");
    win.document.open();
    win.document.write(message);
    win.document.close();
}
}
