var <%= name.camelize %> = function(<%= fields_str %>){
<% fields.each do |field| %>
<% if field.key?("relationship") && field["relationship"] == true %>
  <%= "this." + field["name"].camelize + "Id = " + field["name"] + ";" %>
<% else %>
  <%= "this." + field["name"].camelize + " = " + field["name"] + ";" %>
<% end %>
<% end %>
  this.Id = 0;
  this.CreatedAt = Date.now();
  this.UpdatedAt = Date.now();

  this.save = function(path, successHandler, failureHandler){
    var ret = this
    var xhr = new XMLHttpRequest();
    if(this.Id == 0){
      xhr.open("POST", <%= "path + \"/" + name.pluralize + "/\"" %>, true);
    } else{
      xhr.open("PATCH", <%= "path + \"/" + name.pluralize + "/\"+"%>this.Id);
    }

    xhr.setRequestHeader("Content-type", "application/json");

    xhr.onreadystatechange = function(){
      if(xhr.readyState == 4){
        if(xhr.status == 200){
          var result = JSON.parse(xhr.responseText);
          if(result.Id !== undefined){
            ret.Id = result.Id;
          }

          if(successHandler !== undefined) {
            successHandler(xhr, ret, result);
          }
        } else{
          if(failureHandler !== undefined) {
            failureHandler(xhr, ret, result);
          }
        }
      }
    };

    var data = JSON.stringify(this);
    xhr.send(data);
  };

  this.delete = function(path, successHandler, failureHandler){
    if(this.Id == 0){
      return; //never saved to database, don't have to do anything
    }

    var xhr = new XMLHttpRequest();
    xhr.open("DELETE", <%= "path + \"/" + name.pluralize + "/\"+" %>this.Id, true);
    xhr.setRequestHeader("Content-type", "application/json");
    xhr.onreadystatechange = function() {
      if(xhr.readyState == 4){
        if(xhr.status == 200){
          if(successHandler !== undefined) {
            successHandler(xhr);
          }
        } else{
          if(failureHandler !== undefined) {
            failureHandler(xhr);
          }
        }
      }
    };

    xhr.send(null);
  };

<% fields.each do |field| %>
  <% if field.key?("relationship") && field["relationship"] == true%>
  this.get<%= field["name"].camelize %> = function(path, successHandler, failureHandler){
    var ret = new <%= field["name"].camelize %>();
    <%= field["name"].camelize %>.get(<%= "this." + field["name"].camelize + "Id" %>, ret, path, successHandler, failureHandler);
    return ret;
  };
  <% end %>
<% end %>
};

<%= name.camelize %>.get = function(id, ret, path, successHandler, failureHandler){
  var xhr = new XMLHttpRequest();

  xhr.onreadystatechange = function() {
    if(xhr.readyState == 4){
      if(xhr.status == 200){
        var fields = ["Id", "CreatedAt", "UpdatedAt", <%= fields_str_quoted %>];
        var result = JSON.parse(xhr.responseText);

        for(var field in result){
          if(fields.indexOf(field) !== -1){
            if(result[field].Value !== undefined) {
              ret[field] = result[field].Value;
            } else {
              ret[field] = result[field];
            }
          }
        }

        if(successHandler !== undefined){
          successHandler(xhr, ret);
        }
      } else{
        if (failureHandler !== undefined) {
          failureHandler(xhr, ret);
        }
      }
    }
  };

  xhr.open("GET", <%= "path + \"/" + name.pluralize + "/\"+" %>id, true);
  xhr.send(null);
};

<%= name.camelize %>.getAll = function(arr, path, successHandler, failureHandler){
  var xhr = new XMLHttpRequest();

  xhr.onreadystatechange = function() {
    if(xhr.readyState == 4){
      if(xhr.status == 200){
        var fields = ["Id", "CreatedAt", "UpdatedAt", <%= fields_str_quoted %>];
        var result = JSON.parse(xhr.responseText);

        for(var i = 0; i < result.length; i++){
          tmp = new <%= name.camelize %>();
          obj = result[i];
          for(var field in obj){
            if(fields.indexOf(field) !== -1){
              if (obj[field].Value !== undefined) {
                tmp[field] = obj[field].Value;
              }
              else {
                tmp[field] = obj[field];
              }
            }
          }

          arr.push(tmp);
        }

        if(successHandler !== undefined) {
          successHandler(xhr, ret);
        }
      } else{
        if(failureHandler !== undefined) {
          failureHandler(xhr, ret);
        }
      }
    }
  };

  xhr.open("GET", <%="path + \"/" + name.pluralize + "\"" %>, true);
  xhr.send(null);
};
