var INSERT=0;
var DELETE=1;
var UPDATE=2;
var QUERY=3;

// 
// Class UserCfgXML:  Objects of this class wrap an XML document which holds users information from Config database
//                                      The object manages modifications to XML documents and facilitates insertion of elements to the
//                                      document. Also it provides methods for reading field values from XML document.
//
//                 The XML document has the following format:
//          
//					<USER USER_ID="">
//						<LAST_NAME></LAST_NAME>
//						<FIRST_NAME></FIRST_NAME>
//						<MIDDLE_NAME></MIDDLE_NAME>
//						<NAME_PREFIX></NAME_PREFIX>
//						<NAME_SUFFIX></NAME_SUFFIX>
//						<PASSWORD></PASSWORD>
//						<PW_CREATION_DATE></PW_CREATION_DATE>
//						<ROLE_ID></ROLE_ID>
//						<ACTIVE>yes</ACTIVE>
//						<PW_FAIL_ATTEMPTS></PW_FAIL_ATTEMPTS>
//						<GROUP></GROUP>
//						<DBSOURCE></DBSOURCE>
//						<ARCHIVETYPE></ARCHIVETYPE>
//						<USER_PATIENT_ONLY></USER_PATIENT_ONLY>
//						<COMPRESSION></COMPRESSION>
//						<QUALITY_FACTOR></QUALITY_FACTOR>
//						<CURR_STUDIES></CURR_STUDIES>
//						<FROM_LAST_VALUE></FROM_LAST_VALUE>
//						<FROM_LAST_UNIT></FROM_LAST_UNIT>
//						<MODALITIES></MODALITIES>
//						<RELNOTES></RELNOTES>
//						<SHUTDOWN_SERVICES></SHUTDOWN_SERVICES>
//						<J2K_COMPRESSION_RATIO_STR></J2K_COMPRESSION_RATIO_STR>
//						<LIST>
//							<COLS></COLS>
//							<MY_PATIENTS_ONLY></MY_PATIENTS_ONLY>
//							<FROM_LAST_VALUE></FROM_LAST_VALUE>
//							<FROM_LAST_UNIT></FROM_LAST_UNIT>
//							<LOCATION></LOCATION>
//						</LIST>
//						<PREFERENCESXMLDOC>
//							<PREFERENCES></PREFERENCES>
//						</PREFERENCESXMLDOC>
//					</USER>
//
//     
//  Class Methods:
//		getFieldValue
//		setFieldValue
//  	getXMLDoc 
//      loadXMLDoc
//


// 
// UserCfgXML: Constructs an object of type userCfgXML
//    Parameters:
//	Input: 
//	          userID: User ID of the user whose XML document is being created. Should be of type string.
//
function UserCfgXML(userID)
{
	this.getFieldValue = userGetFieldValue;
	this.setFieldValue = userSetFieldValue;
	this.getXMLDoc = userGetXMLDoc;
	this.loadXMLDoc = userLoadXMLDoc;
	
	// If the input parameter is an string for UserID do the following
	this.m_xmlUserDoc = new ActiveXObject( "Microsoft.XMLDOM" );	
	var xmlUserNode = this.m_xmlUserDoc.createElement("USER");
	xmlUserNode.setAttribute("USER_ID",  userID);
	this.m_xmlUserDoc.appendChild(xmlUserNode);
}


// 
// userGetXMLDoc: Returns the XMLDocument object containing the XML
//
function userGetXMLDoc()
{
	return this.m_xmlUserDoc;
}

//
// userLoadXMLDoc: Set the XML document to a previously created XML document
//
function userLoadXMLDoc(xmlDoc)
{
	this.m_xmlUserDoc = xmlDoc;
}

