var g_chapter = null;

var ChapterMode_Content = 0;
var ChapterMode_Quiz = 1;
var ChapterMode_Other = 2;

function Pages()
{
	this.arr = [];
	this.nCurrentIndex = -1;
}

Pages.prototype.add = function(page)
{
	this.arr.push(page);
}

Pages.prototype.isFirst = function()
{
	return this.nCurrentIndex == 0;
}

Pages.prototype.isLast = function()
{
	return this.nCurrentIndex == this.arr.length - 1;
}

Pages.prototype.isValidIndex = function(nIndex)
{
	return nIndex >= 0 && nIndex < this.arr.length;
}

Pages.prototype.getCount = function()
{
	return this.arr.length;
}

Pages.prototype.getCurrent = function()
{
	if(this.nCurrentIndex < 0 || this.nCurrentIndex > this.arr.length - 1)
	{
		return null;
	}	
	
	return this.arr[this.nCurrentIndex];
}

Pages.prototype.getCurrentIndex = function()
{
	return this.nCurrentIndex;
}

Pages.prototype.setCurrentIndex = function(nIndex)
{
	this.nCurrentIndex = nIndex;
}

Pages.prototype.getPage = function(nIndex)
{
	if(nIndex < 0 || nIndex > this.arr.length - 1)
	{
		return null;
	}	
	
	return this.arr[nIndex];	
}

function Chapter(strNavUrl, strContentFrame, fnNavReady, fnContentLoaded)
{
	this.url = strNavUrl;
	this.pages = new Pages();
	this.quizzes = new Pages();
	this.fScore = 0;
	this.maleaudiodir="";
	this.femaleaudiodir="";
	this.sectionID = "";
	this.unitName = "";
	this.chapterName = "";
	this.helpId = -1;
	this.pageCount = 0;
	
	this.nMode = ChapterMode_Content;

	this.oConfig = null;
	
	this.isReady = false;
	this.strContentFrame = strContentFrame;
	this.bHandlerAttached = false;
	this.fnContentLoaded = fnContentLoaded;
	
	this.bReuse = false;
	this.bUseChapterIndex = true;
	
	this.bCompleted = false;
	
	g_chapter = this;

	if(strNavUrl != null)
	{
	    //alert("url: " + strNavUrl);

		if(window.location.protocol == "http:" || window.location.protocol == "https:")
		{
			var xmlhttp = new XMLHttpRequest();
			xmlhttp.open("GET", strNavUrl);
																									
			xmlhttp.onreadystatechange = function()
				{
					if(xmlhttp.readyState == 4)
					{
						if(xmlhttp.status == 200)
						{
							g_chapter.__load(xmlhttp.responseXML);	
							g_chapter.isReady = true;
							
							if(fnNavReady != null)
							{
								fnNavReady(g_chapter);
							}
						}
					}
				};
			xmlhttp.send(null);
		}
		else
		{
			var oDomDoc = Sarissa.getDomDocument();
			oDomDoc.async = false;

			if(oDomDoc.load(strNavUrl))
			{
				this.__load(oDomDoc);
				g_chapter.isReady = true;
				if(fnNavReady != null)
				{
					fnNavReady(g_chapter);
				}
			}
		}
	}
}

