﻿function isEmpty(textField) {
    return ((textField.value == null) || (textField.value.length == 0));
}

function validate() {
    var form = document.forms['form'];

    var tformEmail = trim(form.email.value);
    var email = /^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/i
    var teln = /^[0-9\. ]+$/
    var postCode = /^[A-Z]{1,2}[0-9]{1,2}[A-Z]? ?[0-9][A-Z]{1,2}$/i


    if ((form.Answer[0].checked == false) && (form.Answer[1].checked == false) && (form.Answer[2].checked == false)) {
        alert("Error: You have not selected the answer to the quiz, please fill in all required fields");
        form.Answer[0].focus();
        return false;
    }
    if (form.title.selectedIndex == 0) {

        alert("Error: You have not entered your Title, please fill in all required fields");
        form.title.focus();
        return false;
    }
    if (isEmpty(form.forename)) {
        alert("Error: You have not entered your Forename, please fill in all required fields");
        form.forename.focus();
        return false;
    }
    if (isEmpty(form.surname)) {
        alert("Error: You have not entered your Surname, please fill in all required fields");
        form.surname.focus();
        return false;
    }
    if (form.firmname) {
        if (isEmpty(form.firmname)) {
            alert("Error: You have not entered your Firm name, please fill in all required fields");
            form.firmname.focus();
            return false;
        }
    }
    if (isEmpty(form.address1)) {
        alert("Error: You have not entered your Address, please fill in all required fileds");
        form.address1.focus();
        return false;
    }
    if (isEmpty(form.postcode)) {
        alert("Error: You have not entered your Postcode, please fill in all required fileds");
        form.postcode.focus();
        return false;
    }
    if (!postCode.test(form.postcode.value)) {
        alert("Error: You have entered an invalid Postcode, please fill in all required fileds");
        form.postcode.focus();
        return false;
    }
    if (isEmpty(form.email)) {
        alert("Error: You have not entered your Email, please fill in all required fileds");
        form.email.focus();
        return false;
    }
    if (!email.test(tformEmail)) {
        alert("Error: You have not entered a valid e-mail address");
        form.email.focus();
        return false;
    }
    if ((form.email.value) != (form.confirmEmail.value)) {
        alert("Error: Your email and confirm email does not match, please fill in all required fields");
        form.confirmEmail.focus();
        return false;
    }
    if (form.eventName) {
        if (form.eventName.selectedIndex == 0) {
            alert("Error: You have not selected your preferred event, please fill in all required fields");
            form.eventName.focus();
            return false;
        }
    }
    if (!form.optin2.checked) {
        alert("Error: You have not agreed to terms and conditions");
        form.optin2.focus();
        return false;
    }
    return true;
}

function trim(str) {
    return str.replace(/^\s+|\s+$/g, '');
}


function validateDates() {
    var form = document.forms['form'];
    var date = /^\d{1,2}\/\d{1,2}\/\d{4}$/

    if (!isEmpty(form.startDate) && trim(form.startDate.value).match(date) == null) {
        alert("Error: Start date must be in the format dd/mm/yyyy");
        form.startDate.focus();
        return false;
    }
    
     if (!isEmpty(form.endDate) && trim(form.endDate.value).match(date) == null) {
        alert("Error: End date must be in the format dd/mm/yyyy");
        form.endDate.focus();
        return false;
    }
}