﻿function validate_required(field, warnField) {

    //Reset back to hidden for any second attempt
    warnField.style.visibility = 'hidden'
    with (field) {
        if (value == null || value == "") {
            warnField.style.visibility = 'visible'; return false;
        }
        else {
            return true;
        }
    }
}

function validate_match(field1, field2, alerttxt) {

    if (field1.value == field2.value) {
        return true;
    }
    else {
        alert(alerttxt); return false;
    }
}

function validate_validemail(emailaddress) {

    var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
        if (reg.test(emailaddress) == false) {
            alert('Invalid Email Address');
            return false;
        }
        return true;
}

function validate_validemail_OLD(emailaddress) {

    var AtPos
    var DotPos
    AtPos = emailaddress.indexOf("@");
    DotPos = emailaddress.lastIndexOf(".");

    if (AtPos == -1 || DotPos == -1)
    { alert("Invalid Email Address"); return false; }

    if (AtPos > DotPos)
    { alert("Invalid Email Address"); return false; }

    if (DotPos - AtPos == 1)
    { alert("Invalid Email Address"); return false; }

    return true;
}

function validate_quickquery_form() {

    var success = true;

    var nameText = document.getElementById('ctl00_RightQuery1_txtQuickQueryName');
    var nameWarn = document.getElementById('ctl00_RightQuery1_lblRequiredQuickQueryName');

    var emailText = document.getElementById('ctl00_RightQuery1_txtQuickQueryEmailAdd');
    var emailWarn = document.getElementById('ctl00_RightQuery1_lblRequiredQuickQueryEmail');

    var telText = document.getElementById('ctl00_RightQuery1_txtQuickQueryTelNo');
    var telWarn = document.getElementById('ctl00_RightQuery1_lblRequiredQuickQueryTel');

    var messText = document.getElementById('ctl00_RightQuery1_txtQuickQueryQuest');
    var messWarn = document.getElementById('ctl00_RightQuery1_lblRequiredQuickQueryMessage');

    var telOnly = false;

    telWarn.style.visibility = 'hidden'
    
    
    // Quick Query Name
    if (validate_required(nameText, nameWarn) == false)
    { success = false; }

    // Quick Query Email or Tel
    if (validate_required(emailText, emailWarn) == false) {
        
        // Only Validate Tel if Email False
        if (validate_required(telText, telWarn) == false) {
            success = false;
        }
        else {
            telOnly = true; // Record that only telephone has been input
            emailWarn.style.visibility = 'hidden'
        }
    
    }

    // Quick Query Message
    if (validate_required(messText, messWarn) == false)
    { success = false; }


    //If all fields populated, check if emails valid when telephone only has not been suibmitted
    if (success == true && telOnly == false) {

        if (validate_validemail(emailText.value) == false) // Check Valid
        { success = false }

    }

    if (success == false)
    { return false; }
    else
    { return true; }

}

function validate_freequote_form() {

    var success = true;

    var nameText = document.getElementById('ctl00_RightQuery1_txtFreeQuoteName');
    var nameWarn = document.getElementById('ctl00_RightQuery1_lblRequiredFreeQuoteName');

    var emailText = document.getElementById('ctl00_RightQuery1_txtFreeQuoteEmailAdd');
    var emailWarn = document.getElementById('ctl00_RightQuery1_lblRequiredFreeQuoteEmail');

    var telText = document.getElementById('ctl00_RightQuery1_txtFreeQuoteTelNo');
    var telWarn = document.getElementById('ctl00_RightQuery1_lblRequiredFreeQuoteTel');

    var prefContEither = document.getElementById('ctl00_RightQuery1_PreferedContact_0');
    var prefContTel = document.getElementById('ctl00_RightQuery1_PreferedContact_1');
    var prefContEmail = document.getElementById('ctl00_RightQuery1_PreferedContact_2');

    // Free Quote Name
    if (validate_required(nameText, nameWarn) == false)
    { success = false; }

//Only validate the preferred contact method
    switch (true) {
        case (prefContTel.checked):
            emailWarn.style.visibility = 'hidden'// Hide this for second time clicked

            if (validate_required(telText, telWarn) == false)
            { success = false; }

            break;
        case (prefContEmail.checked):
            telWarn.style.visibility = 'hidden'// Hide this for second time clicked

            if (validate_required(emailText, emailWarn) == false)
            { success = false; }
            
            break;
        default:
            if (validate_required(telText, telWarn) == false)
            { success = false; }

            if (validate_required(emailText, emailWarn) == false) {
                success = false;
            }
    }


    if (prefContTel.checked == false && success == true) {

        if (validate_validemail(emailText.value) == false) // Check Valid
        { success = false }
    }

    if (success == false)
    { return false; }
    else
    { return true; }
    
}

function validate_feedback_form() {

    var success = true;

    var nameText = document.getElementById('ctl00_RightQuery1_txtFeedbackName');
    var nameWarn = document.getElementById('ctl00_RightQuery1_lblRequiredFeedbackName');

    var emailText = document.getElementById('ctl00_RightQuery1_txtFeedbackEmail');

    var messText = document.getElementById('ctl00_RightQuery1_txtFeedbackMessage');
    var messWarn = document.getElementById('ctl00_RightQuery1_lblRequiredFeedbackMessage');

    // feedback Name
    if (validate_required(nameText, nameWarn) == false)
    { success = false; }

    // Feedback Message
    if (validate_required(messText, messWarn) == false)
    { success = false; }

    if (success == true && emailText.value != "") {

        if (validate_validemail(emailText.value) == false) // Check Valid
        { success = false }
    }


    if (success == false)
    { return false; }
    else
    { return true; }    

}



function validate_contact_form() {

    var success = true;

    //Name
    var nameText = document.getElementById('ctl00_MainContent_txtYourName');
    var nameWarn = document.getElementById('ctl00_MainContent_lblRequiredName');

    if (validate_required(nameText, nameWarn) == false)
    { success = false; }

    //Email
    var emailText = document.getElementById('ctl00_MainContent_txtYourEmailAdd');
    var emailWarn = document.getElementById('ctl00_MainContent_lblRequiredEmail');

    if (validate_required(emailText, emailWarn) == false)
    { success = false; }

    //Confirm
    var confirmText = document.getElementById('ctl00_MainContent_txtRepeatEmailAdd');
    var confirmWarn = document.getElementById('ctl00_MainContent_lblrequiredConfirm');

    if (validate_required(confirmText, confirmWarn) == false)
    { success = false; }

    //Message
    var messageText = document.getElementById('ctl00_MainContent_txtDetailQuery');
    var messageWarn = document.getElementById('ctl00_MainContent_lblRequiredMessage');

    if (validate_required(messageText, messageWarn) == false)
    { success = false; }

    //If all fields populated, check if emails valid and match
    if (success == true) {

        if (validate_validemail(emailText.value) == false) // Check Valid
        { success = false }

        if (success == true) {
            if (validate_match(emailText, confirmText, "Emails do not match") == false)  // Check Match
            { success = false; }
        }
    }

    if (success == false)
    { return false; }
    else
    { return true; }

}

