<!--#include file="../Lib/Config.asp"-->
<!--#include file="../Lib/Func.asp"-->
<!--#include file="../Lib/FileLib.asp"-->
<!--#include file="../Lib/DBConn.asp"-->
<!--#include file="../Lib/AN_Header.asp"-->

<%
Dim HTMLSCRIPT
Dim JSCRIPT
Dim CODES,QUESTION
Dim ATT
Dim VARS,VAR
Dim PAGE,NPAGE
Dim IDKEY,RESTYPE,VC_ID
Dim SKINSTR,PARTSTR
Dim DELAYTIMES
Dim ST
Dim SPSS

Dim V_GENDER,V_AGE,V_AREA
Dim L_GENDER,L_AGE,L_AREA

'=================================================================
'변수명 정보 설정
'=================================================================
L_GENDER = "SQ3"
L_AGE = "SQ2A"
L_AREA = "SQ1A"

'=================================================================
'변수코드 정보 설정
'=================================================================
V_GENDER = "1@남자##2@여자"
V_AGE = "1@19~29세##2@30대##3@40대##4@50대##5@60세 이상"
V_AREA = "1@제1선거구(군포1동, 군포2동, 대야동, 산본1동, 금정동)##2@제2선거구(산본2동, 궁내동, 광정동, 재궁동, 오금동, 수리동)"

'=================================================================
'성별 연령 설문 정보
'=================================================================
Function getQGA(Gender,Age)

	Dim SQL,Rs

	SQL = "SELECT Count(IDX) FROM [Dat_"&PROJECTCODE&"] WHERE ["&L_GENDER&"]='"&Gender&"' and ["&L_AGE&"]='"&Age&"' and Status=10"	
	Set Rs = Server.CreateObject("ADODB.Recordset")

	DBConnect()
	Rs.Open SQL,DBConn,3,1,1

	If Rs.Bof Or Rs.eof Then
		getQGA = 0
	Else
		getQGA = Rs(0)
	End if
	
	Rs.Close
	Set Rs = Nothing
	DBClose()

End Function

'=================================================================
'지역 연령 설문 정보
'=================================================================
Function getQAA(Area,Age)

	Dim SQL,Rs

	SQL = "SELECT Count(IDX) FROM [Dat_"&PROJECTCODE&"] WHERE ["&L_Area&"]='"&Area&"' and ["&L_AGE&"]='"&Age&"' and Status=10"	
	Set Rs = Server.CreateObject("ADODB.Recordset")

	DBConnect()
	Rs.Open SQL,DBConn,3,1,1

	If Rs.Bof Or Rs.eof Then
		getQAA = 0
	Else
		getQAA = Rs(0)
	End if
	
	Rs.Close
	Set Rs = Nothing
	DBClose()

End Function

'=================================================================
'지역 성별 설문 정보
'=================================================================
Function getQAG(Area,Gender)

	Dim SQL,Rs

	SQL = "SELECT Count(IDX) FROM [Dat_"&PROJECTCODE&"] WHERE ["&L_Area&"]='"&Area&"' and ["&L_GENDER&"]='"&Gender&"' and Status=10"	
	Set Rs = Server.CreateObject("ADODB.Recordset")

	DBConnect()
	Rs.Open SQL,DBConn,3,1,1

	If Rs.Bof Or Rs.eof Then
		getQAG = 0
	Else
		getQAG = Rs(0)
	End if
	
	Rs.Close
	Set Rs = Nothing
	DBClose()

End Function

'=================================================================
'성별 연령 할당 정보
'=================================================================
Function getLGA(Gender,Age)

	Dim SQL,Rs

	SQL = "SELECT Sum(Limit) FROM [Q_"&PROJECTCODE&"] WHERE ["&L_GENDER&"]='"&Gender&"' and ["&L_AGE&"]='"&Age&"'"	
	Set Rs = Server.CreateObject("ADODB.Recordset")

	DBConnect()
	Rs.Open SQL,DBConn,3,1,1

	If Rs.Bof Or Rs.eof Then
		getLGA = 0
	Else
		getLGA = Rs(0)
	End if
	
	Rs.Close
	Set Rs = Nothing
	DBClose()

End Function

