
var delimiter="\n"

function xml_escape(string) {
  //return string.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/\/g, "&quot;").replace(/'/g, "&#39;").replace(/"/g,"&quot;").replace(///g,"&#47;");
  return string.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/'/g, "&#39;").replace(/"/g,"&quot;");
}

function IsEmpty(aTextField) {
   if ((aTextField.value.length==0) ||
       (aTextField.value==null)) {
      return true;
   }
   else { return false; }
}

//----------------
// Date
//----------------

function date_getvalue(page, question, prompt)
{
   var ret="";
   for(var i=1; i<=prompt; i++)
   {
     if (!IsEmpty(document.getElementById("q_"+page+"_"+question+"_-"+i+"_month"))
      || !IsEmpty(document.getElementById("q_"+page+"_"+question+"_-"+i+"_day"))
      || !IsEmpty(document.getElementById("q_"+page+"_"+question+"_-"+i+"_year")) )
     {   
         //ret += page+","+question+",-"+i+",'"
              //+ document.getElementById("q_"+page+"_"+question+"_-"+i+"_month").value + "/"
              //+ document.getElementById("q_"+page+"_"+question+"_-"+i+"_day").value   + "/"
              //+ document.getElementById("q_"+page+"_"+question+"_-"+i+"_year").value 
              //+ "'" +delimiter;          
           ret += "<u pid=\"" + page + "\""
                  + " qid=\"" + question + "\""
                  + " qpid=\"-" + i + "\""
                  + " data=\"" + document.getElementById("q_"+page+"_"+question+"_-"+i+"_month").value + "/" 
                               + document.getElementById("q_"+page+"_"+question+"_-"+i+"_day").value + "/"
                               + document.getElementById("q_"+page+"_"+question+"_-"+i+"_year").value + "\""
                  + " />";
     }       
   }
   
   return ret;  
}

//----------------
// dropdownlist
//----------------

function dropdownlist_getvalue(page,question)
{
  var ret="";
  
  if (!IsEmpty(document.getElementById("q_"+page+"_"+question)))
  {
     //ret += page+","+question+",1,"+document.getElementById("q_"+page+"_"+question).value + delimiter;
       ret += "<b pid=\"" + page + "\""
              + " qid=\"" + question + "\""
              + " qpid=\"" + 1 + "\""
              + " qoid=\"" + document.getElementById("q_"+page+"_"+question).value + "\""
              + " />";
  }

  return ret;  
}

//----------------
// Comments
//----------------

function comments_getvalue(page, question)
{
  var ret="";
  
  if (!IsEmpty(document.getElementById("q_"+page+"_"+question)))
  {
      //ret += page+","+question+",-1,N'"+document.getElementById("q_"+page+"_"+question).value + "'" + delimiter;
        ret += "<u pid=\"" + page + "\""
               + " qid=\"" + question + "\""
               + " qpid=\"" + "-1" + "\""
               + " data=\"" + xml_escape(document.getElementById("q_"+page+"_"+question).value) + "\""
               + " />";
  }
   
  return ret;
}


//----------------
// Single_Line
//----------------

function single_line_getvalue(page, question, prompt)
{
   var ret="";
   for(var i=1; i<=prompt; i++)
   {
     if (!IsEmpty(document.getElementById("q_"+page+"_"+question+"_-"+i)))
     {   
         //ret += page+","+question+",-"+i+",N'"+document.getElementById("q_"+page+"_"+question+"_-"+i).value+ "'" +delimiter;          
           ret += "<u pid=\"" + page + "\""
                  + " qid=\"" + question + "\""
                  + " qpid=\"" + "-" + i  + "\""
                  + " data=\"" + xml_escape(document.getElementById("q_"+page+"_"+question+"_-"+i).value) + "\""
                  + " />";
     }       
   }
   
   return ret;  
}

//----------------
// Multi_choice
//----------------

