/**
 * JavaScript Properties Class
 *
 * @class Class that takes a java-like properties file and turns it into
 * a JavaScript object.
 * <br /><br />
 * Only parses variables that are a key=value combination in which
 * the variable name is made up of alphanumeric characters. Otherwise
 * the line is ignored. 
 * <br /><br />
 * A sample property file:
 * <pre>     search=Paul Hackett
 *     after=8:DY
 *     source=NEWS;90days
 *     protocol=https:
 *     target=_blank
 *     override=chain2send.asp
 *     originationCode=00004
 *     api=8
 *     __TIME_DELAY__=2005:8:12:5:0:0;api;12</pre>
 *
 * USAGE:
 * <pre>var props = new JSProperties("properties.txt").getProperties();
 *alert(props.after);</pre>
 *
 * @version 0.1 
 * @requires HttpRequest HttpRequest Preferences Class
 * @author Christopher Baker <Christopher.Baker@lexisnexis.com>
 * @param {String} The filename for the properties file
 * @constructor
 */
function JSProperties() 
{
	this.filename = "js.properties.txt";
	
	if (arguments.length > 0)
	{
		this.filename = arguments[0];
	}
}

/**
 * Use XMLHttpRequest to read the properties file
 *
 * @return The properties as an Object
 * @type Object
 */
JSProperties.prototype.getProperties = function()
{
	var http = new HttpRequest();
	http.setTarget(this.filename);
	var pingGet = http.get();
	
	if (pingGet !== "") {
		return  JSProperties.parseProps(pingGet);
	}
	
	http.setTarget("../" + this.filename);
	pingGet = http.get();
	
	if (pingGet !== "") {
		return  JSProperties.parseProps(pingGet);
	}
	
   	throw new Error("Unable to find property file " + this.filename + ". Please verify that it is available.");
	
	return pingGet; 
};

/**
 * Method to parse an individual line.
 *
 * @private
 * @param {String} The raw properties file
 * @return The properties as an Object
 * @type Object
 */
JSProperties.parseProps = function(raw)
{

	var props = new Object();
	
	var lineArray = raw.split("\n");
	for (var i = 0; i < lineArray.length; i++)
	{
		
		var splitLine = lineArray[i].split("=");
		var key = trimAll(splitLine[0]);
		var val = trimAll(splitLine[1]);

		if (key)
		{
			// if the line begins with something other than a character or a number, do nothing.
			if (key.search(/^[_A-Za-z0-9]/) != -1)
			{
				props[key] = val;
			}
		}
	}
	return props;
};
