/*
HTML format is controlled both by style sheet "faithcheck.css" and within HTML writing functions
to enable Netscape.

All text taken is from the arrays at the head of this document and may be modified, remembering 
that some characters will cause errors and HTML equivs must be used
e.g. simple double quotes=&#34;  apostrophe=&#39; etc


*/

// These variables can be modified

var force_chronological = 1;	// 1 or 0. If set to 1, questions must be answered chronologically
//var feedback_offset = 1;        // triggers feedback--the difference between where I am and my goal





//--------------------------functions relating to user input---------------------------//

var buttons_per_question = 14;
function radio(Qno){
  Qno = Qno.charAt(1);
  var Qname = "q" + Qno;
  var missingQ = missing_question();  
  if (force_chronological && q_complete(Qno)) { Qno++; document.location = "#q"+ Qno};
  if(force_chronological && missingQ!== null && missingQ< Qno){
    missingQ++;
    message = "You haven't finished question " + missingQ + " yet";
    alert(message);
	un_check(Qname);
	document.location = "#q"+ (missingQ-1);
  }else{
	if(missingQ==null) tally()   	//all questions done
  }
}

function un_check(Qno){
  for(var x=0; x<buttons_per_question; x++){
    document.forms[Qno].elements[x].checked = false;
  }
}


function q_complete(Qno){
	
  if(which_now_answer(Qno)!=null && which_goal_answer(Qno)!=null) {
  	return true
  }
  alert ("Question not completed");
  return false;
 
}

function missing_question(){
  for(var x=0; x< question.length; x++){
    if(!which_now_answer(x)==null || which_goal_answer(x)==null){
      return x;
    }
  }
  return null
}


function which_now_answer(Qno){
  Qno = "q" + Qno;
  for(var x=0; x<buttons_per_question; x += 2){
    if(document.forms['quiz'].elements[x].checked == true){
      return x/2
    }
  }
  return null
}

function which_goal_answer(Qno){
  Qno = "q" + Qno;
  for(var x=1; x<buttons_per_question; x += 2){
    if(document.forms['quiz'].elements[x].checked == true){
      return (x-1)/2;
    }
  }
  return null
}


function tally(){
  var now,goal,feedback_ndx,list ="";
  var expires = new Date();
  expires.setTime (expires.getTime() + 24 * 60 * 60 * 1 * 1000);
  
 // killCookie("feedbackList");
  for(var Q=0; Q<=question.length-1; Q++){
    now = which_now_answer(Q);
	goal = which_goal_answer(Q);
	feedback_ndx = goal-now;
	feedback_ndx = (feedback_ndx < 0)? 0:feedback_ndx;
	list += feedback_ndx;
  }
  list = escape(list);
  //setCookie("feedbackList",list,expires,"/");
  window.location = "faith_feedback.php?list="+list

}

//-----------------------------------end of user input functions-------------------//


//-----------------------------------functions to write HTML code for quiz---------//

function rand(num) {
	return Math.floor(Math.random() * num );
}

// called from faith1.htm
function write_quiz(){
  var x = 0;
  var text = '<table width="100%" class="quote"><tr><td><p><FONT color=#333399 ';
  set_up_array("quiz");
  text += 'face="Verdana, Arial, Helvetica, sans-serif" size=2>' + thought[rand(thought.length)];
  text += '</font><br></p></td></tr></table><br><br> <font color="#000000" face="Arial, Helvetica, sans-serif" size="2">Now, when you&#39re ready to begin, ';
  text += '<a href="#q0"><b>click here ...</font></b></a></TD></TR></TBODY></TABLE>';
  text += '<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>';
  
  for(x=0;x<question.length;x++){
    text += write_question(x);  
  }
 return text;


}


function write_question(Qno){
  var x = 0;
  var text = '<a name=q' + Qno + '></a><table width=100% class="question_block"><td><FORM action="" method=post name=q' + Qno +'>';
  text += '<FONT color="red" face="Verdana, Arial, Helvetica, sans-serif" size=2 class="title">' + title[Qno] + '</font><br><br>';             
  text += '<table width=100% class="quote"><tr><td><FONT color=black face="Verdana, Arial, Helvetica, sans-serif" size=2> ';
  text += scripture[Qno][rand(scripture[0].length)] + '<br><br>' + quote[Qno][rand(quote[0].length)] +'</font></td></tr></table>';
  text += '<table>';
  text += '<tr><td width="535" height="7" colspan="3"><FONT color=#333399 face="Verdana, Arial, Helvetica, sans-serif" size=2><blockquote><br><br> ' + question[Qno] + '</font><br><br></blockquote></td></tr>'
  text += '<tr><td width="433" height="2" valign="top"></td>';
  text += '<td width="51" height="2" valign="top" class="radio"><FONT color=black face="Verdana, Arial, Helvetica, sans-serif" size=2>Now</font></td>';
  text += '<td width="51" height="2" valign="top" class="radio"><FONT color=black face="Verdana, Arial, Helvetica, sans-serif" size=2>Goal</font></td></tr>';
  text += '<tr><td width="433" height="1" valign="top"><img width="433" height="1" src="transparent.gif"></td>';
  text += '<td width="51" height="1" valign="top"><img width="51" height="1" src="transparent.gif"></td>';
  text += '<td width="51" height="1" valign="top"><img width="51" height="1" src="transparent.gif"></td>';
  for(x=0;x<option[0].length;x++){
    text += write_option(Qno,x);
  }
  text += '</tr></table></form></td></table><br><br><br>';
  return text;
}