Chapter.prototype.__load = function(doc)
{
	Sarissa.setXpathNamespaces(doc, "xmlns:a='http://www.eoir.com'");

	try
	{
		this.oConfig = doc.selectSingleNode("/a:chapter/a:config");
		this.maleaudiodir = getNavParamValue(this.oConfig, "maleaudiodir", "");
		this.femaleaudiodir = getNavParamValue(this.oConfig, "femaleaudiodir", "");
		this.sectionID = getNavParamValue(this.oConfig, "section_id", "");
		this.unitName = getNavParamValue(this.oConfig, "unit", "");
		this.chapterName = getNavParamValue(this.oConfig, "chapter", "");
		this.title = getNavParamValue(this.oConfig, "title", null);

		try
		{
			this.helpId = parseInt(getNavParamValue(this.oConfig, "helpid", -1));
		}
		catch(e) { }


		var pagesNode = doc.selectSingleNode("/a:chapter/a:pages");
		if(pagesNode != null)
		{
			var bChapIndex = pagesNode.getAttribute("chapterindex");
			if(bChapIndex != null)
			{
				this.bUseChapterIndex = EOIR.Utils.parseBool(bChapIndex);
			}
		}

		var pageNodes = doc.selectNodes("/a:chapter/a:pages/a:page");
		for(var i = 0; i < pageNodes.length; i++)
		{
		    this.pageCount = pageNodes.length;

			this.__addPage(pageNodes[i]);
		}

		var quizzesNode = doc.selectSingleNode("/a:chapter/a:quizzes");
		if(quizzesNode != null)
		{
		    var oQuizConfig = quizzesNode.selectSingleNode("a:config");
		    
		    var strQuizUrl = getNavParamValue(oQuizConfig, "url", null);
		    var quizNodesFromUrl = new Array();
		    if(strQuizUrl != null)
		    {
		        // Add Quizzes from another URL
		        var xml = EOIR.XML.load(EOIR.Utils.getAbsolutePath(strQuizUrl), false, null, "xmlns:a='http://www.eoir.com'");
		        if(xml != null)
		        {
		            var strNode = getNavParamValue(oQuizConfig, "node", "quiz");
		            var nodeQuizzes = xml.selectNodes(strNode);
		            if(nodeQuizzes != null && nodeQuizzes.length > 0)
		            {
		                quizNodesFromUrl = nodeQuizzes;
		            }
		        }	
		    }
		    
			var quizNodes = quizzesNode.selectNodes("./a:quiz");

			var bRandomize = quizzesNode.getAttribute("randomize");
			if(bRandomize == null)
			{
				bRandomize = false;
			}
			else
			{
				bRandomize = EOIR.Utils.parseBool(bRandomize);
			}

			var nMaxQuestions = quizzesNode.getAttribute("maxquestions");
			if(nMaxQuestions == null)
			{
				nMaxQuestions = quizNodes.length + quizNodesFromUrl.length;
			}

			if(bRandomize)
			{
				var arrQuizzes = [];
				// We may have questions that need to be asked.
				var quizNodeLast = quizzesNode.selectNodes("./a:quiz[a:param[@name='ensurelast' and @value = '1'] ]");
				if(nMaxQuestions < (quizNodes.length+quizNodesFromUrl.length))
				{
					var quizNodesEnsure = quizzesNode.selectNodes("./a:quiz[a:param[@name='ensureasked' and @value = '1'] ]");
					//var quizNodesExtra = quizzesNode.selectNodes("./a:quiz[not(a:param[@name='ensureasked' and @value = '1']) ]");
					var quizNodesExtra = quizzesNode.selectNodes("./a:quiz[not(a:param[@name='ensureasked' and @value = '1']) and not(a:param[@name='ensurelast' and @value = '1']) ]");

					addNodesToArray(quizNodesEnsure, arrQuizzes);
					quizNodesExtra = EOIR.Utils.randomizeArray(quizNodesExtra);
					addNodesToArray(quizNodesExtra, arrQuizzes);
                    quizNodesFromUrl = EOIR.Utils.randomizeArray(quizNodesFromUrl);
					addNodesToArray(quizNodesFromUrl,arrQuizzes);
					
					arrQuizzes.length = nMaxQuestions;
				}
				else
				{
					var quizNodesNotLast = quizzesNode.selectNodes("./a:quiz[not(a:param[@name='ensurelast' and @value = '1']) ]");
					addNodesToArray(quizNodesNotLast, arrQuizzes);
					addNodesToArray(quizNodesFromUrl, arrQuizzes);
				}

				arrQuizzes = EOIR.Utils.randomizeArray(arrQuizzes);

				if(quizNodeLast != null)
					addNodesToArray(quizNodeLast, arrQuizzes);

				for(var i = 0; i < arrQuizzes.length; i++)
				{
					this.__addQuiz(arrQuizzes[i]);
				}
			}
			else
			{
				var arrQuizzes = [];
			    addNodesToArray(quizNodesFromUrl, arrQuizzes);
			    addNodesToArray(quizNodes, arrQuizzes);
			    
				for(var i = 0; i < nMaxQuestions; i++)
				{
					this.__addQuiz(arrQuizzes[i]);
				}	
			}
		}
	}
	catch(e)
	{
	}
}

