<!-- ----------------------------------------------------------------------------------
//
// Description:	Contains VBScript functions.
//
//--------------------------------------------------------------------------------- -->

//-------------------------------------------------------------------------------------
// Formats a SQL Server date.
//-------------------------------------------------------------------------------------
Function VBScript_FormatDate(sDate)

	Dim sMonth
	Dim sDay
	Dim sYear

	SetLocale("en-us")
	
	' Check for null
	If (IsNull(sDate)) Then
		VBScript_FormatDate = ""
		Exit Function
	End If	
	
	' Check for blank.
	If (Len(sDate) = 0) Then
		VBScript_FormatDate = ""
		Exit Function
	End If	

    If (InStr(sDate, "T")) Then
        sDate = Mid(sDate, 1, InStr(sDate, "T") - 1)    
    	sDate = FormatDateTime(sDate, vbShortDate)		
    End If
	
	If (Not VB_IsDate(sDate)) Then
		VBScript_FormatDate = ""
		Exit Function
	End If

	sMonth = DatePart("m", sDate)
	If (Len(sMonth) = 1) Then sMonth = "0" & sMonth

	sDay = DatePart("d", sDate)
	If (Len(sDay) = 1) Then sDay = "0" & sDay

	sYear= Right(DatePart("yyyy", sDate), 2)
			
	VBScript_FormatDate = sMonth & "/" & sDay & "/" & sYear
End Function

//-------------------------------------------------------------------------------------
// Formats a SQL Server date with time appended.
//-------------------------------------------------------------------------------------
Function VBScript_FormatDateTime(sDate)

	SetLocale("en-us")
	
	' Check for null.
	If (IsNull(sDate)) Then
		VBScript_FormatDateTime = ""
		Exit Function
	End If	

	' Check for blank.
	If (Len(sDate) = 0) Then
		VBScript_FormatDateTime = ""
		Exit Function
	End If	

	sDate = Replace(sDate, "T", " ")
	
	If (InStr(sDate, ".")) Then
		sDate = Left(sDate, InStr(sDate, ".")-1)
	Else
		//**DAN** Seemed go be truncating dates, so commented out.
		//sDate = Left(sDate, Len(sDate)-3)
	End If	
	
	sDate = FormatDateTime(sDate, vbLongDate) & " " & FormatDateTime(sDate, vbLongTime)
	sDate = Replace(sDate, ":00 PM", " PM")
	sDate = Replace(sDate, ":00 AM", " AM")
	
	VBScript_FormatDateTime = sDate
End Function

//-------------------------------------------------------------------------------------
// Formats a SQL Server date.
//-------------------------------------------------------------------------------------
Function VBScript_GetDayOfWeek(sDate)

    Dim sDayOfWeek
    
    Select Case DatePart("w", sDate)
        Case 1
            sDayOfWeek = "Sunday"
        Case 2
            sDayOfWeek = "Monday"
        Case 3
            sDayOfWeek = "Tuesday"
        Case 4
            sDayOfWeek = "Wednesday"
        Case 5
            sDayOfWeek = "Thursday"
        Case 6
            sDayOfWeek = "Friday"
        Case 7
            sDayOfWeek = "Saturday"
    End Select

    VBScript_GetDayOfWeek = sDayOfWeek
End Function


//-------------------------------------------------------------------------------------
// Determines if the specified date is valid.
//-------------------------------------------------------------------------------------
Function VB_IsDate(sExpression)

	Dim bIsDate

	SetLocale("en-us")
	
	bIsDate = IsDate(sExpression)
	
	If (bIsDate) Then
		bIsDate =  CDate(sExpression) > CDate("1/1/1900") And CDate(sExpression) < CDate("6/6/2079")
	End If
	
	VB_IsDate = bIsDate
End Function

//-------------------------------------------------------------------------------------
// Makes the specified date have a four digit year.
//-------------------------------------------------------------------------------------
Function VB_MakeFourDigitYear(sDate)

	Dim aParts

	SetLocale("en-us")

    If (VB_IsDate(sDate) = False) Then
        VB_MakeFourDigitYear = ""
        Exit Function
    End If        

    sDate = Replace(sDate, "-", "/")
	aParts = Split(sDate, "/")
	
	If (Len(aParts(2)) = 2) Then
		aParts(2) = DatePart("yyyy", sDate)
	End If
	
	VB_MakeFourDigitYear = Join(aParts, "/")
End Function

//-------------------------------------------------------------------------------------
// Returns the VB scripting engine version.
//-------------------------------------------------------------------------------------
Function VBScriptVersion()

	VBScriptVersion = ScriptEngineMajorVersion() & "." & ScriptEngineMinorVersion() & "." & ScriptEngineBuildVersion()
End Function


//-------------------------------------------------------------------------------------
// Returns trimmed string.
//-------------------------------------------------------------------------------------
Function VBScript_Trim(sString)

	VBScript_Trim = Trim(sString)
End Function