'=================================================================
'지역 연령 할당 정보
'=================================================================
Function getLAA(Area,Age)

	Dim SQL,Rs

	SQL = "SELECT Sum(Limit) FROM [Q_"&PROJECTCODE&"] WHERE ["&L_Area&"]='"&Area&"' and ["&L_AGE&"]='"&Age&"'"	
	Set Rs = Server.CreateObject("ADODB.Recordset")

	DBConnect()
	Rs.Open SQL,DBConn,3,1,1

	If Rs.Bof Or Rs.eof Then
		getLAA = 0
	Else
		getLAA = Rs(0)
	End if
	
	Rs.Close
	Set Rs = Nothing
	DBClose()

End Function

'=================================================================
'지역 성별 할당 정보
'=================================================================
Function getLAG(Area,Gender)

	Dim SQL,Rs

	SQL = "SELECT Sum(Limit) FROM [Q_"&PROJECTCODE&"] WHERE ["&L_Area&"]='"&Area&"' and ["&L_Gender&"]='"&Gender&"'"	
	Set Rs = Server.CreateObject("ADODB.Recordset")

	DBConnect()
	Rs.Open SQL,DBConn,3,1,1

	If Rs.Bof Or Rs.eof Then
		getLAG = 0
	Else
		getLAG = Rs(0)
	End if
	
	Rs.Close
	Set Rs = Nothing
	DBClose()

End Function

'=================================================================
'변수 1개만 설정하여 쿼터나 응답 데이터 개수 가져오기
'FieldV : 필드명 (변수명)
'CodeV : 코드값
'TypeV : 쿼터냐 데이터냐 1 : 데이터 2: 쿼터
'=================================================================
Function Get1V(FieldV,CodeV,TypeV)

	Dim SQL,Rs,Field_V,TB_V,Dat_V

	Select Case TypeV
		Case "1"
			TB_V = "[Dat_" & PROJECTCODE & "]"
			Dat_V = "Count(IDX)"
			SQL = "SELECT "&Dat_V&" FROM "&TB_V&" WHERE ["&FieldV&"]='"&CodeV&"' and Status=10"
		Case "2"
			TB_V = "[Q_" & PROJECTCODE & "]"
			Dat_V = "Sum(Limit)"
			SQL = "SELECT "&Dat_V&" FROM "&TB_V&" WHERE ["&FieldV&"]='"&CodeV&"'"
	End Select

	Set Rs = Server.CreateObject("ADODB.Recordset")

	DBConnect()
	Rs.Open SQL,DBConn,3,1,1

	If Rs.Bof Or Rs.eof Then
		Get1V = 0
	Else
		Get1V = Rs(0)
	End if
	
	Rs.Close
	Set Rs = Nothing
	DBClose()
	
End Function

'=================================================================
'전체 합계 가져오기
'TypeV : 쿼터냐 데이터냐 1 : 데이터 2: 쿼터
'=================================================================
Function TotalV(TypeV)

	Dim SQL,Rs,Field_V,TB_V,Dat_V

	Select Case TypeV
		Case "1"
			TB_V = "[Dat_" & PROJECTCODE & "]"
			Dat_V = "Count(IDX)"
			SQL = "SELECT "&Dat_V&" FROM "&TB_V&" WHERE Status=10"
		Case "2"
			TB_V = "[Q_" & PROJECTCODE & "]"
			Dat_V = "Sum(Limit)"
			SQL = "SELECT "&Dat_V&" FROM "&TB_V&" "
	End Select
	

	Set Rs = Server.CreateObject("ADODB.Recordset")

	DBConnect()
	Rs.Open SQL,DBConn,3,1,1

	If Rs.Bof Or Rs.eof Then
		TotalV = 0
	Else
		TotalV = Rs(0)
	End if
	
	Rs.Close
	Set Rs = Nothing
	DBClose()

End Function

'=================================================================
'응답 데이터 가져오기
'AreaV : 지역 변수
'GenderV : 성별 변수
'AgeV : 연령 변수
'=================================================================
Function getQuota(AreaV,GenderV,AgeV)

	Dim SQL,Rs

	SQL = "SELECT Count(IDX) FROM [Dat_"&PROJECTCODE&"] WHERE ["&L_AREA&"]='"&AreaV&"' and ["&L_AGE&"]='"&AgeV&"' and ["&L_GENDER&"]='"&GenderV&"' and Status=10"
	Set Rs = Server.CreateObject("ADODB.Recordset")

	DBConnect()
	Rs.Open SQL,DBConn,3,1,1

	If Rs.Bof Or Rs.eof Then
		getQuota = 0
	Else
		getQuota = Rs(0)
	End if
	
	Rs.Close
	Set Rs = Nothing
	DBClose()