//
// userGetFieldValue: Returns the value of an XML element with the tag strTagName
//    Parameters:
//         Input: 
//               strTagname: The tag name 
//
function userGetFieldValue(strTagName)
{
	if (strTagName == "USER_ID")
		return this.m_xmlUserDoc.documentElement.getAttribute("USER_ID");	

	var xmlFieldNode = this.m_xmlUserDoc.documentElement.selectSingleNode("//USER/" + strTagName);
	if (xmlFieldNode != null)
		return xmlFieldNode.text;
	else 
		return null;
		
}
//
// userSetFieldValue: Sets the value of an XML element with the tag strTagName
//    Parameters:
//         Input: 
//               strTagname: The tag name 
//               fieldValue: The text of the element in the XML document.
//
function userSetFieldValue(strTagName, fieldValue)
{
	var xmlFieldNode = this.m_xmlUserDoc.documentElement.selectSingleNode("//USER/" + strTagName);
	if (xmlFieldNode == null)
	{
		var index = strTagName.indexOf("/");
		if (index > -1)
		{
			xmlParentNode = xmlUserDoc.documentElement.selectSingleNode("//USER/" + strTagName.substr(0, index))
			if (null == xmlParentNode)	
			{
				xmlParentNode = this.m_xmlUserDoc.createElement(strTagName.substr(0, index));
				this.m_xmlUserDoc.documentElement.appendChild(xmlParentNode);
			}
			var xmlFieldNode = this.m_xmlUserDoc.createElement(strTagName.substr(index+1, strTagName.length() - index));
			xmlParentNode.appendChild(xmlFieldNode);					
		}
		else
		{
			xmlFieldNode = this.m_xmlUserDoc.createElement(strTagName);
			this.m_xmlUserDoc.documentElement.appendChild(xmlFieldNode);
		}
	}
	
	if (fieldValue != null)
		xmlFieldNode.text = fieldValue;	
}

// 
// Class GroupCfgXML:  Objects of this class wrap an XML document which holds groups information from Config database
//                     The object manages modifications to XML documents and facilitates insertion of elements to the
//                     document. Also it provides methods for reading field values from XML document.
//
//                 The XML document has the following format:
//          
//					<GROUP GROUP_NAME="">
//					</GROUP>
//
//     
//  Class Methods:
//		getFieldValue
//		setFieldValue
//  	getXMLDoc 
//      loadXMLDoc
//


// 
// GroupCfgXML: Constructs an object of type groupCfgXML
//    Parameters:
//	Input: 
//	          groupName: Group name of the group whose XML document is being created. Should be of type string.
//
function GroupCfgXML(groupName)
{
	this.getFieldValue = groupGetFieldValue;
	this.setFieldValue = groupSetFieldValue;
	this.getXMLDoc = groupGetXMLDoc;
	this.loadXMLDoc = groupLoadXMLDoc;
	
	// If the input parameter is an string for GroupName do the following
	this.m_xmlGroupDoc = new ActiveXObject( "Microsoft.XMLDOM" );	
	var xmlGroupNode = this.m_xmlGroupDoc.createElement("GROUP");
	xmlGroupNode.setAttribute("GROUP_NAME",  groupName);
	this.m_xmlGroupDoc.appendChild(xmlGroupNode);
}


// 
// groupGetXMLDoc: Returns the XMLDocument object containing the XML
//
function groupGetXMLDoc()
{
	return this.m_xmlGroupDoc;
}

//
// groupLoadXMLDoc: Set the XML document to a previously created XML document
//
function groupLoadXMLDoc(xmlDoc)
{
	this.m_xmlGroupDoc = xmlDoc;

}

