﻿// JScript File

var clickrevquiz = new Object();



clickrevquiz.init = function()
{

    //qid,totalquiz,totalhint,totalanswer
   var qid = document.getElementById("quizid"); 
   var totalquiz = document.getElementById("totalNum");
   var totalhint = document.getElementById("totalHintNum");
   var totalanswer = document.getElementById("totalanswerNum");
   
   var locainfo = document.getElementById("btnSubmit"); 
   if(locainfo!=null)
	this.btnSubmit = locainfo.value;
   else
    this.btnSubmit = "Submit";
   locainfo = document.getElementById("btnNext"); 
   if(locainfo!=null)
	this.btnNext = locainfo.value;
   else
    this.btnNext = "Next";
   locainfo = document.getElementById("QstMoveToNext"); 
   if(locainfo!=null)
	this.QstMoveToNext = locainfo.value;
   else
    this.QstMoveToNext = "Do you really want to move onto the next question?";
   locainfo = document.getElementById("TryAgain"); 
   if(locainfo!=null)
	this.TryAgain = locainfo.value;
   else
    this.TryAgain = "Sorry, that's not the correct answer. Have another go… You have {0} of {1} attempts left.";
   locainfo = document.getElementById("WrongMoveToNext"); 
   if(locainfo!=null)
	this.WrongMoveToNext = locainfo.value;
   else
    this.WrongMoveToNext = "Really sorry but that's not the correct answer either. And you have now run out of attempts for this question. You will be forwarded automatically to the next page in 7 seconds or click Next to move on straight away.";
   locainfo = document.getElementById("RightMoveToNext"); 
   if(locainfo!=null)
	this.RightMoveToNext = locainfo.value;
   else
    this.RightMoveToNext = "Well done, that's the right answer. You will be forwarded automatically to the next page in 7 seconds or click Next to move on straight away.";
   locainfo = document.getElementById("FinalScore"); 
   if(locainfo!=null)
	this.FinalScore = locainfo.value;
   else
    this.FinalScore = "You scored {0} out of {1} correct answers";	
   locainfo = document.getElementById("txtAnswerPanel"); 
   if(locainfo!=null)
	this.txtAnswerPanel = locainfo.value;
   else
    this.txtAnswerPanel = "Answer Panel";	
	
   locainfo = document.getElementById("txtResult"); 
   if(locainfo!=null)
	this.txtResult = locainfo.value;
   else
    this.txtResult = "Result";	
	
   locainfo = document.getElementById("txtCorrectAnswer"); 
   if(locainfo!=null)
	this.txtCorrectAnswer = locainfo.value;
   else
    this.txtCorrectAnswer = "The correct answer is";
   locainfo = document.getElementById("txtAutoForward"); 
   if(locainfo!=null)
	this.txtAutoForward = locainfo.value;
   else
    this.txtAutoForward = "You will be forwarded automatically to the next page in 7 seconds or click Next to move on straight away";
    this.qid = qid.value;
    var urlparas = new   Array();
    urlparas = window.location.href.split('?');
    this.pageurl = urlparas[0].split('#')[0];
    this.queryparas = new Array();
    if(urlparas[1] != null)
        this.parseQuerystring(urlparas[1]);


    this.cookieqnum = this.GetCookie(this.qid+"qnum");
    if(this.cookieqnum==null)
    { 
        this.SetCookie(this.qid+"qnum",1); 
        this.cookieqnum = "1"; 
    }
   
   this.queryqnum =  this.queryparas["cp-documentid"];
   if(this.queryqnum==null)
    this.queryqnum = "1";
   if(this.queryqnum != this.cookieqnum) 
    {
		var clickrevquizlay = document.getElementById(this.qid);
          if(clickrevquizlay!=null)
            clickrevquizlay.style.display = "none"; 
		this.SetCookie(this.qid+"qnum",1); 
		this.SetCookie(this.qid+"rn",0); 
		this.SetCookie(this.qid+"wn",0);
		this.cookieqnum = 1;
	   
        window.location.href = this.pageurl + "?cp-documentid=" + this.cookieqnum;
        
    }
	
	this.bAutoNext = false;
	this.DelayTime = 7;
	this.CurDelayTime = 0;
	
	this.showanswerclick = false;

   this.hintnum = 0;
   if(totalhint!=null)
	this.totalhintnum = parseInt(totalhint.value);
	else 
	this.totalhintnum = 0;
   if(totalquiz !=null)
	this.totalquiznum = parseInt(totalquiz.value);
   else
	this.totalquiznum = 0;
	if(totalanswer !=null)
		this.totalanswernum = parseInt(totalanswer.value);
	else
		this.totalanswernum = 0;
   this.submited = 0;
   this.showpanel = 0;
   this.rightnum = this.GetCookie(this.qid+"rn");
   if(this.rightnum==null)
    this.rightnum = 0;
   else
   {
	if(isNaN(this.rightnum))
		this.rightnum = 0;
	else
		this.rightnum = parseInt(this.rightnum);
	}
	
	
	
   this.wrongnum = this.GetCookie(this.qid+"wn");
   if(this.wrongnum==null)
    this.wrongnum = 0;
   else
   {
	if(isNaN(this.wrongnum))
		this.wrongnum = 0;
	else
		this.wrongnum = parseInt(this.wrongnum);
	}
	
	

   this.curbcorrect = 0; 
   this.retrynum = 0;
   this.Maxretrynum = 3;
   if(this.queryqnum > this.totalquiznum)
	this.showresult();
	
	if(this.queryqnum == this.totalquiznum)
	{
	   locainfo = document.getElementById("qstSkip"); 
	   if(locainfo!=null)
		this.QstMoveToNext = locainfo.value;
	   else
	    this.QstMoveToNext = "Do you really want to skip this question and view the result?";	
	}
	
	var bcorrectanswer = document.getElementById("correctanswer");
	if(bcorrectanswer!=null)
		this.correctanswer = bcorrectanswer.innerHTML;
	else
		this.correctanswer = "";
	
}

