var doSubscribe;
var chid;
function wpregister(doSubscribeIndicator, obscureChannelId) {
    var joinname = escape(document.getElementById("joinname").value);
    var joinuname = escape(document.getElementById("joinuname").value);
    var joinemail = escape(document.getElementById("joinemail").value);
    var joinpw = document.getElementById("joinpw").value;
    var joinverifypw = document.getElementById("joinverifypw").value;
    var joinsource = escape(document.getElementById("joinsource").value);
    var compurl = escape(document.getElementById("compurl").value);
    var phone = escape(document.getElementById("phone").value);
    var captcha = document.getElementById("captcha_code").value;

    doSubscribe = doSubscribeIndicator;
    chid = obscureChannelId;
        
    if ((joinname.length ==0) ||
        (joinuname.length == 0) ||
        (joinemail.length == 0) ||
        (joinpw.length == 0) ||
        (joinverifypw.length == 0) ||
        (compurl.length == 0) ||
        (phone.length == 0)
        ) {
        showError("You must fill in all of the fields marked with an (*)");
        return;
        }
    
    if (joinverifypw != joinpw) {
        showError("The passwords don't match.  Please make sure both passwords match.");
        return;
    }
        
    if (joinsource=='Internet ad, search, word-of-mouth, seminar, presentation, press release, other?') {
        joinsource = "";
    }
    
    // encrypt the password
    encrypted = sha256_digest(joinpw);
    
    // Make call to register.  Handle potential errors like duplicate user name
    ajax("services/joinnow_srvc.php", "&uname="+joinuname+"&name="+joinname+"&pw="+encrypted+"&email="+joinemail+"&captcha="+captcha+"&phone="+phone+"&compurl="+compurl, handleRegistrationResponse);
}

function handleRegistrationResponse(contents, responseDoc) {
    // If registration succeeded
    var responses = new Array;
    responses = contents.split(",");
    var succeeded = ((responses[1]=="success") && (responses[0]=="1")) ? true : false;
        // figure that out
    // Set the global target so when the login happens we will go there
    if (succeeded) {
        if (doSubscribe) {
            ajax("./services/dosubscribe_srvc.php", "&chid="+chid, handleSubscribeResponse);
        } else {
            window.location.href = "/?wp=join2";
        }
    } else {
        // Figure out what went wrong and put the error in the error div
        if (responses[1]=="duplicate") {
            showError("There is already a username like this.  Please choose another one.");
        } else if (responses[1]=="captcha") {
            showError("You need to enter the code you see in the image.");
        } else if (responses[1]=="captchamismatch") {
            showError("The code you typed did not match the image.");
        } else if (responses[1]=="NoUname") {
            showError("You need to enter a user name.");
        } else if (responses[1]=="NoName") {
            showError("You need to enter your name.");
        } else if (responses[1]=="NoPassword") {
            showError("You need to enter a password.");
        } else if (responses[1]=="NoEmail") {
            showError("You need to enter an email address.");
        } else if (responses[1]=="InvalidEmail") {
            showError("You need to enter a valid email address.");
        } 
    }
}

function handleSubscribeResponse(contents, responseDoc) {
    var statusElems = responseDoc.getElementsByTagName("status");
    if (statusElems.length > 0) {
        var status=statusElems[0];
        var statusValue = status.getAttribute("value");
        var statusDesc = status.getAttribute("desc");
        if (status.getAttribute("value") == "1") {
            window.location.href = "/?wp=my";
        } else {
            alert(statusDesc);
        }
    }
 }

function showError(errorMsg) {
    var errorBlock = document.getElementById("errorblock");
    if (errorBlock != null) {
        var innerHTML = "<span class='' style='font-size:10pt;font-weight:700;color:red'>"+errorMsg+"</span>";
        errorBlock.innerHTML = innerHTML;
    }
}
