/* forms.js
   
   This file is part of arena.

   arena is free software: you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the
   Free Software Foundation; either version 3 of the License or, at your
   option, any later version.

   arena is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABLITY
   or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
   License for more details.

   You should have received a copy of the GNU General Public License along
   with arena.  If not, see <http://www.gnu.org/licenses/>.
*/

/* This file defines a utility class for working with forms on an arena page.
*/

// Form namespace
var Form = {};

// Encodes form data into a string suitable for use with an AJAX request
Form.encode = function(thisForm) {
  var submitContent = '';
  var element;
  var lastElementName = '';

  // Loop through every element in the form
  for (i = 0; i < thisForm.elements.length; i++) {
    element = thisForm.elements[i];

    switch (element.type) {
      // Text fields, hidden form elements
      case 'text':
      case 'hidden':
      case 'password':
      case 'textarea':
      case 'select-one':
        submitContent += element.name + '=' + escape(element.value) + '&'
        break;

      // Radio buttons
      case 'radio':
        if (element.checked) {
          submitContent += element.name + '=' + escape(element.value) + '&'
        }
        break;

      // Checkboxes
      case 'checkbox':
        if (element.checked) {
          // Continuing multiple, same-name checkboxes
          if (element.name == lastElementName) {
            // Strip of end ampersand if there is one
            if (submitContent.lastIndexOf('&') == (submitContent.length - 1)) {
              submitContent = submitContent.substr(0, (submitContent.length - 1));
            }
            // Append value as comma-delimited string
            submitContent += ',' + escape(element.value);
          }
          else {
            submitContent += element.name + '=' + escape(element.value);
          }
          submitContent += '&';
          lastElementName = element.name;
        }
        break;
    }
  }

  // Remove trailing separator
  submitContent = submitContent.substr(0, (submitContent.length - 1));
  return submitContent;
}

// Validates the specified form
Form.valid = function(thisForm) {
  // If no required fields are specified, return true by default
  if ((!thisForm.required) || (thisForm.required.value == "")) {
    return true;
  }

  // Retrieve the list of all required fields
  var requiredFields = thisForm.required.value.split(",");

  // Loop through the list of all required fields
  for (var i=0; i < requiredFields.length; i++) {
    // Determine the type of the required field
    fieldNameType = requiredFields[i].split("|");
    fieldName = fieldNameType[0];
    fieldType = fieldNameType[1];

    switch(fieldType) {
      // Verify the field is a valid e-mail address
      case "email":
        // Look for the @ symbol and at least one period following it
        emailDest = thisForm.elements[fieldName].value.indexOf("@");
        emailDomain = thisForm.elements[fieldName].value.lastIndexOf(".");
        if ((emailDest < 0) || (emailDomain < 0) || (emailDomain <= emailDest)) {
          alert("Please enter a valid e-mail address before continuing.");
          return false;
        }
        break;

      // Verify a password field
      case "password":
        // Ensure the field is not empty
        if (thisForm.elements[fieldName].value == "") {
          alert("Please enter a password before continuing.");
          return false;
        }
        break;

      default:
        // Ensure the field is not empty, at least
        if (thisForm.elements[fieldName].value == "") {
          alert("Please enter " + fieldType + " before continuing.");
          return false;
        }
    }
  }
  return true;
}
