// AjaxTinySample 0.02
// requires prototype.js
(function() {
  Event.observe(window, 'load', function() {
    new AjaxSample();
  });
})();
var AjaxSample = Class.create();
AjaxSample.prototype = {
  initialize: function() {
    Event.observe("registButton", "click", this.query);
    Event.observe("editButton", "click", this.edit);
  },
  query: function() {
  // 結果表示領域の取得
    var r = $("bindRss");
  // 処理中
    r.className = 'block';
    Element.update(r, 'RSSを作成しています...<img src="/tumblr/binder/img/loading.gif" />');
  // request
    new Ajax.Request("register.php",
      {
        'method': 'POST',
        'parameters': 'users='+$('registUsers').value+'&type='+$('registType').value,
      // 結果取得が完了したら書き換え
        onComplete: function(httpObj) {
          if(httpObj.responseText.match(/^http:.+\.xml$/)) {
            var a = document.createElement('a');
            a.setAttribute('href', httpObj.responseText);
            a.setAttribute('target', '_blank');
            a.appendChild(document.createTextNode(httpObj.responseText));
            Element.update(r, a);
          } else {
            Element.update(r, httpObj.responseText);
          }
        }
      });
  },
  edit: function() {
  // 結果表示領域の取得
    var r = $("bindRss");
  // 処理中
    r.className = 'block';
    Element.update(r, 'RSSに含まれるIDを取得しています...<img src="/images/loading.gif" />');
  // IDの取得
    var str = $('editRss').value.match(/http:\/\/b.umbls.com\/(.+?)(\/.+?)?\.xml/);
    if(!str) {
      Element.update(r, 'RSSのURLが正しくありません');
      return;
    }
    var id = str[1];
  // request
    new Ajax.Request("edit.php",
      {
        'method': 'POST',
        'parameters': 'id='+id,
      // 結果取得が完了したら書き換え
        onComplete: function(httpObj) {
          r.className = 'hidden';
          Element.update($('registUsers'), httpObj.responseText);
        }
      });


  }
}