/*
** This file was created by Cuesta Systems Inc, October 2004
** It contains common functions for joins
** Created By: Monica Socol, Cuesta Systems
** On: Oct 7, 2004
*/

/*
** Changes history:
**  
*/

//local var for loading xml documents
var xmlDocArray =  new Array();
var xmlDoc;

// Constructor for a path,doc pair
function docObject(path, oDoc){
  this.path = path;
  this.oDoc = oDoc;
}

/*
** Lookup and return the document if it's set, otherwise return null.
** Written by Josh Hevenor, February 2007
*/
function getXmlDoc(path){
  for(docNum in xmlDocArray){
    if(xmlDocArray[docNum].path == path){
      return xmlDocArray[docNum].oDoc;
    }
  }
  return null;
}

/*
** Store the couple (path, oDoc) in xmlDocArray
** Written by Josh Hevenor, February 2007
*/
function setXmlDoc(path, oDoc){
  if(getXmlDoc(path) == null){
    // document isn't saved yet
    docObj = new docObject(path, oDoc);
    xmlDocArray.push(docObj);
  }
}

/************************************************************************
** This function loads a generic xml file into xmlDoc
** Created By: Monica Socol, October 2004
** Modified By: jh, feb 2007
*/ 
function csy_loadXMLFile(fileLoad)
{
 xmlDoc = getXmlDoc(fileLoad)        // Check for a cached copy
 if(xmlDoc != null) return xmlDoc;   // return it if we have it
 
 if (window.ActiveXObject)
	{
		xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.async=false;
		var bool = xmlDoc.load(fileLoad);
		if (bool == false)
		{
		   //AD May 24, 2005 - updated message to refer to connection file
		   alert("Cuesta: The connection file does not exist or is not valid for the application.\n        Please review the file!");
		   return;
		}

		if (xmlDoc.parseError.errorCode != 0)
		{
		      msg = 'Error loading data file.';
		      msg += '\nDescription: ' + xmlDoc.parseError.reason;
		      msg += '\nSource text: ' + xmlDoc.parseError.srcText;
		      msg += '\nLine: ' + xmlDoc.parseError.line ;
		      msg += '\nChar: ' + xmlDoc.parseError.linePos;

		      alert(msg);		  
		      return;
		}		
	}
	else
	{
			var myXMLHTTPRequest = new XMLHttpRequest();
			myXMLHTTPRequest.open("GET", fileLoad, false);
			myXMLHTTPRequest.send(null);
			xmlDoc = myXMLHTTPRequest.responseXML;
  }
  // Cache our xmlDoc
  setXmlDoc(fileLoad, xmlDoc);
}

/************************************************************************
** This function search a value into an xml
** Created By: Monica Socol, October 2004
** Modified By: 
*/ 
function searchXMLFile (fileLoad, searchWord)
{
	csy_loadXMLFile(fileLoad);
	var childs = xmlDoc.documentElement.getElementsByTagName(searchWord);

	if(childs.length > 0)
	{        
	   if(childs[0].firstChild != null)      
		  return childs[0].firstChild.nodeValue; 
	} return "";
}

/************************************************************************
** This function search a value into an xml
** Created By: Monica Socol, October 2004
** Modified By: 
*/ 
function searchXMLFileFromParent (searchWord, searchParent)
{
	var childs = searchParent.getElementsByTagName(searchWord);
	if(childs.length > 0)
	{  
	   if(childs[0].firstChild != null)      	   	   
		  return childs[0].firstChild.nodeValue; 		
	} return "";
}


/************************************************************************
** This function search a value into an xml
** Created By: Monica Socol, October 2004
** Modified By: 
*/ 
function getXMLValue(fileLoad, searchWord, searchParent)
{
	csy_loadXMLFile(fileLoad);
	var val = "";		

	var xmlnodes = xmlDoc.documentElement.childNodes;
	if (xmlnodes.length > 0)
	{   
		for (var i=0; i<xmlnodes.length; i++)
		{
			if (xmlnodes.item(i).nodeName == "connection")			
				if (xmlnodes.item(i).attributes.length>0)													
					if (xmlnodes.item(i).attributes.item(0).nodeValue == searchParent)						
						val = searchXMLFileFromParent(searchWord, xmlnodes.item(i));											
		}
	}	
	return val;
}
