﻿//JavaScript Rate Calculator

//The Value of the below arrays represent the number of people staying.  The Price goes down per person for larger groups
//HighSeason Base Rates
var hs7ratespp = new Array()
hs7ratespp[0] = 2418.99 //2199
hs7ratespp[1] = 1868.99 //1699
hs7ratespp[2] = 1538.99 //1399
hs7ratespp[3] = 1318.99 //1199
hs7ratespp[4] = 1318.99 //1199
hs7ratespp[5] = 1318.99 //1199
//LowSeason Base Rates
var ls7ratespp = new Array()
ls7ratespp[0] = 2198.99 //1999
ls7ratespp[1] = 1648.99 //1499
ls7ratespp[2] = 1318.99 //1199
ls7ratespp[3] = 1098.99 //999
ls7ratespp[4] = 1098.99 //999
ls7ratespp[5] = 1098.99 //999

function calcprice() {
    guests = document.forms[0].nofpeople.value - 1;
    nonigths = document.forms[0].nofnights.value

    if (nonigths < 8) {
        hsqratepp = hs7ratespp[guests] / 7 * nonigths;
        lsqratepp = ls7ratespp[guests] / 7 * nonigths;
    }
    else {
        lsqratepp = ls7ratespp[guests]
        hsqratepp = hs7ratespp[guests]
        if (nonigths < 15) {
            hsqratepp = hsqratepp + (hs7ratespp[guests] / 7 * (nonigths - 7) * 0.95);
            lsqratepp = lsqratepp + (ls7ratespp[guests] / 7 * (nonigths - 7) * 0.95);
        }
        else {
            hsqratepp = hsqratepp + (hs7ratespp[guests] * 0.95);
            lsqratepp = lsqratepp + (ls7ratespp[guests] * 0.95);

            if (nonigths < 22) {
                hsqratepp = hsqratepp + (hs7ratespp[guests] / 7 * (nonigths - 14) * 0.925);
                lsqratepp = lsqratepp + (ls7ratespp[guests] / 7 * (nonigths - 14) * 0.925);
            }
            else {
                hsqratepp = hsqratepp + (hs7ratespp[guests] * 0.925);
                lsqratepp = lsqratepp + (ls7ratespp[guests] * 0.925);
                if (document.forms[0].nofnights.value < 30) {
                    hsqratepp = hsqratepp + (hs7ratespp[guests] / 7 * (nonigths - 21) * 0.9);
                    lsqratepp = lsqratepp + (ls7ratespp[guests] / 7 * (nonigths - 21) * 0.9);
                }
                else {
                    hsqratepp = hsqratepp + (hs7ratespp[guests] / 7 * 8 * 0.9);
                    lsqratepp = lsqratepp + (ls7ratespp[guests] / 7 * 8 * 0.9);
                    hsqratepp = hsqratepp + (hs7ratespp[guests] / 7 * (nonigths - 29) * 0.875);
                    lsqratepp = lsqratepp + (ls7ratespp[guests] / 7 * (nonigths - 29) * 0.875);
                }
            }
        }
    }
    if (guests == 0) {
        hsqratepp = hsqratepp + 100;
        lsqratepp = lsqratepp + 100;
    }

    hsqratepp = Math.round(hsqratepp);
    hsqdeposittpp = hsqratepp.toFixed(2) / 2;

    lsqratepp = Math.round(lsqratepp);
    lsqdeposittpp = lsqratepp.toFixed(2) / 2;

    document.getElementById("lsratepp").innerHTML = "$" + addCommas(lsqratepp.toFixed(2));
    document.getElementById("lsdeposittpp").innerHTML = "$" + addCommas(lsqdeposittpp.toFixed(2));

    document.getElementById("hsratepp").innerHTML = "$" + addCommas(hsqratepp.toFixed(2));
    document.getElementById("hsdeposittpp").innerHTML = "$" + addCommas(hsqdeposittpp.toFixed(2));

    if (isDate(document.getElementById("ctl00_MainContent_txtDate").value) == true) {
        var startdate = document.getElementById("ctl00_MainContent_txtDate").value;
        var datestring = new Array;
        datestring = startdate.split("/");
        var month = datestring[0];
        if (month >= 11 || month <= 4) {
            document.getElementById("amount").value = (hsqdeposittpp.toFixed(2)) * (guests + 1);
        }
        else {
            document.getElementById("amount").value = (lsqdeposittpp.toFixed(2)) * (guests + 1);
        }

    }
    else {
        // var startdate = '!Start Date is not Valid!';
    }

    document.getElementById("item_name").value = 'Reservation of ' + (guests + 1) + ' guests starting on ' + startdate + ' for ' + nonigths + ' nights';
    //document.getElementById("reservenow").href = 'testpurchase.aspx?itemname=' + guests + ' guests for ' + nonigths + 'nights' + '&amount=' + (hsqdeposittpp.toFixed(2));            
}

function addCommas(nStr) {
    nStr += '';
    x = nStr.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + ',' + '$2');
    }
    return x1 + x2;
}

function populatenights() {
    for (var dcount = 4; dcount < 31; dcount++) {
        if (dcount == 7) { document.write("<option selected value=", dcount, "> ", dcount, " </option>"); }
        else { document.write("<option value=", dcount, "> ", dcount, " </option>"); }
    }
}

// Declaring valid date character, minimum year and maximum year
var dtCh = "/";
var minYear = "2009";
var maxYear = "2050";

function isInteger(s) {
    var i;
    for (i = 0; i < s.length; i++) {
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag) {
    var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++) {
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary(year) {
    // February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ((!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28);
}
function DaysArray(n) {
    for (var i = 1; i <= n; i++) {
        this[i] = 31
        if (i == 4 || i == 6 || i == 9 || i == 11) { this[i] = 30 }
        if (i == 2) { this[i] = 29 }
    }
    return this
}

function isDate(dtStr) {
    var daysInMonth = DaysArray(12)
    var pos1 = dtStr.indexOf(dtCh)
    var pos2 = dtStr.indexOf(dtCh, pos1 + 1)
    var strMonth = dtStr.substring(0, pos1)
    var strDay = dtStr.substring(pos1 + 1, pos2)
    var strYear = dtStr.substring(pos2 + 1)
    strYr = strYear
    if (strDay.charAt(0) == "0" && strDay.length > 1) strDay = strDay.substring(1)
    if (strMonth.charAt(0) == "0" && strMonth.length > 1) strMonth = strMonth.substring(1)
    for (var i = 1; i <= 3; i++) {
        if (strYr.charAt(0) == "0" && strYr.length > 1) strYr = strYr.substring(1)
    }
    month = parseInt(strMonth)
    day = parseInt(strDay)
    year = parseInt(strYr)
    if (pos1 == -1 || pos2 == -1) {
        alert("The date format should be : mm/dd/yyyy")
        return false
    }
    if (strMonth.length < 1 || month < 1 || month > 12) {
        alert("Please enter a valid month")
        return false
    }
    if (strDay.length < 1 || day < 1 || day > 31 || (month == 2 && day > daysInFebruary(year)) || day > daysInMonth[month]) {
        alert("Please enter a valid day")
        return false
    }
    if (strYear.length != 4 || year == 0 || year < minYear || year > maxYear) {
        alert("Please enter a valid 4 digit year between " + minYear + " and " + maxYear)
        return false
    }
    if (dtStr.indexOf(dtCh, pos2 + 1) != -1 || isInteger(stripCharsInBag(dtStr, dtCh)) == false) {
        alert("Please enter a valid date")
        return false
    }
    return true
}

