var searchForm = function() {

function createCookie(name,value,days) {
    if (days) {
	var date = new Date();
	date.setTime(date.getTime()+(days*24*60*60*1000));
	var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
	var c = ca[i];
	while (c.charAt(0)==' ') c = c.substring(1,c.length);
	if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name,"",-1);
}


function dater1Select(objDate) {
    $('SF_date1').value = objDate.getPHPDate('m/d/y');
    dater2.setEarliestDate(objDate);
}
function dater2Select(objDate) {
    $('SF_date2').value = objDate.getPHPDate('m/d/y');
}

var endDate = new Date();
var todayDate = new Date();
endDate.setDate(endDate.getDate()+330);
var dater1 = new DateChooser();
var dater2 = new DateChooser();

dater1.setXOffset(0);
dater1.setYOffset(20);
dater1.setUpdateFunction(dater1Select);
dater1.setEarliestDate(todayDate);
dater1.setLatestDate(endDate);

dater2.setXOffset(0);
dater2.setYOffset(20);
dater2.setUpdateFunction(dater2Select);
dater2.setEarliestDate(todayDate);
dater2.setLatestDate(endDate);


function showIt(ele) {
    ele.setStyle({display:'block'});
}
function hideIt(ele) {
    ele.setStyle({display:'none'});
}
function popper(text) {
    var popped = window.open('','qmarkwin','toolbar=no,status=no,scrollbars=yes,resizable=no,width=400,height=300');
    popped.document.open();
    popped.document.write('<html><head><title>Info</title><link href="searchForm.css" type="text/css" rel="stylesheet"></head><body style="padding:10px;background:white;">');
    popped.document.write(text);
    popped.document.write('</body></html>');
    popped.document.close();
    popped.focus();
}


function goOptions(radio) {
    switch(radio.value) {
	case 'air':
	    hideIt( $$('.SF_rooms')[0] );
	    showIt( $('SF_airx') );
	    showIt( $$('.SF_Opair')[0] );
	    if( $('SF_roundtrip2').checked ) {
		hideIt( $('SF_returningDate') );
            }
	    $('SF_travelers').innerHTML='Travelers: <img src="images/SF_qmark.gif" id="SF_TravelersQMark">';
	    $('SF_TravelersQMark').observe('click',function() {
		popper( '<h3>Travelers</h3><p>If you are between the ages of 13 and 61 years old, you are considered an adult by the airlines.  Most airlines limit child fares to passengers betweeen the ages of two and 12 years old.  Providing this information will help determine whether one or more passengers in your reservation qualify for a child discount fare.  Please remember that special fares are not available in all markets or on all fares.  If you will be flying with an infant (a child less than 24 months old), please call to find out the rules for purchasing a ticket for them.  Reservations for infants cannot be made online.</p>' );
            });
	    break;
	case 'room':
	    showIt( $$('.SF_rooms')[0] );
	    showIt( $('SF_returningDate') );
	    hideIt( $('SF_airx') );
	    hideIt( $$('.SF_Opair')[0] );
	    $('SF_travelers').innerHTML='Rooms and Travelers: <img src="images/SF_qmark.gif" id="SF_RoomsQMark" alt="Rooms and Travelers">';
	    $('SF_RoomsQMark').observe('click',function() {
		popper( '<h3>Rooms</h3><p>If you need more rooms than the number supported on the drop down, you can make separate bookings or search for the rooms you wish to book and call our support center.  For a search including only one room, the room occupancy is limited to five travelers.  However, for a multiple room search involving two to four rooms, the maximum number of total travelers is six.</p>' );
	    });
	    $('SF_departing').innerHTML = 'Check-in:';
	    $('SF_returning').innerHTML = 'Check-out:';
	    $('SF_destination').innerHTML = 'City or airport:';
	    break;
	case 'car':
	    hideIt( $$('.SF_rooms')[0] );
	    showIt( $('SF_returningDate') );
	    hideIt( $('SF_airx') );
	    hideIt( $$('.SF_Opair')[0] );
	    showIt( $('SF_cartype') );
	    $('SF_destination').innerHTML = 'Pick-up airport:';
	    $('SF_departing').innerHTML = 'Pick-up on:';
	    $('SF_returning').innerHTML = 'Drop-offf on:';
	    $('SF_travelers').innerHTML = 'Travelers:';
	    break;
	case 'air_room_car':
	    showIt( $$('.SF_rooms')[0] );
	    hideIt( $('SF_airx') );
	    showIt( $$('.SF_Opair')[0] );
	    $('SF_roundtrip1').checked = true;
	    break;
	case 'air_room':
	    showIt( $$('.SF_rooms')[0] );
	    hideIt( $('SF_airx') );
	    showIt( $$('.SF_Opair')[0] );
	    $('SF_roundtrip1').checked = true;
	    break;
	case 'room_car':
	    showIt( $$('.SF_rooms')[0] );
	    hideIt( $('SF_airx') );
	    hideIt( $$('.SF_Opair')[0] );
	    $('SF_travelers').innerHTML='Rooms and Travelers: <img src="images/SF_qmark.gif" id="SF_RoomsQMark" alt="Rooms and Travelers">';
	    $('SF_RoomsQMark').observe('click',function() {
		popper( '<h3>Rooms</h3><p>If you need more rooms than the number supported on the drop down, you can make separate bookings or search for the rooms you wish to book and call our support center.  For a search including only one room, the room occupancy is limited to five travelers.  However, for a multiple room search involving two to four rooms, the maximum number of total travelers is six.</p>' );
	    });
	    $('SF_departing').innerHTML = 'Check-in:';
	    $('SF_returning').innerHTML = 'Check-out:';
	    break;
	case 'air_car':
	    hideIt( $$('.SF_rooms')[0] );
	    hideIt( $('SF_airx') );
	    showIt( $$('.SF_Opair')[0] );
	    $('SF_roundtrip1').checked = true;
	    $('SF_travelers').innerHTML='Travelers: <img src="images/SF_qmark.gif" id="SF_TravelersQMark">';
	    $('SF_TravelersQMark').observe('click',function() {
		popper( '<h3>Travelers</h3><p>If you are between the ages of 13 and 61 years old, you are considered an adult by the airlines.  Most airlines limit child fares to passengers betweeen the ages of two and 12 years old.  Providing this information will help determine whether one or more passengers in your reservation qualify for a child discount fare.  Please remember that special fares are not available in all markets or on all fares.  If you will be flying with an infant (a child less than 24 months old), please call to find out the rules for purchasing a ticket for them.  Reservations for infants cannot be made online.</p>' );
            });
	    break;
    }

}

$$('#SF_ropt input').each( function(ele) {
    ele.observe('click',function() {
        $('SF_departing').innerHTML = 'Departing:';
        $('SF_returning').innerHTML = 'Returning:';
        $('SF_travelers').innerHTML = 'Rooms and Travelers: <img src="images/SF_qmark.gif" id="SF_RoomTravelersQMark">';
        $('SF_RoomTravelersQMark').observe('click',function() {
        popper( '<h3>Rooms</h3><p>If you need more rooms than the number supported on the drop down, you can make separate bookings or search for the rooms you wish to book and call our support center.  For a search including only one room, the room occupancy is limited to five travelers.  However, for a multiple room search involving two to four rooms, the maximum number of total travelers is six.</p><h3>Travelers</h3><p>If you are between the ages of 13 and 61 years old, you are considered an adult by the airlines.  Most airlines limit child fares to passengers betweeen the ages of two and 12 years old.  Providing this information will help determine whether one or more passengers in your reservation qualify for a child discount fare.  Please remember that special fares are not available in all markets or on all fares.  If you will be flying with an infant (a child less than 24 months old), please call to find out the rules for purchasing a ticket for them.  Reservations for infants cannot be made online.</p>' );
        });
        $('SF_destination').innerHTML = 'Going to:';
        showIt( $('SF_returningDate') );
        hideIt( $('SF_cartype') );
        $('SF_car_type').options[0].selected = true;
	goOptions(ele);
    });
});

$('SF_roundtrip1').observe('click',function() {
    showIt( $('SF_returningDate') );
});
$('SF_roundtrip2').observe('click',function() {
    hideIt( $('SF_returningDate') );
});
$('SF_searcher').observe('click',function() {
    formSubmit();
});

$('SF_date1').observe('click', dater1.display);
$('SF_date2').observe('click', dater2.display);

function formSubmit() { 
    var searchChoice;
    $$('input[name="choice"]').each( function(ele) {
        if( ele.checked ) { searchChoice = ele.value; }
    });
    switch( searchChoice ) {
        case 'air':
	    $$('input[name="air1"]')[0].value = true;
	    break;
        case 'room':
	    $$('input[name="room1"]')[0].value = true;
	    break;
        case 'car':
	    $$('input[name="car1"]')[0].value = true;
	    break;
        case 'air_room_car':
	    $$('input[name="air1"]')[0].value = true;
	    $$('input[name="room1"]')[0].value = true;
	    $$('input[name="car1"]')[0].value = true;
	    break;
        case 'air_room':
	    $$('input[name="air1"]')[0].value = true;
	    $$('input[name="room1"]')[0].value = true;
	    break;
        case 'room_car':
	    $$('input[name="room1"]')[0].value = true;
	    $$('input[name="car1"]')[0].value = true;
	    break;
        case 'air_car':
	    $$('input[name="air1"]')[0].value = true;
	    $$('input[name="car1"]')[0].value = true;
	    break;
    }
    if( searchChoice != 'room_car' && $('SF_area1').value == '' ) {
	alert('Please enter a "Leaving from" city or airport code');
	$('SF_area1').focus();
	return;
    }
    var dateRegX = /^\d{2}\/\d{2}\/\d{2}/;
    if( ! $('SF_date1').value.match(dateRegX) ) {
	if( searchChoice != 'room_car' ) {
	    alert('Please enter a "Departing" date in the mm/dd/yy format');
	}
	else {
	    alert('Please enter a "Check-in" date in the mm/dd/yy format');
	}
	return;
    }
    if( ! $('SF_date2').value.match(dateRegX) ) {
	if( searchChoice != 'room_car' ) {
	    alert('Please enter a "Returning" date in the mm/dd/yy format');
	}
	else {
	    alert('Please enter a "Check-out" date in the mm/dd/yy format');
	}
	return;
    }
    createCookie( 'SF_FORM', $('SF_Form').serialize(), 0);
    $('SF_Form').submit();
}

function formInit() { 
    var cook = readCookie('SF_FORM');
    if( cook == null ) { cook = ''; }
    var lastSearch = new Object();
    $(cook.split(/&/)).each( function(ele) {
	var key = ele.split('=')[0]; 
	var val = ele.split('=')[1]; 
	lastSearch[key] = unescape(val).replace(/\+/g,' ');
    });

    if( lastSearch.choice ) {
	$$('input[name="choice"][value='+lastSearch.choice+']').each( function(ele) {
	    ele.checked = true;
	    goOptions(ele);
	});
	if( lastSearch.roundtrip ) {
	    if( lastSearch.roundtrip == 'true' ) {
		$('SF_roundtrip1').checked = true;
	    }
	    else {
		$('SF_roundtrip2').checked = true;
		hideIt( $('SF_returningDate') );
	    }
	}
	if( lastSearch.area1 ) {
	    $('SF_area1').value = lastSearch.area1;
	}
	$$('#SF_area2 option[value="'+lastSearch.area2+'"]')[0].selected = true;
	if( lastSearch.date1 ) {
	    $('SF_date1').value = lastSearch.date1;
	}
	else { $('SF_date1').value = 'mm/dd/yy'; }
	if( lastSearch.date2 ) {
	    $('SF_date2').value = lastSearch.date2;
	}
	else { $('SF_date2').value = 'mm/dd/yy'; }
	if( lastSearch.num_rooms ) {
	    $$('#SF_num_rooms option[value="'+lastSearch.num_rooms+'"]')[0].selected=true;
	}
	if( lastSearch.adults ) {
	    $$('#SF_adults option[value="'+lastSearch.adults+'"]')[0].selected=true;
	}
	if( lastSearch.children ) {
	    $$('#SF_children option[value="'+lastSearch.children+'"]')[0].selected=true;
	}
	if( lastSearch.car_type ) {
	    $$('#SF_car_type option[value="'+lastSearch.car_type+'"]')[0].selected=true;
	}
    }
    else {
	$('SF_air_room_car').checked = true;
	$('SF_roundtrip1').checked = true;
        $('SF_departing').innerHTML = 'Departing:';
        $('SF_returning').innerHTML = 'Returning:';
        $('SF_travelers').innerHTML = 'Rooms and Travelers: <img src="images/SF_qmark.gif" id="SF_RoomTravelersQMark">';
        $('SF_RoomTravelersQMark').observe('click',function() {
        popper( '<h3>Rooms</h3><p>If you need more rooms than the number supported on the drop down, you can make separate bookings or search for the rooms you wish to book and call our support center.  For a search including only one room, the room occupancy is limited to five travelers.  However, for a multiple room search involving two to four rooms, the maximum number of total travelers is six.</p><h3>Travelers</h3><p>If you are between the ages of 13 and 61 years old, you are considered an adult by the airlines.  Most airlines limit child fares to passengers betweeen the ages of two and 12 years old.  Providing this information will help determine whether one or more passengers in your reservation qualify for a child discount fare.  Please remember that special fares are not available in all markets or on all fares.  If you will be flying with an infant (a child less than 24 months old), please call to find out the rules for purchasing a ticket for them.  Reservations for infants cannot be made online.</p>' );
        });
        $('SF_destination').innerHTML = 'Going to:';
        showIt( $('SF_returningDate') );
        hideIt( $('SF_cartype') );
        $('SF_car_type').options[0].selected = true;
	goOptions( $('SF_air_room_car') );
    }
    showIt( $('SF_Main') );
}


formInit();
}
