﻿
String.prototype.trim = function()
{
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}

function NewsClick()
{
    var txtMail = document.getElementById('txtEmail');
    var txtFirst = document.getElementById('txtFirstName');
    var txtLast = document.getElementById('txtLastName');
    var txtComp = document.getElementById('txtCompany');

    var email = new String(txtMail.value);
    var firstname = new String(txtFirst.value);
    var lastname = new String(txtLast.value);
    var company = new String(txtComp.value);

    var isValid = true;
    var txtInput = txtMail;
    var errMsg = "";
    var allowed = /[<\>\{\}\[\]\?\/\\\|\,\.\'\"\:\;]/;
    var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;

    if (email.trim() == "") {
        errMsg = 'Please enter an email address';
        txtInput = txtMail;
        isValid = false;
    } else if (!emailPattern.test(email)) {
        errMsg = 'Please enter a valid email address';
        txtInput = txtMail;
        isValid = false;
    } else if (firstname.trim() == "") {
        errMsg = 'Please enter a first name';
        txtInput = txtFirst;
        isValid = false;
    } else if (allowed.test(firstname)) {
        errMsg = 'Please enter a valid first name';
        txtInput = txtFirst;
        isValid = false;
    } else if (lastname.trim() == "") {
        errMsg = 'Please enter a last name';
        txtInput = txtLast;
        isValid = false;
    } else if (allowed.test(lastname)) {
        errMsg = 'Please enter a valid last name';
        txtInput = txtLast;
        isValid = false;
    } else if (company.trim() == "") {
        errMsg = 'Please enter a company';
        txtInput = txtComp;
        isValid = false;
    } else if (allowed.test(company)) {
        errMsg = 'Please enter a valid company';
        txtInput = txtComp;
        isValid = false;
    }

    if (!isValid)
    {
        //var span = $('#spanMessage');
        alert(errMsg);
        //$(span).html('Please enter an email address');
        txtInput.focus();
    }
    else
    {
        document.body.style.cursor = 'wait';

        $.ajax(
            {
                url: '/xmlpages/NewsLetter.ashx',
                data: { 'email': email, 'firstname': firstname, 'lastname': lastname, 'company': company },
                dataType: "json",
                success: function (messages) {
                    //                    var span = $('#spanMessage');

                    //                    $(span).html(messages[0]);

                    //                    var div = $('div[id=panelInput]');

                    if (messages.length == 0) {
                        //                        div[0].style['display'] = 'none';
                        //                        $(span).html
                        self.parent.updated(true);
                        alert("Please check your email. An email has been sent to you for confirmation process.");
                    }
                    else {
                        //                        div[0].style['display'] = '';
                        alert(messages[0]);
                        txtMail.focus();
                        txtMail.select();
                    }

                    document.body.style.cursor = 'auto';
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    //                    var span = $('#spanMessage');
                    //                    $(span).html
                    alert('Oops.. there was an error processing your request');

                    txtMail.focus();
                    txtMail.select();
                    document.body.style.cursor = 'auto';
                }
            });
    }
}


