function in_array( what, where ){
    var a=false;
    for(var i=0;i<where.length;i++){
      if(what == where[i]){
        a=true;
        break;
      }
    }
    return a;
}

$(function() {

    $('tr[class^=child-]').hide().children('td');
    $('tr[class^=editchild-]').hide().children('td');

jQuery.fn.fadeToggle = function(speed, easing, callback) {
   return this.animate({opacity: 'toggle'}, speed, easing, callback);

};
});

//Get cities from state
function list_cities(state, city) {
  if (state == '0') {
    $("#city").hide();
    return false;
  }
  $(".loading-state").show();
  $.ajax({
    type: "GET",
    url: "/registration/list_cities/"+state+"/ajax/"+city,
    success: function(html){
      $("#city").html(html);
      $("#city").show();
      $(".loading-state").hide();
    },
    error: function(obj1, e, obj2) {
      $("#error").html('An error was encountered.');
      $("#error").show();
      $.scrollTo("#error");
      $(".loading-state").hide();
    }
  });
};

//if state = other, show custom option
function check_other(state) {
  if (state >= 10000) {
    $("#state_other").show();
    return true;
  } else {
    $("#state_other").hide();
    $("#city_other").hide();
    return true;
  }
};

//if city = other, show custom option
function check_other2(city) {
  if (city >= 10000) {
    $("#city_other").show();
    return true;
  } else {
    $("#city_other").hide();
    return true;
  }
};

//Get teams from event id
function list_teams(event_id) {
  if (event_id == '0') {
    $("#team_id").hide();
    return false;
  }
  $(".loading-event").show();
  $.ajax({
    type: "GET",
    url: "/registration/list_teams/"+event_id+"/ajax/",
    success: function(html){
      $("#team_id").html(html);
      $("#team_id").show();
      $(".loading-event").hide();
    },
    error: function(obj1, e, obj2) {
      $("#error").html('An error was encountered.');
      $("#error").show();
      $.scrollTo("#error");
      $(".loading-event").hide();
    }
  });
};

//Get organizations from event id
function list_organizations(event_id) {
  if (event_id == '0') {
    $("#organization_id").hide();
    return false;
  }
  $(".loading-event").show();
  $.ajax({
    type: "GET",
    url: "/registration/list_organizations/"+event_id+"/ajax/",
    success: function(html){
      $("#organization_id").html(eval("(" + html + ")"));
      $("#organization_id").show();
      $(".loading-event").hide();
    },
    error: function(obj1, e, obj2) {
      $("#error").html('An error was encountered.');
      $("#error").show();
      $.scrollTo("#error");
      $(".loading-event").hide();
    }
  });
};

//Get teams from event id
function list_teams2(event_id) {
  if (event_id == '0') {
    $("#team_id").hide();
    return false;
  }
  $(".loading-event").show();
  $.ajax({
    type: "GET",
    url: "/registration/list_teams/"+event_id+"/ajax/true/",
    success: function(html){
      $("#team_id").html(html);
      $("#team_id").show();
      $(".loading-event").hide();
    },
    error: function(obj1, e, obj2) {
      $("#error").html('An error was encountered.');
      $("#error").show();
      $.scrollTo("#error");
      $(".loading-event").hide();
    }
  });
};

//Get times from event id
function list_time(event_id) {
  if (event_id == '0') {
    $("#time_10k").hide();
    return false;
  } else if (event_id == '3') {
    var tmp = '5k';
  } else if (event_id == '1' || event_id == '2') {
    var tmp = '10k';
  }
  $(".loading-event").show();
  $.ajax({
    type: "GET",
    url: "/registration/list_time/"+event_id+"/ajax/",
    success: function(html){
      $("#time_name").html(tmp);
      $("#time_10k").html(html);
      $("#time_10k").show();
      $(".loading-event").hide();
    },
    error: function(obj1, e, obj2) {
      $("#error").html('An error was encountered.'+event_id);
      $("#error").show();
      $.scrollTo("#error");
      $(".loading-event").hide();
    }
  });
};

//Hides additional form input based on if runner or volunteer, plus it loads the tshirt sizes in case of volunteer
function hide_elements(type, team_category) {
  if (in_array(type, volunteer_only_ids)) {
    $("#runner").hide();
    $(".loading-type").show();
    //1 = Male, 2 = Female, 4 = Volunteer
    var type = parseInt(type);
    $.ajax({
      type: "GET",
      url: "/registration/list_tshirt/"+type+"_"+team_category+"/ajax/",
      success: function(html){
        $("#tshirt_size").html(html);
        $("#tshirt_size").show();
        $(".loading-type").hide();
      },
      error: function(obj1, e, obj2) {
        $("#error").html('An error was encountered.');
        $("#error").show();
        $.scrollTo("#error");
        $(".loading-type").hide();
      }
    });
  } else {
    $("#runner").show();
    $("#tshirt_size").html('');
    $("#tshirt_size").hide();
  }
};

