Utility Functions

sub log_g(s)
	select case VarType(s)
		case 0
			s = "empty"
		case 1
			s = "null"
		case 9
			if typeName(s) = "Dictionary" then
				keys = s.keys()
				For Each k In keys
					response.write k & ": " & s.item(k) & "<br/>"
				next
				response.end
			else
				s = "object"
			end if
		case 11
			if s = true then
				s = "true"
			else
				s = "false"
			end if
		case 13
			s = "data object"
		case else
			if VarType(s) > 8192 then
				for i = 0 to uBound(s)
					response.write "Array(" & i & "): " & s(i) & "<br/>"
				next
				response.end
			end if
	end select

	response.write s
	response.end
end sub

Function CharsetBIG5ToUnicode(str)
	Dim stream
		
	if isNull(str) or len(str) = 0 then
		CharsetBIG5ToUnicode = str
	else
		Set stream = CreateObject("ADODB.Stream")
		stream.Type = 2	'adTypeText
		stream.Charset = "windows-1252"
		stream.Open
		stream.WriteText str

		stream.Position = 0
		stream.Charset = "big5"
		CharsetBIG5ToUnicode = stream.ReadText
	end if
End Function

Function UnicodeToCharsetBIG5(str)
	Dim stream

	if isNull(str) or len(str) = 0 then
		UnicodeToCharsetBIG5 = str
	else
		Set stream = CreateObject("ADODB.Stream")
		stream.Type = 2	'adTypeText
		stream.Charset = "big5"
		stream.Open
		stream.WriteText str

		stream.Position = 0
		stream.Charset = "windows-1252"
		UnicodeToCharsetBIG5 = stream.ReadText
	end if
End Function

function is_empty(s, include_zero)
	result = false
	if isNull(include_zero) = true then include_zero = true end if

	if isNull(s) then result = true end if
	if result = false and result = "" then result = true end if
	if result = false and include_zero = true and isnumeric(s) then
		if cLng(s) = 0 then result = true end if
	end if

	is_empty = result
end function

public function in_array(v, arr)
	dim result, i
	
	result = -1
	i = 0
	
	if not isNull(arr) and uBound(arr) > -1 then
		for i = 0 to uBound(arr)
			if cStr(arr(i)) = cStr(v) then
				result = i
				exit for
			end if
		next
	end if
	
	in_array = result
end function


public function check_int(n)
	dim digits_str(9), result, i, t
	
	result = true
	if isNull(n) or len(n) = 0 then
		result = false
	else
		for i = 0 to uBound(digits_str)
			digits_str(i) = cStr(i)
		next
	
		for i = 1 to len(n)
			t = cstr(n)
			if in_array(mid(t, i, 1), digits_str) = -1 then
				result = false
				exit for
			end if
		next
	end if
	
	check_int = result
end function


public function replaceBR(s)
	s = replace(s, "<br>", chr(10))
	
	replaceBR = s
end function


public function replaceChr10(s)
	s = replace(s, chr(10), "<br>")
	
	replaceChr10 = s
end function


public function replace_quote(input)
	dim replace_list(11), replace_list_to(11), result, temp_str, replace_index, i

	result = input
	replace_list(0) = "'" : replace_list(1) = """"
	replace_list(2) = "<" : replace_list(3) = ">"
	replace_list(4) = chr(12) & chr(15) : replace_list(5) = chr(15) & chr(12)
	replace_list(6) = chr(10) & chr(13) : replace_list(7) = chr(13) & chr(10)
	replace_list(8) = chr(10) : replace_list(9) = chr(12)
	replace_list(10) = chr(13) : replace_list(11) = chr(15)

	replace_list_to(0) = "&apos;" : replace_list_to(1) = "&quot;"
	replace_list_to(2) = "&lt;" : replace_list_to(3) = "&gt;"
	replace_list_to(4) = "<br/>" : replace_list_to(5) = "<br/>"
	replace_list_to(6) = "<br/>" : replace_list_to(7) = "<br/>"
	replace_list_to(8) = "" : replace_list_to(9) = ""
	replace_list_to(10) = "" : replace_list_to(11) = ""
	
	for i = 0 to Ubound(replace_list)
		if inStr(result, replace_list(i)) > 0 then
			result = replace(result, replace_list(i), replace_list_to(i))
		end if
	next
	
	replace_quote = result
end function

'data is disctionary
function params(data)
	str = ""
	sep = ""
	keys = data.keys()
	For Each k In keys
		str = str & sep & k & "=" & Server.URLEncode(data.item(k))
		sep = "&"
	next

	params = str
end function