// sets up the form to edit the given destination
function edit_destination(key){
    var destination_name = '';
    var destination_short_name = '';
    var google_lat = '';
    var google_lng = '';
    var street_address = '';
    var city = '';
    var province = '';
    var postal_code = '';
    var submit = default_submit_add_text;
    
    if(key != ''){
        show_destination(key);
        destination_name = dest_details[key][1];
        destination_short_name = dest_details[key][2];
        google_lat = dest_details[key][3];
        google_lng = dest_details[key][4];
        street_address = dest_details[key][5];
        city = dest_details[key][6];
        province = dest_details[key][7];
        postal_code = dest_details[key][8];
        submit = dest_details[key][9];
        document.getElementById('delete').style.display = 'block';
        }
    else{
        clear_destination();
        document.getElementById('delete').style.display = 'none';
        }
    document.getElementById("destination_name").value = destination_name;
    document.getElementById("destination_short_name").value = destination_short_name;
    document.getElementById("google_lat").value = google_lat;
    document.getElementById("google_lng").value = google_lng;
    document.getElementById("street_address").value = street_address;
    document.getElementById("city").value = city;
    document.getElementById("province").value = province;
    document.getElementById("postal_code").value = postal_code;
    document.getElementById("submit").value = submit;
}


// moves the map to the given destination
function show_destination(key){
    map.recenterOrPanToLatLng(destinations[key]);
    if(last_marker){
        map.removeOverlay(last_marker);
        }
    last_marker = dest_markers[key];
    map.addOverlay(last_marker);
}

// clears the last dest
function clear_destination(){
    if(last_marker){
        map.removeOverlay(last_marker);
        }
    last_marker = null;
}

// shows the rides to the names destination
function show_rides_to_destination(key){
    show_destination(key); // show the new dest
    clear_dest_rides_markers(); // get rid of old markers
    carpool_search_destination = key;
    if(carpool_search_needed_offered == 'needed' || carpool_search_needed_offered == 'all'){
        for(m in dest_rides[key]['needed']){
            map.addOverlay(dest_rides[key]['needed'][m]);
            }
        }
    if(carpool_search_needed_offered == 'offered' || carpool_search_needed_offered == 'all'){
        for(m in dest_rides[key]['offered']){
            map.addOverlay(dest_rides[key]['offered'][m]);
            }
        }
}

// switches the value of the needed or offered search var
function carpool_set_needed_offered_search(val){
    carpool_search_needed_offered =val;
    show_rides_to_destination(carpool_search_destination);
}

// clears the dest_rides markers
function clear_dest_rides_markers(){
    for(n in dest_rides[carpool_search_destination]){
        for(m in dest_rides[carpool_search_destination][n]){
            map.removeOverlay(dest_rides[carpool_search_destination][n][m]);
            }
        }
}

// moves the map to the given point
function show_home(point, text){
    if(last_clicked){
        // remove the old marker
        map.removeOverlay(last_clicked);
    }
    last_clicked = create_ride_marker(point, text, getCheckedValue(document.getElementById('Rides_QuickForm').elements['needed_offered']));
    map.recenterOrPanToLatLng(point);
    map.addOverlay(last_clicked);
    
}

// swaps the icon used for the home marker
function redisplay_home(text){
if(last_clicked){
    show_home(last_clicked.point, text);
    }
}

// shows where the user clicked and clears the last place they clicked
function show_click(point){
    if(last_clicked){
        // remove the old marker
        map.removeOverlay(last_clicked);
    }
    last_clicked = new GMarker(point);
    map.recenterOrPanToLatLng(point);
    map.addOverlay(last_clicked);


}



// returns the value of the checked radio group
function getCheckedValue(radioObj) {

    if(!radioObj)
        return "";
    var radioLength = radioObj.length;
    if(radioLength == undefined)
        if(radioObj.checked)
            return radioObj.value;
        else
            return "";
    for(var i = 0; i < radioLength; i++) {
        if(radioObj[i].checked) {
            return radioObj[i].value;
        }
    }
    return "";
}



// Creates a marker whose info window displays the given number
function create_ride_marker(point, text, needed_offered) {
var icon = new GIcon();
if(needed_offered == 'offered'){
    icon.image = "data/green.png";
    }
else{
    icon.image = "data/yellow.png";
    }

icon.shadow = "data/shadow.png";
icon.iconSize = new GSize(15, 24);
icon.shadowSize = new GSize(29, 24);
icon.iconAnchor = new GPoint(7, 24);
icon.infoWindowAnchor = new GPoint(7, 1);
var marker = new GMarker(point, icon);

  GEvent.addListener(marker, "click", function() {
    marker.openInfoWindowHtml(text);
  });

  return marker;
}



