<%
	Dim tPollQuestionMaster, tPollQuestionDetails, tPollResultMaster, tPollResultDetails, tQuizPollQuestionMaster, tQuizPollQuestionContent, tQuizPollQuestionDetails, tQuizPollNavigation, tQuizPollResultsPost, tQuizWriteResults
	tPollQuestionMaster =  ReadTemplateIfExists(sTemplateDIR & "Poll.Question.Master.htm")
	tPollQuestionDetails =  ReadTemplateIfExists(sTemplateDIR & "Poll.Question.Details.htm")
	'Za kviz
	tQuizPollQuestionMaster =  ReadTemplateIfExists(sTemplateDIR & "QuizPoll.Question.Master.htm")
	tQuizPollQuestionContent =  ReadTemplateIfExists(sTemplateDIR & "QuizPoll.Question.Content.htm")
	tQuizPollQuestionDetails =  ReadTemplateIfExists(sTemplateDIR & "QuizPoll.Question.Details.htm")
	tQuizPollNavigation =  ReadTemplateIfExists(sTemplateDIR & "QuizPoll.Navigation.htm")
	tQuizPollResultsPost =  ReadTemplateIfExists(sTemplateDIR & "QuizPoll.Results.Post.asp")
	
	tPollResultMaster =  ReadTemplateIfExists(sTemplateDIR & "Poll.Result.Master.htm")
	tPollResultDetails =  ReadTemplateIfExists(sTemplateDIR & "Poll.Result.Details.htm")
	
	tQuizWriteResults = ReadTemplateIfExists(sTemplateDIR &"Quiz.WriteResults.htm")
	
	'Za kviz-------------------------------------------------
	Sub IzpisiAnketeZaKviz(iQuizID)
		IzpisiVprasanjeZaKviz(iQuizID)
	End Sub
	
	Sub DisplayNavigationBar(iPage, iPages, sTemp)
		Dim sTempTemplate
		sTempTemplate = sTemp
		sTempTemplate = SafeReplace(sTempTemplate, "<!--QUIZ_NAVIGATION_PAGE_NUMBER-->", iPage)	
		if iPages <= iPage then
			sTempTemplate = SafeReplace(sTempTemplate, "<!--SLIKA-->", "images/Kviz/NextK.gif") 
		else
			sTempTemplate = SafeReplace(sTempTemplate, "<!--SLIKA-->", "images/Kviz/NextK.gif") 
		end if	
		Response.Write sTempTemplate
	End Sub
	
	Sub QuizResultsSave(iQuizID, iPointsNumber)
		Dim iCountPolls, i, iOdgovorID, iSumMax, iPointsProcent, iFirstPlace, iSecondPlace, iThirdPlace, sTempTemplate, sMestoText, sMestoPic

		objCmd.CommandText = "SELECT Sum( (SELECT  Max(pa.Points) FROM PollAnswer pa  where pa.pollID = p.ID)) as SumPoints FROM  Poll p WHERE p.KvizID =" &  iQuizID		
		objRS2.Open objCmd, , adOpenStatic, adLockReadOnly, adCmdText
		if not objRS2.EOF then
			iSumMax = objRS2("SumPoints")
		End if
		objRS2.Close
		iPointsProcent = Round(iPointsNumber / iSumMax * 100, 0)
		objCmd.CommandText = "SELECT * FROM Kviz WHERE id=" & iQuizID	
		objRS2.Open objCmd, , adOpenStatic, adLockReadOnly, adCmdText
		if not objRS2.EOF then
			iFirstPlace = objRS2("Procent1")
			iSecondPlace = objRS2("Procent2")
			iThirdPlace = objRS2("Procent3")
		End if
		if iPointsProcent > iFirstPlace then
			sMestoText = objRS2("Mesto1Text")
			sMestoPic = objRS2("Mesto1Pic")	
		elseif (iPointsProcent > iSecondPlace) and (iPointsProcent < iFirstPlace) then
			sMestoText = objRS2("Mesto2Text")
			sMestoPic = objRS2("Mesto2Pic")
		elseif (iPointsProcent > iThirdPlace) and (iPointsProcent < iSecondPlace) then
			sMestoText = objRS2("Mesto3Text")
			sMestoPic = objRS2("Mesto3Pic")
		elseif iPointsProcent < iThirdPlace then
			sMestoText = objRS2("Mesto4Text")
			sMestoPic = objRS2("Mesto4Pic")
		End if
		sTempTemplate = tQuizWriteResults
		sTempTemplate = SafeReplace(sTempTemplate, "<!--MESTOTEXT-->", sMestoText)
		if not ((sMestoPic = "") OR (sMestoPic = "x")) then sTempTemplate = SafeReplace(sTempTemplate, "<!--MESTOPIC-->", "<img src='" & cPictureDIR & "Kviz/" & sMestoPic & "' alt=''>")
		sTempTemplate = SafeReplace(sTempTemplate, "<!--POINTS-->", iPointsNumber)
		sTempTemplate = SafeReplace(sTempTemplate, "<!--QUIZID-->", iQuizID)
		sTempTemplate = SafeReplace(sTempTemplate, "<!--PROCENTI-->", iPointsProcent)
		response.Write(sTempTemplate)
		objRS2.Close
	End Sub
	
	Sub IzpisiVprasanjeZaKviz(iQuizID)
		Dim sVprasanje, iVprasanjeID, sContent, sMaster, iCounter, iRecordsPerPage, iPages, iPage, iStartRecord, iEndRecord, iOdgovorID, iPointsNumber, i, iEmpty, iTempPointsNumber, iKolikoCelih, iZmnozek
		
		iCounter	= 0
		sContent = ""
		objCmd.CommandText = "SELECT id, questiontext FROM [poll] WHERE KvizID=" & iQuizID & " ORDER BY SortOrder"		
		objRS.Open objCmd, , adOpenStatic, adLockReadOnly, adCmdText
		
		iRecordsPerPage = cQuizItemsPerPage	
		iPages = CInt(objRS.RecordCount / iRecordsPerPage  + .49)	
		iPage = CInt(Request("Page"))
		iPointsNumber = SafeStrToInt(Request("PointsNumber"), 0, cMaxIdent, sError)
		iTempPointsNumber = iPointsNumber
		
		if iPage <> 0 then
			iStartRecord = (iPage - 1) * iRecordsPerPage + 1
			if iPage = iPages then
				iKolikoCelih = objRS.RecordCount/iRecordsPerPage
				iZmnozek = iKolikoCelih * iRecordsPerPage
				iEndRecord = objRS.RecordCount - iZmnozek + iZmnozek
			else
				iEndRecord = iPage * iRecordsPerPage
			end if
			For i = iStartRecord to iEndRecord
				iOdgovorID = SafeStrToInt(Request("odgovor" & i),1 ,cMaxIdent, sError)
				if iOdgovorID > 0 then
					objCmd.CommandText = "SELECT Points FROM PollAnswer WHERE id =" &  iOdgovorID		
					objRS2.Open objCmd, , adOpenStatic, adLockReadOnly, adCmdText
					if not objRS2.EOF then
						iPointsNumber = iPointsNumber + objRS2("Points")
					End if
					objRS2.Close
				else
					iEmpty = 1
				end if
			Next
		End if

		if iPage < 1 then
			iPage = 1
		End if
		
		if iEmpty = 1 then
			response.Write("Izberite en odgovor pri <b>vsakem</b> vprašanju.")	
			iPage = iPage -1
			iEmpty = 0
			iPointsNumber = iTempPointsNumber
		end if

		if (Request("NextBtn.x") <> "") then
			iPage = iPage + 1
		end if

		if iPage > iPages then
			objRS.Close
			call QuizResultsSave(iQuizID, iPointsNumber)
			Exit Sub	
		end if
		
		iStartRecord = (iPage - 1) * iRecordsPerPage + 1
		iEndRecord = iPage * iRecordsPerPage
				
		Do While Not objRS.EOF
			iCounter = iCounter + 1
			if ((iCounter >= iStartRecord) AND (iCounter <= iEndRecord)) then
				sVprasanje = objRS("questiontext")
				iVprasanjeID = objRS("id")
				
				'Izpis vprasanj
					Dim sTempTemplate, sDetails, sTemp
					sDetails = ""
				
					objCmd.CommandText = "SELECT id, answertext FROM pollanswer WHERE pollid = " & iVprasanjeID & " order by id"		
					objRS2.Open objCmd, , adOpenStatic, adLockReadOnly, adCmdText		
					Do While Not objRS2.EOF
						sTempTemplate = tQuizPollQuestionDetails
						if SafeStrToInt(Request("odgovor" & iCounter),1 ,cMaxIdent, sError) = objRS2("id") then
							sTempTemplate = Replace(sTempTemplate, "<!---CHECKED-->", "checked")
						else
							sTempTemplate = Replace(sTempTemplate, "<!---CHECKED-->", "")
						end if	
						sTempTemplate = Replace(sTempTemplate, "<!--COUNT-->", iCounter)
						sTempTemplate = Replace(sTempTemplate, "<!--POLL_ID-->", objRS2("id"))
						sTempTemplate = Replace(sTempTemplate, "<!--POLL_TEXT-->", objRS2("answertext"))
						sDetails = sDetails & sTempTemplate
						objRS2.MoveNext
					Loop
					
					sTemp = tQuizPollQuestionContent
					sTemp = Replace(sTemp, "<!--SCRIPTNAME-->", Request.ServerVariables("SCRIPT_NAME"))
					sTemp = Replace(sTemp, "<!--POLL_QUESTION-->", sVprasanje)
					sTemp = Replace(sTemp, "<!--POLL_CONTENT-->", sDetails)
					sContent = sContent & sTemp
					objRS2.Close
				'End izpis vprasanj
			end if
			objRS.MoveNext
		Loop
		sMaster = tQuizPollNavigation
		sMaster = Replace(sMaster, "<!--CONTENT-->", sContent)
		sMaster = Replace(sMaster, "<!--QUIZ_POINTS_NUMBER-->", iPointsNumber)
		DisplayNavigationBar iPage, iPages, sMaster			
		objRS.Close
	End Sub

	
	'Za anketo----------------------------------------------
	Sub IzpisiAnketo
		Dim iVprasanjeID, strVoted
		iVprasanjeID = GetCurrentQuestionID()
		strVoted = Request.Cookies("Voted")
		
		if strVoted = CStr(iVprasanjeID) then
			IzpisiRezultate
		else
			IzpisiVprasanje
		end if
	End Sub
	
	Function GetCurrentQuestionID()
		Dim iVprasanjeID
		objCmd.CommandText = "SELECT id FROM poll WHERE active = 1" 		
		objRS.Open objCmd, , adOpenStatic, adLockReadOnly, adCmdText		
		Do While Not objRS.EOF
			iVprasanjeID = objRS("id")
			objRS.MoveNext
		Loop		
		objRS.Close		
		GetCurrentQuestionID = iVprasanjeID
	End Function

	Sub IzpisiVprasanje
		Dim sVprasanje, iVprasanjeID, sMaster
		sMaster = tPollQuestionMaster
				
		objCmd.CommandText = "SELECT id, questiontext, filename FROM poll WHERE active = 1"		
		objRS.Open objCmd, , adOpenStatic, adLockReadOnly, adCmdText		
		Do While Not objRS.EOF
		    sVprasanje = objRS("questiontext")
			iVprasanjeID = objRS("id")
			sFilename = objRS("Filename")
			objRS.MoveNext
		Loop
			
		objRS.Close
		
		Dim sTempTemplate, sDetails
		
		objCmd.CommandText = "SELECT id, answertext FROM pollanswer WHERE pollid = " & iVprasanjeID & " order by id"		
		objRS.Open objCmd, , adOpenStatic, adLockReadOnly, adCmdText		
		Do While Not objRS.EOF
			sTempTemplate = tPollQuestionDetails
			sTempTemplate = Replace(sTempTemplate, "<!--POLL_ID-->", objRS("id"))
			sTempTemplate = Replace(sTempTemplate, "<!--POLL_TEXT-->", objRS("answertext"))
			sDetails = sDetails & sTempTemplate
			objRS.MoveNext
		Loop
		
		sMaster = Replace(sMaster, "<!--SCRIPTNAME-->", Request.ServerVariables("SCRIPT_NAME") & "?" & Request.ServerVariables("QUERY_STRING"))
		sMaster = Replace(sMaster, "<!--POLL_QUESTION-->", sVprasanje)
		sMaster = Replace(sMaster, "<!--POLL_CONTENT-->", sDetails)
		'Display picture
		if sFilename <> "" then sPicture = "<img src=""" & cPictureDIR & "Poll/" & sFilename &""" alt="""">" else sPicture = ""
		sMaster = SafeReplace(sMaster, "<!--PICTURE-->", sPicture)
		response.Write(sMaster)
		objRS.Close
	End Sub

	Sub IzpisiRezultate
		Dim sMaster, sDetails, sVprasanje, iVprasanjeID
		sMaster = tPollResultMaster
		
		objCmd.CommandText = "SELECT id, questiontext, filename FROM poll WHERE active = 1"		
		objRS.Open objCmd, , adOpenStatic, adLockReadOnly, adCmdText		
		Do While Not objRS.EOF
		    sVprasanje = objRS("questiontext")
			iVprasanjeID = objRS("id")
			sFilename = objRS("Filename")
			objRS.MoveNext
		Loop
		objRS.Close

		objCmd.CommandText = "SELECT a.answertext, a.votecount, p.answercount FROM pollanswer a, poll p WHERE a.pollid = p.id AND p.active = 1 ORDER BY a.id;" 
		objRS.Open objCmd, , adOpenStatic, adLockReadOnly, adCmdText
		Dim dPercentage, sAnswerText, iVoteCount, sPercent, sWidthLeft, sWidthRight, sTempTemplate
		Do While Not objRS.EOF
			dPercentage = CDbl(objRS("votecount")  / objRS("answercount") * 100)
			sAnswerText = objRS("answertext")
			iVoteCount = objRS("votecount")
			sPercent = FormatPercent(dPercentage / 100, 1)
			sWidthLeft = CInt(dPercentage * 80 / 100)
			sWidthRight  = CInt((100 - dPercentage) * 80 / 100)
			
			sTempTemplate = tPollResultDetails	
			sTempTemplate = Replace(sTempTemplate, "<!--POLL_ANSWERTEXT-->", sAnswerText)
			sTempTemplate = Replace(sTempTemplate, "<!--POLL_VOTECOUNT-->", iVoteCount)
			sTempTemplate = Replace(sTempTemplate, "<!--POLL_PERCENT-->", sPercent)
			sTempTemplate = Replace(sTempTemplate, "<!--POLL_WIDTH_LEFT-->",sWidthLeft)
			sTempTemplate = Replace(sTempTemplate, "<!--POLL_WIDTH_RIGHT-->", sWidthRight)
			sDetails = sDetails & sTempTemplate
			
			objRS.MoveNext
		Loop
		sMaster = Replace(sMaster, "<!--POLL_QUESTION-->", sVprasanje)
		sMaster = Replace(sMaster, "<!--POLL_CONTENT-->", sDetails)
		
		'Display picture		
		if sFilename <> "" then sPicture = "<img src=""" & cPictureDIR & "Poll/" & sFilename &""" alt="""">" else sPicture = ""
		sMaster = SafeReplace(sMaster, "<!--PICTURE-->", sPicture)
		
		response.Write(sMaster)
		
		objRS.Close
	End Sub

	Sub ShraniGlas
		Response.ExpiresAbsolute = Now() - 1	
		Dim iOdgovorID
		iOdgovorID = CInt(Request("odgovor"))
		
		objCmd.CommandText = "UPDATE pollanswer SET votecount = votecount + 1 WHERE id = " & iOdgovorID		
		objCmd.Execute ,,adCmdText + adExecuteNoRecords
		objCmd.CommandText = "UPDATE poll SET answercount = answercount + 1 WHERE active = 1 "		
		objCmd.Execute ,,adCmdText + adExecuteNoRecords

		'We've gotta write a cookie
		Response.Cookies("Voted") = CStr(GetCurrentQuestionID())
		Response.Cookies("Voted").Expires = DateAdd("d",365,Date)
		
		'Send the user back to the previous page
		Response.Redirect Request("URLFrom")
	End Sub
%>
