//Scroll down to the line with the //CHANGE HERE comment and
//do the modifications described.
var oXml = new ActiveXObject("Msxml.DOMDocument");
oXml.async = false;

var oXsl = new ActiveXObject("Msxml.DOMDocument");
oXsl.async = false;

function OnUserChange(curUser, newUser)
{
}

//Palm Desktop informs us we have a data file to present.
function OnOpenUserDataFile(user, dir, path)
{
  if(!oXml.load(path))
  {
     alert("Failed to load " + path);
		 return;
  }

	//CHANGE HERE//
	//////////////////////////////////////////////////////
	//Add a line like this for each database that needs to analyzed
	//in the xml data file.
	//The argument is the name of the database.
	analyzeRecords("PDAToolboxA");
	//analyzeRecords("PDAToolboxB");
	//////////////////////////////////////////////////////
	
	sXslFile = "gui.xsl";
  if(!oXsl.load(sXslFile))
  {
     alert("Failed to load xsl " + sXslFile);
		 return;
  }
  guiTarget.innerHTML = oXml.transformNode(oXsl);
}

function openUserDataFile()
{
  var sUserDirectory = DeskTopUtility.GetUserDirectory(userSelect.value);
	var sPath = sUserDirectory + "\\pdatoolbox\\dboutput.xml";
  OnOpenUserDataFile(userSelect.value, sUserDirectory, sPath)
}

function ShowUsers()
{
  var szUserName = DeskTopUtility.GetFirstUserName();
  var sUserOptions = "";

	while(szUserName != "")
	{
    sUserOptions += "<OPTION VALUE=\"" + szUserName + "\"/>" + szUserName;
		szUserName = DeskTopUtility.GetNextUserName();
  }
  sUserSelect = "<SELECT ID=userSelect class=\"select\" onchange=\"openUserDataFile();\">" + sUserOptions + "</SELECT>";
  userListTarget.innerHTML  = sUserSelect;
	openUserDataFile();
}


//Extract a string from the list of bytes and create an element 
//in the xml tree with the name sFieldName
function addStringElementFromByteList(oRecord, oByteList, sFieldLen, sFieldName)
{
  var sResult = "";
	var nOddLen = (sFieldLen % 2);
	var nLen = new Number(sFieldLen);
	if( nOddLen == 1)
	{
	  nLen = nLen + 1;
	}
  for (var i=0; i < nLen; i++)
  {
    oByte = oByteList.nextNode;
		var sByte = oByte.firstChild.nodeValue;
		sResult = sResult + String.fromCharCode(sByte);
	}
  var newElem = oXml.createElement(sFieldName);
  newElem.text = sResult;
  oRecord.appendChild(newElem);
}
 
//Extract a byte from the list of bytes.
function getNextByte(oByteList)
{
	return new Number(oByteList.nextNode.firstChild.nodeValue);
}

//Extract a bool from the list of bytes and create an element 
//in the xml tree with the name sFieldName
function addBoolElementFromByteList(oRecord, oByteList, sFieldName)
{
  var sResult = "";
	var hiByte = getNextByte(oByteList);
	var loByte = getNextByte(oByteList);
	var wBool = hiByte * 256 + loByte; 
	if(wBool != 0)
	{
	  sResult = "T";
	}
	else
	{
	  sResult = "F";
	}  
  var newElem = oXml.createElement(sFieldName);
  newElem.text = sResult;
  oRecord.appendChild(newElem);
}

//Extract an integer from the list of bytes and create an element 
//in the xml tree with the name sFieldName
function convertByteListToInteger(oByteList)
{
	var byByte4 = getNextByte(oByteList);
	var byByte3 = getNextByte(oByteList);
	var byByte2 = getNextByte(oByteList);
	var byByte1 = getNextByte(oByteList);
	return byByte4 * 16777216 + byByte3 * 65535 + byByte2 * 256 + byByte1;
}

//Extract a date from the list of bytes and create an element 
//in the xml tree with the name sFieldName
function addDateElementFromByteList(oRecord, oByteList, sFieldName)
{
  var newElem = oXml.createElement(sFieldName);
  newElem.text = convertByteListToInteger(oByteList);
  oRecord.appendChild(newElem);
}

//Extract a time from the list of bytes and create an element 
//in the xml tree with the name sFieldName
function addTimeElementFromByteList(oRecord, oByteList, sFieldName)
{
  var newElem = oXml.createElement(sFieldName);
  newElem.text = convertByteListToInteger(oByteList);
  oRecord.appendChild(newElem);
}

//Extract an alarm from the list of bytes and create an element 
//in the xml tree with the name sFieldName
function addAlarmFieldElementFromByteList(oRecord, oByteList, sFieldName)
{
  var newElem = oXml.createElement(sFieldName);
  newElem.text = convertByteListToInteger(oByteList);
  oRecord.appendChild(newElem);
}

//Run through all the fields defined in this record and add
//new elements as appropriate.
function analyzeFields(oRecord)
{
 var oNode;
 if(oRecord != null)
 {
     var oFieldTypeList = oRecord.getElementsByTagName("fieldType");
		 var oFieldNameList = oRecord.getElementsByTagName("fieldName");
		 var oFieldLenList = oRecord.getElementsByTagName("fieldLen");
		 var oByteList = oRecord.getElementsByTagName("b");

		 
     for (var i=0; i < oFieldTypeList.length; i++)
     {
		   oFieldType = oFieldTypeList.nextNode;
			 oFieldName = oFieldNameList.nextNode;
			 oFieldLen = oFieldLenList.nextNode;
			 
			 var sFieldType = oFieldType.firstChild.nodeValue;
			 var sFieldName = oFieldName.firstChild.nodeValue;
			 var sFieldLen  = oFieldLen.firstChild.nodeValue;
			 switch (sFieldType)
			 {
			    case "0"://String
					  addStringElementFromByteList(oRecord, oByteList, sFieldLen, sFieldName);
					  break;
			    case "1"://Bool (2 byte unsigned integer)
					  addBoolElementFromByteList(oRecord, oByteList, sFieldName);
					  break;
			    case "2"://Date (4 byte unsigned integer)
					  addDateElementFromByteList(oRecord, oByteList, sFieldName);
					  break;
			    case "3"://Time (4 byte unsigned integer)
					  addTimeElementFromByteList(oRecord, oByteList, sFieldName);
					  break;
			    case "4"://Integer
					  addIntElementFromByteList(oRecord, oByteList, sFieldName);
					  break;
			    case "5"://Digital Ink Field
					  addInkFieldElementFromByteList(oRecord, oByteList, sFieldName);
					  break;
			    case "6"://Alarm field (4 byte unsigned integer)
					  addAlarmFieldElementFromByteList(oRecord, oByteList, sFieldName);
					  break;
					default:
					  alert("Unexpected field type:" + sFieldType);
			}//end switch 
 		}//end for all field types
	}//endif oField != NULL
}
 
//For each database, 
function analyzeRecords(sDbName)
{
 var oNode;
 if(oXml.documentElement != null)
 {
   var oPdaToolboxA = oXml.documentElement.selectSingleNode("//" + sDbName);
   if(oPdaToolboxA != null)
   {
     var oRecordList = oPdaToolboxA.getElementsByTagName("record");

     for (var i=0; i < oRecordList.length; i++)
     {
       analyzeFields(oRecordList.nextNode);
     }
   }
 }
}

