﻿        //Function that appends params to URL to make server call for dialog content.
        function prepURLForServer(URL, ItemID, ItemText)
        {
            var Separator = "";
        
            //Determine if this URL already has a QueryString, if so, then Separator is an Ampersand, otherwise it is a Question Mark.
            if ((URL) && (URL != "") && (URL != "#"))
            {
                if (URL.indexOf("?") > 0)
                {
                  Separator = "&";
                }
                else
                {
                  Separator = "?";
                }


                //See if we need to append ItemID 
                if ((ItemID) && (ItemID != "") && null == URL.match("ItemID") )
                {
                    URL += Separator + "ItemID=" + encodeURIComponent(ItemID);
                    
                    Separator = "&";  //Get this ready for next item, whether we need it or not.
                }
                    
                if ((ItemText) && (ItemText != ""))
                {
                    URL += Separator + "ItemText=" + encodeURIComponent(ItemText);
                    
                    Separator = "&";  //Get this ready for next item, whether we need it or not.
                }
            }
            
            return(URL);
        }

        function isIE()
        {
            return /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent);
        }
        
        function isIE6()
        {
            return((navigator.userAgent.toLowerCase().indexOf('msie 6') != -1) && (navigator.userAgent.toLowerCase().indexOf('msie 7') == -1));
        }

        function isIE7() {
            return (navigator.userAgent.toLowerCase().indexOf('msie 7') != -1);
        }
        
        function isFF()
        {
            return((navigator.userAgent.toLowerCase().indexOf("firefox")!=-1));
        }

        function trim(str, chars) 
        {
	        return ltrim(rtrim(str, chars), chars);
        }
         
        function ltrim(str, chars) 
        {
	        chars = chars || "\\s";
	        return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
        }
         
        function rtrim(str, chars) 
        {
	        chars = chars || "\\s";
	        return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
	    }

	    function updateCharsRemaining(sTextBoxID, sDisplayID, iMaxLengthIn) {
	        var oTextBox = document.getElementById(sTextBoxID);
	        var oDisplay = document.getElementById(sDisplayID);

	        if ((oTextBox) && (oDisplay)) {
	            var iCurLength = oTextBox.value.length;
	            var iMaxLength = parseInt(oTextBox.getAttribute("maxlength"));

	            if (iMaxLengthIn) {
	                iMaxLength = iMaxLengthIn;
	            }

	            if ((iMaxLength - iCurLength) < 0) {
	                oTextBox.value = oTextBox.value.substring(0, iMaxLength);

	                iCurLength = oTextBox.value.length;
	            }

	            oDisplay.innerHTML = "" + (iMaxLength - iCurLength);
	        }

	    }

/*  A couple of handy findChildElement functions  */

/*  findChildElementByID()  & findChildElementByClass()
    Copyright 2009 by Doug Hamilton, radQuest, Inc.
    
    Written from scratch, as I didn't find 
    satisfactory similar functions online.

    May be freely redistributed, unmodified, as long as this 
    Copyright note is included.
*/

