﻿function validate(fieldvalue) {
    var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    return (reg.test(fieldvalue));
}

function validateNameAndEmail(name, email, separator) {
    var errormessage = "";

    if (name == null || name == "") {
        errormessage += "Please enter your name" + separator;
    }
    if (email == null || email == "") {
        errormessage += "Please enter your email" + separator;
    }
    else {
        if (validate(email) == false) {
            errormessage += "Email address is invalid" + separator;
        }
    }
    return errormessage;
}

function clearAndCloseQuickContactForm() {
    $("#qemailsection").hide();
    $("#qfullname").val(""); ;
    $("#qemail").val("");
    $("#qphone").val("");
    $("#qcompany").val("");
    $("#qcomments").val("");
}

$(function () {
    var qemaillink = "#qemaillink";
    var section = "#qemailsection";

    $(section).hide();

    $(qemaillink).click(function () {
        $(section).show();

        var pos = $(qemaillink).position();
        $(section).css({ left: pos.left - 500, top: 50 });

    });

    $("#btnqcancel").click(function () {
        clearAndCloseQuickContactForm();
    });

    $("#btnqsubmit").click(function () {
        var name = $("#qfullname").val();
        var email = $("#qemail").val();
        var phone = $("#qphone").val();
        var company = $("#qcompany").val();
        var comments = $("#qcomments").val();

        var errormessage = validateNameAndEmail(name, email, "\n");

        if (errormessage != "") {
            alert(errormessage);
            return;
        }
        var submitobject = { fname: name, femail: email, fphone: phone, fcompany: company, fcomments: comments };
        clearAndCloseQuickContactForm();
        $.post("/Home/SubmitShortEmailForm", submitobject, function (data) {  });

    });
});

