function q_message(t,s)
{
    w = window.open("","QALERT","toolbar=0,menubar=0,directories=0,scrollbars=1,resizable=1,copyhistory=no,width=400,height=300")
    d = w.document
    d.close()
    d.open()
    d.write('<ht'+'ml><head><title>'+t+'&nbsp;&nbsp;&nbsp;<\/title><link href=\"\/vts\/style\/vts_popup.css\" rel=\"stylesheet\" type=\"text\/css\"><\/head><body onload=\"window.focus()\" id=\"popup\">')
    if (correctness=="wrong") { d.write(a_incorrect()) }
    if (correctness=="correct") { d.write(a_correct()) }
    if (correctness=="nearly") { d.write(a_nearly()) }
    if (correctness=="hint") { d.write(a_hint()) }
    if (correctness=="further") { d.write(a_further()) }
    d.write('<p>'+s+'<\/p>')
    d.write(a_answer_bottom())
    d.write('<\/bo'+'dy><\/ht'+'ml>')
    d.close()
    w.focus()
}

function a_incorrect()
{
    return '<div id="incorrect"><h1>Incorrect<\/h1>'
}

function a_correct()
{
    return '<div id="correct"><h1>Correct<\/h1>'

}

function a_nearly()
{
    return '<div id="incorrect"><h1>Nearly...<\/h1>'
}

function a_answer_bottom()
{
    return '<input type="button" value="OK" name="button" class="buttonstyle" onclick="self.close();" \/><\/div>'
}

function a_hint()
{
    return '<div id="basic"><h1>Hint<\/h1>'
}

function a_further()
{
    return '<div id="basic"><h1>Further Information<\/h1>'

}

function q_clickable(q)
{
    obj = "document.question"+q
    if ( eval(obj+".locked.value") == "true" ) {
        q_locked()
        ret = false
    } else {
        ilim = eval(obj+".choice.length")
        for(i=0; i<ilim; i++) {
            eval("document.images.q"+q+".src='\/vts09\/images\/unmarked.gif'")
        }
        ret = true
    }
    return ret
}

function q_editable(targ, q)
{
    obj = "document.question"+q
    if ( eval(obj+".locked.value") == "true" ) {
        targ.blur()
        q_locked()
        ret = false
    } else {
        targ.select()
        eval("document.images['q"+q+"mk1'].src='\/vts09\/images\/unmarked.gif'")
        ret = true
    }
    return ret
}

function q_feedback(q,s)
{
    if (s == "") {
        obj = "document.question"+q
        s = eval(obj + ".response.value")
    }
    q_message(q+". Further Information",s)
}

function q_hint(q)
{
    obj = "document.question"+q
    s = eval(obj + ".hint.value")
    correctness = "hint"
    s = '<span><strong>Hint:</strong> ' + s + '</span>'
    //eval("document.images['q"+q+"'].src='\/vts09\/images\/unmarked.gif'")
    eval("document.images.q"+q+".style.backgroundImage='url(/vts09/images/unmarked.gif)'")
    clearContent('furtherinfo'+q)
    addContent('furtherinfo'+q,s)
    //q_message(q+". Hint",s)
}

function q_explain(q)
{
    obj = "document.question"+q
    if (eval(obj+".locked.value") != "true" && eval(obj+".maxtries.value") != "") {
        q_unanswered()
    } else {
        s = eval(obj + ".explanation.value")
        s = '<span><strong>Further info:</strong> ' + s + '</span>'
        correctness = "further"
        eval("document.images.q"+q+".style.backgroundImage='url(/vts09/images/unmarked.gif)'")
        clearContent('furtherinfo'+q)
        addContent('furtherinfo'+q,s)
        //q_message(q+". Further Information",s)
    }
}

function q_revise(q)
{
    obj = "document.question"+q
    s = eval(obj + ".link.value")
    window.open(s)
}