function write_option(Qno,Option){
  var text = "";
  text += '<tr><td width="433" height="10" valign="top" class="option"><FONT color=#333399 face="Verdana, Arial, Helvetica, sans-serif" size=2>' + option[Qno][Option] + '</font><br><br></td>';
  text += '<td width="51" height="10" valign="top" class="radio">';
  text += '<input type="radio" name=n'+ Qno + ' value="radiobutton" onClick="radio(name)"></div></td>';
  text += '<td width="51" height="10" valign="top" class="radio">';
  text += '<input type="radio" name=g' + Qno + ' value="radiobutton" onClick="radio(name)"></div></td></tr>';
  return text;

}

//called from faith_feedback.htm

function write_feedback(List){
 
  list = List + ""; // force number into string
  set_up_array("feedback");
  var Alpha = "ABCDEFG"
  var score = 0;
  //var list = unescape(getCookie("feedbackList"));
  if(!list) alert("FaithCheck was unable to extract your answers, you may have 'cookies' turned off");

  var text = '';
  text += '<table width =100% border=0><tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>';
  text += '<tr><td colspan=3><font color="black" face = "Verdana, Arial, Helvetica, sans-serif" size=1>(KEY:';
  text += '<font color="red"> A<font color="black"> no problems<font color="red"> B<font color="black"> needs slight improvement';
  text += '<font color="red"> C<font color="black"> could do better<font color="red"> D<font color="black"> some cause for concern';
  text += '<font color="red"> E<font color="black"> needs a major reassessment)</td></tr>';
  text += '<tr><td>Subject</td><td>Grade</td><td>&nbsp;</td></tr>';
  
  for(var x=0;x<list.length;x++){

	score = (parseInt(list.charAt(x)) > 4) ? "E" : Alpha.charAt((list.charAt(x)));
	text += '<tr class=main><td><b>' + title[x] + '</b></td><td><font color=red>' + score + '</font></td>';
	text += '<td><input type="checkbox" name=' + box_name[x] + ' value="yes">' + feedback_label[x] + '</td></tr>\n'
	
  }

  text += '<tr><td colspan=3><font color="black" face = "Verdana, Arial, Helvetica, sans-serif" size=1>These are your scores according to the information given. If you want help to improve any of these areas, please fill in your details, and you will be able to download our specially written PDF documents on your chosen subjects immediately</td></tr>';
  
 // text += '<tr><td></td><td><input type="hidden" name="recipient" value="tim@theway.co.uk"></td</tr>\n';
 // text += '<tr><td></td><td><input type="hidden" name="subject" value="Faithcheck"></td</tr>\n';
 // text += '<tr><td></td><td><input type="hidden" name="required" value="email"></td</tr>\n';
 // text += '<tr><td></td><td><input type="hidden" name="redirect" value="http://www.theway.co.uk/faithcheck/faiththankyou.php"></td</tr>\n';

  text += '<tr><td colspan =2>Name:</td><td><input type="text" name="name"></td></tr>\n';
  text += '<tr><td colspan =2>Email Address:</td><td><input type="text" name="email"></td></tr>\n';
  text += '<input type="hidden" name=list value='+list+'\n';
  
  text += '<tr><td colspan =2>Verify email address:</td><td><input type="text" name="email_confirmation">&nbsp;&nbsp;&nbsp;<input name=Submit type=submit value="Submit"></td></tr>\n';
  
  
  return text;
}


/////////////    Cookiie functions  ///////////////////
//  N.B. as of 1/2/2001 cookies only used during browser session to store input to enable instant feedback 

var expires = new Date();
expires.setTime (expires.getTime() + 24 * 60 * 60 * 365 * 1000);

function setCookie (name, value) {
  var cookieArray = setCookie.arguments;
  var len = setCookie.arguments.length;
  var expires = (len > 2) ? cookieArray[2] : null;
  var path = (len > 3) ? cookieArray[3] : null;
  var domain = (len > 4) ? cookieArray[4] : null;
  var secure = (len > 5) ? cookieArray[5] : false;
  document.cookie = name + "=" + escape (value) +
    ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
    ((path == null) ? "" : ("; path=" + path)) +
    ((domain == null) ? "" : ("; domain=" + domain)) +
    ((secure == true) ? "; secure" : "");
}
   

function getCookie(name) {
  var cookieFound = false;
  var start = 0;
  var end = 0;
  var cookieString = document.cookie;
  var i = 0;
   
  // SCAN THE COOKIE FOR name
  while (i <= cookieString.length) {
    start = i;
    end = start + name.length;
      if (cookieString.substring(start,end) == name) {
        cookieFound = true;
        break;
      }
      i++;
  }
   
  // IS name FOUND?
  if (cookieFound) {
    start = end + 1;
    end = document.cookie.indexOf(";",start);
      if (end < start){
        end = document.cookie.length;
        return document.cookie.substring(start,end);
	   }
   }
   return "";
}


function killCookie (name,path,domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") + "expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

/////////////  end cookie functions  /////////////////

