OTF_CL = 'OTF_C';
OTF_CO = null;
OTF_CF = null;
OTF_CD = new Date();
OTF_CSD = new Date();
OTF_MS = new Array('JANUARY', 'FEBRUARY', 'MARCH', 'APRIL', 'MAY', 'JUNE', 'JULY', 'AUGUST', 'SEPTEMBER', 'OCTOBER', 'NOVEMBER', 'DECEMBER');
function OTF_x(o){
    if (document.layers) 
        return o.left;
    return o.offsetLeft + (o.offsetParent ? OTF_x(o.offsetParent) : 0);
}

function OTF_y(o){
    if (document.layers) 
        return o.top;
    return o.offsetTop + (o.offsetParent ? OTF_y(o.offsetParent) : 0);
}

function OTF_w(o){
    if (document.layers) 
        return o.clip.width;
    return o.offsetWidth;
}

function OTF_h(o){
    if (document.layers) 
        return o.clip.height;
    return o.offsetHeight;
}

function OTF_m(o, x, y){
	x=150;
	y=275;
    if (document.layers) 
        o.moveToAbsolute(x, y);
    else 
        if (o.style.left && (!o.style.pixelLeft)) {
            o.style.left = x + 'px';
            o.style.top = y + 'px';
        }
        else {
            o.style.pixelLeft = x;
            o.style.pixelTop = y;
        }
}

function OTF_i(o, x, y, dx1, dy1, dx2, dy2){
    var l = OTF_x(o);
    var t = OTF_y(o);
    return ((x - (dx1 ? dx1 : 0) >= l) && (y - (dy1 ? dy1 : 0) >= t) && (x - (dx2 ? dx2 : 0) < l + OTF_w(o)) && (y - (dy2 ? dy2 : 0) < t + OTF_h(o)));
}

function OTF_o(a){
    var o = (document.getElementById ? document.getElementById(OTF_CL) : (document.all ? document.all[OTF_CL] : document[OTF_CL]));
    var n = (OTF_CO == null);
    if (OTF_CF && (a == OTF_CF)) 
        return false;
    if (!n) {
        if (document.layers) 
            OTF_CO.visibility = 'hide';
        else 
            OTF_CO.style.visibility = 'hidden';
    }
    OTF_CO = o;
    OTF_CF = a;
    var d;
    if (a.name == 'departureDate') {
        d = new Date();
        d.setDate(d.getDate() + 14);
        OTF_CSD = new Date();
    }
    else {
        d = new Date(OTF_CD.getFullYear(), OTF_CD.getMonth(), OTF_CD.getDate());
        d.setDate(d.getDate() + 1);
        OTF_CSD = d;
    }
    OTF_r(d.getMonth() + 1, d.getFullYear());
    OTF_m(o, OTF_x(a) + OTF_w(a) + 2, OTF_y(a) - 48);
    if (document.layers) 
        o.visibility = 'show';
    else 
        o.style.visibility = 'visible';
    if (n) {
        if (document.layers) 
            document.captureEvents(Event.MOUSEDOWN);
        else 
            document.onmousedown = OTF_v;
    }
}

function OTF_v(e){
    if (OTF_CO == null) 
        return;
    var x = (document.all ? event.clientX + document.body.scrollLeft : e.pageX);
    var y = (document.all ? event.clientY + document.body.scrollTop : e.pageY);
    if (!(OTF_i(OTF_CF, x, y, 0, 0, 4, 0) || OTF_i(OTF_CO, x, y))) 
        OTF_c();
}

function OTF_s(y, m, d){
    if (OTF_CO == null) 
        return;
    OTF_CF.value = (m < 10 ? '0' : '') + m + '/' + (d < 10 ? '0' : '') + d + '/' + y;
    if (OTF_CF.name == 'departureDate') 
        OTF_CD = new Date(y, m - 1, d);
    OTF_c();
}

function OTF_c(){
    if (document.layers) 
        OTF_CO.visibility = 'hide';
    else 
        OTF_CO.style.visibility = 'hidden';
    if (OTF_CO != null) {
        if (document.layers) 
            document.releaseEvents(Event.MOUSEDOWN);
        else 
            document.onmousedown = null;
    }
    OTF_CO = null;
    OTF_CF = null;
}

