// (C) Copyright IBM Corp. 2001 - 2002 All Rights Reserved.
//
// flagchar.js
//
// last upload: Nov 08, 2001 (cbcsrv)
// last update: Mar 12, 2002

function findMinusPlusPointChar(form)
{
// returns false if the following chars are found: "-", "+", or "."

if ((form.sequences.value.indexOf("-") > -1)  ||  (form.sequences.value.indexOf("+") > -1) || (form.sequences.value.indexOf(".") > -1))
	{
	alert('The "Input Sequences" cannot have negative/positive signs or decimal points');
	return false;
	}

return true;
}

function findInvalidChar(form)
{
// return false if a char is NOT: a digit, \n, \r, \t, or [space]
// creating the datastring array is much faster than accessing the char via form.sequences.value.charAt(c_1)

var c_1;
var seqchar;

var datastring = form.sequences.value;
var length = form.sequences.value.length;

for (c_1 = 0; c_1 < length; c_1++)
	{
	seqchar = datastring.charAt(c_1);

	if (!(((seqchar >= "0") && (seqchar <= "9")) || (seqchar == "\n")  || (seqchar == "\r")  || (seqchar == " ")  || (seqchar == "\t")))
		{
		alert('The "Input Sequences" cannot contain nonnumeric characters\n (such as the character "' + seqchar + '")');
		return false;
		}
	}

return true;
}

function findInvalidGeneXpressChar(form)
{
// return false if a char is NOT: a digit, [period], -, +, \n, \r, \t, or [space]

var c_1;
var seqchar;

var datastring = form.sequences.value;
var length = form.sequences.value.length;

for (c_1 = 0; c_1 < length; c_1++)
	{
	seqchar = datastring.charAt(c_1);

	if (!(((seqchar >= "0") && (seqchar <= "9")) || (seqchar == ".") || (seqchar == "-") || (seqchar == "+") || (seqchar == "\n")  || (seqchar == "\r")  || (seqchar == " ")  || (seqchar == "\t")))
		{
		alert('The "Input Sequences" cannot contain nonnumeric characters\n (such as the character "' + seqchar + '")');
		return false;
		}
	}

return true;
}

function findInvalidAAChar(form)
{
// return false if a char is NOT: a valid AA (B, Z, X, and * are illegal) \n, or \r

var answer = true;

var c_1 = 0;
var c_2 = 0;

var lines;
var seqchar;

if (document.thisform.sequences.value.indexOf( '\n' ) >= 0 ) {
    lines = document.thisform.sequences.value.split( '\n' );
  }
  else {
    lines = document.thisform.sequences.value.split( '\r' );
  }

for (c_1 = 1; c_1 < lines.length; c_1++)
	{
	for (c_2 = 0; c_2 < lines[c_1].length; c_2++)
		{
		if (lines[c_1].charAt(0) == '>')
			{
			c_2 = lines[c_1].length;
			}
		else	{
			seqchar = lines[c_1].charAt(c_2);

			if ((seqchar != "A") && (seqchar != "a") && (seqchar != "C") && (seqchar != "c") && (seqchar != "D") && (seqchar != "d") && (seqchar != "E") && (seqchar != "e") && (seqchar != "F") && (seqchar != "f") && (seqchar != "G") && (seqchar != "g") && (seqchar != "H") && (seqchar != "h") && (seqchar != "I") && (seqchar != "i") && (seqchar != "J") && (seqchar != "j") && (seqchar != "K") && (seqchar != "k") && (seqchar != "L") && (seqchar != "l") && (seqchar != "M") && (seqchar != "m") && (seqchar != "N") && (seqchar != "n") && (seqchar != "O") && (seqchar != "o") && (seqchar != "P") && (seqchar != "p") && (seqchar != "Q") && (seqchar != "q") && (seqchar != "R") && (seqchar != "r") && (seqchar != "S") && (seqchar != "s") && (seqchar != "T") && (seqchar != "t") && (seqchar != "U") && (seqchar != "u") && (seqchar != "V") && (seqchar != "v") && (seqchar != "W") && (seqchar != "w") && (seqchar != "Y") && (seqchar != "y") && (seqchar != "\n") && (seqchar != "\r"))
				{
				alert('The "Input Sequences" contains the illegal non-amino acid character "' + seqchar + '" on line ' + eval(c_1+1) + '\n(position ' + eval(c_2 + 1) + ').  Only the amino acids ACDEFGHIJKLMNOPQRSTUVWY are permitted.');
				return false;
				}
			}
		}
	}

return true;
}

