
var canAjax = true;

//To Fix IE: http://msdn2.microsoft.com/en-us/library/ms536438.aspx
document.expando = true;
if (window.ActiveXObject) {
 document.getElementsByName = function(name) {
  var found = [];
  for(i = 0; i < document.all.length; i++){
   if(document.all(i).attributes && document.all(i).attributes.getNamedItem("name") && document.all(i).attributes.getNamedItem("name").value==name) {
   	found.push(document.all(i));
   }
  }
  return found;
 }
}


function AjaxCommunicate (url, currentFunction) {

	canAjax = false;
    var state_callback = function () {
        if (xtr.readyState != 4)
             return;
        if (xtr.status == 200) {
        	eval(currentFunction + "(xtr.responseXML);");
			canAjax = true;
        } else {
        	alert(xtr.responseText);
        	canAjax = true;
        }
	};

    try {
        var xtr = getXTR();
    	xtr.onreadystatechange = state_callback;
	    xtr.open("GET", url);
	    xtr.send(null);
	} catch (Ex) {
		alert(Ex);
		canAjax = true;
	}
}

function parseContent(content_data) {
	var to_eval = "var helperRes = " + content_data + ";\n";
	try {
		eval(to_eval);
	} catch (Ex) {
		alert(content_data);
		setStatus({0: "We couldn't parse: <BR>" + content_data});
		var helperRes = false;
	}
	return helperRes;
}

function getXTR (need_req_header) {
    var xtr;
    var ex;

    if (typeof(XMLHttpRequest) != "undefined") {
	// The Firefox/Safari/Opera way
        xtr = new XMLHttpRequest();
    } else {
	// The IE way(s)

        try {
            xtr = new ActiveXObject("Msxml2.XMLHTTP.4.0");
        } catch (ex) {
            try {
                xtr = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (ex) {
            }
        }
    }

    if (need_req_header) {
	    // don't work in Opera that only half-supports XMLHttpRequest
	    try {
        	    if (xtr && ! xtr.setRequestHeader)
	            xtr = null;
	    } catch (ex) { }
    }

    return xtr;
}

function regEvent (target, evt, func) {
    if (! target) return;
    if (target.attachEvent)
        target.attachEvent("on"+evt, func);
    if (target.addEventListener)
        target.addEventListener(evt, func, false);
}


// I rewrote this to suit the way I see these things working
// This takes either an XML String or and XML Dom Document and
// an XPATH String and returns an array of result nodes.
//
// It should work under IE and Mozilla just fine.
function evalXPath(XML, XPATH) {
	var found = [];
	var xmlDoc = getXMLDocument(XML);
	if (window.ActiveXObject) {
		var nodelist = xmlDoc.selectNodes(XPATH);
		for(var i = 0; i < nodelist.length; i++) {
			found.push(nodelist.item(i));
		}
	} else {
		var result = xmlDoc.evaluate(XPATH, xmlDoc, null, XPathResult.ANY_TYPE, null)
  		var res = result.iterateNext()
  		while (res) {
    		found.push(res);
    		res = result.iterateNext()
    	}
  	}
	return found;
}

//Sometimes we don't know if we already have the dom, or just a String
//Maybe we just don't care and want to be lazy.
function getXMLDocument(XML) {
	var xmlDoc;
	if (window.ActiveXObject) {
		if(XML instanceof ActiveXObject) {
			xmlDoc = XML;
		} else {
			xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
			xmlDoc.loadXML(XML);
		}
	} else {
		if(XML instanceof XMLDocument) {
			xmlDoc = XML;
		} else {
			var objDOMParser = new DOMParser();
	    	var xmlDoc = objDOMParser.parseFromString(XML, "text/xml");
	    }
	}
	return xmlDoc;
}

// This is partly stolen. I've refactored out the getting of the xmlDoc.
// It used to just load the xml from a file as well.
//
// I've also changed the way we include nodes. It's now a replacement
// of the passed in node.
function ReplaceNodeByTransform(node, XML, XSLTPath){
	var xmlDoc = getXMLDocument(XML);
	if(document.implementation && document.implementation.createDocument){
		// Mozilla
	
		var xsltProcessor = new XSLTProcessor();
		
		// load the xslt file
		var myXMLHTTPRequest = new XMLHttpRequest();
		myXMLHTTPRequest.open("GET", XSLTPath, false);
		myXMLHTTPRequest.send(null);
		
		// get the XML document
		var xslStylesheet = myXMLHTTPRequest.responseXML;
		xsltProcessor.importStylesheet(xslStylesheet);
						
		//transform
		 var resultDocument = xsltProcessor.transformToFragment(xmlDoc, document);
		var myParent = node.parentNode;
		myParent.replaceChild(resultDocument, node);
		
	}else if(window.ActiveXObject){
		// IE
		// Load XSL
		xsl = new ActiveXObject("Microsoft.XMLDOM");
		xsl.async = false;
		xsl.load(XSLTPath);
		// Transform
		node.innerHTML=xmlDoc.transformNode(xsl);
		if(xmlDoc.parseError.reason != "") {
			alert(xmlDoc.parseError.reason);
			alert(xmlDoc.parseError.srcText);
		}
	}else{
		// Browser unknown
		alert("Browser unknown");
	}
}

// Yoinked from:
// http://www.w3schools.com/dom/loadxmldoc.asp
// Sensible, I think the async thing makes it so it'll block on the load. Thank goodness!
function loadXMLDoc(dname) {
	var xmlDoc;
	// code for IE
	if (window.ActiveXObject) {
		xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
	}
	// code for Mozilla, Firefox, Opera, etc.
	else if (document.implementation && document.implementation.createDocument) {
		xmlDoc=document.implementation.createDocument("","",null);
	} else {
		alert('Your browser cannot handle this script');
	}
	xmlDoc.async=false;
	xmlDoc.load(dname);
	return(xmlDoc);
}

//A method to run generated SCRIPTS in IE. Use this
// when you've generated HTML and appended it somewhere
// in your document, but it contains scripts that you
// would expect to run. You're out of luck on document.write,
// but if you've got this far, I imagine you can sort out an
// alternative to that readily.
//
//@param node The node under which to look for SCRIPT elements
// you may have thought you called them "script" and so forth,
// but IE probably borked it all up for you. 
function executeScriptsInNode(node) {
	var scriptnodes = node.getElementsByTagName("SCRIPT");
	for(var i = 0; i < scriptnodes.length; i++) {
		eval(scriptnodes[i].innerHTML);
	}
}

//A method that sets all the labels for indicated form fields to
//a yellow highlight.
function notifyAboutFormFields(notifyFieldArray, xml_attribute, tag_name) {
 var toFix = document.getElementsByName(tag_name);
 for(var i = 0; i < toFix.length; i++) {
  toFix[i].style.backgroundColor = "transparent";
  for(var j = 0; j < notifyFieldArray.length; j++) {
   if(notifyFieldArray[j].getAttribute(xml_attribute) == toFix[i].id) {
    toFix[i].style.backgroundColor = "yellow";
    break;
   }   
  }
 }
}


