function init() {
	<!--disableIt('mismatch');-->
	checkJavascriptValidity();
}

/**
*Resets the form. Basically captures the current url, 
*gets the part before the ? and reloads with the 
*base url
**/
function resetForm() {
  var oldurl = self.location.href;
  var sampleindex = oldurl.indexOf('?');
  var newurl = oldurl.substring(0, sampleindex);
  self.location.href = newurl;
}

function checkJavascriptValidity() {
	document.getElementById("jsEnabled").style.visibility= 'visible';
	document.getElementById("jsDisabled").style.visibility= 'hidden';
}

function showOptions() {
	//checkJavascriptValidity();	
	var parentElement= document.getElementById('options');
	var count= document.getElementById('times');
	if(count.value == 0)  {
		var num= (document.getElementById('times').value -1) + 2;
                count.value= num;
                var newDivR= document.createElement('input');
		var newDivL= document.createElement('input');
		var divIdName= 'my'+ num +'Div';

		var textNode= document.createTextNode("WARNING:This may slow down Highlighter. If you have lots of sequences, please upload your own tree.");
		parentElement.appendChild(textNode);		
          	var newLine= document.createElement("br");
		parentElement.appendChild(newLine);		
		newDivR.setAttribute('type', "radio");
		newDivR.setAttribute('name', "treeType");
		newDivR.setAttribute('value', "nj");
		newDivR.setAttribute('id', "treeType");
		newDivR.setAttribute('onClick', "javascript:removeUpload()");
		newDivR.setAttribute('checked', 'checked');
		var textR= document.createTextNode("create NJ tree");
                parentElement.appendChild(newDivR);
		parentElement.appendChild(textR);

		newDivL.setAttribute('type', "radio");
		newDivL.setAttribute('name', "treeType");
		newDivL.setAttribute('value', "upload");
		newDivL.setAttribute('id', "treeType");
		newDivL.setAttribute('onClick', "javascript:addUpload()");
		var textL= document.createTextNode("Use my tree");
		parentElement.appendChild(newDivL);
		parentElement.appendChild(textL);



	}
}

function removeUpload() {
	var count= document.getElementById('uploadTimes');	
	if (count.value == 1) {
		var parentElement= document.getElementById('options');
		//childNodes[4] is radio.
		//5 is the text, 6 is the uploadBox
		parentElement.removeChild(parentElement.childNodes[6]);

		//Decrease the value of count or the if statement in addElements doesn't work properly.
		//the count will keep increasing everytime the yes button is clicked, but because
		//of the if, the upload element will not appear if the yes is clicked after a no.
		var num= count.value -1;
		count.value= num;
	}
}

function addUpload() {
	var parentElement= document.getElementById('options');
	var newDivUpload= document.createElement('input');
	var uploadCount= document.getElementById('uploadTimes');
	if(uploadCount.value == 0)  {
		newDivUpload.setAttribute('type', "file");
		newDivUpload.setAttribute('size', 10);
		newDivUpload.setAttribute('name', "uploadTree");
		newDivUpload.setAttribute('id', "uploadTree");

		parentElement.appendChild(newDivUpload);
	 	var num= (document.getElementById('uploadTimes').value -1) + 2;
                uploadCount.value= num;
	}
}

function removeOptions(divNum) {
	var uploadCount= document.getElementById('uploadTimes');
	if(uploadCount.value != 0) {
		var uploadNum= uploadCount.value-1;
		uploadCount.value= uploadNum;
	}

	var count= document.getElementById('times');	
	if (count.value == 1) {
		var parentElement= document.getElementById('options');
		var olddiv= document.getElementById(divNum);
		parentElement. removeChild(olddiv);
		var nodeNumber= parentElement.childNodes.length;
		var check=0;
		while(parentElement.hasChildNodes()) {
			parentElement.removeChild(parentElement.firstChild);
		}
	
		//Decrease the value of count or the if statement in addElements doesn't work properly.
		//the count will keep increasing everytime the yes button is clicked, but because
		//of the if, the upload element will not appear if the yes is clicked after a no.
		var num= count.value -1;
		count.value= num;
	}
}