//  NOTE: jQuery does provide for some of this functionality, but
//        I couldn't get it to do the things I needed with an actual
//        element [node] pointer...

    function findChildElementByID(oParent, sID)
    {
        var retElement = null;
        var curElement = null;
        var ChildNodes = null;

        if (oParent)
        {
            ChildNodes = oParent.childNodes;

            for (var i = 0; i < ChildNodes.length; i++)
            {
                curElement = ChildNodes[i];

                if ((curElement) && (curElement.id) && (curElement.id == sID))
                {
                    retElement = curElement;
                }

                if (retElement)
                    break;

                curElement = findChildElementByID(curElement, sID);

                if ((curElement) && (curElement.id) && (curElement.id == sID))
                {
                    retElement = curElement;
                }

                if (retElement)
                    break;
            }
        }

        return (retElement);
    }

    function findChildElementByClass(oParent, sClass)
    {
        var retElement = null;
        var curElement = null;
        var ChildNodes = null;

        if (oParent)
        {
            ChildNodes = oParent.childNodes;

            for (var i = 0; i < ChildNodes.length; i++)
            {
                curElement = ChildNodes[i];

                if ((curElement) && (curElement.className) && (curElement.className.indexOf(sClass) >= 0))
                {
                    retElement = curElement;
                }

                if (retElement)
                    break;

                curElement = findChildElementByClass(curElement, sClass);

                if ((curElement) && (curElement.className) && (curElement.className.indexOf(sClass) >= 0))
                {
                    retElement = curElement;
                }

                if (retElement)
                    break;
            }
        }

        return (retElement);
    }

    function getQueryVariable(QueryString, VariableName, DefaultValue) 
    { 
        if (DefaultValue == null)
          DefaultValue = "";
          
        VariableName = VariableName.toLowerCase();
    
        var Vars = QueryString.split("&"); 

        for (var i=0; i < Vars.length; i++) 
        { 
            var pair = Vars[i].split("="); 
            
            if (pair[0].toLowerCase() == VariableName) 
            { 
                var retVal = pair[1];
                
                for (var j=2; j < pair.length; j++)
                {
                    retVal += "=" + pair[j];
                }
                
                return retVal; 
            } 
        } 
        
        return(DefaultValue);
    }

    function determineBarNum(Bar)
    {
        var BarNumber = 0;

        Bar = ("" + Bar).toLowerCase();

        if ((parseInt(Bar) > 0) && (parseInt(Bar) <= 5))
        {
            BarNumber = parseInt(Bar);
        }
        else
        {
            if ((Bar.indexOf("product") == 0) || (Bar.indexOf("red") == 0) || (Bar.indexOf("/product") >= 0))
            {
                BarNumber = 5;
            }
            else if ((Bar.indexOf("galler") == 0) || (Bar.indexOf("blue") == 0) || (Bar.indexOf("/galler") >= 0))
            {
                BarNumber = 4;
            }
            else if ((Bar.indexOf("rep") == 0) || (Bar.indexOf("green") == 0) || (Bar.indexOf("/rep") >= 0))
            {
                BarNumber = 3;
            }
            else if ((Bar.indexOf("about") == 0) || (Bar.indexOf("yellow") == 0) || (Bar.indexOf("/about") >= 0) || (Bar.indexOf("location") >= 0) || (Bar.indexOf("/location") >= 0) || (Bar.indexOf("dynamic") >= 0) || (Bar.indexOf("/dynamic") >= 0))
            {
                BarNumber = 2;
            }
            else if ((Bar.indexOf("my") == 0) || (Bar.indexOf("project") == 0) || (Bar.indexOf("inventory") == 0) || (Bar.indexOf("literature") == 0) || (Bar.indexOf("document") == 0) || (Bar.indexOf("thankyou") == 0) || (Bar.indexOf("tradeshow") == 0) || (Bar.indexOf("travel") == 0) || (Bar.indexOf("download") == 0) || (Bar.indexOf("estimate") == 0) || (Bar.indexOf("rapidentry") == 0) || 
                     (Bar.indexOf("/my") >= 0) || (Bar.indexOf("/project") >= 0) || (Bar.indexOf("/inventory") >= 0) || (Bar.indexOf("/literature") >= 0) || (Bar.indexOf("/document") >= 0) || (Bar.indexOf("/thankyou") >= 0) || (Bar.indexOf("/tradeshow") >= 0) || (Bar.indexOf("/travel") >= 0) || (Bar.indexOf("/download") >= 0) || (Bar.indexOf("/estimate") >= 0) || (Bar.indexOf("/rapidentry") == 0) ||
                     (Bar.indexOf("bargray") == 0) || (Bar.indexOf("gray") == 0) || (Bar.indexOf("/quote") >= 0) || (Bar.indexOf("quote") >= 0))
            {
                BarNumber = 1;
            }
        }

        return (BarNumber);
    }

    function urlEncode(sURL)
    {
        var BasePath = "";
        
        try
        {
            BasePath = ServerBasePath;            
        }
        catch (ex)
        {
          if ((BasePath = "") && (window.top) && (window.top.ServerBasePath))
          {
              BasePath = window.top.ServerBasePath;            
          }
        }
    
        //Strip off any http://, hostname & base path info.
        return(encodeURIComponent(sURL.replace(document.location.protocol + "//" + document.location.host + BasePath + "/", "").replace(document.location.protocol + "//" + document.location.host + BasePath, "").replace(document.location.protocol + "//" + document.location.host + BasePath.toLowerCase() + "/", "").replace(document.location.protocol + "//" + document.location.host + BasePath.toLowerCase(), "")));

//        sURL = sURL.replace(/%20/g, '+');

//        return(sURL);
    }

    function urlDecode(sURL)
    {
        //Strip off any http://, hostname & base path info.
        return(decodeURIComponent(sURL));

//        sURL = sURL.replace(/%20/g, '+');

//        return(sURL);
    }
    
    //Placed here for both portability and a little bit of "security" through obscurity
    function getCurrentUserID()
    {
        var userID = 0;

        if( $.cookie(UserIDCookieName) != null ){
            userID = $.cookie(UserIDCookieName);
        }

        return(userID);
    }

