/// <summary>
 /// Executes on prerendering of the page
 /// </summary>
 /// <param name="e">EventArgs e</param>
 protected override void OnPreRender(EventArgs e)
 {
     try
     {
         ApplicationController objAppController = new ApplicationController();
         if (!objAppController.CheckRequestExtension(Request))
         {
             base.OnPreRender(e);
             SetGoogleAnalytics();
             SageFrameConfig objSageConfig = new SageFrameConfig();
             bool IsCompressCss = bool.Parse(objSageConfig.GetSettingsByKeyIndividual(SageFrameSettingKeys.OptimizeCss));
             bool allowOptimization = bool.Parse(objSageConfig.GetSettingsByKeyIndividual(SageFrameSettingKeys.OptimizeJs));
             if (!isDefaultAdminPage())
             {
                 if (IsCompressCss)
                 {
                     BundleCss();
                 }
                 else
                 {
                     LoadModuleCss();
                 }
             }
             if (!IsAdmin() && allowOptimization)
             {
                 BundleJS();
             }
             else
             {
                 LoadModuleJs();
             }
             HttpContext.Current.Session[SessionKeys.ModuleCss] = new List<CssScriptInfo>();
             HttpContext.Current.Session[SessionKeys.ModuleJs] = new List<CssScriptInfo>();
             HttpContext.Current.Session.Remove(SessionKeys.ModuleJs);
             HttpContext.Current.Session.Remove(SessionKeys.ModuleCss);
         }
     }
     catch (Exception ex)
     {
         ProcessException(ex);
     }
 }
        /// <summary>
        /// Compress All The Css Of All The Modules At Once And Load It
        /// </summary>
        public void LoadModuleCss()
        {
            string activeTemplate = GetActiveTemplate;
            List<CssScriptInfo> lstModuleResources = new List<CssScriptInfo>();
            StringBuilder modulecss = new StringBuilder();
            if (HttpContext.Current.Session[SessionKeys.ModuleCss] != null)
            {
                lstModuleResources = HttpContext.Current.Session[SessionKeys.ModuleCss] as List<CssScriptInfo>;
            }
            if (!IsAdmin())
            {
                lstModuleResources.AddRange(CoreCss.GetTemplateCss(activeTemplate));
            }
            List<KeyValue> lstCssInclude = new List<KeyValue>();
            List<string> lstCss = new List<string>();
            bool isTemplateDefault = Decide.IsTemplateDefault(activeTemplate);
            bool isAdmin = IsAdmin();

            if (lstModuleResources != null)
            {
                PresetInfo preset = GetPresetDetails;
                string templatePathFirst = isTemplateDefault ? "Core" : "Templates";
                string templatePathSecond = isTemplateDefault ? "Template" : activeTemplate;

                #region "Get Modules Resources path"

                foreach (CssScriptInfo css in lstModuleResources)
                {
                    lstCss.Add(css.ModuleName.ToLower());
                    string fullPath_theme = Server.MapPath(string.Format("~/{0}/{1}/themes/{2}/modules/{3}",
                        templatePathFirst, templatePathSecond, preset.ActiveTheme, css.ModuleName));

                    string fullPath_template = Server.MapPath(string.Format("~/{0}/{1}/modules/{2}",
                        templatePathFirst, templatePathSecond, css.ModuleName));

                    string fullPath_module = Server.MapPath(string.Format("~/{0}", css.Path));

                    #region "Strategy 3-Priority-3:Check at the module level(the default fallback)"

                    ///Strategy 3-Priority-3:Check at the module level(the default fallback)
                    if (Directory.Exists(fullPath_module))
                    {
                        ///Check to see if the file exists in the root level
                        if (File.Exists(string.Format("{0}/{1}", fullPath_module, css.FileName)))
                        {
                            lstCssInclude.Add(new KeyValue(string.Format("~/{0}/{1}", css.Path, css.FileName), css.Path));
                        }
                        ///Check to see if the file exists in the css folder
                        else if (File.Exists(string.Format("{0}/css/{1}", fullPath_module, css.FileName)))
                        {
                            lstCssInclude.Add(new KeyValue(string.Format("~/{0}/{1}", css.Path, css.FileName), css.Path));
                        }
                    }

                    #endregion

                    #region "Strategy 1-Priority-1:Check the themes"

                    ///Strategy 1-Priority-1:Check the themes                   
                    if (Directory.Exists(fullPath_theme))
                    {
                        ///Check to see if the file exists in the root level
                        if (File.Exists(fullPath_theme + "/" + css.FileName))
                        {
                            lstCssInclude.Add(new KeyValue(string.Format("~/{0}/{1}/themes/{2}/modules/{3}/{4}",
                                templatePathFirst, templatePathSecond, preset.ActiveTheme, css.ModuleName, css.FileName), css.Path));
                        }
                        ///Check to see if the file exists in the css folder
                        else if (File.Exists(string.Format("{0}/css/{1}", fullPath_theme, css.FileName)))
                        {
                            lstCssInclude.Add(new KeyValue(string.Format("~/{0}/{1}/themes/{2}/modules/{3}/css/{4}",
                                templatePathFirst, templatePathSecond, preset.ActiveTheme, css.ModuleName, css.FileName), css.Path));

                        }
                    }

                    #endregion

                    #region "Strategy 2-Priority-2:Check at the template level"

                    ///Strategy 2-Priority-2:Check at the template level
                    else if (Directory.Exists(fullPath_template))
                    {
                        ///Check to see if the file exists in the root level
                        if (File.Exists(string.Format("{0}/{1}", fullPath_template, css.FileName)))
                        {
                            lstCssInclude.Add(new KeyValue(string.Format("~/{0}/{1}/modules/{2}/{3}", templatePathFirst, templatePathSecond, css.ModuleName, css.FileName), css.Path));
                        }
                        ///Check to see if the file exists in the css folder
                        else if (File.Exists(string.Format("{0}/css/{1}", fullPath_template, css.FileName)))
                        {
                            lstCssInclude.Add(new KeyValue(string.Format("~/{0}/{1}/modules/{2}/css/{3}", templatePathFirst, templatePathSecond, css.ModuleName, css.FileName), css.Path));
                        }
                    }

                    #endregion
                }

                #endregion

                #region "Css Compress"

                SageFrameConfig pagebase = new SageFrameConfig();
                bool IsCompressCss = bool.Parse(pagebase.GetSettingsByKeyIndividual(SageFrameSettingKeys.OptimizeCss));
                if (IsCompressCss && !isAdmin && !IsHandheld())
                {
                    ///1.Loop through the list
                    ///2.Read the Css File
                    ///3.Rewrite the Image Paths in Css Files
                    ///4.Compress the Css file 
                    ///5.Include it in the Css Literal
                    string templateTypeName = isAdmin ? "admintemplate" : "template";
                    lstCss.Insert(0, templateTypeName);
                    string[] cssArr = lstCss.ToArray().Distinct().ToArray();

                    #region "Synchronize the cache and the map data in the files"

                    ///Check cache and refresh it if the files optimized folder do not exist
                    ///Synchronize the cache and the map data in the files
                    Hashtable hst = new Hashtable();
                    if (HttpRuntime.Cache[CacheKeys.SageFrameCss] != null)
                    {
                        hst = (Hashtable)HttpRuntime.Cache[CacheKeys.SageFrameCss];
                        Hashtable hstNew = new Hashtable();
                        foreach (string modulekey in hst.Keys)
                        {
                            string file = string.Format("{0}.css", hst[modulekey].ToString());
                            if (File.Exists(Server.MapPath(string.Format("~/Optimized/{0}", file))))
                            {
                                hstNew.Add(modulekey, hst[modulekey].ToString());
                            }
                        }
                        HttpRuntime.Cache[CacheKeys.SageFrameCss] = hstNew;
                    }

                    #endregion

                    #region "Read the map file and check if the css for this combination already exists"

                    ///Read the map file and check if the css for this combination already exists
                    if (HttpRuntime.Cache[CacheKeys.SageFrameCss] != null)
                    {
                        hst = (Hashtable)HttpRuntime.Cache[CacheKeys.SageFrameCss];
                    }
                    else
                    {
                        XmlDocument doc = SageFrame.Templating.xmlparser.XmlHelper.LoadXMLDocument(Server.MapPath("~/Optimized/map_css.xml"));
                        XmlNode xnresourcemap = doc.SelectSingleNode("resourcemaps");
                        XmlNodeList xnlist = xnresourcemap.ChildNodes;
                        foreach (XmlNode node in xnlist)
                        {
                            string modules = node.SelectSingleNode("modules").InnerText;
                            string map = node.SelectSingleNode("map").InnerText;
                            if (modules != "" && !hst.Contains(modules))
                                hst.Add(modules, map);
                        }
                    }

                    #endregion

                    bool IsExists = false;
                    string optimizedcss = string.Empty;
                    #region "Get optimize css Path"

                    foreach (string modulekey in hst.Keys)
                    {
                        string modules = modulekey;
                        string[] modulesArr = modules.Split(',');
                        if (ArrayHelper.ArraysEqual<string>(cssArr, modulesArr))
                        {
                            IsExists = true;
                            optimizedcss = string.Format("{0}.css", hst[modulekey].ToString());
                            break;
                        }
                    }

                    #endregion

                    #region "Css Optimization"

                    if (IsExists)
                    {
                        IsExists = File.Exists(Server.MapPath(string.Format("~/Optimized/{0}", optimizedcss)));
                    }
                    if (!IsExists)
                    {
                        PresetInfo presetObj = GetPresetDetails;
                        string uniqueid = GenerateUniqueId();
                        XmlDocument doc = SageFrame.Templating.xmlparser.XmlHelper.LoadXMLDocument(Server.MapPath("~/Optimized/map_css.xml"));
                        XmlNode xnresourcemap = doc.SelectSingleNode("resourcemaps");
                        string optimized_css_path = Server.MapPath(string.Format("~/Optimized/{0}.css", uniqueid));
                        ///Write the combination into the map file
                        XmlElement resourcemap = doc.CreateElement("resourcemap");
                        XmlElement modules = doc.CreateElement("modules");
                        XmlElement map = doc.CreateElement("map");
                        modules.InnerText = string.Join(",", cssArr);
                        map.InnerText = uniqueid;
                        resourcemap.AppendChild(modules);
                        resourcemap.AppendChild(map);
                        xnresourcemap.AppendChild(resourcemap);
                        if (cssArr.Length > 0 && !hst.Contains(string.Join(",", cssArr)))
                        {
                            doc.Save(Server.MapPath("~/Optimized/map_css.xml"));

                            ///Hashtable+Cache needs to be reset

                            hst.Add(string.Join(",", cssArr), uniqueid);
                            HttpRuntime.Cache[CacheKeys.SageFrameCss] = hst;
                            ///Only when the optimized file does not exists..else the development mode needs to be on to recreate the css file
                            if (!File.Exists(optimized_css_path))
                            {
                                using (StreamWriter sw = new StreamWriter(optimized_css_path))
                                {
                                    ///Read the template.css file

                                    string compressedcss = "";
                                    string uncompcss = "";
                                    string templatecsspath = isAdmin ? GetAdminTemplatePath() : GetTemplateCssPath();
                                    string imageWritePath = isTemplateDefault ? string.Format("/Core/Template/css") : string.Format("/Templates/{0}/css", activeTemplate);
                                    string imagerewrite = isAdmin ? "/Administrator/Templates/Default/css/" : imageWritePath;
                                    string cssText = isAdmin ? "admin.css" : "template.css";
                                    string path = Server.MapPath("~/js/jquery-ui-1.8.14.custom/css/redmond/jquery-ui-1.8.16.custom.css");
                                    using (StreamReader rdr = new StreamReader(path))
                                    {
                                        uncompcss = rdr.ReadToEnd();
                                    }
                                    compressedcss = CssMinifier.CssMinify(uncompcss);
                                    compressedcss = CssMinifier.RewriteCssImagePath(compressedcss, "/js/jquery-ui-1.8.14.custom/css/redmond/", GetAppPath(), "images");

                                    sw.Write("\n");
                                    sw.Write("/*-----JQuery UI----*/");
                                    sw.Write("\n");
                                    sw.Write(compressedcss);
                                    sw.Write("\n");

                                    ///Read the module files    
                                    if (!isAdmin && !IsHandheld())
                                    {
                                        lstCssInclude.Insert(0, new KeyValue("~/Administrator/Templates/Default/css/front.css", "/Administrator/Templates/Default/css/"));
                                        lstCssInclude.Insert(1, new KeyValue("~/Administrator/Templates/Default/css/core.css", "/Administrator/Templates/Default/css/"));
                                    }
                                    else
                                    {
                                        lstCssInclude.Insert(0, new KeyValue("~/Administrator/Templates/Default/css/core.css", "/Administrator/Templates/Default/css/"));

                                    }
                                    foreach (KeyValue cssfile in lstCssInclude)
                                    {
                                        using (StreamReader rdr = new StreamReader(Server.MapPath(cssfile.Key)))
                                        {
                                            uncompcss = rdr.ReadToEnd();
                                        }
                                        compressedcss = CssJscriptOptimizer.Minifiers.CssMinifier.CssMinify(uncompcss);
                                        compressedcss = CssJscriptOptimizer.Minifiers.CssMinifier.RewriteCssImagePath(compressedcss, string.Format("{0}", cssfile.Value), GetAppPath(), "images");

                                        sw.Write("\n");
                                        sw.Write("/*-----" + Path.GetFileName(cssfile.Key) + "----*/");
                                        sw.Write("\n");
                                        sw.Write(compressedcss);
                                        sw.Write("\n");
                                    }


                                    using (StreamReader rdr = new StreamReader(templatecsspath))
                                    {
                                        uncompcss = rdr.ReadToEnd();
                                    }
                                    compressedcss = CssJscriptOptimizer.Minifiers.CssMinifier.CssMinify(uncompcss);
                                    compressedcss = CssJscriptOptimizer.Minifiers.CssMinifier.RewriteCssImagePath(compressedcss, imagerewrite, GetAppPath(), "images");
                                    sw.Write("\n");
                                    sw.Write("/*-----'" + cssText + "'----*/");
                                    sw.Write("\n");
                                    sw.Write(compressedcss);
                                    sw.Write("\n");
                                }
                                Literal SageFrameModuleCSSlinks = this.Page.FindControl("SageFrameModuleCSSlinks") as Literal;
                                if (SageFrameModuleCSSlinks != null)
                                {
                                    string linkText = "<link href=\"" + Page.ResolveUrl(string.Format("~/Optimized/{0}.css", uniqueid)) + "\" rel=\"stylesheet\" type=\"text/css\" />";
                                    SageFrameModuleCSSlinks.Text += linkText;
                                }
                            }
                        }
                        else
                        {
                            Literal SageFrameModuleCSSlinks = this.Page.FindControl("SageFrameModuleCSSlinks") as Literal;
                            if (SageFrameModuleCSSlinks != null)
                            {
                                string linkText = "<link href=\"" + Page.ResolveUrl("~/Optimized/" + optimizedcss) + "\" rel=\"stylesheet\" type=\"text/css\" />";
                                SageFrameModuleCSSlinks.Text += linkText;
                            }
                        }
                    }
                    else
                    {
                        Literal SageFrameModuleCSSlinks = this.Page.FindControl("SageFrameModuleCSSlinks") as Literal;
                        if (SageFrameModuleCSSlinks != null)
                        {
                            string linkText = "<link href=\"" + Page.ResolveUrl("~/Optimized/" + optimizedcss) + "\" rel=\"stylesheet\" type=\"text/css\" />";
                            SageFrameModuleCSSlinks.Text += linkText;
                        }
                    }

                    #endregion
                }
                else
                {
                    Literal SageFrameModuleCSSlinks = this.Page.FindControl("SageFrameModuleCSSlinks") as Literal;
                    if (SageFrameModuleCSSlinks != null)
                    {
                        SageFrameModuleCSSlinks.Text = "";
                    }
                    SetTemplateCss();
                    AddModuleCssToPage("~/js/jquery-ui-1.8.14.custom/css/redmond/jquery-ui-1.8.16.custom.css");
                    foreach (KeyValue cssfile in lstCssInclude)
                    {
                        AddModuleCssToPage(cssfile.Key);
                    }
                }

                #endregion
            }

        }
        public void LoadModuleCss()
        {
            string activeTemplate = GetActiveTemplate;
            List<CssScriptInfo> lstModuleResources = new List<CssScriptInfo>();
            bool isAdmin = IsAdmin();
            if (HttpContext.Current.Session[SessionKeys.ModuleCss] != null)
            {
                lstModuleResources = HttpContext.Current.Session[SessionKeys.ModuleCss] as List<CssScriptInfo>;
            }
            if (!isAdmin)
            {
                lstModuleResources.AddRange(CoreCss.GetTemplateCss(activeTemplate));
            }
            List<KeyValue> lstCssInclude = new List<KeyValue>();
            List<string> lstCss = new List<string>();
            bool isTemplateDefault = Decide.IsTemplateDefault(activeTemplate);
            if (lstModuleResources != null)
            {
                PresetInfo preset = GetPresetDetails;
                string templatePathFirst = isTemplateDefault ? "Core" : "Templates";
                string templatePathSecond = isTemplateDefault ? "Template" : activeTemplate;

                #region "Get Modules Resources path"

                foreach (CssScriptInfo css in lstModuleResources)
                {
                    lstCss.Add(css.ModuleName.ToLower());
                    string fullPath_theme = Server.MapPath(string.Format("~/{0}/{1}/themes/{2}/modules/{3}",
                        templatePathFirst, templatePathSecond, preset.ActiveTheme, css.ModuleName));

                    string fullPath_template = Server.MapPath(string.Format("~/{0}/{1}/modules/{2}",
                        templatePathFirst, templatePathSecond, css.ModuleName));

                    string fullPath_module = Server.MapPath(string.Format("~/{0}", css.Path));

                    #region "Strategy 3-Priority-3:Check at the module level(the default fallback)"

                    ///Strategy 3-Priority-3:Check at the module level(the default fallback)
                    if (Directory.Exists(fullPath_module))
                    {
                        ///Check to see if the file exists in the root level
                        if (File.Exists(string.Format("{0}/{1}", fullPath_module, css.FileName)))
                        {
                            lstCssInclude.Add(new KeyValue(string.Format("~/{0}/{1}", css.Path, css.FileName), css.Path));
                        }
                        ///Check to see if the file exists in the css folder
                        else if (File.Exists(string.Format("{0}/css/{1}", fullPath_module, css.FileName)))
                        {
                            lstCssInclude.Add(new KeyValue(string.Format("~/{0}/{1}", css.Path, css.FileName), css.Path));
                        }
                    }

                    #endregion

                    #region "Strategy 1-Priority-1:Check the themes"

                    ///Strategy 1-Priority-1:Check the themes                   
                    if (Directory.Exists(fullPath_theme))
                    {
                        ///Check to see if the file exists in the root level
                        if (File.Exists(fullPath_theme + "/" + css.FileName))
                        {
                            lstCssInclude.Add(new KeyValue(string.Format("~/{0}/{1}/themes/{2}/modules/{3}/{4}",
                                templatePathFirst, templatePathSecond, preset.ActiveTheme, css.ModuleName, css.FileName), css.Path));
                        }
                        ///Check to see if the file exists in the css folder
                        else if (File.Exists(string.Format("{0}/css/{1}", fullPath_theme, css.FileName)))
                        {
                            lstCssInclude.Add(new KeyValue(string.Format("~/{0}/{1}/themes/{2}/modules/{3}/css/{4}",
                                templatePathFirst, templatePathSecond, preset.ActiveTheme, css.ModuleName, css.FileName), css.Path));

                        }
                    }

                    #endregion

                    #region "Strategy 2-Priority-2:Check at the template level"

                    ///Strategy 2-Priority-2:Check at the template level
                    else if (Directory.Exists(fullPath_template))
                    {
                        ///Check to see if the file exists in the root level
                        if (File.Exists(string.Format("{0}/{1}", fullPath_template, css.FileName)))
                        {
                            lstCssInclude.Add(new KeyValue(string.Format("~/{0}/{1}/modules/{2}/{3}", templatePathFirst, templatePathSecond, css.ModuleName, css.FileName), css.Path));
                        }
                        ///Check to see if the file exists in the css folder
                        else if (File.Exists(string.Format("{0}/css/{1}", fullPath_template, css.FileName)))
                        {
                            lstCssInclude.Add(new KeyValue(string.Format("~/{0}/{1}/modules/{2}/css/{3}", templatePathFirst, templatePathSecond, css.ModuleName, css.FileName), css.Path));
                        }
                    }

                    #endregion
                }

                #endregion

                #region "Css Load"

                SageFrameConfig pagebase = new SageFrameConfig();
                bool IsCompressCss = bool.Parse(pagebase.GetSettingsByKeyIndividual(SageFrameSettingKeys.OptimizeCss));

                Literal SageFrameModuleCSSlinks = this.Page.FindControl("SageFrameModuleCSSlinks") as Literal;
                if (SageFrameModuleCSSlinks != null)
                {
                    SageFrameModuleCSSlinks.Text = "";

                    foreach (KeyValue cssfile in lstCssInclude)
                    {
                        AddModuleCssToPage(cssfile.Key, SageFrameModuleCSSlinks);
                    }
                    if (IsUserLoggedIn())
                    {
                        AddModuleCssToPage("~/js/jquery-ui-1.8.14.custom/css/redmond/jquery-ui-1.8.16.custom.css", SageFrameModuleCSSlinks);
                    }
                    SetTemplateCss(SageFrameModuleCSSlinks);
                    if (isAdmin)
                    {
                        string cssColoredTemplate = string.Empty;
                        SageFrameConfig sageConfig = new SageFrameConfig();
                        string defaultAdminTheme = sageConfig.GetSettingValueByIndividualKey(SageFrameSettingKeys.DefaultAdminTheme);
                        cssColoredTemplate = "~/Administrator/Templates/Default/themes/" + defaultAdminTheme + ".css";
                        AddModuleCssToPage(cssColoredTemplate, SageFrameModuleCSSlinks);
                    }
                }
                #endregion
            }

        }
        private void OptimizeJs(List<CssScriptInfo> lstJsColl, int Mode)
        {
            Literal LitSageScript = Mode == 1 ? Page.Header.FindControl("LitSageScript") as Literal : Page.Header.FindControl("SageFrameModuleCSSlinks") as Literal;

            bool isAdmin = IsAdmin();
            List<string> lstJs = new List<string>();
            foreach (CssScriptInfo js in lstJsColl)
            {
                lstJs.Add(js.ModuleName);
            }
            string portalType = isAdmin ? ApplicationKeys.admin.ToLower() : ApplicationKeys.portal;
            lstJs.Insert(0, portalType);
            SageFrameConfig pagebase = new SageFrameConfig();
            bool IsCompressJs = bool.Parse(pagebase.GetSettingsByKeyIndividual(SageFrameSettingKeys.OptimizeJs));

            #region "IncludeJs Logic"

            if (IsCompressJs && !isAdmin && !IsHandheld())
            {
                #region "Check cache and refresh it if the files optimized folder do not exist"

                Hashtable hst = new Hashtable();
                ///Check cache and refresh it if the files optimized folder do not exist              
                if (HttpRuntime.Cache[CacheKeys.SageFrameJs] != null)
                {
                    hst = (Hashtable)HttpRuntime.Cache[CacheKeys.SageFrameJs];
                    Hashtable hstNew = new Hashtable();
                    foreach (string modulekey in hst.Keys)
                    {
                        string file = string.Format("{0}.js", hst[modulekey].ToString());
                        if (File.Exists(Server.MapPath(string.Format("~/Optimized/{0}", file))))
                        {
                            hstNew.Add(modulekey, hst[modulekey].ToString());
                        }
                    }
                    HttpRuntime.Cache[CacheKeys.SageFrameJs] = hstNew;
                }

                if (HttpRuntime.Cache[CacheKeys.SageFrameJs] != null)
                {
                    hst = (Hashtable)HttpRuntime.Cache[CacheKeys.SageFrameJs];
                }
                else
                {
                    XmlDocument doc = SageFrame.Templating.xmlparser.XmlHelper.LoadXMLDocument(Server.MapPath("~/Optimized/map_js.xml"));
                    XmlNode xnresourcemap = doc.SelectSingleNode("resourcemaps");
                    XmlNodeList xnlist = xnresourcemap.ChildNodes;
                    foreach (XmlNode node in xnlist)
                    {
                        string modules = node.SelectSingleNode("modules").InnerText;
                        string map = node.SelectSingleNode("map").InnerText;
                        if (modules != "" && !hst.Contains(modules))
                            hst.Add(modules, map);
                    }

                }

                #endregion

                string[] jsArr = lstJs.ToArray().Distinct().ToArray();
                ///Read the map file and check if the css for this combination already exists

                bool IsExists = false;
                string optimizedjs = string.Empty;

                #region "Get Optimized Js file name"

                foreach (string modulekey in hst.Keys)
                {
                    string modules = modulekey;
                    string[] modulesArr = modules.Split(',');
                    if (ArrayHelper.ArraysEqual<string>(jsArr, modulesArr))
                    {
                        IsExists = true;
                        optimizedjs = string.Format("{0}.js", hst[modulekey].ToString());
                        break;
                    }
                }

                #endregion

                #region "Optimize JS"

                if (IsExists)
                {
                    IsExists = File.Exists(Server.MapPath(string.Format("~/Optimized/{0}", optimizedjs)));
                }
                if (!IsExists)
                {
                    string uniqueid = GenerateUniqueId();
                    XmlDocument doc = SageFrame.Templating.xmlparser.XmlHelper.LoadXMLDocument(Server.MapPath("~/Optimized/map_js.xml"));
                    XmlNode xnresourcemap = doc.SelectSingleNode("resourcemaps");
                    string optimized_js_path = Server.MapPath(string.Format("~/Optimized/{0}.js", uniqueid));
                    ///Write the combination into the map file
                    XmlElement resourcemap = doc.CreateElement("resourcemap");
                    XmlElement modules = doc.CreateElement("modules");
                    XmlElement map = doc.CreateElement("map");
                    modules.InnerText = string.Join(",", jsArr);
                    map.InnerText = uniqueid;
                    resourcemap.AppendChild(modules);
                    resourcemap.AppendChild(map);
                    xnresourcemap.AppendChild(resourcemap);
                    if (jsArr.Length > 0 && !hst.Contains(string.Join(",", jsArr)))
                    {
                        doc.Save(Server.MapPath("~/Optimized/map_js.xml"));
                        ///Hashtable+Cache needs to be reset

                        hst.Add(string.Join(",", jsArr), uniqueid);
                        HttpRuntime.Cache[CacheKeys.SageFrameJs] = hst;
                        ///Only when the optimized file does not exists..else the development mode needs to be on to recreate the css file
                        if (!File.Exists(optimized_js_path))
                        {
                            using (StreamWriter sw = new StreamWriter(optimized_js_path))
                            {

                                foreach (CssScriptInfo obj in lstJsColl)
                                {
                                    if (obj.AllowOptimization)
                                    {
                                        string uncompjs = "";
                                        string fullPath = string.Format("{0}/{1}/{2}", Request.PhysicalApplicationPath, obj.Path, obj.FileName);
                                        using (StreamReader rdr = new StreamReader(fullPath))
                                        {
                                            uncompjs = rdr.ReadToEnd();
                                        }
                                        string compressedjs = JsMinifier.GetMinifiedCode(uncompjs);
                                        sw.Write("\n");
                                        sw.Write("/*-----" + obj.FileName + "----*/");
                                        sw.Write("\n");
                                        sw.Write(compressedjs);
                                        sw.Write("\n");
                                    }
                                    else
                                    {

                                        string uncompressedjs = "<script src=\"" + ResolveUrl(string.Format("~/{0}/{1}", obj.Path, obj.FileName)) + "\" type=\"text/javascript\"></script>";
                                        LitSageScript.Text += uncompressedjs;
                                    }
                                }
                            }

                            string js = "<script src=\"" + ResolveUrl(string.Format("~/Optimized/{0}.js", uniqueid)) + "\" type=\"text/javascript\"></script>";
                            LitSageScript.Text += js;

                        }
                    }
                    else
                    {
                        string js = "<script src=\"" + ResolveUrl(string.Format("~/Optimized/{0}", optimizedjs)) + "\" type=\"text/javascript\"></script>";
                        LitSageScript.Text += js;
                    }
                }
                else
                {
                    string js = "<script src=\"" + ResolveUrl(string.Format("~/Optimized/{0}", optimizedjs)) + "\" type=\"text/javascript\"></script>";
                    LitSageScript.Text += js;
                    foreach (CssScriptInfo obj in lstJsColl)
                    {
                        if (!obj.AllowOptimization && LitSageScript != null)
                        {
                            string uncompressedjs = "<script src=\"" + ResolveUrl(string.Format("~/{0}/{1}", obj.Path, obj.FileName)) + "\" type=\"text/javascript\"></script>";
                            LitSageScript.Text += uncompressedjs;
                        }
                    }
                }

                #endregion
            }
            else
            {
                if (LitSageScript != null)
                {
                    foreach (CssScriptInfo obj in lstJsColl)
                    {
                        string js = "<script src=\"" + ResolveUrl(string.Format("~/{0}/{1}", obj.Path, obj.FileName)) + "\" type=\"text/javascript\"></script>";
                        LitSageScript.Text += js;
                    }
                }
            }

            #endregion
        }