<%@ Language=VBScript %>
<!--#INCLUDE file="ADOVBS.inc"-->
<%
	dim int_folio, str_proceso
	dim sqlQuery, rs, conn, vp1(4), vp2(4), vp3(4), vp4(4), vp5(4), vp6(4), vp7(4), vp8(4), vp9(4), STR_SALIR


	dsnt= application("bd_sieps")

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

	conn.Open dsnt
	   
	int_folio = Request.QueryString("folio")
	str_proceso = Request.QueryString("proceso")

	STR_SALIR= Request.Form("SALIR")

	for i=1 to 4
		vp1(i)= Request.Form("1"&"-"&i)
	next
	for i=1 to 4
		vp2(i)= Request.Form("2"&"-"&i)
	next
	for i=1 to 4
		vp3(i)= Request.Form("3"&"-"&i)
	next
	for i=1 to 4
		vp4(i)= Request.Form("4"&"-"&i)
	next
	for i=1 to 4
		vp5(i)= Request.Form("5"&"-"&i)
	next
	for i=1 to 4
		vp6(i)= Request.Form("6"&"-"&i)
	next
	for i=1 to 4
		vp7(i)= Request.Form("7"&"-"&i)
	next
	for i=1 to 4
		vp8(i)= Request.Form("8"&"-"&i)
	next
	for i=1 to 4
		vp9(i)= Request.Form("9"&"-"&i)
	next


	if str_proceso="alta" then ' si es una alta
		sqlQuery = "SELECT * FROM dat_forapre 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_forapre (soli_folio, r11, r12, r13, r14,"_
			&" r21, r22, r23, r24, r31, r32, r33, r34, r41, r42, r43, r44,"_
			&" r51, r52, r53, r54, r61, r62, r63, r64, r71, r72, r73, r74,"_
			&" r81, r82, r83, r84, r91, r92, r93, r94)"_
			&" VALUES(" & int_folio & "," &  vp1(1) & "," &  vp1(2) & "," &  vp1(3) & "," &  vp1(4) & ","_
			&" " &  vp2(1) & "," &  vp2(2) & "," &  vp2(3) & "," &  vp2(4) & ", "_
			&" " &  vp3(1) & "," &  vp3(2) & "," &  vp3(3) & "," &  vp3(4) & ", "_
			&" " &  vp4(1) & "," &  vp4(2) & "," &  vp4(3) & "," &  vp4(4) & ", "_
			&" " &  vp5(1) & "," &  vp5(2) & "," &  vp5(3) & "," &  vp5(4) & ","_
			&" " &  vp6(1) & "," &  vp6(2) & "," &  vp6(3) & "," &  vp6(4) & ","_
			&" " &  vp7(1) & "," &  vp7(2) & "," &  vp7(3) & "," &  vp7(4) & ", "_
			&" " &  vp8(1) & "," &  vp8(2) & "," &  vp8(3) & "," &  vp8(4) & ", "_
			&" " &  vp9(1) & "," &  vp9(2) & "," &  vp9(3) & "," &  vp9(4) & ")"
			
			On Error Resume Next
			conn.Execute sqlQuery,,adCmdText Or adExecuteNoRecords			
			handleError(conn)
			conn.CommitTrans
		END IF
		rs.Close
	elseif str_proceso="cambio" then  ' si es un cambio
		conn.BeginTrans
		sqlQuery = "SELECT * FROM dat_forapre WHERE soli_folio =" & int_folio
		
		On Error Resume Next
		rs.Open sqlQuery, conn,adOpenForwardOnly,adLockReadOnly,1
		handleError(conn)
		
		IF rs.EOF = false then
			sqlQuery = "UPDATE dat_forapre SET r11 =" & vp1(1) & ", r12 =" & vp1(2) & ", r13 =" & vp1(3) & ", r14 =" & vp1(4) & ","_
			& " r21 =" & vp2(1) & ", r22 =" & vp2(2) & ", r23 =" & vp2(3) & ", r24 =" & vp2(4) & ","_
			& " r31 =" & vp3(1) & ", r32 =" & vp3(2) & ", r33 =" & vp3(3) & ", r34 =" & vp3(4) & ", "_
			& " r41 =" & vp4(1) & ", r42 =" & vp4(2) & ", r43 =" & vp4(3) & ", r44 =" & vp4(4) & ", "_
			& " r51 =" & vp5(1) & ", r52 =" & vp5(2) & ", r53 =" & vp5(3) & ", r54 =" & vp5(4) & ", "_
			& " r61 =" & vp6(1) & ", r62 =" & vp6(2) & ", r63 =" & vp6(3) & ", r64 =" & vp6(4) & ", "_
			& " r71 =" & vp7(1) & ", r72 =" & vp7(2) & ", r73 =" & vp7(3) & ", r74 =" & vp7(4) & ", "_
			& " r81 =" & vp8(1) & ", r82 =" & vp8(2) & ", r83 =" & vp8(3) & ", r84 =" & vp8(4) & ", "_
			& " r91 =" & vp9(1) & ", r92 =" & vp9(2) & ", r93 =" & vp9(3) & ", r94 =" & vp9(4) & " 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
%>
