//---------------------------------------------------------
// online_form.js: javascript required for the form where
// users apply for UK Data Archive Usernames and Passwords
//---------------------------------------------------------
// Check that all required fields have been filled in
//---------------------------------------------------------
function ValidateForm()
{
    var msg = "";
    var form = document.getElementById("ApplicationForm");

    // Check the mandatory fields have been populated
    if (form.FirstName.value == "")
        msg += "First Name\n";

     if (form.LastName.value == "")
        msg += "Last Name\n";

    if (form.Institution.selectedIndex == 0)
        msg += "Organisation or academic institution\n";

    // Check that new institution name has been provided if needed
    if (form.Institution[form.Institution.selectedIndex].value == "Other") {
        if (form.InstitutionNew.value == "")
            msg += "Organisation/institution name\n";
    }

    if (form.InstitutionAddress.value == "")
        msg += "Organisation/institution address\n";

    if (form.InstitutionPostcode.value == "")
        msg += "Organisation/institution postcode\n";

    if (form.InstitutionCountry.selectedIndex == 0)
        msg += "Organisation/institution country\n";

    // Check that notes have been provided if needed
    if (form.Institution[form.Institution.selectedIndex].value == "0") {
        if (form.Notes.value == "")
            msg += "Notes\n";
    }

    if (form.Email.value == "") {
        msg += "Email address\n";
    }
    else {
        if (!IsEmailSyntaxValid(form.Email.value)) {
             msg += "Email address does not appear to be a valid email address\n";
        }
    }

    if (form.Email2.value == "") {
        msg += "Email address (again)\n";
    }
    else {
        if (!IsEmailSyntaxValid(form.Email2.value)) {
             msg += "Email address (again) does not appear to be a valid email address\n";
        }
    }

    if (form.Email.value != form.Email2.value)
        msg += "The email addresses are not the same\n";

    if (form.Discipline.selectedIndex == 0)
        msg += "Discipline\n";

    if (form.UserType.selectedIndex == 0)
        msg += "User type\n";

    if (form.TermsAndConditions.checked == false)
        msg += "Your agreement to the terms and conditions\n";

    if (msg != "") {
        msg = "Please enter the following details:\n\n" + msg;
        alert(msg);
        return true; // could return false to stop page submitting, but server side validation is solid and gives useful info
    }
    else
        return true;
}

//---------------------------------------------------------
// Show/Hide inputs based on selected institution
//---------------------------------------------------------
function InstitutionCheck() {
    var form = document.getElementById("ApplicationForm");
    rowToShow1 = document.getElementById("RowInstitutionNew");
    rowToShow2 = document.getElementById("RowNotesIntro");
    rowToShow3 = document.getElementById("RowNotes");

    // Check for 'Other'
    if (form.Institution[form.Institution.selectedIndex].value == "Other")
        rowToShow1.style.display = displayMode;
    else
        rowToShow1.style.display = "none";

    // Check for 'Personal/Genealogical'
    if (form.Institution[form.Institution.selectedIndex].value == "0") {
        rowToShow2.style.display = displayMode;
        rowToShow3.style.display = displayMode
    }
    else {
        rowToShow2.style.display = "none";
        rowToShow3.style.display = "none";
    }
}

//---------------------------------------------------------
// Enforce a max length on <textarea> inputs
//---------------------------------------------------------
function MaxLengthCheck(add_length, i) {
    if (add_length.value.length >= i) {
        add_length.value = add_length.value.slice(0, i);
    }
}

//---------------------------------------------------------
// Check for a valid email syntax
//---------------------------------------------------------
function IsEmailSyntaxValid(email)
{
  // Regex isn't perfect; severe overcomplication would be required to actually meet the
  // standard for email addresses, but this is correct for most email addresses likely to
  // be entered.
  var match = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,6}$/i.test(email);
  if(match)
    return true;
  else
    return false;
}