function q_locked()
{
    q_message("Sorry","This question is now locked.")
}

function q_unanswered()
{
    q_message("Sorry","You must answer the question first!")
}

function crlf()
{
    return '<br />'
}


function spc()
{
    return '&nbsp;'
}

function q_sc_mark(q)
{
    obj = "document.question"+q
    tries = parseInt( eval(obj+".tries.value") )
    tries = tries + 1
    eval(obj+".tries.value=tries")
    maxtries = parseInt( eval(obj+".maxtries.value") )
    if (isNaN(maxtries)) {
        maxtries = 999  //unlimited
    } else {
        if ( eval(obj+".locked.value") == "true" ) {
            q_feedback(q,"")
            return
        }
        if (tries >= maxtries) {
            eval(obj+".locked.value='true'")
        }
        if (tries > maxtries) {
            q_feedback(q,"")
            return
        }
    }
    ans = parseInt( eval(obj+".answer.value") )
    if (isNaN(ans)) { ans = 0 }
    points = 0
    maxpoints = 0
    answered = false
    var feedback = ""
    correctness = "wrong"
    ilim = eval(obj+".choice.length")
    for (i=0 ; i<ilim; i++) {
        n = i + 1
        pts = parseInt( eval(obj+".score"+n+".value") )
        if (isNaN(pts)) {
            pts = 0
        }
        if (pts > maxpoints) {
            maxpoints = pts
        }
        if (eval(obj+".choice["+i+"].checked")) {
            answered = true
            
            feedback += eval(obj+".feedback"+n+".value") + crlf()
            points += pts
            if (n == ans) {
                if (maxtries != 999) {
                    eval(obj+".locked.value='true'")
                }
                eval("document.images.q"+q+".style.backgroundImage='url(/vts09/images/right.gif)'")
                hint = "<p style=\"margin-top: 0\"><strong>Well done!</strong></p>"
            } else {
                eval("document.images.q"+q+".style.backgroundImage='url(/vts09/images/incorrect.gif)'")
                hint = "<p style=\"margin-top: 0\"><strong>Sorry, that's not the right answer</strong></p>"
                
            }
        }
    }
    if (answered) {
        if (ans > 0 && eval(obj+".locked.value") == "true") {
            eval(obj+".choice["+(ans-1)+"].checked='true'")
        }
        eval(obj+".score.value=points")
        eval(obj+".maxscore.value=maxpoints")
        eval("lnk="+obj+".link.value")
        if (lnk != "") {
            lnk = 'For more information: <a href="%THISDOC%?page=lnk" target="_new">Click here</a>'+crlf()+crlf()
        }
        response = hint + feedback + lnk //+"Score: "+points+" / "+maxpoints
        response = response.replace(/"/ig, "&quot;");
        response = '<span>' + response + '</span>'
        eval(obj+".response.value=response")
        clearContent('furtherinfo'+q)
        addContent('furtherinfo'+q,response)
        //q_feedback(q,response)
    } else {
        addContent('furtherinfo'+q,'<span>You need to select an answer first</span>')
        //q_unanswered()
    }
}


function q_mc_mark(q)
{
    obj = "document.question"+q


    tries = parseInt( eval(obj+".tries.value") )
    tries = tries + 1
    eval(obj+".tries.value=tries")


    maxtries = parseInt( eval(obj+".maxtries.value") )
    if (isNaN(maxtries)) {
        maxtries = 999  //unlimited
    } else {
        if ( eval(obj+".locked.value") == "true" ) {
            q_feedback(q,"")
            return
        }
        if (tries >= maxtries) {
            eval(obj+".locked.value='true'")
        }
        if (tries > maxtries) {
            q_feedback(q,"")
            return
        }
    }


    answers = eval(obj+".answer.value")
    alist = answers.split(",")
    alistlength = alist.length
    clist = new Array(0,0,0,0,0)
    for (i=0; i<alistlength; i++) {
        ans = parseInt( alist[i] )
        if (!isNaN(ans)) {
            ans = ans - 1
            if (ans < clist.length)
                clist[ans] = 1
        }
    }


    points = 0
    maxpoints = 0
    answered = false
    var feedback = ""
    correctness = "nearly"
    cnt = 0
    ilim = eval(obj+".choice.length")
    for (i=0; i<ilim; i++) {
        n = i + 1
        pts = parseInt( eval(obj+".score"+n+".value") )
        if (isNaN(pts)) {
            pts = 0
        }
        if (pts > 0) {
            maxpoints += pts
        }
        chk = eval(obj+".choice["+i+"].checked")
        if ((clist[i] == 1 && chk) || (clist[i] == 0 && !chk)) {
            cnt = cnt + 1
            if (pts > 0) {
                points += pts
            }
        } else {
            if (pts < 0) {
                points += pts   //deduct points if -ve and got wrong
            }
        }
    }
    if (cnt == 0) {
        correctness = "wrong"
    }
    if (cnt == ilim) {
        if (maxtries != 999) {
            eval(obj+".locked.value='true'")
        }
        correctness = "correct"
    }
    
    for (i=0; i<ilim; i++) {
        n = i + 1
        if (eval(obj+".choice["+i+"].checked")) {
            answered = true
            chk = true
            fbstr = eval(obj+".feedback"+n+".value")
            if (fbstr != "") {
                feedback += fbstr + crlf() + crlf()
            }
        } else {
            chk = false
        }

    }
    if (answered) {
        eval("lnk="+obj+".link.value")
        if (lnk != "") {
            lnk = 'For more information: <a href="%THISDOC%?page=lnk" target="_new">Click here</a>'+crlf()+crlf()
        }
        if (correctness == 'correct') {
            eval("document.images.q"+q+".style.backgroundImage='url(/vts09/images/right.gif)'")
            hint = "<p style=\"margin-top: 0\"><strong>Well done!</strong></p>"
        } else if (correctness == 'nearly') {
            eval("document.images.q"+q+".style.backgroundImage='url(/vts09/images/incorrect.gif)'")
            hint = "<p style=\"margin-top: 0\"><strong>Sorry, you have not selected the right combination of answers</strong></p>"
        } else {
            eval("document.images.q"+q+".style.backgroundImage='url(/vts09/images/incorrect.gif)'")
            hint = "<p style=\"margin-top: 0\"><strong>Sorry, you have not selected the right combination of answers</strong></p>"
        }
        response = hint + feedback + lnk //+"Score: "+points+" / "+maxpoints
        response = response.replace(/"/ig, "&quot;");
        response = '<span>' + response + '</span>'
        eval(obj+".response.value=response")

        clearContent('furtherinfo'+q)
        addContent('furtherinfo'+q,response)
        //q_feedback(q,response)
    } else {
        addContent('furtherinfo'+q,'<span>You need to select an answer first</span>')
        //q_unanswered()
    }
}



function addContent(divName, content)
{
    document.getElementById(divName).innerHTML = content;
}

function clearContent(divName)
{
    document.getElementById(divName).innerHTML = '';
}


function sh(n) {
    var helpobj = document.getElementById("showmore" + n);
    var butobj  = document.getElementById("showbut" + n);
    if (helpobj.style.display != "block") {
        helpobj.style.display = "block";
        butobj.src = "/sconul/images/hide_notes.gif";
    } else {
        helpobj.style.display = "none";
        butobj.src = "/sconul/images/show_notes.gif";
    }
    return true;
}

function AllowNoDups()
{
   var cookie_ls = document.cookie;
   if (cookie_ls.indexOf(document.location) > -1) 
   {
	alert("You've already submitted this form. Thank you for your interest.  ");
	return false;
   }
   else
   {
	document.cookie = window.location.href + " from " + document.referrer + "; path=/;";
        return true;
   };
};

