using Common.Model; using System; using System.Collections.Generic; using System.Configuration; using System.Data; using System.IO; using System.Text; using System.Web; using System.Web.Script.Serialization; using System.Web.UI; namespace Common.Helper { public static class GetStatic { public static string GetUser() { var user = ReadSession("admin", ""); WriteSession("admin", user); //WriteSession("lastActiveTS", DateTime.Now.ToString()); return user; } public static string ReadSession(string key, string defVal) { try { return HttpContext.Current.Session[key] == null ? defVal : HttpContext.Current.Session[key].ToString(); } catch { return ""; } } public static void WriteSession(string key, string value) { HttpContext.Current.Session[key] = value; } public static string GetVirtualDirName() { return ReadWebConfig("virtualDirName"); } public static string ReadWebConfig(string key) { return ReadWebConfig(key, ""); } public static string ReadWebConfig(string key, string defValue) { return ConfigurationManager.AppSettings[key] ?? defValue; } public static string GetIp() { return HttpContext.Current.Request.ClientCertificate["REMOTE_ADDR"]; } public static void WriteCookie(string key, string value) { if (string.IsNullOrEmpty(value.Trim())) { DeleteCookie(key); return; } var httpCookie = new HttpCookie(key, value); httpCookie.Expires = DateTime.Now.AddDays(1); HttpContext.Current.Response.Cookies.Add(httpCookie); } public static string ReadCookie(string key, string defVal) { var cookie = HttpContext.Current.Request.Cookies[key]; return cookie == null ? defVal : HttpContext.Current.Server.HtmlEncode(cookie.Value); } public static void DeleteCookie(string key) { if (HttpContext.Current.Request.Cookies[key] != null) { var aCookie = new HttpCookie(key); aCookie.Expires = DateTime.Now.AddDays(-1); HttpContext.Current.Response.Cookies.Add(aCookie); } } public static void SetMessage(DbResponse value) { HttpContext.Current.Session["message"] = value; } public static void SetMessage(string responseCode, string msg) { var dbResult = new DbResponse { ResponseCode = responseCode, Msg = msg }; SetMessage(dbResult); } public static DbResponse ParseDbResult(DataTable dt) { var res = new DbResponse(); if (dt.Rows.Count > 0) { res.ResponseCode = dt.Rows[0][0].ToString(); res.Msg = dt.Rows[0][1].ToString(); res.Id = dt.Rows[0][2].ToString(); if (dt.Columns.Count > 3) { res.Extra = dt.Rows[0][3].ToString(); } if (dt.Columns.Count > 4) { res.Extra2 = dt.Rows[0][4].ToString(); } } return res; } public static void JsonResponse(T obk, Page page) { System.Web.Script.Serialization.JavaScriptSerializer jsonData = new System.Web.Script.Serialization.JavaScriptSerializer(); string jsonString = jsonData.Serialize(obk); page.Response.ContentType = "application/json"; page.Response.Write(jsonString); page.Response.End(); } public static void PrintMessage(Page page) { if (HttpContext.Current.Session["message"] == null) { //CallBackJs1(page, "Remove Message", "window.parent.RemoveMessageBox();"); return; } var dbResult = GetMessage(); CallBackJs(page, "Set Message", "window.parent.SetMessageBox(\"" + dbResult.Msg + "\",\"" + dbResult.ResponseCode + "\");"); HttpContext.Current.Session.Remove("message"); } public static DbResponse GetMessage() { return (DbResponse)HttpContext.Current.Session["message"]; } public static void CallBackJs(Page page, String scriptName, string functionName) { ScriptManager.RegisterStartupScript(page, page.GetType(), scriptName, functionName, true); } public static void PrintMessage(Page page, DbResponse dbResult) { CallBackJs(page, "Set Message", "window.parent.SetMessageBox(\"" + dbResult.Msg + "\",\"" + dbResult.ResponseCode + "\");"); } public static void PrintSuccessMessage(Page page, string msg) { PrintMessage(page, "0", msg); } public static void PrintErrorMessage(Page page, string msg) { PrintMessage(page, "1", msg); } public static void PrintMessage(Page page, string errorCode, string msg) { CallBackJs(page, "Set Message", "window.parent.SetMessageBox(\"" + msg + "\",\"" + errorCode + "\");"); } public static void AlertMessage(Page page) { if (HttpContext.Current.Session["message"] == null) return; var dbResult = GetMessage(); if (dbResult.Msg == "") return; CallBackJs(page, "Alert Message", "alert(\"" + FilterMessageForJs(dbResult.Msg) + "\");"); HttpContext.Current.Session.Remove("message"); } public static String FilterMessageForJs(string strVal) { if (strVal.ToLower() != "null") { strVal = strVal.Replace("\"", ""); } return strVal; } public static string GetIpAddress(HttpRequest request) { return request.ServerVariables["remote_addr"]; } public static string GetUserInfo(HttpRequest request) { HttpBrowserCapabilities browser = request.Browser; string str = " Browser Capabilities = Values -:::-" + "Type = " + browser.Type + "-:::-" //-:::- + "Name = " + browser.Browser + "-:::-" + "Version = " + browser.Version + "-:::-" + "Major Version = " + browser.MajorVersion + "-:::-" + "Minor Version = " + browser.MinorVersion + "-:::-" + "Platform = " + browser.Platform + "-:::-" + "Is Beta = " + browser.Beta + "-:::-" + "Is Crawler = " + browser.Crawler + "-:::-" + "Is AOL = " + browser.AOL + "-:::-" + "Is Win16 = " + browser.Win16 + "-:::-" + "Is Win32 = " + browser.Win32 + "-:::-" + "Supports Frames = " + browser.Frames + "-:::-" + "Supports Tables = " + browser.Tables + "-:::-" + "Supports Cookies = " + browser.Cookies + "-:::-" + "Supports VBScript = " + browser.VBScript + "-:::-" + "Supports JavaScript = " + browser.EcmaScriptVersion.ToString() + "-:::-" + "Supports Java Applets = " + browser.JavaApplets + "-:::-" + "Supports ActiveX Controls = " + browser.ActiveXControls + "-:::-" + "Supports JavaScript Version = " + browser["JavaScriptVersion"] + "-:::-" + "CDF = " + browser.CDF + "-:::-" + "IP Adress = " + request.ServerVariables["REMOTE_ADDR"] + "-:::-" + "User Agent = " + request.ServerVariables["HTTP_USER_AGENT"] + "-:::-" + "Refrerer = " + request.ServerVariables["HTTP_REFERER"] + "-:::-" + "Http Accept = " + request.ServerVariables["HTTP_ACCEPT"] + "-:::-" + "Language = " + request.ServerVariables["HTTP_ACCEPT_LANGUAGE"]; return str; } public static string ReadQueryString(string key, string defVal) { return HttpContext.Current.Request.QueryString[key] ?? defVal; } public static string ReadFormData(string key, string defval) { return HttpContext.Current.Request.Form[key] ?? defval; } public static void ClearSession(string key) { HttpContext.Current.Session[key] = ""; } public static string GetUrlRoot() { return ReadWebConfig("urlRoot"); } public static string FormatData(string data, string dataType) { if (string.IsNullOrEmpty(data)) return " "; if (data == "-") return data; if (dataType == "D") { DateTime d; DateTime.TryParse(data, out d); return d.Year + "-" + d.Month.ToString("00") + "-" + d.Day.ToString("00"); } if (dataType == "DT") { DateTime t; DateTime.TryParse(data, out t); return t.Year + "-" + t.Month.ToString("00") + "-" + t.Day.ToString("00") + " " + t.Hour.ToString("00") + ":" + t.Minute.ToString("00"); } if (dataType == "M") { decimal m; decimal.TryParse(data, out m); return m.ToString("N"); } return data; } public static void AlertMessage(Page page, string msg) { CallBackJs1(page, "Alert Message", "alert(\"" + FilterMessageForJs(msg) + "\");"); } public static void CallBackJs1(Page page, String scriptName, string functionName) { ScriptManager.RegisterStartupScript(page, page.GetType(), scriptName, functionName, true); } public static string GetLogoutPage() { return GetUrlRoot() + "/Logout.aspx"; } public static string GetAuthenticationPage() { return GetUrlRoot() + "/Authentication.aspx"; } public static string GetAgent() { return ReadSession("agent", ""); } public static string DataTableToJson(DataTable table) { if (table == null) return ""; var list = new List>(); foreach (DataRow row in table.Rows) { var dict = new Dictionary(); foreach (DataColumn col in table.Columns) { dict[col.ColumnName] = string.IsNullOrEmpty(row[col].ToString()) ? "" : row[col]; } list.Add(dict); } var serializer = new JavaScriptSerializer(); string json = serializer.Serialize(list); return json; } public static string GetUserType() { var userType = ReadSession("userType", ""); //WriteSession("lastActiveTS", DateTime.Now.ToString()); return userType; } public static string GetAgentId() { return ReadSession("agentId", ""); } public static string GetCustomerFilePath() { return ReadWebConfig("customerDocPath"); } public static string GetAppRoot() { return ReadWebConfig("root"); } public static string GetCountryId() { return ReadSession("countryId", ""); } public static string GetSettlingAgent() { return ReadSession("settlingAgent", ""); } public static string GetSuperAgent() { return ReadSession("superAgent", ""); } public static string GetBranch() { return ReadSession("branch", ""); } public static void CallJSFunction(Page page, string functionName) { ScriptManager.RegisterStartupScript(page, page.GetType(), "cb", functionName, true); } public static string UploadSignatureImage(string imageData, string registerDate, string membershipId, string customerId) { string errorCode = "1"; try { string fileExtension = ".png"; string fileName = customerId + "_signature_" + DateTime.Now.Hour.ToString() + DateTime.Now.Millisecond.ToString() + "_" + registerDate.Replace("-", "_") + fileExtension; string path = GetStatic.GetCustomerFilePath() + "CustomerDocument\\" + registerDate.Replace("-", "\\") + "\\" + membershipId; if (!Directory.Exists(path)) Directory.CreateDirectory(path); string fName = path + "\\" + fileName; using (FileStream fs = new FileStream(fName, FileMode.CreateNew)) { using (BinaryWriter bw = new BinaryWriter(fs)) { byte[] data = Convert.FromBase64String(imageData); bw.Write(data); bw.Close(); } } errorCode = fileName; } catch (Exception ex) { errorCode = "1"; } return errorCode; } public static string NumberToWord(string data) { var str = data.Split('.'); int number = Convert.ToInt32(str[0]); int dec = 0; if (str.Length > 1) dec = Convert.ToInt32(str[1].Substring(0, 2)); if (number == 0) return "Zero"; if (number == -2147483648) return "Minus Two Hundred and Fourteen Crore Seventy Four Lakh Eighty Three Thousand Six Hundred and Forty Eight Rupees Fifty Paisa"; int[] num = new int[4]; int first = 0; int u, h, t; StringBuilder sb = new System.Text.StringBuilder(); if (number < 0) { sb.Append("Minus "); number = -number; } string[] words0 = { "", "One ", "Two ", "Three ", "Four ", "Five ", "Six ", "Seven ", "Eight ", "Nine " }; string[] words1 = { "Ten ", "Eleven ", "Twelve ", "Thirteen ", "Fourteen ", "Fifteen ", "Sixteen ", "Seventeen ", "Eighteen ", "Nineteen " }; string[] words2 = { "Twenty ", "Thirty ", "Forty ", "Fifty ", "Sixty ", "Seventy ", "Eighty ", "Ninety " }; string[] words3 = { "Thousand ", "Lakh ", "Crore " }; num[0] = number % 1000; // units num[1] = number / 1000; num[2] = number / 100000; num[1] = num[1] - 100 * num[2]; // thousands num[3] = number / 10000000; // crores num[2] = num[2] - 100 * num[3]; // lakhs for (int i = 3; i > 0; i--) { if (num[i] != 0) { first = i; break; } } for (int i = first; i >= 0; i--) { if (num[i] == 0) continue; u = num[i] % 10; // ones t = num[i] / 10; h = num[i] / 100; // hundreds t = t - 10 * h; // tens if (h > 0) sb.Append(words0[h] + "Hundred "); if (u > 0 || t > 0) { if (h > 0 && i == 0) sb.Append("and "); if (t == 0) sb.Append(words0[u]); else if (t == 1) sb.Append(words1[u]); else sb.Append(words2[t - 2] + words0[u]); } if (i != 0) sb.Append(words3[i - 1]); } //sb.Append(" Rupees "); int d1 = dec / 10; int d2 = dec % 10; if (d1 == 0) sb.Append(words0[d1]); else if (d1 == 1) sb.Append(words1[d2]); else sb.Append(words2[d1 - 2] + words0[d2]); //if (dec > 0) // sb.Append(" Paisa"); return sb.ToString().TrimEnd() + " only"; } public static String ShowWithoutDecimal(String strVal) { if (strVal != "") return String.Format("{0:0,0}", double.Parse(strVal)); else return strVal; } public static String ShowDecimal(String strVal) { if (strVal != "") return String.Format("{0:0,0.00}", double.Parse(strVal)); else return strVal; } } }