function openDialog(url, title, height, width) {
    return window.showModalDialog(url, title, "dialogHeight: " + height + "px; dialogWidth: " + width + "px; center: yes; help: no; resizable: no; scroll: no; status: no;");
}

function openWindow(url, left, top, width, height) {
    width = width <= 0 ? window.screen.availWidth - 50 : width;
    height = height <= 0 ? window.screen.availHeight - 75 : height;
    window.open(url, '_blank', 'location=no,menubar=no,resizable=yes,scrollbars=yes,status=no,titlebar=no,toolbar=no,left=' + left.toString() + ',top=' + top.toString() + ',width=' + width.toString() + ',height=' + height.toString() + '');
}

function trim(s) {
    var s = s.replace(/^\s\s*/, ''),
		ws = /\s/,
		i = s.length;
    while (ws.test(s.charAt(--i)));
    return s.slice(0, i + 1);
}


function CompareDates(d1, d2, compare) {
    if (compare == 'lessthan')
        return d1 < d2;
    if (compare == 'lessthanorequal')
        return d1 <= d2;
    if (compare == 'greaterthan')
        return d1 > d2;
    if (compare == 'greaterthanorequal') {
        return d1 >= d2;
    }
    if (compare == 'equal')
        return d1 == d2;
    return false;
}

function DateRange(s, e, i) {
    var dpStartDate = $find(s);
    var dpEndDate = $find(e);
    if (dpStartDate != null && dpEndDate != null) {
        if (i == 'today') {
            var d = new Date();
            dpStartDate.set_selectedDate(d);
            dpEndDate.set_selectedDate(d);
        }
        if (i == 'month') {
            var d = dpStartDate.get_selectedDate();
            d.setDate(1);

            dpStartDate.set_selectedDate(d);
            dpEndDate.set_selectedDate(DateAdd(DateAdd(d, 0, 1, 0), -1, 0, 0));
        }
        else if (i == 'year') {
            var d = dpStartDate.get_selectedDate();
            d.setDate(1);
            d.setMonth(0);
            dpStartDate.set_selectedDate(d);
            dpEndDate.set_selectedDate(DateAdd(DateAdd(d, 0, 0, 1), -1, 0, 0));
        }
    }
}

function DateAdd(date, day, month, year) {
    var d = new Date(date.getTime());
    var yearsToAdd = year;
    var month = d.getMonth() + month;
    if (month > 11) {
        yearsToAdd = Math.floor((month + 1) / 12);
        month -= 12 * yearsToAdd;
        yearsToAdd += year;
    }
    d.setMonth(month);
    d.setFullYear(d.getFullYear() + yearsToAdd);
    d.setTime(d.getTime() + 60000 * 60 * 24 * day);
    return d;
}