End Function
'=================================================================
'할당 데이터 가져오기
'AreaV : 지역 변수
'GenderV : 성별 변수
'AgeV : 연령 변수
'=================================================================
Function getLimit(AreaV,GenderV,AgeV)

	Dim SQL,Rs

	SQL = "SELECT Sum(Limit) FROM [Q_"&PROJECTCODE&"] WHERE ["&L_AREA&"]='"&AreaV&"' and ["&L_AGE&"]='"&AgeV&"' and ["&L_GENDER&"]='"&GenderV&"'"

	Set Rs = Server.CreateObject("ADODB.Recordset")

	DBConnect()
	Rs.Open SQL,DBConn,3,1,1

	If Rs.Bof Or Rs.eof Then
		getLimit = 0
	Else
		getLimit = Rs(0)
	End if
	
	Rs.Close
	Set Rs = Nothing
	DBClose()

End Function

%>
<script type="text/javascript" language="javascript">
<!--
<% Server.Execute("../Lib/Prev.asp") %>

<% If SurveyStatus=0 Then %>
function prev()
{
	history.go(-2);
}
<% End If %>
function next()
{
<%
	Response.Write JSCRIPT
%>
	var obj;

	document.Survey.submit();
}

function fadeCk()
{
	$(".btnC").hide();
	$(".btnC").delay(<%=DELAYTIMES%>).fadeIn(400);
}

$(document).ready(function(){

	tcount=<%=DELAYTIMES/1000%>;
	fadeCk();
	fc();

           $("#prev").click(function(){
                     prev();
           });

           $("#next").click(function(){
                     next();
           });
});

function UpDate()
{
	document.Survey.action = "UpDate.asp";
	document.Survey.submit();

}


//-->
</script>

<style type="text/css">
.QTable th
{
	border:1px solid #ccc;
	background-color:#000;
	color:#fff;
}
.QTable td
{
	border:1px solid #ccc;
}
.ALeft
{
	color:#fff;
	background-color:#3333CC;
	border:1px solid #ccc;
	text-align:center;padding-left:10px;
}
.rkdwh
{
	font-weight:bold;
	color:#000fff;
}

	.KKang {Width:900px;padding:5px 5px 5px 5px;border-collapse:separate;}
	.KKang th
	{
		background-color:#EBFBFF;
		font-size:14px;
	}
	.KKang td
	{
		font-size:14px;
	}
	
</style>

<div style="width:100%;">
<form name="Survey" method="post" action="Survey.asp">
<!--#include file="../Lib/Variable.asp"-->
	<% 
	SKINSTR = LoadSkin("../SKIN/" & SKIN &"/Skin.asp")
	SKINSTR = Replace(SKINSTR,"##SURVEYTITLE##",SURVEYTITLE)
	SKINSTR = Replace(SKINSTR,"##CLIENTIMG##",CLIENTIMAGE)
	SKINSTR = Replace(SKINSTR,"##PARTSTR##",PARTSTR)
	If PARTSTR<>"" Then
	SKINSTR = Replace(SKINSTR,"<!--","")
	SKINSTR = Replace(SKINSTR,"//-->","")
	End if
	Response.Write SKINSTR
	%>
	<div class="surveyBox">

		<div id="stArea" style="text-align:center;margin-bottom:5px;">
		<table id="STBody" style="margin:0 auto; width:960px;">
		<tr>
			<td width="100%" style="text-align:left;font:14px/1.5 'Malgun Gothic',sans-serif;font-weight:bold;">
			<%=ST%>
			</td>
		</tr>
		</table>	
<% if ST<>"" Then %>
		<script language="javascript">
		$(document).ready(function(){		
			roundTable("STBody");
		});
		</script>
<% End if %>
		</div>

<!-- eun -->

