﻿// JScript File
function CheckAvailibility(sender, args)
{
    args.IsValid = true;
    //Check if date is valid
    //var dateValue = document.getElementById('ctl00_ContentPlaceHolder2_availibility_txtImageButton');
    if (validateDate(dateValue.value)) {
        //Check date is in future
        var current=new Date();
        if (new Date(dateValue.value) < new Date((current.getMonth() + 1) + '/' + current.getDate() + '/' + current.getFullYear()))
        {
            args.IsValid = false;
        }
    }
    else {
        args.IsValid = false;
    }
}

function SetNightsNo(cal){
    var one_day=1000*60*60*24
    var startDate = new Date(dateValue.value);
    var endDate = new Date(cal.value);
    var days = Math.round((endDate.getTime()-startDate.getTime())/(one_day));

    NightsBooked.value = days;
}

function SetEndDate(){
    if (validateDate(dateValue.value))
    {
        var startDate = new Date(dateValue.value);
        startDate.setDate(startDate.getDate()+parseInt(NightsBooked.value));
        
        endDateValue.value = (startDate.getMonth() < 9 ? '0' : '') + ( startDate.getMonth() + 1) + '/' + (startDate.getDate() < 10 ? '0' : '') + (startDate.getDate()) + '/' + startDate.getFullYear() ;
    }
}


function CheckAvailibilityContracts(sender, args)
{
    args.IsValid = true;

    if (validateDate(dateValue.value)) {
        var current=new Date();
        var cur = new Date(dateValue.value);
        
        //check end date and no of nights are not empty
        if (!validateDate(endDateValue.value))
        {
            if (NightsBooked.value == '')
            {
                args.IsValid = false;
                return;
            }
        }
        
        var endDate = new Date(endDateValue.value);
        //check enddate is later than start date
        if (endDate.getTime() - cur.getTime() < 0)
        {
            args.IsValid = false;
                return;
        }

        //check enddate or no of nights is later than start date and current date
        if (new Date(dateValue.value) < new Date((current.getMonth() + 1) + '/' + current.getDate() + '/' + current.getFullYear()))
        {
            var nights = NightsBooked.value;
            if( new Date( ( cur.getMonth() + 1) + '/' + (cur.getDate() + parseInt(nights) ) + '/' + cur.getFullYear() )
            < new Date((current.getMonth() + 1) + '/' + current.getDate() + '/' + current.getFullYear()))
            {
                args.IsValid = false;
            }
        }
    }
    else {
        args.IsValid = false;
    }
}

function validateDate(fld) {
    var RegExPattern = /^(?=\d)(?:(?:(?:(?:(?:0?[13578]|1[02])(\/|-|\.)31)\1|(?:(?:0?[1,3-9]|1[0-2])(\/|-|\.)(?:29|30)\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})|(?:0?2(\/|-|\.)29\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))|(?:(?:0?[1-9])|(?:1[0-2]))(\/|-|\.)(?:0?[1-9]|1\d|2[0-8])\4(?:(?:1[6-9]|[2-9]\d)?\d{2}))($|\ (?=\d)))?(((0?[1-9]|1[012])(:[0-5]\d){0,2}(\ [AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2})?$/;
    if (fld.match(RegExPattern)) {
        return true;
    } else {
        return false;
    } 
}

function CheckNewReservationEndDate(sender, args)
{
    args.IsValid = true;
    //Check if date is valid
    //var dateValue = document.getElementById('ctl00_ContentPlaceHolder2_availibility_txtImageButton');
    if (validateDate(dateValue.value)) {
        //Check date is in future
        var hfdeparture = document.getElementById('hfDepartureDate');
        //alert('3' + hfdeparture.value);
        //alert(new Date(hfdeparture.value));
        //alert(new Date(dateValue.value));
        if (new Date(dateValue.value) > new Date(hfdeparture.value))
        {
            args.IsValid = false;
        }
    }
    else {
        args.IsValid = false;
    }
}
