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

	dsn= application("bd_sieps")

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

'sendMensajeToConsole("ya abrio la base")

	int_folio = Request.QueryString("folio")
	str_proceso = Request.QueryString("proceso")
'sendMensajeToConsole("el folio: "&int_folio+" Proceso: "+str_proceso)	
	STR_SALIR= Request.Form("SALIR")

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

	if str_proceso="alta" then ' si es una alta
		STR_CONSULTA = "SELECT * FROM dat_valores_personales WHERE soli_folio =" & int_folio
		On Error Resume Next
		rs.Open STR_CONSULTA, conn,adOpenForwardOnly,adLockReadOnly,1
		handleError(conn)
		IF rs.EOF = true then ' si no existe
			conn.BeginTrans
			STR_CONSULTA = "INSERT INTO dat_valores_personales (soli_folio, vp11, vp12, vp13, vp14, vp15, vp16,"_
			&" vp21, vp22, vp23, vp24, vp25, vp26, vp31, vp32, vp33, vp34, vp35, vp36, vp41, vp42, vp43, vp44, vp45, vp46,"_
			&" vp51, vp52, vp53, vp54, vp55, vp56, vp61, vp62, vp63, vp64, vp65, vp66, vp71, vp72, vp73, vp74, vp75, vp76,"_
			&" vp81, vp82, vp83, vp84, vp85, vp86, vp91, vp92, vp93, vp94, vp95, vp96, vp101, vp102, vp103, vp104, vp105, vp106)"_
			&" VALUES(" & int_folio & "," &  vp1(1) & "," &  vp1(2) & "," &  vp1(3) & "," &  vp1(4) & "," &  vp1(5) & "," &  vp1(6) & ","_
			&" " &  vp2(1) & "," &  vp2(2) & "," &  vp2(3) & "," &  vp2(4) & "," &  vp2(5) & "," &  vp2(6) & ", "_
			&" " &  vp3(1) & "," &  vp3(2) & "," &  vp3(3) & "," &  vp3(4) & "," &  vp3(5) & "," &  vp3(6) & ", "_
			&" " &  vp4(1) & "," &  vp4(2) & "," &  vp4(3) & "," &  vp4(4) & "," &  vp4(5) & "," &  vp4(6) & ", "_
			&" " &  vp5(1) & "," &  vp5(2) & "," &  vp5(3) & "," &  vp5(4) & "," &  vp5(5) & "," &  vp5(6) & ","_
			&" " &  vp6(1) & "," &  vp6(2) & "," &  vp6(3) & "," &  vp6(4) & "," &  vp6(5) & "," &  vp6(6) & ","_
			&" " &  vp7(1) & "," &  vp7(2) & "," &  vp7(3) & "," &  vp7(4) & "," &  vp7(5) & "," &  vp7(6) & ", "_
			&" " &  vp8(1) & "," &  vp8(2) & "," &  vp8(3) & "," &  vp8(4) & "," &  vp8(5) & "," &  vp8(6) & ", "_
			&" " &  vp9(1) & "," &  vp9(2) & "," &  vp9(3) & "," &  vp9(4) & "," &  vp9(5) & "," &  vp9(6) & ", "_
			&" " &  vp10(1) & "," &  vp10(2) & "," &  vp10(3) & "," &  vp10(4) & "," &  vp10(5) & "," &  vp10(6) & ")"
			
			On Error Resume Next		
			conn.Execute STR_CONSULTA,,adCmdText Or adExecuteNoRecords			
			sendError(conn.Errors)
			conn.CommitTrans
		END IF
		
	elseif str_proceso="cambio" then  ' si es un cambio
		conn.BeginTrans
		STR_CONSULTA = "SELECT * FROM dat_valores_personales WHERE soli_folio =" & int_folio
		rs.Open STR_CONSULTA, conn,adOpenForwardOnly,adLockReadOnly,1
		IF rs.EOF = false then
			STR_CONSULTA = "UPDATE dat_valores_personales SET vp11 =" & vp1(1) & ", vp12 =" & vp1(2) & ", vp13 =" & vp1(3) & ", vp14 =" & vp1(4) & ", vp15 =" & vp1(5) & ", vp16 =" & vp1(6) & ","_
			& " vp21 =" & vp2(1) & ", vp22 =" & vp2(2) & ", vp23 =" & vp2(3) & ", vp24 =" & vp2(4) & ", vp25 =" & vp2(5) & ", vp26 =" & vp2(6) & ","_
			& " vp31 =" & vp3(1) & ", vp32 =" & vp3(2) & ", vp33 =" & vp3(3) & ", vp34 =" & vp3(4) & ", vp35 =" & vp3(5) & ", vp36 =" & vp3(6) & ", "_
			& " vp41 =" & vp4(1) & ", vp42 =" & vp4(2) & ", vp43 =" & vp4(3) & ", vp44 =" & vp4(4) & ", vp45 =" & vp4(5) & ", vp46 =" & vp4(6) & ", "_
			& " vp51 =" & vp5(1) & ", vp52 =" & vp5(2) & ", vp53 =" & vp5(3) & ", vp54 =" & vp5(4) & ", vp55 =" & vp5(5) & ", vp56 =" & vp5(6) & ", "_
			& " vp61 =" & vp6(1) & ", vp62 =" & vp6(2) & ", vp63 =" & vp6(3) & ", vp64 =" & vp6(4) & ", vp65 =" & vp6(5) & ", vp66 =" & vp6(6) & ", "_
			& " vp71 =" & vp7(1) & ", vp72 =" & vp7(2) & ", vp73 =" & vp7(3) & ", vp74 =" & vp7(4) & ", vp75 =" & vp7(5) & ", vp76 =" & vp7(6) & ", "_
			& " vp81 =" & vp8(1) & ", vp82 =" & vp8(2) & ", vp83 =" & vp8(3) & ", vp84 =" & vp8(4) & ", vp85 =" & vp8(5) & ", vp86 =" & vp8(6) & ", "_
			& " vp91 =" & vp9(1) & ", vp92 =" & vp9(2) & ", vp93 =" & vp9(3) & ", vp94 =" & vp9(4) & ", vp95 =" & vp9(5) & ", vp96 =" & vp9(6) & ", "_
			& " vp101 =" & vp10(1) & ", vp102 =" & vp10(2) & ", vp103 =" & vp10(3) & ", vp104 =" & vp10(4) & ", vp105 =" & vp10(5) & ", vp106 =" & vp10(6) & " WHERE soli_folio =" & int_folio
			On Error Resume Next		
			conn.Execute STR_CONSULTA,,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
%>
