	<!--============================================================================-->
	<!--===========    Copyright ©2003 YAKOM - All rights reserved        ==========-->
	<!--===========    http://www.yakom.it ====== info@yakom.it           ==========-->
	<!--===========    YUDO - The Ultimate ASP Web Desk                   ==========-->
	<!--===========    YUDO Release:2.0Beta                               ==========-->
	<!--===========    Support forums: http://www.freedesk.it             ==========-->
	<!--============================================================================-->

<%
'Database connection
Set cn = Server.CreateObject("ADODB.Connection")
cn.open path

Set rs = server.CreateObject ("ADODB.Recordset")

function cleanstr(val)
	val=replace(trim(val), "'", "´")
	cleanstr=val
end function

function cFecha(valor)
	cFecha=mid(valor,7,2) & "/" & mid(valor,5,2) & "/" & mid(valor,1,4)
end function

function dbFecha(valor)
	dbFecha=mid(valor,7,4) & mid(valor,4,2) & mid(valor,1,2)
end function

function cHora(valor)
	cHora=mid(valor,1,2) & ":" & mid(valor,3,2) & ":" & mid(valor,5,2)
end function

function dbHora(valor)
	dbHora=replace(valor,":","")
end function

function splittime(mins)
	elapsedday=0
	elapsedhour=0
	elmin=0
	if mins>=60 then
		elapsedhour=int(mins/60)
		elmin=mins mod 60
		if elapsedhour>=24 then
			elapsedday=int(elapsedhour/24)
			elapsedhour=elapsedhour mod 24
		end if
	else
		elmin=mins mod 60
	end if
	elapsedtime = elapsedday & " " & lg_days & " " & elapsedhour & " " & lg_hours & " " & elmin & " " & lg_minutes
	splittime=elapsedtime
end function

'HexToDec encrypt/decrypt functions

Function Dec(hexadecimal)
    Dec = CInt("&H" & hexadecimal)
End Function

Function conversion(mensaje, clave, encripta)
    'Si encripta=true --> encripto si es false --> desencripto
    Dim i, j, X, cMensaje, nMensaje, cClave, nFinal, hFinal
    conversion = ""
    If encripta Then
        j = 1
        For i = 1 To Len(mensaje)
            cMensaje = Mid(mensaje, i, 1)
            cClave = Mid(clave, j, 1)
            nFinal = Asc(cMensaje) * 16 + Asc(cClave)
            hFinal = Hex(nFinal)
            For X = Len(hFinal) To 3
                hFinal = "0" & hFinal
            Next
            conversion = conversion & hFinal
            j = j + 1
            If j > Len(clave) Then
                j = 1
            End If
        Next
    Else
        conversion = ""
        j = 1
        For i = 1 To Len(mensaje) Step 4
            cMensaje = Mid(mensaje, i, 4)
            nMensaje = Dec(cMensaje)
            cClave = Mid(clave, j, 1)
            nFinal = (nMensaje - Asc(cClave)) / 16
            conversion = conversion & Chr(nFinal)
            j = j + 1
            If j > Len(clave) Then
                j = 1
            End If
        Next
    End If
End Function

'Database FieldGetType, SELECT, INSERT, UPDATE, DELETE functions

Dim sql,delim

Function gettype(table, field)
	rs.Open "SELECT * FROM " & table,cn,1,3
	tipo=rs.Fields(field).type
	tipostr=Array(129, 130, 200, 201, 202, 203)
	tiponum=Array(2, 3, 4, 5, 6, 11, 14, 16, 17, 18, 19, 20, 21, 131)
	tipodate=Array(7, 133, 134, 135)
	i=0
	found=false
	do while i<=ubound(tipostr) and not found
		if tipo=tipostr(i) then
			delim="'"
			found=true
		end if
		i=i+1
	loop
	i=0
	do while i<=ubound(tiponum) and not found
		if tipo=tiponum(i) then
			delim=""
			found=true
			if valor="" then valor=0
		end if
		i=i+1
	loop
	i=0
	do while i<=ubound(tipodate) and not found
		if tipo=tipodate(i) then
			if provider="sqlserver" then delim="'"
			if provider="access" then delim="#"
			found=true
		end if
		i=i+1
	loop
	rs.close
End Function

Sub dbselect (fields, table, criteria, order)
	sql="SELECT "
	for i=0 to ubound(fields)
		sql=sql & fields(i) & ", "
	next
	sql=mid(sql, 1, len(sql)-2)
	sql=sql & " FROM " & table
	if ubound(criteria)>0 then
		sql=sql & " WHERE "
		for i=0 to ubound(criteria) step 4
			gettype table,criteria(i)
			if criteria(i+1)="isnull" then
				sql=sql & criteria(i) & " IS NULL" & criteria(i+3)
			else
				sql=sql & criteria(i) & criteria(i+1) & delim & criteria(i+2) & delim & " " & criteria(i+3)
			end if
		next
	end if
	if order<>"" then
		sql=sql & " ORDER BY " & order
	end if
End Sub

Sub dbinsert(table, fields, values)
	sql="INSERT INTO " & table & " ("
	for i=0 to ubound(fields)
		sql=sql & fields(i) & ", "
	next
	sql=mid(sql, 1, len(sql)-2)
	sql=sql & ") VALUES("
	if ubound(values)>0 then
		for i=0 to ubound(values)
			gettype table,fields(i)
			sql=sql & delim & values(i) & delim & ", "
		next
	end if
	sql=mid(sql, 1, len(sql)-2)
	sql=sql & ")"
End Sub

Sub dbupdate(table, fields, values, criteria)
	sql="UPDATE " & table & " SET "
	for i=0 to ubound(fields)
		gettype table,fields(i)
		sql=sql & fields(i) & "=" & delim & values(i) & delim & ", "
	next
	sql=mid(sql, 1, len(sql)-2)
	if ubound(criteria)>0 then
		sql=sql & " WHERE "
		for i=0 to ubound(criteria) step 4
			gettype table,criteria(i)
			sql=sql & criteria(i) & criteria(i+1) & delim & criteria(i+2) & delim & " " & criteria(i+3)
		next
	end if
End Sub

Sub dbdelete(table, criteria)
	sql="DELETE FROM " & table
	if ubound(criteria)>0 then
		sql=sql & " WHERE "
		for i=0 to ubound(criteria) step 4
			gettype table,criteria(i)
			sql=sql & criteria(i) & criteria(i+1) & delim & criteria(i+2) & delim & " " & criteria(i+3)
		next
	end if
End Sub

%>