<%@ Language=VBScript %>
<!--#INCLUDE file="ADOVBS.inc"-->
<%
	dim int_folio, str_proceso
	dim sqlQuery, rs, conn, str_res_1, str_res_2, str_res_3, STR_SALIR


	dsn= application("bd_sieps")

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

	conn.Open dsn
	   
	int_folio = Request.QueryString("folio")
	str_proceso = Request.QueryString("proceso")
	STR_SALIR= Request.Form("SALIR")

	for i=1 to 566
		int_numero=0
		for j=1 to 2
			if Request.Form("r"&i&"-"&j)="on" then
				int_numero =J	
			end if
		next
		if i<=250 then
			str_res_1=str_res_1 & int_numero
		ElseIf i > 250 And i<= 500 Then
			str_res_2=str_res_2 & int_numero
		ElseIf i > 500 And i <= 566 Then
			str_res_3=str_res_3 & int_numero
		end if
	next


	if str_proceso="alta" then ' si es una alta
		sqlQuery = "SELECT * FROM dat_mmpi WHERE soli_folio =" & int_folio
		
		On Error Resume Next
		rs.Open sqlQuery, conn,adOpenForwardOnly,adLockReadOnly,1
		handleError(conn)
		
		IF rs.EOF = true then ' si no existe
			conn.BeginTrans
			
			sqlQuery = "INSERT INTO dat_mmpi (soli_folio, r1, r2, r3)"_
			&" VALUES(" & int_folio & ",'" &  str_res_1 & "','" &  str_res_2 & "','" &  str_res_3 & "')"
			
			On Error Resume Next
			conn.Execute sqlQuery,,adCmdText Or adExecuteNoRecords			
			handleError(conn)
			conn.CommitTrans
		END IF

	elseif str_proceso="cambio" then  ' si es un cambio
		sqlQuery = "SELECT * FROM dat_mmpi WHERE soli_folio =" & int_folio
		
		On Error Resume Next
		rs.Open sqlQuery, conn,adOpenForwardOnly,adLockReadOnly,1
		handleError(conn)
		
		IF rs.EOF = false then
			conn.BeginTrans
			
			sqlQuery = "UPDATE dat_mmpi SET r1 ='" & str_res_1 & "', r2 ='" & str_res_2 & "', r3 ='" & str_res_3 & "' WHERE soli_folio =" & int_folio
			
			On Error Resume Next
			conn.Execute sqlQuery,,adCmdText Or adExecuteNoRecords			
			handleError(conn)
			conn.CommitTrans
		END IF		
	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 STR_SALIR ="SALIR" then
		Response.Redirect("aplicacion_termina.asp")
	else
		Response.Redirect("uti_siguiente.asp")
	end if

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
%>