//*************************************************************************************************
//**********                                                                           ************
//**********   XMLUtilities.js                                                         ************
//**********                                                                           ************
//**********   A collection of utility functions for manipulating                      ************
//**********   the members.xml database extract                                        ************
//**********                                                                           ************
//*************************************************************************************************

var gXmlDoc  = null ;     //  Global XML Document variable shared between list and search functions

var gHTTP_PATH  = "http://www.lythamgreendrive.co.uk/data/" ;
var gLOCAL_PATH = "data/" ;


function getMembershipXML()
{
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
    }
  else
    {// code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  xmlhttp.open("GET","http://www.lythamgreendrive.co.uk/data/members.xml",false);
  xmlhttp.send();
  gXmlDoc=xmlhttp.responseXML;
}


function checkMemberDetails(surname, memberid)
//*************************************************************************************************
//**********                                                                           ************
//**********         Search the member list by the surname and user id supplied        ************
//**********                                                                           ************
//*************************************************************************************************
{
    // Get a collection of the Rows in the Members xml database
    var rows = gXmlDoc.getElementsByTagName("Row");

    // Find out how many Rows are in the collection so that we can iterate through them
    var noOfRows = gXmlDoc.getElementsByTagName("Row").length;
    // alert("No of rows :- " + noOfRows);
    for (var i = 0; i < noOfRows; i++)
    {
        // Obtain a forenames collection from each record (there will only be 1)
        var forenames = rows[i].getElementsByTagName("Column0");
        // Obtain a surnames collection from each record (there will only be 1)
        var surnames  = rows[i].getElementsByTagName("Column1");
        // Obtain a memberids collection from each record (there will only be 1)
        var memberids = rows[i].getElementsByTagName("Column7");

        // Get the values from the first (and only) item in each collection
        var listForename  = forenames[0].firstChild.nodeValue ;
        var listSurname   = surnames[0].firstChild.nodeValue ;
        var listMemberId  = memberids[0].firstChild.nodeValue ;

        // Convert the surname to lower case and then compare what the user typed in against the member record
        if(surname == listSurname.toLowerCase() && memberid == listMemberId)
        {
//          alert("Found \nMember :- "  + listForename + " " + listSurname + "\nMember Id :- " + listMemberId) ;
        //  Set a session cookie that can be retrieved to put the members name on the top of the pages
            document.cookie="member=" + listForename ;
            return true ;
        }
    }

//  We have got to the end of the list and not been able to match the details typed in against the member list
   return false ;
}

function checkMemberEmail(email)
//*************************************************************************************************
//**********                                                                           ************
//**********         Search the member list by the surname and user id supplied        ************
//**********                                                                           ************
//*************************************************************************************************
{
    // Get a collection of the Rows in the Members xml database
    var rows = gXmlDoc.getElementsByTagName("Row");

    // Find out how many Rows are in the collection so that we can iterate through them
    var noOfRows = gXmlDoc.getElementsByTagName("Row").length;
    for (var i = 0; i < noOfRows; i++)
    {
        // Obtain a forenames collection from each record (there will only be 1)
        var emailids  = rows[i].getElementsByTagName("Column5");
        // Obtain a surnames collection from each record (there will only be 1)
        var names     = rows[i].getElementsByTagName("Column0");
        // Obtain a memberids collection from each record (there will only be 1)
        var memberids = rows[i].getElementsByTagName("Column7");

        // Get the values from the first (and only) item in each collection
        var listEmailId   = emailids[0].firstChild.nodeValue ;
        var listName      = names[0].firstChild.nodeValue ;
        var listMemberId  = memberids[0].firstChild.nodeValue ;

        // Convert the surname to lower case and then compare what the user typed in against the member record
        if(email.toLowerCase() == listEmailId.toLowerCase() )
        {
            alert("Lytham Green Drive Golf Club\n\n" + listName + ", your email address has been verified.\n\nYou will shortly receive an email, at that address, with your membership no.\n\nThank you." ) ;

            document.getElementById('memberno').value   = listMemberId ;
            document.getElementById('membername').value = listName ;
            document.getElementById('spamstop').value   = "bl0mil3y" ;

//            alert("Found \nMember :- "  + listName + "\nEmail Id :- " + listEmailId +"\nMember Id :- " + listMemberId) ;
            return true ;
        }
    }

//  We have got to the end of the list and not been able to match the details typed in against the member list
    alert("Lytham Green Drive Golf Club\n\nWe cannot find the email address - " + email + " - in our records. \n\n- Check the email address is entered correctly.\n\n- If you still have problems contact the office.") ;
    return false ;
}


