﻿//*** Survey.js
//*** Code to handle survey/user interaction via javascript and AJAX.

//*** Survey overide flag.  If true, survey will display at page load.
var ENABLESURVEY_OVERRIDE = false;

//*** Checks survey override flag and attempts to force survey display.
function ProcessSurveyOverride() {
    if (ENABLESURVEY_OVERRIDE) {
        if (window.DisplaySurvey) { DisplaySurvey(); }
        ENABLESURVEY_OVERRIDE = false;
    }
}

//*** JavaScript function to display survey panel.
function DisplaySurvey() {
    var SurveyObj = $find("jsSurveyObjBehavior");
    if (SurveyObj != null) {
        SurveyObj.show();
    }
}

//*** Function to gather survey data and submit/post survey to database using AJAX call.
function SubmitSurvey() {
    var frm = document.forms[0];
    var str = "";
    var FormPost = "";
    for(var i=0; i<frm.elements.length; i++){
        var typ = frm.elements[i].type;
        var nam = frm.elements[i].name;
        if (nam.indexOf("question") > -1) {
            if (typ == "radio") {
                if (frm.elements[i].checked) {
					FormPost += nam.substring(nam.indexOf("question")) + "=" + escape(frm.elements[i].value) + "&";
					//>> See if this field has associated field
					if(frm.elements[i].getAttribute("other") != null) {
						FormPost += nam.substring(nam.indexOf("question")) + "_OTH=" + escape(document.getElementById(frm.elements[i].getAttribute("other")).value) + "&";
					}
                }
            }
            else if (typ == "textarea") {
				//>> Be sure this field isn't associated with a radio button
				if(frm.elements[i].getAttribute("radio") == null) {
					FormPost += nam.substring(nam.indexOf("question")) + "=" + escape(frm.elements[i].value) + "&";        
				}
            }
            else {
                alert("SubmitSurvey Error >>>\n\n Type not supported (" + frm.elements[i].type  + ").");
            }
        }
    }
    if (FormPost.length > 0) {
        FormPost = FormPost.substring(0,FormPost.length-1);
        try {
            if (surveyEmail.length > 0) {
                FormPost = FormPost + "&email=" + surveyEmail;
            }
        }
        catch(err) {
            alert(err.description);
        }
        var wRequest = new Sys.Net.WebRequest();
        wRequest.set_url("/Common/Survey/SurveyPostHandler.aspx?GRPID=" + jsSurveyGroupID);
        wRequest.set_httpVerb("POST");
        wRequest.set_body(FormPost);
        wRequest.get_headers()["Content-Length"] = FormPost.length;
        wRequest.add_completed(OnSurveySubmitCompleted);
        wRequest.invoke();
    } else {
        alert("SubmitSurvey Error >>>\n\nSurvey questions not answered.");
    }
}

//*** Function which executes in the Submit Survey process once the report survey is successfully 
//*** saved to the database.  Executes the cancel survey function to clear force survey check.
function OnSurveySubmitCompleted(executor, eventArgs) {
    if(executor.get_responseAvailable()) {
        var resp = executor.get_responseData();
        if (resp.indexOf("SUCCESS") > -1) {
            CancelSurvey();
       }
        else {
            alert("SubmitSurvey Error (AJAX) >>>\n\n " + resp + ".");
        }
    }
    else {
        if (executor.get_timedOut())
            alert("SubmitSurvey Error (AJAX) >>>\n\nOperation Timed Out.");
        else
            if (executor.get_aborted())
                alert("SubmitSurvey Error (AJAX) >>>\n\nOperation Aborted.");
    }
}

//*** Function to cancel survey display and clear session logic (if applicable).
function CancelSurvey() {
    var SurveyObj = $find("jsSurveyObjBehavior");
    if (SurveyObj != null) {
        var wRequest = new Sys.Net.WebRequest();
        wRequest.set_url("/Common/Survey/SurveyCancelHandler.aspx?GRPID=" + jsSurveyGroupID);
        wRequest.set_httpVerb("POST");
        wRequest.set_body("");
        wRequest.get_headers()["Content-Length"] = 0;
        wRequest.add_completed(OnSurveyCancelCompleted);
        wRequest.invoke();  
    }
}

//*** Function which executes in the Cancel Survey process once the survey is successfully 
//*** cancelled in the business logic.  Hides the survey when complete.
function OnSurveyCancelCompleted(executor, eventArgs) {
    if(executor.get_responseAvailable()) {
        var resp = executor.get_responseData();
        if (resp.indexOf("SUCCESS") > -1) {
            var SurveyObj = $find("jsSurveyObjBehavior");
            if (SurveyObj != null) { SurveyObj.hide(); }
       }
        else {
            alert("CancelSurvey Error (AJAX) >>>\n\n " + resp + ".");
        }
    }
    else {
        if (executor.get_timedOut())
            alert("CancelSurvey Error (AJAX) >>>\n\nOperation Timed Out.");
        else
            if (executor.get_aborted())
                alert("CancelSurvey Error (AJAX) >>>\n\nOperation Aborted.");
    }
}

//*** Notify the AJAX system this script is loaded.  
if (typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();

