﻿//Extend element object to give 
if (!window.ActiveXObject) {

    Element.prototype.selectNodes = function (sXPath) {

        var oEvaluator = new XPathEvaluator();

        var oResult = oEvaluator.evaluate(sXPath, this, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);

        var aNodes = new Array();

        if (oResult != null) {

            var oElement = oResult.iterateNext();

            while (oElement) {

                aNodes.push(oElement);

                oElement = oResult.iterateNext();

            }

        }

        return aNodes;

    }

    Element.prototype.selectSingleNode = function (sXPath) {

        var oEvaluator = new XPathEvaluator();

        // FIRST_ORDERED_NODE_TYPE returns the first match to the xpath.
        var oResult = oEvaluator.evaluate(sXPath, this, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);

        if (oResult != null) {

            return oResult.singleNodeValue;

        } else {

            return null;

        }

    }

}



//Container for Client List

var arClientList = [];
var arCaseStudyList = [];
var arCarouselList = [];
var arBiogList = [];

var iCurrentCaseStudy;

var XMLHttpArray = [
        function () { return new XMLHttpRequest() },
        function () { return new ActiveXObject("Msxml2.XMLHTTP") },
        function () { return new ActiveXObject("Msxml2.XMLHTTP") },
        function () { return new ActiveXObject("Microsoft.XMLHTTP") }
        ];
function createXMLHTTPObject() {
    var xmlhttp = false;
    for (var i = 0; i < XMLHttpArray.length; i++) {
        try {
            xmlhttp = XMLHttpArray[i]();
        } catch (e) {
            continue;
        }
        break;
    }
    return xmlhttp;
} ////

function AjaxRequest(url, callback, method) {
    var req = createXMLHTTPObject();
    req.onreadystatechange = function () {
        if (req.readyState != 4) return;
        if (req.status != 200) return;
        callback(req);
    }
    req.open(method, url, true);
    req.setRequestHeader('User-Agent', 'My XMLHTTP Agent');
    req.send(null);
} ////

