function validateAddress(incoming) {
    var emailstring = incoming;
    var ampIndex = emailstring.indexOf("@");
    var afterAmp = emailstring.substring((ampIndex + 1), emailstring.length);
    // find a dot in the portion of the string after the ampersand only
    var dotIndex = afterAmp.indexOf(".");
    // determine dot position in entire string (not just after amp portion)
    dotIndex = dotIndex + ampIndex + 1;
    // afterAmp will be portion of string from ampersand to dot
    afterAmp = emailstring.substring((ampIndex + 1), dotIndex);
    // afterDot will be portion of string from dot to end of string
    var afterDot = emailstring.substring((dotIndex + 1), emailstring.length);
    var beforeAmp = emailstring.substring(0, (ampIndex));
    //old regex did not allow subdomains and dots in names
    //var email_regex = /^[\w\d\!\#\$\%\&\'\*\+\-\/\=\?\^\_\`\{\|\}\~]+(\.[\w\d\!\#\$\%\&\'\*\+\-\/\=\?\^\_\`\{\|\}\~])*\@(((\w+[\w\d\-]*[\w\d]\.)+(\w+[\w\d\-]*[\w\d]))|((\d{1,3}\.){3}\d{1,3}))$/;
    var email_regex = /^\w(?:\w|-|\.(?!\.|@))*@\w(?:\w|-|\.(?!\.))*\.\w{2,3}/
    // index of -1 means "not found"
    if ((emailstring.indexOf("@") != "-1") && (emailstring.length > 5) && (afterAmp.length > 0) && (beforeAmp.length > 1) && (afterDot.length > 1) && (email_regex.test(emailstring))) {
        return true;
    } else {
        alert("Please check your email address");
        return false;
    }
}

function isEmpty(s) {
    return ((s == null) || (s.length == 0) || /^\s+$/.test(s))
}

