/*
VERSION:		1.1

USAGE:
	#include "functions/flagIsOn.as"
	if(  flagIsOn("merf>10", allFlags_obj)  )
		// do stuff
*/
flagIsOn = function( flag_txt, flag_obj )
{
	var getFlagParts = function( flag )
	{// convert "myFlag" to "myFlag != undefined"		"!myFlag" to "myFlag = undefined"		"myFlag>2" to "myFlag > 2"
		var parts_array = [];
		
		// remove space from beginning of flag
		var firstChar = flag.charAt(0);
		while(firstChar==" "){
			flag = flag.substr(1);		// chop off first character
			firstChar = flag.charAt(0);
		}// while:  space exists at the beginning
		
		// remove space from end of flag
		var lastChar = flag.charAt( flag.length-1 );
		while(lastChar==" "){
			flag = flag.substr(0, flag.length-1);		// chop off last character
			lastChar = flag.charAt( flag.length-1 );
		}// while:  space exists at the end
		
		// catch:  !myFlag
		if( flag.charAt(0) == "!" ){
			flag = flag.substr(1)+"=undefined";
		}// if:  flag starts with !
		
		if( flag.indexOf("!=") != -1 ){
			var endAt = flag.indexOf("!=");
			var startAt = endAt+2;
		}
		else if( flag.indexOf("<=") != -1 ){
			var endAt = flag.indexOf("<=");
			var startAt = endAt+2;
		}
		else if( flag.indexOf(">=") != -1 ){
			var endAt = flag.indexOf(">=");
			var startAt = endAt+2;
		}
		else if( flag.indexOf("<") != -1 ){
			var endAt = flag.indexOf("<");
			var startAt = endAt+1;
		}
		else if( flag.indexOf(">") != -1 ){
			var endAt = flag.indexOf(">");
			var startAt = endAt+1;
		}
		else if( flag.indexOf("=") != -1 ){
			var endAt = flag.indexOf("=");
			var startAt = endAt+1;
		}
		else{// if:  there's no operator
			var endAt = flag.length;
			flag = flag+"!=undefined"
			var startAt = endAt+2;
		}// if:  there's no operator
		
		parts_array.push(flag.substr( 0, endAt ));					// [0]	myFlag
		parts_array.push(flag.substring( endAt, startAt ));	// [1]	!=
		parts_array.push(flag.substr( startAt ));						// [2]	"merf"
		
		// remove space from end of flag name
		var lastChar = parts_array[0].charAt( parts_array[0].length-1 );
		while(lastChar==" "){
			parts_array[0] = parts_array[0].substr(0, parts_array[0].length-1);		// chop off last character
			lastChar = parts_array[0].charAt( parts_array[0].length-1 );
		}// while:  space exists at the end
		// remove space from beginning of value
		var firstChar = parts_array[2].charAt(0);
		while(firstChar==" "){
			parts_array[2] = parts_array[2].substr(1);		// chop off first character
			firstChar = parts_array[2].charAt(0);
		}// while:  space exists at the beginning
		
		return parts_array;
	}// getFlagParts()
	
	
	
	var checkFlag = function( spriteFlag, display_obj )
	{
		var output = true;			// returns boolean "false" when a flag fails a test
		var flagParts_array = getFlagParts( spriteFlag );
		var variable = flagParts_array[0];
		var operation = flagParts_array[1];
		var value = flagParts_array[2];
		
		// convert value into likely datatype
		// // number
		if( isNaN(value)==false )
			value = Number(value);
		// // boolean
		if( value=="true" )
			value = true;
		if( value=="false" )
			value = false;
		// // undefined
		if( value=="undefined" )
			value = undefined;
		// // string
		// already a string by default
		
		// separate variable name from variable path
		var endAt = variable.lastIndexOf(".");
		if( endAt != -1 )
		{// if:  there is an object path
			var flagPath = variable.substr(0, endAt);			// myObj.anotherObject
			var flagName = variable.substr( endAt+1 );		// myVar
			var flagPath = "display_obj."+flagPath;		// display_obj.myObj.anotherObject
		}// if:  there is an object path
		else
		{// if:  there are no preceeding objects
			var flagName = variable;
			var flagPath = "display_obj";
		}// if:  there are no preceeding objects
		var displayValue = eval(flagPath+"."+flagName);
		
		// check specified contents, in the specified ways
		switch( operation )
		{
			case "=":
				output = ( displayValue == value );
			break;
			case "!=":
				output = ( displayValue != value );
			break;
			case "<":
				output = ( displayValue < value );
			break;
			case ">":
				output = ( displayValue > value );
			break;
			case "<=":
				output = ( displayValue <= value );
			break;
			case ">=":
				output = ( displayValue >= value );
			break;
		}// case:  operation
		
		return output;
	}// checkFlag()
	
	
	
	return checkFlag( flag_txt, flag_obj );
}// flagIsOn()