// create some global variables to alleviate any slight overheads!!
// create an XML document to parse anything
var objXMLDoc = CreateXMLDocumentObject(); //new ActiveXObject("MSXML.DOMDocument");
objXMLDoc.async = false;
// store last singlenode and last list selected
var objNode;	
var objNodeList;
var strLastURL = "";

// create the xml HTTP object
var objHTTP = CreateHTTPRequestObject(); //new ActiveXObject("Microsoft.XMLHTTP");

function ClearSelect(objSelect)
{
	// make sure we haven't got a null object
	if (objSelect == null)
	{
		return;
	}
	// clear the selection
	objSelect.selectedIndex = -1;
	// find the number of items
	var intItemCount = objSelect.options.length;
	// go through each item and delete it!
	for (var intCount = 0; intCount < intItemCount; intCount++)
		objSelect.options.remove(0);
}

function ClearSelectFiltered(objSelect, strFilter)
{
	var strText = strFilter.toUpperCase();
	
	// make sure we haven't got a null object
	if ((objSelect == null) || (strText == null) || (strText == ""))
	{
		return;
	}
	
	// find the number of items
	var intItemCount = objSelect.options.length;
	// go through each item and delete it!
	for (var intCount = intItemCount - 1; intCount >= 0; intCount--)
	{
		if (objSelect.options.item(intCount).text.toUpperCase().indexOf(strText) == -1)
		{
			objSelect.options.remove(intCount);
		}
	}
}

function AddSelectItem(objSelect, strValue, strText)
{
	// create an option element to add to the select
	var objOption = document.createElement("option");
	// set its value and text
	if (strValue != "")
	{
		objOption.value = strValue;
	}
	objOption.text = strText;
	// add the option into the select
	objSelect.add(objOption);
}

function GetXMLData(strURL, objData)
{
	var strReply = "";
	var strError;

	try
	{
		// try to connect to the supplied URL
		alert('test');
		objHTTP.Open("POST", strURL, false);
	}
	catch (e)
	{
		// we couldn't open for some reason or the other,
		// so tell the user
		strError = "An error occurred trying to retrieve XML Data.\n\n" +
				   "Error " + e.number + ": " + e.description + "\n" +
				   "URL: " + strURL;
		alert(strError);
	}

	// make sure we are ready to try and send data
	if (objHTTP.readyState != 0)
	{
		// send the data
		objHTTP.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		objHTTP.Send(objData);

		// get the response from the server
		strReply = objHTTP.ResponseText;
		// make sure we haven't got an error
	
		// did we complete OK?
		if (objHTTP.status != 200)
		{
			// no, so failure
			strReply = "";

			// build an error message for the user
			strError = "HTTP Error " + objHTTP.status + " - " + objHTTP.statusText + ":\n\n" +
					   "URL: " + strURL;

			if (objHTTP.status == 500)
			{
				strError += "Response:\n" + objHTTP.ResponseText;
			}
		
			strError += "\n\nClick on OK to copy the URL to the clipboard to aid debugging\n" +
						"Otherwise click on Cancel to proceed normally"
																	
			// tell the user (for what little good that will do!!)
			if (confirm(strError) == true)
			{
				// copy URL to clipboard for the user
				window.clipboardData.setData("Text", strURL);
			}
		}
	}

	// return the response
	return strReply;
}

function LoadXML(strURL, objData)
{
	// get the XML data
	// alert('getajax');
	var strXML = getRoadworks(strURL);//GetXMLData(strURL, objData);
//objXMLDoc.load(strURL);
	return true;
}


function LoadSelect(objSelect, strURL, strValueField, strTextField)
{
	// clear all items from the select object
	ClearSelect(objSelect);
	
	// get the XML data
	if (LoadXML(strURL) == true)
	{
		var strValue;
		var strText;
		
		// get the data rows out of the XML
		objNodeList = objXMLDoc.documentElement.selectNodes("//rs:data/z:row");
		// go through each row
		for (var intCount = 0; intCount < objNodeList.length; intCount++)
		{
			// get the value and text
			if (strValueField != "")
			{
				strValue = objNodeList.item(intCount).getAttribute(strValueField);
			}
			else
			{
				strValue = "";
			}
			strText = objNodeList.item(intCount).getAttribute(strTextField);
			// add the option into the select
			AddSelectItem(objSelect, strValue, strText);
		}
	}
	// make sure that there is no initial selection
	objSelect.selectedIndex = -1;
}

function FirstValueOK()
{
	return (objNode != null);
}

function SelectSingleNode(strNode)
{
	// select the node
	objNode = objXMLDoc.documentElement.selectSingleNode(strNode);
}

