var selectedRow = "";

function getFasta(species,idStd){
        var id2 = selectedRow;//document.getElementById("scoringIDs").value;
	if(id2 == ""){
		id2=idStd;
	}
        var url = "getFASTA.php";

        var content = "species=" + species + "&id=" + id2;
        var xhr = createXHR();
        xhr.onreadystatechange=function()
        {
                if(xhr.readyState == 4)
                {
			
			var textarea = "<textarea name='lizenz' cols='140' rows='2' readonly>"+xhr.responseText+"</textarea>";
			document.getElementById('seq').innerHTML =  textarea;

                }else{
                         document.getElementById('seq').innerHTML = "<h1><h1>"
                }
        };

        xhr.open("POST", url, true);
        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xhr.send(content);	 

}

function Calc(id, species){
	
	 var url = "score.php";
	 document.ajax.secid.value = id;
	 var content = "id=" + id + "&species=" + species;
	 document.getElementById('out').innerHTML =content;
 	 var xhr = createXHR();

         xhr.onreadystatechange=function(){
              if(xhr.readyState == 4){
                   document.getElementById('out').innerHTML = xhr.responseText; //server side
		   show_alert();
              }else{
			document.getElementById('out').innerHTML = "<h1>calculating - this can take some time ...</h1>the results will appear automatically, if you want to check your results later use the current url.";
              }
         };

         xhr.open("POST", url, true);
         xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
         xhr.send(content);

}

function Calc_Batch(id, species){
         var url = "score_batch.php";
	 var ids = id.split(",");
	 var results="";
	var resultUrlP1 ="http://www.cibiv.at/FACT/runScoring.php?id=";

	for(var i=0; i<ids.length; i++){
		results = results+resultUrlP1+ids[i]+"&species="+species+"<br>";
	}
	document.getElementById('urls').innerHTML=results;


         	document.ajax.secid.value = ids;
         	var content = "id=" + id + "&species=" + species;
         	document.getElementById('out').innerHTML =content;
         	var xhr = createXHR();

         	xhr.onreadystatechange=function(){
              		if(xhr.readyState == 4){
                		document.getElementById('out').innerHTML = xhr.responseText; //server side
                   		show_alert();
              		}else{
                        	document.getElementById('out').innerHTML = "<h1>calculating - this can take some time ...</h1>the results will be provided under the following URLs:";
              		}
         	};

         	xhr.open("POST", url, true);
         	xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
         	xhr.send(content);
	
	

}


function SortBy(id, species, mode, col){
         var url = "score.php";
         document.ajax.secid.value = id;
         var content = "id=" + id + "&species=" + species + "&mode=" + mode + "&sort="+col;
         document.getElementById('out').innerHTML =content;
         var xhr = createXHR();


         xhr.onreadystatechange=function(){
                if(xhr.readyState == 4){
                   document.getElementById('out').innerHTML = xhr.responseText; //server side
              }else{
                       document.getElementById('out').innerHTML = "<h1>calculating - this can take some time ...</h1>the results will appear automatically, if you want to check your results later use the current url.";
              }
         };

         xhr.open("POST", url, true);
         xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
         xhr.send(content);

}

function checkfile2(){
	document.getElementById('run').disabled = false;
}

function checkrun(){
	 if(!document.ajax.uploaded_file2.value || !document.ajax.uploaded_file.value) document.getElementById('run').disabled = true;
}

function sleep(milliseconds) {
            var start = new Date().getTime();
            for (var i = 0; i < 1e7; i++) {
	                if ((new Date().getTime() - start) > milliseconds){
			     break;
			     }
            }
}

function setSecid(){
         var myid = mysec;
         var mul=1;
         var countcheck = 0;
         for (i=0; i<document.ajax.feature.length; i++){
	     if (document.ajax.feature[i].checked==true){
		++countcheck;
                mul *= document.ajax.feature[i].value;
              }
	 }
         if(!countcheck) {mul = 2;}
}


function startApplet(species, idStd){
	var id2 = selectedRow;//document.getElementById("scoringIDs").value;
       if(id2 == ""){
                id2=idStd;
        }
        var url = "showApplet.php";
        var content = "species=" + species + "&id1=" + document.ajax.secid.value + "&id2=" + id2;
        var xhr = createXHR();
        xhr.onreadystatechange=function()
        {
                if(xhr.readyState == 4)
                {
                        document.getElementById('applet').innerHTML = xhr.responseText; //server side

                }else{
                         document.getElementById('applet').innerHTML = "<h1>The applet will come in a few minutes, please wait...<h1>"
                }
        };
        xhr.open("POST", url, true);
        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xhr.send(content);
}


