
function resetclicked()
{
	var url = self.location.href;
	self.location.href = url;
}

function setEltDisplay(id, disp) {
	if (!document.getElementById) return false;
	var elt = document.getElementById(id);
	if (elt) elt.style.display = disp;
	return false;
}

function showElt(id) {
	setEltDisplay(id, "");
	return false;
}

function hideElt(id) {
	setEltDisplay(id, "none");
	return false;
}

function setBasic(thisForm) {
	thisForm.MODE.value = "basic";
	thisForm.FUN[1].disabled = false;
	thisForm.FUN[2].disabled = false;

	// set all advanced inputs to default. 
	thisForm.POPULATION_SIZE.value = 200;
	thisForm.CYCLE_TIME.value = 10;
	thisForm.STALL_FACTOR.value = 10;
	thisForm.INTERNAL_CROSSOVER_PROBABILITY.value = 0.5;
	thisForm.ITERATION.value = 0;
	thisForm.RANDOM_SEED.value = 0;
	thisForm.MAX_HOUR.disabled = false;
	thisForm.MAX_MIN.disabled = false;
	thisForm.MAX_HOUR.value = 5;
	thisForm.MAX_MIN.value = 0;
	thisForm.RERUN_OFF.checked = false;
}

function setAdvanced(thisForm) {
	thisForm.MODE.value = "advanced";
	thisForm.FUN[0].disabled = false;
	thisForm.FUN[0].checked = true;
	checkRadio(thisForm);
	thisForm.FUN[1].disabled = true;
	thisForm.FUN[2].disabled = true;
	thisForm.MAX_HOUR.value = 5;
	thisForm.MAX_MIN.value = 0;
}

function checkSampleInput(thisForm) {
	if ( thisForm.SAMPLE.checked ) {
		thisForm.SEQUENCE.value = thisForm.hid_SEQUENCE.value;
	} else  {
		thisForm.SEQUENCE.value = "";
	}
}

function checkRadio(thisForm)
{
	if (thisForm.FUN[0].checked) 	// create cocktail 
	{ 
		// alert ("cocktail"); 
		thisForm.RARE_THRESHOLD.disabled = false;
		thisForm.MAX_HOUR.disabled = false;
		thisForm.MAX_MIN.disabled = false;
		thisForm.FIXEDSEQ.disabled = false;
		thisForm.FIXEDUPLOAD.disabled = false;
		thisForm.JOB.value = 'COCKTAIL';
	}
	else	// pick best natural sequence or see the coverage distribution of natural seq
	{
		//thisForm.PICK_BEST.value = "YES";
		thisForm.RARE_THRESHOLD.disabled = true;
		thisForm.MAX_HOUR.disabled = true;
		thisForm.MAX_MIN.disabled = true;
		thisForm.FIXEDSEQ.disabled = true;
		thisForm.FIXEDUPLOAD.disabled = true;
		if (thisForm.FUN[1].checked) 	// 
		{
	 		//alert ("pick natural"); 
			thisForm.JOB.value = 'PICKBEST';
		}
		else
		{
	 		//alert ("see coverage "); 
			thisForm.JOB.value = 'SEECOVERAGE';
		}
	}
}

function checkReRun(thisForm)
{
	if ( thisForm.RERUN_OFF.checked ) 
	{
		thisForm.MAX_HOUR.disabled = true;
		thisForm.MAX_MIN.disabled = true;
	} 
	else {
		thisForm.MAX_HOUR.disabled = false;
		thisForm.MAX_MIN.disabled = false;
	}

}

function checkNum(obj, minVal, maxVal, decimal)
{
	//alert("object= " + obj.name);
	var checkStr = obj.value;
	var allValid = true;
	var validGroups = true;
	var checkOK;

	if ( decimal == "Y" )
	{
		checkOK = "0123456789."; 
	}
	else {
		checkOK = "0123456789"; 
	}

	if ( checkStr == "" )
	{
		//if ( obj.name == "MAX_HOUR" ||  obj.name == "MAX_MIN" ) {
		//	return (true);
		//}
		alert("Please Enter a(an) " + obj.name);
		return (false);
	}

	for (i = 0;  i < checkStr.length;  i++)
	{
		ch = checkStr.charAt(i);
		for (j = 0;  j < checkOK.length;  j++)
			if (ch == checkOK.charAt(j))
				break;
		if (j == checkOK.length)
		{
			allValid = false;
			break;
		}
	}
	
	if (!allValid)
	{
		if (minVal == "NA" || maxVal == "NA" ) 
		{
			alert("\"" + ch + "\" is not a valid " + obj.name + ".\nPlease enter a number.");
		} 
		else 
		{
			alert("\"" + ch + "\" is not a valid " + obj.name + ".\nPlease enter a number between " + minVal + " and " + maxVal + ".");
		}
		obj.select();
		obj.focus();
		return (false);
	}

	if (minVal != "NA" && maxVal != "NA" ) 
	{
		var prsVal = parseInt(checkStr);

		if (( prsVal > maxVal || prsVal < minVal ))
		{
			alert("Please enter a(an) " + obj.name + " between " + minVal + " to " + maxVal + "." );
			return (false);
		}
    }

	return (true);
}

