﻿
//--- Utilities XML/DOM
function createXML() {
    var xmldoc=new ActiveXObject("Microsoft.XMLDOM");
    return xmldoc;
}

function retrieveXML() {
    var xmldoc=document.implementation.createDocument("", "", null);
    return xmldoc;
}

function readXML(url) {
    var xmldoc=createXML();
    loadXMLSync(xmldoc, url);
    return getXMLText(xmldoc);
}

function writeXML(url, text) {
    var xmldoc=createXML();
    setXMLText(xmldoc, text) ;
    writeXMLSync(xmldoc, url);
}

function setXMLText(xmldoc,text) {
	xmldoc.loadXML( text);
    isXMLOk(xmldoc);
	return xmldoc;
}

function getXMLText(xmldoc){
    return xmldoc.xml;
}

function loadXMLAsync(handler,url) {
	if (document.implementation && document.implementation.createDocument) {
		var xmldoc=retrieveXML();
		xmldoc.onload = function() {  handler(xmldoc,url); 	}
	} else if (window.ActiveXObject) {
		var xmldoc=createXML();
		xmldoc.onreadystatechange = function() {
		    if (xmldoc.readyState==4) handler(xmldoc,url); }
	}
	xmldoc.load(url);
    isXMLOk(xmldoc);
	return xmldoc;
}

function loadXMLSync(xmldoc, url) {
    xmldoc.async = false;
    xmldoc.validateOnParse = false;
    xmldoc.resolveExternals = false;
	xmldoc.load(url);
    isXMLOk(xmldoc);
	return xmldoc;
}

function writeXMLSync(xmldoc, url) {
    xmldoc.async = false;
    xmldoc.validateOnParse = false;
    xmldoc.resolveExternals = false;
	xmldoc.save(xmldoc.url);
    isXMLOk(xmldoc);
	return xmldoc;
}

function transformXMLSync(xmldoc,xmlurl,xsldoc,xslurl) {
    loadXMLSync(xmldoc, xmlurl);
    loadXMLSync(xsldoc, xslurl);
	
	var xmltransformed=new ActiveXObject("Microsoft.XMLDOM");
	xmldoc.transformNodeToObject(xsldoc,xmltransformed);
    isXMLOk(xmltransformed);
	return xmltransformed;
}

function createXMLHTTP() {
    var xhr = new ActiveXObject("MSXML2.XMLHTTP.3.0");
    return xhr;
}

function getResponseXML(url)
{
    var xmldoc=null;
    try {
        var xhr = createXMLHTTP();
        xhr.open("GET", url, false);
        xhr.send();

        xmldoc = xhr.responseXML;
    }
    catch (e) {
        //alert(e.description);
    }
    finally {
        var xhr = null;
    }
    return xmldoc;
}

function isXMLOk(xmldoc) {
    if (xmldoc.parseError.errorCode != 0) {
       var myErr = xmldoc.parseError;
       window.alert("XML Error " + myErr.reason);
       return false;
    } else {
       //window.alert(xmldoc.xml);
    }
   return true
}



function getNewsFeedXML(url) {
    return getXMLText(getResponseXML(url));
}

//--- Helpers DSO/XML recordset navigation

function moveFirst(xmlData) {
    xmlData.recordset.moveFirst();
}

function movePrevious(xmlData) {
    xmlData.recordset.movePrevious();
    if (xmlData.recordset.BOF) xmlData.recordset.moveFirst();
}

function moveNext(xmlData) {
    xmlData.recordset.moveNext();
    if (xmlData.recordset.EOF) xmlData.recordset.moveLast();
}

function moveLast(xmlData) {
    xmlData.recordset.moveLast();
}

function movePreviousVerified(xmlData){
    if (xmlData.recordset.absoluteposition > 1)	{
	    xmlData.recordset.moveprevious();
        if (xmlData.recordset.BOF) xmlData.recordset.moveFirst();
	}
}
function moveNextVerified(xmlData){
    if (xmlData.recordset.absoluteposition < xmlData.recordset.recordcount)	{
	    xmlData.recordset.movenext();
        if (xmlData.recordset.EOF) xmlData.recordset.moveLast();
	}
}

function deleteRecord(xmlData,recordNo) {
    if (setRecordNo(xmlData,recordNo)) {
        return deleteCurrentRecord(xmlData);
    }
    return false;
}

function deleteLastRecord(xmlData) {
    if (xmlData.recordset.RecordCount>0) {
        if (setRecordNo(xmlData,xmlData.recordset.RecordCount));
            return deleteCurrentRecord(xmlData);
    }
    return false;
}

function deleteCurrentRecord(xmlData) {
    if (xmlData.recordset.RecordCount > 0) {
        xmlData.recordset.Delete();
        return true;
    }
    return false;
}

function setRecordNo(xmlData,recordNo) {
    if ((xmlData.recordset.RecordCount>0) && (recordNo<=xmlData.recordset.RecordCount)) {
	    xmlData.recordset.AbsolutePosition = recordNo;
	    return true;
	}
	return false;
}

function addRecord(xmlData) {
    xmlData.recordset.AddNew();
    return true;
}
function setCurrentRowFieldValue(xmlData, control, value){
    xmlData.recordset.fields(control.dataFld).value = value;
}        
function getCurrentRowFieldValue(xmlData, control){
    return xmlData.recordset.fields(control.dataFld).value;
}        
function getOldXmlValue(xmlData,control) {
    var origValue = xmlData.recordset.fields(control.dataFld).value;
    return origValue;
}


//--- Helpers XML DOM recordnavigation

function removeRecord(xmlData,index){
	//retrieve the child node cooresponding to that index 
	var childNode = xmlData.XMLDocument.documentElement.childNodes(index);
	if (childNode!=null) {
	    //call the removeChild method to delete this node
	    xmlData.XMLDocument.documentElement.removeChild(childNode);
	}
	childNode="";
}

function removeLastRecord(xmlData){
	var lastIndex = xmlData.XMLDocument.documentElement.childNodes.length-1;
	//retrieve the child node cooresponding to that index 
	var childNode = xmlData.XMLDocument.documentElement.childNodes(lastIndex);
	if (childNode!=null) {
	    //call the removeChild method to delete this node
	    xmlData.XMLDocument.documentElement.removeChild(childNode);
	}
	childNode="";
}

function addRecord(xmlData, xmlDataNew){
		//Create a clone of the root element of the data island
		//that contains the prebuilt XML data record
		var newRecord = xmlDataNew.XMLDocument.documentElement.cloneNode(true)
		//append node as a child to the root element of our main data island
		xmlData.XMLDocument.documentElement.appendChild(newRecord)
		newRecord = "";
}

function addLastRecord(xmlData){
	//retrieve the index of the last child node
	var lastNodeIndex = xmlData.XMLDocument.documentElement.childNodes.length-1;
	//create a clone of the last child node
	var newRecord = xmlData.XMLDocument.documentElement.childNodes(lastNodeIndex).cloneNode(true);
	//append the new node to the root element of out main data island
	xmlData.XMLDocument.documentElement.appendChild(newRecord);
	newRecord="";
}
