﻿// Called on a search.
function SetupSearch()
{
     if ($get("isPageLinkSearch").value == 'true')
    {
        $get("isPageLinkSearch").value = 'false';
        $get("pageNumber").value = "1";
    }
}

// Show the what's this depending on the value given
function showWhatsThisPopupPanel(parent, width)
{
    var elem = document.getElementById("ctl00_phContent_Clipboard1_WhatsThisPanel");
    
    var x = parent.offsetLeft;
    var y = parent.offsetTop;
    
    while (parent.offsetParent)
    {
        parent = parent.offsetParent;
        x += parent.offsetLeft;
        y += parent.offsetTop;
    }
    elem.style.top = y + "px";
    elem.style.left = (x - width) + "px";
    
    elem.style.visibility = "visible";
    elem.style.display = "block";
    
    return false;
}

function hideWhatsThisPopupPanel()
{
    var elem = document.getElementById("ctl00_phContent_Clipboard1_WhatsThisPanel");
    
    elem.style.visibility = "hidden";
    elem.style.display = "none";
    
    return false;
}

function ValidateSearchDates(val, args)
{
    if (checkoutTextBox.value && checkoutTextBox.value != 'Check-out Date' && 
        checkinTextBox.value && checkinTextBox.value != 'Check-in Date')
    {
        var checkinDate = getDateFromShortDateString(checkinTextBox.value);
        var checkoutDate = getDateFromShortDateString(checkoutTextBox.value);
        var today = new Date();
        today = new Date(today.getFullYear(), today.getMonth(), today.getDate());
        if (checkoutDate <= checkinDate)
        {
            datesCustomValidator.innerHTML = "*Check-out must be greater than Check-in.<br/>";
            args.IsValid = false;
        }
        else if (checkinDate < today)
        {
            datesCustomValidator.innerHTML = "*Check-in cannot be in the past.<br/>";
            args.IsValid = false;
        }
    }
    else if ((checkoutTextBox.value && checkoutTextBox.value != 'Check-out Date' && (!checkinTextBox.value || checkinTextBox.value == 'Check-in Date')) ||
        (checkinTextBox.value && checkinTextBox.value != 'Check-in Date' && (!checkoutTextBox.value || checkoutTextBox.value == 'Check-out Date')))
    {
        datesCustomValidator.innerHTML = "*Either provide both or no dates.<br/>";
        args.IsValid = false;
    }
}

function getDateFromShortDateString(shortDate)
{
    var dateParts = shortDate.split('/');
    var month = dateParts[0] - 1;
    var day = dateParts[1];
    var year = dateParts[2];
    return new Date(year, month, day);
}