﻿    // JScript File

		
        function CreateXMLObject(){

        
        }

        function createHttpObject(){
        
           
            if (!window.ActiveXObject){
                 this.oHttp = new XMLHttpRequest();
            }else{
                this.oHttp = new ActiveXObject("Microsoft.XMLHTTP"); 
            }    
            return this.oHttp
            

         }

		function loadXML(sXML){

			try
			{
			    
			     if (!window.ActiveXObject){
			        //this.xmlDoc = document.implementation.createDocument("","",null); 
                    var oParser = new DOMParser();
	    		    this.xmlDoc=oParser.parseFromString(sXML,"text/xml");
	            }else{
                    this.xmlDoc = new ActiveXObject("Microsoft.XMLDOM")   
	                this.xmlDoc.loadXML(sXML)
	
			    } 		     

				if (this.xmlDoc.childNodes.length > 0){
					if (this.xmlDoc.childNodes(0).Name == "xml"){
						this.oRoot = this.xmlDoc.childNodes(1)
					}else{
						this.oRoot = this.xmlDoc.childNodes(0)
					}
				}
				this.bDocLoaded = true
			}
			catch(e)
			{
				//alert('Error Loading XML')
				this.bDocLoaded = false
		        
			}
		}
	
		
		
		
		function removeXMLRecords(sRecords){
			try
			{
	            var vCount = 0
				this.oNodes = this.xmlDoc.getElementsByTagName(sRecords)
				vCurrentRecords = sRecords
				vCount = this.oNodes.length;

				for(vItem=vCount-1;vItem>=0;vItem=vItem-1){

				    this.oNodes.item(vItem).parentNode.removeChild(this.oNodes.item(vItem))

				}
				
				return true
			}
			catch(e)
			{
				return false
			}

		}


		function removeXMLRecord(){
			try
			{

				if (this.bNodeLoaded == false){
				
					if (this.bNodesLoaded == false){

						this.loadXMLRecords()
					}else{
						this.loadXMLRecord()
					}
				}

				this.oNode.parentNode.removeChild(this.oNode)
				//objNode.removeall()       
				
				return true
			}
			catch(e)
			{
				return false
			}

		}

		
		
		function loadXMLRecords(sRecords){
			try
			{
				//If m_srtCase = CaseFormat.UpperCase Then
				//    strRecords = UCase(strRecords)
				//ElseIf m_srtCase = CaseFormat.LowerCase Then
				//    strRecords = LCase(strRecords)
				//End If
				if (sRecords == ""){
					if (this.xmlDoc.childNodes.Count > 0){
						if (this.xmlDoc.childNodes(0).NodeType == 7){
							this.oNodes = this.xmlDoc.childNodes(1).childNodes
						}else{
							this.oNodes = this.xmlDoc.firstChild.childNodes
						}
					}
				}else{
					this.oNodes = this.xmlDoc.getElementsByTagName(sRecords)
				}
				
				    vCurrentRecords = sRecords
				    this.bNodesLoaded = true
				    this.bNodeLoaded = false //because this is a new node

                //this put in for return only
				if (this.xmlRecordCount() > 0){
				    return true
                }else{
                                
                    return false
                }				
			}
			catch(e)
			{

				//alert('Error Loading XML Records')
				this.bNodeLoaded=false
				this.bNodesLoaded=false
                return false
			}

		}
		
		function xmlRecordCount(){
			try
			{

				//if no nodes are loaded then load them
				if (this.bNodesLoaded == false){
					this.loadXMLRecords()
				}
				
				return this.oNodes.length
			}
			catch(e)
			{
				
				//alert('Error Occurred')
				return 0
			}
	    }

		
		function loadXMLRecord(vItem){

			try
			{
			
				if (vItem==""){
					vItem = 0
				}
				if (eval(vItem)<0){
					vItem = 0
				}
				
				//If No Nodes Are Loaded
				if (this.bNodesLoaded == false){
					this.loadXMLRecords()
				}
				if (this.oNodes.length > vItem){
					this.oNode = this.oNodes.item(vItem)
					this.bNodeLoaded = true
					vCurrentRecord = vItem
					return true
				}else{
					this.bNodeLoaded = false
					vCurrentRecord = 0
					return false
				}
				
			}
			catch(e)
			{
				this.bNodeLoaded = false
				return false
				//alert('Error Occurred')
            }

		}

		function isValidAtttibute(sAttribute){
			var bValidAttribute=false
			try
			{
				if (this.bNodeLoaded ==false){
					this.loadXMLRecord()
				}


				//if _srtCase = CaseFormat.UpperCase Then
				//	strAttribute = UCase(strAttribute)
				//ElseIf m_srtCase = CaseFormat.LowerCase Then
				//	strAttribute = LCase(strAttribute)
				//End If
				var vValue = ""
				vValue = this.oNode.attributes.getNamedItem(sAttribute).value
				return true
			}
			catch(e)
			{
				return false
			}
		}

		function getAttributeValue(sAttribute){

			var vPattern
			var vReturn

			try
			{

				if (this.bNodeLoaded == false){
				
					if (this.bNodesLoaded == false){

						this.loadXMLRecords()
					}else{
						this.loadXMLRecord()
					}
				}

				//if m_srtCase = CaseFormat.UpperCase Then
				//    strAttribute = UCase(strAttribute)
				//ElseIf m_srtCase = CaseFormat.LowerCase Then
				//    strAttribute = LCase(strAttribute)
				//End If

				if (this.isValidAtttibute(sAttribute)==true){
					vReturn = this.oNode.attributes.getNamedItem(sAttribute).value
				}else{
					vReturn = ""
				}
				return vReturn
			}
			
			catch(e)
			{
				return ""
			}
		}


		function getNodeValue(){

			var vPattern
			var vReturn

			try
			{

				if (this.bNodeLoaded == false){
				
					if (this.bNodesLoaded == false){

						this.loadXMLRecords()
					}else{
						this.loadXMLRecord()
					}
				}


            if (!window.ActiveXObject){
            
                vReturn = this.oNode.childNodes[0].nodeValue;
                //var y = this.oNode.childNodes[0];
                //vReturn = y.nodeValue;
                
                //                x=xmlDoc.getElementsByTagName("title")[0];
                //              y=x.childNodes[0];
                //            txt=y.nodeValue; 
                //          this.oNode.text
            }else{
                vReturn = this.oNode.text
                //this.oHttp = new ActiveXObject("Microsoft.XMLHTTP"); 
            }    


                //vReturn = oNode.HasChildNodes();
                
                //vReturn = oNode.ChildNodes(0).InnerText
				//vReturn = oNode.InnerText
				return vReturn
			}
			
			catch(e)
			{
				return ""
			}
		}


		function setAttributeValue( sAttribute, vValue){

			var oAttribute
			
			try
			{
			
				if (this.bNodeLoaded == false){
					this.LoadXMLRecord()
				}

				//if (m_srtCase = CaseFormat.UpperCase Then
                //strAttribute = UCase(strAttribute)
	            //ElseIf m_srtCase = CaseFormat.LowerCase Then
		        //  strAttribute = LCase(strAttribute)
			    //End If

				//vCurrentValue = GetAttributeValue(strAttribute, srtFormat)

				if (this.isValidAtttibute(sAttribute)==true){
	                this.oNode.attributes.getNamedItem(sAttribute).value = vValue
                }else{
					oAttribute = this.xmlDoc.createAttribute(sAttribute)
					this.oNode.attributes.setNamedItem(oAttribute)
					this.oNode.attributes.getNamedItem(sAttribute).value = vValue
				}
				
				return true
			}
			catch(e)
			{
				return false
			}
            
		}



		function appendChildRecords(sRecords){

            var	xmlNewDoc = new ActiveXObject("Microsoft.XMLDOM")
			//var	xmlNewDoc = new ActiveXObject("Msxml2.DOMDocument.4.0")
			var oNewElement //As XmlNode
			
			try
			{

				oNewElement = this.xmlDoc.createNode(1, "", "", "")
				oNewElement.InnerXml = sRecords

				if (this.xmlNewDoc.childNodes(0).NodeType = 7){
					oNewElement = this.xmlNewDoc.childNodes(1)
				}else{
					oNewElement = this.xmlNewDoc.firstChild
				}


				if (this.bNodeLoaded == false){
					this.loadXMLRecord()
				}
				
				//if (oNode Is Nothing Then
				//	objDoc.DocumentElement.AppendChild(objNewElement)
				//Else
					this.oNode.appendChild(oNewElement)
				//End If
			}
			catch(e)
			{
				this.bDocLoaded = False
				this.bDocLoaded = False
				//bAppended = False
				
			}
		}



	
		function createXMLDocument(sRootName){

			var vPI //As XmlNode
			var vLine 
			//Dim blnDocCreated As Boolean

			try
			{

			     if (!window.ActiveXObject){
			        this.xmlDoc = document.implementation.createDocument("","",null); 
                }else{
                    this.xmlDoc = new ActiveXObject("Microsoft.XMLDOM")   
			    } 

		        this.oRoot = this.xmlDoc.createElement(eval("sRootName")); 
		        this.xmlDoc.appendChild(this.oRoot);
				this.bDocLoaded = true
				return true
				
			}
			catch(e)
			{
			//	alert('error: createXMLDocument line-' + vLine)
				this.bDocLoaded = false
				return false
			}
		}				
		
		 function loadXPath(sPath){

            //'===============================================
			//'This will create a new record
			//'and will set it to current node
			//'===============================================
			var otmpChild // As XmlElement
            var vPathExist = 1
          
            var arNode = sPath.split("/")
            var i = 0
           
            for (i=0;i < arNode.length; i++){
               
               if (vPathExist==1){
                    if (this.loadXMLRecords(arNode[i])==false){
		                vPathExist = 0
		            }else{
                        this.loadXMLRecord(this.xmlRecordCount() - 1)
                    }
                }else{
                  //  this.createXMLChildRecord(arNode[i])
		        }
                               
            }
            
            if (vPathExist==1){
            
                return true
            
            }else{
            
                return false
            }
        }
		
		
        function createXPath(sPath){

            //'===============================================
			//'This will create a new record
			//'and will set it to current node
			//'===============================================
			var otmpChild // As XmlElement
            var vPathExist = 1
          
            var arNode = sPath.split("/")
            var i = 0
           
            for (i=0;i < arNode.length; i++){
               
                
               if (i == arNode.length-1){
                    // it is assumed that we create a new record each time
                     this.createXMLChildRecord(arNode[i])
               
               }else{
                   if (vPathExist==1){
                        if (this.loadXMLRecords(arNode[i])==false){
			                this.createXMLChildRecord(arNode[i])
			                vPathExist = 0
			            }else{
                            this.loadXMLRecord(this.xmlRecordCount() - 1)
                        }
                    }else{
                        this.createXMLChildRecord(arNode[i])
			        }
               }                 
            }
        }
		
	    function createXMLChildRecord(sNodeName){

			//'===============================================
			//'This will create a new record
			//'and will set it to current node
			//'===============================================
			var otmpChild // As XmlElement
			
			    
			
			
			// person/
			try
			{
				if (this.bDocLoaded == true){
					otmpChild = this.xmlDoc.createElement(eval("sNodeName"))//"testrecord")//sNodeName.toString)
					if (typeof(this.oRoot)=="undefined"){
						this.xmlDoc.appendChild(otmpChild)
					}else{
						if (this.bNodeLoaded == true){
							//this.oNode.parentNode.appendChild(otmpChild)
							this.oNode.appendChild(otmpChild)
						}else{						
							this.oRoot.appendChild(otmpChild)
						}
					}

					//If m_srtCase = CaseFormat.UpperCase Then
					//	strNodeName = UCase(strNodeName)
					//ElseIf m_srtCase = CaseFormat.LowerCase Then
					//	strNodeName = LCase(strNodeName)
					//End If
					this.loadXMLRecords(sNodeName)
					this.loadXMLRecord(this.xmlRecordCount() - 1)
					//objtmpchild = nothing

				
					return true
				}else{
				   // alert('Error creating record')
					return false
				}

			}
			catch(e)
			{
				return false
			}
		}
		
		
		function createXMLRecord(sNodeName){

			//'===============================================
			//'This will create a new record
			//'and will set it to current node
			//'===============================================
			var otmpChild // As XmlElement
			
			    
			
			
			// person/
			try
			{
				if (this.bDocLoaded == true){
					otmpChild = this.xmlDoc.createElement(eval("sNodeName"))//"testrecord")//sNodeName.toString)
					if (typeof(this.oRoot)=="undefined"){
						this.xmlDoc.appendChild(otmpChild)
					}else{
						if (this.bNodeLoaded == true){
							this.oNode.parentNode.appendChild(otmpChild)
							//this.oNode.appendChild(otmpChild)
						}else{							
							this.oRoot.appendChild(otmpChild)
						}
					}

					//If m_srtCase = CaseFormat.UpperCase Then
					//	strNodeName = UCase(strNodeName)
					//ElseIf m_srtCase = CaseFormat.LowerCase Then
					//	strNodeName = LCase(strNodeName)
					//End If
					this.loadXMLRecords(sNodeName)
					this.loadXMLRecord(this.xmlRecordCount() - 1)
					//objtmpchild = nothing

				
					return true
				}else{
				//alert('Error creating record')
					return false
				}

			}
			catch(e)
			{
				return false
			}
		}

	    function getXML(){
			var sReturn = ""
	  
	        try
	        {
				if (this.bDocLoaded == true){
				    if (!window.ActiveXObject){
				       sReturn = (new XMLSerializer()).serializeToString(this.xmlDoc)
				    }else{
					    sReturn = this.xmlDoc.xml
					}
				}else{
					sReturn = ""
				}		
				return sReturn			
			}	
			catch(e)
			{
				return ""
			}
		}
		
		
		function getXMLRecords(){
	        try
	        {
				if (this.bNodesLoaded == false){
					this.loadXMLRecords()
				}
				
				return this.oNodes(0).xml
			}
			catch(e)
			{
				return ""

			}
		}
		

				
		function destroyXML(){
		
			this.bDocLoaded=false
			this.bNodesLoaded=false
			this.bNodeLoaded=false
			this.bChildNodesLoaded=false
			this.bChildNodeLoaded=false


		}
		
			
		
			
    /*the below creates a new object, and gives it the two methods defined earlier*/
        function dtxmlObject(){
            // properties
             this.oHttp
             this.xmlDoc 
             this.oRoot 
		     this.oNodes
		     this.oNode
		     this.oElement
		     this.oChildNodes
		     this.oChildNode
		     this.vCurrentRecords
		     this.vCurrentRecord
		     this.vCase
		     this.bDocLoaded=false
		     this.bNodesLoaded=false
		     this.bNodeLoaded=false
		     this.bChildNodesLoaded=false
		     this.bChildNodeLoaded=false
             
            //methods
            //this.CreateXMLObject=CreateXMLObject
            this.loadXML= loadXML
            this.removeXMLRecords = removeXMLRecords
            this.removeXMLRecord = removeXMLRecord
            this.removeXMLRecord = removeXMLRecord
            this.loadXMLRecords = loadXMLRecords
            this.xmlRecordCount = xmlRecordCount
            this.loadXMLRecord = loadXMLRecord
            this.isValidAtttibute = isValidAtttibute
            this.getAttributeValue = getAttributeValue
            this.getNodeValue = getNodeValue
            this.setAttributeValue = setAttributeValue
            this.appendChildRecords = appendChildRecords
            this.createXMLDocument = createXMLDocument
            this.createXMLRecord = createXMLRecord
            this.createXPath = createXPath
            this.loadXPath = loadXPath
            this.createXMLChildRecord = createXMLChildRecord
            
            this.getXML = getXML
            this.getXMLRecords = getXMLRecords
            this.destroyXML = destroyXML   
            
            this.createHttpObject = createHttpObject
        }
