function CreateAccount(url) {
  this.url = url;

  if (typeof CreateAccount._initialized == "undefined") {

    CreateAccount.prototype.init = function (parentid) {
      var parentEl = document.getElementById(parentid);

      if (!parentEl) {
        parentEl = document;
      }

      var myElmts = parentEl.getElementsByTagName('input');

      for (i=0;i<myElmts.length;i++) {
        myElmts[i].pageObj = this;
        addEvent(myElmts[i], 'blur', this.checkForm, false);
        addEvent(myElmts[i], 'keypress', this.handleField, false);
      }

      var checklink = document.getElementById("logincheck");
      checklink.pageObj = this;
      addEvent(checklink, 'click', this.checkAvailability, false);
    }

    CreateAccount.prototype.checkAvailability = function (e) {
      var el = null;
      el = getTarget(e);

      if (!el) {
        return;
      }
      var pageObj = (this.pageObj) ? this.pageObj: el.pageObj;

      var loginField = document.getElementById("login");
      if (!loginField.value) {
        var msg_field = document.getElementById("available_msg");
        msg_field.innerHTML = "Please enter a username";
        return;
      }

      url = pageObj.url+"&ajax=1&logincheck=1&login="+loginField.value;

      //alert(url);
      myContent = new AjaxContent(url,"available_msg");
      myContent.getContent();

    }

    CreateAccount.prototype.handleField = function (e) {
      var el = null;
      el = getTarget(e);

      if (!el) {
        return;
      }
      var pageObj = (this.pageObj) ? this.pageObj: el.pageObj;

      clearErrorMsg(e);
      if (isEnterPressed(e)) {
        if (el.name == "login") {
          stopBubble(e);
          pageObj.checkAvailability(e);
        }
      }
    }

    CreateAccount.prototype.checkForm = function (e) {
      var el = null;
      el = getTarget(e);

      if (!el) {
        return;
      }
      var pageObj = (this.pageObj) ? this.pageObj: el.pageObj;

      switch (el.id) {
        case "login":
          if (isEmpty(el.value)) {
            showErrorMsg(el,"Please enter a username");
          } else {
            pageObj.checkAvailability(el);
          }
          break;
        case "pass1":
          if (isEmpty(el.value)) {
            showErrorMsg(el,"Please enter a password");
          }
          break;
        case "pass2":
          if (!pageObj.checkPass(el)) {
            showErrorMsg(el,"Passwords do not match");
          }
          break;
        case "email":
          if (isEmpty(el.value)) {
            showErrorMsg(el,"Please enter an email");
          } else if (!isEmail(el.value)) {
            showErrorMsg(el,"Email format invalid");
          }
          break;
      }
    }

    CreateAccount.prototype.checkPass = function (el) {
      var passField = document.getElementById("pass1");
      return (passField.value == el.value);
    }

    CreateAccount._initialized = true;
  }
}

function initForm_obj() {
  form_obj.init('maincontent');
}


