
// ------------------------------------------------------------------------
// ------------------------------------------------------------------------

//Collection of Javascript functions that all work towards
//loading and parsing property files, returning the
//results in an associative array.
//
// $Id: get_property_file.js,v 1.2 2009/10/16 23:46:42 ntt Exp $

// ------------------------------------------------------------------------
// ------------------------------------------------------------------------

//Get file from server
function getFile(filename)
{
    var oxmlhttp = null;
    try {
    	oxmlhttp = new XMLHttpRequest();
	//oxmlhttp.overrideMimeType("text/xml");
    } catch (ex) {
    	try {
	    oxmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (ex2) {
	    return null;
	}
    }
    
    if (!oxmlhttp)
    	return null;
	
    try {
        oxmlhttp.open("GET", filename ,false);
	oxmlhttp.send(null);
    } catch (ex) {
	return null;
    }
    
    return oxmlhttp.responseText;
}


// ------------------------------------------------------------------------
// ------------------------------------------------------------------------


function trimString(entry)
{
    var newEntry = entry.replace(/^\s*/, "");
    newEntry = newEntry.replace(/\s*$/, "");
    return newEntry;	
}


// ------------------------------------------------------------------------
// ------------------------------------------------------------------------


function getPropertiesFromFile(filename)
{
    var contents = getFile(filename);
    
    if (!contents)
        return null;
    
    var propArray = new Array();
    var lineArray = contents.split("\n");
    
    for (var i = 0 ; i < lineArray.length; i++)
    {
	var line = lineArray[i];
	
	//strip comment if found
	var commPosition = line.indexOf("#");
	if (commPosition > -1)
	    line = line.substring(0, commPosition);
	
	line = trimString(line);
	
	if (line.length == 0)
	    continue;
	
	var eqPosition = line.indexOf("=");
	if (eqPosition < 0)
	    continue;
	
	var key = line.substring(0, eqPosition);
	var val = line.substring(eqPosition + 1);
	
	if (key.length == 0)
	    continue;
	
	key = trimString(key);
	val = trimString(val);
	
	propArray[key] = val;	    
    }
    
    return propArray;
}


// ------------------------------------------------------------------------
// ------------------------------------------------------------------------


// ------------------------------------------------------------------------
// ------------------------------------------------------------------------