function findInvalidNtsChar(form)
{
// return false if a char is NOT: ACGTURYMKSWBDHVN, \n, or \r

var answer = true;

var c_1 = 0;
var c_2 = 0;

var lines;
var seqchar;

if (document.thisform.sequences.value.indexOf( '\n' ) >= 0 ) {
    lines = document.thisform.sequences.value.split( '\n' );
  }
  else {
    lines = document.thisform.sequences.value.split( '\r' );
  }

for (c_1 = 1; c_1 < lines.length; c_1++)
	{
	for (c_2 = 0; c_2 < lines[c_1].length; c_2++)
		{
		if (lines[c_1].charAt(0) == '>')
			{
			c_2 = lines[c_1].length;
			}
		else	{
			seqchar = lines[c_1].charAt(c_2);

			if ((seqchar != "A") && (seqchar != "a") && (seqchar != "C") && (seqchar != "c") && (seqchar != "G") && (seqchar != "g") && (seqchar != "T") && (seqchar != "t") && (seqchar != "U") && (seqchar != "u") && (seqchar != "R") && (seqchar != "r") && (seqchar != "Y") && (seqchar != "y") && (seqchar != "M") && (seqchar != "m") && (seqchar != "K") && (seqchar != "k") && (seqchar != "S") && (seqchar != "s") && (seqchar != "W") && (seqchar != "w") && (seqchar != "B") && (seqchar != "b") && (seqchar != "D") && (seqchar != "d") && (seqchar != "H") && (seqchar != "h") && (seqchar != "V") && (seqchar != "v") && (seqchar != "N") && (seqchar != "n") && (seqchar != "\n") && (seqchar != "\r"))
				{
				alert('The "Input Sequences" contains the illegal character "' + seqchar + '" on line ' + eval(c_1+1) + '\n(position ' + eval(c_2 + 1) + ').  Only the symbols ACGTURYMKSWBDHVN are permitted.');
				return false;
				}
			}
		}
	}

return true;
}

function findInvalidTempChar(form)
{
// return false if a char is an *;

var answer = true;

var c_1 = 0;
var c_2 = 0;

var lines;
var seqchar;

if (document.thisform.sequences.value.indexOf( '\n' ) >= 0 ) {
    lines = document.thisform.sequences.value.split( '\n' );
  }
  else {
    lines = document.thisform.sequences.value.split( '\r' );
  }

for (c_1 = 1; c_1 < lines.length; c_1++)
	{
	for (c_2 = 0; c_2 < lines[c_1].length; c_2++)
		{
		if (lines[c_1].charAt(0) == '>')
			{
			c_2 = lines[c_1].length;
			}
		else	{
			seqchar = lines[c_1].charAt(c_2);

//			if ((seqchar != "G") && (seqchar != "A") && (seqchar != "T") && (seqchar != "C") && (seqchar != "R") && (seqchar != "Y") && (seqchar != "N") && (seqchar != "g") && (seqchar != "a") && (seqchar != "t") && (seqchar != "c") && (seqchar != "r") && (seqchar != "y") && (seqchar != "n") && (seqchar != "\n") && (seqchar != "\r"))

			if (seqchar == "*")
				{
				alert('The "Input Sequences" contains the illegal character "' + seqchar + '" on line ' + eval(c_1+1) + '\n(position ' + eval(c_2 + 1) + ').  Asterisks are NOT permitted.');
				return false;
				}
			}
		}
	}

return true;
}

function selectValidChar(form)
{
var answer = true;

if	(form.validCharOption[0].checked)
	{
	answer = findInvalidAAChar(form);
	}
else if (form.validCharOption[1].checked)
	{
	answer = findInvalidNtsChar(form);
	}
else if (form.validCharOption[2].checked)
	{
	answer = findInvalidTempChar(form);
	}

return answer;
}
