﻿var errorInvalidEmail = "Please enter valid address";
var errorTextGeneral = "<p>Sorry, there was an error sumbmitting form.</p>";

function ClearDefaultText() {
    if (document.getElementById('email').value == " Email" || document.getElementById('email').value == errorInvalidEmail) {
        document.getElementById('email').value = "";
    }
    return false;
}
function ShowSignUpForm() {

    document.getElementById("newslettersignup").innerHTML = "<p><input id=\"email\" name=\"email\" value=\" Email\" onclick=\"Javascript:ClearDefaultText();\"/>" +
                    "&nbsp;<input class=\"submit\" onclick=\"Javascript:NewsletterSignUp();return false;\" id=\"submit\" name=\"submit\" value=\"Submit\" type=\"button\"/></p>";
    document.getElementById("articles-links").innerHTML = "";
}

function NewsletterSignUp() {

    var email = ValidateEmail();

    if (email == false) {
        return false;
    }

    xmlhttpPost("/PageTemplates/FormProcessing.aspx?" +
                "FormType=newslettersignup" +
                "&Email=" + escape(document.getElementById('email').value));
                
    return false;
}

function xmlhttpPost(strURL) {
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('GET', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
            updatepage(self.xmlHttpReq.responseText);
        }
    }
    self.xmlHttpReq.send(null);
}


function updatepage(str) {
        document.getElementById("newslettersignup").innerHTML = str;
}

function ValidateFirstName() {
    if (document.getElementById('firstname').value != "") {
        document.getElementById('ErrFirstName').style.display = "none";
        document.getElementById('ErrFirstName').innerHTML = "";
        return true;
    }
    else {
        document.getElementById('ErrFirstName').style.display = "";
        document.getElementById('ErrFirstName').innerHTML = errorTextFirstname;
        return false;
    }
}

function ValidateLastName() {
    if (document.getElementById('secondname').value != "") {
        document.getElementById('ErrLastName').style.display = "none";
        document.getElementById('ErrLastName').innerHTML = "";
        return true;
    }
    else {
        document.getElementById('ErrLastName').style.display = "";
        document.getElementById('ErrLastName').innerHTML = errorTextLastName;
        return false;
    }
}

function ValidateEmail() {
    var email = document.getElementById('email').value;
    if ((email.indexOf("@") > 1) && //  must contain @, and it must not be the first character
          (email.lastIndexOf(".") > email.indexOf("@")) &&  // last dot must be after the @
          (email.indexOf("@") != email.length) &&  // @ must not be the last character
          (email.indexOf("..") < 0) && // two periods in a row is not valid
          (email.indexOf(".") != email.length) &&  // . must not be the last character
          (AllValidEmailChars(email))) // all characters must be valid
    {
        //document.getElementById('ErrEmail').style.display = "none";
        //document.getElementById('ErrEmail').innerHTML = "";
        return true;
    }
    else {
        document.getElementById('email').value = errorInvalidEmail;
        return false;
    }
}

function AllValidEmailChars(email) {
    var isValid = true;
    var validchars = "abcdefghijklmnopqrstuvwxyz0123456789@.-_";
    for (var i = 0; i < email.length; i++) {
        var letter = email.charAt(i).toLowerCase();
        if (validchars.indexOf(letter) != -1) {
            continue;
        }
        else {
            isValid = false;
            break;
        }
    }
    return isValid;
}


