﻿// JScript File

<!--


function verifyCheckOutDate(sender, args)
{   if (sender._selectedDate < new Date())
    {
        alert("You cannot select a day earlier than today!");
        //sender._selectedDate = new Date();

        //set date box to nothing
        sender._textbox.set_Value("")

        // set the date back to the current date
        //sender._textbox.set_Value(sender._selectedDate.format(sender._format))
    }
}


function checkDate(sender,args)
{
    if (sender._selectedDate < new Date())
    {
        alert("You cannot select a day earlier than today!");
        //sender._selectedDate = new Date();

        //set date box to nothing
        sender._textbox.set_Value("")

        // set the date back to the current date
        //sender._textbox.set_Value(sender._selectedDate.format(sender._format))
    }
}

function eAdd(users) {
    var noSpam = "";
    document.write("<a href='" + "ma" + noSpam + "il" + noSpam + "to:" + users + "@" + "queenbeehotel.com" + "'>" + users + "@" + "queenbeehotel.com" + "</a>");
}
    
function setStyle(x)
{
document.getElementById(x).style.background="yellow";
}

function convertToDate(str)
{
    var arr = str.split("/");
    var year = arr[2];
    var day = arr[1];
    var month = arr[0]-1; //month argument starts with 0

    return new Date(year, month, day, 0,0,0);
}

function CurrencyFormatted(amount)
{
	var i = parseFloat(amount);
	if(isNaN(i)) { i = 0.00; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = minus + s;
	return s;
}
// end of function CurrencyFormatted()


function recalculate(checkInID, checkOutID, numGuestsID, numBedsID, taxesID, subtotalID, totalID)
{  
    var OneBedOneGuest = 78.85;
    var OneBedTwoGuests = 85.50;
    var TwoBedTwoGuests = 90.25;
    var TwoBedThreeGuests = 96.90;
    var TwoBedFourGuests = 103.55;
    var basePrice = 0.0;
    
    var NumGuestsSelect = document.getElementById(numGuestsID); 
    var GuestSelIndex = NumGuestsSelect.selectedIndex;
    var NumberGuests = NumGuestsSelect.options[GuestSelIndex].value;

    var NumBedsSelect = document.getElementById(numBedsID); 
    var BedSelIndex = NumBedsSelect.selectedIndex;
    var NumberBeds = NumBedsSelect.options[BedSelIndex].value;
    
    //if the number of guests is greater than two then a one bed room cannot be selected
    //change the value accordingly if that's the case
    if (NumberGuests > 2)
    {    var NumberBeds = 2;
        document.getElementById(numBedsID).selectedIndex = 1;
    }
    
    //also if the number of guests is 1, change the number of beds to 1
    if (NumberGuests == 1)
    {    var NumberBeds = 1;
        document.getElementById(numBedsID).selectedIndex = 0;
    }
  
    // The number of milliseconds in one day
    var ONE_DAY = 1000 * 60 * 60 * 24;
   
    // Convert both dates to milliseconds
    var date1_ms = convertToDate(document.getElementById(checkInID).value);
    var date2_ms = convertToDate(document.getElementById(checkOutID).value);

    // Calculate the difference in milliseconds
    var difference_ms = (date2_ms - date1_ms);
    
    if (difference_ms > 0)  //if this value is negative, the checkout date is before the checkin date which is wrong
    {       
        // Convert back to days
        var daysDifference =  Math.round(difference_ms/ONE_DAY);

        //discount wil be based on length of stay
        var discount = 0.0;

        var subtotal = 0;
        var taxes = 0;
        var total = 0;
        
        if (daysDifference > 30)
        {   discount = 0.3;
        } 
        else if (daysDifference > 20)
        {   discount = 0.25;
        }
        else if (daysDifference > 15)
        {   discount = 0.2;
        }
        else if (daysDifference > 10)
        {   discount = 0.15;
        }
        else if (daysDifference > 5)
        {   discount = 0.1;
        }
        
        if (NumberGuests >=4)
        {   basePrice = TwoBedFourGuests;
        }      
        else if (NumberGuests == 3)
        {   basePrice = TwoBedThreeGuests;
        }
        else if (NumberGuests == 2)
        {       
            if (NumberBeds == 2 )
            {   basePrice = TwoBedTwoGuests;
            }
            else if (NumberBeds == 1)
            {   basePrice = OneBedTwoGuests;
            }
        }
        else if (NumberGuests == 1)
        {   basePrice = OneBedOneGuest;
        }
        
        subtotal = (daysDifference*basePrice)*(1.0-discount);
        taxes = (subtotal*0.05)+((subtotal + (subtotal*0.05))*0.12);
        total = subtotal+taxes;

        document.getElementById(subtotalID).value = CurrencyFormatted(subtotal); 
        document.getElementById(taxesID).value = CurrencyFormatted(taxes);
        document.getElementById(totalID).value = CurrencyFormatted(total);
    }
    else
    {   document.getElementById(subtotalID).value = 0;
        document.getElementById(taxesID).value = 0;
        document.getElementById(totalID).value = 0;
    }
    
}
//-->