
/* 
	Java script for ASAP, SVMTM, Golgi Web form input checking etc
	Author: Shane Zhang, IMB, UQ, s.zhang@imb.uq.edu.au
	check if user has entered sequence, handles submission etc 
*/

var warning_empty = "There is nothing to submit. Please try again.";
var invalid_seq = "The entered protein sequence does not look valid!";
var debugging = "The system is undergoing major maintenance and upgrade. Please check back later.";
var warning_run = "The batch prediction process may take a while to complete. Please leave your browser window open.";
var testing = "The system is currently under testing, and will be back to normal soon.";
			
function warning_for_old_browsers() {
	var browser = navigator.appName;
	var version = parseFloat(navigator.appVersion);
	if((browser == "Netscape" && version < 5) || (browser == "Microsoft Internet Explorer" && version < 4.0)) {
		alert("This site is best viewed with IE5.5+ or Netscape 6+. Your browser seems a bit too old.");
	}
}
			
function check_asa_input(strForm) {
	var myForm = document.forms[strForm];	
	var strSeq = myForm.seq_text.value;
	
	strSeq = trim(strSeq);
	
	if (!strSeq) {
		window.alert(warning_empty);
		myForm.seq_text.focus();
		return false;
	}
	
	//check if valid sequence entered?
	if (!isSeqValid(strSeq)) {
		window.alert(invalid_seq);
		return false;
	}
	
	return true;
}

function check_svmtm_file(frmName) {
	var myForm = document.forms[frmName];
	var strEvalue = "";
	var strFile = "";
		
	strFile = myForm.seq_file.value;
	strEvalue = myForm.threshold1.value;
		
	strFile = trim(strFile);
	strEvalue = trim(strEvalue);
		
	if (!strFile) {
		window.alert(warning_empty);
		myForm.seq_file.focus();
		return false;
	}
		
	if (!isScoreValid(strEvalue)) {
		window.alert("Please enter a valid score threshold.");
		myForm.threshold1.focus();
		return false;
	}
	disableSubmit('btnSubmit1');
	return true;
}

function check_svmtm_text(frmName) {
	var myForm = document.forms[frmName];
	var strEvalue = "";
	var strSeq = "";
	
	strSeq = myForm.seq_text.value;
	strEvalue = myForm.threshold2.value;
		
	strSeq = trim(strSeq);
	strEvalue = trim(strEvalue);
		
	if (!strSeq) {
		window.alert(warning_empty);
		myForm.seq_text.focus();
		return false;
	}
	
	if (!isSeqValid(strSeq)){
		window.alert(invalid_seq);
		myForm.seq_text.focus();
		return false;
	}
		
	if (!isScoreValid(strEvalue)) {
		window.alert("Please enter a valid score threshold.");
		myForm.threshold2.focus();
		return false;
	}
	disableSubmit('btnSubmit2');
	return true;
}

function isScoreValid(strEvalue) {
	
	if (!strEvalue || isNaN(strEvalue)) {		
		return false;
	}
	
	if (parseFloat(strEvalue) < 0) {
		return false;
	}
	
	return true;
}

function check_golgi_input(strForm) {
	var myForm = document.forms[strForm];
	var intCase = 0;
	
	if (strForm == "file_input") {
		intCase = 1;
		if (myForm.fastafile.value == "") {
			window.alert(warning_empty);
			myForm.fastafile.focus();
			return false; 
		}
	}
	else if (strForm == "text_input") {
		intCase = 2;
		if (myForm.fastatext.value == "") {
			window.alert(warning_empty);
			myForm.fastatext.focus();
			return false; 
		}
		
		if (!isSeqValid(myForm.fastatext.value)) {
			window.alert(invalid_seq);
			return false;
		}
	}
	
	switch(intCase) {
		case 1:
			disableSubmit('btnSubmit1');
			break;
		case 2:
			disableSubmit('btnSubmit2');
			break;
	}
	return true;
}
		
function reload_content(){
	//window.alert("The page will be refreshed ... ");
	window.location.replace("http://ccb.imb.uq.edu.au/asapred/index.html");
}

function trim(myStr) {
	return myStr.replace(/^\s+|\s+$/g, "");
}

function stripWhite(myStr) {
	return myStr.replace(/\s+/g, "");
}

function checkSequence(strForm, textID) {
	var strSeq = null;
	var myForm = document.forms[strForm];
	var myText = document.getElementById(textID);
	
	strSeq = myText.value;
	
	strSeq = trim(strSeq);
	if (!strSeq) {
		alert(warning_empty);
		return false;
	}
	
	if (!isSeqValid(strSeq)) {
		//alert(invalid_seq);
		return false;
	}	
	return true;
}

function isSeqValid(strSeq) {
	// pattern to check = /[^acdefghiklmnpqrstvwyx]/i
	strSeq = trim(strSeq);
	if (!strSeq.match(/>\s*\w+/)) {
		strSeq = stripWhite(strSeq);
		if (strSeq.match(/[^acdefghiklmnpqrstvwyx]/i)) {
			return false;
		}
		
		if (limitExceeded(strSeq)) {
			return false;
		}			
	}
	else {
		// sequenc has id line 
		strSeq = strSeq.replace(/>\s*\w+\s+/, "");
		strSeq = stripWhite(strSeq);

		if (strSeq.match(/[^acdefghiklmnpqrstvwyx]/i)) {
			return false;
		}
		
		if (limitExceeded(strSeq)) {
			return false;
		}				
	}
	return true;
}

function limitExceeded(strSeq) {
	if (strSeq.length < 20) {
		alert("Minimum protein sequence length = 20!");
		return true;
	}
	if (strSeq.length > 10000) {
		alert("Maximum protein sequence length = 10000!");
		return true;
	} 
	return false;
}

function enableSubmit(btnID) {
	var cmdSubmit = document.getElementById(btnID);
	if (cmdSubmit) {
		cmdSubmit.disabled = false;
	}
}

function resetAll(btnID) {
	//resetError("Ok");
	//resetProcessMsg("Ok");
	enableSubmit(btnID);
}
	
function disableSubmit(btnID) {
	var cmdSubmit = document.getElementById(btnID);
	if (cmdSubmit) {
		cmdSubmit.disabled = true;
	}
}

