function expandable_toggle(subject) {
	var id = $(subject).getProperty('id').split('-')[1];
	var ul = $('expandable-' + id);
	var img = $('toggle_image-' + id);
	if(ul.getStyle('display') == 'block') {
		ul.setStyle('display', 'none');
		img.setAttribute('src', '/images/plus.gif');
	}
	else {
		ul.setStyle('display', 'block');
		img.setAttribute('src', '/images/minus.gif');
	}
}


function confirm_user_deletion(name, id, type) {
	if (! confirm("Are you sure you want to delete this " + type + "?\n\nName: " + name + "  ID:" + id))
		return false;
	else 
		return true;
}

function confirm_multiple_user_deletion(type) {
	if (! confirm("Are you sure you want to delete the selected " + type + "s?"))
		return false;
	else 
		return true;
}


function assign_tutor(select, student_user_id) {
  select = $(select);
  var selected = select.getSelected()[0];
  if (selected) {
    var tutor_id = selected.getProperty('value');
    if (tutor_id) {
      var request = new Request({
        url: '/mom/assign',
        method: 'post',
        data: {'student_user_id': student_user_id, 'tutor_id': tutor_id},
        onSuccess: function() {
          select.getParent().set('text', selected.get('text'));
          window.open("/mailto/tutor_assignment?tutor_id="+tutor_id+"&student_user_id="+student_user_id, "mailto", "menubar=0,resizable=1,width=853,height=650");
        }
      }).send();
    }
  }
}

function recommend_tutors(student_user_id) {
    var overlay_parent = $('joblisting-recommend-'+student_user_id);
    var overlay = new Element('div', {
        'styles': {
            'position': 'absolute',
            'z-index': 100
        }
    });
    overlay.set('html', '<div class="tip" style="padding:4em;">Searching...</div>');
    overlay.inject(overlay_parent, 'top');
    
  var request = new Request({
    url: '/mom/recommend_tutors',
    method: 'post',
    data: {'student_user_id': student_user_id},
    onSuccess: function(responseText) {
        overlay.set('html', responseText);
    }
  }).send();
}

function add_availability_input(type) {
  var request = new Request({
    url: '/'+type+'/add_availability_input',
    method: 'post',
    data: {},
    onSuccess: function(responseText) {
        if ($('no-availability')) {
            $('no-availability').dispose();
        }
        var input = new Element('div', {
            'styles': { 'margin': 0, 'padding': 0}
        });
        input.set('html', responseText);
        input.inject($('availability-input-container'));
    }
  }).send();
}

function delete_availability(type, id, element) {
  var request = new Request({
    url: '/'+type+'/delete_availability',
    method: 'post',
    data: {'id': id},
    onSuccess: function(responseText) {
        $(element).dispose();
    }
  }).send();
}


function alter_mailto(checkbox, email) {
  var mailto = $('mailto');
  if (mailto) {
    var addresses = mailto.getProperty('href').substring(7).split(',');
    if (addresses.length > 0 && addresses[0] == '') {
      addresses.splice(0,1);
    }
    if ($(checkbox).getProperty('checked')) {
      addresses.push(email);
    }
    else {
      for (var i = 0; i < addresses.length; i++) {
        if (addresses[i] == email) {
          addresses.splice(i, 1);
          break;
        }
      }
    }
    mailto.setProperty('href', 'mailto:' + addresses.join(','));
  }
}

var text_mailto_addresses = [];

function alter_text_mailto(checkbox, email) {
    if ($(checkbox).getProperty('checked')) {
      text_mailto_addresses.push(email);
    }
    else {
      for (var i = 0; i < text_mailto_addresses.length; i++) {
        if (text_mailto_addresses[i] == email) {
          text_mailto_addresses.splice(i, 1);
          break;
        }
      }
    }
}

function text_mailto() {
    if (text_mailto_addresses) {
        window.open("/mailto/smspopup?to=" + encodeURIComponent(text_mailto_addresses.join(',')), "mailto", "menubar=0,resizable=1,width=853,height=550");
    }
}

function select_all(selector, value) {
    $$(selector).each( function(e) {
        if (value && !e.getProperty('checked')) {
            e.setProperty('checked', 'checked');
            eval(e.getProperty('onclick'));
        }
        else if (!value && e.getProperty('checked')) {
            e.removeProperty('checked');
            eval(e.getProperty('onclick'));
        }
    });
}

function add_tutor_manager(tutor_id, manager_id) {
  var request = new Request({
    url: '/mom/add_tutor_manager',
    method: 'post',
    data: {'manager_id': manager_id, 'tutor_id': tutor_id},
    onSuccess: function() {
      $('add-'+tutor_id).dispose();
    }
  }).send();
}

function deactivate_user(user_id) {
  if (window.confirm('Are you sure you want to delete this job listing?')) {
      var request = new Request({
        url: '/mom/deactivate_user',
        method: 'post',
        data: {'user_id': user_id},
        onSuccess: function() {
          //$('deactivate-'+user_id).dispose();
          $('joblisting-'+user_id).dispose();
        }
      }).send();
  }
}

function send_tutor_status_email(user_id, status) {
    if (window.confirm('Are you sure you want to send the standard email notification that this tutor has been ' + status + '?')) {
        var request = new Request({
            url: '/hr_admin/notify_tutor',
            method: 'post',
            data: {'user_id': user_id, 'status': status},
            onSuccess: function(responseText) {
                if (responseText.indexOf('sent') > -1) {
                    $('tutor-status-email').set('text', 'Email sent!');
                }
                else {
                    $('tutor-status-email').set('text', 'Error sending email');
                }
            }
        }).send();
    }
}

