 // Constructor -- pass a REST request URL to the constructor
   function JSONscriptRequest(fullUrl) {
        // REST request path
      this.fullUrl = fullUrl;
      // Keep IE from caching requests
      this.noCacheIE = '&noCacheIE=' + (new Date()).getTime();
      // Get the DOM location to put the script tag
      this.headLoc = document.getElementsByTagName("head").item(0);
      // Generate a unique script tag id
      this.scriptId = 'JscriptId' + JSONscriptRequest.scriptCounter++;
   }
        // Static script ID counter
   JSONscriptRequest.scriptCounter = 1;
   
   COMPONENT_COMMENTSPINNER = 1;
   COMPONENT_ACTIVESESSIONS = 2;
   COMPONENT_QUICKTHOUGHT = 3;
   COMPONENT_QUICKPOLL = 4;
   COMPONENT_MYIDEAS = 5;
   COMPONENT_TEXT = 6;
   
   function popUpWindow(window, intervalId) {
      this.window = window;
      this.intervalId = intervalId;
   	
   }
   
   var popupWindowList = new Array();
   
   
   // buildScriptTag method
   JSONscriptRequest.prototype.buildScriptTag = function () {
   // Create the script tag
      this.scriptObj = document.createElement("script");
      // Add script object attributes
      this.scriptObj.setAttribute("contentType", "text/javascript");
      this.scriptObj.setAttribute("type", "text/javascript");
      this.scriptObj.setAttribute("charset", "utf-8");
      this.scriptObj.setAttribute("src", this.fullUrl + this.noCacheIE);
      this.scriptObj.setAttribute("id", this.scriptId);
   }
   // removeScriptTag method
   JSONscriptRequest.prototype.removeScriptTag = function () {
      // Destroy the script tag
      this.headLoc.removeChild(this.scriptObj);
   }
   // addScriptTag method            
   JSONscriptRequest.prototype.addScriptTag = function () {
      // Create the script tag
      this.headLoc.appendChild(this.scriptObj);               
   }
   
   function replaceAmpersand(clearString) {
        var output =  clearString.replace("&", "<AMPERSAND>", "gi");       
        return output;
   }
   
   function URLEncode (clearString, escapeAmp) {
     var output = '';
     var x = 0;
    
     clearString = clearString.toString();
     if(escapeAmp != null && escapeAmp == 'true') {
         clearString = replaceAmpersand(clearString);
     }
     var regex = /(^[a-zA-Z0-9_.]*)/;
     while (x < clearString.length) {
       var match = regex.exec(clearString.substr(x));
       if (match != null && match.length > 1 && match[1] != '') {
         output += match[1];
         x += match[1].length;
       } else {
         if (clearString[x] == ' ')
           output += '+';
         else {
           var charCode = clearString.charCodeAt(x);
           var hexVal = charCode.toString(16);
           output += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();
         }
         x++;
       }
     }
     return output;
   }


   function queryWebService(destURL) {      
      var htmlContentReq = new JSONscriptRequest(destURL);       
      htmlContentReq.buildScriptTag();
      htmlContentReq.addScriptTag();
   }
     
   function updateJS(jsText) {

      // Create the script tag
      var scriptObj = document.createElement("script");
      // Add script object attributes
      scriptObj.setAttribute("contentType", "text/javascript");
      scriptObj.setAttribute("type", "text/javascript");
      scriptObj.setAttribute("charset", "utf-8");

      if(jsText !=null) {         
         appendScriptChild(scriptObj,jsText);
      }
      var headLoc = document.getElementsByTagName("head")[0];
      if(headLoc != null) {
         headLoc.appendChild(scriptObj);
      }
   }
     
   function updateCss(cssText) {

      // Create the script tag
      var cssObj = document.createElement("link");
      // Add script object attributes
      cssObj.setAttribute("rel", "stylesheet");
      cssObj.setAttribute("type", "text/css");
      cssObj.setAttribute("href", cssText);

      var headLoc = document.getElementsByTagName("head").item(0);
      if(headLoc != null) {
         headLoc.appendChild(cssObj);
      }
   }
     
     // IE hack for the adding child to script node    
   function appendScriptChild(node, text) {

      if (null == node.canHaveChildren || node.canHaveChildren) {
         node.appendChild(document.createTextNode(text));
      } else {
         node.text = text;
      }
   } 
      
   function updateCommentSpinner(jsonData) {
      updateComponent(jsonData, COMPONENT_COMMENTSPINNER);
   }
   
   function updateActiveSessions(jsonData) {
         updateComponent(jsonData, COMPONENT_ACTIVESESSIONS);
   }
   
   function updateMyIdeas(jsonData) {
      updateComponent(jsonData, COMPONENT_MYIDEAS);
   }    

   function updateQuickPoll(jsonData) {
      updateComponent(jsonData, COMPONENT_QUICKPOLL);
   }
   
   function updateTextComponent(jsonData) {
      updateComponent(jsonData, COMPONENT_TEXT);
   }
   
   function updateComponent(jsonData, componentType) {
      
      var htmlContent;
      var jsText;
      var cssText;

      if(jsonData != null){
         htmlContent = jsonData.htmlOutput;
         jsText = jsonData.scriptOutput;
         cssText=jsonData.cssOutput;
      }

      if(htmlContent != null) {     
         replaceComponentHtml(jsonData,componentType);           
      }

      if(jsText != null) {
         updateJS(jsText);
      }
      if(cssText != null) {
         updateCss(cssText);
      }
      
      // update the dependent components.
   } 


   function submitProject(destURL) {  

      var htmlContentReq = new JSONscriptRequest(destURL);
      htmlContentReq.buildScriptTag();
      htmlContentReq.addScriptTag();

   }
   
   function updateQuickThought(jsonData)  {
      updateComponent(jsonData, COMPONENT_QUICKTHOUGHT);
   
   }
   
   function changeQuickPollParameters(frameId) {

      var parametersQ = "&answer=";
      var inputArray = document.getElementsByTagName('input');
      var startChoicenumber = 0;
      var stChoiceFlag = true;
      var isFirstChecked = true;

      var searchStr = "qodQuestionComponent_";
      if(frameId != null && frameId.length > 0) {
         searchStr = searchStr + frameId;
      }

      for(i=0;i<inputArray.length;i++){
         var input= inputArray[i];
         if(input.name.indexOf(searchStr) >= 0) {
            if(input.name == searchStr+"0") {
               startChoicenumber = i;
            }
            else if(input.name == searchStr && stChoiceFlag) {
               startChoicenumber = i;
               stChoiceFlag = false;
            }
            if(input.checked) {
               if(input.type=="radio"){
                  parametersQ = parametersQ + (i-startChoicenumber);
                  break;
               } else if(input.type=="checkbox" && i >= startChoicenumber && isFirstChecked){
                  isFirstChecked = false;
                  parametersQ = parametersQ +(i-startChoicenumber);
               }
               else {
                  parametersQ = parametersQ + "," +(i-startChoicenumber);
               }
            }
         }
      }
      return parametersQ;
   }

   function submitQuickPollAnswer(destURL, frameId) {
      var parametersQ = changeQuickPollParameters(frameId);
      var htmlContentReq = new JSONscriptRequest(destURL+parametersQ);
      htmlContentReq.buildScriptTag();
      htmlContentReq.addScriptTag();
   }
   
	   function confirmation() 
	  {
		var answer = confirm("Registered user can only access the Servey ! please login...")
		if (answer)	{
	    window.location = "login.jspa";
		}
	 }
   
   function gatherQuickThoughtParameters(parameters, frameId) {

      var inputArray = document.getElementsByTagName('textarea');
      var startChoicenumber = 0;
      var stChoiceFlag = true;
      var isFirstChecked = true;
      var qtSearch = "quickThought_";
      var qtSurveySearch = "quickThoughtSurvey_";
      if(frameId != null && frameId.length > 0) {
         qtSearch = qtSearch  + frameId;
         qtSurveySearch = qtSurveySearch  + frameId; 
      }
      for(i=0;i<inputArray.length;i++){
         var input= inputArray[i];
         if(input.name ==qtSearch) {
            // var myJSONText = JSON.stringify(replaceAmpersand(input.value));
            var myJSONText = URLEncode(input.value);
            // alert( " END text : " + myJSONText);
            parameters = parameters+myJSONText;
         }
      }

      var inputArray = document.getElementsByTagName('select');
      for(i=0;i<inputArray.length;i++){
         var input= inputArray[i];
         if(input.name == qtSurveySearch ){
            parameters = parameters+",answer:["+input.selectedIndex+"]";
         }
      }
      parameters = parameters + "}" ;
      return parameters;
   }
      
   function submitAnswerQuickThought(destURL, frameId) {
      var parameters = "&answer={thought:"
      parameters = gatherQuickThoughtParameters(parameters, frameId);
      //changeParameterCatogorical();
      //alert("calling external " + destURL+parameters+"}");
      parameters = URLEncode(parameters);
      var htmlContentReq = new JSONscriptRequest(destURL+parameters);
      // alert("content " + htmlContentReq);
      htmlContentReq.buildScriptTag();
      htmlContentReq.addScriptTag();      
      resetMyideasWidget();
   }

   
     
     
   function replaceComponentHtml(jsonData, componentType){
      if (!document.getElementsByTagName) {
         return;
      }
      
      var nodeTobeReplaced;
      
      var htmlContent = jsonData.htmlOutput;
      var frameId = jsonData.frameId;
      
      arr_nodes = document.getElementsByTagName('div');
      
      if(componentType == COMPONENT_COMMENTSPINNER) {         
         nodeTobeReplaced = "satmetrix_comment_spinner";           
      } 
      else if(componentType == COMPONENT_ACTIVESESSIONS) {     
         nodeTobeReplaced = "satmetrix_active_sessions_";  
      }
      else if(componentType == COMPONENT_QUICKTHOUGHT) { 
         nodeTobeReplaced = "satmetrix_quickthought_";
      }
      else if(componentType == COMPONENT_QUICKPOLL) {     
         nodeTobeReplaced = "satmetrix_quickpoll_";
      }
      else if(componentType == COMPONENT_MYIDEAS) {     
         nodeTobeReplaced = "satmetrix_myideas_";
      }
      else if(componentType == COMPONENT_TEXT) {     
         nodeTobeReplaced = "satmetrix_text_component_";  
                    
      }  
      
      if(frameId != null && frameId.length > 0) {
         nodeTobeReplaced = nodeTobeReplaced  + frameId;
      }
      
      
      
      for (i = 0; i < arr_nodes.length; i++) {
         obj_node = arr_nodes[i];
         if (obj_node.id == nodeTobeReplaced) {
           
               var tn = document.createTextNode(htmlContent);
               obj_node.appendChild(tn);           
               obj_node.innerHTML=htmlContent;
            
         }
      }
   }   
   
   function resetMyideasWidget(){
      var webresourceid = "myideas_web_resource";
      var myideasDeployed = document.getElementById("myideas_deployed");
      if(myideasDeployed){
        arr_nodes = document.getElementsByTagName('input');
        for (i = 0; i < arr_nodes.length; i++) {
             obj_node = arr_nodes[i];
             if (obj_node.id == webresourceid) {           
                 var serviceurl = obj_node.value;
                 queryWebService(serviceurl);            
            }
         }
       }  
   }
   
   function startPopupTimer() {
		for(var i=0; i<popupWindowList.length; i++) {
			var value = popupWindowList[i];	
	   	if(value.window == null || value.window.closed ) {
	   		resetActiveSessionsWidget();
	   		clearInterval(value.intervalId);
	   		popupWindowList.splice(i,1);
	   	}
	   }  
   }      
   function resetActiveSessionsWidget(){
   	var webresourceid = "active_sessions_web_resource";
      arr_nodes = document.getElementsByTagName('input');
      for (i = 0; i < arr_nodes.length; i++) {
      	obj_node = arr_nodes[i];
         if (obj_node.id.indexOf(webresourceid) >= 0) {         
          	var serviceurl = obj_node.value;
           	queryWebService(serviceurl);            
         }
      }
   }   
     
   //targeting supportive script
   
   function applyResult(htmlContent,elementId){
        if (!document.getElementsByTagName) {
          return;
        }
        arr_nodes = document.getElementsByTagName('input');
        for (i = 0; i < arr_nodes.length; i++) {
          obj_node = arr_nodes[i];
          if (obj_node.id == elementId) { 
            alert(htmlContent);        
            obj_node.value=htmlContent;
          }
        }
      }
      
       function getSurveyQuestionResponse(jsonData) {
      
        var htmlContent;       
		if(jsonData != null){		     
			 htmlContent = jsonData.htmlOutput;	
         	 var domNode = jsonData.nodeToUpdate;         	 
			 // trim leading and trailing empty strings	
			 htmlContent = htmlContent.replace(/^\s+|\s+$/g,"");
			 addHiddenInputField(domNode, htmlContent);	
		
		 }
		 }
		 
		 function addHiddenInputField(fieldName, fieldValue) {
		 
		     var targetDiv = document.getElementById('satmetrix_target_values');
 		     var inputElement = document.createElement("input");
             inputElement.setAttribute("type", "hidden");
             inputElement.setAttribute("id", fieldName);
             inputElement.setAttribute("value", fieldValue);   
             targetDiv.appendChild(inputElement);
                 
            }
		 
	function getSessionCount(jsonData) {
      
        var htmlContent;       
		if(jsonData != null){		     
			 htmlContent = jsonData.htmlOutput;	
			 var domNode = jsonData.nodeToUpdate;       
			 // trim leading and trailing empty strings	
			 htmlContent = htmlContent.replace(/^\s+|\s+$/g,"");	
			 addHiddenInputField(domNode, htmlContent);			 		 
			 
		 }
		 }
    function isTaskCompleted(jsonData) {
      
        var htmlContent;       
		if(jsonData != null){		     
			 htmlContent = jsonData.htmlOutput;	
			 var domNode = jsonData.nodeToUpdate;       
			 // trim leading and trailing empty strings	
			 htmlContent = htmlContent.replace(/^\s+|\s+$/g,"");	
			 addHiddenInputField(domNode, htmlContent);			 		 
			 
		 }
		 }
		 
    function setDemographics(jsonData) {
      
        var htmlContent;       
		if(jsonData != null){		     
			 htmlContent = jsonData.htmlOutput;	
			 // trim leading and trailing empty strings	
			 htmlContent = htmlContent.replace(/^\s+|\s+$/g,"");			 		 
			 applyResult(htmlContent,"setDemographics");
		 }
		 }
	function setSurveyQuestionResponse(jsonData) {
      
        var htmlContent;       
		if(jsonData != null){		     
			 htmlContent = jsonData.htmlOutput;	
			 // trim leading and trailing empty strings	
			 htmlContent = htmlContent.replace(/^\s+|\s+$/g,"");			 		 
			 applyResult(htmlContent,"setSurveyQuestionResponse");
		 }
		 }
    