//*********************************************************************************************************************************
//**********                                                                                                           ************
//**********                      FUNCTIONS BEING USED BY THE MembersContacts.html PAGE                                ************
//**********                                                                                                           ************
//*********************************************************************************************************************************

function searchMembers(surnameKeyedIn)
//*************************************************************************************************
//**********                                                                           ************
//**********         Search the member list by the surname                             ************
//**********         (Unless data quality check flag is passed in)                     ************
//**********                                                                           ************
//**********                                                                           ************
//*************************************************************************************************
{
    // Check to see if a data quality flag has been entered
    if(surnameKeyedIn == "-email"    ||
       surnameKeyedIn == "-mobile"   ||
       surnameKeyedIn == "-phone"    ||
       surnameKeyedIn == "-nophone"  ||
       surnameKeyedIn == "-surname"  ||
       surnameKeyedIn == "-forename" ||
       surnameKeyedIn == "-memberid"
                					)
    {
      dataQualityCheck(surnameKeyedIn) ;
      return ;
    }


    var noOfMatches = 0 ;

    // Get a collection of the Rows in the Members xml database
    var rows = gXmlDoc.getElementsByTagName("Row");

    // Find out how many Rows are in the collection so that we can iterate through them
    var noOfRows = gXmlDoc.getElementsByTagName("Row").length;
    var tablerow = 0 ;

    // Format the table header
    var resultView =
        "<table width=\"95%\" align=\"center\">" +
        "<tr>" +
        "<th width=\"28%\"><b>Member name</b></th>" +
        "<th width=\"20%\"><b>Mobile No</b></th>" +
        "<th width=\"20%\"><b>Phone No</b></th>" +
        "<th width=\"22%\"><b>eMail address</b></th>" +
        "<th width=\"10%\" align=\"right\">&nbsp;</th>" +
        "</tr>" ;

    for (var i = 0; i < noOfRows; i++)
    {
        var listSurnames = rows[i].getElementsByTagName("Column1");
        var theSurname  = listSurnames[0].firstChild.nodeValue ;
        if( ( theSurname.toLowerCase().match(surnameKeyedIn.toLowerCase()) ) || surnameKeyedIn.toLowerCase()=="-all" )   //  Match against the surname entered by the user
        {
          var listForenames = rows[i].getElementsByTagName("Column0");
          var theForename  = listForenames[0].firstChild.nodeValue ;

          var listTitles = rows[i].getElementsByTagName("Column2");
          var theTitle  = listTitles[0].firstChild.nodeValue ;

          var listMobiles = rows[i].getElementsByTagName("Column3");
          var theMobile  = listMobiles[0].firstChild.nodeValue ;

          var listPhones = rows[i].getElementsByTagName("Column4");
          var thePhone  = listPhones[0].firstChild.nodeValue ;

          var listEmails = rows[i].getElementsByTagName("Column5");
          var theEmail  = listEmails[0].firstChild.nodeValue ;

          tablerow++ ;

         // Check whether count is odd or even so you can colour the rows differently
           if(tablerow % 2)
           {
            tr = "<tr  class=\"row-a\">"
           }
           else
           {
            tr = "<tr  class=\"row-b\">"
           } ;

    // Merge the details from the member list into the table elememnts

           resultView +=
            tr +
            "<td class=\"align-left\">" + theTitle   + " " + theForename + " " + theSurname + "</td>" +
            "<td>" + theMobile  + "</td>" +
            "<td>" + thePhone   + "</td>" +
            "<td class=\"align-right\">" + "<a href=\"javascript:hide(\'listbox\'); hide(\'searchform\'); hide(\'updatebox\'); show(\'emailform\'); emailInForm(\'m2m-message\', \'"       + theEmail + "\', \'" + theForename + "\'); \">" + theEmail + "</a></td>" +
            "<td class=\"align-right\">" + "<a href=\"javascript:hide(\'listbox\'); hide(\'searchform\'); hide(\'updatebox\'); show(\'emailform\'); emailInForm(\'sec-changedetails\', \'" + theEmail + "\', \'" + theForename + " " + theSurname + "\'); \"><span class=\"red\">" + "amend</a></span></td>" +
            "</tr>" ;

           noOfMatches++ ;    // Increment the count of members that match
        }
     }

    // Format the table footer
    resultView += "</table><p class=\"post-footer align-right\">Found <b><span class=\"green\"> " + noOfMatches + "</span></b> out of <b><span class=\"green\">" + noOfRows + "</span></b> members matching your search criteria. <span class=\"date\">" + formatDate() + "</span></p>" ;

    //  Insert the HTML into the listArea div
    document.getElementById("listArea").innerHTML = resultView ;
}