function emailCheck (emailStr) 
{
	//alert("string=" + emailStr);
	var emailPat=/^(.+)@(.+)$/
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
	var validChars="\[^\\s" + specialChars + "\]"
	var quotedUser="(\"[^\"]*\")"
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	var atom=validChars + '+'
	var word="(" + atom + "|" + quotedUser + ")"
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")


	var matchArray=emailStr.match(emailPat)
	if (matchArray==null) {
		alert("Email address seems incorrect (check @ and .'s)")
		return false
	}

	var user=matchArray[1]
	var domain=matchArray[2]

	// See if "user" is valid 
	if (user.match(userPat)==null) {
		// user is not valid
		alert("The username doesn't seem to be valid.")
		return false
	}

	/* if the e-mail address is at an IP address (as opposed to a symbolic
	host name) make sure the IP address is valid. */
	var IPArray=domain.match(ipDomainPat)
	if (IPArray!=null) {
		// this is an IP address
		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
				alert("Destination IP address is invalid!")
				return false
			}
		}
  		return true
	}

	// Domain is symbolic name
	var domainArray=domain.match(domainPat)
	if (domainArray==null) {
		alert("The domain name doesn't seem to be valid.")
		return false
	}

	/* domain name seems valid, but now make sure that it ends in a
	   three-letter word (like com, edu, gov) or a two-letter word,
	   representing country (uk, nl), and that there's a hostname preceding 
	   the domain or country. */

	/* Now we need to break up the domain to get a count of how many atoms
	   it consists of. */
	var atomPat=new RegExp(atom,"g")
	var domArr=domain.match(atomPat)
	var len=domArr.length
	if (domArr[domArr.length-1].length<2 || domArr[domArr.length-1].length>3) {
	   // the address must end in a two letter or three letter word.
		alert("The address must end in a three-letter domain, or two letter country.")
		return false
	}

	// Make sure there's a host name preceding the domain.
	if (len<2) {
		var errStr="This address is missing a hostname!"
		alert(errStr)
		return false
	}

	// If we've gotten this far, everything's valid!
	return true;
}


function Form_Validator(theForm)
{
//alert ("random:"+theForm.RANDOM_SEED.value); 
	if ( theForm.SEQUENCE.value == "" && theForm.UPLOAD.value == "" ) {
		alert("Paste set of protein sequences or Uploade a file");
		theForm.SEQUENCE.select();
		theForm.SEQUENCE.focus();
		return (false);
	}
	else if (checkNum(theForm.COCKTAIL_SIZE, 1, 10, "N" ) == false )
	{
		theForm.COCKTAIL_SIZE.select();
		theForm.COCKTAIL_SIZE.focus();
		return (false);
	}
	else if (checkNum(theForm.EPITOPE_LENGTH, 8, 12, "N" ) == false )
	{
		theForm.EPITOPE_LENGTH.select();
		theForm.EPITOPE_LENGTH.focus();
		return (false);
	}

	//if  ( theForm.PICK_BEST.value == "NO" ) 	// run seq-optimize-cocktail
	if (theForm.FUN[0].checked) 	// create cocktail 
	{
		if (checkNum(theForm.RARE_THRESHOLD, 1, 10, "N" ) == false )
		{
			theForm.RARE_THRESHOLD.select();
			theForm.RARE_THRESHOLD.focus();
			return (false);
		}

		if ( theForm.RERUN_OFF.checked == false ) 
		{
			if (checkNum(theForm.MAX_HOUR, 0, 100, "N" ) == false )
			{
				theForm.MAX_HOUR.select();
				theForm.MAX_HOUR.focus();
				return (false);
			}
			else if (checkNum(theForm.MAX_MIN, 0, 60, "N" ) == false )
			{
				theForm.MAX_HOUR.select();
				theForm.MAX_HOUR.focus();
				return (false);
			}
		}	
	}
	if ( theForm.MODE.value == "advanced" )	
	{
		if (checkNum(theForm.POPULATION_SIZE, 1, 800, "N" ) == false )
		{
			theForm.POPULATION_SIZE.select();
			theForm.POPULATION_SIZE.focus();
			return (false);
		}
		else if (checkNum(theForm.CYCLE_TIME, 1, 100, "N" ) == false )
		{
			theForm.CYCLE_TIME.select();
			theForm.CYCLE_TIME.focus();
			return (false);
		}
		else if (checkNum(theForm.STALL_FACTOR, "NA", "NA", "N" ) == false )
		{
			theForm.STALL_FACTOR.select();
			theForm.STALL_FACTOR.focus();
			return (false);
		}
		else if (checkNum(theForm.INTERNAL_CROSSOVER_PROBABILITY, 0, 1, "Y" ) == false )
		{
			theForm.INTERNAL_CROSSOVER_PROBABILITY.select();
			theForm.INTERNAL_CROSSOVER_PROBABILITY.focus();
			return (false);
		}
		else if (checkNum(theForm.ITERATION, "NA", "NA", "N" ) == false )
		{
			theForm.ITERATION.select();
			theForm.ITERATION.focus();
			return (false);
		}
		else if (checkNum(theForm.RANDOM_SEED, "NA", "NA", "N" ) == false )
		{
			theForm.RANDOM_SEED.select();
			theForm.RANDOM_SEED.focus();
			return (false);
		}
		
	}
    //if (emailCheck(theForm.EMAIL.value) == false )
	//{
	//	theForm.EMAIL.select();
	//	theForm.EMAIL.focus();
	//	return (false);
	//}

	// assign a random value, if user didn't assign.
	// here is what happened....
	// in case users use default(=0) or don't put any value for a random seed 
	// the optimizer assign a number to a random seed based on the system clock.
	// so, if users submit multiple same jobs in a short time slot without a random seed(or with default),
	// some jobs could be initiated almost at the same time in hivclu,
	// and then they get the same seed and eventually get the exactly same output.
	if ( theForm.RANDOM_SEED.value == 0 ) 
	{
		theForm.RANDOM_SEED_BETTER.value = Math.floor(Math.random()*1e+8)
	}
	else 
	{
		theForm.RANDOM_SEED_BETTER.value = theForm.RANDOM_SEED.value;
	}

	return (true);
}