function OTF_r(m, y){
    if (OTF_CO == null) 
        return;
    var t_y = OTF_CSD.getFullYear();
    var t_m = OTF_CSD.getMonth() + 1;
    var t_d = OTF_CSD.getDate();
    var d = new Date(y, m - 1, 1);
    var max = (m == 2 ? 28 + ((y % 4 == 0) && ((y % 100 != 0) || (y % 400 == 0)) ? 1 : 0) : 30 + (m < 8 ? 1 - ((m - 1) % 2) : (m - 1) % 2));
    var html = '<table border=0 cellpadding=1 cellspacing=0 bgcolor="#003366"><tr><td><table border=0 cellpadding=3 cellspacing=1 width=150><tr bgcolor="#003366"><td align=center><a href="javascript:void(0)" onclick="OTF_r(';
    html += (m > 1 ? m - 1 : 12) + ',' + (m > 1 ? y : y - 1) + ')"><img src="http://www.worldexecutive.com/a_left.gif" width=8 height=8 border=0></a></td><td colspan=5 align=center style="font:bold 11px sans-serif;color:white">';
    html += OTF_MS[m - 1] + ' ' + y + '</td><td align=center><a href="javascript:void(0)" onclick="OTF_r(' + (m < 12 ? m + 1 : 1) + ',' + (m < 12 ? y : y + 1) + ')"><img src="http://www.worldexecutive.com/a_right.gif" width=8 height=8 border=0></a></td></tr><tr bgcolor="#e1e1e1">';
    html += '<td align=center style="font:10px sans-serif">S</td><td align=center style="font:10px sans-serif">M</td><td align=center style="font:10px sans-serif">T</td><td align=center style="font:10px sans-serif">W</td><td align=center style="font:10px sans-serif">T</td><td align=center style="font:10px sans-serif">F</td><td align=center style="font:10px sans-serif">S</td></tr>';
    var offset = d.getDay() + 1;
    var count = 1, i, a;
    for (i = 1; i <= max; i++) {
        if (count == 1) 
            html += '<tr bgcolor=white>';
        if ((i == 1) && (offset > 1)) {
            html += '<td colspan=' + (offset - 1) + ' bgcolor="#e1e1e1"><font size=1>&nbsp;</td>';
            count = offset;
        }
        a = ((y > t_y) || ((y == t_y) && ((m > t_m) || ((m == t_m) && (i >= t_d)))));
        html += '<td align=center style="font:' + (a ? 'bold 10px sans-serif"><a href="javascript:void(0)" onclick="OTF_s(' + y + ',' + m + ',' + i + ')">' : '10px sans-serif;color:gray">') + i + (a ? '</a>' : '') + '</td>';
        if ((i == max) && (count < 7)) {
            html += '<td colspan=' + (7 - count) + ' bgcolor="#e1e1e1"><font size=1>&nbsp;</td>';
            count = 7;
        }
        count++;
        if (count > 7) {
            count = 1;
            html += '</tr>';
        }
    }
    html += '</table></td></tr></table>';
    if (document.layers) {
        OTF_CO.document.open('text/html');
        OTF_CO.document.write(html);
        OTF_CO.document.close();
    }
    else 
        OTF_CO.innerHTML = html;
}

function OTF_d(d, n){
    if (!d.value) {
        alert('Please add a valid ' + n + '.');
        return false;
    }
    var e1, e2;
    if ((e1 = d.value.match(/^(1[0-2]|0?[1-9])\/(3[01]|[12][0-9]|0?[1-9])\/((20)?[0-9]{2})$/)) || (e2 = d.value.match(/^(3[01]|[12][0-9]|0?[1-9])\.\s*(1[0-2]|0?[1-9])\.\s*((20)?[0-9]{2})$/))) {
        var d, m, y;
        if (e1) {
            d = Number(e1[2]);
            m = Number(e1[1]);
            y = Number(e1[3]);
        }
        else {
            d = Number(e2[1]);
            m = Number(e2[2]);
            y = Number(e2[3]);
        }
        y = (y < 2000 ? y + 2000 : y);
        if (d > (m == 2 ? 28 + ((y % 4 == 0) && ((y % 100 != 0) || (y % 400 == 0)) ? 1 : 0) : 30 + (m < 8 ? 1 - ((m - 1) % 2) : (m - 1) % 2))) {
            alert('The ' + n + ' does not seem to be a valid date.');
            return false;
        }
        d.value = (m < 10 ? '0' : '') + m + '/' + (d < 10 ? '0' : '') + d + '/' + y;
    }
    else {
        alert('The ' + n + ' does not seem to be a valid date.\nPlease provide a date in either the format\n"mm/dd/yyyy" or "dd. mm. yyyy".');
        return false;
    }
    return true;
}

function OTF_t(f){
    if ((f.from && (!f.from.value)) || (f.to && (!f.to.value))) {
        alert('Please provide city names or airport codes\nfor your flight search.');
        return false;
    }
    if (!OTF_d(f.departureDate, 'Departure Date')) 
        return false;
    if (!OTF_d(f.returnDate, 'Return Date')) 
        return false;
    var d1 = f.departureDate.value.match(/^(1[0-2]|0[1-9])\/(3[01]|[12][0-9]|0[1-9])\/(20[0-9]{2})$/);
    var d2 = f.returnDate.value.match(/^(1[0-2]|0[1-9])\/(3[01]|[12][0-9]|0[1-9])\/(20[0-9]{2})$/);
    if ((d2[3] < d1[3]) || ((d2[3] == d1[3]) && ((d2[1] < d1[1]) || ((d2[1] == d1[1]) && (d2[2] < d1[2]))))) {
        alert('The Return Date must be ON OR AFTER the Departure Date.\nPlease correct and try again.');
        return false;
    }
    return true;
}