function searchMblMembers(surnameKeyedIn)
//*************************************************************************************************
//**********                                                                           ************
//**********         Search the member list by the surname                             ************
//**********         (Unless data quality check flag is passed in)                     ************
//**********                                                                           ************
//**********                                                                           ************
//*************************************************************************************************
{

    var noOfMatches = 0 ;

    // Get a collection of the Rows in the Members xml database
    var rows = gXmlDoc.getElementsByTagName("Row");

    // Find out how many Rows are in the collection so that we can iterate through them
    var noOfRows = gXmlDoc.getElementsByTagName("Row").length;
    var tablerow = 0 ;

    // Format the table header
    var resultView =
        "<table width=\"95%\" align=\"center\">" +
        "<tr>" +
        "<th width=\"28%\"><b>Member name</b></th>" +
        "<th width=\"22%\"><b>Mobile No</b></th>" +
        "<th width=\"22%\"><b>Phone No</b></th>" +
        "<th width=\"28%\"><b>eMail address</b></th>" +
        "</tr>" ;

    for (var i = 0; i < noOfRows; i++)
    {
        var listSurnames = rows[i].getElementsByTagName("Column1");
        var theSurname  = listSurnames[0].firstChild.nodeValue ;
        if( ( theSurname.toLowerCase().match(surnameKeyedIn.toLowerCase()) ) || surnameKeyedIn.toLowerCase()=="-all" )   //  Match against the surname entered by the user
        {
          var listForenames = rows[i].getElementsByTagName("Column0");
          var theForename  = listForenames[0].firstChild.nodeValue ;

          var listTitles = rows[i].getElementsByTagName("Column2");
          var theTitle  = listTitles[0].firstChild.nodeValue ;

          var listMobiles = rows[i].getElementsByTagName("Column3");
          var theMobile  = listMobiles[0].firstChild.nodeValue ;

          var listPhones = rows[i].getElementsByTagName("Column4");
          var thePhone  = listPhones[0].firstChild.nodeValue ;

          var listEmails = rows[i].getElementsByTagName("Column5");
          var theEmail  = listEmails[0].firstChild.nodeValue ;

          tablerow++ ;

         // Check whether count is odd or even so you can colour the rows differently
           if(tablerow % 2)
           {
            tr = "<tr  class=\"row-a\">"
           }
           else
           {
            tr = "<tr  class=\"row-b\">"
           } ;

    // Merge the details from the member list into the table elememnts

           resultView +=
            tr +
            "<td class=\"align-left\">" + theTitle   + " " + theForename + " " + theSurname + "</td>" +
            "<td>" + theMobile  + "</td>" +
            "<td>" + thePhone   + "</td>" +
            "<td class=\"align-right\">" + "<a href=\"javascript:hide(\'listbox\'); hide(\'searchform\'); hide(\'updatebox\'); show(\'emailform\'); emailInForm(\'m2m-message\', \'"       + theEmail + "\', \'" + theForename + "\'); \">" + theEmail + "</a></td>" +
            "</tr>" ;

           noOfMatches++ ;    // Increment the count of members that match
        }
     }

    // Format the table footer
    resultView += "</table><p class=\"post-footer align-right\">Found <b><span class=\"green\"> " + noOfMatches + "</span></b> out of <b><span class=\"green\">" + noOfRows + "</span></b> members matching your search criteria. <span class=\"date\">" + formatDate() + "</span></p>" ;

    //  Insert the HTML into the listArea div
    document.getElementById("listArea").innerHTML = resultView ;
}


