dim MonthNames(12)
MonthNames(1) = "1"
MonthNames(2) = "2"
MonthNames(3) = "3"
MonthNames(4) = "4"
MonthNames(5) = "5"
MonthNames(6) = "6"
MonthNames(7) = "7"
MonthNames(8) = "8"
MonthNames(9) = "9"
MonthNames(10) = "10"
MonthNames(11) = "11"
MonthNames(12) = "12"

Const ShMldef = 226895
Const OneYear = 365.24199
Dim MilMonth(12) 
Dim DayWeeks(7) 
Dim Months(12) 

Dim SHYear 
Dim SHMonth 
Dim SHDay 

Dim MLYear 
Dim MLMonth 
Dim MLDay 

Private Function CalculateDayNo(Year , Month , Day ) 

    Dim YearDay 
    Dim Ctr 
    
    MilMonth(1) = 31: MilMonth(2) = 28: MilMonth(3) = 31: MilMonth(4) = 30
    MilMonth(5) = 31: MilMonth(6) = 30: MilMonth(7) = 31: MilMonth(8) = 31
    MilMonth(9) = 30: MilMonth(10) = 31: MilMonth(11) = 30: MilMonth(12) = 31
    
    YearDay = 0
    For Ctr = 1 To (Month - 1)
        YearDay = YearDay + MilMonth(Ctr)
    Next

    YearDay = YearDay + Day

    If (Month > 2) And ((Year Mod 4) = 0) And (((Year Mod 100) <> 0) Or ((Year Mod 400) = 0)) Then
        YearDay = YearDay + 1
    End If

    CalculateDayNo = (Year - 1) * 365 + ((Year - 1) \ 4) - ((Year - 1) \ 100) + ((Year - 1) \ 400) + YearDay
End Function

Public Sub MiladiToShamsi(MLYear , MLMonth , MLDay , SHYear , SHMonth , SHDay )

    Dim YearDay 
    Dim AllDays 
    Dim LeftDays 
    
    SHYear = 0
    SHMonth = 0
    SHDay = 0
    AllDays = CalculateDayNo(MLYear, MLMonth, MLDay)
    
    AllDays = AllDays - ShMldef
    
    SHYear = CLng(AllDays / OneYear)
    LeftDays = AllDays - SHYear * OneYear

    If LeftDays < 0.5 Then
        SHYear = SHYear - 1
        LeftDays = AllDays - SHYear * OneYear
    End If

    YearDay = CLng(LeftDays)
    If (LeftDays - YearDay) >= 0.5 Then
        YearDay = YearDay + 1
    End If

    If (YearDay \ 31) > 6 Then
        SHMonth = 6
        YearDay = YearDay - 6 * 31
        SHMonth = SHMonth + YearDay \ 30
        If (YearDay Mod 30) <> 0 Then
            SHMonth = SHMonth + 1
        End If
        YearDay = YearDay - ((SHMonth - 7) * 30)
    Else
        SHMonth = YearDay \ 31
        If (YearDay Mod 31) <> 0 Then
            SHMonth = SHMonth + 1
        End If
        YearDay = YearDay - ((SHMonth - 1) * 31)
    End If
    
    SHDay = YearDay
    SHYear = SHYear + 1
End Sub

Public Sub ShamsiToMiladi(SHYear , SHMonth , SHDay , MLYear , MLMonth , MLDay )

    Dim CalDays 
    Dim ADays 
    Dim AllDays 
    Dim LeftDays 
    
    MilMonth(1) = 31: MilMonth(2) = 28: MilMonth(3) = 31: MilMonth(4) = 30
    MilMonth(5) = 31: MilMonth(6) = 30: MilMonth(7) = 31: MilMonth(8) = 31
    MilMonth(9) = 30: MilMonth(10) = 31: MilMonth(11) = 30: MilMonth(12) = 31

    MLYear = 0
    MLMonth = 0
    MLDay = 0

    ADays = (SHYear - 1) * OneYear
    CalDays = CLng(ADays)
    If (ADays - CalDays) > 0.5 Then
        CalDays = CalDays + 1
    End If
    If SHMonth > 6 Then
        CalDays = CalDays + 6 * 31 + (SHMonth - 6 - 1) * 30 + SHDay
    Else
        CalDays = CalDays + (SHMonth - 1) * 31 + SHDay
    End If

    AllDays = CalDays + ShMldef

    MLYear = CLng(AllDays / 365)
  
    Do
        LeftDays = AllDays - CalculateDayNo((MLYear + 1), 0, 0)
        If LeftDays <= 0 Then
            MLYear = MLYear - 1
        End If
    Loop Until (LeftDays > 0)

    MLYear = MLYear + 1

