var errorMessages = new Array();

/**
 * Verify the registration data before submitting the form.
 */
function verifyForm()
  {
    var msg;

    var feeFaculty, feeStudent, feeSocamatyc, feeTeacher, feeHsStudent, feeDinner, feeTotal;

    feeFaculty   = document.getElementById("feeFacultyId");
    feeStudent   = document.getElementById("feeStudentId");
    
    feeTeacher   = document.getElementById("feeTeacherId");
    feeHsStudent = document.getElementById("feeHsStudentId");
    feeDinner    = document.getElementById("feeDinnerId");
    feeTotal     = document.getElementById("feeTotalId");

    msg  = "Invalid registration data.  Please correct and re-submit.\n";
    msg += "___________________________________________________________________\n\n";

    // clear the array of error messages
    errorMessages.length = 0;

    var firstName, lastName, state, phoneNum, emailAddr, occupation, numAttending;

    firstName  = document.getElementById("firstNameId");
    lastName   = document.getElementById("lastNameId");
    state      = document.getElementById("stateId");
    phoneNum   = document.getElementById("phoneNumId");
    emailAddr  = document.getElementById("emailAddrId");
    occupation = document.getElementById("occupationId");

    validateRequiredStringField(firstName,  "- Please enter your first name.\n\n");
    validateRequiredStringField(lastName,   "- Please enter your last name.\n\n");
    validateRequiredStringField(state,      "- Please select a state.\n\n");
    validateRequiredStringField(occupation, "- Please select your occupation.\n\n");

    validateAtLeastOneField(phoneNum, emailAddr, "- Please enter either phone or email address.\n\n");

    numAttending = document.getElementById("numAttendingId");
    validateOptionalIntField(numAttending, 0, 9, "- Number attending the social event must be an integer between 0 and 9.\n\n");

    if (errorMessages.length > 0)
      {
        for (i = 0;  i < errorMessages.length;  ++i)
            msg += errorMessages[i];

        alert(msg);
        return false;
      }
    else
      {
        feeFaculty.disabled   = false;
        feeStudent.disabled   = false;
        
        feeTeacher.disabled   = false;
        feeHsStudent.disabled = false;
        feeDinner.disabled    = false;
        feeTotal.disabled     = false;

        return true;
      }
  }


/**
 * Check for the presence of a required string field.
 */
function validateRequiredStringField(field, errorMsg)
  {
    if (field.value == "")
        errorMessages.push(errorMsg);
  }


/**
 * Check for the presence of at least one of these string fields.
 */
function validateAtLeastOneField(field1, field2, errorMsg)
  {
    if (field1.value == "" && field2.value == "")
        errorMessages.push(errorMsg);
  }


/**
 * Check for a valid integer in an optional int field.
 */
function validateOptionalIntField(field, lowerBound, upperBound, errorMsg)
  {
    var fieldAmt;

    if (field.value != "")
      {
        fieldAmt = parseInt(field.value);

        if (isNaN(fieldAmt) || fieldAmt < lowerBound || fieldAmt > upperBound)
            errorMessages.push(errorMsg);
      }
  }


// for calculating registration fees
function calculateFees()
  {
    var feeFacultyBtn, feeStudentBtn, feeTeacherBtn, feeHsStudentBtn;
    var numAttending, numAttendingAmt,numAttendingStudent,numAttendingStudentAmt;;
    var feeFaculty, feeStudent, feeTeacher, feeHsStudent, feeDinner, feeTotal;
    var feeFacultyAmt, feeStudentAmt, feeTeacherAmt, feeHsStudentAmd, feeDinnerAmt, feeTotalAmt;
    var dateNow, dateFeeChange, isEarlyRegistration;

    dateNow = new Date();
    dateFeeChange = new Date("Mar 02, 2010");
    isEarlyRegistration = ((dateFeeChange - dateNow) >= 0);

    feeFacultyBtn   = document.getElementById("feeFacultyBtnId");
    feeStudentBtn   = document.getElementById("feeStudentBtnId");
    feeTeacherBtn   = document.getElementById("feeTeacherBtnId");
    feeHsStudentBtn = document.getElementById("feeHsStudentBtnId");
    
    numAttending = document.getElementById("numAttendingId");
	numAttendingStudent = document.getElementById("numAttendingStudentId");
	

    feeFaculty   = document.getElementById("feeFacultyId");
    feeStudent   = document.getElementById("feeStudentId");
    
    feeTeacher   = document.getElementById("feeTeacherId");
    feeHsStudent = document.getElementById("feeHsStudentId");
    feeDinner    = document.getElementById("feeDinnerId");
    feeTotal     = document.getElementById("feeTotalId");

    // disable all fee fields
    feeFaculty.disabled   = true;
    feeStudent.disabled   = true;
    
    feeTeacher.disabled   = true;
    feeHsStudent.disabled = true;
    feeDinner.disabled    = true
    feeTotal.disabled     = true;

    // initialize registration fees
    feeFaculty.value   = "";
    feeStudent.value   = "";
   
    feeTeacher.value   = "";
    feeHsStudent.value = "";
    feeDinner.value    = "";
    feeTotal.value     = "";

    if (isEarlyRegistration)
      {
        if (feeFacultyBtn.checked)
            feeFaculty.value   = 25;
        else if (feeStudentBtn.checked)
            feeStudent.value   = 10;
        else if (feeTeacherBtn.checked)
            feeTeacher.value   = 15;
        else if (feeHsStudentBtn.checked)
            feeHsStudent.value = 0;
      }
    else
      {
        if (feeFacultyBtn.checked)
            feeFaculty.value   = 35;
        else if (feeStudentBtn.checked)
            feeStudent.value   = 15;
        else if (feeTeacherBtn.checked)
            feeTeacher.value   = 20;
        else if (feeHsStudentBtn.checked)
            feeHsStudent.value = 0;
      }

   

    // calculate amount for dinner
    feeDinnerAmt = 0;  
    numAttendingAmt = parseInt(numAttending.value);
    if (isNaN(numAttendingAmt) || numAttendingAmt == 0)
        {feeDinnerAmt = 0;}
    else
        {feeDinnerAmt = 24*numAttendingAmt;}
		numAttendingStudentAmt = parseInt(numAttendingStudent.value);
    if (isNaN(numAttendingStudentAmt) || numAttendingStudentAmt == 0)
        {feeDinnerAmt = feeDinnerAmt;}
    else
        {feeDinnerAmt =feeDinnerAmt+18*numAttendingStudentAmt;}
    
    if (feeDinnerAmt > 0)
        feeDinner.value = feeDinnerAmt;

    

    // calculate the total amount

    feeFacultyAmt = parseInt(feeFaculty.value);
    if (isNaN(feeFacultyAmt))
        feeFacultyAmt = 0;

    feeStudentAmt = parseInt(feeStudent.value);
    if (isNaN(feeStudentAmt))
        feeStudentAmt = 0;

    feeTeacherAmt = parseInt(feeTeacher.value);
    if (isNaN(feeTeacherAmt))
        feeTeacherAmt = 0;

    feeHsStudentAmt = parseInt(feeHsStudent.value);
    if (isNaN(feeHsStudentAmt))
        feeHsStudentAmt = 0;

    feeTotal.value = feeFacultyAmt + feeStudentAmt   
                   + feeTeacherAmt + feeHsStudentAmt + feeDinnerAmt;
  }