Chapter.prototype.initialize = function ()
{
	if(this.oConfig != null)
	{
		var frm = document.getElementById(this.strContentFrame);

		try
		{
			var strBkgImage = this.getParam("bkgimage");
			if(strBkgImage != null)
			{
				frm.style.backgroundImage = "url('" + strBkgImage + "')";

				var strBkgRepeat = this.getParam("bkgrepeat", "no-repeat");
				if(strBkgRepeat != null)
				{
					frm.style.backgroundRepeat = strBkgRepeat;
				}

				var strBkgSize = this.getParam("bkgsize");
				if(strBkgSize != null)
				{
					YAHOO.util.Dom.setStyle(frm, "background-size", strBkgSize);
					YAHOO.util.Dom.setStyle(frm, "-moz-background-size", strBkgSize);
					YAHOO.util.Dom.setStyle(frm, "-o-background-size", strBkgSize);
					YAHOO.util.Dom.setStyle(frm, "-webkit-background-size", strBkgSize);
					YAHOO.util.Dom.setStyle(frm, "-khtml-background-size", strBkgSize);
				}
			}
		}
		catch(e)
		{
		}

		try
		{
			var strNavPosition = this.getParam("contentposition");
			if(strNavPosition != null)
			{
				var arrPos = strNavPosition.split(",");
				frm.style.left = arrPos[0] + "px";
				frm.style.top = arrPos[1] + "px";
				frm.style.width = arrPos[2] + "px";
				frm.style.height = arrPos[3] + "px";
			}
		}
		catch(e)
		{
		}
	}
}

Chapter.prototype.getParam = function(strParamName, strDefaultValue)
{
	if(this.oConfig != null)
	{
		return getNavParamValue(this.oConfig, strParamName, strDefaultValue);
	}
	
	return null;
}

Chapter.prototype.getMode = function()
{
	return this.nMode;
}

Chapter.prototype.getTitle = function () {
    return this.title;
}

Chapter.prototype.getRealPageCount = function () {
    return this.pageCount;
}

Chapter.prototype.setMode = function(nMode)
{
	if(this.nMode != nMode)
	{
		this.nMode = nMode;
		switch(this.nMode)
		{
			case ChapterMode_Content:
				this.gotoPage(this.pages.getCurrentIndex());
				break;
				
			case ChapterMode_Quiz:
				this.gotoQuiz(0);
				break;
		}
	}
}

Chapter.prototype.isContentMode = function()
{
	return this.getMode() == ChapterMode_Content;
}

Chapter.prototype.isQuizMode = function()
{
	return this.getMode() == ChapterMode_Quiz;
}

Chapter.prototype.__addPage = function(page)
{
    var strHeader = getNavParamValue(page, "header"); //if a page has its own header, we treat it like it's part of a different chapter
    if(strHeader == null)
    {
	    var strUrl = getNavParamValue(page, "url");
	    if(strUrl != null)
	    {
		    var bGraphic = EOIR.Utils.parseBool(getNavParamValue(page, "graphic", "0"));
		    var bEmbeddedQuiz = EOIR.Utils.parseBool(getNavParamValue(page, "embeddedquiz", "0"));
		    var audiofile = getNavParamValue(page, "audio", null);
		    var refinfo = getNavParamValue(page, "refinfo", null);
		    var audioDelay = parseInt(getNavParamValue(page, "delayaudio", "0"));
		    var helpId = parseInt(getNavParamValue(page, "helpid", this.helpId));

		    this.pages.add(new NavPage(strUrl, getNavParamValue(page, "title"), page, bGraphic, bEmbeddedQuiz, audiofile, refinfo, audioDelay, helpId));
	    }
	}
}

Chapter.prototype.__addQuiz = function(quiz)
{
	var strUrl = getNavParamValue(quiz, "url");

	if(strUrl != null)
	{
		this.quizzes.add(new NavPage(strUrl, getNavParamValue(quiz, "title"), quiz,false,false,"","",0,-1));
	}
}

Chapter.prototype.isFirstPage = function()
{
	try
	{
		var frm = window.frames[this.strContentFrame];
		if(frm.isFirstPage != null)
		{
			return frm.isFirstPage();
		}
	}
	catch(e)
	{
	}


	return this.pages.isFirst();
}

Chapter.prototype.isLastPage = function()
{
	try
	{
		var frm = window.frames[this.strContentFrame];
		if(frm.isLastPage != null)
		{
			return frm.isLastPage();
		}
	}
	catch(e)
	{
	}

	return this.pages.isLast();
}

Chapter.prototype.getNumPages = function()
{
    //return this.pages.getCount();
    return this.getRealPageCount();
}

