<%@ Language=VBScript %>
<!--#INCLUDE file="ADOVBS.inc"-->
<%
	Response.Buffer=true
	dim solicitudeFolio
	dim sqlQuery, rs, conn
	dim maxIncogPerTabb, incognitas, tabbs

	dsn = application("bd_sieps")

	set conn = server.CreateObject ("ADODB.Connection")
	set rs = server.CreateObject ("ADODB.Recordset")

	conn.Open dsn

	testId = Session("testId")
	solicitudeFolio = Session("solicitudeFolio")
	maxIncogPerTabb = Request.Form("incognitas")
	'El numero max de tabs que tiene la prueba
	tabbs = Request.Form("incognitas").Count

	incognitas = Request.Form("incognita")
	' El tamaņo de incognitas me va a dar el numero total de incognitas !!!
	maxIncognitas = Request.Form("incognita").Count
	nextTest = Request.Form("nextTest")

	'Reviso si no hay un registro previo con el folio. De haber, hacer un update
	sqlQuery = "SELECT soli_folio FROM dat_ortografia WHERE soli_folio =" & solicitudeFolio
	
'sendMensajeToConsole("El testID: "&testId)	

	if testId = "ORTO" then	
		on Error Resume Next
		rs.Open sqlQuery, conn,adOpenForwardOnly,adLockReadOnly,1
		handleError(conn)
		
		if rs.EOF = true then ' si no existe... entonces hacer un Insert
			conn.BeginTrans
			sqlQuery = "INSERT INTO dat_ortografia ( soli_folio, "
			sqlQuery = sqlQuery & getQueryFields("R",tabbs ) & " ) "
			sqlQuery = sqlQuery & "VALUES ( "&solicitudeFolio&", "
			sqlQuery = sqlQuery & getQueryValues() & " ) "
			
'sendMensajeToConsole("la primer parte de query queda: "&sqlQuery)
			On Error Resume Next
			conn.Execute sqlQuery,,adCmdText Or adExecuteNoRecords			
			handleError(conn)
			conn.CommitTrans
		else 'ya existe el registro.... entonces hacer un update. Esto es para guardar los cambios de la segunda parte. La de los acentos.

			' Actualizar el registro.
'			conn.BeginTrans
'			sqlQuery = "UPDATE dat_ortografia SET soli_folio="&solicitudeFolio
			' Agregar los campos
'			sqlQuery = sqlQuery & " WHERE soli_folio="&solicitudeFolio
			
'			On Error Resume Next
'			conn.Execute sqlQuery,,adCmdText Or adExecuteNoRecords			
'			handleError(conn)
'			conn.CommitTrans
		end if
	else	
		conn.BeginTrans
		sqlQuery = "UPDATE dat_ortografia SET "		
		sqlQuery = sqlQuery & getQueryFieldValue()
		sqlQuery = sqlQuery & " WHERE soli_folio = " & solicitudeFolio
'sendMensajeToConsole("el query UPDATE queda: "&sqlQuery)		
		On Error Resume Next
		conn.Execute sqlQuery,,adCmdText Or adExecuteNoRecords			
		handleError(conn)
		conn.CommitTrans
	end if

	'clean up
	If rs.State = adStateOpen then
		rs.Close
	End If
	If conn.State = adStateOpen then
		conn.Close
	End If
	Set rs = Nothing
	Set conn = Nothing
	
	if nextTest <> "" then
		' Borrar de la session el valor anterior de la prueba del 		
		' Poner en session el nuevo valor de la prueba de orto2
		Session("testId") = "ORTO2"
		Response.Redirect("TestStart.asp")		
	else
		Response.Redirect("uti_siguiente.asp")
	end if

' Parameters: 
' prefixField: puede ser R o BR, es el prefix que tiene el campo.
' tabbs: la cantidad de tabbs que tiene la prueba
function getQueryFields(prefixField, maxTabbs)
	dim qry
	dim fieldName
	for tabb = 1 to maxTabbs
		for element = 1 to Request.Form("incognitas")(tabb)
			fieldName = prefixField & tabb & element
			qry = qry & fieldName & ","			
		next
	next
	' Quito la ultima coma
	qry = Left(qry,Len(qry)-1)
	getQueryFields = qry
end function

' 
function getQueryFieldValue()
	dim qry, auxVal
	dim fieldName
	dim AuxTab(4)
	auxtab(1)=30
	auxtab(2)=14
	auxtab(3)=7
	auxtab(4)=9
	prefixField = "BR"	
	auxVal = "0"
	for tabb = 1 to 4
		for element = 1 to auxtab(tabb)'Request.Form("incognitas")(tabb)
			auxVal = Request.form("incognita")(element)
			if auxVal <> "" then
				auxVal = "1"
			else
				auxVal = "0"
			end if
			fieldName = prefixField & tabb & element & " = " & "'"& auxVal & "',"
			qry = qry & fieldName
		next
	next
	' Quito la ultima coma
	qry = Left(qry,Len(qry)-1)
	getQueryFieldValue = qry
end function

function getQueryValues()
	dim auxQry
	dim aux, idx
	aux = ""
	idx = 1
	str =""
	for n = 1 to Request.form("incognita").Count
		aux = Request.Form("incognita")(n)
		if aux = "" then
			aux = "_"
		end if
		aux = "'"& aux & "',"
		
		auxQry = auxQry & aux
	next	
	auxQry = Left(auxQry,Len(auxQry)-1)
		
	getQueryValues = auxQry
end function

sub handleError(connectionRef)
	dim objErr
	dim str_c
	dim strrr, flag
	flag = 0

	if connectionRef.Errors.Count <> 0 Then
		str_c = str_c & "Count: " 
		str_c = str_c & connectionRef.Errors.Count
		for each objErr in connectionRef.Errors
			if objErr.Number <> 0 then ' Number = 0 el error es meramente informativo
				str_c = str_c & " Number: "
				str_c = str_c & objErr.Number			
				
				str_c = str_c & " Description: "
				str_c = str_c & objErr.Description  
			  
				str_c=str_c&" Native error: "
				str_c=str_c&objErr.NativeError 
				  
				str_c=str_c&" SQL state: "
				str_c=str_c&objErr.SQLState
				flag = 1
			end if
		next		
		if flag <> 0 then
			' clean up		
			if conn.State = adStateOpen then
				conn.Close
			end if
			
			set rs = Nothing
			set conn = Nothing
			set connectionRef = nothing
			
			strrr = "uti_console.asp?mensaje=" & "Error: " & str_c 
			Response.Redirect(strrr)
		end if		
	end if	
end sub

sub sendMensajeToConsole(mensaje)
	dim strrr
	strrr="uti_console.asp?mensaje=" & mensaje
	Response.Redirect(strrr)
end sub
%>