function dataQualityCheck(dataField)
//*************************************************************************************************
//**********                                                                           ************
//**********         Search the member list according to the search flag               ************
//**********                                                                           ************
//*************************************************************************************************
{

    var BLANK_FIELD = "_" ; // Constant for a blank field in the CSV file
    var noOfMatches = 0 ;
    var resultView = "" ;

    // Get a collection of the Rows in the Members xml database
    var rows = gXmlDoc.getElementsByTagName("Row");

    // Find out how many Rows are in the collection so that we can iterate through them
    var noOfRows = gXmlDoc.getElementsByTagName("Row").length;

    if(dataField == "-memberid")
    {
        // Format the table header for checking member id
        resultView =
        "<table class=\"lgdgc1\" width=\"95%\" align=\"center\">" +
        "<thead><tr>" +
        "<th width=\"20%\"><b>Forename(s)</b></th>" +
        "<th width=\"20%\"><b>Surname</b></th>" +
        "<th width=\"20%\"><b>Member Id</b></th>" +
        "<th width=\"20%\"><b>Phone</b></th>" +
        "<th width=\"20%\"><b>eMail</b></th>" +
        "</tr></thead><tbody>" ;
     }
     else
     {
        // Format the table header as the standard header
        resultView =
        "<table class=\"lgdgc1\" width=\"95%\" align=\"center\">" +
        "<thead><tr>" +
        "<th width=\"20%\"><b>Forename(s)</b></th>" +
        "<th width=\"20%\"><b>Surname</b></th>" +
        "<th width=\"20%\"><b>Mobile</b></th>" +
        "<th width=\"20%\"><b>Phone</b></th>" +
        "<th width=\"20%\"><b>eMail</b></th>" +
        "</tr></thead><tbody>" ;
    }

    for (var i = 0; i < noOfRows; i++)
    {

      switch(dataField)
      {
        case '-email' :
            var listEmails = rows[i].getElementsByTagName("Column5");
            var theEmail  = listEmails[0].firstChild.nodeValue ;

            if(theEmail == BLANK_FIELD)
            {
              var listSurnames = rows[i].getElementsByTagName("Column1");
              var theSurname  = listSurnames[0].firstChild.nodeValue ;

              var listForenames = rows[i].getElementsByTagName("Column0");
              var theForename  = listForenames[0].firstChild.nodeValue ;

              var listTitles = rows[i].getElementsByTagName("Column2");
              var theTitle  = listTitles[0].firstChild.nodeValue ;

              var listMobiles = rows[i].getElementsByTagName("Column3");
              var theMobile  = listMobiles[0].firstChild.nodeValue ;

              var listPhones = rows[i].getElementsByTagName("Column4");
              var thePhone  = listPhones[0].firstChild.nodeValue ;

            // Merge the details from the member list into the table elememnts
               resultView +=
                "<tr>" +
                "<td>" + theTitle   + " " + theForename + "</td>" +
                "<td>" + theSurname + "</td>" +
                "<td>" + theMobile  + "</td>" +
                "<td>" + thePhone   + "</td>" +
                "<td>" + theEmail   + "</td>" +
                "</tr>" ;

               noOfMatches++ ;    // Increment the count of members that match
            }
            break ;

        case '-mobile' :
            var listMobiles = rows[i].getElementsByTagName("Column3");
            var theMobile  = listMobiles[0].firstChild.nodeValue ;

            if(theMobile == BLANK_FIELD)
            {
              var listSurnames = rows[i].getElementsByTagName("Column1");
              var theSurname  = listSurnames[0].firstChild.nodeValue ;

              var listForenames = rows[i].getElementsByTagName("Column0");
              var theForename  = listForenames[0].firstChild.nodeValue ;

              var listTitles = rows[i].getElementsByTagName("Column2");
              var theTitle  = listTitles[0].firstChild.nodeValue ;

              var listEmails = rows[i].getElementsByTagName("Column5");
              var theEmail  = listEmails[0].firstChild.nodeValue ;

              var listPhones = rows[i].getElementsByTagName("Column4");
              var thePhone  = listPhones[0].firstChild.nodeValue ;

            // Merge the details from the member list into the table elememnts
               resultView +=
                "<tr>" +
                "<td>" + theTitle   + " " + theForename + "</td>" +
                "<td>" + theSurname + "</td>" +
                "<td>" + theMobile  + "</td>" +
                "<td>" + thePhone   + "</td>" +
                "<td>" + theEmail   + "</td>" +
                "</tr>" ;

               noOfMatches++ ;    // Increment the count of members that match
            }
            break ;

        case '-phone' :
            var listPhones = rows[i].getElementsByTagName("Column4");
            var thePhone  = listPhones[0].firstChild.nodeValue ;

            if(thePhone == BLANK_FIELD)
            {
              var listSurnames = rows[i].getElementsByTagName("Column1");
              var theSurname  = listSurnames[0].firstChild.nodeValue ;

              var listForenames = rows[i].getElementsByTagName("Column0");
              var theForename  = listForenames[0].firstChild.nodeValue ;

              var listTitles = rows[i].getElementsByTagName("Column2");
              var theTitle  = listTitles[0].firstChild.nodeValue ;

              var listEmails = rows[i].getElementsByTagName("Column5");
              var theEmail  = listEmails[0].firstChild.nodeValue ;

              var listMobiles = rows[i].getElementsByTagName("Column3");
              var theMobile  = listMobiles[0].firstChild.nodeValue ;

            // Merge the details from the member list into the table elememnts
               resultView +=
                "<tr>" +
                "<td>" + theTitle   + " " + theForename + "</td>" +
                "<td>" + theSurname + "</td>" +
                "<td>" + theMobile  + "</td>" +
                "<td>" + thePhone   + "</td>" +
                "<td>" + theEmail   + "</td>" +
                "</tr>" ;

               noOfMatches++ ;    // Increment the count of members that match
            }
            break ;

        case '-nophone' :
            var listPhones = rows[i].getElementsByTagName("Column4");
            var thePhone  = listPhones[0].firstChild.nodeValue ;

            var listMobiles = rows[i].getElementsByTagName("Column3");
            var theMobile  = listMobiles[0].firstChild.nodeValue ;

            if(thePhone == BLANK_FIELD && theMobile == BLANK_FIELD)
            {
              var listSurnames = rows[i].getElementsByTagName("Column1");
              var theSurname  = listSurnames[0].firstChild.nodeValue ;

              var listForenames = rows[i].getElementsByTagName("Column0");
              var theForename  = listForenames[0].firstChild.nodeValue ;

              var listTitles = rows[i].getElementsByTagName("Column2");
              var theTitle  = listTitles[0].firstChild.nodeValue ;

              var listEmails = rows[i].getElementsByTagName("Column5");
              var theEmail  = listEmails[0].firstChild.nodeValue ;

              var listMobiles = rows[i].getElementsByTagName("Column3");
              var theMobile  = listMobiles[0].firstChild.nodeValue ;

            // Merge the details from the member list into the table elememnts
               resultView +=
                "<tr>" +
                "<td>" + theTitle   + " " + theForename + "</td>" +
                "<td>" + theSurname + "</td>" +
                "<td>" + theMobile  + "</td>" +
                "<td>" + thePhone   + "</td>" +
                "<td>" + theEmail   + "</td>" +
                "</tr>" ;

               noOfMatches++ ;    // Increment the count of members that match
            }
            break ;

        case '-forename' :
            var listForenames = rows[i].getElementsByTagName("Column0");
            var theForename  = listForenames[0].firstChild.nodeValue ;

            if(theForename == BLANK_FIELD)
            {
              var listSurnames = rows[i].getElementsByTagName("Column1");
              var theSurname  = listSurnames[0].firstChild.nodeValue ;

              var listPhones = rows[i].getElementsByTagName("Column4");
              var thePhone  = listPhones[0].firstChild.nodeValue ;

              var listTitles = rows[i].getElementsByTagName("Column2");
              var theTitle  = listTitles[0].firstChild.nodeValue ;

              var listEmails = rows[i].getElementsByTagName("Column5");
              var theEmail  = listEmails[0].firstChild.nodeValue ;

              var listMobiles = rows[i].getElementsByTagName("Column3");
              var theMobile  = listMobiles[0].firstChild.nodeValue ;

            // Merge the details from the member list into the table elememnts
               resultView +=
                "<tr>" +
                "<td>" + theTitle   + " " + theForename + "</td>" +
                "<td>" + theSurname + "</td>" +
                "<td>" + theMobile  + "</td>" +
                "<td>" + thePhone   + "</td>" +
                "<td>" + theEmail   + "</td>" +
                "</tr>" ;

               noOfMatches++ ;    // Increment the count of members that match
            }
            break ;

        case '-surname' :
            var listSurnames = rows[i].getElementsByTagName("Column1");
            var theSurname  = listSurnames[0].firstChild.nodeValue ;

            if(theSurname == BLANK_FIELD)
            {
              var listForenames = rows[i].getElementsByTagName("Column0");
              var theForename  = listForenames[0].firstChild.nodeValue ;

              var listPhones = rows[i].getElementsByTagName("Column4");
              var thePhone  = listPhones[0].firstChild.nodeValue ;

              var listTitles = rows[i].getElementsByTagName("Column2");
              var theTitle  = listTitles[0].firstChild.nodeValue ;

              var listEmails = rows[i].getElementsByTagName("Column5");
              var theEmail  = listEmails[0].firstChild.nodeValue ;

              var listMobiles = rows[i].getElementsByTagName("Column3");
              var theMobile  = listMobiles[0].firstChild.nodeValue ;

            // Merge the details from the member list into the table elememnts
               resultView +=
                "<tr>" +
                "<td>" + theTitle   + " " + theForename + "</td>" +
                "<td>" + theSurname + "</td>" +
                "<td>" + theMobile  + "</td>" +
                "<td>" + thePhone   + "</td>" +
                "<td>" + theEmail   + "</td>" +
                "</tr>" ;

               noOfMatches++ ;    // Increment the count of members that match
            }
            break ;

        case '-memberid' :
            var listSurnames = rows[i].getElementsByTagName("Column1");
            var theSurname  = listSurnames[0].firstChild.nodeValue ;

            var listForenames = rows[i].getElementsByTagName("Column0");
            var theForename  = listForenames[0].firstChild.nodeValue ;

            var listPhones = rows[i].getElementsByTagName("Column4");
            var thePhone  = listPhones[0].firstChild.nodeValue ;

            var listTitles = rows[i].getElementsByTagName("Column2");
            var theTitle  = listTitles[0].firstChild.nodeValue ;

            var listEmails = rows[i].getElementsByTagName("Column5");
            var theEmail  = listEmails[0].firstChild.nodeValue ;

        	// Obtain a memberids collection from each record (there will only be 1)
        	var memberids = rows[i].getElementsByTagName("Column7");
            var theId     = memberids [0].firstChild.nodeValue ;

            // Merge the details from the member list into the table elememnts
            resultView +=
              "<tr>" +
              "<td>" + theTitle   + " " + theForename + "</td>" +
              "<td>" + theSurname + "</td>" +
              "<td>" + theId      + "</td>" +
              "<td>" + thePhone   + "</td>" +
              "<td>" + theEmail   + "</td>" +
              "</tr>" ;

            noOfMatches++ ;    // Increment the count of members that match
            break ;

          default :
              break ;

          }
     }

    // Format the table footer
    resultView += "</tbody><tfoot><tr><td colspan=\"5\" align=\"right\"><b>" + noOfMatches + "</b> out of <b>" + noOfRows + "</b> members have been found using this data quality check</td></tr></tfoot></table>" ;

    //  Insert the HTML into the listArea div
    document.getElementById("listArea").innerHTML = resultView ;
}


