/*javascript functions for CossMOSS BLAST 
Author: Daniel Lang 
Last Changes 21.01.2004
#sz -- changed onKeyPress; submit login form, if user,password not emtpy 
*/

//handle RETURN/ENTER key
function onKeyPress(e){
	var keycode;
	if (!e && window.event.srcElement.type=="textarea") return true;
	else if (e && e.target.type=="textarea") return true; 
	if (window.event) keycode =window.event.keyCode;
	else if (e) keycode = e.which;
	else return true;
	if (keycode == 13) { 
	    if ((!(document.Login.user.value=="")) && (!(document.Login.password.value==""))) {
		
		document.Login.submit();
		return false;
		}		
		else {
			submit_Data();
			return false;
		}
	    }
	return true;
}
document.onkeypress= onKeyPress;


function checkSequence() {
	if (document.Formular.sequence.value.length > 30000) {
		alert("The sequence you provided is too long!\nA maximum of 30000 characters are permitted for the sequence textbox!\nIf you want to BLAST more sequences please provide the sequence from file!");
	}
}
function checkEmail() {
	if (document.Formular.emailcheck.checked) {
		document.Formular.email.style.visibility="visible"
		document.getElementById('text').checked=true
	}
	else {
		document.Formular.email.style.visibility="hidden"
	}
}
function testEmail() {
	if (document.Formular.emailcheck.checked) {
		document.Formular.email.value='';
	}
}
function check_blasttype(){
	var peptide = /_p$/i; // _p for peptides && _n for nucleic acids
	var nucleic = /_n$/i;

	if (document.Formular.database.value.match(nucleic)  && document.Formular.molecule.value== 'nucleic acid'){ //case BLASTN
		document.Formular.blasttype.options[0].selected=true
		document.getElementById('Matrix').style.visibility="hidden"
		document.getElementById('MatrixDesc').style.visibility="hidden"
		document.Formular.evalue.value='1e-2'
		}

	else if (document.Formular.database.value.match(nucleic) && document.Formular.molecule.value=='peptide'){ //case TBLASTN
		document.Formular.blasttype.options[3].selected=true
		document.getElementById('Matrix').style.visibility="visible"
		document.getElementById('MatrixDesc').style.visibility="visible"
		document.Formular.evalue.value='1e-4'
		}

	else if (document.Formular.database.value.match(peptide) && document.Formular.molecule.value=='nucleic acid'){ //case BLASTX
		document.Formular.blasttype.options[2].selected=true
		document.getElementById('Matrix').style.visibility="visible"
		document.getElementById('MatrixDesc').style.visibility="visible"
		document.Formular.evalue.value='1e-4'
		}
	else if (document.Formular.database.value.match(peptide) && document.Formular.molecule.value=='peptide'){ //case BLASTP
		document.Formular.blasttype.options[1].selected=true
		document.getElementById('Matrix').style.visibility="visible"
		document.getElementById('MatrixDesc').style.visibility="visible"
		document.Formular.evalue.value='1e-4'
		}	
}		

function showBox() {
	if (!document.Formular.Advanced.checked) {
		document.Formular.Advanced.style.visibility="visible"
		document.getElementById('table1').style.visibility="hidden"
		}
	else if (document.Formular.Advanced.checked) {
		document.Formular.Advanced.style.visibility="visible"
		}
}

function changeAdvanced() {
	if (document.Formular.Advanced.checked) {
		document.getElementById('table1').style.visibility="visible"
	}
	else if (!document.Formular.Advanced.checked){
		document.getElementById('table1').style.visibility="hidden"
	}	
}

function check_Program(){
//case nucleic acid
	if (document.Formular.blasttype.selectedIndex=='0' ){//case BLASTN
		document.getElementById('Matrix').style.visibility="hidden"
		document.getElementById('MatrixDesc').style.visibility="hidden"
		if (document.Formular.database.selectedIndex=='3' || document.Formular.molecule.selectedIndex=='1'){ 
			alert('Please check your selections! \nThis combination of input sequence, BLAST type and database is impossible!')
		}	
	}
	else if (document.Formular.blasttype.selectedIndex=='3') {//case TBLASTN
		document.getElementById('Matrix').style.visibility="hidden"
		document.getElementById('MatrixDesc').style.visibility="hidden"
		if (document.Formular.database.selectedIndex=='3' || document.Formular.molecule.selectedIndex=='0'){ 
			alert('Please check your selections! \nThis combination of input sequence, BLAST type and database is impossible!')
		}
	}
	else if (document.Formular.blasttype.selectedIndex=='4'){//case TBLASTX
		document.getElementById('Matrix').style.visibility="visible"
		document.getElementById('MatrixDesc').style.visibility="visible"
		if (!((document.Formular.database.selectedIndex=='0' || document.Formular.database.selectedIndex=='1' || document.Formular.database.selectedIndex=='2')&& document.Formular.molecule.selectedIndex=='0')){ 
			alert('Please check your selections! \nThis combination of input sequence, BLAST type and database is impossible!')
		}
	}
	else if (document.Formular.blasttype.selectedIndex=='2'){//case BLASTX
		document.getElementById('Matrix').style.visibility="hidden"
		document.getElementById('MatrixDesc').style.visibility="hidden"
		if (!(document.Formular.database.selectedIndex=='3' && document.Formular.molecule.selectedIndex=='0')){ 
			alert('Please check your selections! \nThis combination of input sequence, BLAST type and database is impossible!')
		}
	}
	else if (document.Formular.blasttype.selectedIndex=='1'){//BLASTP
		document.getElementById('Matrix').style.visibility="visible"
		document.getElementById('MatrixDesc').style.visibility="visible"
		if (!(document.Formular.database.selectedIndex=='3' && document.Formular.molecule.selectedIndex=='1')){ 
			alert('Please check your selections! \nThis combination of input sequence, BLAST type and database is impossible!')
		}
	}
	else if (document.Formular.blasttype.selectedIndex=='3'){//case TBLASTN
		document.getElementById('Matrix').style.visibility="visible"
		document.getElementById('MatrixDesc').style.visibility="visible"
		if ( !(document.Formular.database.selectedIndex=='0'||document.Formular.database.selectedIndex=='1'|| document.Formular.database.selectedIndex=='2')&& document.Formular.molecule.selectedIndex=='0'){ 
			alert('Please check your selections! \nThis combination of input sequence, BLAST type and database is impossible!')
		}
	}
}

function submit_Data() {
	document.Formular.submit()
}
function reset_Form() {
	document.Formular.reset()
        location.reload()    	
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);

function MM_goToURL() { //v3.0
  var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
  for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}
