<%@ Page Language="vb" %>
<%@ Import Namespace="TourEx.BLL" %>
<%@ Import Namespace="System.data" %>
<%@ Import Namespace="System.data.SqlClient" %>
<script runat="server">
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        Response.ClearContent()
        Response.ContentType = "text/xml"
        Dim HID As String = FilterStr(Request.QueryString("HID"))
        Dim Tm1 As String = FilterStr(Request.QueryString("Tm1"))
        Dim Tm2 As String = FilterStr(Request.QueryString("Tm2"))
        If Not IsDate(Tm1) Then
        	Tm1 = GetCookies("tm1")
        	If Not IsDate(Tm1) Then
        		Tm1 = Date.Today
        	End If
        End If
        If Not IsDate(Tm2) Then
        	Tm2 = GetCookies("tm2")
        	If Not IsDate(Tm2) Then
        		Tm2 = DateAdd(DateInterval.Day, 1, CDate(Tm1))
        	End If
        End If
        If DateDiff(DateInterval.Day, CDate(Tm1), CDate(Tm2)) < 1 Then
        	Tm2 = DateAdd(DateInterval.Day, 1, CDate(Tm1))
        End If
        SetCookies("tm1", Tm1)
        SetCookies("tm2", Tm2)
        HID = Regex.Replace(HID, "([^\d,])", "", RegexOptions.IgnoreCase)
        While InStr(HID, ",,") > 0
            HID = Replace(HID, ",,", ",")
        End While
        If HID.StartsWith(",") Then
            HID = Mid(HID, 2)
        End If
        If HID.EndsWith(",") Then
            HID = Left(HID, Len(HID) - 1)
        End If
        If HID = "" Then
            Exit Sub
        End If
        Dim FirstDayPrice, Week, Days, TempPrice, PriceCount, S As Integer
        Dim SpecialPrice, Temp As String
        Dim TempDate As Date
        Dim Dr As SqlDataReader = Procedure.DataList("Select R_ID,R_Hotelid,R_RackRate,R_Price,R_Breakfast,R_Internet,R_Name,R_Bonus,R_Friday,R_Saturday,R_Weekend,R_SpecialPrice,R_BedCategory,R_Floor,R_Area,R_Windows,R_Bathroom,R_AddBed From [" & TablePrefix & "Room] Where R_HotelID In (" & HID & ") Order By R_OrderID")
        Response.Write("<?xml version=""1.0"" encoding=""utf-8"" standalone=""yes""?><rooms>")
        Days = DateDiff(DateInterval.Day, CDate(Tm1), CDate(Tm2)) - 1
        While Dr.Read
            Response.Write("<room ")
            SpecialPrice = "|" & Dr("R_SpecialPrice") & "|"
            FirstDayPrice = 0
            PriceCount = 0
            For j As Integer = 0 To Days
                TempPrice = Dr("R_Price")
                TempDate = DateAdd(DateInterval.Day, j, Date.Today)
                S = InStr(SpecialPrice, "|" & TempDate.Month & "-" & TempDate.Day & ":")
                Week = TempDate.DayOfWeek
                If S > 0 Then
                    Temp = Mid(SpecialPrice, InStr(S, SpecialPrice, ":") + 1)
                    TempPrice = Val(Left(Temp, InStr(Temp, "|")))
                ElseIf Week > 4 OrElse Week < 1 Then
                    If Week = 5 Then
                        TempPrice = Dr("R_Friday")
                    ElseIf Week = 6 Then
                        TempPrice = Dr("R_Saturday")
                    Else
                        TempPrice = Dr("R_Weekend")
                    End If
                End If
                If FirstDayPrice = 0 Then
                    FirstDayPrice = TempPrice
                End If
                PriceCount += TempPrice
            Next
            With Response
                .Write("firstdayprice=""")
                .Write(FirstDayPrice)
                .Write(""" averageprice=""")
                .Write(PriceCount \ (Days + 1))
                .Write(""" r_id=""" & Dr("r_id"))
                .Write(""" r_hotelid=""")
                .Write(Dr("R_hotelId"))
                .Write(""" r_rackrate=""")
                .Write(Dr("R_RackRate"))
                .Write(""" r_breakfast=""")
                .Write(Dr("R_Breakfast"))
                .Write(""" r_internet=""")
                .Write(Dr("R_Internet"))
                .Write(""" r_name=""")
                .Write(Dr("R_Name"))
                .Write(""" r_bonus=""")
                .Write(Dr("R_Bonus"))
                .Write(""" r_bedcategory=""")
                .Write(Dr("R_BedCategory"))
                .Write(""" r_floor=""")
                .Write(Dr("R_Floor"))
                .Write(""" r_area=""")
                .Write(Dr("R_Area"))
                .Write(""" r_windows=""")
                .Write(Dr("R_Windows"))
                .Write(""" r_bathroom=""")
                .Write(Dr("R_Bathroom"))
                .Write(""" r_addbed=""")
                .Write(Dr("R_AddBed"))
                .Write(""" />")
            End With
        End While
        Dr.Close()
        Response.Write("</rooms>")
   End Sub
</script>