var objXMLHttp 

function subscribe(str) {
    var regEx = /^[\w\.\+-]{1,}\@([\da-zA-Z-]{1,}\.){1,}[\da-zA-Z-]{2,6}$/; 
    if (!regEx.test(str)) { 
      $(".nlmsg").html('Invalid email');
      alert('Invalid email address!');
      return;
    }
	
 
    objXMLHttp=createXmlHttpObject()
    
    if (objXMLHttp==null) {
        alert ('Your browser does not support the XMLHttpRequest object!');
        return;
    } 
    
    var url='subscribe/?eml=' + str + '&sid=' + Math.random(); // prevent caching
    objXMLHttp.onreadystatechange = stateChanged;
    objXMLHttp.open('GET',url,true); // GET method
    objXMLHttp.send(null); // always use null for GET method
}
    
function createXmlHttpObject() {
    var req = null;
    try {
        req = new XMLHttpRequest(); //ie7, ff, safari
    } 
    catch (e) {
        try {
            req = new ActiveXObject("Msxml2.XMLHTTP"); //later ie
        }
        catch (e) {
            try {
                req = new ActiveXObject("Microsoft.XMLHTTP") // earlier ie
            }
            catch (e) {
                // could not create XMLHttpRequest object
                return false;
            }
        }
    }
    return req;
}

function stateChanged()
{ 
    if (objXMLHttp.readyState == 0)    {
         $(".nlmsg").html("<p style=\"color:red;\"> Processing...</p>"); //uninitialized
    }
    else if(objXMLHttp.readyState == 1)    {
         $(".nlmsg").html("<p style=\"color:red;\"> Processing...</p>"); //loading
    }
    else if(objXMLHttp.readyState == 2)    {
         $(".nlmsg").html("<p style=\"color:red;\"> Processing...</p>"); //loaded
    }
    else if(objXMLHttp.readyState == 3)    {
         $(".nlmsg").html("<p style=\"color:red;\"> Processing...</p>"); //interactive
    }
    else if (objXMLHttp.readyState == 4 || objXMLHttp.readyState == "complete") { 
         $(".nlmsg").html("<p style=\"color:#ccc;\"> Thank you for subscribing to our newsletter.</p>"); // completed
    } 
}
