/**
    Document   : step1.js
    Created on : Jun 10, 2009
    Author     : Mike Stephens <MikeyPHP>
    Description: Functions for the Step 1 form
*/

/**
 *Function to see the correct display style on applicant details
 *@param field Object to use
 *@return
 */
function ApplicantNo(field)
{
    // Get each applicant form
    var app = document.getElementById("app");
    var app2 = document.getElementById("app2");

    if(field.value == "") // Please Select
    {
      app.style.display = "none";
      app2.style.display = "none";
    }
    else if(field.value == "1") // 1 Applicant
    {
      app.style.display = "block";
      app2.style.display = "none";
    }
    else if(field.value == "2")  // 2 Applicants
    {
      app.style.display = "block";
      app2.style.display = "block";
    }
    else if(field.value == "3") // 3 Applicants
    {
      app.style.display = "none";
      app2.style.display = "none";
      alert("Please contact us if you wish to add more than 3 applicants.");
    }

}

/**
 * Simple validation for the form. Calls external functions in functions.js
 * @param
 * @return boolean
 */
function validateForm()
{

    // Get form as an object
    var form = document.getElementById("frmStep1");

    // Array of fileds to validate in the format <field|type|des>
    var fields = new Array(
                           "app_no|sel|Number of applicants required",
                           "app_title|sel|Applicant Title",
                           "app_name|txt|Applicant Name",
                           "app_address|txt|Applicant Address",
                           "app_postcode|txt|Applicant Post Code",
                           "app_dob|date|Applicant Date Of Birth",
                           "app_tel|num|Applicant Telephone Number",
                           "app_email|email|Applicant Email Address"
                          );

    // Custom code...
    // If user requires 2 applicants add the additional validation
    if(document.getElementById("app_no").value == "2") {

        var extraFields = Array(
                                "app2_title|sel|Second Applicant Title",
                                "app2_name|txt|Second Applicant Name",
                                "app2_address|txt|Second Applicant Address",
                                "app2_postcode|txt|Second Applicant Post Code",
                                "app2_dob|date|Second Applicant Date Of Birth",
                                "app2_tel|num|Second Applicant Telephone Number",
                                "app2_email|email|Second Applicant Email Address"
                               );

        // Add to original array
        fields = fields.concat(extraFields);
        
    }

    // Ensure we have elements to work with
    if(fields.length > 0)
    {
        // Iterate through the array
        for(var i in fields)
        {

          // Split each element into another array
          var row = fields[i].split("|");
          var target = document.getElementById(row[0])

          // Remove any required field class assigned to it
          removeClass(target,"reqField");

          // Do we have a field which is empty?
          if(target.value == "")
          {

              // Call custom error function
              showError(row[1],row[2]);

              // Add reqField class to element
              addClass(target,"reqField");
              
              return false;
              
           }

        }

        return true;

    } else {

        /* We have no fields to validate so we should not call this function
           or there has been a problem declaring the fields */

        alert("An Internal Error Occured.\nPlease contact the owner of this site.");
        
        return false;
        
    }

}