/* Use an immediately invoked function to isolate widget variables */ (function(){ var countScriptCalled = 0; var _Hostname = "//comment-app.hhs.gov"; /* main - Main function for the voc widget. This function is called once * the VOC library has been loaded. */ function main() { var changeTimer, changeInterval = 5000, lastSubmitted; /* create the voc object */ VOC.Survey = Survey; VOC.HOSTNAME = "//comment-app.hhs.gov"; var survey = new VOC.Survey(124, "survey_target"); survey.loadSurvey(); } function Survey(surveyId, targetId) { var _this = this; _this.targetId = targetId; _this.surveyId = surveyId; _this.pageURL = document.URL; _this.surveyContainer = document.getElementById(_this.targetId); /* * Load the survey into the target container */ _this.loadSurvey = function() { var surveyUrl = _Hostname + '/surveys/' + _this.surveyId + '.json?page_url=' + _this.pageURL; VOC.fn.getJSON(surveyUrl, function(data) { _this.surveyContainer.innerHTML = data.html; bindSubmitForm(); VOC.fn.bindPaginationLinks(_this.surveyContainer); var focus = ""; if(focus == "true") VOC.fn.focusFirstElementIn(_this.surveyContainer); }); }; /* Create a hidden iFrame to post to in order to avoid cross-domain * POST limitations. */ _this.postToIFrame = function(form) { var uniqueString = "surveyPostContainerIframe" + _this.targetId; var hiddenIFrame = document.createElement("iframe"); hiddenIFrame.height = 0; hiddenIFrame.width = 0; hiddenIFrame.tabindex = -1; hiddenIFrame.title = 'empty'; hiddenIFrame.style.display = 'none'; document.body.appendChild(hiddenIFrame); /* Direct the post into the iframe's window */ hiddenIFrame.contentWindow.name = uniqueString; form.setAttribute("target", uniqueString); form.setAttribute("action", _Hostname + "/survey_responses"); form.submit(); /* Load the ThankYou page */ loadThankYouPage(); }; /* * Private function to fetch the thank you page and replace the survey * contents with the contents of the thank you page. */ function loadThankYouPage() { var _thankYouURL = _Hostname + "/surveys/" + _this.surveyId + "/thank_you_page.json"; VOC.fn.getJSON(_thankYouURL, function(data) { _this.surveyContainer.innerHTML = data.html; }); }; /* * Bind the submit button from the survey to run our custom submit. */ function bindSubmitForm() { var _responseForm = _this.surveyContainer.querySelector('form'); var pageNumber = _this.surveyContainer.querySelector(".current_page").id.split('_')[1]; VOC.fn.addEventListener(_responseForm, "submit", function(e) { if(VOC.fn.validateBeforeSubmit(e)) _this.postToIFrame(_responseForm); if(e.preventDefault) e.preventDefault(); e.returnValue = false; return false; }); }; }; /* Called once the VOC library has been loaded */ function scriptLoadHandler(){ countScriptCalled++; if(countScriptCalled > 1) return; var interval = setInterval(function(){ if(VOC.fn !== undefined) { clearInterval(interval); main(); } }, 100); } /* Load the VOC library helper functions */ function loadLibraryFunctions() { var script_tag = document.createElement('script'); script_tag.setAttribute('type', 'text/javascript'); script_tag.setAttribute('src', "//comment-app.hhs.gov/assets/voc.fn.js"); /* Wait for the script to load */ //Other browsers if(script_tag.addEventListener) { script_tag.addEventListener("load", scriptLoadHandler, false); } // For Old versions of IE else if(script_tag.readyState) { script_tag.onreadystatechange = scriptLoadHandler; } // Try to find the head, otherwise default to the documentElement (document.getElementsByTagName("head")[0] || document.documentElement) .appendChild(script_tag); } function completed() { document.removeEventListener("DOMContentLoaded", completed); window.removeEventListener("load", completed); loadLibraryFunctions(); } /* Handle the case where we don't execute this code until after the DOM has * finished loading. */ if(document.readyState === "complete" || (document.readyState !== "loading" && !document.documentElement.doScroll) ){ window.setTimeout(loadLibraryFunctions()); } else { document.addEventListener("DOMContentLoaded", completed); window.addEventListener("load", completed); } })();