function submit_form() {
    if (document.getElementById("formstandard_nameInput") && isEmpty(document.getElementById("formstandard_nameInput").value)) {
        alert("Please enter your first name");
        document.getElementById("formstandard_nameInput").focus();
        return false;
    }
    if (document.getElementById("formstandard_lastNameInput") && isEmpty(document.getElementById("formstandard_lastNameInput").value)) {
        alert("Please enter your last name");
        document.getElementById("formstandard_lastNameInput").focus();
        return false;
    }
    if (document.getElementById("formstandard_company") && isEmpty(document.getElementById("formstandard_company").value)) {
        alert("Please enter your company");
        document.getElementById("formstandard_company").focus();
        return false;
    }
    var from;
    from = '<%=FromPage %>'
    if (from != 'directsalesenquiry') {
        if (document.getElementById("formstandard_companyDepartment") && isEmpty(document.getElementById("formstandard_companyDepartment").value)) {
            alert("Please select your Department");
            document.getElementById("formstandard_companyDepartment").focus();
            return false;
        }
        if (document.getElementById("formstandard_countrySelect_country") && isEmpty(document.getElementById("formstandard_countrySelect_country").value)) {
            alert("Please select your country");
            document.getElementById("formstandard_countrySelect_country").focus();
            return false;
        }

        if (document.getElementById('formstandard_countrySelect_country') && document.getElementById('formstandard_countrySelect_country').options[document.getElementById('formstandard_countrySelect_country').selectedIndex].value == "Australia") {
            if (document.getElementById("stateselect").style.display = "none") {
                displaystate()
                if (document.getElementById("formstandard_stateSelect_state") && isEmpty(document.getElementById("formstandard_stateSelect_state").value)) {
                    alert("Please select your state");
                    document.getElementById("formstandard_stateSelect_state").focus();
                    return false;
                }
            }
            else {
                document.getElementById("stateselect").style.display = "block";
                if (isEmpty(document.getElementById("formstandard_stateSelect_state").value)) {
                    alert("Please select your state");
                    document.getElementById("formstandard_stateSelect_state").focus();
                    return false;
                }
            }
        }

        if (document.getElementById("formstandard_email") && isEmpty(document.getElementById("formstandard_email").value)) {
                alert("Please enter your business email");
                document.getElementById("formstandard_email").focus();
                return false;
        }
        var validEmail;
        validEmail = validateAddress(document.getElementById("formstandard_email").value);
        if (validEmail == false) {
            return false;
        }

        if (document.getElementById("formstandard_phone") && isEmpty(document.getElementById("formstandard_phone").value)) {
            alert("Please enter your Phone Number");
            document.getElementById("formstandard_phone").focus();
            return false;
        }
        if (document.getElementById("formstandard_enquiry") && isEmpty(document.getElementById("formstandard_enquiry").value)) {
            alert("Please enter your enquiry");
            document.getElementById("formstandard_enquiry").focus();
            return false;
        }
        if (document.getElementById("formstandard_comments") && isEmpty(document.getElementById("formstandard_comments").value)) {
            alert("Please enter your comments");
            document.getElementById("formstandard_comments").focus();
            return false;
        }
        if (document.getElementById("formstandard_MediaEnquiry") && isEmpty(document.getElementById("formstandard_MediaEnquiry").value)) {
            alert("Please enter your enquiry");
            document.getElementById("formstandard_MediaEnquiry").focus();
            return false;
        }
        if (document.getElementById("formstandard_CustomerYes") && document.getElementById("formstandard_CustomerNo")) {
            if ((document.getElementById("formstandard_CustomerYes").checked == false) && (document.getElementById("formstandard_CustomerNo").checked == false)) {
                alert("Please select whether you are currently an Intranet DASHBOARD Customer or not");
                document.getElementById("formstandard_CustomerYes").focus();
                return false;
            }
        }    
        if (document.getElementById("formstandard_whatVersion") && isEmpty(document.getElementById("formstandard_whatVersion").value)) {
            alert("Please select which version of iD you are using");
            document.getElementById("formstandard_whatVersion").focus();
            return false;
        }
        if (document.getElementById("fileStoreContainer") && document.getElementById("fileStoreContainer").style.display != "none" && isEmpty(document.getElementById("formstandard_fileStore").value)) {
            alert("Please select which iD File Store Type you are using");
            document.getElementById("formstandard_fileStore").focus();
            return false;
        }
        if (document.getElementById("formstandard_whatSQL") && isEmpty(document.getElementById("formstandard_whatSQL").value)) {
            alert("Please select which version of SQL Server you are using");
            document.getElementById("formstandard_whatSQL").focus();
            return false;
        }
        if (document.getElementById("formstandard_windowsServer") && isEmpty(document.getElementById("formstandard_windowsServer").value)) {
            alert("Please select which version of Windows Server you are running");
            document.getElementById("formstandard_windowsServer").focus();
            return false;
        }
        if (document.getElementById("formstandard_netVersion") && isEmpty(document.getElementById("formstandard_netVersion").value)) {
            alert("Please select which version of IIS .NET you are running");
            document.getElementById("formstandard_netVersion").focus();
            return false;
        }
        if (document.getElementById("formstandard_numPpl") && isEmpty(document.getElementById("formstandard_numPpl").value)) {
            alert("Please enter the number of people that access the intranet");
            document.getElementById("formstandard_numPpl").focus();
            return false;
        }
        if (document.getElementById("formstandard_WhichCity")) {
            var checked = false;
            if (document.getElementById("formstandard_WhichCity_0").checked || document.getElementById("formstandard_WhichCity_1").checked || document.getElementById("formstandard_WhichCity_2").checked) {
                checked = true;
            }
            if (checked == false) {
                alert("Please select which showcase you are registering for");
                document.getElementById("formstandard_WhichCity").focus();
                return false;
            }
        }
        if (document.getElementById("formstandard_WhichCity2")) {
            var checked = false;
            if (document.getElementById("formstandard_WhichCity2_0").checked || document.getElementById("formstandard_WhichCity2_1").checked || document.getElementById("formstandard_WhichCity2_2").checked) {
                checked = true;
            }
            if (checked == false) {
                alert("Please select which seminar you are registering for");
                document.getElementById("formstandard_WhichCity2").focus();
                return false;
            }
        }
        if (document.getElementById("formstandard_WhichCity3")) {
            var checked = false;
            if (document.getElementById("formstandard_WhichCity3_0").checked || document.getElementById("formstandard_WhichCity3_1").checked) {
                checked = true;
            }
            if (checked == false) {
                alert("Please select which showcase you are registering for");
                document.getElementById("formstandard_WhichCity3").focus();
                return false;
            }
        }        
        if (document.getElementById("formstandard_WhichRegion")) {
            var checked = false;
            if (document.getElementById("formstandard_WhichRegion_0").checked || document.getElementById("formstandard_WhichRegion_1").checked || document.getElementById("formstandard_WhichRegion_2").checked || document.getElementById("formstandard_WhichRegion_3").checked) {
                checked = true;
            }
            if (checked == false) {
                alert("Please select which region you would like to register for events");
                document.getElementById("formstandard_WhichRegion").focus();
                return false;
            }
        }
    }
}

function displaystate() {
    if (document.getElementById('stateselect')) {
        document.getElementById('stateselect').style.display = 'none';
        var whichstate = 'table-row'
        if (document.all) { whichstate = 'block' }
        if (document.getElementById('formstandard_countrySelect_country').options[document.getElementById('formstandard_countrySelect_country').selectedIndex].value == "Australia") {
            document.getElementById('stateselect').style.display = whichstate + "";
        }
    }
}

function OtherFindUs(object) {
    if (object.options[object.selectedIndex].value == "Other") {
        document.getElementById('howfindus2tr').style.display = "";
    }
    else {
        document.getElementById('howfindus2tr').style.display = "none";
    }
}
function OtherFindUs2(object) {
    if (object.options[object.selectedIndex].value == "Other") {
        document.getElementById('howfindus3tr').style.display = "";
    }
    else {
        document.getElementById('howfindus3tr').style.display = "none";
    }
}
function OtherFindUs3(object) {
    if (object.options[object.selectedIndex].value == "Other") {
        document.getElementById('howfindus4tr').style.display = "";
    }
    else {
        document.getElementById('howfindus4tr').style.display = "none";
    }
}
function sqlSelectChange(obj) {
    if (obj.value == "MSDE" || obj.value == "SQL Express 2005" || obj.value == "SQL Express 2008") {
        document.getElementById("fileStoreContainer").style.display = "";
    } else {
        document.getElementById("fileStoreContainer").style.display = "none";
    }
}