Chapter.prototype.getCurrentPage = function()
{
	if(this.pages.getCurrentIndex() < 0 || this.pages.getCurrentIndex() > this.pages.getCount() - 1)
	{
		return null;
	}	
	
	return this.pages.getPage(this.pages.getCurrentIndex());
}

Chapter.prototype.getCurrentPageIndex = function()
{
	return this.pages.getCurrentIndex();
}

Chapter.prototype.gotoNextPage = function()
{
	var frm = window.frames[this.strContentFrame];
	var bHasNextPage = !(this.pages.getCurrentIndex() < 0 || this.pages.getCurrentIndex() >= this.pages.getCount() - 1);
	try
	{
		if(frm.hasNextPage != null)
		{
			bHasNextPage = bHasNextPage || frm.hasNextPage();
		}
	}
	catch(e)
	{
	}

	if(!bHasNextPage)
	{
		return false;
	}

    var user = EOIR.User.get();
	
	if(!this.pages.getCurrent().bEmbeddedQuiz || !EOIR.User.isDemoUser(user))
	{
	    try
	    {
	        var frmMetas = frm.document.getElementsByTagName("meta");
	        if(frmMetas != null)
	        {
	            var bContinue = true;
	            for(var i = 0; i < frmMetas.length && bContinue; i++)
	            {
	                if(frmMetas[i].name == "ProgId" && frmMetas[i].content == "PowerPoint.Slide")
	                {
	                    if(frm.frames.length >= 1)
	                    {
	                        frm = frm.frames[0];
	                        bContinue = false;
	                    }
	                }
	            }
	        }
	    }
	    catch(e)
	    {
	    }

		try
		{
			if(frm.isComplete != null)
			{
				if(!frm.isComplete())
				{
					if(frm.completePage != null)
					{
						frm.completePage();
						return false;
					}
				}
			}
		}
		catch(e)
		{
		}

		try
		{
			if(frm.beforeNextPage != null)
			{
				if(!frm.beforeNextPage())
				{
					return false;
				}
			}
		}
		catch(e)
		{
		}
	}
	var nPage = this.pages.getCurrentIndex() + 1;
	var bEditContent = EOIR.Utils.getProperty("editcontent", false);
	while(this.pages.getPage(nPage).bGraphic && bEditContent && nPage < this.pages.getCount() - 1)
	{
		nPage++;
	}
	return this.gotoPage(nPage);
}

Chapter.prototype.gotoPrevPage = function()
{
	var bHasPrevPage = !(this.pages.getCurrentIndex() < 1 || this.pages.getCurrentIndex() > this.pages.getCount() - 1);
	try
	{
		if(frm.hasPrevPage != null)
		{
			bHasPrevPage = bHasPrevPage || frm.hasPrevPage();
		}
	}
	catch(e)
	{
	}

	if(!bHasPrevPage)
	{
		return false;
	}

	try
	{
		var frm = window.frames[this.strContentFrame];
		if(frm.beforePrevPage != null)
		{
			if(!frm.beforePrevPage())
			{
				return false;
			}
		}
	}
	catch(e)
	{
	}

	var nPage = this.pages.getCurrentIndex() - 1;
	var bEditContent = EOIR.Utils.getProperty("editcontent", false);
	while(this.pages.getPage(nPage).bGraphic && bEditContent && nPage > 0)
	{
		nPage--;
	}

	return this.gotoPage(nPage);
}

Chapter.prototype.gotoPage = function(nPage)
{
	if(this.pages.isValidIndex(nPage))
	{
		var frm = window.frames[this.strContentFrame];		
		var user = EOIR.User.get();
	/*	if(this.pages.getPage(nPage).bEmbeddedQuiz && !isDemoUser(user))
		{
		    if(frm.completePage != null)
		    {
			    frm.completePage();
		    }
		}*/
		
		if(frm.onLeavePage != null)
		{
		    frm.onLeavePage();
		}
		
		// track of what units/chapters have been completed 
		/* need to rethink this
		if(!isDemoUser(user))
	    {
	        var strProperty = this.buildPropertyString() + "_highestpage";
	        var nHighestPage = getUserProperty(user, strProperty,0);
	        if(this.pages.getCurrentIndex()+1>nHighestPage)
	        {
	                user[strProperty]=this.pages.getCurrentIndex()+1;
	        }
	        
	    }
	    else
	    {
            if(user == null)
            {
                user = createDemoUser();
                setProperty("User", user);
            }
            var strProperty = this.buildPropertyString() + "_highestpage";
            var nHighestPage = getUserProperty(user,strProperty,0);
            if(nPage+1>nHighestPage)
            {
                user[strProperty] = nPage+1;
            }
	    }*/
	
		this.pages.setCurrentIndex(nPage);
		
		this.pages.getPage(this.pages.getCurrentIndex()).bVisited = true;
		
		if(this.pages.getPage(this.pages.getCurrentIndex()).title != null)
		{
			document.title = this.pages.getPage(this.pages.getCurrentIndex()).title;
		}
	
		if(!this.bHandlerAttached)
		{
			this.bHandlerAttached = true;
			YAHOO.util.Event.addListener(document.getElementById(this.strContentFrame), "load", __onFrameLoad);
		}
		
		document.getElementById(this.strContentFrame).src = this.pages.getPage(this.pages.getCurrentIndex()).url;
		return true;
	}
	else
	{
		return false;
	}
}

