//this is the function which is called by the onblur event in the form element
function getWeather(input, response) {
  if (response != ''){ 
    // Response mode
  } else {
    // Input mode
    url  = '../getweather.php';
    loadXMLDoc(url);
  }
}

//checks to see which method of http request is available and makes the call
function loadXMLDoc(url)  {
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChange;
        req.open("GET", url, true);
        req.send(null);
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChange;
            req.open("GET", url, true);
            req.send();
        }
    }
}

function processReqChange() 
{
    // only if req shows "complete"
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
            // ...parse returning xml here...and map the values to dom objects
            response  = req.responseXML.documentElement;
            result = response.getElementsByTagName('p')[0].firstChild.data;
            var parts = result.split(" | ");
            var temperature = parts[0].split(":");
            var conditions = parts[3].split(":");
            var divHere = document.getElementById("weather"); 
            divHere.innerHTML = temperature[1] + " &#124; " + conditions[1];  //append new p inside div
            
        } else {
            //alert("There was a problem retrieving the XML data:\n" + req.statusText);
        }
    }
}

/*
 This function serializes the the xml from the given node. However, Safari will only serialize if starting from root node
*/
function getXMLNodeSerialisation(xmlNode) {
  var text = false;
  try {
    // Gecko-based browsers, Safari, Opera.
    var serializer = new XMLSerializer();
    text = serializer.serializeToString(xmlNode);
  }
  catch (e) {
    try {
      // Internet Explorer.
      text = xmlNode.xml;
    }
    catch (e) {}
  }
  return text;
}