function loadMembership()
//*************************************************************************************
//**********                                                               ************
//**********   LOAD THE MEMBERSHIP XML DOCUMENT                            ************
//**********   to the gXmlDoc variable                                     ************
//**********                                                               ************
//*************************************************************************************
{
    loadXMLDocument('members.xml') ;
    return ;
}

//*********************************************************************************************************************************
//**********                                                                                                           ************
//**********                              HELPER FUNCTIONS FOR THE MAIN FUNCTIONS ABOVE                                ************
//**********                                                                                                           ************
//*********************************************************************************************************************************


function loadXMLDocument(url)
//*************************************************************************************
//**********                                                               ************
//**********   Generic method to load an XML file and assign it            ************
//**********   to the gXmlDoc variable                                     ************
//**********                                                               ************
//*************************************************************************************
{
//  Try the XMLHttpRequest method first
    loadHttpXMLDoc(gHTTP_PATH + url);       // This should assign the XML object to gXmlDoc

//  If it fails try a local load.  NOTE: This will only work in Internet Explorer (It is useful for testing locally)
    if(!gXmlDoc)
    {
     // alert("XMLHttpRequest failed, now using the direct load method") ;
       gXmlDoc = loadLocalXMLDoc(gLOCAL_PATH + url);
    }

    return ;
}