//
// groupGetFieldValue: Returns the value of an XML element with the tag strTagName
//    Parameters:
//         Input: 
//               strTagname: The tag name 
//		<GROUP GROUP_NAME=" ">
//			<DESC></DESC>
//			<PASSWORD></PASSWORD>
//			<SITES>
//				<SITE></SITE>
//				<SITE></SITE>
//				<SITE></SITE>
//			</SITES>
//		</GROUP>
//
//
//
function groupGetFieldValue(strTagName)
{
	if (strTagName == "GROUP_NAME")
		return this.m_xmlGroupDoc.documentElement.getAttribute("GROUP_NAME");	

	if (strTagName == "ACTION")
		return this.m_xmlGroupDoc.documentElement.getAttribute("ACTION");	

	if (strTagName == "ADMIN_GROUPID")
		return this.m_xmlGroupDoc.documentElement.getAttribute("ADMIN_GROUPID");	

	var xmlFieldNode = this.m_xmlGroupDoc.documentElement.selectSingleNode("//GROUP/" + strTagName);
	if (xmlFieldNode != null)
		return xmlFieldNode.text;
	else 
		return null;
		
}
//
// groupSetFieldValue: Sets the value of an XML element with the tag strTagName
//    Parameters:
//         Input: 
//               strTagname: The tag name 
//               fieldValue: The text of the element in the XML document.
//
//		<GROUP GROUP_NAME=" ">
//			<DESC></DESC>
//			<PASSWORD></PASSWORD>
//			<SITES>
//				<SITE></SITE>
//				<SITE></SITE>
//				<SITE></SITE>
//			</SITES>
//		</GROUP>
//
//  change the logic, if there is "/", keep appending to the end instead of modifing
//
function groupSetFieldValue(strTagName, fieldValue)
{
	var xmlFieldNode = this.m_xmlGroupDoc.documentElement.selectSingleNode("//GROUP/" + strTagName);
	var index = strTagName.indexOf("/");

	if (xmlFieldNode == null || index > -1)
	{	
		if (index > -1)
		{	
			var xmlParentNode = this.m_xmlGroupDoc.documentElement.selectSingleNode("//GROUP/" + strTagName.substr(0, index))
			if (null == xmlParentNode)	
			{			
				xmlParentNode = this.m_xmlGroupDoc.createElement(strTagName.substr(0, index));
				this.m_xmlGroupDoc.documentElement.appendChild(xmlParentNode);
			}			
			xmlFieldNode = this.m_xmlGroupDoc.createElement(strTagName.substr(index+1, strTagName.length - index));			
			xmlParentNode.appendChild(xmlFieldNode);					
		}
		else
		{		
			xmlFieldNode = this.m_xmlGroupDoc.createElement(strTagName);
			this.m_xmlGroupDoc.documentElement.appendChild(xmlFieldNode);
		}
	}
	
	if (fieldValue != null)
		xmlFieldNode.text = fieldValue;	
}

// 
// Class TransactionXML: Objects of this class wrap an XML which contains transactions being performed on user Config database
//                       The class facilitates the creation and manipulation of the XML document.
//           
//     The transaction XML has the following format:
//				<TRANSACTION>
//					<DELETE>
//						<USER>                      or                 <GROUP>
//						</USER>
//						<USER>
//						</USER>
//						....
//					</DELETE>
//					<INSERT>
//						<USER>                    or               <GROUP>
//						</USER>
//						<USER>
//						</USER>
//						....
//					</INSERT>
//					<UPDATE>
//						<USER>                     or               <GROUP>
//						</USER>
//						<USER>
//						</USER>
//						....
//					</UPDATE>
//					<INSERT>                                  
//					...
//
//					</INSERT>
//					...
//				</TRANSACTION>							
//
//
//    Class Methods:
//			getXMLDoc
//			loadXMLDoc
//			addTransaction
//
function TransactionXML()
{
	this.getXMLDoc = transactGetXMLDoc;
	this.addTransaction = transactAddTransaction;
	this.m_xmlTransactionDoc = new ActiveXObject( "Microsoft.XMLDOM" );	
	var xmlTransactionNode = this.m_xmlTransactionDoc.createElement("TRANSACTION");
	this.m_xmlTransactionDoc.appendChild(xmlTransactionNode);
}

//
//	transactGetXMLDoc: Returns the XML document containing the transaction
//		
function transactGetXMLDoc()
{
	return this.m_xmlTransactionDoc;
}



//
//	transactAddTransaction: Adds a new transaction to the transaction XML. 
//   Parameters:
//       Input:
//           strTransactionType:  The type of transaction. Can be one of the strings 'I', 'D' or 'U' corresponding to
//                                 'Insert', 'Delete' and 'Update'
//
//           xmlUserNode: The xml node that contains the configuration information of a user. For the xml format 
//                        of the user node please refer to comments for class UserCfgXML
//
function transactAddTransaction(nTransactionType, xmlUserNode)
{
	var strTag;
	switch (nTransactionType)
	{
		case INSERT:
			strTag = "INSERT";
			break;
		case DELETE:
			strTag = "DELETE";
			break;
		case UPDATE:
			strTag = "UPDATE";
			break;
		case QUERY:
			strTag = "QUERY";
			break;
		default:
			throw "Programming Error: addTransaction only accepts INSERT, DELETE, UPDATE or QUERY as transaction type.";
	}

	var xmlNode = this.m_xmlTransactionDoc.createElement(strTag);
	xmlNode.appendChild(xmlUserNode);
	this.m_xmlTransactionDoc.documentElement.appendChild(xmlNode);
	return;
}
