﻿function Subscribe(formRef) {

    if (formRef.firstname.value.length > 0 && formRef.lastname.value.length > 0 && formRef.email.value.length > 0) {
        // check if valid email, Juan 8/28/2010
        if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(formRef.email.value)) {
            var SubscribeURL = "http://blog.smart401k.com/lead.php";
            xmlhttpPost(SubscribeURL, formRef);
            return true;
        } else {
            var SubscribeError = document.getElementById('SubscribeError');
            SubscribeError.innerHTML = 'Please enter a valid email address.<br/><br/>';
            return false;
        }        
    }
    else {
        var SubscribeError = document.getElementById('SubscribeError');
        SubscribeError.innerHTML = 'Please enter a firstname, lastname, and email.<br/><br/>';
        return false;
    }

}

function xmlhttpPost(strURL, formRef) {

    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('POST', 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(getquerystring(formRef));
}
function getquerystring(formRef) {
    qstr = 'FirstName=' + escape(formRef.firstname.value);  // NOTE: no '?' before querystring
    qstr += '&LastName=' + escape(formRef.lastname.value);
    qstr += '&Email=' + escape(formRef.email.value);
    //alert(qstr);
    return qstr;
}

function updatepage(str) {
    //alert(str);
}