'    MLMonth = 1
'    Do While LeftDays > MilMonth(MLMonth)
'        LeftDays = LeftDays - MilMonth(MLMonth)
'        MLMonth = MLMonth + 1
'    Loop

    For MLMonth = 1 To 12
        If LeftDays > MilMonth(MLMonth) Then
            LeftDays = LeftDays - MilMonth(MLMonth)
        Else
            Exit For
        End If
    Next

    If (MLMonth > 2) And ((MLYear Mod 4) = 0) And (((MLYear Mod 100) <> 0) Or ((MLYear Mod 400) = 0)) Then
        If LeftDays > 1 Then
            LeftDays = LeftDays - 1
        Else
            MLMonth = MLMonth - 1
            If MLMonth = 2 Then
                LeftDays = 29
            Else
                LeftDays = MilMonth(MLMonth)
            End If
        End If
    End If

    MLDay = LeftDays
End Sub

Public Function SToM(strDate) 
    Dim FirstSlash , SecondSlash 
    Dim SHyy , SHmm , SHdd 
    Dim MLyy , MLmm , MLdd 
    FirstSlash = InStr(strDate, "/")
    SecondSlash = InStr(FirstSlash + 1, strDate, "/")
    SHyy = cint(Left(strDate, FirstSlash - 1))
    SHmm = cint(Mid(strDate, FirstSlash + 1, SecondSlash - FirstSlash - 1))
    SHdd = cint(Right(strDate, Len(strDate) - SecondSlash))
    ShamsiToMiladi SHyy, SHmm, SHdd, MLyy, MLmm, MLdd
    SToM = DateSerial(MLyy, MLmm, MLdd)
End Function

Public Function MToS1(vYear,vMonth,vDay) 
	mDate = DateSerial(vYear,vMonth,vDay)
	strDate = MToS(mDate)
	MToS1  = cint(mid(strDate,9,2)) & " " & MonthNames(cint(mid(strDate,6,2))) & " " & Left(strDate,4)
end function

Public Function MToSy(vYear,vMonth,vDay) 
	mDate = DateSerial(vYear,vMonth,vDay)
	strDate = MToS(mDate)
	MToSy  = left(strDate,4)
end function
Public Function MToSm(vYear,vMonth,vDay) 
	mDate = DateSerial(vYear,vMonth,vDay)
	strDate = MToS(mDate)
	MToSm  = mid(strDate,6,2)
end function
Public Function MToSd(vYear,vMonth,vDay) 
	mDate = DateSerial(vYear,vMonth,vDay)
	strDate = MToS(mDate)
	MToSd  = mid(strDate,9,2)
end function


Public Function MToS(mDate)
	
	if not IsNull(mDate) and mDate<>"" then
		Dim SHyy , SHmm , SHdd 
		Dim MLyy , MLmm , MLdd 
		MLyy = Year(mDate)
		MLmm = Month(mDate)
		MLdd = Day(mDate)
		call MiladiToShamsi( MLyy, MLmm, MLdd, SHyy, SHmm, SHdd)
		MToS = Right("0000" + CStr(SHyy), 4) + "/" + Right("0" + CStr(SHmm), 2) + "/" + Right("0" + CStr(SHdd), 2)
	else
		MToS = "    /  /  "
	end if
End Function
function DateReverse(strDate)
	dim aDate 
	aDate = split(strDate,"/")
	DateReverse =  aDate(2) & "/" & aDate(1) & "/" & aDate(0)
end function

function GetPostedDate(strDateName)
	strYear = request("txt" & strDateName & "Year")
	strMonth = request("txt" & strDateName & "Month")
	strDay = request("txt" & strDateName & "Day")
	
	GetPostedDate = strYear & "/" & left("00",2-Len(strMonth)) & strMonth & "/" & left("00",2-Len(strDay)) & strDay
	if trim(GetPostedDate) = "/00/00"  then GetPostedDate = ""
end function
