﻿var _hostId = "ctl00_ctl00_ctl00_IIRBBody_IIRBZonesBody_pageContentPlaceHolder_";

var labelBackColor;
function validateForm() {
    var requiredFields = "";
    var invalidFields = "";

    var lblFirstName = document.getElementById(_hostId + 'lblFirstName')
    var lblLastName = document.getElementById(_hostId + 'lblLastName')
    var lblEmail = document.getElementById(_hostId + 'lblEmail')
    var lblMessage = document.getElementById(_hostId + 'lblMessage')
    var lblCompany = document.getElementById(_hostId + 'lblCompany')
    var lblPhone = document.getElementById(_hostId + 'lblPhone')

    if (labelBackColor != null) {
        lblFirstName.style.color = labelBackColor;
        lblLastName.style.color = labelBackColor;
        lblCompany.style.color = labelBackColor;
        lblMessage.style.color = labelBackColor;
        lblPhone.style.color = labelBackColor;
        lblMessage.style.color = labelBackColor;
    }
    else {
        labelBackColor = lblFirstName.style.color;
    }
    var txtFirstName = document.getElementById(_hostId + 'txtFirstName')
    var txtLastName = document.getElementById(_hostId + 'txtLastName')
    var txtEmail = document.getElementById(_hostId + 'txtEmail')
    var txtPhone = document.getElementById(_hostId + 'txtPhone')
    var txtCompany = document.getElementById(_hostId + 'txtCompany')
    var txtMessage = document.getElementById(_hostId + 'txtMessage')

    // required fields
    if (txtFirstName.value.trim() == "") {
        requiredFields = requiredFields + "\r\tyour First Name"
        lblFirstName.style.color = "Red";
    }
    if (txtLastName.value.trim() == "") {
        requiredFields = requiredFields + "\r\tyour Last Name"
        lblLastName.style.color = "Red";
    }
    if (txtCompany.value.trim() == "") {
        requiredFields = requiredFields + "\r\tyour Company"
        lblCompany.style.color = "Red";
    }
    if (txtEmail.value.trim() == "") {
        requiredFields = requiredFields + "\r\tyour Email"
        lblEmail.style.color = "Red";
    }
    else {
        var re = /^([0-9a-zA-Z]([-\.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/; //new RegExp("^([0-9a-zA-Z]([-\.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$");
        if (!txtEmail.value.trim().match(re)) {
            invalidFields = invalidFields + "\r\tEmail has unexpected format"
            lblEmail.style.color = "Red";
        }
    }
    if (txtPhone.value.trim() == "") {
        requiredFields = requiredFields + "\r\tyour Phone"
        lblPhone.style.color = "Red";
    }
    else {
        var re = /^[01]?[- .]?(\([2-9]\d{2}\)|[2-9]\d{2})[- .]?\d{3}[- .]?\d{4}$/; //new RegExp("^[01]?[- .]?(\([2-9]\d{2}\)|[2-9]\d{2})[- .]?\d{3}[- .]?\d{4}$");
        if (!txtPhone.value.trim().match(re)) {
            invalidFields = invalidFields + "\r\tphone has unexpected format"
            lblEmail.style.color = "Red";
        }
    }
    
    if (txtMessage.value.trim() == "") {
        requiredFields = requiredFields + "\r\tthe Message to us"
        lblMessage.style.color = "Red";
    }

    if (requiredFields != "" || invalidFields != "") {
        var message = "";
        if (requiredFields != "") {
            message = "The following fields are required:\r" + requiredFields + "\r\r"
        }

        if (invalidFields != "") {
            message = message + "The following fields does not respect the format:\r" + invalidFields + "\r\r"
        }

        alert(message);
        return false;
    }

    return true;
}    

