﻿
    function queryStringManager(qs) { // optionally pass a querystring to parse
        this.params = {};
    	
        if (qs == null) qs = location.search.substring(1, location.search.length);
        if (qs.length == 0) return;

        qs = qs.replace(/\+/g, ' ');
        var args = qs.split('&');

        for (var i = 0; i < args.length; i++) {
	        var pair = args[i].split('=');
	        var name = decodeURIComponent(pair[0]);
    		
	        var value = (pair.length==2)
		        ? decodeURIComponent(pair[1])
		        : name;
    		
	        this.params[name] = value;
        }
    }

    queryStringManager.prototype.get = function(key, default_) { 
        var value = this.params[key];
        return (value != null) ? value : default_;
    };

    queryStringManager.prototype.contains = function(key) {
        var value = this.params[key];
        return (value != null);
    };

    function setImageBGPositionXY(e, x, y)
    {
        e.style.backgroundPosition = x + "px " + y + "px";
    }

    function setImageBGPositionY(e, i)
    {
        var sBGPosition = e.style.backgroundPosition;
        var iSpace = sBGPosition.indexOf(" ");
        sBGPosition = sBGPosition.substring(0, iSpace) + " " + i + "px";
        e.style.backgroundPosition = sBGPosition;
    }
    
    function tabbedContent(baseID, noTabs)
    {
        var tabbedContent = this;
        tabbedContent.baseID = baseID;
        tabbedContent.noTabs = noTabs;
        tabbedContent.tabCount = 0;
        tabbedContent.tabs = new Object();
        tabbedContent.panelCount = 0;
        tabbedContent.panels = new Object();
        tabbedContent.panelClass = new Object();
        tabbedContent.selectedTab = 0;
        tabbedContent.initialise();
        tabbedContent.onBeforeTabChanged = null;
        tabbedContent.onTabChanged = null;
    }

    tabbedContent.prototype =
    {
        initialise: function()
        {
            var tabbedContent = this, div, a, qs = new queryStringManager(), inpSelected = document.getElementById(tabbedContent.baseID + "_SelectedTab");
            var tab = 1;

            if (inpSelected && inpSelected.value != "")
            {
                tab = inpSelected.value;
                if (tab < 1) tab = 1;
                if (tab > tabbedContent.noTabs) tab = 1;
                inpSelected.value = tab;
            }

            tabbedContent.selectedTab = tab - 1;

            for (var i = 0; i < tabbedContent.noTabs; i++)
            {
                div = document.getElementById(tabbedContent.baseID + "_Tab" + (i + 1));
                if (div)
                {
                    tabbedContent.tabs[tabbedContent.tabCount] = div;
                    tabbedContent.tabCount++;

                    if (i == (tab - 1)) div.parentNode.className = "TabIn";
                    else div.parentNode.className = "Tab";

                    if (div.childNodes.length > 0)
                    {
                        a = div.childNodes[0];
                        a.href = "javascript:tabbedcontentmanager.showTab(\"" + tabbedContent.baseID + "\", " + i + ");";
                    }
                }
                div = document.getElementById(tabbedContent.baseID + "_Content" + (i + 1));
                if (div)
                {
                    tabbedContent.panels[tabbedContent.panelCount] = div;
                    tabbedContent.panelClass[tabbedContent.panelCount] = div.parentNode.className;
                    tabbedContent.panelCount++;
                    if (i != (tab - 1))
                        div.parentNode.className = tabbedContent.panelClass[tabbedContent.panelCount] + " TabPanelOff";
                }
            }

            if (tabbedContent.onTabChanged != null)
                tabbedContent.onTabChanged(tabbedContent.selectedTab);
        },

        showTab: function(tabNo)
        {
            var tabbedContent = this, inpSelected = document.getElementById(tabbedContent.baseID + "_SelectedTab");

            if (!tabbedContent.onBeforeTabChanged || (tabbedContent.onBeforeTabChanged && tabbedContent.onBeforeTabChanged(tabNo)))
            {

                for (var i = 0; i < tabbedContent.tabCount; i++)
                {
                    if (i == tabNo)
                        tabbedContent.tabs[i].parentNode.className = "TabIn";
                    else
                        tabbedContent.tabs[i].parentNode.className = "Tab";
                }
                for (var i = 0; i < tabbedContent.panelCount; i++)
                {
                    if (i == tabNo)
                    {
                        tabbedContent.panels[i].parentNode.className = tabbedContent.panelClass[i];
                        tabbedContent.panels[i].parentNode.style.display = "";
                    }
                    else
                        tabbedContent.panels[i].parentNode.className = tabbedContent.panelClass[i] + " TabPanelOff";
                }

                if (inpSelected) inpSelected.value = tabNo + 1;
                tabbedContent.selectedTab = tabNo;

                if (tabbedContent.onTabChanged != null)
                    tabbedContent.onTabChanged(tabbedContent.selectedTab);

            }
        }
    };
    
    function tabbedContentManager()
    {
        var tabbedContentManager = this;
        tabbedContentManager.tabbedContentCount = 0;
        tabbedContentManager.tabbedContent = new Object();
    }

    tabbedContentManager.prototype =
    {
        create: function(baseID, noTabs)
        {
            var tabbedContentManager = this, tc = new tabbedContent(baseID, noTabs);
            tabbedContentManager.tabbedContent[tabbedContentManager.tabbedContentCount] = tc;
            tabbedContentManager.tabbedContentCount++;
            return tc;
        },

        getByBaseID: function(baseID)
        {
            var tabbedContentManager = this;
            for (var i = 0; i < tabbedContentManager.tabbedContentCount; i++)
            {
                if (tabbedContentManager.tabbedContent[i].baseID == baseID)
                    return tabbedContentManager.tabbedContent[i];
            }
        },

        showTab: function(baseID, tabNo)
        {
            try
            {
                var tabbedContentManager = this, tc = tabbedContentManager.getByBaseID(baseID);
                if (tc) tc.showTab(tabNo);
            }
            catch (e) { };
        }
    };
    
    var tabbedcontentmanager = new tabbedContentManager();
    
    function helpPopUpManager()
    {
        this.divPopupBlocker = null;
        this.divPopup = null;
    }
    
    helpPopUpManager.prototype =
    {
        initialise: function()
        {
            for (var i = 0; i < document.links.length; i++)
            {
                if (document.links[i].className.indexOf("HelpPopUp") > -1)
                {
                    document.links[i].id = "helppopup" + i;
                    document.links[i].href = "javascript:helppopupmanager.launchPopup('" + document.links[i].href + "', 'helppopup" + i + "');";
                    document.links[i].target = "";
                }
            }
            this.divPopupBlocker = document.createElement("div");
            this.divPopupBlocker.className = "PopUpBlocker";
            this.divPopupBlocker.style.textAlign = "left";
            this.divPopup = document.createElement("div");
            this.divPopupBlocker.appendChild(this.divPopup);
        },
        
        getXMLHTTP: function()
        {
            var obj = null;

            try
            {
                obj = new ActiveXObject("Msxml2.XMLHTTP");
            }
            catch (e) { }

            if (obj == null)
            {
                try
                {
                    obj = new ActiveXObject("Microsoft.XMLHTTP");
                }
                catch (e) { }
            }

            if ((obj == null) && (typeof XMLHttpRequest != "undefined"))
                obj = new XMLHttpRequest();

            return(obj);
        },
        
        launchPopup: function(url, elID)
        {
            var xmlHTTP = this.getXMLHTTP();
            if (xmlHTTP)
            {
                xmlHTTP.open("GET", url, false);
                xmlHTTP.send("");
                var failed = false;
                
                if (xmlHTTP.status != 200)
                    failed = true
                else
                {
                    var html = xmlHTTP.responseText;
                    var bodyStart = html.indexOf("<body");
                    if (bodyStart > -1) bodyStart = html.indexOf(">", bodyStart);
                    if (bodyStart > -1)
                    {
                        var bodyEnd = html.indexOf("</body");
                        if (bodyEnd > -1)
                        {
                            this.divPopup.innerHTML = html.substring(bodyStart + 1, bodyEnd);
                            document.body.appendChild(this.divPopupBlocker);
                            this.center(elID);
                        }
                        else
                            failed = true;
                    }
                    else
                        failed = true;
                }
                
                if (failed) window.open(url);
            }
        },
        
        center: function(elID)
        {
            var el = document.getElementById(elID);
            if (el)
            {
                var x = findPosX(el), y = findPosY(el);
                this.divPopup.style.position = "absolute";
                this.divPopup.style.left = "50%";
                this.divPopup.style.marginLeft = -(this.divPopup.offsetWidth / 2) + "px";
                this.divPopup.style.top = y + el.offsetHeight + "px";
            }
        },
        
        closePopUp: function()
        {
            try
            {
                document.body.removeChild(this.divPopupBlocker);
            }
            catch (e) {}
        }
        
    };
    
    var helppopupmanager = new helpPopUpManager();
    
    function initialiseHelpPopUps()
    {
        var qs = new queryStringManager();
        var designmode = qs.get("designmode", "");
        if (designmode != "on")
            helppopupmanager.initialise();
    }
    
    function getFirstParentNode(n, nodeName)
    {
        var node = n;
        var nodeNames = nodeName.split("|");
        while (node)
        {
            for (var i = 0; i < nodeNames.length; i++)
            {
                if (nodeNames[i] != "" && nodeNames[i].toUpperCase() == node.nodeName.toUpperCase()) return node;
            }
            node = node.parentNode;
        }
        return null;
    }
    
    // Default Button
    function clickButton(btn, event)
    {
        if ((event.keyCode && event.keyCode == 13) || (event.which && event.which == 13))
        {
            var oBtn = document.getElementById(btn);
            if (oBtn)
            {
                event.returnValue = false;
                event.cancel = true;
                if (event.preventDefault) event.preventDefault();
                oBtn.click();
            }
        }
    }
    
    function findPosX(obj)
    {
        if (!obj) return 0;
        var curleft = 0;
        if(obj.offsetParent)
            while(1) 
            {
                curleft += obj.offsetLeft;
                if(!obj.offsetParent)
                    break;
                obj = obj.offsetParent;
            }
        else if(obj.x)
            curleft += obj.x;
        return curleft;
    }

    function findPosY(obj)
    {
        if (!obj) return 0;    
        var curtop = 0;
        if(obj.offsetParent)
            while(1)
            {
                curtop += obj.offsetTop;
                if(!obj.offsetParent)
                    break;
                obj = obj.offsetParent;
            }
        else if(obj.y)
            curtop += obj.y;
        return curtop;
    }
    
    if (window.addEventListener)
        window.addEventListener("load", initialiseHelpPopUps, false);
    else if (window.attachEvent)
        window.attachEvent("onload", initialiseHelpPopUps);
        
    function exitSurvey()
    {
        this.close = function()
        {
            var
                divButton = document.getElementById("exitsurvey_popup"),
                divPanel = document.getElementById("exitsurvey"),
                divThankyou = document.getElementById("exitsurvey_thankyou");
            
            divButton.style.display = "none";
            divPanel.style.display = "none";
            divThankyou.style.display = "none";
            
            var expiryDate = new Date();
            expiryDate.setDate(expiryDate.getDate() + 90);
            document.cookie = "exitSurvey=off; expires=" + expiryDate.toGMTString() + "; path=/";
        };
        
        this.showPanel = function()
        {
            var
                divButton = document.getElementById("exitsurvey_popup"),
                divPanel = document.getElementById("exitsurvey"),
                divThankyou = document.getElementById("exitsurvey_thankyou");
            
            divButton.style.display = "none";
            divPanel.style.display = "";
            divThankyou.style.display = "none";
        };
    }
    
    var exitsurvey = new exitSurvey();

    var oGLocalSearch = new GlocalSearch();
    
    function geocodeSubmit(path, elID, event, qs)
    {
        var el = document.getElementById(elID), isPostcode = false;
        
        if (/^[a-zA-Z]{1,2}[0-9][0-9A-Za-z]{0,1}[ ]{0,1}[0-9][A-Za-z]{2}$/.test(el.value))
            isPostcode = true;
        
        if (isPostcode)
        {
            el.value = el.value.toUpperCase();
            if (el.value.indexOf(" ") == -1)
            {
                if (el.value.length > 4)
                    el.value = el.value.substr(0, el.value.length - 3) + " " + el.value.substr(el.value.length - 3)
            }
            oGLocalSearch.setSearchCompleteCallback
            (
                null,
                function()
                {
                    if (oGLocalSearch.results[0])
                        window.location = path + "?loc=" + el.value + "&lat=" + oGLocalSearch.results[0].lat + "&lng=" + oGLocalSearch.results[0].lng + (qs ? "&" + qs : "");
                    else
                        alert("Postcode not found!");
                }
            );
            oGLocalSearch.execute(el.value + ", UK");
        }
        else
            window.location = path + "?loc=" + el.value + (qs ? "&" + qs : "");
        
        if (event)
        {
            if (event.preventDefault) event.preventDefault();
            event.returnValue = false;
            event.cancel = true;
        }
        return false;
    }