#include "../global.h"
#include "../survey.h"
#include "shared_survey_string.h"
#include "shared_survey_util.h"
#include "shared_survey_path_util.h"
#include "shared_cs_util.h"

void replace_forbidden_str(char *str)
{
   int i,j, len;
   char *forbidden_str[3] = {SURVEY_CDATA_START, SURVEY_CDATA_END, "<![["};
   char *ptr;
  
   for(j =0; j<3; j++)
   {
    do
    {
      ptr = strstr(str, forbidden_str[j]);
      if(ptr)
      {
	len = strlen(forbidden_str[j]);
	for(i =0; i<len; i++)
           *(ptr + i) = ' ';
      }
     }while(ptr);
   }

}

SURVEY_DATA *
read_choice_parameters(int id )
{
#define MAX_TMP_NAME 40
   SURVEY_DATA *data;
   ELE_CHOICE *ptr;
   char *str =0,  name[MAX_TMP_NAME +1];
   int number, i;

   data = (SURVEY_DATA*)malloc(sizeof(SURVEY_DATA));
   data = initialize_survey_data(data);
   data->tag_name = malloc_str("choice");
   data->id = id;
   
   str = hdf_get_value(global_cgi->hdf, "Query.more", 0);
   if(str)
   {
     str = hdf_get_value(global_cgi->hdf, "Query.caption", 0);
     if(str)
     {
       replace_forbidden_str(str);
       data->caption=malloc_str(str);         /* shared_survey_string.c */
     }  
   }
   else
   {
     str = cs_survey_get_stringvalue("caption");  /* shared_cs_util.c */
     replace_forbidden_str(str);
     data->caption=malloc_str(str);         /* shared_survey_string.c */
     free(str);
   }

   str = hdf_get_value(global_cgi->hdf, "Query.multi", 0);
   
   if(str)
     data->steps_bgcolor = malloc_str(str); 
   else
     data->steps_bgcolor = malloc_str("no"); 

   number = hdf_get_int_value(global_cgi->hdf, "Query.number_choices", 0);
   
   for(i = 0; i<number; i++)
   {
      snprintf(name, MAX_TMP_NAME +1, "choice_%d", i);
      str = cs_survey_get_optvalue (name);
      if(str)
       {
          replace_forbidden_str(str);
          ptr = (ELE_CHOICE *)malloc(sizeof(ELE_CHOICE));
          ptr->choice = malloc_str(str);
	  free(str);
          ptr->next = 0;
         if(!data->head)
	  data->head = ptr;
         else 
   	   data->current->next = ptr;
         data->current = ptr;
       }
   }
   return data;
 #undef MAX_TMP_NAME
}

SURVEY_DATA *
read_custom_parameters(int id)
{
  SURVEY_DATA *data;
  char *str =0; 

  data = (SURVEY_DATA*)malloc(sizeof(SURVEY_DATA));
  data = initialize_survey_data(data);

  data->tag_name = malloc_str("custom");
  data->id = id;

   str = cs_survey_get_stringvalue("caption");  /* shared_cs_util.c */
   replace_forbidden_str(str);
   data->caption=malloc_str(str);         /* shared_survey_string.c */
   free(str);

  return data;
}

int
check_left_right(const char *str)
{
   int i, len;

   len = strlen(str);

   for(i = 0; i < len; i++)
   {
     if( (str[i] == '>')  || (str[i] == '<') )
	   return 0;
   }
   return 1;
}





