<%@ Language=JScript debug="true" %>
<%@ import namespace="System.Data" %>
<%@ import namespace="System.Data.OleDb" %>
<% Response.Buffer=true;

var rst : OleDbDataReader;

var DbCmd : OleDbCommand = new OleDbCommand();
DbCmd.Connection=Application("appDbConnection");
if (DbCmd.Connection.State==ConnectionState.Open) DbCmd.Connection.Close();
DbCmd.CommandText="select survey_question from survey where survey_id="+Request("survey");
DbCmd.Connection.Open();
rst = DbCmd.ExecuteReader();
rst.Read();
%>
<HTML>
	<HEAD>
		<LINK rel="stylesheet" type="text/css" href="survey.css">
	</HEAD>
	<BODY>
		<form method="post" action="">
			<table>
				<tr>
					<td class="sq">
						<%=rst("survey_question")%>
					</td>
				</tr>
				<%
rst.Close();
if (DbCmd.Connection.State==ConnectionState.Open) DbCmd.Connection.Close();
DbCmd.CommandText="select so_text,so_number from survey_options where so_survey_id=" + Request("survey") + " order by so_number asc";
DbCmd.Connection.Open();
rst = DbCmd.ExecuteReader();

//rst.Open("select so_text,so_number from survey_options where so_survey_id=" + Request("survey") + " order by so_number asc",appDbConnection);
while( rst.Read() ) {

	Response.Write("<tr><td class=\"sopt\"><input type=\"radio\" name=\"surveyOpt\" value=\"" + (parseInt(rst("so_number"))+1) + "\">" + rst("so_text") + "</td></tr>");
	//removed: rst.MoveNext();
}		
rst.Close();
%>
				</tr>
			</table>
			<br>
			<table>
				<tr>
					<td>
						<input name="btnAction" type="submit" value="Vote">
					</td>
					<td>
						<input name="btnAction" type="submit" value="Results">
					</td>
				</tr>
			</table>
		</form>
		<%
		
if (Request.Form("btnAction")=="Vote") {	
	if(Request("surveyOpt") > 0 ) {

		//rst.Open("select so_votes from survey_options where so_survey_id=" + Request("survey") +
		//	" and so_number=" + (Request("surveyOpt")-1),appDbConnection);

		if (DbCmd.Connection.State==ConnectionState.Open) DbCmd.Connection.Close();
		DbCmd.CommandText="select so_votes from survey_options where so_survey_id=" + Request("survey") +
			" and so_number=" + (Request("surveyOpt")-1);
		DbCmd.Connection.Open();
		rst = DbCmd.ExecuteReader();
		rst.Read();
		var nextVoteCount:int=parseInt(rst("so_votes"))+1;
		rst.Close();

		//appDbConnection.Execute("update survey_options set so_votes=" + nextVoteCount + 
		//	" where  so_survey_id=" + Request("survey") +
		//	" and so_number=" + (Request("surveyOpt")-1));

		if (DbCmd.Connection.State==ConnectionState.Open) DbCmd.Connection.Close();
		DbCmd.CommandText="UPDATE survey_options set so_votes=" + nextVoteCount + 
			" where  so_survey_id=" + Request("survey") +
			" and so_number=" + (Request("surveyOpt")-1);
		DbCmd.Connection.Open();
		DbCmd.ExecuteNonQuery();
		DbCmd.Connection.Close();
		
	}	
	Response.Redirect("showResult.aspx?survey="+Request("survey"));
}
else if (Request("btnAction")=="Results")
	Response.Redirect("showResult.aspx?survey="+Request("survey"));

%>
	</BODY>
</HTML>
