<%
	'###########################################################################################
	'정규식을 이용하여 문자열을 치환하는 함수
	'###########################################################################################
	Public Function eregi_replace(ByVal pattern, ByVal replacestr, ByVal text)
		Dim eregObj

		'** Create regular expression
		Set eregObj = New RegExp

		eregObj.Pattern = pattern '** Set Pattern(패턴 설정)
		eregObj.IgnoreCase = True '** Set Case Insensitivity(대소문자 구분 여부)
		eregObj.Global = True '** Set All Replace(전체 문서에서 검색)

		eregi_replace = eregObj.Replace(text, replacestr) '** Replace String
	End Function

	'###########################################################################################
	'데이터입력시 쿼리에 영향주는 기회
	'###########################################################################################
	Function checkValue(inWord)
		inWord = eregi_replace("'", "''", inWord)
		inWord = eregi_replace(";", "", inWord)
		inWord = eregi_replace("""", "&quot;", inWord)
		inWord = eregi_replace("--", "", inWord)
		inWord = eregi_replace("\@variable ", "", inWord)
		inWord = eregi_replace("\@@variable ", "", inWord)
		inWord = eregi_replace("\+", "", inWord)
		inWord = eregi_replace("print ", "", inWord)
		inWord = eregi_replace("set ", "", inWord)
'		inWord = eregi_replace("\%", "", inWord)
		inWord = eregi_replace("union ", "", inWord)
		inWord = eregi_replace("insert ", "", inWord)
		inWord = eregi_replace("select ", "", inWord)
		inWord = eregi_replace("update ", "", inWord)
		inWord = eregi_replace("create ", "", inWord)
		inWord = eregi_replace("drop ", "", inWord)
		inWord = eregi_replace("openrowset ", "", inWord)
		inWord = eregi_replace("exec ", "", inWord)
		inWord = eregi_replace("&", "&amp;", inWord)
		inWord = eregi_replace("§", "", inWord)
		inWord = eregi_replace("∑", "", inWord)
		inWord = eregi_replace("∮", "", inWord)
'		inWord = eregi_replace("†", "", inWord)
		inWord = eregi_replace("╫", "", inWord)
		inWord = eregi_replace("╬", "", inWord)
		checkValue = inWord
	End Function

	'###########################################################################################
	'설문 종료 처리
	'###########################################################################################		
	Function EndProcess(Status)
		Dim SQL,Q_SQL
		Dim QRS,ResType
		
		If Request.Form("IDKEY")<>"" Then
			IDKEY = Request.Form("IDKEY")
		End if
		If Request.Form("VC_ID")<>"" Then
			VC_ID = Request.Form("VC_ID")
		End If
		
		RESTYPE = getIDValue("RESTYPE",IDKey)
		VC_ID = getIDValue("VC_ID",IDKey)

		If Trim(Status)="" then
			Status=10						
			SQL = "UPDATE " & TB_NAME & " SET Status=" & Status & ",EndDate=getDate(),Page='"& SURVEY_FINISH &"' WHERE [KEY]='" & IDKEY & "';"

			If TB_NAME1<>"" Then
				SQL = SQL & "UPDATE " & TB_NAME1 & " SET Status=" & Status & ",EndDate=getDate(),Page='"& SURVEY_FINISH &"' WHERE [KEY]='" & IDKEY & "'"
			End If
			
			If TB_NAME2<>"" Then
				SQL = SQL & "UPDATE " & TB_NAME2 & " SET Status=" & Status & ",EndDate=getDate(),Page='"& SURVEY_FINISH &"' WHERE [KEY]='" & IDKEY & "'"
			End if


		ElseIf Status="99" then	'Quota Out
			SQL = "UPDATE " & TB_NAME & " SET Status=" & Status & ",EndDate=getDate(),Page='"& QUOTA_PAGE &"' WHERE [KEY]='" & IDKEY & "';"

			If TB_NAME1<>"" Then
				SQL = SQL & "UPDATE " & TB_NAME1 & " SET Status=" & Status & ",EndDate=getDate(),Page='"& QUOTA_PAGE &"' WHERE [KEY]='" & IDKEY & "'"
			End If

			If TB_NAME2<>"" Then
				SQL = SQL & "UPDATE " & TB_NAME2 & " SET Status=" & Status & ",EndDate=getDate(),Page='"& QUOTA_PAGE &"' WHERE [KEY]='" & IDKEY & "'"
			End If

			
		ElseIf Status="98" Then	'Screen Out
			SQL = "UPDATE " & TB_NAME & " SET Status=" & Status & ",EndDate=getDate() WHERE [KEY]='" & IDKEY & "';"

			If TB_NAME1<>"" Then
				SQL = SQL & "UPDATE " & TB_NAME1 & " SET Status=" & Status & ",EndDate=getDate() WHERE [KEY]='" & IDKEY & "'"
			End If

			If TB_NAME2<>"" Then
				SQL = SQL & "UPDATE " & TB_NAME2 & " SET Status=" & Status & ",EndDate=getDate() WHERE [KEY]='" & IDKEY & "'"
			End If
			
		Else
			SQL = "UPDATE " & TB_NAME & " SET Status=" & Status & ",EndDate=getDate() WHERE [KEY]='" & IDKEY & "';"	

			If TB_NAME1<>"" Then
				SQL = SQL & "UPDATE " & TB_NAME1 & " SET Status=" & Status & ",EndDate=getDate() WHERE [KEY]='" & IDKEY & "'"
			End If

			If TB_NAME2<>"" Then
				SQL = SQL & "UPDATE " & TB_NAME2 & " SET Status=" & Status & ",EndDate=getDate() WHERE [KEY]='" & IDKEY & "'"
			End If
			
		End If
		
		ExecuteSQL(SQL)
				
	End Function
	
	'###########################################################################################
	'값 찾아서 가져오기
	'###########################################################################################			

	Function getDValue(fieldNm)
		
		Dim TB,SQL
		Dim rsField
	
		If checkField(fieldNm,TB_NAME) Then
			TB=TB_NAME
		ElseIf checkField(fieldNm,TB_NAME1) Then
			TB=TB_NAME1	 
		ElseIf checkField(fieldNm,TB_NAME2) Then
			TB=TB_NAME2	 	
		End If
	 	
		DBConnect()
		Set rsField = Server.CreateObject("ADODB.Recordset")
		SQL = "select " & fieldNm & " from " & TB & " WHERE [KEY]='" & Request.Form("IDKEY") & "'"
	 
		RsField.Open SQL,DBConn,3,1,1
	  
		If RsField.Eof or RsField.Bof Then
			getDValue=""
		else	 
			getDValue = rsField(0)
		End if
	 
		RsField.Close
		DBClose()

	End Function

	'###########################################################################################
	'값 찾아서 가져오기
	'###########################################################################################			

	Function getIDValue(fieldNm,ID_KEY)
		
		Dim TB,SQL
		Dim rsField	
		
		DBConnect()
		Set rsField = Server.CreateObject("ADODB.Recordset")
		SQL = "select " & fieldNm & " from " & TB_NAME & " WHERE [KEY]='" & ID_KEY & "'"
	 
		RsField.Open SQL,DBConn,3,1,1
	  
		If RsField.Eof or RsField.Bof Then
			getIDValue=""
		else	 
			getIDValue = rsField(0)
		End if
	 
		RsField.Close
		DBClose()

	End Function

	'###########################################################################################
	'설문 DB SQL 쿼리 실행
	'###########################################################################################			
	Function ExecuteSQL(SQL)
		
		If SQL="" then
			ExecuteSQL = true
			Exit Function
		end If
		
		ExecuteSQL=TExecuteSQL(SQL)
			
	End Function

	'###########################################################################################
	'설문 DB SQL 쿼리 실행
	'###########################################################################################			
	Function TExecuteSQL(SQL)
		
		If SQL="" then
			ExecuteSQL = true
			Exit Function
		end If

		If SURVEYSTATUS<>"3" Then
			On Error Resume Next
		End IF
		
		DBConnect()
		DBConn.BeginTrans
'		Response.write SQL & "<BR>"
		DBConn.Execute SQL

		If DBConn.Errors.Count <> 0 Then
			TExecuteSQL = False
			DBConn.RollbackTrans
			DBClose()
		Else
			TExecuteSQL = True
			DBConn.CommitTrans
			DBClose()
		End IF
				
	End Function
	
	'###########################################################################################
	'Table내의 필드이름 확인
	'###########################################################################################				
	Function checkField(fieldNm,tableNm)		
		
		Dim Tvars,SQL
		Dim rsField
			 
		Tvars = replace(tableNm,"[","")
		Tvars = replace(Tvars,"]","")
 		
		DBConnect()
		Set rsField = Server.CreateObject("ADODB.Recordset")
		SQL = "select column_name from INFORMATION_SCHEMA.COLUMNS where table_name = '" & Trim(Tvars) &"' and column_name='" & trim(fieldNm) & "'"
	 
		RsField.Open SQL,DBConn,3,1,1
	  
		If RsField.Eof or RsField.Bof Then
			checkField=false
		else
			checkField=true
		End if
	 
		RsField.Close
		DBClose()
	End Function	

	'###########################################################################################
	'쿼리 결과를 리턴
	'###########################################################################################
	Public Function getRecordset(ByVal SQL,ByRef RecordSet)
		If DBFlag=False Then
			DBConnect()
		End If
			
		Set Recordset = Server.CreateObject("ADODB.Recordset")

		Recordset.Open SQL,DBConn,3,1,1
		
		getRecordset = Recordset	
		
	End Function
	
	'###########################################################################################
	'쿼리 결과를 리턴 2 스트링 형태로 리턴
	'Row는 vbCR 로 Split 하여 사용하고 Col은 vbTab으로 Split 하여 사용
	'단순 리스트 조회의 경우 사용 권장
	'###########################################################################################
	Public Function getRecordString(ByVal SQL)
		Dim Recordset

		If DBFlag=False Then
			DBConnect()
		End If

		Set Recordset = Server.CreateObject("ADODB.Recordset")
		Recordset.Open SQL,DBConn,3,1,1

'		Response.Write SQL
		
		getRecordString = Recordset.GetString(2)
		
		Recordset.Close
		Set Recordset = Nothing
		DBClose()
	End Function
	
	'###########################################################################################
	'쿼리 결과를 리턴 2 배열 형태로 리턴
	'단순 리스트 조회의 경우 사용 권장
	'###########################################################################################
	Public Function getRecordArray(ByVal SQL)
		Dim Recordset

		If DBFlag=False Then
			DBConnect()
		End If
		
		Set Recordset = Server.CreateObject("ADODB.Recordset")
		Recordset.Open SQL,DBConn,3,1,1

		If Recordset.Eof Or Recordset.Bof Then		
			getRecordArray = null
		Else
			getRecordArray = Recordset.getRows
		End If
		
		Recordset.Close
		Set Recordset = Nothing
		DBClose()
	End Function


	'###########################################################################################
	'DataBase에 저장하는 함수
	'이후로 모든 DataBase 관련은 아래의 함수를 통해서만 저장한다.
	'Survey_Flag 가 True이면 처음 저장하는 경우 False 는 두번째 저장하는 경우
	'DataSTR은 SQ1=1&SQ1_1=2 이런식으로 "&" 로 데이터를 저장한다.
	'###########################################################################################
	Function SaveDB(Survey_Flag,DataSTR)

		Dim VSTR,DSTR,TSTR
		Dim iCnt,cCnt

		If DataSTR="" Then
			SaveDB = False
		Else
			SaveDB = False
			VSTR = Split(DataSTR & DB2STR,DB2STR)
			DSTR = ""

			If Survey_FLAG=true Then	'최초 입력
				Dim DSTR1,DSTR2
				DSTR = "INSERT INTO " & TB_NAME & " ("

				For iCnt=0 To UBound(VSTR)
					If VarType(VSTR(iCnt))<>0 And VarType(VSTR(iCnt))<>1 And VSTR(iCnt)<>"" Then
'						Response.Write VSTR(iCnt)
						TSTR = Split(VSTR(iCnt),DB1STR)
						DSTR1 = DSTR1 & "[" & TSTR(0) & "],"
						DSTR2 = DSTR2 & "N'" & checkValue(TSTR(1)) & "',"
					End IF
				Next

				DSTR1 = Left(DSTR1,Len(DSTR1)-1)
				DSTR2 = Left(DSTR2,Len(DSTR2)-1)

				DSTR = DSTR & DSTR1 & ",[VC_ID],[Key],[Status],[StartDate],[Page],[IPName],[ResType]) VALUES ("
				DSTR = DSTR & DSTR2 & ",'" & Request.Form("VC_ID") &"','" & Request.Form("IDKEY") & "',1,getDate(),'" & NEXTPAGE & "','" & Request.ServerVariables("REMOTE_ADDR") & "','" & RESTYPE & "')"

				If TB_NAME1<>"" Then
					DSTR = DSTR & "INSERT INTO " & TB_NAME1 & " ([VC_ID],[Key],[Status],[StartDate],[IPName],[ResType]) VALUES ('" & Request.Form("VC_ID") &"','" & IDKey & "',1,getDate(),'" & Request.ServerVariables("REMOTE_ADDR") & "','" & RESTYPE & "')"
				End If

				If TB_NAME2<>"" Then
					DSTR = DSTR & "INSERT INTO " & TB_NAME2 & " ([VC_ID],[Key],[Status],[StartDate],[IPName],[ResType]) VALUES ('" & Request.Form("VC_ID") &"','" & IDKey & "',1,getDate(),'" & Request.ServerVariables("REMOTE_ADDR") & "','" & RESTYPE & "')"
				End If

			Else	'두번째 입력
'				DSTR = "UPDATE " & TB_NAME & " SET [Page]='" & NEXTPAGE & "',"

				If VarType(VSTR(0))<>0 And VarType(VSTR(0))<>1 And VSTR(0)<>"" Then
					TSTR = Split(VSTR(0),DB1STR)
				End If

				If checkField(TSTR(0),TB_NAME) Then
					DSTR = "UPDATE " & TB_NAME & " SET "
				ElseIf checkField(TSTR(0),TB_NAME1) Then
					DSTR = "UPDATE " & TB_NAME1 & " SET "				
				ElseIf checkField(TSTR(0),TB_NAME2) Then
					DSTR = "UPDATE " & TB_NAME2 & " SET "				
				End If

				For iCnt=0 To UBound(VSTR)
					If VarType(VSTR(iCnt))<>0 And VarType(VSTR(iCnt))<>1 And VSTR(iCnt)<>"" Then
						TSTR = Split(VSTR(iCnt),DB1STR)
						DSTR = DSTR & "[" & TSTR(0) & "]=N'" & checkValue(TSTR(1)) & "',"
					End IF
				Next
				
				DSTR = Left(DSTR,Len(DSTR)-1) & " WHERE [KEY]='" & Request.Form("IDKEY") & "'"
			End If

			If DSTR<>"" Then
				Call WriteFileLog(Server.MapPath(LogFolder & "/" & Request.Form("IDKEY") & ".qry"),DSTR,Survey_FLAG)
				SaveDB=ExecuteSQL(DSTR)
			End If
		End If
		
	End Function

	Function SavePage(PAGENAME)
		SaveJSON CDat("PAGENAME=" & PAGENAME)

		Dim PSQL
		PSQL = "UPDATE " & TB_NAME & " SET [PAGE]='" & PAGENAME & "' WHERE [KEY]='" & Request.Form("IDKEY") & "'"

		If TB_NAME1<>"" Then
			Dim PSQL1
			PSQL1 = "UPDATE " & TB_NAME1 & " SET [PAGE]='" & PAGENAME & "' WHERE [KEY]='" & Request.Form("IDKEY") & "'"
		End If

		If TB_NAME2<>"" Then
			Dim PSQL2
			PSQL2 = "UPDATE " & TB_NAME2 & " SET [PAGE]='" & PAGENAME & "' WHERE [KEY]='" & Request.Form("IDKEY") & "'"
		End If
		
		ExecuteSQL (PSQL)

		If TB_NAME1<>"" Then
			ExecuteSQL (PSQL1)
		End if

		If TB_NAME2<>"" Then
			ExecuteSQL (PSQL2)
		End if

	End Function

	'###########################################################################################
	'XML에 저장하는 함수
	'DataSTR은 SQ1=1&SQ1_1=2 이런식으로 "&" 로 데이터를 저장한다.
	'###########################################################################################
	Function SaveXML(DataSTR)

		Dim objXML
		Set objXML = New XMLClass

		If SURVEYSTATUS<>"3" Then
			On Error Resume Next
		End IF

		Dim VSTR,TSTR
		Dim iCnt

		If FileChecker(XMLFolder) Then
			objXML.Load XMLFolder
		End If

		If DataSTR="" Then
			SaveXML = False
		Else
			VSTR = Split(DataSTR & DB2STR,DB2STR)

			For iCnt=0 To UBound(VSTR)
				If VarType(VSTR(iCnt))<>0 And VarType(VSTR(iCnt))<>1 And VSTR(iCnt)<>"" Then
					TSTR = Split(VSTR(iCnt),DB1STR)
					objXML.SetValue TSTR(0),checkValue(TSTR(1))
				End IF
			Next

			objXML.SetValue "PAGE",PAGENAME
			
			objXML.Save XMLFolder
			SaveXML = True
		End IF
		
		Set objXML = Nothing

		If Err.Number>0 Then
			SaveXML = False
		End IF

	End Function

	'###########################################################################################
	'json에 저장하는 함수
	'DataSTR은 SQ1=1&SQ1_1=2 이런식으로 "&" 로 데이터를 저장한다.
	'###########################################################################################
	Function SaveJSON(DataSTR)
		Dim VSTR,TSTR
		Dim iCnt
		Dim dataJSON
		Set dataJSON = New aspJSON

		'On Error Resume Next

		If FileChecker(DAT_PATH) Then
			dataJSON.loadJSON(ReadUTF_File(DAT_PATH))
		End If

		If DataSTR="" Then
			SAVEJSON = False
		Else
			VSTR = Split(DataSTR & DB2STR,DB2STR)

			For iCnt=0 To UBound(VSTR)
				If VarType(VSTR(iCnt))<>0 And VarType(VSTR(iCnt))<>1 And VSTR(iCnt)<>"" Then
					TSTR = Split(VSTR(iCnt),DB1STR)
					dataJSON.data(TSTR(0)) = CStr(checkValue(TSTR(1)))					
				End IF
			Next
			dataJSON.data("PAGE")=PAGENAME

			WriteUTF_File DAT_PATH,dataJSON.JSONoutput()
			
			SAVEJSON = True
		End IF
		
		Set dataJSON = Nothing

		If Err.Number>0 Then
			SAVEJSON = False
		End IF
		
	End Function

	Function SAVEData(DataSTR)
		Dim Flag
		Dim ERRMSG

		Flag = True

		If SURVEYSTATUS<>"3" Then
		On Error Resume Next
		End IF

		Flag = SaveDB(Survey_Flag,DataSTR)

		If Flag = False Or Err.Number>0 Then
			ERRMSG = ""
			ERRMSG = ERRMSG & " RESPONSE ID : " & IDKey & VBCRLF
			ERRMSG = ERRMSG & " ERROR PAGE : " & PAGENAME & VBCRLF
			ERRMSG = ERRMSG & " ERROR CODE : " & Err.Number & VBCRLF			
			ERRMSG = ERRMSG & " DESCRIPTION : " & Err.Description & VBCRLF
			ERRMSG = ERRMSG & " SOURCE : " & Err.Source & VBCRLF
			ERRMSG = ERRMSG & " ERROR DATE : " & Now() & VBCRLF
			
			If FileChecker(Server.MapPath(ERR_PATH))=False Then
				Call WriteFileLog(Server.MapPath(ERR_PATH,ERRMSG,False))
			Else
				Call WriteFileLog(Server.MapPath(ERR_PATH,ERRMSG,True))
			End If

			Err.Raise 1,"사용자 지정오류","DB 저장 실패"
			ErrorMail Err
			Server.Transfer "Survey_Error.asp"
		Else
			Flag = True
		End If

'		Flag = SaveXML(DataSTR)

'		If Flag = False Or Err.Number>0 Then
'			Err.Raise 1,"사용자 지정오류","XML 저장 실패"
'			ErrorMail Err
'			Server.Transfer "Survey_Error.asp"
'		End If

		Flag = SaveJSON(DataSTR)

		If Flag = False Or Err.Number>0 Then
'			Err.Raise 1,"사용자 지정오류","JSON 저장 실패"
'			ErrorMail Err
'			Server.Transfer "Survey_Error.asp"
		End If

		SAVEData=Flag

	End Function

	'###########################################################################################
	'LIST STATUS UPDATE
	'###########################################################################################
		Function getListStatus(ListStatus)
			Dim ListSQL

			ListSQL = "UPDATE " & LI_NAME & " SET [STATUS] = "& ListStatus &" where "& LISTKEY &"='" & Request.Form("IDKEY") & "'"
			ExecuteSQL(ListSQL)
		End Function

		Function getListStatus1(ListStatus)
			Dim ListSQL

			ListSQL = "UPDATE " & LI_NAME & " SET [STATUS] = "& ListStatus &" where "& LISTKEY &"='" & Request.Form("IDKEY") & "'"
			ExecuteSQL(ListSQL)
		End Function


	'###########################################################################################
	'로그파일 읽어오기
	'###########################################################################################

		 Function LoadStream(FilePath)
		  Dim oFSO,wFile
		  'Set oFSO = Server.CreateObject("Scripting.FileSystemObject")
		 Set oFSO = Server.CreateObject("ADODB.Stream")

'		 Response.Write FilePath

		 oFSO.CharSet = "utf-8"
		 oFSO.Mode = 3
		 oFSO.Type = 2
		 oFSO.Open
		 oFSO.LoadFromFile FilePath
		 LoadStream = oFSO.ReadText

		 oFSO.Close
		 Set oFSO = Nothing

		 End Function 

	'###########################################################################################
	'Quota / Screen Out Page Save
	'###########################################################################################

		Function QuotaPageSave(OutPage)

		Dim OutSQL
			OutSQL = "UPDATE " & TB_NAME & " SET [OUTPAGE] = '"& OutPage &"' where [KEY]='" & Request.Form("IDKEY") & "'"
			ExecuteSQL(OutSQL)

		End Function

	'###########################################################################################
	'패널대여 / 실사대여 KEY 확인.
	'###########################################################################################

		Function PANEL_EXPORT_COMPLETE()

			Dim PArr(2)
			PArr(0) = ""
			PArr(1) = ""

			RsConnect()
			Dim SQL,RS

			Set RS = Server.CreateObject("ADODB.Recordset")
			SQL = "SELECT * FROM [T_PANEL_EXPORT] Where [PROJECT_CODE]='"& PROJECTCODE &"'"
			Rs.Open SQL,RSConn,3,1,1

			If Rs.Eof Or Rs.Bof Then
				PArr(0) = ""
				PArr(1) = ""
			Else
				Do While Not Rs.Eof
					If Request(Rs("VC_OUT_ID"))<>"" Then
						PArr(0) = getCompany(Rs("VC_COMPANY"))		'RESTYPE
						PArr(1) = Request(Rs("VC_OUT_ID"))			'KEY
						Exit do
					End if
				Rs.MoveNext
				Loop				
			End If
			
			PANEL_EXPORT_COMPLETE = PArr
			 
			Rs.Close
			RsConClose()

		End Function

		Function PANEL_EXPORT_URL()

			Dim PArr(5)
			PArr(0) = ""
			PArr(1) = ""
			PArr(2) = ""
			PArr(3) = ""
			PArr(4) = ""

			Dim oFSO
			Set oFSO = Server.CreateObject("Scripting.FileSystemObject") 

			Dim EXPORT_DOM
			Set EXPORT_DOM = new XMLClass

			EXPORT_DOM.LoadHTTP("http://admin.surveymonster.kr/Project/NewProject/XML/" & PROJECTCODE & "_" & Request("RESTYPE") & ".xml")

			PArr(0) = EXPORT_DOM.TAGTEXT("VC_OUT_ID",0)
			PArr(1) = EXPORT_DOM.TAGTEXT("VC_OUTURL_COMPLETE",0)
			PArr(2) = EXPORT_DOM.TAGTEXT("VC_OUTURL_SCREEN",0)
			PArr(3) = EXPORT_DOM.TAGTEXT("VC_OUTURL_QUOTA",0)

			EXPORT_DOM.LoadHTTP("http://admin.surveymonster.kr/Project/NewProject/XML/" & PROJECTCODE & "_SENDER.xml")
			PArr(4) = EXPORT_DOM.TAGTEXT("VC_OUTURL_SURVEY",0)
				
			PANEL_EXPORT_URL = PArr

		End Function

		Function getCompany(CompanyCode)

			Dim CMSQL,CMRs
			CMSQL = "Select * From [T_OUTSOURCING] Where bn_index="&CompanyCode

			RsConnect()
			Set CMRs = Server.CreateObject("ADODB.Recordset")
			CMRs.Open CMSQL,RSConn,3,1,1

			getCompany = CMRs(1)

			CMRs.Close
			RsConClose()

		End Function

		Function getCompanyCode(CompanyName)

			Dim CMSQL,CMRs
			CMSQL = "Select * From [T_OUTSOURCING] Where vc_company='"&CompanyName & "'"

			RsConnect()
			Set CMRs = Server.CreateObject("ADODB.Recordset")
			CMRs.Open CMSQL,RSConn,3,1,1

			getCompanyCode = CMRs(0)

			CMRs.Close
			RsConClose()

		End Function


	'###########################################################################################
	'전화번호 가져오기
	'###########################################################################################				
	

	Function getTel_Number()
		Dim RsQuota,Rs1,Rs2,SQLQuota,SQLTel,SQLData,SQLUsed
		Dim Qvar,i,j,flagk

		If Trim(Quota_Variables)<>"" Then					

			SQLQuota = "SELECT * FROM " & Quota_TBNAME & " Order By NewID() ASC"
			
			Set RsQuota = Server.CreateObject("Adodb.Recordset")
			
			RsQuota.Open SQLQuota,DBConn,3,1,1
			
			If RsQuota.Eof or RsQuota.Bof Then	'조사대상 없음
				%>
				<script language="javascript">
					alert("조사 대상 리스트가 없습니다.\n관리자에게 문의하세요");
					window.close();
				</script>
				<%
				RsQuota.Close
				Set RsQuota = Nothing
				Response.End
			End IF

			For j=0 to RsQuota.RecordCount -1
			
				Set Rs1 = Server.CreateObject("Adodb.Recordset")
				SQLData = "SELECT Count(IDX) From " & TB_NAME & " WHERE Status=10"
				
				flagk=0
				
				If Trim(Quota_Variables)="" Then
					SQLData=SQLData & ""
					flagk=0
				ElseIf Instr(Quota_Variables,",")>0 Then
					QVar = Split(Quota_Variables,",")
				
					For i=0 to UBound(Qvar)
						If Trim(QVar(i))<>"" Then
							SQLData= SQLData & " AND " & QVar(i) & "='" & RsQuota(QVar(i)) & "'"
						End If
					Next
					
					flagk=2
				ElseIf Instr(Quota_Variables,",")<1 Then
					SQLData= SQLData & " AND " & Quota_Variables & "='" & RsQuota(Quota_Variables) & "'"
					flagk=1
				End If
			
				Rs1.Open SQLData,DBConn,3,1,1
				
				If RsQuota("Limit")> Rs1(0) Then
					Exit For
				End If			
				Rs1.Close
				
				RsQuota.Movenext
			Next			
		End IF
		
		'쿼터의 갯수(0:없음, 1:하나, 2:두개이상)
		Select Case flagk
		
			Case 0
				SQLTel = "SELECT Top 1 * From " & LI_NAME & " WHERE Status=0 AND UseDate is null AND [Int] is Null ORDER by newid()" 
			Case 1
				SQLTel = "SELECT Top 1 * From " & LI_NAME & " WHERE Status=0 AND UseDate is null AND [Int] is Null " 

				if Not RsQuota.Eof AND Not RsQuota.Bof Then
					'쿼터변수확인
					If Trim(Quota_Variables)<>"" Then
						SQLTel = SQLTel & " AND [DongCode]='" & RsQuota(Quota_Variables) & "'"
					End If
					
					SQLTel = SQLTel & " ORDER by newid()"
				Else
					%>
					<script language="javascript">
						alert('설문이 완료되었습니다.');
						window.close();
					</script>
					<%
					RsQuota.Close				
					Response.End
				End if
			Case 2
				SQLTel = "SELECT Top 1 * From " & LI_NAME & " WHERE Status=0 AND UseDate is null AND [Int] is Null "
				
				if Not RsQuota.Eof AND Not RsQuota.Bof Then
					'쿼터변수확인
					If Trim(QVar(0))<>"" Then
						SQLTel = SQLTel & " AND [DongCode]='" & RsQuota(QVar(0)) & "'"
					End If
					If Trim(QVar(1))<>"" Then
						SQLTel = SQLTel & " AND [GuBun]='" & RsQuota(QVar(1)) & "'"
					End If
					If Trim(QVar(2))<>"" Then
						SQLTel = SQLTel & " AND [Age]='" & RsQuota(QVar(2)) & "'"
					End If
					
					SQLTel = SQLTel & " ORDER by newid()"				
				Else
					%>
					<script language="javascript">
						alert('설문이 완료되었습니다.');
						window.close();
					</script>
					<%
					RsQuota.Close				
					Response.End
				End if
								
		End Select

		Set Rs2 = Server.CreateObject("ADODB.Recordset")
		
		Rs2.Open SQLTel,DBConn,3,1,1		
		
		If Rs2.Eof or Rs2.Bof Then
			%>
			<script language="javascript">
				alert('리스트 전화번호가 없습니다.\n관리자에게 말씀해 주십시오');
				window.close();
			</script>
			<%
			RsQuota.Close
			Rs1.Close
			Rs2.Close
		
			Set RsQuota = Nothing

			Set Rs1 = Nothing
			Set Rs2 = Nothing
			Response.End
		Else

			'Cookies에 값 저장하기<필요한 항목들은 추가로 정리하여 넣기>
			'기본 부분
			Response.Cookies("MemberCode") = Trim(Rs2("IDX"))
			Response.Cookies("HP") = Trim(Rs2("HP"))
			Response.Cookies("TEL") = Trim(Rs2("TEL"))		

			'추가부분 <리스트 검토후 내용 저장하기>
			Response.Cookies("Name") = Trim(Rs2("당원명"))
			Response.Cookies("Dong") = Trim(Rs2("행정동구분"))
			Response.Cookies("DongCode") = Trim(Rs2("DongCode"))
			Response.Cookies("Bir") = Trim(Rs2("생년월일"))
			Response.Cookies("Gubun")=Trim(Rs2("Gubun"))
			Response.Cookies("Sex")=Trim(Rs2("Sex"))
				
			SQLUsed = "UPDATE " & LI_NAME & " Set Status=1,UseDate=getDate(),[Int]=" & Request.Cookies("MemberID") & " WHERE IDX='" & Rs2("IDX") & "'"
			ExecuteSQL SQLUsed

		End IF
		
	End Function


%>