﻿//Contact Us Form
function SendEmailToNovotronix() {
    var errorMessage = "";
    var fromName = $("input[name='textName']").val();
    var fromCompany = $("input[name='textCompany']").val();
    var fromTelephone = $("input[name='textTelephone']").val();
    var fromMessage = $("textarea[name='TextMessage']").val();
    var fromSubject = "WebSite Contact from Novotronix.com";
    var recipientEmail = "info@novotronix.com";
    var fromEmail = $("input[name='TextEmail']").val();
    var isValidInput = "true";

    if (!isValidEmailAddress(fromEmail)) {
        isValidInput = "false";
        errorMessage = errorMessage + "Please enter a valid Email Address\n";
    }
    if (fromName == "") {
        isValidInput = "false";
        errorMessage = errorMessage + "Please enter your name\n";
    }
    if (fromTelephone == "") {
        isValidInput = "false";
        errorMessage = errorMessage + "Please enter your telephone number\n";
    }
    if (fromCompany == "") {
        isValidInput = "false";
        errorMessage = errorMessage + "Please enter your company name\n";
    }
    if (fromMessage == "") {
        isValidInput = "false";
        errorMessage = errorMessage + "Please enter a message\n";
    }

    if (isValidInput == "true") {
        $(".contactForm").html("<img src='/_images/novotronix/loading.gif' style='border:none'/>");
        var fullMessage = "&lt;h1&gt;Web Site Contact&lt;/h1&gt; \
	Name: &lt;h3&gt;" + fromName + "&lt;/h3&gt; \
	Email: &lt;h3&gt;" + fromEmail + "&lt;/h3&gt; \
	Company: &lt;h3&gt;" + fromCompany + "&lt;/h3&gt; \
	Telephone: &lt;h3&gt;" + fromTelephone + "&lt;/h3&gt; \
	Message: &lt;h3&gt;" + fromMessage + "&lt;/h3&gt;";

        var soapEnvelope = "<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'> \
	  <soap:Body> \
	    <SendEmail xmlns='http://webservices.novotronix.com/'> \
	      <address>" + recipientEmail + "</address> \
	      <subject>" + fromSubject + "</subject> \
	      <message>" + fullMessage + "</message> \
	      <htmlformat>true</htmlformat> \
	    </SendEmail> \
	  </soap:Body> \
	</soap:Envelope>";

        $.ajax({
            url: '/_layouts/novotronix/webservice/emailservice.asmx',
            success: emailSuccess,
            error: emailError,
            type: 'POST',
            data: soapEnvelope,
            contentType: "text/xml; charset=\'utf-8\'",
            dataType: 'xml'
        });
    }
    else {
        alert(errorMessage);
    }
}

function emailSuccess(xData, status) {
    location.href = "Thank-You.aspx";
}

function emailError(xData, status) {
    alert("Oops - we could not send your message. Please try again.\n If this happens again, please call 01902 424277");
    location.href = location.href;
}

function isValidEmailAddress(emailAddress) {
    var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
    return pattern.test(emailAddress);
}
