﻿function contact_check(contactsales) {
    var submitform = true;
    var required_text_fields = new Array("Contact_Name", "Contact_Email", "Contact_Message");
    //Reset all feilds to white
    for (i = 0; i <= required_text_fields.length - 1; i++) {
        var inputvalue = document.getElementById(required_text_fields[i]).value;
        if ((inputvalue == "") || (inputvalue == null)) {
            flipcolor(required_text_fields[i], "#FF0000");
            submitform = false;
        } else {
            flipcolor(required_text_fields[i], "#FFFFFF");

        }
    }
    if (submitform == false) {
        alert('Please fill out the required feilds in red');
        return false;
    } else {
    if (emailCheck(document.getElementById('Contact_Email').value) == false) {
        flipcolor("Contact_Email", "#FF0000");
            return false;
        } else {
            document.getElementById('contact_us_table').style.display = "none";
            document.getElementById('please_wait').style.display = "";
            return true;
        }
    }
}
function flipcolor(divid, colorvalue) {
    document.getElementById(divid).style.background = colorvalue;
}
function emailCheck(emailStr) {
    var emailPat = /^(.+)@(.+)$/
    var specialChars = "\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
    var validChars = "\[^\\s" + specialChars + "\]"
    var quotedUser = "(\"[^\"]*\")"
    var ipDomainPat = /^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
    var atom = validChars + '+'
    var word = "(" + atom + "|" + quotedUser + ")"
    var userPat = new RegExp("^" + word + "(\\." + word + ")*$")
    var domainPat = new RegExp("^" + atom + "(\\." + atom + ")*$")
    var matchArray = emailStr.match(emailPat)
    if (matchArray == null) {
        alert("Invalid Email Address")
        return false
    }
    var user = matchArray[1]
    var domain = matchArray[2]
    if (user.match(userPat) == null) {
        alert("Invalid Email Address")
        return false
    }
    var IPArray = domain.match(ipDomainPat)
    if (IPArray != null) {
        for (var i = 1; i <= 4; i++) {
            if (IPArray[i] > 255) {
                alert("Invalid Email Address")
                return false
            }
        }
        return true
    }
    var domainArray = domain.match(domainPat)
    if (domainArray == null) {
        alert("Invalid Email Address")
        return false
    }
    var atomPat = new RegExp(atom, "g")
    var domArr = domain.match(atomPat)
    var len = domArr.length
    if (domArr[domArr.length - 1].length < 2 || domArr[domArr.length - 1].length > 3) {
        alert("Invalid Email Address")
        return false
    }
    if (len < 2) {
        var errStr = "Invalid Email Address"
        alert(errStr)
        return false
    }
    return true;
}
