
/**
 * JSON to store how we check various page elements.
 */
var inputFields = {

  // form on play.php for registering a web user
  "newsletter" : {

    "carrier" : {

      "type" : "select",
      "description" : "Your Carrier",
      "confirm" : false,
      "empty" : false

    },

    "cellnum" : {

      "type" : "text",
      "description" : "Your Mobile Number",
      "confirm" : false,
      "empty" : false

    },

    "email" : {

      "type" : "email",
      "description" : "Your Email Address",
      "confirm" : false,
      "empty" : false

    },

    "optin" : {

      "type" : "checkbox",
      "description" : "Over 13 Years Old",
      "confirm" : false,
      "empty" : true,
      "mandatory" : true

    }

  },
  
  "mailinglist" : {
    
      "email" : {
  
        "type" : "email",
        "description" : "Your Email Address",
        "confirm" : false,
        "empty" : false
  
      }
    
  }

};

/**
 * Check to see if an email address is valid (stolen code).
 */
function validEmail(eAddr) {

  var result = false;
  var ndxAt = ndxDot =  0;

  var ndxAt = eAddr.indexOf("@");
  var ndxDot = eAddr.indexOf(".");
  var ndxDot2 = eAddr.lastIndexOf(".");

  if ((ndxDot < 0) || (ndxAt < 0)) {

    alert("Your email address lacks a '.' or '@'.\n\nThe valid format is 'you@domain.suffix.'");

  } else if ( (ndxDot2 - 3) <= ndxAt) {

    alert("You may be missing your domain name.\n\nThe format is 'you@dom.suf'");

  } else {

    result = true;

  }

  return result;

}

/**
 * Check input fields.
 */
function checkInput(prefix,form) {

  var errorMsg = "";
  var returnValue = true;

  for (var x in inputFields[form]) {

    // don't append prefix if it's empty
    var thisElementId = (prefix == "" ? x : prefix + "_" + x);

    var thisElement = document.getElementById(thisElementId);

    // check to see if we have a confirm field
    if (inputFields[form][x].confirm) {

      // don't append prefix if it's empty
      var thisElementConfirmId = (prefix == "" ? "confirm_" + x : prefix + "_confirm_" + x);

      var thisElementConfirm = document.getElementById(thisElementConfirmId);

      if (thisElement.value != thisElementConfirm.value) {

        errorMsg = errorMsg + "The values entered for " + inputFields[form][x].description + " do not match.\n";

        returnValue = false;

      }

    }

    // if it's text, check to make sure it's not empty
    if (!inputFields[form][x].empty && thisElement.value == "") {

      errorMsg = errorMsg + "You must enter a value for " + inputFields[form][x].description + "\n";

      returnValue = false;

    }

    // do any special processing
    switch (inputFields[form][x].type) {

      case "email":

        if (!validEmail(thisElement.value)) {

          returnValue = false;

        }

        break;

      case "checkbox":

        if (inputFields[form][x].mandatory && !thisElement.checked) {

          errorMsg = errorMsg + "You must check the " + inputFields[form][x].description + " field.\n";

          returnValue = false;

        }

        break;

      case "select":

        if (!inputFields[form][x].empty && thisElement.selectedIndex == 0) {

          errorMsg = errorMsg + "You must select a value for " + inputFields[form][x].description + ".\n";

          returnValue = false;

        }

        break;

      default:

        break;

    }

  }

  // display any errors that we've accumulated
  if (errorMsg != "") {

    alert(errorMsg);

  }

  return returnValue;

}

/**
 * Retrieve the passable value of an input (added unnecessary break
 * statements just for clarity).
 */
function getValue(prefix,form,index) {

  var realId = (prefix == "" ? index : prefix + "_" + index);

  switch (inputFields[form][index].type) {

    case "text":
    case "email":

      return document.getElementById(realId).value;

      break;

    case "select":

      var js_select = document.getElementById(realId);

      return js_select.options[js_select.selectedIndex].value;

      break;

    case "checkbox":

      if (document.getElementById(realId).checked) {
        return 1;
      } else {
        return 0;
      }

      break;

    default:

      return "";

      break;

  }

}
