var date_picker_user_callback = null;
var date_picker_date_to_select = null;
var date_picker_last_selected_date = null;

function parsedate(str)
{
    if (str == null || str == '' || isNaN(time = Date.parse(str + ' GMT')))
        return null;

    date = new Date();
    date.setTime(time);
    
    if ((year = date.getUTCFullYear()) < 1970)
        date.setUTCFullYear(2000 + (year % 100));

    return date;
}

function date_picker_twodigits(n)
{
    return n < 10 ? "0" + n : n;
}

function date_picker_show_callback(str)
{
    el = document.getElementById('date_picker_placeholder');
    el.innerHTML = str;
    el.style.visibility = "visible";

    if ((date = parsedate(date_picker_date_to_select)) != null)
    {
        if ((el = document.getElementById('tm' + (date.getTime() / 1000))) != null)
            el.style.borderColor = 'red';
    }
}

function date_picker_show(pid, source, assoc_id, callback)
{
    date_picker_date_to_select = document.getElementById(assoc_id).value;

    date_picker_user_callback = callback;

    el = document.getElementById('date_picker_placeholder');
    el.style.visibility = "hidden";
    el.innerHTML = "";

    x = 0;
    y = 0;

    for (temp = source; temp != null; temp = temp.offsetParent)
    {
        x += temp.offsetLeft;
        y += temp.offsetTop;
    }

    x -= 124;
    y += 24;

    el.style.top = y + 'px';
    el.style.left = x + 'px';

    if ((date = parsedate(date_picker_date_to_select)) == null &&
        (date = date_picker_last_selected_date) == null)
        date = new Date();

    date_picker_last_selected_date = date;

    if (typeof(global_listings_directory) == "undefined") global_listings_directory = '';

    xmlhttp('GET', global_listings_directory + 'datepicker.php?pid=' + pid + '&year=' + date.getUTCFullYear() + '&month=' + (date.getUTCMonth() + 1), '', date_picker_show_callback);

    if (window.event != undefined)      // IE bug workaround
        window.event.returnValue = false;

    return false;
}

function date_picker_newdate(pid, year, month)
{
    date_picker_last_selected_date = new Date();
    date_picker_last_selected_date.setUTCFullYear(year);
    date_picker_last_selected_date.setUTCMonth(month - 1);

    if (typeof(global_listings_directory) == "undefined") global_listings_directory = '';

    xmlhttp('GET', global_listings_directory + 'datepicker.php?pid=' + pid + '&year=' + year + '&month=' + month, '', date_picker_show_callback);

    if (window.event != undefined)      // IE bug workaround
        window.event.returnValue = false;

    return false;
}

function date_picker_hide()
{
    date_picker_user_callback = null;

    el = document.getElementById('date_picker_placeholder');
    el.style.visibility = "hidden";
    el.innerHTML = "";
}

function date_picker_selected(date)
{
    if (date_picker_user_callback)
        date_picker_user_callback(date);

    date_picker_hide();
}

function date_picker_mouse_over(el)
{
    el.style.saveBorderColor = el.style.borderColor;
    el.style.borderColor = 'red';
}

function date_picker_mouse_out(el)
{
    el.style.borderColor = el.style.saveBorderColor;
}

function date_picker_check_dates(allowEmptyDates, allowPastDates)
{
    fromDate = trim(document.getElementById('from').value);
    toDate = trim(document.getElementById('to').value);

    if (allowEmptyDates && fromDate == '' && toDate == '')
        return true;

    if (fromDate == '')
    {
        alert('Enter Arrival Date');
        return false;
    }

    if ((fromDate = parsedate(fromDate)) == null)
    {
        alert('Enter valid Arrival Date');
        setTimeout("date_picker_select_and_focus('from')", 1);
        return false;
    }

    curDate = new Date();
    curDate = parsedate((curDate.getUTCMonth() + 1) + '/' + curDate.getUTCDate() + '/' + curDate.getUTCFullYear());

    if (!allowPastDates && fromDate.getTime() < curDate.getTime())
    {
        alert('Arrival Date must be no earlier than today');
        return false;
    }

    if (toDate == '')
    {
        alert('Enter Departure Date');
        return false;
    }

    if ((toDate = parsedate(toDate)) == null)
    {
        alert('Enter valid Departure Date');
        setTimeout("date_picker_select_and_focus('to')", 1);
        return false;
    }

    if ((toDate.getTime() - fromDate.getTime()) <= 0)
    {
        alert('Departure Date must be at least one day later than Arrival Date');
        return false;
    }

    return true;
}

function date_picker_on_date_selected_from(date)
{
    document.getElementById('from').value = date;

    if (typeof(date_picker_on_date_selected_from_hook) == "function")
        date_picker_on_date_selected_from_hook();
}

function date_picker_on_date_selected_to(date)
{
    document.getElementById('to').value = date;

    if (typeof(date_picker_on_date_selected_to_hook) == "function")
        date_picker_on_date_selected_from_hook();
}

function date_picker_select_and_focus(id)
{
    el = document.getElementById(id);
    el.select();
    el.focus();
}