<center>
		<%
		Dim AgeArr,tmpAr,CWidth
		Dim iCnt,cCnt
		AgeArr = Split(V_AGE,"##")

		CWidth = 85/(UBound(AgeArr)+3)
		%>
			<span style='font-weight;bold;font-size:16px;'><%=SURVEYTITLE%></span><br><br>
			<table class="KKang"  cellpadding="5" cellspacing="1" border="1" style="font-size:12px;">
				<colgroup>
					<col width="*">
					<col width="<%=CWidth%>%">
					<col width="<%=CWidth%>%">
					<%For iCnt=0 To UBound(AgeArr)%>
					<col width="<%=CWidth%>%">
					<%Next%>
				</colgroup>
				<tr>
					<th>행정구역명</th>
					<th>성별</th>
					<th>합계</th>
					<%For iCnt=0 To UBound(AgeArr)%>
					<%tmpAr = Split(AgeArr(iCnt),"@")%>
					<th><%=tmpAr(1)%></th>
					<%Next%>
				</tr>
					<!--전체 연령 Page//-->
					<tr>
						<th rowspan="3" style="background-color:#FAFAD2;text-align:center;">지역</th>	
						<th style="background-color:#FAFAD2;">계</th>
						<td style="background-color:#FAFAD2;"><%=TotalV("1")%>/<%=TotalV("2")%></td>
						<%For iCnt=0 To UBound(AgeArr)%>
						<%tmpAr = Split(AgeArr(iCnt),"@")%>
						<td style="background-color:#FAFAD2;"><%=Get1V(L_AGE,tmpAr(0),"1")%>/<%=Get1V(L_AGE,tmpAr(0),"2")%></td>
						<%Next%>
					</tr>
					<!--성별 연령 Page//-->

					<%Dim GenderArr,tmpArr
						GenderArr = Split(V_GENDER,"##")
						For iCnt=0 To UBound(GenderArr)
						tmpArr = Split(GenderArr(iCnt),"@")%>
					<tr>						
						<td><%=tmpArr(1)%></td>
						<td><%=Get1V(L_GENDER,tmpArr(0),"1")%>/<%=Get1V(L_GENDER,tmpArr(0),"2")%></td>

						<%For cCnt=0 To UBound(AgeArr)%>
						<%tmpAr = Split(AgeArr(cCnt),"@")%>
						<td><%=getQGA(tmpArr(0),tmpAr(0))%>/<%=getLGA(tmpArr(0),tmpAr(0))%></td>
						<%Next%>
					</tr>
					<%	Next%>
				<%
					Dim C1,C2,ii		
					ii=1
					Dim City,xx
					City = Split(V_AREA,"##")

					For xx=0 To Ubound(City)
						C1 = Split(City(xx),"@")(0)
						C2 = Split(City(xx),"@")(1)
						If xx<>0 Then
							ii=ii+1
						End If
					%>

					<tr>
						<th rowspan="3" style="background-color:#FAFAD2;text-align:center;"><%=C2%></th>	
						<th style="background-color:#FAFAD2;">계</th>
						<td style="background-color:#FAFAD2;"><%=get1V(L_AREA,C1,"1")%>/<%=get1V(L_AREA,C1,"2")%></td>
						<%For cCnt=0 To UBound(AgeArr)%>
						<%tmpAr = Split(AgeArr(cCnt),"@")%>
						<td style="background-color:#FAFAD2;"><%=getQAA(C1,tmpAr(0))%>/<%=getLAA(C1,tmpAr(0))%></td>
						<%Next%>
					</tr>
					<%
					For iCnt=0 To UBound(GenderArr)
					tmpArr = Split(GenderArr(iCnt),"@")

					%>
					<tr>						
						<td><%=tmpArr(1)%></td>
						<td><%=getQAG(C1,tmpArr(0))%>/<%=getLAG(C1,tmpArr(0))%></td>
						<%
							For cCnt=0 To UBound(AgeArr)
							tmpAr = Split(AgeArr(cCnt),"@")
						%>
						<td><%=getQuota(C1,tmpArr(0),tmpAr(0))%>/<input type="text" size="3" name="A_<%=ii%>" value="<%=getLimit(C1,tmpArr(0),tmpAr(0))%>"></td>
						<%
							If iCnt=uBound(GenderArr) And cCnt=UBound(AgeArr) Then
'								Response.Write"!" & "<BR>"
							Else
								ii=ii+1
							End IF
							Next
						%>
					</tr>
					<%
					Next
					%>
				<%
					Next
				%>

			</table>
			<br>
		<input type='button' name='B5' style='height:50px;font-size:15px;width:150px' value='목록으로' onclick='location.href="MAIN.asp"'>　　
		<input type='Button' name='Up' value='쿼터 수정하기' style='height:50px;font-size:15px' onclick='javascript:UpDate();'>
</center>

</form>
</div>
</body>
</html>