using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace CitySunlight
{
    public static class HttpUtils
    {
        public static String getElementsbyName(String tagName)
        {
            if (HttpContext.Current.Request.Form[tagName] != null)
                return HttpContext.Current.Request.Form[tagName].ToString().Trim();
            else
                return null;
        }
        public static String getQueryString(String tagName)
        {
            if (HttpContext.Current.Request.QueryString[tagName] != null)
                return HttpContext.Current.Request.QueryString[tagName];
            else
                return null;
        }

        public static HttpPostedFile getFilebyName(String tagName)
        {
            HttpPostedFile file = HttpContext.Current.Request.Files[tagName];
            if (file != null && file.ContentLength > 0)
                return file;
            else
                return null;
        }
        public static long currentTimeMillis()
        {
            return (long)((DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds);
        }
        public static String addTd()
        {
            return "<td></td>";
        }
        public static String addTd(String s)
        {
            return String.Format("<td align=\"center\">{0}</td>", s);
        }
        public static String addTd(String s, int col, int row)
        {
            String td = "<td align=\"center\"";
            td += col != 0 ? String.Format(" colspan=\"{0}\"", col) : "";
            td += row != 0 ? String.Format(" rowspan=\"{0}\"", row) : "";
            return String.Format(td + " >{0}</td>", s);
        }
        public static int ToInt(this string s)
        {
            int i;
            if (!int.TryParse(s, out i))
            {
                i = 0;
            }
            return i;
        }
        public static decimal Todecimal(this string s)
        {
            decimal i;
            if (!decimal.TryParse(s, out i))
            {
                i = 0;
            }
            return i;
        }
    }
}