function pausecomp(millis)
{
var date = new Date();
var curDate = null;

do { curDate = new Date(); }
while(curDate-date < millis);
}


function submitForm_scoring(){

	var mysec=Math.floor(Math.random()*1111111)

        document.ajax.secid.value = mysec;
	document.ajax.program.value="scoring";

	var q = document.getElementById('querySeq').value;
	var species = document.getElementById('species').value;
	var ziel ="runScoring.php?id="+mysec+"&species="+species;

        var url = "upload.php";
        var content = "secid=" + mysec + "&program=scoring&querySeq=" + q;

	var xhr = createXHR();
        xhr.onreadystatechange=function()
        {
                if(xhr.readyState == 4)
                {	
                        //document.getElementById('applet').innerHTML = xhr.responseText; //server side
						
						window.location.href = ziel;
                }else{
                  	document.getElementById('runScoringOut').innerHTML = "processing, please wait..."
			
                }
        };
        xhr.open("POST", url, true);
        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xhr.send(content);

}

function submitForm_scoring_batch(){

/*
	var q = document.getElementById('querySeq').value;
	var numberOfFasta = q.match(/>/gi);

	var fastas = q.split(">");

	var randIds = new Array(2);
	for (var x = 0; x < fastas.length-1; x++)
   	{
		randIds[x]=Math.floor(Math.random()*1111111);
	}
        document.ajax.secid.value = randIds;
        document.ajax.program.value="batch";
        document.ajax.submit();
	
	var species = document.getElementById('species').value;
	var ziel ="runBatchScoring.php?id="+randIds+"&species="+species;
	pausecomp(500);
        window.location.href = ziel;

*/



		var q = document.getElementById('querySeq').value;
		var numberOfFasta = q.match(/>/gi);

		var fastas = q.split(">");

		var randIds = new Array(2);
		for (var x = 0; x < fastas.length-1; x++)
	   	{
			randIds[x]=Math.floor(Math.random()*1111111);
		}
	    document.ajax.secid.value = randIds;
		document.ajax.program.value="batch";

		var q = document.getElementById('querySeq').value;
		var species = document.getElementById('species').value;
		var ziel ="runBatchScoring.php?id="+randIds+"&species="+species;

	    var url = "upload.php";
	    var content = "secid=" + randIds + "&program=batch&querySeq=" + q;

		var xhr = createXHR();
	        xhr.onreadystatechange=function()
	        {
	                if(xhr.readyState == 4)
	                {	
	                        //document.getElementById('applet').innerHTML = xhr.responseText; //server side

							window.location.href = ziel;
	                }else{
	                  	document.getElementById('runScoringOut').innerHTML = "processing, please wait..."

	                }
	        };
	        xhr.open("POST", url, true);
	        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	        xhr.send(content);




}



function submitForm(){ 
	 var mysec=Math.floor(Math.random()*1111111)
         document.ajax.secid.value = mysec;
	 document.ajax.program.value = "compare";
         document.ajax.submit();

         var myid = mysec;
         Write("process.php", "phuong=" + myid + "&opts=");
} 

function Write(url, content){    // url is the script and data is a string of parameters
	 var xhr = createXHR();
         // Create a function that will receive data sent from the server

        xhr.onreadystatechange=function(){
                if(xhr.readyState == 4){
		     document.getElementById('applet').innerHTML = xhr.responseText; //server side
                     document.getElementById('run').disabled = false;
                }else{
                     document.getElementById('run').disabled = true;
                     document.getElementById('applet').innerHTML = "<h1>The feature dotplot will appear soon, please wait...<h1>"
                }
        };
        xhr.open("POST", url, true);            
        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xhr.send(content); 
}

var TableRow1 = {
    selected:-1,
    hoverColor:'#CCCCCC',
    defaultColor:'#FFFFFF',
    onmouseover : function(trow,id)
    {
        if (TableRow1.selected!= id)
            trow.bgColor=TableRow1.hoverColor;
    },
    onmouseout : function(trow,id)
    {
        if (TableRow1.selected!= id)
            trow.bgColor=TableRow1.defaultColor;
    },
    onclick : function(trow,id,protid)
    {
        if (TableRow1.selected == id)
        {
            TableRow1.selected = -1;
            trow.bgColor=TableRow1.hoverColor;
        }
        else
        {
            var r = document.getElementById('id_tablerow_'+TableRow1.selected);
 	 
            if (TableRow1.selected !=-1 && r)
               r.bgColor=TableRow1.defaultColor;
            TableRow1.selected = id;
            trow.bgColor=TableRow1.hoverColor;
	    selectedRow=protid;


        }
    }
} 


