
// Variables need to be set in this function
  function loadRegVars() {
    // URL where the server request will be sent to
    url = 'https://secure2.convio.net/dpa/site/CRConsAPI';

    // Registration html form
    reg_form = getEl('reg-form');

    // Box where error messages are displayed
    msg_box = getEl('reg-msg');

    // Rest of registration form (other than email)
    reg_long = getEl('reg-long');

    // Email form field
    email_field = getEl('email');

    // Convio's internal ID for each user
    cons_id = getEl('cons_id');
  }

  var reg_form, msg_box, reg_long, email_field, method_field, cons_id, json, url;


  /* Short for GET ELEMENT
   * If arg is a string, returns the element with id == arg
   * If arg is an element, returns arg */
  function getEl(arg) {
    if (typeof arg == 'string') {
      return document.getElementById(arg);
    } else {
      return arg;
    }
  }


  // Sets the json variable, used in parsing the callback from the server
  function setJSON(str) {
    str = 'json = ' + str;
    eval(str);  // only real way in converting string --> json (or js literal)
  }


  /* Sets the content of a message box, used for error messages.
   * By default, the message box has id == 'reg-msg' */
  function setMsg(str) {
    msg_box.innerHTML = str;
  }

  function clearMsg() {
    msg_box.innerHTML = '';
  }


  // Makes an Ajax call to the server
  function regSubmit() {
    // Validation for email field
    if (!email_field.value && !cons_id.value) {
      setMsg('Error: Must enter an email address!');
      return;
    }

    new Ajax.Request(url, {
      parameters: Form.serialize(reg_form),
      onSuccess: function(trans) {
        setJSON(trans.responseText);
        var resp = json.createOrUpdateConsResponse || json.createConsResponse;
        clearMsg();

        // If creating (vs updating)
        if (!cons_id.value) {
          loadUpdateForm(resp);
        }
      },
      onFailure: function(trans) {
        setJSON(trans.responseText);
        setMsg(json.errorResponse.message);
      }
    });
  }


  // Changes the state of the form from creating to updating
  function loadUpdateForm(resp) {
    // cons_id is Convio's id for the new user, this sets it in the form
    cons_id.value = resp.cons_id;
  
    // Makes the email field read-only
    email_field.disabled = 'true';
    email_field.className = 'read-only';
  
    // Show long form and change method from 'create' to 'update'
    reg_long.style.display = 'block';
  }


  /* Cross-browser event handling object
   * If using a Javascript library with event handling, the cvEvent
   * object and the call below it can be replaced. The Prototype version
   * of the window.onload call is commented out below. */
  var cvEvent = {
    add: function(obj,type,fn) {
      if (obj.attachEvent) {
        obj['e'+type+fn] = fn;
        obj[type+fn] = function() { obj['e'+type+fn](window.event); }
        obj.attachEvent('on'+type,obj[type+fn]);
      } else
      obj.addEventListener(type,fn,false);
    }
  }

        // Sets variables after the html is loaded
  cvEvent.add(window, 'load', loadRegVars);  

  // Prototype version
  // Event.observe(window, 'load', loadRegVars);