var xmlReq ;       // XMLHttpRequest object

function loadHttpXMLDoc(url)
//*************************************************************************************
//**********                                                               ************
//**********   loading an xml document using XMLHttpRequest                ************
//**********   this is the only method that Safari will use                ************
//**********                                                               ************
//**********                                                               ************
//*************************************************************************************
{
	xmlReq = false;

    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest)
    {
        xmlReq = new XMLHttpRequest();
        xmlReq.onreadystatechange = processXMLChange;
        xmlReq.open("GET", url, false);
        xmlReq.send(null);
    // branch for IE/Windows ActiveX version
    }
    else if (window.ActiveXObject)
    {
        xmlReq = new ActiveXObject("Microsoft.XMLHTTP");
        if (xmlReq)
        {
            xmlReq.onreadystatechange = processXMLChange;
            xmlReq.open("GET", url, false);
            xmlReq.send();
        }
    }

}

function processXMLChange()
//*************************************************************************************
//**********                                                               ************
//**********   callback function forusing loadHttpXMLDoc function          ************
//**********                                                               ************
//*************************************************************************************
{
// only if xmlReq shows "loaded"
    if (xmlReq.readyState == 4)
    {
        if (xmlReq.status == 200)   // only if "OK"
        {
//          alert("XMLHttpRequest has loaded") ;
            gXmlDoc = xmlReq.responseXML ;
        }
        else
        {
//            alert("There was a problem retrieving the XML data:\n" + xmlReq.statusText);
        }
    }
}