SURVEY_DATA *
read_likert_parameters(int id)
{
  SURVEY_DATA *data;
  char *str =0;
  char *newstr =0;

  data = (SURVEY_DATA*)malloc(sizeof(SURVEY_DATA));
  data = initialize_survey_data(data);


  /** Prior to version 3, the next read:
  ***
  ***   data->tag_name = malloc_str("lickert");
  ***
  *** (that is, 'likert' was misspelled as 'lickert')
  *** This fix means that surveys created with this version
  *** of Manhattan WON'T WORK with versions of Manhattan prior
  *** to 3.0
  **/
  
  
  data->tag_name = malloc_str("likert");
  data->id = id;

   str = cs_survey_get_stringvalue("caption");  /* shared_cs_util.c */
   replace_forbidden_str(str);
   data->caption=malloc_str(str);         /* shared_survey_string.c */
   free(str);
   
   str = cs_survey_get_stringvalue("steps");
   data->steps_bgcolor = malloc_str(str);
   free(str);

   str = cs_survey_get_stringvalue("lefttag");  /* shared_cs_util.c */
   if(!check_left_right(str))
   {
     
      free(data);
      free(str);
      cs_critical_error(ERR_SURVEY_LIKERT_LEFT_TAG, "");
   }
   newstr = replace_sp(str);
   data->left_cols=malloc_str(newstr);         /* shared_survey_string.c */
   free(str);
   free(newstr);

   str = cs_survey_get_stringvalue("righttag");  /* shared_cs_util.c */
   if(!check_left_right(str))
   {
      free(data);
      free(str);
      cs_critical_error(ERR_SURVEY_LIKERT_RIGHT_TAG, "");
   }
   newstr = replace_sp(str);
   data->right_rows=malloc_str(newstr);         /* shared_survey_string.c */
   free(newstr);
   free(str);
   return data;
}








SURVEY_DATA *
read_text_parameters(int id)
{
  SURVEY_DATA *data;
  char *str =0;

  data = (SURVEY_DATA*)malloc(sizeof(SURVEY_DATA));
  data = initialize_survey_data(data);

  data->tag_name = malloc_str("text");
  data->id = id;

   str = cs_survey_get_stringvalue("caption");  /* shared_cs_util.c */
   replace_forbidden_str(str);
   data->caption=malloc_str(str);         /* shared_survey_string.c */
   free(str);

  return data;

}

SURVEY_DATA *
read_memo_parameters(int id)
{
   SURVEY_DATA *data;
   char *str =0;

   data = (SURVEY_DATA*)malloc(sizeof(SURVEY_DATA));
   data = initialize_survey_data(data);

   data->tag_name = malloc_str("memo");
   data->id = id;

   str = cs_survey_get_stringvalue("caption");  /* shared_cs_util.c */
   replace_forbidden_str(str);
   data->caption=malloc_str(str);         /* shared_survey_string.c */
   free(str);

   str = cs_survey_get_stringvalue("cols");  /* shared_cs_util.c */
   data->left_cols=malloc_str(str);         /* shared_survey_string.c */
   free(str);
   str = cs_survey_get_stringvalue("rows");  /* shared_cs_util.c */
   data->right_rows=malloc_str(str);         /* shared_survey_string.c */
   free(str);

   return data;
}

void
read_com_parameters(int *is_admin, char *key,  char *count, int *pre_id)
{

    char tmp[12];

     cs_get_required_parameter("id", key, MAX_KEY);
     cs_get_required_parameter("count", count, MAX_PATH);
     cs_get_required_parameter("pre_id", tmp, 11);
     *pre_id = atoi(tmp);

     cs_get_optional_parameter("admin", tmp, 11);
     *is_admin = !(*tmp)? 0 : atoi(tmp);
}






int check_bad_char(const char *str)
{
   int i, len = strlen(str);
   for(i =0; i<len; i++)
   {
       if(str[i] == '\"' || str[i] == '\'' || str[i] == '>' 
	 || str[i] == '<' || str[i] == '&')
	       return 0;
   }
   return 1;
}

SURVEY_DATA *
read_title_parameters(int id)
{
  
  SURVEY_DATA *data;
  char *str =0;

  data = (SURVEY_DATA*)malloc(sizeof(SURVEY_DATA));
  data = initialize_survey_data(data);

  data->tag_name = malloc_str(SURVEY_TITLE_TAG);
  data->id = id;

   str = cs_survey_get_stringvalue("link");  /* shared_cs_util.c */
   if(!check_bad_char(str))
	cs_critical_error(ERR_HAS_BAD_CHAR, "link");
   data->left_cols=malloc_str(str);         /* shared_survey_string.c */
   free(str);

   str = cs_survey_get_stringvalue("title");
   replace_forbidden_str(str);
   data->caption=malloc_str(str);
   free(str);

   str = cs_survey_get_stringvalue("color");
   data->steps_bgcolor = malloc_str(str);
   free(str);

   return data;

}

