var actioncode = 0;

function pageload(){
    var txt = getCookieValue("locale");
    if (txt != "") {

        //alert("pageload: " + getCookieValue("svc_action"));

        if (getCookieValue("svc_action") == 1){
            actioncode = 1;
        } else {
            actioncode = 0;
        }

        //alert("pageload: " + getCookieValue("svc_action"));

        document.frmFeedback.txtFeedback.value = txt;
        if (getCookieValue("svc_name") != "" && getCookieValue("svc_email") != "" ) {
            document.frmFeedback.txtEmail.value = getCookieValue("svc_email");
            document.frmFeedback.txtName.value = getCookieValue("svc_name");
        } else if (getCookieValue("bsm-now.com_username")){
            document.frmFeedback.txtEmail.value = getCookieValue("bsm-now.com_username");
        }

        if (actioncode == 0){
            document.getElementById("txtmore").innerHTML = "If you are having an issue with an assessment please include a description of the problem, and any links you have to the assessment.&nbsp;Your last location or activity in the application has been placed in the feedback box below.";
        } else {
            txt = 'This is your request to download the "Target Their Hearts!" SaaS special report and to join our blog updates mailing list. ';
            txt += "However, before we send the report to you, please confirm your subscription (to help fight spam). ";
            txt += "PLEASE CONFIRM BY CLICKING THE SUBMIT BUTTON BELOW.";
            document.getElementById("txtmore").innerHTML = txt;
        }
    }

}

function DoSubmit(){
    httpObject = getHTTPObject();
    if (httpObject != null) {
        httpObject.open("POST", "feedback_script.php");
        httpObject.onreadystatechange = get_xyz; // must be the function to call
        httpObject.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

        //alert("DoSubmit: " + actioncode);

        var frm = document.frmFeedback;
        var cmd = "n=" +  frm.txtName.value + "&e=" + frm.txtEmail.value + "&f=" + frm.txtFeedback.value + "&cp=" + document.getElementById('varchecksum').value + "&ac=" + actioncode;
        httpObject.send(cmd); // send the request
    }
}

function get_xyz(){
    if(httpObject.readyState == 4){
        var retval = httpObject.responseText;

        if (retval == -1){ // -1=failure in feedback_script.php; 0=SENT
            if (actioncode == 1) { // signup for news letter worked!
                var na = getCookieValue("svc_name").split(" ");
                retval = na[0] + ", \nThank you for signing up to our news letter. Your e-mail -- " + getCookieValue("svc_email") + ' -- has neen added to our list. As promised, check your inbox for an email with the link to download your the brand new "Target Their Hearts!" SaaS report.';
                writeSessionCookie('svc_action', 0);
                writeSessionCookie('svc_name', "");
                writeSessionCookie('svc_email', "");
                writeSessionCookie('locale', "");
            } else {
                retval = "Thank you! Your feedback is on its way to us.";
            }
            alert (retval);
            window.location.reload();
        } else {
            writeSessionCookie('svc_action', 0);
            writeSessionCookie('svc_name', "");
            writeSessionCookie('svc_email', "");
            writeSessionCookie('locale', "");
            alert(retval);
        }
    }
}

function getHTTPObject(){
    // generic get HTTPobject
    if (window.XMLHttpRequest) return new XMLHttpRequest(); // try modern browser first
    else if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP"); // try old IE hack
    else { // sorry
        alert("Your browser does not support Web 2.0 rich internet applications.");
        return null;
    }
}