var num_new_rides = 0;
var to_from = 'to';
// adds a ride form element to the given table
function addRide(id, label){

    var newoption;
    num_new_rides++;
    var dayselect = document.createElement('select');
    dayselect.name = "newride_"+num_new_rides+"[day_of_week]";
    dayselect.id = "newride_"+num_new_rides+"[day_of_week]";
    for(dow in carpool_days_of_week){
        newoption = document.createElement('option');
        newoption.value = dow;
        newoption.text = carpool_days_of_week[dow];
        try {
            dayselect.add(newoption,null);
            } catch (ie) {
            dayselect.add(newoption,dayselect.length)
            }
        }
    hoursselect = document.createElement('select');
    hoursselect.name = "newride_"+num_new_rides+"[h]";
    hoursselect.id = "newride_"+num_new_rides+"[h]";
    for(var i = 1; i <= 12; i++){
        newoption = document.createElement('option');
        newoption.value = i;
        newoption.text = i;
        try {
            hoursselect.add(newoption,null);
            } catch (ie) {
            hoursselect.add(newoption,hoursselect.length);
            }
        }
    minutesselect = document.createElement('select');
    minutesselect.name = "newride_"+num_new_rides+"[i]";
    minutesselect.id = "newride_"+num_new_rides+"[i]";
    for(var j = 0; j < 6; j++){
        newoption = document.createElement('option');
        newoption.value = j + "0";
        newoption.text = j + "0";
        try{
            minutesselect.add(newoption,null);        
            } catch (ie) {
            minutesselect.add(newoption,minutesselect.length);
            }
        }   
    ampm = new Array();
    ampm['am'] = 'am';
    ampm['pm'] = 'pm';
    ampmselect = document.createElement('select');
    ampmselect.name = "newride_"+num_new_rides+"[a]";
    ampmselect.id = "newride_"+num_new_rides+"[a]";
    for(ampmvalue in ampm){
        newoption = document.createElement('option');
        newoption.value = ampmvalue;
        newoption.text = ampm[ampmvalue];        
        try{
            ampmselect.add(newoption,null);        
            } catch (ie) {
            ampmselect.add(newoption,ampmselect.length);
            }
        }
    
     
    try {
        radio_to = document.createElement("<INPUT name='newride_"+num_new_rides+"[to_from]'>");
        } catch (ie){
        radio_to = document.createElement('input');
        radio_to.name = "newride_"+num_new_rides+"[to_from]";
        }
    radio_to.type = 'radio';
    radio_to.id = "newride_"+num_new_rides+"_to";
    radio_to.value = "to";
    try {
        radio_from = document.createElement("<INPUT name='newride_"+num_new_rides+"[to_from]'>");
        } catch(ie) {
        radio_from = document.createElement('input');
        radio_from.name = "newride_"+num_new_rides+"[to_from]";
        }
    radio_from.type = 'radio';
    radio_from.id = "newride_"+num_new_rides+"_from";
    radio_from.value = "from";
     
     
    var tbody = document.getElementById(id).getElementsByTagName("TBODY")[0];
    var row = document.createElement("TR");
    var td1 = document.createElement("TD");
    //td1.appendChild(document.createTextNode(label[0]));
    var td2 = document.createElement("TD");
    td2.appendChild(radio_to);
    td2.appendChild(document.createTextNode(label[1]));
    td2.appendChild(radio_from);
    td2.appendChild(document.createTextNode(label[2]));
    //td2.appendChild(document.createTextNode(label[3]));
    td2.appendChild(dayselect);
    td2.appendChild(document.createTextNode(label[4]));
    td2.appendChild(hoursselect);
    td2.appendChild(minutesselect);
    td2.appendChild(ampmselect);
    row.appendChild(td1);
    row.appendChild(td2);
    tbody.appendChild(row);    
    // because IE can only manipulate the checked state of radio buttons after they are added, we do it last
    radio_to.checked = true;
    if(num_new_rides > 1){
        last_num = num_new_rides -1;
        last_day_sel = document.getElementById("newride_"+last_num+"[day_of_week]"); 
        last_hour_sel = document.getElementById("newride_"+last_num+"[h]"); 
        last_min_sel = document.getElementById("newride_"+last_num+"[i]"); 
        last_ampm_sel = document.getElementById("newride_"+last_num+"[a]"); 
        dayselect.selectedIndex = (last_day_sel.selectedIndex + 1) % 7;
        hoursselect.selectedIndex = last_hour_sel.selectedIndex;
        minutesselect.selectedIndex = last_min_sel.selectedIndex;
        ampmselect.selectedIndex = last_ampm_sel.selectedIndex;
        last_radio_to = document.getElementById("newride_"+last_num+"_to"); 
        last_radio_from = document.getElementById("newride_"+last_num+"_from"); 
        if(last_radio_to.checked){
            radio_to.checked = true;
            radio_from.checked = false;
            }
        if(last_radio_from.checked){
            radio_from.checked = true;
            radio_to.checked = false;
            }
        }
}