clickrevquiz.parseQuerystring = function(querystring)
{
   var paras = new Object();
   var qps = new Array(); 
   paras = querystring.split('&');
   for(var i=0;i<paras.length;i++)
   {
        qps = paras[i].split('=');
        this.queryparas[qps[0]] = qps[1];
   } 
}


clickrevquiz.GetCookieVal = function(offset)
{

    var endstr = document.cookie.indexOf (";", offset);
    if (endstr == -1)
        endstr = document.cookie.length;
    return unescape(document.cookie.substring(offset, endstr));
}

clickrevquiz.SetCookie = function(name,value)
{
    var expdate = new Date();
    var argv = this.SetCookie.arguments;
    var argc = this.SetCookie.arguments.length;
    var expires = (argc > 2) ? argv[2] : null;
    var path = (argc > 3) ? argv[3] : null;
    var domain = (argc > 4) ? argv[4] : null;
    var secure = (argc > 5) ? argv[5] : false;
    if(expires!=null) expdate.setTime(expdate.getTime() + ( expires * 1000 ));
    document.cookie = name + "=" + escape (value) +((expires == null) ? "" : ("; expires="+ expdate.toGMTString()))
    +((path == null) ? "" : ("; path=" + path)) +((domain == null) ? "" : ("; domain=" + domain))
    +((secure == true) ? "; secure" : "");
}

clickrevquiz.DelCookie = function(name)
{
    var exp = new Date();
    exp.setTime (exp.getTime() - 1);
    var cval = GetCookie (name);
    document.cookie = name + "=" + cval + "; expires="+ exp.toGMTString();
}

clickrevquiz.GetCookie = function(name)
{
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    while (i < clen)
    {
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg)
    return this.GetCookieVal (j);
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break;
    }
    return null;
}

