// Load XML file before html loads
var xmlDoc;
function init(){
// For IE based browsers (tested under IE6 and IE7):
if (window.ActiveXObject)
{
    xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
   
    // Turn off asynchronus download.
    // Load the entire file before trying to do anything with it.
    xmlDoc.async=false;
   
    //load the XML file windows only
    xmlDoc.load("http://dillards.findlocation.com/search.aspx?page=XMLData");

}
// For other browsers (Netscape/Firefox/Chrome):
else if (document.implementation && document.implementation.createDocument)
{
    //Removed line to fix Google Chrome bug (26/01/2009):- xmlDoc = document.implementation.createDocument("","doc",null);
    //Replaced with below script
    var xmlhttp = new window.XMLHttpRequest();
    //load the XML file other browsers
    xmlhttp.open("GET","http://dillards.findlocation.com/search.aspx?page=XMLData",false);
    xmlhttp.send(null);

var domParser = new DOMParser();
xmlDoc = domParser.parseFromString(
xmlhttp.responseText,
'application/xml'
);

   // xmlDoc = xmlhttp.responseXML.documentElement;

}
else
{
        // The 'something fails' alert!
        alert('Your browser is unable to handle this script');
}
fillStateList();
}
// Populate selected dropdown list with data from XML file
// (<option value="storeName">storeName</option>)
// Function used for both state Fill and store Fill
function fillList(selectbox,text,value)
{
    var optionValue = document.createElement("option");
    optionValue.text = text;
    optionValue.value = value;
    selectbox.options.add(optionValue);
}

// Populate state list on page load
// Requires: <body onload="fillStateList();"> on HTML page
function fillStateList ()
{
    // Get state element ID
    var stateList = document.getElementById("cboState");

    // Clear any current values in select box
    // If JavaScript isn't working existing text will remain
    for (var x = stateList.options.length-1; x >-1; x--)
    {
        stateList.options[x] = null;
    }
    // Fill state name Array from XML file
    var stateNames = xmlDoc.getElementsByTagName("state");
    // Gets total Number of countries in XML file
    var numberOfCountries = stateNames.length;

    // Loop through state name Array and populate state select
    for (var i=0; i <numberOfCountries; i++)
    {
        var currentstate =  stateNames[i].getAttribute("name");
        fillList(stateList,currentstate,currentstate);
    }
}

// Populate store/Province list on state change
function fillStoreList()
{
    // Get store/Province element ID
    var storeList = document.getElementById("cboStore")
   
    // Clear any current values in select box
    // If JavaScript isn't working existing text will remain
    for (var x = storeList.options.length-1; x >-1; x--)
    {
        storeList.options[x] = null;
    }
   
    // Get currently selected ID from state element ID
    var stateListSelected = document.getElementById("cboState").selectedIndex;
    // Get number of stores for current selected state (populates Array)
    var numberstores = xmlDoc.getElementsByTagName("state")[stateListSelected].getElementsByTagName("store").length;
  
   // Loop through storesS Array and populate store selection for current state
    for (var i=0; i < numberstores; i++)
    {
        var currentStore =  xmlDoc.getElementsByTagName("state")[stateListSelected].getElementsByTagName("name")[i].firstChild.nodeValue;
        var storeLink = xmlDoc.getElementsByTagName("state")[stateListSelected].getElementsByTagName("url")[i].firstChild.nodeValue;
        fillList(storeList,currentStore,storeLink);
    }
       
}