Chapter.prototype.isFirstQuiz = function()
{
	return this.quizzes.isFirst();
}

Chapter.prototype.isLastQuiz = function()
{
	return this.quizzes.isLast();
}

Chapter.prototype.isLast = function()
{
    switch(this.getMode())
    {
     case ChapterMode_Content:
        return this.pages.isLast();						
     case ChapterMode_Quiz:
        return this.quizzes.isLast();
     default:
        return false;    
    }
}

Chapter.prototype.getNumQuizzes = function()
{
	return this.quizzes.getCount();
}

Chapter.prototype.getCurrentQuiz = function()
{
	if(this.quizzes.getCurrentIndex() < 0 || this.quizzes.getCurrentIndex() > this.quizzes.getCount() - 1)
	{
		return null;
	}	
	
	return this.quizzes.getPage(this.quizzes.getCurrentIndex());
}

Chapter.prototype.getCurrentQuizIndex = function()
{
	return this.quizzes.getCurrentIndex();
}

Chapter.prototype.gotoNextQuiz = function()
{	
	if(this.quizzes.getCurrentIndex() < 0 || this.quizzes.getCurrentIndex() >= this.quizzes.getCount() - 1)
	{
		return false;
	}
    
	var frm = window.frames[this.strContentFrame];

	var user = EOIR.User.get();
	if(!EOIR.User.isDemoUser(user))
	{
	    if(frm.isComplete != null)
	    {
		    if(!frm.isComplete())
		    {
			    if(frm.completePage != null)
			    {
				    frm.completePage();
				    return false;
			    }
		    }			
	    }
	}
    if (frm.pageNextHandler != null)
    {
        frm.pageNextHandler(this.quizzes.getCurrentIndex() + 1);
        return true;
    }
    else       
	    return this.gotoQuiz(this.quizzes.getCurrentIndex() + 1);
}

Chapter.prototype.gotoPrevQuiz = function ()
{
	if(this.quizzes.getCurrentIndex() < 1 || this.quizzes.getCurrentIndex() > this.quizzes.getCount() - 1)
	{
		return false;
	}

	return this.gotoQuiz(this.quizzes.getCurrentIndex() - 1);
}

Chapter.prototype.gotoQuiz = function(nPage)
{
	if(this.quizzes.isValidIndex(nPage))
	{
		var frm = window.frames[this.strContentFrame];		
		//check if is demo
		var user = EOIR.User.get();
	    if(!EOIR.User.isDemoUser(user))	
	    {
		    if(frm.completePage != null)
		    {
			    frm.completePage();
		    }
		}
		if(frm.scorePage != null)
		{
			this.fScore += frm.scorePage();
		}
		
		var nPrevIndex = this.quizzes.getCurrentIndex();
		var strPrevUrl = nPrevIndex >= 0 ? this.quizzes.getPage(nPrevIndex).url : null;
		
		this.quizzes.setCurrentIndex(nPage);
		
		if(this.quizzes.getPage(this.quizzes.getCurrentIndex()).title != null)
		{
			document.title = this.quizzes.getPage(this.quizzes.getCurrentIndex()).title;
		}
	
		if(!this.bHandlerAttached)
		{
			this.bHandlerAttached = true;
			YAHOO.util.Event.addListener(document.getElementById(this.strContentFrame), "load", __onFrameLoad);
		}
		
		if(this.bReuse && this.quizzes.getPage(this.quizzes.getCurrentIndex()).url == strPrevUrl)
		{  
		    __onFrameLoad();
		}
		else
		{  
		    document.getElementById(this.strContentFrame).src = this.quizzes.getPage(this.quizzes.getCurrentIndex()).url;
		}
		   
		return true;
	}
	else
	{   
		return false;
	}
}

