﻿// JavaScript for data View AJAX handlers
/** Function called when the data has been loaded.
    @param xmlObject Either a XMLHttpObject or a SVGViewer data object.
*/
function operationComplete(xmlObject, addTo) {
    var domDoc = null;
    if (xmlObject.readyState) {
	    if (xmlObject.readyState == 4) {
		    try {
			    if (xmlObject.responseText) {
				    domDoc = xmlObject.responseText;
			    }
		    }
		    catch (ex) {
			    alert(ex);
			}
		}
	}
    else {
		alert('XML is ? ' + xmlObject);
	}
    if (domDoc != null) {
        var dc = document.createElement('div');
        dc.className = 'dvList';
        if (domDoc.indexOf('<body') > 0) {
            domDoc = domDoc.substring(domDoc.indexOf('>',  domDoc.indexOf('<body')) + 1);
        }
        if (domDoc.indexOf('</body') > 0) {
            domDoc = domDoc.substring(0, domDoc.indexOf('</body'));
        }
        // Strip to the UL only, if we can...
        if (domDoc.indexOf('<ul') > 0) {
            domDoc = domDoc.substring(domDoc.indexOf('<ul'));
        }
        if (domDoc.indexOf('</ul>') > 0) {
            domDoc = domDoc.substring(0, domDoc.indexOf('</ul>') + '</ul>'.length);
        }
        dc.innerHTML = domDoc;
	    //addTo.insertBefore(dc, addTo.firstChild);
	    addTo.appendChild(dc);
    }
}
/** Start the loading of the XML data.
*/
function loadAndAdd(path, div, onCompleteFunc) {
    var addTo = div;
    try {
        var xmlhttp;
	    // IE
	    if (window.ActiveXObject) {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			if (xmlhttp) {
				xmlhttp.onreadystatechange = function() {
					operationComplete(xmlhttp, addTo);
					if (onCompleteFunc) {
					    onCompleteFunc(xmlhttp);
					}
				};
				xmlhttp.open('GET', path, true);
				xmlhttp.send();
			}
		}
		// Mozillans
		else if (window.XMLHttpRequest) {
    		xmlhttp = new XMLHttpRequest();
	    	xmlhttp.onreadystatechange = function() {
		    	operationComplete(xmlhttp, addTo);
		    	if (onCompleteFunc) {
				    onCompleteFunc(xmlhttp);
				}
		    };
			xmlhttp.open('GET', path, true);
			xmlhttp.send(null);
		}
	}
    catch (ex) {
	    alert(ex);
	}
}