//Hides additional form input based on if captain is running or non-running
function hide_captain_elements(type) {
  if (in_array(type, captain_nonrunning_ids)) { // Non-running
    $("#runner").hide();
    $("#tshirt_size").hide();
  } else {
    $("#runner").show();
    $("#tshirt_size").show();
  }
};

//Hides additional form input based on if captain is running or non-running
function hide_captain_elements2(type) {
  if (in_array(type, captain_nonrunning_ids)) { // Non-running
    $(".meal_preference").hide();
    $(".time_10k").hide();
    $(".tshirt_size").hide();
  } else {
    $(".meal_preference").show();
    $(".time_10k").show();
    $(".tshirt_size").show();
  }
};

//Loads tshirts corresponding to Male or Female
function load_tshirt(type, team_category) {
  $(".loading-sex").show();
  $.ajax({
    type: "GET",
    url: "/registration/list_tshirt/"+type+"_"+team_category+"/ajax/",
    success: function(html){
      $("#tshirt_size").html(html);
      $("#tshirt_size").show();
      $(".loading-sex").hide();
    },
    error: function(obj1, e, obj2) {
      $("#error").html('An error was encountered.');
      $("#error").show();
      $.scrollTo("#error");
      $(".loading-sex").hide();
    }
  });
};

$(document).ready(function() {

  //Popup
    $('A[rel="external"]').click( function() {
        window.open( $(this).attr('href'),this,'width=700,height=380' );
        return false;
    });

  //AJAX forms
  $('#ajaxform #submit').live("click", function() {
    var data = $('#ajaxform').serialize();
    var url = $('#ajaxform').attr('action') + 'ajax/';
    var method = $('#ajaxform').attr('method');

    $("#submit").attr("disabled", true);
    $(".loading-submit").show();
    $("#error").fadeOut();

    $.ajax({
      url: url,
      type: method,
      data: data,
      dataType: 'json',
      success: function(data) {
        if (typeof data.error != 'undefined') {
          $("#error").html(data.error);
          $("#error").fadeIn();
          $.scrollTo("#error");
          $("#submit").attr("disabled", false);
        } else {
          $("#message").html(data.message);
          $("#message").fadeIn();
          $.scrollTo("#message");
          $('#ajaxform').fadeOut(1500);
        }
        $(".loading-submit").hide();
      },
      error: function(obj1, e, obj2) {
        $("#error").html('An error was encountered.');
        $("#error").fadeIn();
        $.scrollTo("#error");
        $(".loading-submit").hide();
        $("#submit").attr("disabled", false);
      }
    });

    return false;
  });

  //Delete member
    $('a.delete').click(function() {
        var msg = 'Are you sure?';
        if (confirm(msg)) {
            var element = $(this);
            var noteid = element.attr("id");
            var info = noteid;

            $.ajax({
                type: "GET",
        data: '',
        dataType: 'json',
                url: "/team/detail/delete/"+info+"/ajax/",
        success: function(data) {
          if (typeof data.error != 'undefined') {
            $("#error").html(data.error);
            $("#error").fadeIn();
            $.scrollTo("#error");
          } else {
                        $('.child-'+noteid).fadeOut(3000);
                        $(element).parents('tr').children('td').css('background-color', '#FF0000');
                        $(element).parents('tr').fadeOut(3000);
          }
        },
                error: function(obj1, e, obj2) {
            $("#error").html('An error was encountered.');
            $("#error").fadeIn();
            $.scrollTo("#error");
                    }
             });
        }
            return false;
    });

  //Delete admin team
    $('a.team_delete').click(function() {
        var msg = 'Are you sure?';

        if (confirm(msg)) {
            var element = $(this);
            var noteid = element.attr("id");
            var info = noteid;

            $.ajax({
                type: "GET",
        data: '',
        dataType: 'json',
                url: "/admin/detail/delete/"+info+"/ajax/",
        success: function(data) {
          if (typeof data.error != 'undefined') {
            $("#error").html(data.error);
            $("#error").fadeIn();
            $.scrollTo("#error");
          } else {
                        $(element).parents('tr').children('td').css('background-color', '#FF0000');
                        $(element).parents('tr').fadeOut(3000);
          }
        },
                error: function(obj1, e, obj2) {
            $("#error").html('An error was encountered.');
            $("#error").fadeIn();
            $.scrollTo("#error");
                    }
             });
        }
            return false;
    });

  //Delete black list
    $('a.blacklist_delete').click(function() {
        var msg = 'Are you sure?';

        if (confirm(msg)) {
            var element = $(this);
            var noteid = element.attr("id");
            var info = noteid;

            $.ajax({
                type: "GET",
        data: '',
        dataType: 'json',
                url: "/admin/detail/blacklist_delete/"+info+"/ajax/",
        success: function(data) {
          if (typeof data.error != 'undefined') {
            $("#error").html(data.error);
            $("#error").fadeIn();
            $.scrollTo("#error");
          } else {
                        $(element).parents('tr').children('td').css('background-color', '#FF0000');
                        $(element).parents('tr').fadeOut(3000);
          }
        },
                error: function(obj1, e, obj2) {
            $("#error").html('An error was encountered.');
            $("#error").fadeIn();
            $.scrollTo("#error");
                    }
             });
        }
            return false;
    });

  //Delete coupon
    $('a.coupon_delete').click(function() {
        var msg = 'Are you sure?';

        if (confirm(msg)) {
            var element = $(this);
            var noteid = element.attr("id");
            var info = noteid;

            $.ajax({
                type: "GET",
        data: '',
        dataType: 'json',
                url: "/admin/detail/coupon_delete/"+info+"/ajax/",
        success: function(data) {
          if (typeof data.error != 'undefined') {
            $("#error").html(data.error);
            $("#error").fadeIn();
            $.scrollTo("#error");
          } else {
                        $(element).parents('tr').children('td').css('background-color', '#FF0000');
                        $(element).parents('tr').fadeOut(3000);
          }
        },
                error: function(obj1, e, obj2) {
            $("#error").html('An error was encountered.');
            $("#error").fadeIn();
            $.scrollTo("#error");
                    }
             });
        }
            return false;
    });

  //Delete Organization's Assignemnt
    $('a.assignment_delete').click(function() {
        var msg = 'Are you sure?';

        if (confirm(msg)) {
            var element = $(this);
            var noteid = element.attr("id");
            var info = noteid;

            $.ajax({
                type: "GET",
        data: '',
        dataType: 'json',
                url: "/admin/volunteer/unlink/"+info+"/",
        success: function(data) {
          if (typeof data.error != 'undefined') {
            $("#error").html(data.error);
            $("#error").fadeIn();
            $.scrollTo("#error");
          } else {
                        $(element).parents('tr').children('td').css('background-color', '#FF0000');
                        $(element).parents('tr').fadeOut(3000);
          }
        },
                error: function(obj1, e, obj2) {
            $("#error").html('An error was encountered.');
            $("#error").fadeIn();
            $.scrollTo("#error");
                    }
             });
        }
            return false;
    });

    // Unassign Volunteer
    $('a.volunteer_unassign').click(function() {
        var msg = 'Are you sure?';

        if (confirm(msg)) {
            var element = $(this);
            var noteid = element.attr("id");
            var info = noteid;

            $.ajax({
                type: "GET",
                data: '',
                dataType: 'json',
                        url: "/organization/index/unassign/"+info+"/",
                success: function(data) {
                  if (typeof data.error != 'undefined') {
                    $("#error").html(data.error);
                    $("#error").fadeIn();
                    $.scrollTo("#error");
                  } else {
                                $(element).parents('tr').children('td').css('background-color', '#FF0000');
                                $(element).parents('tr').fadeOut(3000);
                  }
                },
                error: function(obj1, e, obj2) {
                    $("#error").html('An error was encountered.');
                    $("#error").fadeIn();
                    $.scrollTo("#error");
                }
            });
        }
            return false;
    });

    // Unassign Volunteer
        $('a.volunteer_unassign2').click(function() {
            var msg = 'Are you sure?';

            if (confirm(msg)) {
                var element = $(this);
                var noteid = element.attr("id");
                var info = noteid;

                $.ajax({
                    type: "GET",
                       data: '',
                       dataType: 'json',
                       url: "/team/volunteer//unassign/"+info+"/",
                       success: function(data) {
                           if (typeof data.error != 'undefined') {
                               $("#error").html(data.error);
                               $("#error").fadeIn();
                               $.scrollTo("#error");
                           } else {
                               $("#message").html(data.message);
                               $("#message").fadeIn();
                               $.scrollTo("#message");
                           }
                       },
                                                  error: function(obj1, e, obj2) {
                                                      $("#error").html('An error was encountered.');
                                                      $("#error").fadeIn();
                                                      $.scrollTo("#error");
                                                  }
                });
            }
                        return false;
        });

    // Delete Volunteer
    $('a.volunteer_delete').click(function() {
        var msg = 'Are you sure?';

        if (confirm(msg)) {
            var element = $(this);
            var noteid = element.attr("id");
            var info = noteid;

            $.ajax({
                type: "GET",
                data: '',
                dataType: 'json',
                        url: "/organization/index/delete_volunteer/"+info+"/",
                success: function(data) {
                  if (typeof data.error != 'undefined') {
                    $("#error").html(data.error);
                    $("#error").fadeIn();
                    $.scrollTo("#error");
                  } else {
                                $(element).parents('tr').children('td').css('background-color', '#FF0000');
                                $(element).parents('tr').fadeOut(3000);
                  }
                },
                error: function(obj1, e, obj2) {
                    $("#error").html('An error was encountered.');
                    $("#error").fadeIn();
                    $.scrollTo("#error");
                }
            });
        }
            return false;
    });

    // Delete Organization
        $('a.organization_delete').click(function() {
            var msg = 'Are you sure? Keep in mind this will delete all the organization\'s associated data (Volunteers, Assignments, etc)';

            if (confirm(msg)) {
                var element = $(this);
                var noteid = element.attr("id");
                var info = noteid;

                $.ajax({
                    type: "GET",
                       data: '',
                       dataType: 'json',
                       url: "/admin/volunteer/delete_organization/"+info+"/",
                       success: function(data) {
                           if (typeof data.error != 'undefined') {
                               $("#error").html(data.error);
                               $("#error").fadeIn();
                               $.scrollTo("#error");
                           } else {
                               $(element).parents('tr').children('td').css('background-color', '#FF0000');
                               $(element).parents('tr').fadeOut(3000);
                           }
                       },
                                                  error: function(obj1, e, obj2) {
                                                      $("#error").html('An error was encountered.');
                                                      $("#error").fadeIn();
                                                      $.scrollTo("#error");
                                                  }
                });
            }
                        return false;
        });

    // Delete Exchange
        $('a.exchange_delete').click(function() {
            var msg = 'Are you sure? Keep in mind this will delete all the Exchange\'s associated assignments.';

            if (confirm(msg)) {
                var element = $(this);
                var noteid = element.attr("id");
                var info = noteid;

                $.ajax({
                    type: "GET",
                       data: '',
                       dataType: 'json',
                       url: "/admin/volunteer/delete_exchange/"+info+"/",
                       success: function(data) {
                           if (typeof data.error != 'undefined') {
                               $("#error").html(data.error);
                               $("#error").fadeIn();
                               $.scrollTo("#error");
                           } else {
                               $(element).parents('tr').children('td').css('background-color', '#FF0000');
                               $(element).parents('tr').fadeOut(3000);
                           }
                       },
                                                  error: function(obj1, e, obj2) {
                                                      $("#error").html('An error was encountered.');
                                                      $("#error").fadeIn();
                                                      $.scrollTo("#error");
                                                  }
                });
            }
                        return false;
        });

    //View member
    $('a.view').click(function() {
        $('.child-'+this.id).fadeToggle(500);
        $.scrollTo('#parent-'+this.id);
    });

    //Edit member
    $('a.edit').click(function() {
      var id = this.id
      var url = '/team/detail/edit_member/'+id;

      $(".loading-edit-"+id).show();

    $.ajax({
      url: url,
      type: 'get',
      dataType: 'json',
      success: function(data) {
        if (typeof data.error != 'undefined') {
          $("#error").html(data.error);
          $("#error").fadeIn();
          $.scrollTo("#error");
        } else {
          $("#edit-"+id).html(data.message);
          $("#edit-"+id).fadeToggle(500);
          $.scrollTo('#parent-'+id);
        }
        $(".loading-edit-"+id).hide();
      },
      error: function(obj1, e, obj2) {
        $("#error").html('An error was encountered.');
        $("#error").fadeIn();
        $.scrollTo("#error");
        $(".loading-edit-"+id).hide();
      }
    });
    });

    //View all members
    $('a.view_all').click(function() {
        $("tr[class^='child']").each(function() {
      $(this).fadeToggle(500);
    });
    });

    // Show upload field
    $('a.import').click(function() {
        $('span#'+this.id).fadeToggle(500);
        return false;
    });

});