function AjaxResponse(req) 
{
    var respXML = req.responseXML;
    if (!respXML) return;

    //var respVAL = respXML.getElementsByTagName('family')[0].getAttribute('result');

    var arClients = [];
    var arCaseStudies = [];
    var arCarousel = [];
    var arBiogs = [];

    arClients = respXML.getElementsByTagName('client');
    arCaseStudies = respXML.getElementsByTagName('case-study');
    arCarousel = respXML.getElementsByTagName('carousel-item');
    arBiogs = respXML.getElementsByTagName('biog');

    var xml = respXML;

    var i, iLen, iImg;
    var strName, strImage, strReference, iCaseSudy, iClientID, strClientName, strCaseStudyName, strImageBase, strCSRef;
    var strHTMLContent, iOrderID, strCarouselImage, strCarouselTitle, strCarouselCssID, strURLRef;
    var iPersonID, iSectorID, strCopy;
    var strId;

    var arCSImages = [];
    var arCSPushImages = [];

    var oClient;

    iLen = arClients.length
    arClientList.length = 0;
    arCaseStudyList.length = 0;

    if (iLen) 
    {
        for (i = 0; i < iLen; i++) 
        {

            if (navigator.appName.indexOf("Internet Explorer") != -1) 
            {

                strId = arClients[i].childNodes[0].text;
                strClientName = arClients[i].childNodes[1].text;
                strImage = arClients[i].childNodes[2].text;
                strReference = arClients[i].childNodes[3].text;
                iCaseSudy = arClients[i].childNodes[4].text;
                strCaseStudyName = arClients[i].childNodes[5].text;
                try{
                    strCSRef = arClients[i].childNodes[6].text;
                }
                catch(ex)
                {
                    strCSRef = '';
                }
            }
            else 
            {

                strId = arClients[i].childNodes[0].textContent;
                strClientName = arClients[i].childNodes[1].textContent;
                strImage = arClients[i].childNodes[2].textContent;
                strReference = arClients[i].childNodes[3].textContent;
                iCaseSudy = arClients[i].childNodes[4].textContent;
                strCaseStudyName = arClients[i].childNodes[5].textContent;
                try{
                    strCSRef = arClients[i].childNodes[6].textContent;
                }
                catch(ex)
                {
                    strCSRef = '';
                }
            }

            oClient = new Object;
            oClient.ID = strId;
            oClient.ClientName = strClientName;
            oClient.Image = strImage;
            oClient.Reference = strReference;
            oClient.CaseStudyId = iCaseSudy;
            oClient.CaseStudyName = strCaseStudyName;
            oClient.CaseStudyRef = strCSRef;

            arClientList.push(oClient);
        }
    }

    var oCaseStudy;
    var oImage;

    iLen = arCaseStudies.length
    arCaseStudyList.length = 0;

    if (iLen) {
        for (i = 0; i < iLen; i++) {

            if (navigator.appName.indexOf("Internet Explorer") != -1) {

                strId = arCaseStudies[i].childNodes[0].text;
                iClientID = arCaseStudies[i].childNodes[1].text;
                strClientName = arCaseStudies[i].childNodes[2].text;
                strHTMLContent = arCaseStudies[i].childNodes[3].text;
                strName = arCaseStudies[i].childNodes[4].text;
                iOrderID = arCaseStudies[i].childNodes[5].text;
                strImage = arCaseStudies[i].childNodes[6].text;
                strCSRef = arCaseStudies[i].childNodes[7].text;

                arCSImages = arCaseStudies[i].childNodes[8].getElementsByTagName('image');

                var arCSPushImages = [];

                for (iImg = 0; iImg < arCSImages.length; iImg++) {

                    oImage = new Object;

                    oImage.ID = arCSImages[iImg].childNodes[0].text;
                    oImage.FileName = arCSImages[iImg].childNodes[1].text;

                    arCSPushImages.push(oImage);
                }

                strURLRef = arCaseStudies[i].childNodes[10].text;
                
            }
            else {

                strId = arCaseStudies[i].childNodes[0].textContent;
                iClientID = arCaseStudies[i].childNodes[1].textContent;
                strClientName = arCaseStudies[i].childNodes[2].textContent;
                strHTMLContent = arCaseStudies[i].childNodes[3].textContent;
                strName = arCaseStudies[i].childNodes[4].textContent;
                iOrderID = arCaseStudies[i].childNodes[5].textContent;
                strImage = arCaseStudies[i].childNodes[6].textContent;

                arCSImages = arCaseStudies[i].childNodes[8].getElementsByTagName('image');

                var arCSPushImages = [];

                for (iImg = 0; iImg < arCSImages.length; iImg++) {

                    oImage = new Object;

                    oImage.ID = arCSImages[iImg].childNodes[0].textContent;
                    oImage.FileName = arCSImages[iImg].childNodes[1].textContent;

                    arCSPushImages.push(oImage);
                }

                strURLRef = arCaseStudies[i].childNodes[10].textContent;

            }

            oCaseStudy = new Object;
            oCaseStudy.ID = strId;
            oCaseStudy.ClientID = iClientID;
            oCaseStudy.ClientName = strClientName;
            oCaseStudy.HTMLContent = strHTMLContent;
            oCaseStudy.Name = strName;
            oCaseStudy.OrderID = iOrderID;
            oCaseStudy.ClientLogo = strImage
            oCaseStudy.Images = arCSPushImages;
            oCaseStudy.URLRef = strURLRef;

            arCaseStudyList.push(oCaseStudy);
        }
    }

    var oCarouselItem;

    iLen = arCarousel.length
    arCarouselList.length = 0;

    if (iLen) {
        for (i = 0; i < iLen; i++) {

            if (navigator.appName.indexOf("Internet Explorer") != -1) {

                strId = arCarousel[i].childNodes[0].text;
                strClientName = arCarousel[i].childNodes[1].text;
                strCarouselImage = arCarousel[i].childNodes[2].text;
                strCarouselTitle = arCarousel[i].childNodes[3].text;
                strCarouselCssID = arCarousel[i].childNodes[4].text;
                strCaseStudyName = arCarousel[i].childNodes[5].text;
                strURLRef = arCarousel[i].childNodes[6].text;
            }
            else {

                strId = arCarousel[i].childNodes[0].textContent;
                strClientName = arCarousel[i].childNodes[1].textContent;
                strCarouselImage = arCarousel[i].childNodes[2].textContent;
                strCarouselTitle = arCarousel[i].childNodes[3].textContent;
                strCarouselCssID = arCarousel[i].childNodes[4].textContent;
                strCaseStudyName = arCarousel[i].childNodes[5].textContent;
                strURLRef = arCarousel[i].childNodes[6].textContent;
            }

            oCarouselItem = new Object;
            oCarouselItem.ID = strId;
            oCarouselItem.ClientName = strClientName;
            oCarouselItem.CarouselImage = strCarouselImage;
            oCarouselItem.Title = strCarouselTitle;
            oCarouselItem.CssID = strCarouselCssID;
            oCarouselItem.URLRef = strURLRef;

            arCarouselList.push(oCarouselItem);
        }
    }

    var oBiogItem;

    iLen = arBiogs.length
    arBiogList.length = 0;

    if (iLen) {
        for (i = 0; i < iLen; i++) {

            if (navigator.appName.indexOf("Internet Explorer") != -1) {

                strId = arBiogs[i].childNodes[0].text;
                strImage = arBiogs[i].childNodes[1].text;
                strName = arBiogs[i].childNodes[2].text;
                iOrderID = arBiogs[i].childNodes[3].text;
                iPersonID = arBiogs[i].childNodes[4].text;
                iSectorID = arBiogs[i].childNodes[5].text;
                strCopy = arBiogs[i].childNodes[6].text;
            }
            else {

                strId = arBiogs[i].childNodes[0].textContent;
                strImage = arBiogs[i].childNodes[1].textContent;
                strName = arBiogs[i].childNodes[2].textContent;
                iOrderID = arBiogs[i].childNodes[3].textContent;
                iPersonID = arBiogs[i].childNodes[4].textContent;
                iSectorID = arBiogs[i].childNodes[5].textContent;
                strCopy = arBiogs[i].childNodes[6].textContent;
            }

            oBiogItem = new Object;
            oBiogItem.ID = strId;
            oBiogItem.Image = strImage;
            oBiogItem.Name = strName;
            oBiogItem.OrderID = iOrderID;
            oBiogItem.PersonID = iPersonID;
            oBiogItem.SectorID = iSectorID;
            oBiogItem.Copy = strCopy

            arBiogList.push(oBiogItem);
        }
    }
    receiveContent(arClientList, arCaseStudyList, arCarouselList, arBiogList);
}


function MakeRequest(strSectors, strServices, iCaseStudy) {
    AjaxRequest("Views/ClientsXML.aspx?sectors=" + strSectors + "&services=" + strServices + "&casestudy=" + iCaseStudy.toString(), AjaxResponse, "get");
} ////

function MakePeopleRequest(strSectors) {
    AjaxRequest("Views/ClientsXML.aspx?peoplesector=" + strSectors, AjaxResponse, "get");
} ////

function MakeRequestClients(strSectors) {
    AjaxRequest("Views/ClientsXML.aspx?clientsector=" + strSectors, AjaxResponse, "get");
} ////