function GetFirstValue(strURL, strField, objData)
{
	var strValue = "";

	if (LoadXML(strURL, objData) == true)
	{
		// get the data row out of the XML
		SelectSingleNode("//rs:data/z:row");
		if (objNode != null)
		{
			// get the value out
			strValue = objNode.getAttribute(strField);
		}
	}
	else
	{
		objNode = null;
	}

	return strValue;
}

function GetSingleValue(strField)
{
	var strValue = "";
	
	if (objNode != null)
	{
		// get the value out
		strValue = objNode.getAttribute(strField);
	}
	
	return strValue;
}

function NVL(strValue, strNVL)
{
	if (strValue == null)
	{
		return strNVL;
	}
	else
	{
		return strValue;
	}
}


function CreateHTTPRequestObject () {
    if (typeof (XMLHttpRequest) == "undefined") {
        if (typeof (ActiveXObject) != "undefined") {
            var progIDs = new Array (
                                      "Msxml2.XMLHTTP.6.0", 
                                      "Msxml2.XMLHTTP.5.0", 
                                      "Msxml2.XMLHTTP.4.0", 
                                      "Msxml2.XMLHTTP.3.0", 
                                      "Msxml2.XMLHTTP", 
                                      "Microsoft.XMLHTTP"
                                    );
            for (i in progIDs) {
                try { 
                    return new ActiveXObject(progIDs[i]); 
                } catch(e) {};
            }
        }
    }
    else {
        return new XMLHttpRequest;
    }
    alert ("Your browser doesn't support XML handling!");
    return null;
}

function CreateMSXMLDocumentObject () {
    if (typeof (ActiveXObject) != "undefined") {
        var progIDs = new Array (
                                  "Msxml2.DOMDocument.6.0", 
                                  "Msxml2.DOMDocument.5.0", 
                                  "Msxml2.DOMDocument.4.0", 
                                  "Msxml2.DOMDocument.3.0", 
                                  "MSXML2.DOMDocument", 
                                  "MSXML.DOMDocument"
                                );
        for (i in progIDs) {
            try { 
                return new ActiveXObject(progIDs[i]); 
            } catch(e) {};
        }
    }
    alert ("Your browser doesn't support XML handling!");
    return null;
}

function CreateXMLDocumentObject (rootName) {
    if (rootName == null)
        rootName = "";
    var xmlDoc = null;
        //Firefox, Opera, Safari
    if (document.implementation.createDocument) {
        xmlDoc = document.implementation.createDocument ("", rootName, null);
    }
    else {
            // Internet Explorer
        xmlDoc = CreateMSXMLDocumentObject ();
        if (rootName != "") {
            var rootNode = xmlDoc.createElement (rootName);
            xmlDoc.appendChild (rootNode);
        }
    }
    
    return xmlDoc;
}


function setAjax() {
var ajaxRequest;  // The variable that makes Ajax possible!
//alert('getajax');
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
			
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	return ajaxRequest;
}

function getRoadworks(strURL){
//alert('getrw');
ajaxRequest = setAjax();
ajaxRequest.onreadystatechange = function(){
	if(ajaxRequest.readyState == 4){
	//alert(ajaxRequest.responseText);
	
	//objXMLDoc=ajaxRequest.responseXML;
	
	if (window.DOMParser)
          {
          parser=new DOMParser();
          xmlDoc=parser.parseFromString(ajaxRequest.responseText,"text/xml");
          var serializer = new XMLSerializer();
var xml = serializer.serializeToString(xmlDoc.firstChild);

           document.getElementById('ctlSWSummary_divStreetworksOverview').innerHTML = xml;
          }
        else // Internet Explorer
          {
          xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
          xmlDoc.async="false";
          xmlDoc.loadXML(ajaxRequest.responseText); 
         // alert(xmlDoc.childNodes(1).xml);
          document.getElementById('ctlSWSummary_divStreetworksOverview').innerHTML = xmlDoc.childNodes(1).xml;
        //  alert(document.getElementById('ctlSWSummary_divStreetworksOverview').outerHTML);
          } 
	
	
	
	//document.getElementById('ctlSWSummary_divStreetworksOverview').innerHTML = ajaxRequest.responseText;
return ajaxRequest.responseText;
	}
};
ajaxRequest.open("GET", strURL, true);
ajaxRequest.send(null);
}


function replaceAmp (strFilter) {
   // alert(strFilter);
    strFilter= strFilter.replace(/\&amp;/g,'&');
   // alert(strFilter);
    return strFilter;
}