function multi_choice_getvalue(page, question, prompt)
{
  var ret="";  
  for(var i=1; i<= prompt; i++)
  {        
     if (!IsEmpty(document.getElementById("q_"+page+"_"+question+"_"+i)))
     {   
         //ret += page+","+question+","+i+",1"+delimiter;         
           ret += "<b pid=\"" + page + "\""
                  + " qid=\"" + question + "\""
                  + " qpid=\"" + i + "\""
                  + " qoid=\"" + 1 + "\""
                  + " />";
     }         
  }    
  
  if (document.getElementById("q_"+page+"_"+question+"_-1") != null)
  {
     if (!IsEmpty(document.getElementById("q_"+page+"_"+question+"_-1")))
     {
         //ret += page+","+question+",-1,1"+delimiter;         
           ret += "<b pid=\"" + page + "\""
                  + " qid=\"" + question + "\""
                  + " qpid=\"" + -1 + "\""
                  + " qoid=\"" + 1  + "\""
                  + " />";
     }
     
     if (!IsEmpty(document.getElementById("q_"+page+"_"+question+"_open_ended")))
     {
         //ret +=page+","+question+",-1,N'"+document.getElementById("q_"+page+"_"+question+"_open_ended").value+"'" + delimiter;
           ret += "<u pid=\"" + page + "\""
                  + " qid=\"" + question + "\""
                  + " qpid=\"" + "-1"  + "\""
                  + " data=\"" + xml_escape(document.getElementById("q_"+page+"_"+question+"_open_ended").value) + "\""
                  + " />";
     }
  }
  
  
  return ret;
}

//--------------------
// Multi_choice_matrix
//--------------------

function multi_choice_matrix_getvalue(page, question, prompt, option)
{
  var ret=""
  
  for(var i=1; i<= prompt; i++)
  {     
     for(var j=1; j<=option; j++)
     {        
        if (!IsEmpty(document.getElementById("q_"+page+"_"+question+"_"+i+"_"+j)))
        {  
            //ret += page+","+question+","+ i + "," + j +delimiter;
              ret += "<b pid=\"" + page + "\""
                     + " qid=\"" + question + "\""
                     + " qpid=\"" + i + "\""
                     + " qoid=\"" + j + "\""
                     + " />";
            
        }         
     }   
  }
  
  return ret; 
  
}


//----------------
// single_choice
//----------------

function single_choice_getvalue(page,question,prompt)
{ 
  var ret=""
  if (!IsEmpty(document.getElementById("q_"+page+"_"+question+"_"+prompt)))
  {
     //ret=page+","+question+","+prompt+","+document.getElementById("q_"+page+"_"+question+"_"+prompt).value+delimiter;
       ret = "<b pid=\"" + page + "\""
             + " qid=\"" + question + "\""
             + " qpid=\"" + prompt + "\""
             + " qoid=\"" + document.getElementById("q_"+page+"_"+question+"_"+prompt).value + "\""
             + " />";
     
     if (document.getElementById("q_"+page+"_"+question+"_"+prompt).value < 0)
     {
        if (!IsEmpty(document.getElementById("q_"+page+"_"+question+"_open_ended")))
        {
           //ret +=page+","+question+",-1,N'"+document.getElementById("q_"+page+"_"+question+"_open_ended").value+"'"+delimiter;
             ret += "<u pid=\"" + page + "\""
                    + " qid=\"" + question + "\""
                    + " qpid=\"" + "-1"  + "\""
                    + " data=\"" + xml_escape(document.getElementById("q_"+page+"_"+question+"_open_ended").value) + "\""
                    + " />";
           
        }
     }
  }
 
  return ret;
}


//----------------------
// Single_choice_matrix
//----------------------

function single_choice_matrix_getvalue(page,question,prompt)
{
  var ret="";
  for(var i=1; i<=prompt; i++)
  { 
     if (!IsEmpty(document.getElementById("q_"+page+"_"+question+"_"+i)))
     {            
         //ret += page+","+question+","+i+","+document.getElementById("q_"+page+"_"+question+"_"+i).value+delimiter;
           ret += "<b pid=\"" + page + "\""
                  + " qid=\"" + question + "\""
                  + " qpid=\"" + i + "\""
                  + " qoid=\"" + document.getElementById("q_"+page+"_"+question+"_"+i).value + "\""
                  + " />";
     }  
  }
  
  return ret;
  
}

