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

function check_input(strForm) {
	var warning_empty = "Please enter protein sequence in the textarea and try again.";
	var debugging = "The system is undergoing major maintenance and upgrade. Please check back later.";
	var testing = "The system is currently under testing, and will be back to normal soon.";
	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("The entered protein sequence does not look valid!");
		return false;
	}
	
	//window.alert("Warning: system upgrade/under evaluation.");
	disableSubmit();
	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, "");
}

/*
* minimum sequence length = 60, maximum length = 6000
*/
function isSeqValid(strSeq) {
	// pattern to check = /[^acdefghiklmnpqrstvwyx]/i
	if (!strSeq.match(/^>/)) {
		strSeq = stripWhite(strSeq);
		if (strSeq.match(/[^acdefghiklmnpqrstvwyx]/i)) {
			return false;
		}
		
		if (!isGoodLength(strSeq)) {
			return false;
		}
	}
	else {
		// sequenc has id line 
		strSeq = strSeq.replace(/>\s+/, ">");
		var seq_array = strSeq.split(/\s+/);
		seq_array.shift();
		
		if (seq_array.length == 0) {
			return false;
		}
		
		strSeq = seq_array.join("");
		
		if (strSeq == "") {
			return false;
		}
		
		if (strSeq.match(/[^acdefghiklmnpqrstvwyx]/i)) {
			return false;
		}
		
		if (!isGoodLength(strSeq)) {
			return false;
		}
	}
	return true;
}

function isGoodLength(strSeq) {
	if (strSeq.length < 60) {
		return false;
	}
	
	if (strSeq.length > 6000) {
		return false;
	}
	return true;
}

function checkSequence(strForm) {
	var myForm = document.forms[strForm];	
	var strSeq = myForm.seq_text.value;
	strSeq = trim(strSeq);
	
	if (!strSeq) {
		window.alert(warning_empty);
		disableSubmit();
		return false;
	}
	
	//check if valid sequence entered?
	if (!isSeqValid(strSeq)) {
		window.alert("The entered protein sequence does not look valid!");
		disableSubmit();
		return false;
	}
	enableSubmit();
	return true;
}

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

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