Chapter.prototype.completed = function()
{
	switch(this.getMode())
	{
		case ChapterMode_Quiz:
			var frm = window.frames[this.strContentFrame];	
			var user = EOIR.User.get();
	        if(!EOIR.User.isDemoUser(user))	
	        {		
			    if(frm.completePage != null)
			    {
				    frm.completePage();
			    }
			}
			if(frm.scorePage != null)
			{
				this.fScore += frm.scorePage();
			}			
			break;
	}
	
	try
	{
	    var strProperty = this.buildPropertyString();
	    var user = EOIR.User.get();
        if(user!=null)	
        {		
            user.setProperty(strProperty, "1");
		}
	}
	catch(e){}
	
	this.bCompleted = true;
}

Chapter.prototype.__onFrameLoaded = function()
{
	var content = null;
	switch(this.nMode)
	{
		case ChapterMode_Content:
			content = this.getCurrentPage();
			break;

		case ChapterMode_Quiz:
			content = this.getCurrentQuiz();
			break;
	}

	if(content != null)
	{
		try
		{
			//alert(window.frames[this.strContentFrame].document.title);
			if(window.frames[this.strContentFrame].initializeFromParameters != null)
			{
				window.frames[this.strContentFrame].initializeFromParameters(content.pageNode);
			}
		}
		catch(e)
		{
		}

		try
		{
		    var helpid = null;
		    if(window.frames[this.strContentFrame].getHelpId != null)
			{
				helpid = window.frames[this.strContentFrame].getHelpId();
			}
			if(helpid == null)
			{
			    helpid = content.helpId;
			}
			EOIR.Help.setId(helpid);
		}
		catch(e)
		{
		}

		try
		{
			if(this.fnContentLoaded != null)
			{
				this.fnContentLoaded(content);
			}
		}
		catch(e)
		{
		}
	}
}

Chapter.prototype.buildPropertyString = function()
{
    var strProperty = "";
    if(this.unitName != "")
    {
        strProperty += this.unitName + "_" + this.chapterName;
    }
    else if(this.chapterName != "")
    {
        strProperty += this.chapterName;
    }
    return "chapter" + strProperty.toLowerCase();
}

function __onFrameLoad()
{
	g_chapter.__onFrameLoaded();
}

function NavPage(strUrl, strTitle, pageNode, bGraphic, bEmbeddedQuiz, audiofile, refinfo, audioDelay, helpId) 
{
	this.url = strUrl;
	this.title = strTitle;
	this.pageNode = pageNode;
	this.bGraphic = bGraphic;
	this.bEmbeddedQuiz = bEmbeddedQuiz;
	this.audiofile = audiofile;
	this.audioDelay = audioDelay;
	this.helpId = helpId;
	this.refinfo = refinfo;
	this.completed = false;
};

NavPage.prototype.isComplete = function()
{
	return this.completed;
}

NavPage.prototype.complete = function()
{
	this.completed = true;
}

function getParamValue(nodeParam)
{
	if(nodeParam != null)
	{
		var strType = nodeParam.getAttribute("type");
		switch(strType)
		{
			case "text":
				return serializeChildren(nodeParam);
			
			case "html":
				return unescape(serializeChildren(nodeParam));
			
			case "xml":
				return nodeParam;
			
			default:
				return nodeParam.getAttribute("value");				
		}
	}
}

function getParamID(nodeParam)
{
    if(nodeParam != null)
    {
	    return nodeParam.getAttribute("id");				
    }
}
  

function getNavParam(node, strParamName)
{   
    if(node != null)
	    return node.selectSingleNode("./a:param[@name='" + strParamName + "']");
	else
	    return null;
}

function getNavParams(node, strParamName)
{
    if(node != null)
	    return node.selectNodes("./a:param[@name='" + strParamName + "']");
	else
	    return null;
}

function getNavParamValue(node, strParamName, strDefaultValue)
{
	try
	{
		var nodeParam = getNavParam(node, strParamName);
		if(nodeParam != null)
		{
			return getParamValue(nodeParam);
		}
	}
	catch(e)
	{
	}
				
	return strDefaultValue;
}

function addNodesToArray(nodes, arr)
{
    for(var i = 0; i < nodes.length; i++)
	{
		arr.push(nodes[i]);
	}				
}
