CREATE FUNCTION dbo.ValidateWareWeekDay (@AWareNumber INT, @AStartDateTime DATETIME)  
--Created AV 2008 0610 Called from SP OnlineFindAvailableHallsTimeFromWare
RETURNS BIT AS  
BEGIN 
  DECLARE @Return BIT
  DECLARE @WeekDay INT

  SET @Return = 1

  IF EXISTS (SELECT * FROM WareWeekDayXTab WITH (NOLOCK) WHERE FK_WareNumber = @AWareNumber)  --if no records then return true.
  BEGIN
    SELECT  @WeekDay = DATEPART(WEEKDAY, @AStartDateTime) 

    --In wareweekdayxtab table the weekday number are not stored the same way as the datepart function
    IF @Weekday = 1
      SET @WeekDay = 6
    ELSE IF @Weekday = 2
      SET @WeekDay = 0
    ELSE IF @Weekday = 3
      SET @WeekDay = 1
    ELSE IF @Weekday = 4
      SET @WeekDay = 2
    ELSE IF @Weekday = 5
      SET @WeekDay = 3
    ELSE IF @Weekday = 6
      SET @WeekDay = 4
    ELSE IF @Weekday = 7
      SET @WeekDay = 5

    IF NOT EXISTS (SELECT * FROM WareWeekDayXTab WITH (NOLOCK) WHERE FK_WareNumber = @AWareNumber AND WeekDayNumber = @WeekDay)
      SET @Return = 0
  END

  RETURN @Return
END