function loadLocalXMLDoc(url)
//*************************************************************************************
//**********                                                               ************
//**********  Cross browser method for creating an XML Object from file    ************
//**********  This function can be used to load an XML file as a local     ************
//**********  process i.e. without the need for a web server (IE Only)     ************
//**********                                                               ************
//**********  Useful for testing on your laptop                            ************
//**********                                                               ************
//*************************************************************************************
{
// Create an XML Document
  try //Internet Explorer
  {
    xmlObj=new ActiveXObject("Microsoft.XMLDOM");
//  alert("Successful creation of XML Document in Explorer");
  }
  catch(e)
  {
    try //Firefox, Mozilla, Opera, etc.
    {
      xmlObj=document.implementation.createDocument("","",null);
//    alert("Successful creation of XML Document in Firefox etc..");
    }
  catch(e) {alert("Firefox etc.. " + e.message)}
  }

// Now load the file
  try
  {
      xmlObj.async=false;
      xmlObj.load(url);
//    alert("Document :- " + docname + " loaded") ;
      return(xmlObj);
  }
  catch(e) {alert("Loading of " + url + " failed\n\n" + e.message)}

  return(null);
}


function cbDisplayXML(xmlDoc)
//*************************************************************************************
//**********                                                      *********************
//**********  Cross browser method for displaying athe contents   *********************
//**********  of an XML object                                    *********************
//*************************************************************************************
{
    if (xmlDoc.xml == null)
    {
//      Firefox etc. method
        var serializer = new XMLSerializer();
        var xmlString = serializer.serializeToString(xmlDoc);
        alert(xmlString);
    }
    else
    {
//      IE method
        alert(xmlDoc.xml) ;
    }
}