clickrevquiz.usersubmit = function()
{
    if(this.submited == 0)
   { 
       var useranswer = document.getElementById("AnswerAccepter");
       if(useranswer==null)
        return;
       var trimanswer = useranswer.value.replace(/(^\s*)|(\s*$)/g, ""); 
       for(var i=1;i<=this.totalanswernum;i++)
       {
            var hiddenanswer = document.getElementById("questionanswer" + i);
			
			if(hiddenanswer!=null)
            if(hiddenanswer.value.toUpperCase() == trimanswer.toUpperCase())
                {
                     this.curbcorrect = 1;
                     break;
                }
       }
       this.showpanel = 1;
       if(this.curbcorrect==0)
       {
            this.retrynum++;
            if(this.retrynum >= this.Maxretrynum)
                 {
                    this.submited = 1;
                    this.wrongnum++;
                 }
       } 
       else
       {
           this.submited = 1;
           this.rightnum++;
       } 
	   var useranswertxt = document.getElementById("answerpaneltxt");
	   if(useranswertxt!=null)
		useranswertxt.innerHTML = this.txtResult;
       this.showcontrol();
   }
   else
   {
        nextquestion();
   }
}

clickrevquiz.showcontrol = function()
{
       var panelhead = document.getElementById("answertitle");
       if(this.curbcorrect==0)
       {
        if(panelhead!=null)
            if(this.retrynum >= this.Maxretrynum) 
			{
                panelhead.innerHTML =  this.WrongMoveToNext;
				this.startAntoNext();

			}
            else 
                panelhead.innerHTML =  this.stringreplace(this.TryAgain,this.Maxretrynum -this.retrynum,this.Maxretrynum);// you have " + (this.Maxretrynum -this.retrynum) + " times to retry!" ;
       } 
       else
       {
        if(panelhead!=null)
           panelhead.innerHTML =  this.RightMoveToNext + "<br> </br>"; 
		 
		var viewanswerbutton = document.getElementById("ViewAnswer");
		if(viewanswerbutton!=null)
			viewanswerbutton.disabled = true;
		this.startAntoNext();
       } 
       var submitbutton = document.getElementById("Submit"); 
       
       if(submitbutton!=null)
       {   
            if(this.submited == 0)
                submitbutton.value = this.btnSubmit;
            else     
                submitbutton.value = this.btnNext;
        } 

	
	   var answerpanel = document.getElementById("answerpanel");
	   if(answerpanel!=null)
	   {
			if(this.showpanel == 0)
				answerpanel.style.display = 'none';
			else
			{
				
				answerpanel.style.display = 'block';  
				var bcorrectanswer = document.getElementById("correctanswer");
				
				if(bcorrectanswer!=null)
					if(this.curbcorrect == 1 || this.showanswerclick)
						bcorrectanswer.style.display = 'block';
					else
						bcorrectanswer.style.display = 'none';
						
				if(this.retrynum == this.Maxretrynum || this.curbcorrect==1)
				{
						var hintbutton = document.getElementById("Hint");
						if(hintbutton!=null)
							hintbutton.disabled = true;
				}
			}	
			
	   }
	   
	
	   for(var i =1;i<=this.totalhintnum;i++)
	   {
			var hintlayer = document.getElementById("questionhint" + i);
			if(hintlayer!=null)
				if(i==this.hintnum && (this.showpanel==0 || (this.curbcorrect == 1 && this.showpanel==0)))
					hintlayer.style.display = 'block';
				else
					hintlayer.style.display = "none";      
	   }     
	   
}

clickrevquiz.showHint = function()
{
    if(this.hintnum == 0)
        this.hintnum = 1;
	if(this.showpanel != 0)
		this.showpanel = 0;	
    this.showcontrol();
}

clickrevquiz.nexthint = function()
{
    this.hintnum++;
   if(this.hintnum > this.totalhintnum) 
    this.hintnum = this.totalhintnum;
   this.showcontrol();
}

clickrevquiz.prehint = function()
{
        this.hintnum--;
   if(this.hintnum < 1) 
    this.hintnum = 1;
   this.showcontrol();
}