function send_tutor_contract_email(from_user_type, user_id) {
    var request = new Request({
        url: '/'+from_user_type+'/send_tutor_contract',
        method: 'post',
        data: {'user_id': user_id},
        onSuccess: function(responseText) {
            if (responseText.indexOf('sent') > -1) {
                $('tutor-contract-email').set('text', 'Email sent!');
            }
            else {
                $('tutor-contract-email').set('text', 'Error sending email');
            }
        }
    }).send();
}

window.addEvent('domready', function() { 
	
	
	if($('dismiss')) {
		$('dismiss').addEvent('click', function() {
			new Request({
				url: '/ajax/dismiss',
				method: 'get',
				data: {}, 
				onSuccess: function() {
					window.location = window.location;
				}
			}).send();
		});
	}

    // reminder delete events
    $$('.reminder_checkbox').each(function(el) {
        el.addEvent('click', function() {
            if (confirm('Delete this reminder?')) {
                var reminder_entry = $('reminder-'+el.getProperty('id'));
                new Request({   url: '/reminder/delete',
                                method:'post',
                                data: { 'test':'test','reminder_id': el.getProperty('id') },
                                onSuccess: function() {
                                    reminder_entry.dispose();
                                }
                            }).send();
            }
        });
    });
    
    // reminder add
    if ($('reminder_add')) {
        $('reminder_add').addEvent('click', function(event) {
            event.stop();
            var content = $('reminder_new').get('value');
            new Request({   url: '/reminder/add',
                            method:'post',
                            data: { 'content': content },
                            onSuccess: function(responseText) {
                                if (responseText) {
                                    var input = new Element('input', {
                                        'class': 'reminder_checkbox',
                                        'type': 'checkbox',
                                        'id': responseText});
                                    var list_item = new Element('li', {
                                        'class': 'reminder_entry',
                                        'id': 'reminder-' + responseText});
                                    $('reminder_new').set('value', '');
                                    list_item.set('text', ' ' + content);
                                    input.inject(list_item, 'top');
                                    list_item.inject($('reminder_list'));

                                    input.addEvent('click', function() {
                                        if (confirm('Delete this reminder?')) {
                                            var reminder_entry = $('reminder-'+responseText);
                                            new Request({   url: '/reminder/delete',
                                                            method:'post',
                                                            data: { 'test':'test','reminder_id': responseText },
                                                            onSuccess: function() {
                                                                reminder_entry.dispose();
                                                            }
                                                        }).send();
                                        }
                                    });

                                }
                                else {
                                    alert('There was an error while trying to add your reminder.');
                                }
                            }
                        }).send();
        });
    }
    
    if (typeof Tips != 'undefined') {
      //store titles and text for tips
      $$('.showtip').each(function(element,index) {  
          var content = element.get('title').split('::');  
          element.store('tip:title', content[0]);  
          element.store('tip:text', content[1]);
      });  
    
      //create the tooltips  
      var tips = new Tips('.showtip',{  
          //className: 'tip',  
          fixed: true,  
          hideDelay: 50,  
          showDelay: 50  
      });
    }
    
    // Interview time selection
    if ($('interview-times')) {
        $('interview-times').getElements('td.available').each(function(td){
            td.addEvent('click', function (event) {
                element = $(event.target);
                $('interview_time').setProperty('value', element.getChildren()[0].getProperty('value'));
                $('interview-times').getElements('td.selected').each(function(item) {
                    item.removeClass('selected');
                });
                element.addClass('selected');
            });

        });
    }

});

// Google map on /mom/tutors
//if (window.location.toString().indexOf('/mom/tutors') > -1 || window.location.toString().indexOf('/mom/clients') > -1) {
// Do we really need to limit this? Temporarily opened up to test hr_admin, superset of Moms reach

  var map = null;
  var geocoder = null;
  var bounds = null;
  var remaining_locations = null;

  
  function showTutorMap(state, map_id) {
    if (GBrowserIsCompatible()) {
      map = new GMap2(document.getElementById(map_id));
      bounds = new GLatLngBounds();
      geocoder = new GClientGeocoder();
      if (geocoder) {
        geocoder.getLatLng(state,
          function(point) {
            if (!point) {
              //alert(state + " not found");
            } else {
              map.setCenter(point, 10);
              map.addControl(new GLargeMapControl());
              map.addControl(new GMapTypeControl());
            }
          }
        );
      }
    }
  }

  function addToMap(address, textInfo, color) {
    if (GBrowserIsCompatible()) {
      if (geocoder) {
        geocoder.getLocations(address,
          function(response) {
            if (response) {
              if (response.Status.code == 200) {
                place = response.Placemark[0];
                point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
                if (!point) {
                  //alert(address + " not found");
                } else {
                  if (color == 'green') {
                    var colorIcon = new GIcon(G_DEFAULT_ICON);
                    colorIcon.image = "http://www.google.com/mapfiles/dd-start.png";
                    var marker = new GMarker(point, {icon:colorIcon});
                  }
                  else {
                    var marker = new GMarker(point);
                  }
                  marker.bindInfoWindowHtml(textInfo);
                  map.addOverlay(marker);
                  bounds.extend(point);
                }
              }
              else {
                //alert(response.Status.code);
              }
            }

            remaining_locations--;
            if (remaining_locations <= 0) {
              autoZoomMap();
            }
          }
        );
      }
    }
  }

  function autoZoomMap() {
    map.setZoom(map.getBoundsZoomLevel(bounds));
    map.setCenter(bounds.getCenter());
  }

//End of disabled MOM check
//}
