/**
 *	Notifies the user that this site works best with JavaScript enabled.
 *	If JavaScript is enabled on the client browser, then this message will
 *	be removed, and the user won't see it. If JavaScript is not enabled, then
 *	this function will not be executed.
 */
function disableJsMessage(){
	var javascriptMessage = document.getElementById("jsmessage");
	if (javascriptMessage != null){
		var parent = javascriptMessage.parentNode;
		parent.removeChild(javascriptMessage);
	}
}

/**
 *	Adds a message about blast results in window named "BlastResult".
 *	(Called from validateBlast() below)
 */
function generateBlastWindow(){
	var blastWindow = window.open("","BlastResult","height=600,width=900,scrollbars=yes,resizable=yes");
	blastWindow.document.write("<html><head><title>HGSC at Baylor College of Medicine</title>");
	blastWindow.document.write("<link type='text/css' title='Style' rel='stylesheet' href='/stylesheets/hgsc.css'/>");
	blastWindow.document.write("</head><body>");
	blastWindow.document.write("<div class='pageTitle' id='titlePanel4' style='top:50px;'>HGSC BLAST Results</div>");
	blastWindow.document.write("<div class='doubleColumnCentered' id='blastresults'><p>The result of your BLAST query will appear in this window.</p>");
	blastWindow.document.write("</body></html>");
	blastWindow.document.close();
	blastWindow.focus();
}

/**
 *	Validate the input fields for blast
 *	ALGORITHM:
 *	 if the field does not have a correct entry then display an alert
 *	 else
 *	   call a function to open a new window for the blast results 
 */
 
//Signals whether the user has confirmed the query type by clicking the radio buttons.
var validated = "false";

function validateBlast(thisform){
	with (thisform){
		var jsenabled = document.getElementById("jsenabled");
		jsenabled.value = "Y";//tell BlastOutputGenerator not to re-validate
		var ds = document.getElementById("dataset");
		if (ds.options[ds.selectedIndex].value == "default"){
			alert("Please select a dataset.");
			return false;
		}
		var query = sequence.value; //get the user's query from the textarea
	    query = trim(query);
		if (query.length > 50000){
			alert("The sequence can have only 50,000 characters.");
			return false;
		}
		if ((query.length == 0) && ((upfile.value == ""))){
			alert("Please enter a query or select a file to upload.");
			return false;
		}
		if (isIllegalQuery(query)) {
			return false;
		}
		else {
			if (validated == "true"){//user set the querytype
				generateBlastWindow();
				validated = "false"; //toggle for next submission
				return true;
			}
			else{
				var isKnownQueryType = setQueryType(query);
				if (isKnownQueryType){;
					generateBlastWindow();
					return true;
				}
				else{
					return false;
				}
			}
		}
	}
}
// Removes leading whitespaces
function LTrim( value ) {
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
}
// Removes ending whitespaces
function RTrim( value ) {
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
}
// Removes leading and ending whitespaces
function trim( value ) {
	return LTrim(RTrim(value));
}
//Look for chars that are not allowed in a query
function isIllegalQuery(query) {
	query = removeDefinitionLine(query);
	//Remove whitespace so that reIllegalChars will work
    query = query.replace(/[\s+]/g,"");
	var reIllegalChars = new RegExp("([^*abcdefghiklmnpqrstuvwxyz-])+","gim");
	var matches = reIllegalChars.exec(query);
	if ((matches != null)) {
		alert("The character '" + matches[1] + "' is not allowed in the query.");
		return true;
	}
	return false;
}
function removeDefinitionLine(query) {
	//query should already be trimmed
	if (query.charAt(0) == '>') {
		var index = query.indexOf("\n");
		query = query.substring(index + 1);
	}
	return query;
}
/**
 * Examines chars that are in the query, shows confirmation question when needed,
 * and sets value of the "querytype" radio button.
 * Returns true if the query type is known, either by examination or user confirmation.
 * Returns false if the query type must be confirmed by the user.
 */
function setQueryType(query) {
	var confirmationContainer = document.getElementById("confirmationContainer");
	//var re = /[efilpqxz*]/gi; //Does it have chars that are allowed only in protein queries?
	var re = new RegExp("[efilpqxz*]","gim");
	var matchProt = re.exec(query);
	if (query.match(re)) {
		//alert("protein");
		var proteinRadio = document.getElementById("proteinRadio");
		proteinRadio.click();
		return true;
	}
	else {
		var reNucWildCards = new RegExp("[bdhkmrsvwy]","gim");//Does it have nuc wildcard chars?
		var matches = reNucWildCards.exec(query);
		var nucleotideRadio = document.getElementById("nucleotideRadio");
		nucleotideRadio.click();//this is probably a nucleotide query
		if (matches == null) { //No nuc wildcards.
			//alert("nucleotide");
			return true;
		}
		else {
			//Possible nucleotide query that has wildcard chars.
			//alert("Possible nucleotide");
			if (validated == "false"){
				//Ask the user to confirm by selecting the "querytype" radio buttons.
				showNucleotideQueryConfirmation();
				validated = "true";
				return false;
			}
			else {
				return true;
			}
		}
	}
}
//Displays a question that asks the user to confirm the type of query
function showNucleotideQueryConfirmation(){
	var confirmationContainer = document.getElementById("confirmationContainer");
	var submitContainer = document.getElementById("submitContainer");
	submitContainer.className = "hide";
	confirmationContainer.className = "showAlert";
}
//Hides the question that asks the user to confirm the type of query
//and sets validated to "true"
function hideNucleotideQueryConfirmation(){
	var confirmationContainer = document.getElementById("confirmationContainer");
	var submitContainer = document.getElementById("submitContainer");
	submitContainer.className = "show";
	confirmationContainer.className = "hide";
	validated = "true";
}