clickrevquiz.showanswerpanel =  function()
{
   this.showanswerclick = true;
   this.showpanel = 1; 
   this.retrynum = this.Maxretrynum;
   this.submited = 1;
   this.showcontrol(); 
   var useranswertxt = document.getElementById("answerpaneltxt");
   if(useranswertxt!=null)
		useranswertxt.innerHTML = this.txtAnswerPanel;
   var panelhead = document.getElementById("answertitle");
   if(panelhead!=null)
        panelhead.innerHTML = this.txtCorrectAnswer ;
   	var bcorrectanswer = document.getElementById("correctanswer");
	if(bcorrectanswer!=null)
		bcorrectanswer.innerHTML = this.correctanswer + "<p/>" + this.txtAutoForward;
   this.startAntoNext();
}

clickrevquiz.AutoNext = function()
{	
	if(!this.bAutoNext)
		return;
	if(this.DelayTime <= this.CurDelayTime)
		this.nextquestion();
	else
	{
		this.CurDelayTime++;
		setTimeout("clickrevquiz.AutoNext()",1000);
	}
}

clickrevquiz.startAntoNext= function()
{
	this.CurDelayTime = 0;
	if(!this.bAutoNext)
	{
		this.bAutoNext = true;
		this.AutoNext();
	}
}

clickrevquiz.cancelAntoNext= function()
{
	this.bAutoNext = false;
}

clickrevquiz.nextquestion = function()
{
    if(this.curbcorrect==0 && this.retrynum < this.Maxretrynum)
        if(!confirm(this.QstMoveToNext))
            return;
        else
            this.wrongnum++;  
   this.SetCookie(this.qid+"qnum",(parseInt(this.cookieqnum)+1)); 
   if(isNaN(this.rightnum))
	this.SetCookie(this.qid+"rn",0);
   else
	this.SetCookie(this.qid+"rn",this.rightnum); 
	
   if(isNaN(this.wrongnum))
	this.SetCookie(this.qid+"wn",0);
   else
	this.SetCookie(this.qid+"wn",this.wrongnum);  
   window.location.href = this.pageurl + "?cp-documentid=" + (parseInt(this.cookieqnum)+1);
}

clickrevquiz.showresult = function()
{
	var res;
	if(this.totalquiznum==0)
		res=-1;
	else
		res = this.rightnum / this.totalquiznum;
	if(res<=0.25 && res>=0)
		res=1;
	else if(res>0.25 && res<=0.5)
		res=2;
	else if(res>0.5 && res <= 0.75)
		res=3;
	else if(res>0.75)
		res=4;
		
	for(var i=1;i<=4;i++)
	{
		var reslay = document.getElementById("resultdetai" + i);
		
		if(reslay!=null)
			if(i==res)
				reslay.style.display = 'block';
            else
                reslay.style.display = "none";    
	}
	var sorcelay = document.getElementById("resulttitle");
	if(isNaN(this.rightnum))
		this.rightnum = "0";
	if(sorcelay!=null)
		sorcelay.innerHTML = this.stringreplace(this.FinalScore,this.rightnum,this.totalquiznum);
}

clickrevquiz.stringreplace = function(inputstr,rpval1,rpval2)
{
	inputstr = inputstr.replace('{0}',rpval1);
	inputstr = inputstr.replace('{1}',rpval2);
	return inputstr;
}

function nextquestion()
{
    clickrevquiz.nextquestion();
}

function showanswerpanel()
{
    clickrevquiz.showanswerpanel();
}

function usersubmit()
{
    clickrevquiz.usersubmit();
}

function showhint()
{
    clickrevquiz.showHint();
}

function nexthint()
{
    clickrevquiz.nexthint();
}

function prehint()
{
    clickrevquiz.prehint();
}

function keydown(e)
{
    e   =   (e)   ?   e   :   ((window.event)   ?   event   :   null); 
	//var e=(typeof event!='undefined')?window.event:e;// IE : Moz
	//alert(e.keyCode);
    if(e.keyCode==13)
		usersubmit();
}