using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Self_Project
{
    class Functions
    {
        static public string UserName = "", Pass = "";
        //-----------------------------------------------------------------------
        static public Boolean IsNumber(string Number)
        {
            if (Number == "") return false;
            for (int i = 0; i <= Number.Length - 1; i++)
            {
                if (!char.IsDigit(Convert.ToChar(Number.Substring(i, 1))))
                {
                    return false;
                }
            }
            return true;
        }
        //-----------------------------------------------------------------------
        static public int FindMaxID(string TableName, string FieldName)
        {
            DataTable DT = new DataTable();
            DT = DataBase_SQL.Action(DBOperation.Select, null, TableName, "Max(" + FieldName + ")");
            if (Convert.IsDBNull(DT.Rows[0][0]))
                return 0;
            else
                return Convert.ToInt16(DT.Rows[0][0]) + 1;
        }
        //-----------------------------------------------------------------------
        static public Int32 Sub2Date(string Date1, string Date2)
        {
            if (Date1.Length != 10 || Date2.Length != 10) return 0;
            //-----------------------------------------------------
            short Day1 = 0, Day2 = 0, Month1 = 0, Month2 = 0;
            int Year1 = 0, Year2 = 0, i = 0;
            long Count1 = 0, Count2 = 0;
            int[] Mahs = new int[12] { 0, 31, 62, 93, 124, 155, 186, 216, 246, 276, 306, 326 };
            //--------------
            Year1 = Convert.ToInt16(Date1.Substring(0, 4));
            Month1 = Convert.ToInt16(Date1.Substring(5, 2));
            Day1 = Convert.ToInt16(Date1.Substring(8, 2));
            //--------------
            Year2 = Convert.ToInt16(Date2.Substring(0, 4));
            Month2 = Convert.ToInt16(Date2.Substring(5, 2));
            Day2 = Convert.ToInt16(Date2.Substring(8, 2));
            //-------------- Count1
            for (i = 1387; i < Year1; i++)
            {
                if (i % 4 == 1) Count1 += 366; else Count1 += 365;
            }
            Count1 += Mahs[Month1 - 1];
            Count1 += Day1;
            //-------------- Count2
            for (i = 1387; i < Year2; i++)
            {
                if (i % 4 == 1) Count2 += 366; else Count2 += 365;
            }
            Count2 += Mahs[Month2 - 1];
            Count2 += Day2;
            //--------------
            return Convert.ToInt32(Count1 - Count2);
        }
        //-----------------------------------------------------------------------
        static public string FarsiDate(string Alamat)
        {
            System.Globalization.PersianCalendar Date2 = new System.Globalization.PersianCalendar();
            string A, B, C;
            A = Date2.GetYear(DateTime.Now.Date).ToString();
            B = Date2.GetMonth(DateTime.Now.Date).ToString();
            C = Date2.GetDayOfMonth(DateTime.Now.Date).ToString();
            if (B.Length == 1) B = "0" + B;
            if (C.Length == 1) C = "0" + C;
            return A + Alamat + B + Alamat + C;
        }
        //-----------------------------------------------------------------------
        static public string AddDate(String Tarikh,Int32 DayCount)
        {
            string[] Temp_Tarikh = Tarikh.Split('/');
            Int32 Day = Convert.ToInt32(Temp_Tarikh[1]), Month = Convert.ToInt32(Temp_Tarikh[0]);
            Int32 Temp_Day = Day + DayCount;
            string Result;
            if ((Month >= 1) && (Month <= 6) && (Temp_Day > 31))
            {
                Month += 1;
                Temp_Day = 31-Day;
                DayCount = DayCount - Temp_Day;
                Day = DayCount;
            }
            else if ((Month > 6) && (Month <= 12) && (Temp_Day > 30))
            {
                if ((Month + 1) > 12)
                    Month = 1;
                else
                    Month += 1;
                Temp_Day = 30-Day;
                DayCount = DayCount - Temp_Day;
                Day = DayCount;
            }
            else
            {
                Day += DayCount;
            }
            if ((Month >=1) && (Month <=9))
                Temp_Tarikh[0] = "0"+Month.ToString();
            else
                Temp_Tarikh[0]=Month.ToString();
            if ((Day >= 1) && (Day <= 9))
                Temp_Tarikh[1] = "0" + Day.ToString();
            else
                Temp_Tarikh[1] = Day.ToString();

            Result = Temp_Tarikh[0] + "/" + Temp_Tarikh[1];
            return Result;
        }
        //-----------------------------------------------------------------------
    }
}
