Function sequence problem

Please see the actionscript

[AS]
XMLDoc = new XML();
//xmldocurl = _root.dataurl;
//If no path has been provided, then just load a default XML file. This part is optional.
//if (xmldocurl.length<1) {
tinterval=" ";
xmldocurl = “product.xml”;

_global.productname=new Array();
_global.productvalue=new Array();
//}
//Load the XML document
//XMLDoc.load(xmldocurl+"?curr="+getTimer());
trace(XMLDoc.load(xmldocurl));
XMLDoc.onLoad = fnloaded;
OnUpdate();

function fnloaded()
{

num=0;
cldnodes = new Array();

cldnodes = XMLDoc.childNodes;
dataset=cldnodes[0].childNodes;
//Time interval
// tinterval=cldnodes[0].attributes.tinterval;
// trace(“Time:”+cldnodes[0].attributes.tinterval);
trace(dataset.length);
for (k=0; k<=dataset.length; k++)
{
if (dataset[k].nodeName == “set”)
{
//Increment counter
num = num+1;
//Get the x-axis name
_global.productname[num] = dataset[k].attributes.name;
//Get the value
_global.productvalue[num] = dataset[k].attributes.value;
// trace(_global.productname[num]);
trace(“Value:in fnloaded”+_global.productvalue[num]);

}
}
//OnUpdate();

}

function OnUpdate()
{
// var fileContents = “productvals”;
var fileContents = “<productvals tinterval=”;
// fileContents=fileContents+tinterval;
trace(“Third:”+_global.productname[3]);
//“This is the string we are writing to the filejksdfsdj jksdf l.”;

/*
for(x=1;x<=num/2;x++)
{
//fileContents=fileContents+"
“+”<set name="+productname[x]+" value=""+productvalue[x]+""/>";
fileContents=fileContents+""+"=";
}
*/
trace(fileContents);
loadVarsText = new LoadVars();
loadVarsText.onLoad = function(success) {
if (success) {
if (this.wroteFile)
{
trace(“Success! Write to file complete.”);
}
else
{
trace(“Error, write to file failed!.”);
}
}
else
{
trace(“Error, loadVars failed!”);
}
}
loadVarsText.load("<A href=“http://localhost/writeFile.php?&stuffToWrite="+fileContents">http://localhost/writeFile.php?&stuffToWrite=”+fileContents);

}
[/AS]
The calling sequence is
XMLDoc.onLoad = fnloaded;
OnUpdate();
I dont know why I am getting in trace(“Third:”+_global.productname[3]); as undefined!!
Please help!
Thanks