//Error output function
function parseError(string, i) {
	alert("Parse Error at character " + i + ":\n" +
			string);
}

/*
//checks if there are any weird characters.
function isValidInput(string) {
	var temp = "";
	for (var i = 0; i < string.length; i++ ) {
		temp = string.charAt(i);
		if ( !isValidStart(temp) || !isValidEnd(temp)
			|| (temp != " ") || (temp != ",") ) {
			parseError("Character is not valid.", i);
			return false;	
		}
	}
	return true;
}
*/


//check if a character is a valid PC set number
function isValidNum(string) {
	if ( string == "t" )
		return true;
	else if ( string == "e" )
		return true;
	else if ( ((string.charCodeAt(0) - 48) >=0) && ((string.charCodeAt(0) - 48) <12) )
		return true;
	else
		return false;
}

/*
function isValidStart(string) {
	return (isValidNum(string) || (string == "<" )
			|| (string == "(") || (string == "{") )
}
*/

function isValidEnd(string) {
	return ( (string == ";") || (string == "\n") || (string == "") );
}

//stores a PCSet into the master array.  returns the index.
function parsePCSet(string, index) {
	var i = index;
	var set = "";
	while (!isValidEnd(string.charAt(i))) {
		if (!isValidNum(string.charAt(i)) ) {
			i++;
			continue;
		} else {
			if (set.indexOf(string.charAt(i)) == -1) {
				set += string.charAt(i);
			}
			else {
				//already has an element like this..forget it, just advance				
			}
			i++;
			continue;
		}
	}
	if ( (set.length < 3) || (set.length > 9) ) {
		//error, too small or large, discard
		//parseError("Discarding set <" + set + ">. Too few elements (0, 1, or 2).")
		i++;
		return i;
	}
	else {
		master[MASTER_LENGTH] = new Array();
		master[MASTER_LENGTH][SET] = set;
		master[MASTER_LENGTH][REAL_TRANS] = new Array();
		master[MASTER_LENGTH][REAL_INV] = new Array();
		master[MASTER_LENGTH][REAL_RETRO] = new Array();
		master[MASTER_LENGTH][REAL_RETRO_INV] = new Array();
		master[MASTER_LENGTH][REAL_NEAR_EQUIV] = new Array();
		master[MASTER_LENGTH][ABS_NEAR_EQUIV] = new Array();
		MASTER_LENGTH++;
		i++;
		return i;
	}
}

function parse(string) {
	var i = 0;
	var temp = "";
	while (i < string.length) {
		temp = string.charAt(i);
		i = parsePCSet(string, i);
	}
	return;
}
