create or replace PACKAGE BODY call_game_play
IS
-------------------------------------------------------
PROCEDURE test_game
IS
CURSOR eo_cur
IS
  SELECT user_id,first_name,last_name,call_game_dept_id
    FROM user_int_v
   WHERE acc_account_id = sess.acct_id
     AND nvl(call_game_dept_id,0) > 0
     --AND nvl(show_escrow,'N') = 'Y'
   ORDER BY first_name,last_name;

BEGIN
  set_hdr('Test Game');
  wl('<body>');
  wl('<h2><center>Test Game</center></h2>');
  
  wl('<a href="/prv/call_game.filter_matrix" target="_blank">Filter Matrix</a><br><br>');
  wl('<script>
        function chooseUser(valP){
          location="/prv/call_game_play.main_game?user_p="+valP; 
        }
      </script>');
  wl('<select name="user_p" id="user_p" onchange="chooseUser(this.value);">');
  wl('<option value="0">Choose User</option>');
  FOR e IN eo_cur LOOP
    wl('<option value="'||e.user_id||'">'||e.first_name||' '||e.last_name||' - '||call_game.get_dept_name(e.call_game_dept_id)||'</option>');
  END LOOP;
  wl('</select>');
  wl('<br><br>');

  wl('</body>');
  wl('</html>');
END;
-------------------------------------------------------
FUNCTION cnt_qst_by_user_type(user_p NUMBER,type_p NUMBER) RETURN NUMBER
IS
level_n NUMBER:=0;
CURSOR usr_cur
IS
  SELECT *
    FROM user_int_v
   WHERE user_id = user_p;
u usr_cur%ROWTYPE;
CURSOR quest_cur(dept_p NUMBER,lev_n NUMBER)
IS
  SELECT q.*,call_game.get_level_num(q.min_level) min_lev,call_game.get_level_num(q.max_level) max_lev
    FROM call_game_quests q
   WHERE sub_quest_type_id IN (SELECT sub_id FROM call_quest_sub_types WHERE sub_tp_id = type_p)
     AND quest_id IN (SELECT quest_id FROM call_game_quest_asg_dept WHERE dept_id = dept_p)
     --AND min_level = lev_n
     AND nvl(validated,0) = 1
   ORDER BY min_lev,max_lev;
   
CURSOR all_quest_cur(dept_p NUMBER,lev_n NUMBER)
IS
  SELECT q.*,call_game.get_level_num(q.min_level) min_lev,call_game.get_level_num(q.max_level) max_lev
    FROM call_game_quests q
   WHERE quest_id IN (SELECT quest_id FROM call_game_quest_asg_dept WHERE dept_id = dept_p)
     --AND min_level = lev_n
     AND nvl(validated,0) = 1
   ORDER BY min_lev,max_lev;
cnt NUMBER:=0;
BEGIN
  OPEN usr_cur;
  FETCH usr_cur INTO u;
  CLOSE usr_cur;
  level_n := call_game.get_user_level_id(user_p);
  IF type_P = 4 THEN
    FOR a IN all_quest_cur(u.call_game_dept_id,level_n) LOOP
      cnt := cnt + 1;
    END LOOP;
  ELSE
    FOR q IN quest_cur(u.call_game_dept_id,level_n) LOOP
      cnt := cnt + 1;
    END LOOP;
  END IF;
  IF trunc(sysdate) = to_date('01-FEB-18') AND type_p = 3 THEN
    cnt := 1;
  END IF;
  RETURN cnt;
END;
-------------------------------------------------------
FUNCTION is_quest_type_done(user_p NUMBER,type_p NUMBER) RETURN BOOLEAN
IS
CURSOR usr_qst_cur
IS
  SELECT *
    FROM game_user_quests
   WHERE user_id = user_p
     AND trunc(assign_dt) = trunc(sysdate)
     AND quest_id IN (SELECT quest_id 
                        FROM call_game_quests gq
                       WHERE gq.sub_quest_type_id IN (SELECT st.sub_id FROM call_quest_sub_types st WHERE st.sub_tp_id = type_p));
uq usr_qst_cur%ROWTYPE;
ret_b BOOLEAN:=FALSE;
BEGIN
  OPEN usr_qst_cur;
  FETCH usr_qst_cur INTO uq;
  IF usr_qst_cur%FOUND THEN
    ret_b := TRUE;
  END IF;
  CLOSE usr_qst_cur;
  IF type_p = 2 THEN
    ret_b := FALSE;
  END IF;
  IF trunc(sysdate) = to_date('02-FEB-18') AND type_p = 3 THEN
    ret_b := FALSE;
  END IF;

  RETURN ret_b;
END;
-------------------------------------------------------
FUNCTION is_quest_type_complete(user_p NUMBER,type_p NUMBER) RETURN BOOLEAN
IS
CURSOR usr_qst_cur
IS
  SELECT *
    FROM game_user_quests
   WHERE user_id = user_p
     AND trunc(assign_dt) = trunc(sysdate)
     AND complete_dt IS NOT NULL
     AND quest_id IN (SELECT quest_id 
                        FROM call_game_quests gq
                       WHERE gq.sub_quest_type_id IN (SELECT st.sub_id FROM call_quest_sub_types st WHERE st.sub_tp_id = type_p));
uq usr_qst_cur%ROWTYPE;
ret_b BOOLEAN:=FALSE;
BEGIN
  OPEN usr_qst_cur;
  FETCH usr_qst_cur INTO uq;
  IF usr_qst_cur%FOUND THEN
    ret_b := TRUE;
  END IF;
  CLOSE usr_qst_cur;
  RETURN ret_b;
END;

-------------------------------------------------------
PROCEDURE choose_quest_type(user_p NUMBER)
IS
level_n NUMBER:=1;
CURSOR type_cur
IS
  SELECT *
    FROM call_quest_types t
   ORDER BY type_name;
done_cnt NUMBER:=0;

out_cnt NUMBER:=0;

CURSOR vote_cur
IS
  SELECT count(1)
    FROM user_idea_vote
   WHERE quest_id = 402
     AND user_id = user_p;
v_cnt NUMBER:=0;

CURSOR usr_qst_seq_cur
IS
  SELECT *
    FROM call_game_user_qst_seq
   WHERE user_id = user_p
     AND complete_dt IS NULL;
uqs usr_qst_seq_cur%ROWTYPE;

uq_e EXCEPTION;
uq_n NUMBER;

nm_v VARCHAR2(500);
CURSOR grp_cur(id_p NUMBER)
IS 
  SELECT group_name
    FROM call_game_quest_grp
   WHERE grp_id = id_p;

BEGIN
  OPEN vote_cur;
  FETCH vote_cur INTO v_cnt;
  CLOSE vote_cur;
  FOR t IN type_cur LOOP
    IF call_game_play.is_quest_type_done(user_p,t.tp_id) THEN
      done_cnt := done_cnt + 1;
    END IF;
  END LOOP;
  
  OPEN usr_qst_seq_cur;
  FETCH usr_qst_seq_cur INTO uqs;
  IF usr_qst_seq_cur%FOUND THEN
    uq_n := uqs.qst_seq_id;
    OPEN grp_cur(uqs.grp_id);
    FETCH grp_cur INTO nm_v;
    CLOSE grp_cur;
    --nm_v := call_game.get_quest_name(uqs.);
    --RAISE uq_e;
  END IF;
  CLOSE usr_qst_seq_cur;
wl('
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset=''UTF-8''>

  <title>'||get_user_name(user_p)||' Game</title>  
        <style>
            body {
                font-family: Comic Sans MS,  sans-serif;
                font-size:19pt;
                background-color: #ffffff;
                background-image: url("https://rightdata.driggstitle.com/callGame/Kwest_icons/Kwest_hub_background.png");
            }
        </style>
</head>');
wl('<body>');
call_game.disable_back;
  level_n := call_game.get_user_level_id(user_p);
  --wl('<h3><center>Game for '||get_user_name(user_p)||'</center></h3>');
  wl('<center><img src="//rightdata.driggstitle.com/callGame/Kwest_icons/Choose Kwest Type.png" width="578px" height="65px"></center>');
  wl('<p id="doneP" style="display:none" onclick="goPast();">Repeat a Kwest for more points by clicking the PTS. icon above</p>');
  wl('<script>function chgType(valP){
                if (valP == 4){
                  location="/prv/call_game_play.asg_surprise?user_p='||user_p||'"; 
                }else{
                  location="/prv/call_game_play.choose_quest?user_p='||user_p||'&type_p="+valP;
                }
              }
              
              function goPast(){
                location="/prv/call_game_play.show_quest_hist?user_p='||user_p||'";
              }
      </script>');
  --wl('<div style="margin-left:350px">');
  --wl('<p style="margin-left:100px;">');
  FOR t IN type_cur LOOP
    t.quest_type_icon := replace(t.quest_type_icon,'http:','https:');
    --wl(t.tp_id||' '||t.type_name||' '||call_game_play.cnt_qst_by_user_type(user_p,t.tp_id)||'<br>');
    IF nvl(call_game_play.cnt_qst_by_user_type(user_p,t.tp_id),0) >= 1 THEN
      out_cnt := out_cnt + 1;
      IF t.tp_id = 4 THEN
        
        IF done_cnt < 2 AND nvl(uq_n,0) = 0 THEN
          wl('<p style="cursor:pointer" onclick="chgType('||t.tp_id||');">');
          wl('<img src="'||t.quest_type_icon||'" width="30px" height="30px" onclick="chgType('||t.tp_id||');" style="color:blue;cursor:pointer;" >');
          wl(t.type_name);
          --wl(done_cnt);
          wl('</p>');
        ELSE
          wl('<p style="cursor:pointer;color:gray;" onclick="">');
          wl('<img src="'||t.quest_type_icon||'" width="30px" height="30px" onclick="chgType('||t.tp_id||');" style="cursor:pointer;filter: grayscale(100%);" >');
          wl(t.type_name);
          --wl(done_cnt);
          wl('</p>');
          
        END IF;
        --sftp://oracle@ltfs2283.aus.us.siteprotect.com/oracle/Middleware/webtier_home/user_projects/domains/base_domain/config/fmwconfig/components/OHS/instances/ohs1/htdocs/callGame/Kwest_icons/Kwest_type_icon_Suprise.png
      ELSE
        IF call_game_play.is_quest_type_done(user_p,t.tp_id) = FALSE AND nvl(uq_n,0) = 0 THEN
          wl('<p style="cursor:pointer" onclick="chgType('||t.tp_id||');">');
          --wl('<img src="https://rightdata.driggstitle.com/callGame/Kwest_icons/Gold_Heart.png" width="30px" height="30px" onclick="chgType('||t.tp_id||');" style="color:blue;cursor:pointer;" >');
          wl('<img src="'||t.quest_type_icon||'" width="30px" height="30px" onclick="chgType('||t.tp_id||');" style="color:blue;cursor:pointer;" >');
          wl(t.type_name);
          wl('</p>');
        ELSE
          IF 1=2 THEN
            wl('<p style="cursor:pointer" onclick="chgType('||t.tp_id||');">');
          ELSE
            wl('<p style="cursor:pointer;color:gray;" onclick="goPast();">');
          END IF;
          --wl('<img src="https://rightdata.driggstitle.com/callGame/Kwest_icons/Gold_Heart.png" width="30px" height="30px" onclick="chgType('||t.tp_id||');" style="color:blue;cursor:pointer;" >');
          wl('<img src="'||t.quest_type_icon||'" width="30px" height="30px" onclick="" style="cursor:pointer;filter: grayscale(100%);" >');
          wl(t.type_name);
          
          IF call_game_play.is_quest_type_complete(user_p,t.tp_id) = FALSE THEN
            wl(' (Kwest open but not complete.  View open quests above)');
          ELSE
            wl(' (Kwest completed for today)');
          END IF;
          wl('</p>');
          
        END IF;
        --wl('<option value="'||t.tp_id||'">'||t.type_name||' '||call_game_play.cnt_qst_by_user_type(user_p,t.tp_id)||'</option>');
        null;
      END IF;
    END IF;
  END LOOP;
  
  IF nvl(uq_n,0) > 0 THEN
    wl('<script>function doSeq(){
                  location="/prv/call_game_play.do_user_qst_seq?user_p='||user_p||'&qst_seq_p='||uq_n||'";
                }
        </script>');
    wl('<script>
    
        </script>');
    wl('<p style="cursor:pointer" onclick="doSeq();">');
    wl('<img src="http://rightdata.driggstitle.com/callGame/Kwest_type_icon_Suprise.png" width="30px" height="30px" onclick="doSeq();" >');
    wl('Continue '||nm_v);
    wl('</p>');
  END IF;
  IF done_cnt > 1 THEN
    wl('<script> var pt = document.getElementById("doneP");
                 pt.style.display = "inline";
        </script>');
  END IF;
  /*
  IF out_cnt = 0 THEN
    wl('<center>Kwests Coming soon.</center>');
  END IF;
  */
/*  
  wl('<select name="type_p" id="type_p" onchange="chgType(this.value);">');
  wl('<option value="0">Choose Quest Type</option>');
  FOR t IN type_cur LOOP
    IF nvl(call_game_play.cnt_qst_by_user_type(user_p,t.tp_id),0) >= 1 THEN
      IF t.tp_id = 4 THEN
        wl('<option value="'||t.tp_id||'">'||t.type_name||'</option>');
      ELSE
        wl('<option value="'||t.tp_id||'">'||t.type_name||' '||call_game_play.cnt_qst_by_user_type(user_p,t.tp_id)||'</option>');
      END IF;
    END IF;
  END LOOP;
  wl('</select>');
 */ 
EXCEPTION
  WHEN uq_e THEN
    wl('<script>location="/prv/call_game_play.do_user_qst_seq?user_p='||user_p||'&qst_seq_p='||uq_n||'";</script>');
END;
--FUNCTION cnt_qst_by_user_type(user_p NUMBER,type_p NUMBER) RETURN NUMBER
-------------------------------------------------------
PROCEDURE do_user_qst_seq(user_p NUMBER,qst_seq_p NUMBER)
IS
CURSOR usr_cur
IS
  SELECT *
    FROM user_int_v
   WHERE user_id = user_p;
u usr_cur%ROWTYPE;

CURSOR seq_cur
IS
  SELECT *
    FROM call_game_user_qst_seq
   WHERE qst_seq_id = qst_seq_p;
s seq_cur%ROWTYPE;

CURSOR dtl_cur(grp_p NUMBER)
IS
  SELECT *
    FROM call_game_quest_grp_dtl
   WHERE grp_id = grp_p
   ORDER BY item_order;

CURSOR gm_usr_qsts_cur(qst_p NUMBER,dt DATE)
IS
  SELECT *
    FROM game_user_quests
   WHERE user_id = user_p
     AND quest_id = qst_p
     AND assign_dt > dt;
g gm_usr_qsts_cur%ROWTYPE;

asg_qst_n NUMBER:=-1;
go_usr_qst_n NUMBER:=-1;

asg_new_qst_e EXCEPTION;
go_exist_qst_e EXCEPTION;
BEGIN
  OPEN usr_cur;
  FETCH usr_cur INTO u;
  CLOSE usr_cur;  
  
  OPEN seq_cur;
  FETCH seq_cur INTO s;
  CLOSE seq_cur;
  
  FOR d IN dtl_cur(s.grp_id) LOOP
    OPEN gm_usr_qsts_cur(d.qst_id,s.assign_dt);
    FETCH gm_usr_qsts_cur INTO g;
    IF gm_usr_qsts_cur%NOTFOUND THEN
      asg_qst_n := d.qst_id;
      RAISE asg_new_qst_e;
    ELSE
      IF g.complete_dt IS NULL THEN
        go_usr_qst_n := g.usr_qst_id;
        RAISE go_exist_qst_e;
      END IF;
    END IF;
    CLOSE gm_usr_qsts_cur;
  END LOOP;

EXCEPTION
  WHEN asg_new_qst_e THEN
    wl('<script>location="/prv/call_game_play.assign_quest_to_usr?user_p='||user_p||'&quest_p='||asg_qst_n||'";</script>');
  WHEN go_exist_qst_e THEN
    wl('<script>location="/prv/call_game_play.get_quest_screen?gq_p='||go_usr_qst_n||'";</script>');
END;
--PROCEDURE assign_quest_to_usr(user_p NUMBER,quest_p NUMBER)
--/prv/call_game_play.get_quest_screen?gq_p=
-------------------------------------------------------
PROCEDURE check_grp_qst_complete(user_p NUMBER)
IS
CURSOR usr_qst_seq_cur
IS
  SELECT *
    FROM call_game_user_qst_seq
   WHERE user_id = user_p
     AND complete_dt IS NULL;
uqs usr_qst_seq_cur%ROWTYPE;

uq_e EXCEPTION;
uq_n NUMBER;

CURSOR dtl_cur(grp_p NUMBER)
IS
  SELECT *
    FROM call_game_quest_grp_dtl
   WHERE grp_id = grp_p
   ORDER BY item_order;
   
CURSOR gm_usr_qsts_cur(qst_p NUMBER,dt DATE)
IS
  SELECT *
    FROM game_user_quests
   WHERE user_id = user_p
     AND quest_id = qst_p
     AND assign_dt > dt
     AND complete_dt IS NOT NULL;
g gm_usr_qsts_cur%ROWTYPE;

group_done NUMBER:=1;

CURSOR hdr_cur(grp_p NUMBER)
IS
  SELECT *
    FROM call_game_quest_grp
   WHERE grp_id = grp_p;
h hdr_cur%ROWTYPE;

cont_n NUMBER;

CURSOR usr_cur
IS
  SELECT nvl(user_cont_id,0) 
    FROM ed_users
   WHERE user_id = user_p;
BEGIN
  
  OPEN usr_qst_seq_cur;
  FETCH usr_qst_seq_cur INTO uqs;
  IF usr_qst_seq_cur%FOUND THEN
    uq_n := uqs.qst_seq_id;
    FOR d IN dtl_cur(uqs.grp_id) LOOP
      OPEN gm_usr_qsts_cur(d.qst_id,uqs.assign_dt);
      FETCH gm_usr_qsts_cur INTO g;
      IF gm_usr_qsts_cur%NOTFOUND THEN
        group_done := 0;
      END IF;
      CLOSE gm_usr_qsts_cur;
    END LOOP;
    IF group_done = 1 THEN
      UPDATE call_game_user_qst_seq
         SET complete_dt = sysdate
       WHERE qst_seq_id = uqs.qst_seq_id;
      COMMIT;
      OPEN hdr_cur(uqs.grp_id);
      FETCH hdr_cur INTO h;
      CLOSE hdr_cur;
      IF nvl(h.def_new_pass,'N') = 'Y' THEN
        OPEN usr_cur;
        FETCH usr_cur INTO cont_n;
        CLOSE usr_cur;
        IF nvl(cont_n,0) > 0 THEN
          UPDATE cli_branch_contacts
             SET pass_cert_cplt = sysdate
           WHERE cont_id = cont_n
             AND pass_cert_cplt = to_date('01-JAN-01');
        ELSE
          UPDATE ed_users
             SET rep_cfg_dt = sysdate
           WHERE user_id = user_p
             AND rep_cfg_dt = to_date('01-JAN-01');
        END IF;
      END IF;
    END IF;
  END IF;
  CLOSE usr_qst_seq_cur;
  
END;
-------------------------------------------------------
PROCEDURE asg_surprise(user_p NUMBER)
IS
level_n NUMBER:=1;
CURSOR usr_cur
IS
  SELECT *
    FROM user_int_v
   WHERE user_id = user_p;
u usr_cur%ROWTYPE;

CURSOR quest_cur(dept_p NUMBER,lev_p NUMBER)
IS
  SELECT q.*,call_game.get_level_num(q.min_level) min_lev,call_game.get_level_num(q.max_level) max_lev
    FROM call_game_quests q
   WHERE quest_id IN (SELECT quest_id FROM call_game_quest_asg_dept WHERE dept_id = dept_p)
     AND quest_id NOT IN (SELECT quest_id FROM game_user_quests WHERE user_id = user_p AND complete_dt IS NOT NULL)
     AND q.min_level = lev_p
     AND nvl(validated,0) = 1
   ORDER BY min_lev,max_lev,quest_name;
cnt NUMBER:=0;
cnter NUMBER:=0;
rand_n NUMBER;
qst_n NUMBER;
BEGIN
  OPEN usr_cur;
  FETCH usr_cur INTO u;
  CLOSE usr_cur;
  level_n := call_game.get_user_level_id(user_p);
  FOR q IN quest_cur(u.call_game_dept_id,level_n) LOOP
    cnt := cnt + 1;
  END LOOP;
  rand_n := ceil(dbms_random.value(1,cnt));
  wl(level_n||' in surprise '||user_p||'   '||cnt||'   '||rand_n);
  FOR q IN quest_cur(u.call_game_dept_id,level_n) LOOP
    cnter := cnter + 1;
    IF cnter = rand_n THEN
      qst_n := q.quest_id;
    END IF;
  END LOOP;
  wl('<script>location="/prv/call_game_play.assign_quest_to_usr?user_p='||user_p||'&quest_p='||qst_n||'";</script>');
END;
-------------------------------------------------------
PROCEDURE choose_quest(user_p NUMBER,type_p NUMBER)
IS
level_n NUMBER:=1;
CURSOR usr_cur
IS
  SELECT *
    FROM user_int_v
   WHERE user_id = user_p;
u usr_cur%ROWTYPE;
CURSOR quest_cur(dept_p NUMBER)
IS
  SELECT q.*,call_game.get_level_num(q.min_level) min_lev,call_game.get_level_num(q.max_level) max_lev
    FROM call_game_quests q
   WHERE sub_quest_type_id IN (SELECT sub_id FROM call_quest_sub_types WHERE sub_tp_id = type_p)
     AND quest_id IN (SELECT NVL(quest_id,0) FROM call_game_quest_asg_dept WHERE dept_id = dept_p)
     AND NVL(quest_id,0) NOT IN (SELECT NVL(quest_id,0) FROM game_user_quests WHERE user_id = user_p AND complete_dt IS NOT NULL)
     AND quest_id != 226
     AND nvl(validated,0) = 1
   ORDER BY min_lev,max_lev;

CURSOR quest_402_cur
IS
  SELECT q.*,call_game.get_level_num(q.min_level) min_lev,call_game.get_level_num(q.max_level) max_lev
    FROM call_game_quests q
   WHERE quest_id = 402
   ORDER BY min_lev,max_lev;
   
CURSOR type_cur
IS
  SELECT *
    FROM call_quest_types
   WHERE tp_id = type_p;
tp type_cur%ROWTYPE;

cnt NUMBER:=0;

q_cnt NUMBER:=0;

cnter NUMBER:=0;

beg_cnt NUMBER:=0;

loop_cnt NUMBER:=0;
CURSOR vote_cur
IS
  SELECT count(1)
    FROM user_idea_vote
   WHERE quest_id = 402
     AND user_id = user_p;
v_cnt NUMBER:=0;
CURSOR admin_cur
IS
  SELECT *
    FROM game_admin_items;
a admin_cur%ROWTYPE;

curr_amt_n NUMBER:=0;
BEGIN
  OPEN admin_cur;
  FETCH admin_cur INTO a;
  CLOSE admin_cur;
  OPEN vote_cur;
  FETCH vote_cur INTO v_cnt;
  CLOSE vote_cur;
  OPEN type_cur;
  FETCH type_cur INTO tp;
  CLOSE type_cur;

  OPEN usr_cur;
  FETCH usr_cur INTO u;
  CLOSE usr_cur;
  level_n := call_game.get_user_level(user_p);

  FOR q IN quest_cur(u.call_game_dept_id) LOOP
    q_cnt := q_cnt + 1;
  END LOOP;
  
  IF q_cnt <= 3 THEN
    beg_cnt:=1;
  ELSE
    q_cnt := q_cnt - 2;
    beg_cnt := round(DBMS_RANDOM.value(1, q_cnt));

  END IF;
  
  --IF trunc(sysdate) = to_date('16-JAN-18') THEN
    --beg_cnt := 1;
  --END IF;
    
wl('
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset=''UTF-8''>

  <title>'||get_user_name(user_p)||' Game</title>  
        <style>
            body {
                font-family: Comic Sans MS,  sans-serif;
                font-size:19pt;
                background-color: #ffffff;
                background-image: url("https://rightdata.driggstitle.com/callGame/Kwest_icons/Kwest_hub_background.png");
            }
        </style>
</head>');
wl('<body>');  
call_game.disable_back;
  wl('<script>function chooseQst(valP){
                location="/prv/call_game_play.assign_quest_to_usr?user_p='||user_p||'&quest_p="+valP;
              }
      </script>');
  wl('<center><img src="'||tp.top_img_path||'" width="578px" height="65px"></center>');  
  wl('<div style="margin-left:20px">');
  IF type_p = 3 AND (trunc(sysdate) = to_date('02-FEB-18') OR (is_ryan AND trunc(sysdate) = to_date('01-FEB-18'))) THEN
    FOR qs IN quest_402_cur LOOP
      wl('<p style="cursor:pointer" onclick="chooseQst('||qs.quest_id||');" style="cursor:pointer;" >');
      wl('<img src="https://rightdata.driggstitle.com/callGame/Kwest_icons/Gold_Heart.png" width="30px" height="30px" onclick="chooseQst('||qs.quest_id||');" style="color:blue;cursor:pointer;" >');
      wl(qs.quest_name);
      
      wl('</p>');
    END LOOP;
  ELSE
  FOR qs IN quest_cur(u.call_game_dept_id) LOOP
    IF qs.min_lev <= level_n THEN
      loop_cnt := loop_cnt + 1;

      IF loop_cnt >= beg_cnt THEN
        cnter := cnter + 1;

        IF cnter <= 3 THEN
          wl('<p style="cursor:pointer" onclick="chooseQst('||qs.quest_id||');" style="cursor:pointer;" >');
          IF nvl(qs.diff_level,0) = 3 THEN
            wl('<img src="https://rightdata.driggstitle.com/callGame/Kwest_icons/Gold_Heart.png" width="30px" height="30px" onclick="chooseQst('||qs.quest_id||');" style="color:blue;cursor:pointer;" >');
          ELSIF nvl(qs.diff_level,0) = 2 THEN
            wl('<img src="https://rightdata.driggstitle.com/callGame/Kwest_icons/Silver_Heart.png" width="30px" height="30px" onclick="chooseQst('||qs.quest_id||');" style="color:blue;cursor:pointer;" >');
          ELSE
            wl('<img src="https://rightdata.driggstitle.com/callGame/Kwest_icons/Bronze_Heart.png" width="30px" height="30px" onclick="chooseQst('||qs.quest_id||');" style="color:blue;cursor:pointer;" >');
          END IF;
          wl(qs.quest_name);
          curr_amt_n := a.base_currency * nvl(qs.diff_mult,0);
          wl('<span style="font-size:10pt">&nbsp;&nbsp;'||curr_amt_n||' currency </span>');
          IF nvl(qs.author_id,0) > 0 THEN
            wl('<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');
            wl('<span style="font-size:10pt">Authored By: '||get_user_name(qs.author_id)||'</span>');
          END IF;
          wl('</p>');
          
        END IF;
      END IF;
    END IF;
  END LOOP;
  END IF;
  
 -- IF user_p IN (96490,96491) THEN
  IF type_p = 3 AND trunc(sysdate) BETWEEN to_date('09-FEB-18') and to_date('12-FEB-18') THEN
    IF v_cnt = 0 THEN
    wl('<script>
          function voteItm(){
            location="/prv/call_game_screen.idea_vote?id_p=402&user_p='||user_p||'";
          }
        </script>');
    
    wl('<p style="cursor:pointer" onclick="voteItm();">');
    wl('<img src="https://rightdata.driggstitle.com/callGame/Kwest_icons/Gold_Heart.png" width="30px" height="30px" onclick="chooseQst(226);" style="color:blue;cursor:pointer;" >');
    wl('Vote on the best idea to integrate clients into the Kindness Kwest</p>');
    END IF;
  END IF;

  wl('<p style="cursor:pointer" onclick="chooseQst(226);" style="cursor:pointer;" >');
  wl('<img src="https://rightdata.driggstitle.com/callGame/Kwest_icons/Gold_Heart.png" width="30px" height="30px" onclick="chooseQst(226);" style="color:blue;cursor:pointer;" >');
  wl('Create a Kwest -  challenge your teammates');
  wl('</p>');    
  /*
  wl('<select name="quest_p" id="quest_p" onchange="chooseQst(this.value);">');
  wl('<option value="0">Choose Quest</option>');
  FOR qs IN quest_cur(u.call_game_dept_id) LOOP
    IF qs.min_lev <= level_n THEN
      wl('<option value="'||qs.quest_id||'">'||qs.quest_name||'</option>');
    END IF;
  END LOOP;
  wl('</select>');
  */
  --wl('in choose quest '||user_p||'  '||type_p);
END;
-------------------------------------------------------
PROCEDURE assign_quest_to_usr(user_p NUMBER,quest_p NUMBER)
IS
usr_qst_n NUMBER;

CURSOR qst_cur
IS
  SELECT *
    FROM call_game_quests
   WHERE quest_id = quest_p;
q qst_cur%ROWTYPE;
BEGIN
  IF nvl(quest_p,0) > 0 THEN
  SELECT game_user_quests_s.NEXTVAL INTO usr_qst_n
    FROM dual;
  INSERT INTO game_user_quests(usr_qst_id,quest_id,user_id,assign_dt)
  VALUES(usr_qst_n,quest_p,user_p,sysdate);
  COMMIT;
  OPEN qst_cur;
  FETCH qst_cur INTO q;
  CLOSE qst_cur;
  
  wl('<script>location="/prv/call_game_play.get_quest_screen?gq_p='||usr_qst_n||'";</script>');
  ELSE
    wl('<script>location="/prv/call_game_play.choose_quest_type?user_p='||user_p||'";</script>');
  END IF;
  /*
  IF nvl(q.beg_screen,0) > 0 THEN
    wl('<script>location="/prv/call_game.do_quest?qst_p='||usr_qst_n||'";</script>'); 
  ELSE
    wl('<script>location="/prv/call_game.asg_qst_cont?qst_p='||usr_qst_n||'";</script>'); 
  END IF;
  */
END;
-------------------------------------------------------
PROCEDURE get_quest_screen(gq_p NUMBER)
IS
CURSOR gq_cur
IS
  SELECT *
    FROM game_user_quests
   WHERE usr_qst_id = gq_p;
gq_r gq_cur%ROWTYPE;

CURSOR qst_cur(id_p NUMBER)
IS
  SELECT *
    FROM call_game_quests
   WHERE quest_id = id_p;
q qst_cur%ROWTYPE;

CURSOR beg_cur(beg_p NUMBER)
IS
  SELECT *
    FROM call_game_start_screen
   WHERE screen_id = beg_p
   ORDER BY screen_name;
beg beg_cur%ROWTYPE;    
BEGIN
  OPEN gq_cur;
  FETCH gq_cur INTO gq_r;
  CLOSE gq_cur;
  
  OPEN qst_cur(gq_r.quest_id);
  FETCH qst_cur INTO q;
  CLOSE qst_cur;

  IF nvl(q.beg_screen,0) > 0 THEN
    --wl('<script>location="/prv/call_game.do_quest?qst_p='||gq_r.usr_qst_id||'";</script>');
    OPEN beg_cur(q.beg_screen);
    FETCH beg_cur INTO beg;
    CLOSE beg_cur;
    IF nvl(beg.driggs_screen,'None') = 'None' THEN
      null;
      wl('<script>top.location="/prv/call_game_play.youtube_hdr?usr_qst_p='||gq_r.usr_qst_id||'";</script>'); 
     -- youtube_hdr
    ELSE
      wl('<script>location="/prv/call_game_play.do_kindness_quest?gq_p='||gq_r.usr_qst_id||'";</script>'); 
    END IF;
  ELSIF nvl(q.mindfl_crs_id,0) > 0 THEN
    --wl('<h2><center>Please wait while course opens</center></h2>');
    --wl('<script>location="/prv/call_game_play.do_mindflash?gq_p='||gq_r.usr_qst_id||'";</script>'); 
    
wl('<title>Mindflash</title>');
wl('<style>
html,body {
    height:100%;
}
.h_iframe1 iframe {
    position:absolute;
    top:0;
    left:0;
    width:100%; 
    height:400px;
}
.h_iframe2 iframe {
    position:absolute;
    bottom:0;
    right:0;
    width:100%; 
    height:80px;
}</style>'); 

--height:calc(75% - 80px);
/*
wl('<table width="100%">');
wl('<tr>');
wl('<td width="20%">');
wl('<iframe name="frame2" src="/prv/call_game_play.mindflash_wait?user_p='||gq_r.user_id||'&usr_qst_p='||gq_r.usr_qst_id||'" style="height: 80px; width: 100%; border: none;"></iframe>');
wl('</td>');
wl('<td width="80%">');
wl('<iframe name="frame1" src="/prv/call_game_play.do_mindflash?gq_p='||gq_r.usr_qst_id||'";" style="height: 700px; width: 100%; border: none;"></iframe>');
wl('</td>');
wl('</tr>');
wl('</table>');

wl('<script>
       var fm = top.document.getElementById("optsFrame");
       fm.style.height="1000px";
       fm.style.zIndex="-1";
       //fm.top = 0px;
    </script>');
*/
wl('<script>top.location="/prv/call_game_play.mind_flash_hdr?usr_qst_p='||gq_r.usr_qst_id||'";</script>');
/*
wl('

<div id="frame1">
   <iframe name="frame1" src="/prv/call_game_play.do_mindflash?gq_p='||gq_r.usr_qst_id||'";" style="height: 400px; width: 100%; border: none;"></iframe>
</div><!-- close div#frame1 -->

<div id="frame2">
   <iframe name="frame2" src="/prv/call_game_play.mindflash_wait?user_p='||gq_r.user_id||'&usr_qst_p='||gq_r.usr_qst_id||'" style="height: 80px; width: 100%; border: none;"></iframe>
</div><!-- close div#frame2 -->

');   
*/  
  ELSIF lower(call_game.get_quest_type_name_by_sub(q.sub_quest_type_id)) LIKE '%kindness%' THEN
    wl('<script>location="/prv/call_game_play.do_kindness_quest?gq_p='||gq_r.usr_qst_id||'";</script>');
  ELSE
    IF nvl(gq_r.asg_cont_id,0) > 0 THEN
      wl('<script>location="/prv/call_game.asg_qst_action?qst_p='||gq_r.usr_qst_id||'";</script>'); 
    ELSIF nvl(q.filter1,0) > 0 THEN
      wl('<script>location="/prv/call_game.asg_qst_cont?qst_p='||gq_r.usr_qst_id||'";</script>'); 
    ELSIF nvl(q.sign_docs_qst,'N') = 'Y' THEN
      wl('<script>location="/prv/user_comp_docs.sign_all_docs_qst?user_p='||gq_r.user_id||'&qst_p='||gq_r.usr_qst_id||'";</script>'); 
    ELSE
      wl('<script>location="/prv/call_game_play.do_kindness_quest?gq_p='||gq_r.usr_qst_id||'";</script>');
    END IF;
  END IF;

END;
-------------------------------------------------------
PROCEDURE youtube_hdr(usr_qst_p NUMBER)
IS
CURSOR gq_cur
IS
  SELECT *
    FROM game_user_quests
   WHERE usr_qst_id = usr_qst_p;
gq_r gq_cur%ROWTYPE;

CURSOR qst_cur(id_p NUMBER)
IS
  SELECT *
    FROM call_game_quests
   WHERE quest_id = id_p;
q qst_cur%ROWTYPE;

CURSOR beg_cur(beg_p NUMBER)
IS
  SELECT *
    FROM call_game_start_screen
   WHERE screen_id = beg_p
   ORDER BY screen_name;
beg beg_cur%ROWTYPE;    

vid_id VARCHAR2(500);

bet_b VARCHAR2(20):='FALSE';
BEGIN
  IF is_beta THEN
    bet_b := 'TRUE';
  END IF;
  OPEN gq_cur;
  FETCH gq_cur INTO gq_r;
  CLOSE gq_cur;
  
  OPEN qst_cur(gq_r.quest_id);
  FETCH qst_cur INTO q;
  CLOSE qst_cur;

  OPEN beg_cur(q.beg_screen);
  FETCH beg_cur INTO beg;
  CLOSE beg_cur;
  
  vid_id := replace(beg.beg_url,'https://www.youtube.com/embed/');
call_game.disable_back;
--IF sess.user_id = 96490 THEN
IF 1=1 THEN
wl('
<!DOCTYPE html>
<html>');
wl('<head>');
wl('<title>Watch '||q.quest_name||'</title>');
wl('<script src="//www.youtube.com/player_api"></script>');
wl('</head>');
wl('<body>');

wl('<script src="//www.youtube.com/player_api"></script>');
wl('<table width="100%">');
wl('<tr>');
wl('<td width="100%">');
wl('
<div id="player"></div>
   <script>
        var ht = window.innerHeight;
        console.log(ht);
        ht = parseFloat(ht) * .8;
        var set=0;
    </script>
    <script>
      // 2. This code loads the IFrame Player API code asynchronously.
      var tag = document.createElement(''script'');

      tag.src = "https://www.youtube.com/iframe_api";
      var firstScriptTag = document.getElementsByTagName(''script'')[0];
      firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

      // 3. This function creates an <iframe> (and YouTube player)
      //    after the API code downloads.
      var player;
      function onYouTubeIframeAPIReady() {
        console.log(ht);
        player = new YT.Player(''player'', {
          height: ht,
          width: ''100%'',
          videoId: '''||vid_id||''',
          events: {
            ''onReady'': onPlayerReady,
            ''onStateChange'': onPlayerStateChange
          }
        });
      }

      // 4. The API will call this function when the video player is ready.
      function onPlayerReady(event) {
        event.target.playVideo();
      }

      // 5. The API calls this function when the player''s state changes.
      //    The function indicates that when playing a video (state=1),
      //    the player should play for six seconds and then stop.
      var done = false;
      function onPlayerStateChange(event) {
        console.log("in state change "+event.data);  
        var num = player.getDuration();
        console.log(num+" player duration ");
        num = parseFloat(num) * .8;
        console.log(num+" stopTime");
        num = parseFloat(num) * 1000;
        if (set == 0){
          set = 1;
          //If stacia or BigE
          if('||gq_r.user_id||' ==187228 || '||gq_r.user_id||' ==195659)
            num=0;
            
          setTimeout("setEnab();",num);
        }
      }
      function stopVideo() {
        player.stopVideo();
      }
    </script>');
    
    wl('<script>function setEnab(){
                  console.log("in set enab");
                  var frm = document.getElementById("navFrm");
                  frm.src="/prv/call_game_play.nav_frame_enabled?user_p='||gq_r.user_id||'&usr_qst_p='||gq_r.usr_qst_id||'";
                }
        </script>');
    --https://www.youtube.com/embed/tsMNQsl574E
--wl('<iframe width="100%" id="controllable_player" height="500px" src="'||beg.beg_url||'" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>');

--wl('<iframe name="frame1" src="/prv/call_game_play.do_mindflash?gq_p='||gq_r.usr_qst_id||'";" style="height: 700px; width: 100%; border: none;"></iframe>');
wl('</td>');
wl('</tr>');
wl('<tr>');
wl('<td width="100%">');
wl('<iframe name="frame2" id="navFrm" src="/prv/call_game_play.mindflash_wait?user_p='||gq_r.user_id||'&usr_qst_p='||gq_r.usr_qst_id||'" style="height: 80px; width: 100%; border: none;"></iframe>');
wl('</td>');
wl('</tr>');
wl('</table>'); 

wl('</body>');
wl('</html>');
ELSE
wl('<title>Watch Video</title>');
wl('<script src="//www.youtube.com/player_api"></script>');
wl('<table width="100%">');
wl('<tr>');
wl('<td width="100%">');
wl('<iframe width="100%" id="controllable_player" height="500px" src="'||beg.beg_url||'" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>');

--wl('<iframe name="frame1" src="/prv/call_game_play.do_mindflash?gq_p='||gq_r.usr_qst_id||'";" style="height: 700px; width: 100%; border: none;"></iframe>');
wl('</td>');
wl('</tr>');
wl('<tr>');
wl('<td width="100%">');
wl('<iframe name="frame2" src="/prv/call_game_play.mindflash_wait?user_p='||gq_r.user_id||'&usr_qst_p='||gq_r.usr_qst_id||'" style="height: 80px; width: 100%; border: none;"></iframe>');
wl('</td>');
wl('</tr>');
wl('</table>');
END IF;

END;

-------------------------------------------------------
PROCEDURE mind_flash_hdr(usr_qst_p NUMBER)
IS
CURSOR gq_cur
IS
  SELECT *
    FROM game_user_quests
   WHERE usr_qst_id = usr_qst_p;
gq_r gq_cur%ROWTYPE;

CURSOR qst_cur(id_p NUMBER)
IS
  SELECT *
    FROM call_game_quests
   WHERE quest_id = id_p;
q qst_cur%ROWTYPE;

BEGIN
  OPEN gq_cur;
  FETCH gq_cur INTO gq_r;
  CLOSE gq_cur;
  
  OPEN qst_cur(gq_r.quest_id);
  FETCH qst_cur INTO q;
  CLOSE qst_cur;
call_game.disable_back;
wl('<table width="100%">');
wl('<tr>');
wl('<td width="100%">');
--wl('<iframe width="100%" height="700px" src="https://www.youtube.com/embed/o3PK5_LLOJs" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>');

wl('<iframe name="frame1" src="/prv/call_game_play.do_mindflash?gq_p='||gq_r.usr_qst_id||'";" style="height: 700px; width: 100%; border: none;"></iframe>');
wl('</td>');
wl('</tr>');
wl('<tr>');
wl('<td width="100%">');
wl('<iframe name="frame2" src="/prv/call_game_play.mindflash_wait?user_p='||gq_r.user_id||'&usr_qst_p='||gq_r.usr_qst_id||'" style="height: 80px; width: 100%; border: none;"></iframe>');
wl('</td>');
wl('</tr>');
wl('</table>');
  
END;
-------------------------------------------------------
PROCEDURE show_cont_dtl(cont_p NUMBER)
IS
CURSOR cli_cur(cont_p NUMBER)
IS
  SELECT *
    FROM client_v
   WHERE cont_id = cont_p;
c cli_cur%ROWTYPE;

CURSOR dtl_cur(cont_p NUMBER)
IS
  SELECT *
    FROM cont_bus_card_dtls
   WHERE cont_id = cont_p;
d dtl_cur%ROWTYPE;

s_file_n NUMBER;

BEGIN
  OPEN cli_cur(cont_p);
  FETCH cli_cur INTO c;
  CLOSE cli_cur;
  
  OPEN dtl_cur(cont_p);
  FETCH dtl_cur INTO d;
  CLOSE dtl_cur;

  call_game.disable_back;
  wl('<table width="100%">');
  wl('<tr>');
  wl('<td width="80%">');
  --cli_tabs.cli_profile(cont_p);
  wl('<table width="100%" border="0">');
  wl('<tr>');
  IF nvl(length(d.photo_link),0) < 4 THEN
    d.photo_link := '//rightdata.driggstitle.com/marketers/place_holder.jpg';
  END IF;
  wl('<td width="15%" valign="top" style="background-color:black">');
  wl('<a href="/prv/business_card.show_client_card?id_p='||cont_p||'&no_access=1" target="_blank">');
  wl('<img width="200px" height="200px" src="'||d.photo_link||'">');
  wl('</a>');
  wl('</td>');

  wl('<td width="20%" valign="top" style="background-color:black;color:white;">');
  wl('<p>');
  wl('<a href="/prv/lfs.show_simp_lead?cli_p='||c.cli_id||'&cont_p='||c.cont_id||'" target="_blank">'||substr(c.client_company,1,30)||'</a><br>');
  wl(c.cont_name||'<br>');
  wl(format_ph_all(fc.get_best_cont_phone(cont_p))||'<br>');
  wl(c.cont_email||'<br>');
  wl(fc.get_client_addr_lines_abbr(cont_p)||'<br>');
  wl('EO: '||nvl(get_user_name(c.brch_escrow_id),'N/A')||'<br>');
  wl('Rep: '||nvl(get_user_name(c.brch_mrket_id),'N/A')||'<br>');
  --wl('User Name: '||un_v||'<br>');
  --wl('Password: '||pw_v||'<br>');
  wl('</p>');
  wl('</td>');
  
  --PROCEDURE pitch_chart(cont_p NUMBER)
wl('</td>');

wl('<td width="20%" align="left" valign="top">');
  IF nvl(c.first_order_dt,to_date('01-JAN-01')) = to_date('01-JAN-01') THEN
    wl('First Order: N/A<br>');
  ELSE
    wl('First Order: '||to_char(c.first_order_dt,'MM/DD/YYYY')||'<br>');
  END IF;
  IF nvl(c.last_order_dt,to_date('01-JAN-01')) = to_date('01-JAN-01') THEN
    wl('Last Order: N/A<br>');
  ELSE
    wl('Last Order: '||to_char(c.last_order_dt,'MM/DD/YYYY')||'<br>');
  END IF;
  wl('Total Times Client: '||cp_utils.get_file_cnt(c.cont_id)||'<br>');
  
  s_file_n := sales_production.get_last_soft_file(c.cont_id);
  IF nvl(s_file_n,0) = 0 THEN
    wl('Last Soft Lead: N/A<br>');
  ELSE
    c.soft_lead_dt := df.get_open_date(s_file_n);
    wl('Last Soft Lead: '||to_char(c.soft_lead_dt,'MM/DD/YYYY')||'<br>');
    wl('Last Soft File: <a href="/prv/order_entry.load_oe_frames?file_p='||s_file_n||'" target="_blank" style="color:black">'||get_fil_id(s_file_n)||'</a><br>');
  END IF;
  wl('Total Soft Lead: '||cp_utils.get_soft_file_cnt(c.cont_id)||'<br>');
  IF nvl(c.first_connection,to_date('01-JAN-01')) = to_date('01-JAN-01') THEN
    wl('First Connection: N/A<br>');
  ELSE
    wl('First Connection: '||to_char(c.first_connection,'MM/DD/YYYY')||'<br>');
  END IF;
  IF nvl(c.last_connection,to_date('01-JAN-01')) = to_date('01-JAN-01') THEN
    wl('Last Connection: N/A<br>');
  ELSE
    wl('Last Connection: '||to_char(c.last_connection,'MM/DD/YYYY')||'<br>');
  END IF;
  IF nvl(c.soft_proc_beg_dt,to_date('01-JAN-01')) = to_date('01-JAN-01') THEN
    wl('Process: N/A<br>');
  ELSE
    wl('Process: '||to_char(c.soft_proc_beg_dt,'MM/DD/YYYY'));
  END IF;
  --wl('<a href="" target="_blank">Listing Tour Schedule</a><br>');
  --wl('<a href="" target="_blank">Class Schedule</a><br>');

wl('</td>');
wl('</tr>');
  wl('</table>');
  wl('</table>');
  
END;
-------------------------------------------------------
PROCEDURE mindflash_wait(user_p NUMBER,usr_qst_p NUMBER)
IS
BEGIN
wl('<h2><center>Please Wait While Course Loads</center></h2>');
wl('<script>
       function goNav(){
         location="/prv/call_game_play.mindflash_nav_frame?user_p='||user_p||'&usr_qst_p='||usr_qst_p||'"; 
       }
       setTimeout("goNav();",100);
    </script>');
END;

-------------------------------------------------------
PROCEDURE nav_frame_enabled(user_p NUMBER,usr_qst_p NUMBER)
IS
CURSOR gq_cur
IS
  SELECT *
    FROM game_user_quests
   WHERE usr_qst_id = usr_qst_p;
gq_r gq_cur%ROWTYPE;

CURSOR qst_cur(qst_p NUMBER)
IS
  SELECT *
    FROM call_game_quests
   WHERE quest_id = qst_p;
qr qst_cur%ROWTYPE;

CURSOR cnt_qst_asg_cur(qst_p NUMBER)
IS
  SELECT count(1)
    FROM game_user_quests
   WHERE user_id = user_p
     AND quest_id = qst_p;
asg_cnt NUMBER:=0;
num_n NUMBER;
BEGIN
  OPEN gq_cur;
  FETCH gq_cur INTO gq_r;
  CLOSE gq_cur;
  
  OPEN qst_cur(gq_r.quest_id);
  FETCH qst_cur INTO qr;
  CLOSE qst_cur;
  
  OPEN cnt_qst_asg_cur(gq_r.quest_id);
  FETCH cnt_qst_asg_cur INTO asg_cnt;
  CLOSE cnt_qst_asg_cur;

  IF nvl(qr.video_duration,0) > 1 THEN
    num_n := qr.video_duration * 800;
  ELSE
    num_n := 30000;
  END IF;
--  IF sess.user_id = 96490 then
--    num_n := 1;
--  END IF;

call_game.disable_back;
wl('<style>
.btn {
  background: #eb393e;
  background-image: -webkit-linear-gradient(top, #eb393e, #eb393e);
  background-image: -moz-linear-gradient(top, #eb393e, #eb393e);
  background-image: -ms-linear-gradient(top, #eb393e, #eb393e);
  background-image: -o-linear-gradient(top, #eb393e, #eb393e);
  background-image: linear-gradient(to bottom, #eb393e, #eb393e);
  -webkit-border-radius: 28;
  -moz-border-radius: 28;
  border-radius: 28px;
  font-family: Arial;
  color: #ffffff;
  font-size: 20px;
  padding: 5px 10px 5px 10px;
  text-decoration: none;
}

.btnDis {
  background: #999999;
  background-image: -webkit-linear-gradient(top, #999999, #999999);
  background-image: -moz-linear-gradient(top, #999999, #999999);
  background-image: -ms-linear-gradient(top, #999999, #999999);
  background-image: -o-linear-gradient(top, #999999, #999999);
  background-image: linear-gradient(to bottom, #999999, #999999);
  -webkit-border-radius: 28;
  -moz-border-radius: 28;
  border-radius: 28px;
  font-family: Arial;
  color: #ffffff;
  font-size: 20px;
  padding: 5px 10px 5px 10px;
  text-decoration: none;
}

</style>');
  wl('<script>
         function cplt(){
           var frm = top.document.getElementById("optsFrame");
           frm.src="/prv/call_game.complete_qst?qst_p='||usr_qst_p||'";
           frm.style.height="550px";
         }
         
         function takeQz(){
           try{
             var frm = top.document.getElementById("optsFrame");
             frm.src="/prv/call_game_play.take_quiz?usr_qst_p='||usr_qst_p||'";
             frm.style.height="550px";
           }catch(e){
             top.location="/prv/call_game_play.take_quiz?usr_qst_p='||usr_qst_p||'";
           }
         }
       </script>');
       
/*
wl('<script>
       var fm = top.document.getElementById("optsFrame");
       fm.style.height="700px";
    </script>');
*/
       --PROCEDURE take_quiz(usr_qst_p NUMBER)
  IF nvl(qr.quiz_id,0) > 0 THEN
    wl('<center><input type="button" class="btn" name="cpltBtn" id="cpltBtn" value="Click Here to Take Quiz" onclick="takeQz();"></center>');
  ELSE
    wl('<center><input type="button" class="btn" name="cpltBtn" id="cpltBtn" value="Click Here to Complete Kwest" onclick="cplt();"></center>');
  END IF;

  wl('<script>
        function allowBtn(){
          var bn = document.getElementById("cpltBtn");
          bn.disabled=false; 
          bn.className="btn";
        }
        setTimeout("allowBtn()","'||num_n||'");
        
      </script>');
      
END;
-------------------------------------------------------
PROCEDURE mindflash_nav_frame(user_p NUMBER,usr_qst_p NUMBER)
IS
CURSOR gq_cur
IS
  SELECT *
    FROM game_user_quests
   WHERE usr_qst_id = usr_qst_p;
gq_r gq_cur%ROWTYPE;

CURSOR qst_cur(qst_p NUMBER)
IS
  SELECT *
    FROM call_game_quests
   WHERE quest_id = qst_p;
qr qst_cur%ROWTYPE;

CURSOR cnt_qst_asg_cur(qst_p NUMBER)
IS
  SELECT count(1)
    FROM game_user_quests
   WHERE user_id = user_p
     AND quest_id = qst_p;
asg_cnt NUMBER:=0;
num_n NUMBER;
BEGIN
  OPEN gq_cur;
  FETCH gq_cur INTO gq_r;
  CLOSE gq_cur;
  
  OPEN qst_cur(gq_r.quest_id);
  FETCH qst_cur INTO qr;
  CLOSE qst_cur;
  
  OPEN cnt_qst_asg_cur(gq_r.quest_id);
  FETCH cnt_qst_asg_cur INTO asg_cnt;
  CLOSE cnt_qst_asg_cur;

  IF nvl(qr.video_duration,0) > 1 THEN
    num_n := qr.video_duration * 800;
  ELSE
    num_n := 30000;
  END IF;
  IF sess.user_id = 96490 then
    num_n := 1;
  END IF;

call_game.disable_back;
wl('<style>
.btn {
  background: #eb393e;
  background-image: -webkit-linear-gradient(top, #eb393e, #eb393e);
  background-image: -moz-linear-gradient(top, #eb393e, #eb393e);
  background-image: -ms-linear-gradient(top, #eb393e, #eb393e);
  background-image: -o-linear-gradient(top, #eb393e, #eb393e);
  background-image: linear-gradient(to bottom, #eb393e, #eb393e);
  -webkit-border-radius: 28;
  -moz-border-radius: 28;
  border-radius: 28px;
  font-family: Arial;
  color: #ffffff;
  font-size: 20px;
  padding: 5px 10px 5px 10px;
  text-decoration: none;
}

.btnDis {
  background: #999999;
  background-image: -webkit-linear-gradient(top, #999999, #999999);
  background-image: -moz-linear-gradient(top, #999999, #999999);
  background-image: -ms-linear-gradient(top, #999999, #999999);
  background-image: -o-linear-gradient(top, #999999, #999999);
  background-image: linear-gradient(to bottom, #999999, #999999);
  -webkit-border-radius: 28;
  -moz-border-radius: 28;
  border-radius: 28px;
  font-family: Arial;
  color: #ffffff;
  font-size: 20px;
  padding: 5px 10px 5px 10px;
  text-decoration: none;
}

</style>');
  wl('<script>
         function cplt(){
           var frm = top.document.getElementById("optsFrame");
           frm.src="/prv/call_game.complete_qst?qst_p='||usr_qst_p||'";
           frm.style.height="550px";
         }
         
         function takeQz(){
           try{
             var frm = top.document.getElementById("optsFrame");
             frm.src="/prv/call_game_play.take_quiz?usr_qst_p='||usr_qst_p||'";
             frm.style.height="550px";
           }catch(e){
             top.location="/prv/call_game_play.take_quiz?usr_qst_p='||usr_qst_p||'";
           }
         }
       </script>');
       
/*
wl('<script>
       var fm = top.document.getElementById("optsFrame");
       fm.style.height="700px";
    </script>');
*/
       --PROCEDURE take_quiz(usr_qst_p NUMBER)
  IF nvl(qr.quiz_id,0) > 0 THEN
    IF is_beta THEN
      wl('<center><input type="button" class="btn" name="cpltBtn" id="cpltBtn" value="Click Here to Take Quiz" onclick="takeQz();"></center>');
    ELSE
      wl('<center><input type="button" class="btnDis" name="cpltBtn" id="cpltBtn" disabled value="Click Here to Take Quiz" onclick="takeQz();"></center>');
    END IF;
  ELSE
    wl('<center><input type="button" class="btn" name="cpltBtn" id="cpltBtn" value="Click Here to Complete Kwest" onclick="cplt();"></center>');
  END IF;

  wl('<script>
        function allowBtn(){
          var bn = document.getElementById("cpltBtn");
          bn.disabled=false; 
          bn.className="btn";
        }
        setTimeout("allowBtn()","'||num_n||'");
        
      </script>');
      
END;
-------------------------------------------------------
PROCEDURE do_mindflash(gq_p NUMBER)
IS
CURSOR gq_cur
IS
  SELECT *
    FROM game_user_quests
   WHERE usr_qst_id = gq_p;
gq_r gq_cur%ROWTYPE;

CURSOR qst_cur(id_p NUMBER)
IS
  SELECT *
    FROM call_game_quests
   WHERE quest_id = id_p;
qt qst_cur%ROWTYPE;

CURSOR usr_cur(usr_p NUMBER)
IS
  SELECT *
    FROM user_int_v u
   WHERE acc_account_id = sess.acct_id
     AND user_id = usr_p
     AND nvl(u.call_game_dept_id,0) > 0
   ORDER BY first_name,last_name;
u usr_cur%ROWTYPE;   
pieces utl_http.html_pieces;

api_key VARCHAR2(1000):='f4afdbdaafdd4aef8b29c24ae4152581';

pg1 VARCHAR2(32767):='';

req   UTL_HTTP.REQ;
resp  UTL_HTTP.RESP;

req2   UTL_HTTP.REQ;
resp2  UTL_HTTP.RESP;

buffer varchar2(4000); 

cnter NUMBER:=0;

id_v VARCHAR2(500);
dsh_url VARCHAR2(4000);

pg2 VARCHAR2(32767);

content varchar2(4000) := '{
  "clientDatestamp": "'||to_char(sysdate,'YYYY-MM-DD')||'",
  "required": false
}';
BEGIN
  OPEN gq_cur;
  FETCH gq_cur INTO gq_r;
  CLOSE gq_cur;
  
  OPEN qst_cur(gq_r.quest_id);
  FETCH qst_cur INTO qt;
  CLOSE qst_cur;
  
  OPEN usr_cur(gq_r.user_id);
  FETCH usr_cur INTO u;
  CLOSE usr_cur;
  
  --u.email := replace(u.email,'@','%40');
  delete rtest;
  
  wl('<h2><center>Please wait while course opens '||u.email||'  '||qt.mindfl_crs_id||'</center></h2>');
  wl('<h2><center>Please wait while course opens</center></h2>');
  utl_http.set_wallet('file:/oracle/utilhttp_wallet','tudriggs11');
  req := utl_http.begin_request(url => 'https://api.mindflash.com/api/v2/auth?email='||u.email);
  UTL_HTTP.set_header (req,'x-mindflash-apikey',api_key); 
  resp := utl_http.get_response(req);
  utl_http.read_text(resp,pg1);

 

utl_http.end_response(resp); 

  apex_json.parse(pg1);
  id_v := apex_json.get_varchar2('userId');  
  dsh_url := apex_json.get_varchar2('dashboardUrl'); 
  
  ins_rtest2(dsh_url);
  wl('<iframe id="logFrm" src="'||dsh_url||'" style="display:none"></iframe>');

  utl_http.set_wallet('file:/oracle/utilhttp_wallet','tudriggs11');
  req2 := utl_http.begin_request(url => 'https://api.mindflash.com/api/v2/course/'||qt.mindfl_crs_id||'/user/'||id_v||'/invite',method =>  'POST',http_version =>  'HTTP/1.1');
  utl_http.set_header(req2, 'user-agent', 'mozilla/4.0'); 
  utl_http.set_header(req2, 'content-type', 'application/json'); 
  utl_http.set_header(req2, 'Content-Length', length(content));

  UTL_HTTP.set_header (req2,'x-mindflash-apikey',api_key); 
  utl_http.write_text(req2, content);
  resp2 := utl_http.get_response(req2);

  utl_http.end_response(resp2); 
  
  --wl('<iframe id="logFrm" src="'||dsh_url||'" style="display:none"></iframe>');
  wl('<script>setTimeout(''location="https://coaching.driggstitle.com/Trainee/#/player/'||qt.mindfl_crs_id||'"'',3000);</script>');
EXCEPTION
  WHEN OTHERS THEN
    wl(sqlerrm);
END;
-------------------------------------------------------
PROCEDURE load_course_list
IS

req   UTL_HTTP.REQ;
resp  UTL_HTTP.RESP;
api_key VARCHAR2(1000):='f4afdbdaafdd4aef8b29c24ae4152581';

val_v VARCHAR2(1024);
pg1 VARCHAR2(32767);
BEGIN
  delete rtest;
  utl_http.set_wallet('file:/oracle/utilhttp_wallet','tudriggs11');
  req := utl_http.begin_request(url => 'https://api.mindflash.com/api/v2/course?status=Active');
  --utl_http.set_header(req, 'Authorization', 'Basic '||api_key||'');
  UTL_HTTP.set_header (req,'x-mindflash-apikey',api_key); 
  resp := utl_http.get_response(req);
  dbms_output.put_line('after read text');
  --utl_http.read_text(resp,pg1);
  --dbms_output.put_line('after read text');
  
  LOOP

    UTL_HTTP.READ_LINE(resp, val_v, TRUE);

    dbms_output.put_line(val_v);

  END LOOP;  
  utl_http.end_response(resp); 
  
  --dbms_output.put_line(pg1);
  ins_rtest2(substr(pg1,1,3500));
  COMMIT;
EXCEPTION
  WHEN OTHERS THEN
    dbms_output.put_line(sqlerrm);
END;
-------------------------------------------------------
FUNCTION get_course_name(id_p NUMBER) RETURN VARCHAR2
IS
api_key VARCHAR2(1000):='f4afdbdaafdd4aef8b29c24ae4152581';

pg1 VARCHAR2(32767):='';
pg2 VARCHAR2(32767):='';

req   UTL_HTTP.REQ;
resp  UTL_HTTP.RESP;

nm_v VARCHAR2(4000);

BEGIN
  utl_http.set_wallet('file:/oracle/utilhttp_wallet','tudriggs11');
  req := utl_http.begin_request(url => 'https://api.mindflash.com/api/v2/course/'||id_p);
  --utl_http.set_header(req, 'Authorization', 'Basic '||api_key||'');
  UTL_HTTP.set_header (req,'x-mindflash-apikey',api_key); 
  resp := utl_http.get_response(req);
  utl_http.read_text(resp,pg2);
  
  utl_http.end_response(resp); 
  apex_json.parse(pg2);
  nm_v := apex_json.get_varchar2('name');  
  
  RETURN nm_v;
END;
-------------------------------------------------------
PROCEDURE show_user_course_rslts(quest_p NUMBER)
IS
CURSOR gq_cur
IS
  SELECT *
    FROM game_user_quests
   WHERE usr_qst_id = quest_p;
gq_r gq_cur%ROWTYPE;

CURSOR qst_cur(id_p NUMBER)
IS
  SELECT *
    FROM call_game_quests
   WHERE quest_id = id_p;
qt qst_cur%ROWTYPE;

CURSOR usr_cur(usr_p NUMBER)
IS
  SELECT *
    FROM user_int_v u
   WHERE acc_account_id = sess.acct_id
     AND user_id = usr_p
     AND nvl(u.call_game_dept_id,0) > 0
   ORDER BY first_name,last_name;
u usr_cur%ROWTYPE;   
pieces utl_http.html_pieces;

api_key VARCHAR2(1000):='f4afdbdaafdd4aef8b29c24ae4152581';

pg1 VARCHAR2(32767):='';
pg2 VARCHAR2(32767):='';

req   UTL_HTTP.REQ;
resp  UTL_HTTP.RESP;

id_v VARCHAR2(4000);
dsh_url VARCHAR2(4000);

score_v VARCHAR2(4000);

l_count NUMBER:=0;

j apex_json.t_values;

qz_nm VARCHAR2(4000);
qz_score VARCHAR2(4000);
qz_status VARCHAR2(4000);
BEGIN
  OPEN gq_cur;
  FETCH gq_cur INTO gq_r;
  CLOSE gq_cur;
  
  OPEN qst_cur(gq_r.quest_id);
  FETCH qst_cur INTO qt;
  CLOSE qst_cur;
  
  OPEN usr_cur(gq_r.user_id);
  FETCH usr_cur INTO u;
  CLOSE usr_cur;

  --u.email := 'zachm@driggstitle.com';
  utl_http.set_wallet('file:/oracle/utilhttp_wallet','tudriggs11');
  req := utl_http.begin_request(url => 'https://api.mindflash.com/api/v2/auth?email='||u.email);
  --utl_http.set_header(req, 'Authorization', 'Basic '||api_key||'');
  UTL_HTTP.set_header (req,'x-mindflash-apikey',api_key); 
  resp := utl_http.get_response(req);
  utl_http.read_text(resp,pg1);
  --req.method
  --pg1 := utl_http.request('https://api.mindflash.com/api/v2/auth?apikey='||api_key||'&email=ryano%40driggstitle.com');

 

utl_http.end_response(resp); 

  apex_json.parse(pg1);
  id_v := apex_json.get_varchar2('userId');  
  dsh_url := apex_json.get_varchar2('dashboardUrl'); 
  
  --ins_rtest2(dsh_url);

  --/api/v2/course/1763047348/user/1769442485

  utl_http.set_wallet('file:/oracle/utilhttp_wallet','tudriggs11');
  req := utl_http.begin_request(url => 'https://api.mindflash.com/api/v2/course/'||qt.mindfl_crs_id||'/user/'||id_v);
  --utl_http.set_header(req, 'Authorization', 'Basic '||api_key||'');
  UTL_HTTP.set_header (req,'x-mindflash-apikey',api_key); 
  resp := utl_http.get_response(req);
  utl_http.read_text(resp,pg2);
  --req.method
  --pg1 := utl_http.request('https://api.mindflash.com/api/v2/auth?apikey='||api_key||'&email=ryano%40driggstitle.com');

 

utl_http.end_response(resp); 
ins_rtest2(pg2);
wl(pg2);

  apex_json.parse(pg2);
  score_v := apex_json.get_varchar2('finalGrade');  
wl('<br><br>');
wl(call_game_play.get_course_name(qt.mindfl_crs_id)||'<br>');
wl(score_v||' score');

--l_count := APEX_JSON.get_count(p_path => 'quizScores');
l_count := 0;
apex_json.parse(j,pg2);
FOR x IN 0..50 LOOP
  if apex_json.does_exist(p_path => 'quizScores[%d].quizName', p0 => X, p_values => j) THEN
    --wl('<br>in if '||x);
    l_count := x;
  END IF;
END LOOP;
--apex_json.find_paths_like(

FOR y IN 0..l_count LOOP
  qz_nm := apex_json.get_varchar2(p_path=>'quizScores[%d].quizName',p0=> Y,p_values=>j);
  qz_score := apex_json.get_varchar2(p_path=>'quizScores[%d].score',p0=> Y,p_values=>j);
  qz_status := apex_json.get_varchar2(p_path=>'quizScores[%d].gradeStatus',p0=> Y,p_values=>j);
  wl(qz_nm||'  '||qz_score||'  '||qz_status);
  wl('<br>');
END LOOP;

END;
/*
qz_nm VARCHAR2(4000);
qz_score VARCHAR2(4000);
qz_status VARCHAR2(4000);
*/
-------------------------------------------------------
PROCEDURE do_kindness_quest(gq_p NUMBER)
IS
--quest_complete_prompt_p
CURSOR gq_cur
IS
  SELECT *
    FROM game_user_quests
   WHERE usr_qst_id = gq_p;
gq_r gq_cur%ROWTYPE;

CURSOR qst_cur(id_p NUMBER)
IS
  SELECT *
    FROM call_game_quests
   WHERE quest_id = id_p;
q qst_cur%ROWTYPE;

CURSOR usr_cur
IS
  SELECT *
    FROM user_int_v u
   WHERE acc_account_id = sess.acct_id
     AND nvl(u.call_game_dept_id,0) > 0
   ORDER BY first_name,last_name;

CURSOR beg_cur(beg_p NUMBER)
IS
  SELECT *
    FROM call_game_start_screen
   WHERE screen_id = beg_p
   ORDER BY screen_name;
beg beg_cur%ROWTYPE;    

BEGIN
  OPEN gq_cur;
  FETCH gq_cur INTO gq_r;
  CLOSE gq_cur;
  
  OPEN qst_cur(gq_r.quest_id);
  FETCH qst_cur INTO q;
  CLOSE qst_cur;

wl('
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset=''UTF-8''>

  <title>'||get_user_name(gq_r.user_id)||' Do Quest</title>

  <link rel=''stylesheet'' href=''/css/style.css''>

  <script src="//ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script>

        <style>
            body {
                font-family: Comic Sans MS,  sans-serif;
                font-size:13pt;
                background-color: #ffffff;
                background-image: url("https://rightdata.driggstitle.com/callGame/Kwest_icons/Kwest_hub_background.png");
            }
            td{
              padding: 5px 5px 5px 5px;
            }

          </style>
</head>');
  wl('<body>');
call_game.disable_back;
wl('<style>
.btn {
  background: #eb393e;
  background-image: -webkit-linear-gradient(top, #eb393e, #eb393e);
  background-image: -moz-linear-gradient(top, #eb393e, #eb393e);
  background-image: -ms-linear-gradient(top, #eb393e, #eb393e);
  background-image: -o-linear-gradient(top, #eb393e, #eb393e);
  background-image: linear-gradient(to bottom, #eb393e, #eb393e);
  -webkit-border-radius: 28;
  -moz-border-radius: 28;
  border-radius: 28px;
  font-family: Arial;
  color: #ffffff;
  font-size: 20px;
  padding: 5px 10px 5px 10px;
  text-decoration: none;
}

.greyBtn {
  background: #BDC3C7;
  background-image: -webkit-linear-gradient(top, #BDC3C7, #BDC3C7);
  background-image: -moz-linear-gradient(top, #BDC3C7, #BDC3C7);
  background-image: -ms-linear-gradient(top, #BDC3C7, #BDC3C7);
  background-image: -o-linear-gradient(top, #BDC3C7, #BDC3C7);
  background-image: linear-gradient(to bottom, #BDC3C7, #BDC3C7);
  -webkit-border-radius: 28;
  -moz-border-radius: 28;
  border-radius: 28px;
  font-family: Arial;
  color: #ffffff;
  font-size: 20px;
  padding: 5px 10px 5px 10px;
  text-decoration: none;
}
</style>');
  wl('<h2><center>Kindness Quest</center></h2>');
  
  IF nvl(gq_r.link_to_grp_qst,0) = 0 THEN
    wl('Instructions: '||q.instructions);
  ELSE
    wl('Instructions: '||q.group_instructions);  
  END IF;
  wl('<br>');
  wl('Group Quest with: '||get_user_name(gq_r.link_to));
  wl('<br><br>');
  wl('<script>function subThis(){
                var nt = document.getElementById("user_complete_notes_p").value;
                location="/prv/call_game.complete_qst?qst_p='||gq_r.usr_qst_id||'&notes_p="+escape(nt);
              }
              function cplt(){
                try{
                  var dt = document.getElementById("dataFrame");
                  dt.contentWindow.subFm();
                }catch(e){}
                console.log("after catch");
                setTimeout("subThis()",500);
                console.log("after set timeout");
              }
              function cpltNotes(obj){
                var ob = document.getElementById("cpltBtn");
                if (obj.value.length >=5){
                  ob.disabled=false;
                  ob.className="btn"; 
                }else{
                  ob.disabled=true;
                  ob.className="greyBtn"; 
                }
              }
      </script>');
  IF nvl(length(q.quest_complete_prompt),0) > 1 THEN
    /*
    wl('<script>function updCpltNotes(obj){
                  var frm = document.getElementById("flash");
                  frm.src="/prv/call_game_play.upd_
                }
        </script>');
    wl('<iframe id="flash" style="display:none"></iframe>');
    */
    wl(q.quest_complete_prompt||': <textarea rows="6" cols="80" name="user_complete_notes_p" id="user_complete_notes_p" onkeyup="cpltNotes(this);">'||gq_r.user_complete_notes||'</textarea>');
    wl('<br><br>');
  ELSE
    wl('<input type="hidden" name="user_complete_notes_p" id="user_complete_notes_p" value="">');
  END IF;
  
  IF nvl(q.beg_screen,0) > 0 THEN
    OPEN beg_cur(q.beg_screen);
    FETCH beg_cur INTO beg;
    CLOSE beg_cur;
    IF nvl(beg.driggs_screen,'None') = 'R' THEN
      wl('<iframe width="100%" height="250px" id="dataFrame" frameborder="0" src="/prv/call_game_screen.recognize_someone?usr_qst_p='||gq_p||'"></iframe>');
    ELSIF nvl(beg.driggs_screen,'None') = 'S' THEN
      wl('<iframe width="100%" height="400px" id="dataFrame" frameborder="0" src="/prv/call_game_screen.suggest_a_quest?usr_qst_p='||gq_p||'"></iframe>');
    ELSIF nvl(beg.driggs_screen,'None') = 'U' THEN
      wl('<iframe width="100%" height="300px" id="dataFrame" frameborder="0" src="/prv/call_game_screen.upload_ids?usr_qst_p='||gq_p||'"></iframe>');
    ELSIF nvl(beg.driggs_screen,'None') = 'A' THEN
      wl('<iframe width="100%" height="300px" id="dataFrame" frameborder="0" src="/prv/call_game_screen.go_adp?usr_qst_p='||gq_p||'"></iframe>');
    ELSIF nvl(beg.driggs_screen,'None') = 'K' THEN
      wl('<iframe width="100%" height="300px" id="dataFrame" frameborder="0" src="/prv/call_game_screen.go_kplan?usr_qst_p='||gq_p||'"></iframe>');
  
    END IF;
  END IF;
  
  wl('<input type="button" name="cpltBtn" class="btn" id="cpltBtn" onclick="cplt();" value="Complete Quest">');
  
  IF nvl(length(q.quest_complete_prompt),0) > 1 THEN
    wl('<script>var ob = document.getElementById("cpltBtn");ob.disabled=true;ob.className="greyBtn";</script>');
  END IF;
  /*
  wl('<br><br>');
  IF nvl(gq_r.link_to_grp_qst,0) = 0 THEN
    wl('<hr>');
    wl('<script>function linkQst(valP){
                  location="/prv/call_game_play.assign_grp_usr?gq_p='||gq_p||'&user_p="+valP;
                }
        </script>');
    wl('Choose User: <select name="link_to_p" id="link_to_p" onchange="linkQst(this.value);">');
    wl('<option value="0">Choose User for Group Quest</option>');
    FOR u IN usr_cur LOOP
      IF u.user_id != gq_r.user_id THEN
        wl('<option value="'||u.user_id||'">'||u.first_name||' '||u.last_name||'</option>');
      END IF;
    END LOOP;
    wl('</select>');
    wl('<br><br>');
    wl('<script>function doSurGrp(){
                  location="/prv/call_game_play.assign_surprise?gq_p='||gq_p||'";
                }
        </script>');
    wl('<input type="button" name="doAsGrpBtn" id="doAsGrpBtn" value="Surprise Group Quest" onclick="doSurGrp();">');
  
  END IF;
  */
END;
-------------------------------------------------------
PROCEDURE assign_grp_usr(gq_p NUMBER,user_p NUMBER)
IS
CURSOR gq_cur
IS
  SELECT *
    FROM game_user_quests
   WHERE usr_qst_id = gq_p;
gq_r gq_cur%ROWTYPE;

CURSOR qst_cur(id_p NUMBER)
IS
  SELECT *
    FROM call_game_quests
   WHERE quest_id = id_p;
q qst_cur%ROWTYPE;

usr_qst_n NUMBER;
BEGIN
  OPEN gq_cur;
  FETCH gq_cur INTO gq_r;
  CLOSE gq_cur;

  OPEN qst_cur(gq_r.quest_id);
  FETCH qst_cur INTO q;
  CLOSE qst_cur;

  SELECT game_user_quests_s.NEXTVAL INTO usr_qst_n
    FROM dual;
  INSERT INTO game_user_quests(usr_qst_id,quest_id,user_id,assign_dt,init_grp_qst,link_to_grp_qst,link_to)
  VALUES(usr_qst_n,gq_r.quest_id,user_p,sysdate,0,gq_p,gq_r.user_id);
  COMMIT;

  UPDATE game_user_quests
     SET init_grp_qst = 1,
         link_to_grp_qst = usr_qst_n,
         link_to = user_p
   WHERE usr_qst_id = gq_p;
   
  wl('<script>location="/prv/call_game_play.do_kindness_quest?gq_p='||gq_p||'";</script>');
  --wl(gq_p||' in assign grp usr '||user_p);
END;
-------------------------------------------------------
PROCEDURE assign_surprise(gq_p NUMBER)
IS
CURSOR usr_cur
IS
  SELECT *
    FROM user_int_v
   WHERE acc_account_id = sess.acct_id
     AND nvl(call_game_dept_id,0) > 0
   ORDER BY first_name,last_name;

CURSOR gq_cur
IS
  SELECT *
    FROM game_user_quests
   WHERE usr_qst_id = gq_p;
gq_r gq_cur%ROWTYPE;

CURSOR qst_cur(id_p NUMBER)
IS
  SELECT *
    FROM call_game_quests
   WHERE quest_id = id_p;
qt qst_cur%ROWTYPE;

CURSOR type_cur(id_p NUMBER)
IS
  SELECT *
    FROM call_quest_sub_types
   WHERE sub_id = id_p;
t type_cur%ROWTYPE;
usr_qst_n NUMBER;

brch_n NUMBER:=0;

cnter NUMBER:=0;
show_b BOOLEAN;
rand_n NUMBER;

user_n NUMBER:=0;
BEGIN
  OPEN gq_cur;
  FETCH gq_cur INTO gq_r;
  CLOSE gq_cur;

  OPEN qst_cur(gq_r.quest_id);
  FETCH qst_cur INTO qt;
  CLOSE qst_cur;
  brch_n := get_user_def_branch_id(gq_r.user_id);
  
  OPEN type_cur(qt.sub_quest_type_id);
  FETCH type_cur INTO t;
  CLOSE type_cur;
  FOR u IN usr_cur LOOP
    show_b := TRUE;
    IF nvl(t.surprise_user_branch,0) = 1 THEN
      IF nvl(brch_n,0) != nvl(u.def_branch,-1) THEN
        show_b := FALSE;
      END IF; 
    END IF;
    IF nvl(t.surprise_user_dept,0) > 0 THEN
      IF u.call_game_dept_id NOT IN (t.surprise_user_dept,t.surprise_user_dept2,t.surprise_user_dept3) THEN
        show_b := FALSE;
      END IF;
    END IF;
    IF show_b THEN
      cnter := cnter + 1;
    END IF;
  END LOOP;
  rand_n := ceil(dbms_random.value(1,cnter));
  cnter := 0;
  FOR u IN usr_cur LOOP
    show_b := TRUE;
    IF nvl(t.surprise_user_branch,0) = 1 THEN
      IF nvl(brch_n,0) != nvl(u.def_branch,-1) THEN
        show_b := FALSE;
      END IF; 
    END IF;
    IF nvl(t.surprise_user_dept,0) > 0 THEN
      IF u.call_game_dept_id NOT IN (t.surprise_user_dept,t.surprise_user_dept2,t.surprise_user_dept3) THEN
        show_b := FALSE;
      END IF;
    END IF;
    IF show_b THEN
      cnter := cnter + 1;
      IF cnter = rand_n THEN
        user_n := u.user_id;
      END IF;
    END IF;
  END LOOP;
  
  --PROCEDURE assign_grp_usr(gq_p NUMBER,user_p NUMBER)
  call_game_play.assign_grp_usr(gq_p,user_n);
END;
/*
alter table call_quest_sub_types add(
surprise_user_branch NUMBER,
surprise_user_dept NUMBER,
surprise_user_dept2 NUMBER,
surprise_user_dept3 NUMBER
)

ALTER TABLE game_user_quests ADD(
init_grp_qst NUMBER DEFAULT 0,
link_to_grp_qst NUMBER DEFAULT 0,
link_to NUMBER DEFAULT 0,
suprise_bonus NUMBER DEFAULT 0

)
*/
-------------------------------------------------------
FUNCTION get_level_pts_pct(user_p NUMBER) RETURN NUMBER
IS
beg_pts NUMBER:=0;
end_pts NUMBER:=0;

curr_pts NUMBER:=0;

curr_lev NUMBER:=0;
prev_lev NUMBER:=0;

my_pts_this_level NUMBER:=0;
CURSOR lev_cur(lev_p NUMBER)
IS
  SELECT *
    FROM call_game_levels
   WHERE level_id = lev_p;
beg_lev_r lev_cur%ROWTYPE;
prev_lev_r lev_cur%ROWTYPE;

lev_pts NUMBER;

ret_n NUMBER;
BEGIN
  curr_lev := call_game.get_user_level_id(user_p);
  curr_pts := call_game.get_user_pts(user_p);
  OPEN lev_cur(curr_lev);
  FETCH lev_cur INTO beg_lev_r;
  CLOSE lev_cur;
  end_pts := beg_lev_r.pts_to_reach;
  IF curr_lev = 21 THEN
    beg_pts := 0;
  ELSE
    prev_lev := call_game.find_prev_level_id(curr_lev);
    OPEN lev_cur(prev_lev);
    FETCH lev_cur INTO prev_lev_r;
    CLOSE lev_cur;
    beg_pts := prev_lev_r.pts_to_reach;
  END IF;
  
  lev_pts := end_pts - beg_pts;
  
  my_pts_this_level := curr_pts - beg_pts;
  IF nvl(my_pts_this_level,0) = 0 THEN
    ret_n := 0;
  ELSE
    ret_n := my_pts_this_level / lev_pts;
    ret_n := ret_n * 100;
    ret_n := ceil(ret_n);
  END IF;
  RETURN ret_n;
END;
--FUNCTION find_prev_level_id(level_p NUMBER) RETURN NUMBER
-------------------------------------------------------
FUNCTION get_current_clot_dtl_id(usr_closet_p NUMBER) RETURN NUMBER
IS
cloth_id_n NUMBER;

to_do_n NUMBER;

done_n NUMBER;

CURSOR closet_item_cur
IS
  SELECT *
    FROM call_user_closet_items
   WHERE clos_item_id = usr_closet_p;
ci closet_item_cur%ROWTYPE;

CURSOR clothing_cur(id_p NUMBER)
IS
  SELECT *
    FROM call_clothing_header
   WHERE clothing_id = id_p;
cl clothing_cur%ROWTYPE;
CURSOR dtl_cur(cloth_p NUMBER)
IS
  SELECT *
    FROM call_clothing_detail
   WHERE clothing_id = cloth_p;
   
steps_done_tot NUMBER;

ret_n NUMBER:=0;
BEGIN
  OPEN closet_item_cur;
  FETCH closet_item_cur INTO ci;
  CLOSE closet_item_cur;
  
  OPEN clothing_cur(ci.clothing_id);
  FETCH clothing_cur INTO cl;
  CLOSE clothing_cur;
  
  to_do_n := call_game_closet.get_clothing_item_tot_do_cnt(ci.clothing_id);--this is steps * quests per step
  done_n := call_game_closet.get_closet_qst_comp_cnt(usr_closet_p,ci.user_id);
  IF done_n >= cl.quests_per_step THEN
    steps_done_tot := done_n / cl.quests_per_step;
    steps_done_tot := floor(steps_done_tot);
  ELSE
    steps_done_tot := 0;
  END IF;
  
  FOR d IN dtl_cur(ci.clothing_id) LOOP
    IF steps_done_tot = d.step_id THEN
      ret_n := d.dtl_id;
    END IF;
  END LOOP;
  RETURN ret_n;
END;
-------------------------------------------------------
FUNCTION get_clothing_type_by_user_id(usr_closet_p NUMBER) RETURN VARCHAR2
IS
dtl_n NUMBER;
CURSOR clothing_cur(id_p NUMBER)
IS
  SELECT *
    FROM call_clothing_header
   WHERE clothing_id = id_p;
cl clothing_cur%ROWTYPE;
CURSOR dtl_cur(dtl_p NUMBER)
IS
  SELECT *
    FROM call_clothing_detail
   WHERE dtl_id = dtl_p;
d dtl_cur%ROWTYPE;
BEGIN
  dtl_n := call_game_play.get_current_clot_dtl_id(usr_closet_p);
  
  OPEN dtl_cur(dtl_n);
  FETCH dtl_cur INTO d;
  CLOSE dtl_cur;
  
  OPEN clothing_cur(d.clothing_id);
  FETCH clothing_cur INTO cl;
  CLOSE clothing_cur;
  
  RETURN cl.clothing_type;
END;
--FUNCTION get_current_clot_dtl_id(usr_closet_p NUMBER) RETURN NUMBER
-------------------------------------------------------
FUNCTION get_clothing_img_by_usr_cl_id(usr_closet_p NUMBER) RETURN VARCHAR2
IS
dtl_n NUMBER;
BEGIN
  dtl_n := call_game_play.get_current_clot_dtl_id(usr_closet_p);
  RETURN call_game_play.get_clothing_dtl_img(dtl_n);
END;
-------------------------------------------------------
FUNCTION get_clothing_dtl_img(dtl_p NUMBER) RETURN VARCHAR2
IS
CURSOR dtl_cur
IS
  SELECT image_url
    FROM call_clothing_detail
   WHERE dtl_id = dtl_p;
url_v VARCHAR2(1000);
BEGIN
  OPEN dtl_cur;
  FETCH dtl_cur INTO url_v;
  CLOSE dtl_cur;
  RETURN url_v;
END;
-------------------------------------------------------
PROCEDURE main_game(user_p NUMBER:=0)
IS
usr_n NUMBER;
type_v VARCHAR2(1);
pts_n NUMBER:=0;
curr_n NUMBER:=0;
level_n NUMBER:=1;

CURSOR usr_cur
IS
  SELECT *
    FROM user_int_v
   WHERE user_id = user_p;
u usr_cur%ROWTYPE;
CURSOR quest_cur
IS
  SELECT q.*,call_game.get_level_num(q.min_level) min_lev,call_game.get_level_num(q.max_level) max_lev
    FROM call_game_quests q
   WHERE nvl(apply_to_eo,'N') = 'Y'
   ORDER BY min_lev,max_lev;
q_type VARCHAR2(100);

q_class VARCHAR2(100);

CURSOR qst_hist_cur(usr_p NUMBER)
IS
  SELECT *
    FROM game_user_quests
   WHERE user_id = usr_p
   ORDER BY usr_qst_id;
   
lev_id_n NUMBER;   

CURSOR level_cur(lev_p NUMBER)
IS
  SELECT *
    FROM call_game_levels l
   WHERE l.level_id = lev_p;
lv level_cur%ROWTYPE;

pct_n NUMBER;--:=call_game_play.get_level_pts_pct(user_p);

next_lev_num_n NUMBER;

usr_closet_id NUMBER;

cloth_id_n NUMBER;

CURSOR cloth_cur(id_p NUMBER)
IS
  SELECT *
    FROM call_clothing_header
   WHERE clothing_id = id_p;
cl cloth_cur%ROWTYPE;

CURSOR coords_cur
IS
  SELECT *
    FROM call_game_last_coord
   WHERE user_id = user_p;
cord coords_cur%ROWTYPE;
BEGIN
  ins_rtest('in main');
  OPEN coords_cur;
  FETCH coords_cur INTO cord;
  CLOSE coords_cur;
  DELETE call_game_last_coord
   WHERE user_id = user_p;
  COMMIT;
  INSERT INTO call_game_last_coord(user_id,last_date,last_x,hood_id)
  VALUES(user_p,sysdate,cord.last_x,cord.hood_id);
  COMMIT;
  usr_closet_id := call_game_closet.get_active_closet_item(user_p);
  cloth_id_n := call_game_closet.get_user_closet_cloth_itm_id(usr_closet_id);
  
  OPEN cloth_cur(cloth_id_n);
  FETCH cloth_cur INTO cl;
  CLOSE cloth_cur;
  
  usr_n := user_p;
  OPEN usr_cur;
  FETCH usr_cur INTO u;
  CLOSE usr_cur;

  lev_id_n := call_game.get_user_level_id(usr_n);
  
  OPEN level_cur(lev_id_n);
  FETCH level_cur INTO lv;
  CLOSE level_cur;
  
  pts_n := call_game.get_user_pts(usr_n);
  curr_n := call_game.get_user_curr(usr_n);
  --level_n := call_game.get_user_level(usr_n);
  level_n := call_game_closet.cnt_closet_comp(usr_n);
  next_lev_num_n := level_n + 1;
  pct_n := call_game_closet.get_closet_done_pct(usr_n);
  --call_game.game_hdr(get_user_name(usr_n)||' Game');
wl('
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset=''UTF-8''>

  <title>'||get_user_name(usr_n)||' Game</title>

  <link rel=''stylesheet'' href=''/css/style.css''>

  <script src="//ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script>

        <style>
            body {
                font-family: Comic Sans MS,  sans-serif;
                font-size:19pt;
                background-color: #ffffff;
                
            }
            .graph {
                width: 100%; /* width and height are arbitrary, just make sure the #bar styles are changed accordingly */
                height: 30px;
                border: 1px solid #888; 
                background: rgb(168,168,168);
                background: -moz-linear-gradient(top, rgba(168,168,168,1) 0%, rgba(204,204,204,1) 23%);
                background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(168,168,168,1)), color-stop(23%,rgba(204,204,204,1)));
                background: -webkit-linear-gradient(top, rgba(168,168,168,1) 0%,rgba(204,204,204,1) 23%);
                background: -o-linear-gradient(top, rgba(168,168,168,1) 0%,rgba(204,204,204,1) 23%);
                background: -ms-linear-gradient(top, rgba(168,168,168,1) 0%,rgba(204,204,204,1) 23%);
                filter: progid:DXImageTransform.Microsoft.gradient( startColorstr=''#eb393e'', endColorstr=''#cccccc'',GradientType=0 );
                background: linear-gradient(top, rgba(168,168,168,1) 0%,rgba(204,204,204,1) 23%);
                position: relative;
            }
            #bar {
                height: 29px; /* Not 30px because the 1px top-border brings it up to 30px to match #graph */
                background: #eb393e; 
                background: -moz-linear-gradient(top, rgb(235, 57, 62) 0%, rgb(235, 57, 62) 100%); 
                background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgb(235, 57, 62)), color-stop(100%,rgb(235, 57, 62))); 
                background: -webkit-linear-gradient(top, rgb(235, 57, 62) 0%,rgb(235, 57, 62) 100%); 
                background: -o-linear-gradient(top, rgb(235, 57, 62) 0%,rgb(235, 57, 62) 100%); 
                background: -ms-linear-gradient(top, rgb(235, 57, 62) 0%,rgb(235, 57, 62) 100%); 
                background: linear-gradient(top, rgb(235, 57, 62) 0%,rgb(235, 57, 62) 100%); 
                border-top: 1px solid #eb393e;
            }
            #bar p { position: absolute; text-align: center; width: 100%; margin: 0; line-height: 30px; }
            .error {
                /* These styles are arbitrary */
                background-color: #eb393e;
                padding: 1em;
                font-weight: bold;
                color: red;
                border: 1px solid red;
            }
            
img
{
	vertical-align: center;
  //margin-top: 20px
}
        </style>
</head>');
  wl('<body style="">');
  call_game.disable_back;
  wl('<script>function goHist(){
                var frm = document.getElementById("optsFrame");
                frm.src="/prv/call_game_play.show_quest_hist?user_p='||user_p||'";
              }
      </script>
      <script>
              function goPlay(){
                var frm = document.getElementById("optsFrame");
                frm.src="/prv/call_game_play.choose_quest_type?user_p='||user_p||'";

              }
              
              function goOpen(){
                var frm = document.getElementById("optsFrame");
                frm.src="/prv/call_game_play.show_open_quests?user_p='||user_p||'";
              }
              
              function feedback(){
                var frm = document.getElementById("optsFrame");
                frm.src="/prv/call_game_screen.give_feedback?user_p='||user_p||'";
                
              }
              
              function goGroup(){
                var frm = document.getElementById("optsFrame");
                frm.src="/prv/call_game_play.show_group?user_p='||user_p||'";
              }
      </script>');
      --show_quest_hist
  --wl('<h2><center>Main Game for '||get_user_name(usr_n)||' - Level '||level_n||'</center></h2>');
  --wl('<h4>Game for '||get_user_name(usr_n)||'</h4>');
  wl('<table width="100%">');
  wl('<tr>');
  wl('<td width="100%" valign="top">');
  wl('<table width="100%" cellspacing="0" cellpadding="0">');
  wl('<tr>');
  wl('<td width="5%" nowrap style="border-right-style:outset" onclick="goPlay();">
  &nbsp;
  <img src="https://rightdata.driggstitle.com/callGame/Kwest_icons/icon_home.png" width="44px" height="50px" onclick="goPlay();" style="color:blue;cursor:pointer;">
  </td>');
  --<img src="https://rightdata.driggstitle.com/callGame/Kwest_icons/icon_home.png" width="44px" height="50px" onclick="goPlay();" style="color:blue;cursor:pointer;">
  wl('<td width="2%">&nbsp;</td>');
  wl('<td width="19%" onclick="goHist();" style="color:blue;cursor:pointer;border-top-style:groove;border-bottom-style:groove;" nowrap >
  &nbsp;&nbsp;<img src="https://rightdata.driggstitle.com/callGame/Kwest_icons/icon_points.png" width="85px" height="30px" onclick="goHist();" style="color:blue;cursor:pointer;" >
  <span style="margin-bottom:10px;"> :'||pts_n||'</span><br>
  
  </td>');
  wl('<td width="19%" onclick="" style="cursor:hand;border-top-style:groove;border-bottom-style:groove;" nowrap valign="middle">
  <img src="https://rightdata.driggstitle.com/callGame/Kwest_icons/icon_currency.png" width="85px" height="30px" onclick="goHist();" style="color:blue;cursor:pointer;">
  :$'||curr_n||'<br></td>');
  
  wl('<td width="19%" onclick="" style="color:blue;cursor:pointer;border-top-style:groove;border-bottom-style:groove;" nowrap valign="middle"> 
  <img src="https://rightdata.driggstitle.com/callGame/Kwest_icons/icon_level.png" width="85px" height="30px" onclick="goHist();" style="color:blue;cursor:pointer;">
   :'||level_n||' Donations<br></td>');
  
  wl('<td width="19%" onclick="" style="color:blue;cursor:pointer;border-top-style:groove;border-bottom-style:groove;" nowrap valign="middle">
  <img src="https://rightdata.driggstitle.com/callGame/Kwest_icons/icon_expiring.png" width="85px" height="30px" onclick="goOpen();" style="color:blue;cursor:pointer;">
   :'||call_game_play.cnt_open_quests(user_p)||'<br></td>');
   
  wl('<td width="19%" onclick="" style="color:blue;cursor:pointer;border-top-style:groove;border-bottom-style:groove;" nowrap valign="middle"> 
  <img src="https://rightdata.driggstitle.com/callGame/Kwest_icons/icon_group.png" width="85px" height="30px" onclick="goGroup();" style="color:blue;cursor:pointer;">
  :0<br></td>');

 wl('<td width="19%" onclick="" style="color:blue;cursor:pointer;border-top-style:groove;border-bottom-style:groove;" nowrap valign="middle"> 
  <img src="//rightdata.driggstitle.com/callGame/Feedback_icon.png" width="85px" height="30px" onclick="feedback();" style="color:blue;cursor:pointer;">
  <br></td>');
  
 wl('</tr>');
  wl('</table>');
  
  wl('<hr style="border-top: 2px solid #eb393e;">');
  wl('<iframe id="optsFrame" src="/prv/call_game_play.choose_quest_type?user_p='||user_p||'" width="100%" height="550px" frameborder="0" ></iframe>');

  wl('<script>function goHood(){
                top.location="/prv/call_game_play.back_to_my_hood?user_p='||user_p||'";
              }
              function goCloset(){
                top.location="/prv/call_game_closet.user_closet?active_user_p='||user_p||'&home_owner='||user_p||'&hood_p='||cord.hood_id||'&x_coord_p='||cord.last_x||'";  
              }
      </script>');
  wl('
  
  <table width="100%">
  <tr>
  <td width="20%">
  <img src="https://rightdata.driggstitle.com/callGame/Kwest_icons/back_to_street.png" width="90px" height="45px" onclick="goHood();" style="cursor:pointer;">
  <img src="https://rightdata.driggstitle.com/callGame/back_to_closet.png" width="90px" height="45px" onclick="goCloset();" style="cursor:pointer;">
  
  </td>
  <td width="60%">
  
<div id="progress" class="graph"><div id="bar" style="width:'||pct_n||'%"><p>'||pct_n||'% of the way to level donation # '||next_lev_num_n||'</p></div></div>
</td>
<td width="20%" align="right">');
IF lower(call_game_play.get_clothing_type_by_user_id(usr_closet_id)) = 'shoes' THEN
  wl('<img src="'||call_game_play.get_clothing_img_by_usr_cl_id(usr_closet_id)||'" width="42px" height="42px" onclick="goCloset();">&nbsp;&nbsp;&nbsp;');
ELSE
  wl('<img src="'||call_game_play.get_clothing_img_by_usr_cl_id(usr_closet_id)||'" width="26px" height="42px" onclick="goCloset();">&nbsp;&nbsp;&nbsp;');
END IF;
--wl(cloth_id_n);
wl('</td>
</tr>
</table>
');
ins_rtest('in main game');
/*
<div class="meter red">
	<span style="width: '||pct_n||'%"></span>
</div>
*/  

/*
  wl('<br><br>');
  wl('<script>function rst(){
                location="/prv/call_game.reset_user?user_p='||usr_n||'";
              }
              function rstAll(){
                location="/prv/call_game.reset_all_users";
              }
      </script>');
  wl('<input type="button" name="rstBtn" id="rstBtn" value="Re-Set User" onclick="rst();">');
  wl('<br><br>');
  wl('<input type="button" name="rstBtn" id="rstBtn" value="Re-Set All Users" onclick="rstAll();">');
  wl('</td>');
  wl('<td width="4%">&nbsp;</td>');
  wl('<td width="48%" valign="top">');
  wl('&nbsp;');
  */
  /*
  wl('<h4><center>Quest History</center></h4>');
  wl('<script>function goItem(idP){
                var frm = document.getElementById("optsFrame");
                frm.src="/prv/call_game_play.get_quest_screen?gq_p="+idP;
              }
      </script>');
  wl('<p>Required Quests for next level<br>');
  IF nvl(lv.req_quest_id1,0) > 0 THEN
    wl(call_game.get_quest_name(lv.req_quest_id1)||'<br>');
  END IF;
  IF nvl(lv.req_quest_id2,0) > 0 THEN
    wl(call_game.get_quest_name(lv.req_quest_id2)||'<br>');
  END IF;
  IF nvl(lv.req_quest_id3,0) > 0 THEN
    wl(call_game.get_quest_name(lv.req_quest_id3)||'<br>');
  END IF;

  wl('</p>');
  wl('<table width="100%" border="1">');
  wl('<tr>');
  wl('<th>Date</th>');
  wl('<th>Completed</th>');
  wl('<th>Quest</th>');
  wl('<th>Points</th>');
  wl('<th>Currency</th>');
  wl('<th>Group With</th>');
  wl('<tr>');
  
  FOR qh IN qst_hist_cur(usr_n) LOOP
    IF qh.quest_id IN (lv.req_quest_id1,lv.req_quest_id2,lv.req_quest_id3) THEN
      wl('<tr style="background-color:pink">');
    ELSE
      wl('<tr>');
    END IF;
    IF nvl(call_game.get_usr_qst_pts(qh.usr_qst_id),0) = 0 THEN
      wl('<td onclick="goItem('||qh.usr_qst_id||');" style="color:blue;cursor:pointer;"><u>'||to_char(qh.assign_dt,'MM/DD/YYYY HH:MI AM')||'</u></td>');
    ELSE
      wl('<td>'||to_char(qh.assign_dt,'MM/DD/YYYY HH:MI AM')||'</td>');
    END IF;
    wl('<td>'||to_char(qh.complete_dt,'MM/DD/YYYY')||'</td>');
    wl('<td>'||call_game.get_quest_name(qh.quest_id)||'</td>');
    wl('<td>'||call_game.get_usr_qst_pts(qh.usr_qst_id)||'</td>');
    wl('<td>'||call_game.get_usr_qst_curr(qh.usr_qst_id)||'</td>');
    IF nvl(qh.link_to,0) = 0 THEN
      wl('<td>N/A</td>');
    ELSE
      wl('<td>'||get_user_name(qh.link_to)||'</td>');
    END IF;
    wl('</tr>');
  END LOOP;
  wl('</table>');
  */
  wl('</td>');
  wl('</tr>');
  wl('</table>');
END;
------------------------------------------------------
PROCEDURE back_to_my_hood(user_p NUMBER)
IS
CURSOR pref_cur
IS
  SELECT *
    FROM call_user_prefs
   WHERE user_id = user_p;
p pref_cur%ROWTYPE;
BEGIN
  OPEN pref_cur;
  FETCH pref_cur INTO p;
  CLOSE pref_cur;
  wl('<script>top.location="/prv/call_game_nav.nav?user_p='||user_p||'&hood_p='||p.hood_id||'";</script>');
END;
------------------------------------------------------
PROCEDURE re_assign_quest(user_p NUMBER,quest_p NUMBER)
IS
CURSOR open_cur
IS
  SELECT *
    FROM game_user_quests
   WHERE user_id = user_p
     AND quest_id = quest_p
     AND complete_dt IS NULL;
op open_cur%ROWTYPE;
BEGIN
  OPEN open_cur;
  FETCH open_cur INTO op;
  IF open_cur%FOUND THEN
    null;
    --PROCEDURE get_quest_screen(gq_p NUMBER)
    wl('<script>location="/prv/call_game_play.get_quest_screen?gq_p='||op.usr_qst_id||'";</script>');
  ELSE
    null;
    wl('<script>location="/prv/call_game_play.assign_quest_to_usr?user_p='||user_p||'&quest_p='||quest_p||'";</script>');
    --PROCEDURE assign_quest_to_usr(user_p NUMBER,quest_p NUMBER)
  END IF;
  CLOSE open_cur;
END;
------------------------------------------------------
FUNCTION cnt_open_quests(user_p NUMBER) RETURN NUMBER
IS
CURSOR qst_cur
IS
  SELECT count(1)
    FROM game_user_quests
   WHERE user_id = user_p
     AND complete_dt IS NULL;
cnt NUMBER:=0;
BEGIN
  OPEN qst_cur;
  FETCH qst_cur INTO cnt;
  CLOSE qst_cur;
  RETURN cnt;
END;
------------------------------------------------------
FUNCTION get_user_qst_cnt(user_p NUMBER,qst_p NUMBER) RETURN NUMBER
IS
CURSOR hist_cur
IS
  SELECT count(1)
    FROM game_user_quests gq
   WHERE user_id = user_p
     AND gq.quest_id = qst_p
     AND nvl(gq.quest_failed,0) = 0;
num NUMBER:=0;
BEGIN
  OPEN hist_cur;
  FETCH hist_cur INTO num;
  CLOSE hist_cur;
  RETURN num;
END;
------------------------------------------------------
PROCEDURE show_group(user_p NUMBER)
IS
BEGIN
wl('
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset=''UTF-8''>

  <title>'||get_user_name(user_p)||' Game</title>

  <link rel=''stylesheet'' href=''/css/style.css''>

  <script src="//ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script>

        <style>
            body {
                font-family: Comic Sans MS,  sans-serif;
                font-size:13pt;
                background-color: #ffffff;
                background-image: url("https://rightdata.driggstitle.com/callGame/Kwest_icons/Kwest_hub_background.png");
            }
            td{
              padding: 5px 5px 5px 5px;
            }

          </style>
</head>
<body>');
call_game.disable_back;
wl('<h2><center>Group Quests Coming Soon</center></h2>');  

wl('</body>');
wl('</html>');
END;
------------------------------------------------------
PROCEDURE show_open_quests(user_p NUMBER)
IS
CURSOR qst_hist_cur(usr_p NUMBER)
IS
  SELECT *
    FROM game_user_quests uq
   WHERE user_id = usr_p
     AND uq.complete_dt IS NULL
   ORDER BY uq.assign_dt DESC;
CURSOR quest_cur(id_p NUMBER)
IS
  SELECT q.*,call_game.get_level_num(q.min_level) min_lev,call_game.get_level_num(q.max_level) max_lev
    FROM call_game_quests q
   WHERE q.quest_id = id_p
   ORDER BY min_lev,max_lev;
qt quest_cur%ROWTYPE;
q_type VARCHAR2(100);

q_class VARCHAR2(100);

prev_q_cnt NUMBER:=0;
BEGIN
wl('
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset=''UTF-8''>

  <title>'||get_user_name(user_p)||' Game</title>

  <link rel=''stylesheet'' href=''/css/style.css''>

  <script src="//ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script>

        <style>
            body {
                font-family: Comic Sans MS,  sans-serif;
                font-size:13pt;
                background-color: #ffffff;
                background-image: url("https://rightdata.driggstitle.com/callGame/Kwest_icons/Kwest_hub_background.png");
            }
            td{
              padding: 5px 5px 5px 5px;
            }

          </style>
</head>
<body>');
call_game.disable_back;
wl('<h2><center>My Open Quests</center></h2>');

  wl('<script>
              function goItem(idP){
                //var frm = document.getElementById("optsFrame");
                location="/prv/call_game_play.get_quest_screen?gq_p="+idP;
              }
      </script>');
  wl('<table width="75%" border="1" cellspacing="0" cellpadding="0">');
  wl('<tr>');
  --wl('<th>&nbsp;</th>');
  wl('<th>Assigned</th>');
  wl('<th>Quest</th>');
  wl('<th>Client</th>');
  wl('<tr>');
  
  FOR qh IN qst_hist_cur(user_p) LOOP
    OPEN quest_cur(qh.quest_id);
    FETCH quest_cur INTO qt;
    CLOSE quest_cur;
    wl('<tr>');
   -- wl('<td><input type="button" name="doAgainBtn'||qh.usr_qst_id||'" id="doAgainBtn'||qh.usr_qst_id||'" value="Do Again" onclick="asgnAgain('||qh.quest_id||');">');
    IF nvl(call_game.get_usr_qst_pts(qh.usr_qst_id),0) = 0 THEN
      wl('<td onclick="goItem('||qh.usr_qst_id||');" style="color:blue;cursor:pointer;"><u>'||to_char(qh.assign_dt,'MM/DD/YYYY HH:MI AM')||'</u></td>');
    ELSE
      wl('<td>'||to_char(qh.assign_dt,'MM/DD/YYYY HH:MI AM')||'</td>');
    END IF;
    --wl('<td>'||to_char(qh.complete_dt,'MM/DD/YYYY')||'</td>');
    
    IF nvl(qt.mindfl_crs_id,0) > 0 THEN
      wl('<td>'||call_game.get_quest_name(qh.quest_id)||' Mindflash '||qh.quest_id||'</td>');
    ELSE
      wl('<td>'||call_game.get_quest_name(qh.quest_id)||' '||qh.quest_id||'</td>');
    END IF;
    IF nvl(qh.asg_cont_id,0) > 0 THEN
      wl('<td>'||fc.get_client_both_names_no_br(qh.asg_cont_id)||'</td>');
    ELSE
      wl('<td>N/A</td>');
    END IF;
    --wl('<td>'||call_game.get_usr_qst_pts(qh.usr_qst_id)||'</td>');
    --wl('<td>'||call_game.get_usr_qst_curr(qh.usr_qst_id)||'</td>');
    /*
    IF nvl(qh.link_to,0) = 0 THEN
      wl('<td>N/A</td>');
    ELSE
      wl('<td>'||get_user_name(qh.link_to)||'</td>');
    END IF;
    */
    wl('</tr>');
  END LOOP;
  wl('</table>');

wl('</body>');
wl('</html>');
END;
------------------------------------------------------
PROCEDURE show_quest_hist(user_p NUMBER)
IS
CURSOR usr_cur
IS
  SELECT *
    FROM user_int_v
   WHERE user_id = user_p;
u usr_cur%ROWTYPE;
CURSOR quest_cur(id_p NUMBER)
IS
  SELECT q.*,call_game.get_level_num(q.min_level) min_lev,call_game.get_level_num(q.max_level) max_lev
    FROM call_game_quests q
   WHERE q.quest_id = id_p
   ORDER BY min_lev,max_lev;
qt quest_cur%ROWTYPE;
q_type VARCHAR2(100);

q_class VARCHAR2(100);

CURSOR qst_hist_cur(usr_p NUMBER)
IS
  SELECT *
    FROM game_user_quests uq
   WHERE user_id = usr_p
     AND uq.complete_dt IS NOT NULL
   ORDER BY uq.complete_dt DESC;
   
lev_id_n NUMBER;   

CURSOR level_cur(lev_p NUMBER)
IS
  SELECT *
    FROM call_game_levels l
   WHERE l.level_id = lev_p;
lv level_cur%ROWTYPE;
usr_n NUMBER;

prev_q_cnt NUMBER:=0;
BEGIN
  usr_n := user_p;
  OPEN usr_cur;
  FETCH usr_cur INTO u;
  CLOSE usr_cur;

  lev_id_n := call_game.get_user_level_id(usr_n);
  
  OPEN level_cur(lev_id_n);
  FETCH level_cur INTO lv;
  CLOSE level_cur;

  --set_hdr('User Quest History');
wl('
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset=''UTF-8''>

  <title>'||get_user_name(usr_n)||' Game</title>

  <link rel=''stylesheet'' href=''/css/style.css''>

  <script src="//ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script>

        <style>
            body {
                font-family: Comic Sans MS,  sans-serif;
                font-size:13pt;
                background-color: #ffffff;
                background-image: url("https://rightdata.driggstitle.com/callGame/Kwest_icons/Kwest_hub_background.png");
            }
          </style>
');          
wl('<style>
.btn {
  background: #eb393e;
  background-image: -webkit-linear-gradient(top, #eb393e, #eb393e);
  background-image: -moz-linear-gradient(top, #eb393e, #eb393e);
  background-image: -ms-linear-gradient(top, #eb393e, #eb393e);
  background-image: -o-linear-gradient(top, #eb393e, #eb393e);
  background-image: linear-gradient(to bottom, #eb393e, #eb393e);
  -webkit-border-radius: 28;
  -moz-border-radius: 28;
  border-radius: 28px;
  font-family: Arial;
  color: #ffffff;
  font-size: 13px;
  padding: 5px 10px 5px 10px;
  text-decoration: none;
}
td{
padding: 5px 5px 5px 5px;
}
</style>');

wl('
</head>
<body>');
call_game.disable_back;
  wl('<script>function goHist(){
                //var frm = document.getElementById("optsFrame");
                location="/prv/call_game_play.show_quest_hist?user_p='||user_p||'";
              }
              function goItem(idP){
                //var frm = document.getElementById("optsFrame");
                location="/prv/call_game_play.get_quest_screen?gq_p="+idP;
              }
      </script>');

  --wl('<p>Required Quests for next level<br>');
  wl('<h2><center>My Quest History</center></h2>');
  IF nvl(lv.req_quest_id1,0) > 0 THEN
    wl(call_game.get_quest_name(lv.req_quest_id1)||'<br>');
  END IF;
  IF nvl(lv.req_quest_id2,0) > 0 THEN
    wl(call_game.get_quest_name(lv.req_quest_id2)||'<br>');
  END IF;
  IF nvl(lv.req_quest_id3,0) > 0 THEN
    wl(call_game.get_quest_name(lv.req_quest_id3)||'<br>');
  END IF;
  wl('</p>');
  wl('<script>function asgnAgain(idP){
                location="/prv/call_game_play.re_assign_quest?user_p='||user_p||'&quest_p="+idP;
              }
      </script>');
      --PROCEDURE re_assign_quest(user_p NUMBER,quest_p NUMBER)
  wl('<table width="100%" border="1" cellspacing="0" cellpadding="0">');
  wl('<tr>');
  wl('<th>&nbsp;</th>');
  wl('<th>Date</th>');
  wl('<th>Completed</th>');
  wl('<th>Quest</th>');
  wl('<th>Points</th>');
  wl('<th>Currency</th>');
  wl('<th>Group With</th>');
  wl('<th>Info</th>');
  wl('<tr>');
  
  FOR qh IN qst_hist_cur(user_p) LOOP
    OPEN quest_cur(qh.quest_id);
    FETCH quest_cur INTO qt;
    CLOSE quest_cur;
    IF qh.quest_id IN (lv.req_quest_id1,lv.req_quest_id2,lv.req_quest_id3) THEN
      wl('<tr style="background-color:pink">');
    ELSE
      wl('<tr>');
    END IF;
    IF nvl(qt.quiz_id,0) > 0 THEN
      prev_q_cnt := 0;
      prev_q_cnt := call_game_play.get_user_qst_cnt(user_p,qt.quest_id);
      --FUNCTION get_user_qst_cnt(user_p NUMBER,qst_p NUMBER) RETURN NUMBER
      IF prev_q_cnt >= 5 THEN
        wl('<td>Completed '||prev_q_cnt||'</td>');
      ELSE
        IF call_game.get_quest_type_name_by_sub(qt.sub_quest_type_id) LIKE 'Kind%' THEN
          wl('<td>Kindness Quest Completed');
        ELSE
          wl('<td><input type="button" class="btn" name="doAgainBtn'||qh.usr_qst_id||'" id="doAgainBtn'||qh.usr_qst_id||'" value="Do Again" onclick="asgnAgain('||qh.quest_id||');">');
        END IF;
      END IF;
    ELSE
        IF call_game.get_quest_type_name_by_sub(qt.sub_quest_type_id) LIKE 'Kind%' THEN
          wl('<td>Kindness Quest Completed');
        ELSIF call_game.get_quest_type_name_by_sub(qt.sub_quest_type_id) LIKE 'Knowledge%' THEN
          wl('<td>Knowlege Quest Completed');

        ELSE
         wl('<td><input type="button" class="btn" name="doAgainBtn'||qh.usr_qst_id||'" id="doAgainBtn'||qh.usr_qst_id||'" value="Do Again" onclick="asgnAgain('||qh.quest_id||');">');
        END IF;
    END IF;
    IF nvl(call_game.get_usr_qst_pts(qh.usr_qst_id),0) = 0 AND nvl(qh.quest_failed,0) = 0 THEN
      wl(to_char(qh.assign_dt,'MM/DD/YYYY HH:MI AM'));
      --wl('<td onclick="goItem('||qh.usr_qst_id||');" style="color:blue;cursor:pointer;"><u>'||to_char(qh.assign_dt,'MM/DD/YYYY HH:MI AM')||'</u></td>');
    ELSE
      wl('<td>'||to_char(qh.assign_dt,'MM/DD/YYYY HH:MI AM')||'</td>');
    END IF;
    wl('<td>'||to_char(qh.complete_dt,'MM/DD/YYYY')||'</td>');
    IF nvl(qt.mindfl_crs_id,0) > 0 THEN
      wl('<td>'||call_game.get_quest_name(qh.quest_id)||'</td>');
    ELSE
      wl('<td>'||call_game.get_quest_name(qh.quest_id)||'</td>');
    END IF;
    wl('<td>'||nvl(call_game.get_usr_qst_pts(qh.usr_qst_id),0)||'</td>');
    wl('<td>'||nvl(call_game.get_usr_qst_curr(qh.usr_qst_id),0)||'</td>');
    IF nvl(qh.link_to,0) = 0 THEN
      wl('<td>N/A</td>');
    ELSE
      wl('<td>'||get_user_name(qh.link_to)||'</td>');
    END IF;
    IF nvl(qh.asg_cont_id,0) > 0 THEN
      wl('<td>'||fc.get_client_both_names_no_br(qh.asg_cont_id)||'</td>');
    ELSE
      wl('<td>'||qh.comments||'&nbsp;</td>');
    END IF;
    wl('</tr>');
  END LOOP;
  wl('</table>');
END;
------------------------------------------------------
PROCEDURE ins_quiz(quest_p NUMBER,quiz_name_p VARCHAR2)
IS
id_n NUMBER;
BEGIN
  SELECT call_game_quiz_s.NEXTVAL INTO id_n
    FROM dual;
  INSERT INTO call_game_quiz(quiz_id,quiz_name)
  VALUES(id_n,quiz_name_p);

 wl('<script>location="/prv/call_game_play.edit_quiz?id_p='||id_n||'";</script>');
/*
CREATE TABLE call_game_quiz(
quiz_id NUMBER,
quiz_name VARCHAR2(1000),
*/
END;
------------------------------------------------------
FUNCTION get_quiz_type_name(tp_p NUMBER) RETURN VARCHAR2
IS
CURSOR qz_type_cur
IS
  SELECT type_name
    FROM quiz_types
   WHERE quiz_type_id = tp_p;
type_v VARCHAR2(100);
BEGIN
  OPEN qz_type_cur;
  FETCH qz_type_cur INTO type_v;
  CLOSE qz_type_cur;
  RETURN type_v;
END;

------------------------------------------------------
PROCEDURE edit_quiz(id_p NUMBER)
IS
CURSOR quiz_cur
IS
  SELECT *
    FROM call_game_quiz;
qz quiz_cur%ROWTYPE;

CURSOR quest_cur
IS
  SELECT *
    FROM call_game_quiz_questions
   WHERE quiz_id = id_p;
BEGIN
  OPEN quiz_cur;
  FETCH quiz_cur INTO qz;
  CLOSE quiz_cur;
  set_hdr('Edit Quiz '||qz.quiz_name);
  wl('<body>');
  wl('<h2><center>Edit Quiz '||qz.quiz_name||'</center></h2>');
  
  wl('<table width="100%" border="1">');
  wl('<tr>');
  wl('<th>Question</th>');
  wl('<th>Type</th>');
  
  wl('</tr>');
  FOR qt IN quest_cur LOOP
    wl('<tr>');
    wl('<td><a href="/prv/call_game_play.edit_quiz_quest?id_p='||qt.quiz_quest_id||'" target="_blank">'||qt.quiz_question||'&nbsp;&nbsp;</a></td>');
    wl('<td>'||call_game_play.get_quiz_type_name(qt.quiz_type)||'</td>');
    wl('</tr>');
  END LOOP;
  wl('</table>');
  wl('<br><br>');
  wl('<script>function addQst(){
                location="/prv/call_game_play.ins_quiz_quest?quiz_p='||id_p||'";
              }
      </script>');
  wl('<input type="button" name="addQstBtn" id="addQstBtn" value="Add Quiz Question" onclick="addQst();">');
  wl('</body>');
  wl('</html>');
END;
------------------------------------------------------
PROCEDURE ins_quiz_quest(quiz_p NUMBER)
IS
qz_id_n NUMBER;
BEGIN
  SELECT call_game_quiz_questions_s.NEXTVAL INTO qz_id_n
    FROM dual;
  INSERT INTO call_game_quiz_questions(quiz_quest_id,quiz_id)
  VALUES(qz_id_n,quiz_p);
  wl('<script>location="/prv/call_game_play.edit_quiz_quest?id_p='||qz_id_n||'";</script>');
END;
------------------------------------------------------
PROCEDURE edit_quiz_quest(id_p NUMBER)
IS
CURSOR quiz_quest_cur
IS
  SELECT *
    FROM call_game_quiz_questions
   WHERE quiz_quest_id = id_p;
qz quiz_quest_cur%ROWTYPE;

CURSOR quiz_cur(quiz_id_p NUMBER)
IS
  SELECT *
    FROM call_game_quiz
   WHERE quiz_id = quiz_id_p;
quiz_r quiz_cur%ROWTYPE;

CURSOR qst_cur(quiz_id_p NUMBER)
IS
  SELECT *
    FROM call_game_quests
   WHERE quiz_id = quiz_id_p;
   
CURSOR qz_type_cur
IS
  SELECT *
    FROM quiz_types
   WHERE quiz_type_id != 4
   ORDER BY quiz_type_id;
BEGIN
  OPEN quiz_quest_cur;
  FETCH quiz_quest_cur INTO qz;
  CLOSE quiz_quest_cur;
  
  OPEN quiz_cur(qz.quiz_id);
  FETCH quiz_cur INTO quiz_r;
  CLOSE quiz_cur;

  --wl('in edit quiz '||id_p);
  set_hdr('Edit Quiz '||quiz_r.quiz_name);
  wl('<body>');
  wl('<h2><center>Edit Quiz <a href="/prv/call_game_play.edit_quiz?id_p='||qz.quiz_id||'">'||quiz_r.quiz_name||'</a></center></h2>');
  
  wl('<p>Linked Quests:<br>');
  FOR q IN qst_cur(qz.quiz_id) LOOP
    wl(q.quest_name||'<br>');
  END LOOP;
  wl('</p>');
  
  wl('<form name="editQuizForm" action="!call_game_play.edit_quiz_quest_sub" method="POST">');
  wl('<input type="hidden" name="id_p" value="'||id_p||'">');
  wl('<table width="100%">');
  wl('<col width="25%"><col width="75%">');
  wl('<script>function chgQTp(){
                document.forms[0].submit();
              }
      </script>');
      
  wl('<tr>');
  wl('<td align="right">Quiz Question:</td>');
  wl('<td>');
  wl('<input type="text" name="quiz_question_p" id="quiz_question_p" value="'||qz.quiz_question||'" size="100">');
  wl('</td>');
  wl('</tr>');
  wl('<tr>');
  wl('<td align="right">');
  wl('Quiz Type:');
  wl('</td>');
  wl('<td>');
  wl('<select name="quiz_type_p" id="quiz_type_p" onchange="chgQTp();">');
  wl('<option value="0">Choose Quiz Type:</option>');
  FOR qt IN qz_type_cur LOOP
    IF nvl(qt.quiz_type_id,0) = qz.quiz_type THEN
      wl('<option value="'||qt.quiz_type_id||'" selected>'||qt.type_name||'</option>');
    ELSE
      wl('<option value="'||qt.quiz_type_id||'">'||qt.type_name||'</option>');
    END IF;
  END LOOP;
  
  wl('</select>');
  wl('</td>');
  wl('</tr>');
  IF nvl(qz.quiz_type,0) > 0 THEN
    call_game_play.layout_quiz_row(1,qz.quiz_type,qz.ans1,qz.ans1_seq,qz.ans1_corr);
    
    call_game_play.layout_quiz_row(2,qz.quiz_type,qz.ans2,qz.ans2_seq,qz.ans2_corr);
    
    call_game_play.layout_quiz_row(3,qz.quiz_type,qz.ans3,qz.ans3_seq,qz.ans3_corr);
    
    call_game_play.layout_quiz_row(4,qz.quiz_type,qz.ans4,qz.ans4_seq,qz.ans4_corr);
    
    call_game_play.layout_quiz_row(5,qz.quiz_type,qz.ans5,qz.ans5_seq,qz.ans5_corr);
    
    call_game_play.layout_quiz_row(6,qz.quiz_type,qz.ans6,qz.ans6_seq,qz.ans6_corr);
    
    call_game_play.layout_quiz_row(7,qz.quiz_type,qz.ans7,qz.ans7_seq,qz.ans7_corr);
  END IF;
  wl('</table>');
  wl('<br><br><input type="submit" name="sub" value="Submit">');
  wl('</form>');
  wl('</body>');
  wl('</html>');

END;
------------------------------------------------------
PROCEDURE layout_quiz_row(num_p NUMBER,type_p NUMBER,ans_val VARCHAR2,seq_val NUMBER,corr_val NUMBER)
IS
BEGIN
    wl('<tr><td>&nbsp;</td></tr>');
    wl('<tr>');
    wl('<td align="right">Answer '||num_p||':</td>');
    wl('<td><input type="text" name="ans'||num_p||'_p" id="ans'||num_p||'_p" value="'||ans_val||'" size="50"></td>');
    wl('</tr>');
    IF type_p = 4 THEN
      wl('<tr>');
      wl('<td align="right">Enter Sequence Order:</td>');
      wl('<td><input type="text" name="ans'||num_p||'_seq_p" id="ans'||num_p||'_seq_p" value="'||seq_val||'" size="4"></td>');
      wl('</tr>');
    ELSE
      wl('<tr>');
      wl('<td align="right">Is Answer Correct:</td>');
      wl('<td>');
      wl('<select name="ans'||num_p||'_corr_p" id="ans'||num_p||'_corr_p">');
      IF nvl(corr_val,0) = 0 THEN
        wl('<option value="1">Yes</option>');
        wl('<option value="0" selected>No</option>');
      ELSE
        wl('<option value="1" selected>Yes</option>');
        wl('<option value="0">No</option>');
      END IF;
      wl('</select>');
      wl('</td>');
      wl('</tr>');
    END IF;  
END;
------------------------------------------------------
PROCEDURE edit_quiz_quest_sub(num_entries INTEGER,
                              name_array  OWA.VC_ARR, 
                              value_array OWA.VC_ARR,
                              reserved    OWA.VC_ARR)
IS
id_p NUMBER;
quiz_name_p VARCHAR2(500);
quiz_question_p VARCHAR2(4000);
quiz_type_p NUMBER;

ans1_p VARCHAR2(1000);
ans1_corr_p NUMBER;
ans1_seq_p NUMBER;

ans2_p VARCHAR2(1000);
ans2_corr_p NUMBER;
ans2_seq_p NUMBER;

ans3_p VARCHAR2(1000);
ans3_corr_p NUMBER;
ans3_seq_p NUMBER;

ans4_p VARCHAR2(1000);
ans4_corr_p NUMBER;
ans4_seq_p NUMBER;

ans5_p VARCHAR2(1000);
ans5_corr_p NUMBER;
ans5_seq_p NUMBER;

ans6_p VARCHAR2(1000);
ans6_corr_p NUMBER;
ans6_seq_p NUMBER;

ans7_p VARCHAR2(1000);
ans7_corr_p NUMBER;
ans7_seq_p NUMBER;

CURSOR quest_cur(id_p NUMBER)
IS
  SELECT *
    FROM call_game_quiz_questions
   WHERE quiz_quest_id = id_p;
qt quest_cur%ROWTYPE;
BEGIN
  null;

  FOR x IN 1..num_entries LOOP
    IF lower(name_array(x)) = 'id_p' THEN
      id_p := value_array(x);
    ELSIF lower(name_array(x)) = 'quiz_name_p' THEN
      quiz_name_p := value_array(x);
    ELSIF lower(name_array(x)) = 'quiz_question_p' THEN
      quiz_question_p := value_array(x);
    ELSIF lower(name_array(x)) = 'quiz_type_p' THEN
      quiz_type_p := value_array(x);
      UPDATE call_game_quiz_questions
         SET quiz_type = quiz_type_p,
             quiz_question = quiz_question_p
       WHERE quiz_quest_id = id_p;
      IF nvl(length(quiz_question_p),0) = 0 AND nvl(quiz_type_p,0) = 0 THEN
        DELETE call_game_quiz_questions
         WHERE quiz_quest_id = id_p;
      END IF;
    END IF;
  END LOOP;
  commit;
  
  FOR y IN 1..num_entries LOOP
    IF lower(name_array(y)) = 'ans1_p' THEN
      ans1_p := value_array(y);
    ELSIF lower(name_array(y)) = 'ans1_corr_p' THEN
      ans1_corr_p := value_array(y);
    ELSIF lower(name_array(y)) = 'ans1_seq_p' THEN
      ans1_seq_p := value_array(y);

    ELSIF lower(name_array(y)) = 'ans2_p' THEN
      ans2_p := value_array(y);
    ELSIF lower(name_array(y)) = 'ans2_corr_p' THEN
      ans2_corr_p := value_array(y);
    ELSIF lower(name_array(y)) = 'ans2_seq_p' THEN
      ans2_seq_p := value_array(y);

    ELSIF lower(name_array(y)) = 'ans3_p' THEN
      ans3_p := value_array(y);
    ELSIF lower(name_array(y)) = 'ans3_corr_p' THEN
      ans3_corr_p := value_array(y);
    ELSIF lower(name_array(y)) = 'ans3_seq_p' THEN
      ans3_seq_p := value_array(y);

    ELSIF lower(name_array(y)) = 'ans4_p' THEN
      ans4_p := value_array(y);
    ELSIF lower(name_array(y)) = 'ans4_corr_p' THEN
      ans4_corr_p := value_array(y);
    ELSIF lower(name_array(y)) = 'ans4_seq_p' THEN
      ans4_seq_p := value_array(y);

    ELSIF lower(name_array(y)) = 'ans5_p' THEN
      ans5_p := value_array(y);
    ELSIF lower(name_array(y)) = 'ans5_corr_p' THEN
      ans5_corr_p := value_array(y);
    ELSIF lower(name_array(y)) = 'ans5_seq_p' THEN
      ans5_seq_p := value_array(y);

    ELSIF lower(name_array(y)) = 'ans6_p' THEN
      ans6_p := value_array(y);
    ELSIF lower(name_array(y)) = 'ans6_corr_p' THEN
      ans6_corr_p := value_array(y);
    ELSIF lower(name_array(y)) = 'ans6_seq_p' THEN
      ans6_seq_p := value_array(y);

    ELSIF lower(name_array(y)) = 'ans7_p' THEN
      ans7_p := value_array(y);
    ELSIF lower(name_array(y)) = 'ans7_corr_p' THEN
      ans7_corr_p := value_array(y);
    ELSIF lower(name_array(y)) = 'ans7_seq_p' THEN
      ans7_seq_p := value_array(y);
      
    END IF;
  END LOOP;

  UPDATE call_game_quiz_questions
     SET ans1 = ans1_p,
         ans1_corr = ans1_corr_p,
         ans1_seq = ans1_seq_p,
         ans2 = ans2_p,
         ans2_corr = ans2_corr_p,
         ans2_seq = ans2_seq_p,
         ans3 = ans3_p,
         ans3_corr = ans3_corr_p,
         ans3_seq = ans3_seq_p,
         ans4 = ans4_p,
         ans4_corr = ans4_corr_p,
         ans4_seq = ans4_seq_p,
         ans5 = ans5_p,
         ans5_corr = ans5_corr_p,
         ans5_seq = ans5_seq_p,
         ans6 = ans6_p,
         ans6_corr = ans6_corr_p,
         ans6_seq = ans6_seq_p,
         ans7 = ans7_p,
         ans7_corr = ans7_corr_p,
         ans7_seq = ans7_seq_p
   WHERE quiz_quest_id = id_p;

  COMMIT;
  
  OPEN quest_cur(id_p);
  FETCH quest_cur INTO qt;
  CLOSE quest_cur;  
  wl('<script>location="/prv/call_game_play.edit_quiz_quest?id_p='||id_p||'";</script>');

END;
------------------------------------------------------
FUNCTION cnt_quiz_quests(id_p NUMBER) RETURN NUMBER
IS
CURSOR quest_cur
IS
  SELECT count(1)
    FROM call_game_quiz_questions
   WHERE quiz_id = id_p;
cnt NUMBER:=0;
BEGIN
  OPEN quest_cur;
  FETCH quest_cur INTO cnt;
  CLOSE quest_cur;
  RETURN cnt;
END;
------------------------------------------------------
PROCEDURE quiz_setup
IS
CURSOR qz_cur(act_p VARCHAR2)
IS
  SELECT *
    FROM call_game_quiz qz
   WHERE nvl(active,'A') = act_p
   ORDER BY qz.quiz_name;
   
CURSOR qst_cur(qz_id_p NUMBER)
IS
  SELECT *
    FROM call_game_quests qs
   WHERE nvl(qs.quiz_id,0) = qz_id_p
      OR qs.quiz2_id = qz_id_p
      OR qs.quiz3_id = qz_id_p
      OR qs.quiz4_id = qz_id_p
      OR qs.quiz5_id = qz_id_p
   ORDER BY quest_name;
BEGIN
  set_hdr('Quiz Setup');
  wl('<body>');
  wl('<h2><center>Quiz Setup</center></h2>');
  
  wl('<table width="100%" border="1">');
  wl('<tr>');
  wl('<th>&nbsp;</th>');
  wl('<th>Quiz Name</th>');
  wl('<th># of Questions</th>');
  wl('<th>Quest(s) Assigned</th>');
  
  wl('</tr>');
  FOR qz IN qz_cur('A') LOOP
    wl('<tr>');
    wl('<td><input type="button" name="setQzBtn'||qz.quiz_id||'" id="setQzBtn'||qz.quiz_id||'" value="Set Inactive" onclick="setAct('||qz.quiz_id||',''I'');"></td>');
    wl('<td><a href="/prv/call_game_play.edit_quiz?id_p='||qz.quiz_id||'" target="_blank">'||qz.quiz_name||'</a></td>');
    wl('<td>'||call_game_play.cnt_quiz_quests(qz.quiz_id)||'</td>');
    wl('<td>');
    FOR q IN qst_cur(qz.quiz_id) LOOP
      wl(q.quest_name||'<br>');
    END LOOP;
    wl('</td>');
    
    wl('</tr>');
  END LOOP;
  wl('</table>');
  wl('<br><br>');
  wl('<script>function setAct(idP,actP){
                location="/prv/call_game_play.set_quiz_act?id_p="+idP+"&act_p="+actP;
              }
      </script>');
  wl('<script>function crtQuiz(){
                  var prpt = prompt("Enter name of New Quiz","");
                  window.open("/prv/call_game_play.ins_quiz?quest_p=-1&quiz_name_p="+prpt,"_blank"); 
              }
      </script>');
  wl('<input type="button" name="crtBtn" value="Create New Quiz" onclick="crtQuiz();">');
wl('<hr>');
wl('<h3><center>Inactive Quizes</center></h3>');

  wl('<table width="100%" border="1">');
  wl('<tr>');
  wl('<th>&nbsp;</th>');
  wl('<th>Quiz Name</th>');
  wl('<th># of Questions</th>');
  wl('<th>Quest(s) Assigned</th>');
  
  wl('</tr>');
  FOR qz IN qz_cur('I') LOOP
    wl('<tr>');
    wl('<td><input type="button" name="setQzBtn'||qz.quiz_id||'" id="setQzBtn'||qz.quiz_id||'" value="Set Active" onclick="setAct('||qz.quiz_id||',''A'');"></td>');
    wl('<td><a href="/prv/call_game_play.edit_quiz?id_p='||qz.quiz_id||'" target="_blank">'||qz.quiz_name||'</a></td>');
    wl('<td>'||call_game_play.cnt_quiz_quests(qz.quiz_id)||'</td>');
    wl('<td>');
    FOR q IN qst_cur(qz.quiz_id) LOOP
      wl(q.quest_name||'<br>');
    END LOOP;
    wl('</td>');
    
    wl('</tr>');
  END LOOP;
  wl('</table>');
  wl('<br><br>');

END;
------------------------------------------------------
PROCEDURE set_quiz_act(id_p NUMBER,act_p VARCHAR2)
IS
BEGIN
  UPDATE call_game_quiz
     SET active = act_p
   WHERE quiz_id = id_p;
  WL('<script>location="/prv/call_game_play.quiz_setup";</script>');
END;
------------------------------------------------------
FUNCTION is_quiz_question_passed(user_p NUMBER,quest_p NUMBER) RETURN BOOLEAN
IS
CURSOR q_cur
IS
  SELECT *
    FROM call_game_user_quiz_questions
   WHERE user_id = user_p
     AND quest_id = quest_p
     AND nvl(correct,0) = 1;
q_r q_cur%ROWTYPE;
ret_b BOOLEAN:=FALSE;
BEGIN
  OPEN q_cur;
  FETCH q_cur INTO q_r;
  IF q_cur%FOUND THEN
    ret_b := TRUE;
  END IF;
  CLOSE q_cur;
  RETURN ret_b;
END;
------------------------------------------------------
PROCEDURE take_quiz(usr_qst_p NUMBER)
IS
CURSOR gq_cur
IS
  SELECT *
    FROM game_user_quests
   WHERE usr_qst_id = usr_qst_p;
gq_r gq_cur%ROWTYPE;

CURSOR qst_cur(qst_p NUMBER)
IS
  SELECT *
    FROM call_game_quests
   WHERE quest_id = qst_p;
qr qst_cur%ROWTYPE;
user_game_qz_id NUMBER;

nxt_quest_id NUMBER;
quest_id_n NUMBER;

CURSOR questions_cur(qz_p NUMBER,user_p NUMBER)
IS
  SELECT *
    FROM call_game_quiz_questions qs
   WHERE quiz_id = qz_p
     AND qs.quiz_quest_id NOT IN (SELECT quiz_quest_id FROM call_game_user_quiz_questions WHERE user_id = user_p AND nvl(correct,0) = 1)
   ORDER BY quiz_quest_id;
question_cnt NUMBER:=0;

do_quest NUMBER;

cnter NUMBER:=0;
BEGIN
  OPEN gq_cur;
  FETCH gq_cur INTO gq_r;
  CLOSE gq_cur;
  
  OPEN qst_cur(gq_r.quest_id);
  FETCH qst_cur INTO qr;
  CLOSE qst_cur;
  
  SELECT call_game_user_quiz_s.NEXTVAL INTO user_game_qz_id
    FROM dual;
  INSERT INTO call_game_user_quiz(usr_quiz_id,user_id,usr_quest_id,quest_id,quiz_id,date_taken)
  VALUES(user_game_qz_id,gq_r.user_id,gq_r.usr_qst_id,gq_r.quest_id,qr.quiz_id,sysdate);
  COMMIT;
  ---------------------------------------------------------------------
  FOR qst IN questions_cur(qr.quiz_id,gq_r.user_id) LOOP
    question_cnt := question_cnt + 1;
  END LOOP;
  --THIS GETS THE TOTAL # OF QUESTIONS ATTACHED TO THIS QUIZ
  
  --WE WILL DIVIDE BY 5.  THEY CAN DO EACH KNOWLEGE QUEST 5 TIMES AND EARN POINTS  SO WE WILL GIVE THEM 1/5TH OF THE QUESTIONS EACH TIME
  
  do_quest := question_cnt / 5;
  --do_quest := question_cnt;
  do_quest := ceil(do_quest);

  
  --------------------------------------------------------------------
  FOR qst1 IN questions_cur(qr.quiz_id,gq_r.user_id) LOOP
    IF call_game_play.is_quiz_question_passed(gq_r.user_id,qst1.quiz_quest_id) = FALSE THEN 
      cnter := cnter + 1;
      IF cnter <= do_quest THEN
        SELECT call_game_user_quiz_quest_s.NEXTVAL INTO nxt_quest_id
          FROM dual;
        INSERT INTO call_game_user_quiz_questions(usr_q_qst_id,usr_quiz_id,quiz_id,quest_id,user_id,quiz_quest_id,date_assigned)
        VALUES (nxt_quest_id,user_game_qz_id,qr.quiz_id,gq_r.quest_id,gq_r.user_id,qst1.quiz_quest_id,sysdate);
      END IF;
    END IF;
  END LOOP;
  
  wl('<script>location="/prv/call_game_play.execute_quiz?usr_qz_p='||user_game_qz_id||'";</script>');
  --           var frm = top.document.getElementById("optsFrame");
          -- frm.src="/prv/call_game.complete_qst?qst_p='||usr_qst_p||'";
END;
------------------------------------------------------
PROCEDURE execute_quiz(usr_qz_p NUMBER)
IS
CURSOR usr_qz_cur
IS
  SELECT *
    FROM call_game_user_quiz
   WHERE usr_quiz_id = usr_qz_p;
uqz_r usr_qz_cur%ROWTYPE;

CURSOR gq_cur(id_p NUMBER)
IS
  SELECT *
    FROM game_user_quests
   WHERE usr_qst_id = id_p;
gq_r gq_cur%ROWTYPE;

CURSOR qst_cur(qst_p NUMBER)
IS
  SELECT *
    FROM call_game_quests
   WHERE quest_id = qst_p;
qr qst_cur%ROWTYPE;

CURSOR quiz_cur(id_p NUMBER)
IS
  SELECT *
    FROM call_game_quiz
   WHERE quiz_id = id_p;
qz quiz_cur%ROWTYPE;

CURSOR question_cur 
IS
  SELECT *
    FROM call_game_user_quiz_questions
   WHERE usr_quiz_id = usr_qz_p;
   
CURSOR qq_cur(id_p NUMBER)
IS
  SELECT *
    FROM call_game_quiz_questions
   WHERE quiz_quest_id = id_p;
qq_r qq_cur%ROWTYPE;

cnter NUMBER:=0;

quest_cnt NUMBER:=0;
BEGIN
  OPEN usr_qz_cur;
  FETCH usr_qz_cur INTO uqz_r;
  CLOSE usr_qz_cur;
  
  OPEN gq_cur(uqz_r.usr_quest_id);
  FETCH gq_cur INTO gq_r;
  CLOSE gq_cur;
  
  OPEN qst_cur(gq_r.quest_id);
  FETCH qst_cur INTO qr;
  CLOSE qst_cur;

  OPEN quiz_cur(uqz_r.quiz_id);
  FETCH quiz_cur INTO qz;
  CLOSE quiz_cur;
  
  --set_hdr('Take Quiz');
wl('
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset=''UTF-8''>
');

--jquery_libraries;
wl('
  <title>Take Quiz</title>  
        <style>
            body {
                font-family: Comic Sans MS,  sans-serif;
                font-size:12pt;
                background-color: #ffffff;
                background-image: url("//rightdata.driggstitle.com/callGame/Kwest_icons/Kwest_hub_background.png");
            }
        </style>
        
');
wl('<style>
.btn {
  background: #eb393e;
  background-image: -webkit-linear-gradient(top, #eb393e, #eb393e);
  background-image: -moz-linear-gradient(top, #eb393e, #eb393e);
  background-image: -ms-linear-gradient(top, #eb393e, #eb393e);
  background-image: -o-linear-gradient(top, #eb393e, #eb393e);
  background-image: linear-gradient(to bottom, #eb393e, #eb393e);
  -webkit-border-radius: 28;
  -moz-border-radius: 28;
  border-radius: 28px;
  font-family: Arial;
  color: #ffffff;
  font-size: 20px;
  padding: 5px 10px 5px 10px;
  text-decoration: none;
}
</style>');

wl('
</head>');
wl('<body>');
call_game.disable_back;  
wl('<h3>Take the quiz below</h3>');
  wl('<table width="100%">');
  FOR qust_r IN question_cur LOOP
    --wl(qust_r.quiz_quest_id||'<br>');
    --wl(qq_r.quiz_question||'<br>');
    cnter := cnter + 1;
    IF cnter = 1 THEN
      wl('<tr>');
    END IF;
    wl('<td width="50%" valign="top">');
    quest_cnt := quest_cnt + 1;
    call_game_play.layout_quiz_question(qust_r.quiz_quest_id,quest_cnt,qust_r.usr_q_qst_id);
    
    wl('</td>');
    IF cnter = 1 THEN
      wl('</tr>');
      cnter := 0;
    END IF;
   -- wl('<hr>');
  END LOOP;
  wl('</table>');
  wl('<br>');
  wl('<script>function grdQz(){
                var qcnt = 0;
                var corrCnt = 0;
                var cnt = '||quest_cnt||';
                for (var x=1;x<=cnt;x++){
                  qcnt = parseFloat(qcnt) + 1;
                  var idN = document.getElementById("quest_num_p"+x).value;
                  var corr="Y";
                  for (var z=1;z<=7;z++){
                    try{
                      var ans = document.getElementById("ans_p"+idN+"_"+z);
                      var dl = ans.getAttribute("data-line");
                      if (dl == "C" && ans.checked == false){
                        corr = "N";
                      }else if (dl == "I" && ans.checked){
                        corr = "N";
                      }
                    }catch(e){}
                  }
                  //alert("Question "+x+" correct is "+corr);
                  if (corr == "Y"){
                    corrCnt = parseFloat(corrCnt) + 1;
                  }
                }
                console.log("Questions: "+qcnt+" Correct:"+corrCnt);
                if (corrCnt == qcnt){
                  location="/prv/call_game_play.quiz_passed?usr_qz_p='||usr_qz_p||'&quest_tot="+qcnt+"&correct_tot="+corrCnt;
                }else{
                  location="/prv/call_game_play.quiz_failed?usr_qz_p='||usr_qz_p||'&quest_tot="+qcnt+"&correct_tot="+corrCnt;
                }
              }
      </script>');
  wl('<input type="button" name="gradeQuizBtn" id="gradeQuizBtn" value="Grade Quiz" onclick="grdQz();" class="btn">');
  wl('</body>');
  wl('</html>');
END;
------------------------------------------------------
PROCEDURE quiz_passed(usr_qz_p NUMBER,quest_tot NUMBER,correct_tot NUMBER)
IS
CURSOR quiz_cur 
IS
  SELECT *
    FROM call_game_user_quiz
   WHERE usr_quiz_id = usr_qz_p;
qz quiz_cur%ROWTYPE;
BEGIN
  UPDATE call_game_user_quiz_questions
     SET correct = 1
   WHERE usr_quiz_id = usr_qz_p;
   
   UPDATE call_game_user_quiz
      SET date_completed = sysdate,
          pass_fail = 1
    WHERE usr_quiz_id = usr_qz_p;
   COMMIT;
   
   OPEN quiz_cur;
   FETCH quiz_cur INTO qz;
   CLOSE quiz_cur;
 
   UPDATE game_user_quests
      SET comments = 'Quiz completed successfully',
          quest_failed = 0
    WHERE usr_qst_id = qz.usr_quest_id;
 
wl('<script>location="/prv/call_game.complete_qst?qst_p='||qz.usr_quest_id||'";</script>'); 
END;
--PROCEDURE complete_qst(qst_p NUMBER)
------------------------------------------------------
PROCEDURE quiz_failed(usr_qz_p NUMBER,quest_tot NUMBER,correct_tot NUMBER)
IS
CURSOR quiz_cur 
IS
  SELECT *
    FROM call_game_user_quiz
   WHERE usr_quiz_id = usr_qz_p;
qz quiz_cur%ROWTYPE;
BEGIN
  UPDATE call_game_user_quiz_questions
     SET correct = 0
   WHERE usr_quiz_id = usr_qz_p;
   
   UPDATE call_game_user_quiz
      SET date_completed = sysdate,
          pass_fail = 0
    WHERE usr_quiz_id = usr_qz_p;
   COMMIT;
   
   OPEN quiz_cur;
   FETCH quiz_cur INTO qz;
   CLOSE quiz_cur;
   
   UPDATE game_user_quests
      SET complete_dt = sysdate,
          link_to = -1,
          comments = 'Quiz failed no points/currency given',
          quest_failed = 1
    WHERE usr_qst_id = qz.usr_quest_id;
wl('
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset=''UTF-8''>
');

--jquery_libraries;
wl('
  <title>Take Quiz</title>  
        <style>
            body {
                font-family: Comic Sans MS,  sans-serif;
                font-size:12pt;
                background-color: #ffffff;
                background-image: url("//rightdata.driggstitle.com/callGame/Kwest_icons/Kwest_hub_background.png");
            }
        </style>
        
');
wl('<style>
.btn {
  background: #eb393e;
  background-image: -webkit-linear-gradient(top, #eb393e, #eb393e);
  background-image: -moz-linear-gradient(top, #eb393e, #eb393e);
  background-image: -ms-linear-gradient(top, #eb393e, #eb393e);
  background-image: -o-linear-gradient(top, #eb393e, #eb393e);
  background-image: linear-gradient(to bottom, #eb393e, #eb393e);
  -webkit-border-radius: 28;
  -moz-border-radius: 28;
  border-radius: 28px;
  font-family: Arial;
  color: #ffffff;
  font-size: 20px;
  padding: 5px 10px 5px 10px;
  text-decoration: none;
}
</style>');

wl('
</head>');
wl('<body>'); 
call_game.disable_back; 
wl('<h3><center>Quiz Failed You got '||correct_tot||'/'||quest_tot||' questions correct</center></h3>');
--alert("failed ask to try again and remove the quest.  Don''t mark questions as taken");

wl('<script>
       function doAgain(){
         location="/prv/call_game_play.assign_quest_to_usr?user_p='||qz.user_id||'&quest_p='||qz.quest_id||'";
       }  
    </script>');
wl('<input type="button" name="takBtn" id="takBtn" onclick="doAgain();" value="View Video and Try Quiz Again" class="btn">');

wl('<br><br>');
wl('
<img src="//rightdata.driggstitle.com/callGame/forward_to_closet.png" width="80px" height="40px" onclick="goGame();"/>');
wl('
<script>function goGame(){
                  top.location="/prv/call_game_play.main_game?user_p='||qz.user_id||'"
                }
        </script>
');
wl('</body>');
wl('</head>');  
END;
------------------------------------------------------
PROCEDURE layout_quiz_question(question_id_p NUMBER,cnter_p NUMBER,id_p NUMBER)
IS
CURSOR qq_cur
IS
  SELECT *
    FROM call_game_quiz_questions
   WHERE quiz_quest_id = question_id_p;
qq_r qq_cur%ROWTYPE;

dl VARCHAR2(100):='';
BEGIN
  OPEN qq_cur;
  FETCH qq_cur INTO qq_r;
  CLOSE qq_cur;
  --$(row).contents().slideDown();
  --wl(qq_r.quiz_question||'<br>');
  IF qq_r.quiz_type = 1 THEN
    wl('<h4>Choose 1 Correct Answer</h4>');
  ELSIF qq_r.quiz_type = 2 THEN
    wl('<h4>Choose True or False</h4>');
  ELSIF qq_r.quiz_type = 3 THEN
    wl('<h4>Choose All that Apply</h4>');
  ELSE
    wl('<h4>Put Items in Sequence</h4>');
  END IF;
  wl('<input type="hidden" name="quiz_quest_id_p'||id_p||'" id="quiz_quest_id_p'||id_p||'" value="'||question_id_p||'">');
  wl('<input type="hidden" name="quest_num_p'||cnter_p||'" id="quest_num_p'||cnter_p||'" value="'||id_p||'">');
  wl('<input type="hidden" name="quiz_type_id_p'||id_p||'" id="quiz_type_id_p'||id_p||'" value="'||qq_r.quiz_type||'">');
  wl('<p>Quest # '||cnter_p||' - '||qq_r.quiz_question||'</p>');
  IF qq_r.quiz_type IN (1,2) THEN
    IF nvl(length(qq_r.ans1),0) > 2 THEN
      IF nvl(qq_r.ans1_corr,0) = 1 THEN
        dl := 'data-line="C"';
      ELSE
        dl := 'data-line="I"';
      END IF;
      wl('<input type="radio" name="ans_p'||id_p||'" id="ans_p'||id_p||'_1" '||dl||'>'||qq_r.ans1||'<br><br>');
    END IF;
    IF nvl(length(qq_r.ans2),0) > 2 THEN
      IF nvl(qq_r.ans2_corr,0) = 1 THEN
        dl := 'data-line="C"';
      ELSE
        dl := 'data-line="I"';
      END IF;
      wl('<input type="radio" name="ans_p'||id_p||'" id="ans_p'||id_p||'_2" '||dl||'>'||qq_r.ans2||'<br><br>');
    END IF;
    IF nvl(length(qq_r.ans3),0) > 2 THEN
      IF nvl(qq_r.ans3_corr,0) = 1 THEN
        dl := 'data-line="C"';
      ELSE
        dl := 'data-line="I"';
      END IF;
      wl('<input type="radio" name="ans_p'||id_p||'" id="ans_p'||id_p||'_3" '||dl||'>'||qq_r.ans3||'<br><br>');
    END IF;
    IF nvl(length(qq_r.ans4),0) > 2 THEN
      IF nvl(qq_r.ans4_corr,0) = 1 THEN
        dl := 'data-line="C"';
      ELSE
        dl := 'data-line="I"';
      END IF;
      wl('<input type="radio" name="ans_p'||id_p||'" id="ans_p'||id_p||'_4" '||dl||'>'||qq_r.ans4||'<br><br>');
    END IF;
    IF nvl(length(qq_r.ans5),0) > 2 THEN
      IF nvl(qq_r.ans5_corr,0) = 1 THEN
        dl := 'data-line="C"';
      ELSE
        dl := 'data-line="I"';
      END IF;
      wl('<input type="radio" name="ans_p'||id_p||'" id="ans_p'||id_p||'_5" '||dl||'>'||qq_r.ans5||'<br><br>');
    END IF;
    IF nvl(length(qq_r.ans6),0) > 2 THEN
      IF nvl(qq_r.ans6_corr,0) = 1 THEN
        dl := 'data-line="C"';
      ELSE
        dl := 'data-line="I"';
      END IF;
      wl('<input type="radio" name="ans_p'||id_p||'" id="ans_p'||id_p||'_6" '||dl||'>'||qq_r.ans6||'<br><br>');
    END IF;
    IF nvl(length(qq_r.ans7),0) > 2 THEN
      IF nvl(qq_r.ans7_corr,0) = 1 THEN
        dl := 'data-line="C"';
      ELSE
        dl := 'data-line="I"';
      END IF;
      wl('<input type="radio" name="ans_p'||id_p||'" id="ans_p'||id_p||'_7" '||dl||'>'||qq_r.ans7||'<br><br>');
    END IF;
  ELSIF qq_r.quiz_type = 3 THEN
    IF nvl(length(qq_r.ans1),0) > 2 THEN
      IF nvl(qq_r.ans1_corr,0) = 1 THEN
        dl := 'data-line="C"';
      ELSE
        dl := 'data-line="I"';
      END IF;
      wl('<input type="checkbox" name="ans1_p'||id_p||'" id="ans_p'||id_p||'_1" '||dl||'>'||qq_r.ans1||'<br><br>');
    END IF;
    IF nvl(length(qq_r.ans2),0) > 2 THEN
      IF nvl(qq_r.ans2_corr,0) = 1 THEN
        dl := 'data-line="C"';
      ELSE
        dl := 'data-line="I"';
      END IF;
      wl('<input type="checkbox" name="ans2_p'||id_p||'" id="ans_p'||id_p||'_2" '||dl||'>'||qq_r.ans2||'<br><br>');
    END IF;
    IF nvl(length(qq_r.ans3),0) > 2 THEN
      IF nvl(qq_r.ans3_corr,0) = 1 THEN
        dl := 'data-line="C"';
      ELSE
        dl := 'data-line="I"';
      END IF;
      wl('<input type="checkbox" name="ans3_p'||id_p||'" id="ans_p'||id_p||'_3" '||dl||'>'||qq_r.ans3||'<br><br>');
    END IF;
    IF nvl(length(qq_r.ans4),0) > 2 THEN
      IF nvl(qq_r.ans4_corr,0) = 1 THEN
        dl := 'data-line="C"';
      ELSE
        dl := 'data-line="I"';
      END IF;
      wl('<input type="checkbox" name="ans4_p'||id_p||'" id="ans_p'||id_p||'_4" '||dl||'>'||qq_r.ans4||'<br><br>');
    END IF;
    IF nvl(length(qq_r.ans5),0) > 2 THEN
      IF nvl(qq_r.ans5_corr,0) = 1 THEN
        dl := 'data-line="C"';
      ELSE
        dl := 'data-line="I"';
      END IF;
      wl('<input type="checkbox" name="ans5_p'||id_p||'" id="ans_p'||id_p||'_5" '||dl||'>'||qq_r.ans5||'<br><br>');
    END IF;
    IF nvl(length(qq_r.ans6),0) > 2 THEN
      IF nvl(qq_r.ans6_corr,0) = 1 THEN
        dl := 'data-line="C"';
      ELSE
        dl := 'data-line="I"';
      END IF;
      wl('<input type="checkbox" name="ans6_p'||id_p||'" id="ans_p'||id_p||'_6" '||dl||'>'||qq_r.ans6||'<br><br>');
    END IF;
    IF nvl(length(qq_r.ans7),0) > 2 THEN
      IF nvl(qq_r.ans7_corr,0) = 1 THEN
        dl := 'data-line="C"';
      ELSE
        dl := 'data-line="I"';
      END IF;
      wl('<input type="checkbox" name="ans7_p'||id_p||'" id="ans_p'||id_p||'_7"'||dl||'>'||qq_r.ans7||'<br><br>');
    END IF;    
  ELSE
    null;
  END IF;
  --wl(qq_r.quiz_type||'<br>');
  
END;
------------------------------------------------------
END call_game_play;
/*
CREATE TABLE call_game_quiz_questions(
quiz_quest_id NUMBER,
quiz_id NUMBER,
quiz_question VARCHAR2(4000),
quiz_type VARCHAR2(100),

ans1 VARCHAR2(2000),
ans1_corr VARCHAR2(1),
ans1_seq NUMBER,

ans2 VARCHAR2(2000),
ans2_corr VARCHAR2(1),
ans2_seq NUMBER,

ans3 VARCHAR2(2000),
ans3_corr VARCHAR2(1),
ans3_seq NUMBER,

ans4 VARCHAR2(2000),
ans4_corr VARCHAR2(1),
ans4_seq NUMBER,

ans5 VARCHAR2(2000),
ans5_corr VARCHAR2(1),
ans5_seq NUMBER,

ans6 VARCHAR2(2000),
ans6_corr VARCHAR2(1),
ans6_seq NUMBER,

ans7 VARCHAR2(2000),
ans7_corr VARCHAR2(1),
ans7_seq NUMBER

);
*/