/**
*This function does exactly what disable it does but 
*in addition, also disables the iupac and gaps buttons.
**/
function disableIUPACAlso(choice) {

        document.input.colorPolymorphisms[0].checked = false;
        document.input.colorPolymorphisms[1].checked = false;
        document.input.colorPolymorphisms[0].disabled = true;
        document.input.colorPolymorphisms[1].disabled = true;

        document.input.useBars[0].checked = false;
        document.input.useBars[1].checked = false;
        document.input.useBars[0].disabled = true;
        document.input.useBars[1].disabled = true;
        
	if(document.input.numberOfConsensus != null) {
            document.input.numberOfConsensus.value="";
            document.input.numberOfConsensus.disabled = true;
        }

        document.input.gaps[0].checked = false;
	document.input.gaps[1].checked = false;
	document.input.gaps[0].disabled= true;
	document.input.gaps[1].disabled= true;

	document.input.iupac[0].checked= false;
	document.input.iupac[1].checked= false;
	document.input.iupac[2].checked= false;
	document.input.iupac[0].disabled= true;
	document.input.iupac[1].disabled= true;
	document.input.iupac[2].disabled= true;

	if(choice != "mismatch") {
		//alert(choice);
		disableApobec();		
		enableTreeSort();
	}	
	else {
		enableApobec();
	}
}

function disableIt(choice) {
        	
	//alert("You selected");		
	/*Buttons label polymorphisms and use bars and the 
	 * text area for inserting number of masters should
	 * be used ONLY during matches, so if any other button
	 * is selected, then these fields are disabled*/

        document.input.colorPolymorphisms[0].checked = false;
        document.input.colorPolymorphisms[1].checked = false;
        document.input.colorPolymorphisms[0].disabled = true;
        document.input.colorPolymorphisms[1].disabled = true;
	
        document.input.useBars[0].checked = false;
	document.input.useBars[1].checked = false;
	document.input.useBars[0].disabled = true;
	document.input.useBars[1].disabled = true;
	
	//First clears the text area before disabling it
        if(document.input.numberOfConsensus != null) {
	    document.input.numberOfConsensus.value="";
	        document.input.numberOfConsensus.disabled = true;
        }	
	
	/*When any other button than silent non silent is 
	 *clicked, the iupac handling gaps and handling gaps
	  *needs to be done. So enabling it again, because it
	  * it might have got disabled while clicking on SNS
	 */
        document.input.gaps[0].checked = true;
	document.input.gaps[0].disabled= false;
	document.input.gaps[1].disabled= false;

	document.input.iupac[0].checked= true;
	document.input.iupac[0].disabled= false;		
	document.input.iupac[1].disabled= false;
	document.input.iupac[2].disabled= false;

	/*Check the sort by similarity option because this 
	may have been removed when user clicked on match
	before clicking on mismatch/TT/SNS*/
	document.input.sort[0].checked=true;

	if(choice != "mismatch") {
		disableApobec();		
	}	
	else {
	      enableApobec();
	}

	enableTreeSort();	
}


function disableApobec() {
	//alert("Not mismatch");
	document.input.Apobec.checked= false;
	document.input.Apobec.disabled= true;
	
}

function enableApobec() {
	//alert("Not mismatch");
	document.input.Apobec.checked= false;
	document.input.Apobec.disabled= false;
	
}

function enableIt(choice) {
	/*Sets a default value of 2 for the number of masters
	 *enables color polymorphisms etc. This is done when the 
	 *match option is selected.
	 */
	if(document.input.numberOfConsensus != null) {
	    document.input.numberOfConsensus.disabled = false;
	    document.input.numberOfConsensus.value="2";
	}	       
        document.input.colorPolymorphisms[0].disabled = false;
        document.input.colorPolymorphisms[1].disabled = false;
	document.input.colorPolymorphisms[0].checked = true;
	
        document.input.useBars[0].disabled = false;
        document.input.useBars[1].disabled = false;
        document.input.useBars[1].checked = true;

	/*
	 *Enabling gaps and iupac also because this 
	 *could have been disabled if SNS was clicked 
	 *earlier
	 */
        document.input.gaps[0].checked = true;
	document.input.gaps[0].disabled= false;
	document.input.gaps[1].disabled= false;

	document.input.iupac[0].checked= true;
	document.input.iupac[0].disabled= false;		
	document.input.iupac[1].disabled= false;
	document.input.iupac[2].disabled= false;

	if(choice != "mismatch") {
		//alert(choice);
		disableApobec();		
	}	
	else {
		enableApobec();
	}

	disableTreeSort();
}

function disableTreeSort () {
	document.input.sort[1].checked= false;	
	document.input.sort[1].disabled= true;
	/*Remove the options for tree sort if match is clicked*/
	var parentElement= document.getElementById('options');
	var nodeNumber= parentElement.childNodes.length;
	while(parentElement.hasChildNodes()) {
		parentElement.removeChild(parentElement.firstChild);
	}
}

function enableTreeSort () {
	document.input.sort[1].disabled= false;
}

/**
*Checks if user supplied no input.
*If so, prevents the form from being submitted.
*If both are submitted, the cgi prints a warning, but
*continues processing request.
**/
function checkIfInputPresent(pastename, uploadname) {
	if( pastename == '' && uploadname == '') {
		alert('Please paste or upload a sequence file.');
		return false;		
	}
	else {
		form.submit();

	}
   
}
