$( function() {
	
	$( "#selDistirct" ).change( function() {
		var sidoCode = window.document.getElementById( "selDistirct" ).value;
		var params = "method=getSggList&prmSidoCode="+ sidoCode;
		new ajax.xhr.Request( "status0101.do", params, setSelectBox, "GET", "selGu" );
	});
	
	$( "#selGu" ).change( function() {
		var sggCode = window.document.getElementById( "selGu" ).value;	
		var chartKind = window.document.getElementById( "selChartKind" ).value;
		var params = "method=getChartIndexList&prmChartKind="+ encodeURIComponent( chartKind ) +"&prmSggCode=" + sggCode;
		new ajax.xhr.Request( "status0101.do", params, setSelectBox, "GET", "selChartIndex" );
	});
	
	$( "#selChartKind" ).change( function() {
		var sggCode = window.document.getElementById( "selGu" ).value;	
		var chartKind = window.document.getElementById( "selChartKind" ).value;
		var params = "method=getChartIndexList&prmChartKind="+ encodeURIComponent( chartKind ) +"&prmSggCode=" + sggCode;
		new ajax.xhr.Request( "status0101.do", params, setSelectBox, "GET", "selChartIndex" );
	});
	
	$( "#selChartIndex" ).change( function() {		
		var index = new String ( window.document.getElementById( "selChartIndex" ).value );
		var arrIndex = index.split( "," );
		var indexCode, indexName;
		if( arrIndex.length == 2 ) {
			indexCode = arrIndex[0];
			indexName = arrIndex[1];
		}
		var sggCode = window.document.getElementById( "selGu" ).value;	
		var chartKind = window.document.getElementById( "selChartKind" ).value;
		var params = "method=getYear&prmIndexName="+ encodeURIComponent( indexName ) +"&prmSggCode=" + sggCode + "&prmChardKind=" + chartKind + "&prmIndexCode=" + indexCode;
		new ajax.xhr.Request( "status0101.do", params, setYears, "GET", null );
	});
	
	$( "#btnSubmit" ).click( function() {
		var index = window.document.getElementById( "selChartIndex" ).value;
		var arrIndex = index.split( "," );
		var indexCode, indexName;
		if( arrIndex.length == 2 ) {
			indexCode = arrIndex[0];
			indexName = arrIndex[1];
		}
		
		if( checkValidation() ) {
			window.document.forms[ "frmChartList" ].method.value = "getValueList"; 
			window.document.forms[ "frmChartList" ].strSidoCode.value = window.document.getElementById( "selDistirct" ).value;
			window.document.forms[ "frmChartList" ].strSggCode.value = window.document.getElementById( "selGu" ).value;
			window.document.forms[ "frmChartList" ].strTableName.value = window.document.getElementById( "selChartKind" ).value;
			window.document.forms[ "frmChartList" ].strIndexCode.value = indexCode;
			window.document.forms[ "frmChartList" ].strIndexName.value = indexName;
			window.document.forms[ "frmChartList" ].strStartYear.value = window.document.getElementById( "selStartYear" ).value;
			window.document.forms[ "frmChartList" ].strEndYear.value = window.document.getElementById( "selEndYear" ).value;
			window.document.forms[ "frmChartList" ].strChartName.value = window.document.getElementById( "selChartShape" ).value; 
			window.document.forms[ "frmChartList" ].target = "ifrmChart";
			window.document.forms[ "frmChartList" ].submit();
			
			window.document.getElementById( "divBefore" ).style.display = "none";
			window.document.getElementById( "imgBefore" ).src = "";
		}
	}).css( "cursor" ,"pointer" );
	
	$( "#btnExcel" ).click( function() {		
		window.document.forms[ "frmChartList" ].method.value = "chartExel"; 
		window.document.forms[ "frmChartList" ].target = "_blank";
		window.document.forms[ "frmChartList" ].submit();
	}).css( "cursor" ,"pointer" );
	
	$( "#btnPrint" ).click( function() {		
		var chk = window.open( 'about:blank','chartPop','toolbar=no,scrollbars=no,resizable=no,width=605,height=400,left=0,top=0' ); 
		window.document.forms[ "frmChartList" ].method.value = "getValueList"; 
		window.document.forms[ "frmChartList" ].target = "chartPop";
		window.document.forms[ "frmChartList" ].submit();
	}).css( "cursor" ,"pointer" );
	
});

function checkValidation() {
	var returnValue = true;
	
	// 시군구 값 확인
	var sggCode = window.document.getElementById( "selGu" ).value;
	if( sggCode == "" || sggCode == undefined ) {
		alert( unescape( "구 정보를 선택하세요." ) );
		return false;
	} else {
		if( !validateSpecialChar( sggCode, "()+" ) ) {
			alert( unescape( "잘못된 값이 들어왔습니다.\n알맞는 구 정보를 선택하세요." ) );
			return false;
		}
	}
	
	// 인덱스 이름 확인
	var index = window.document.getElementById( "selChartIndex" ).value;
	var arrIndex = index.split( "," );
	var indexCode, indexName;
	if( arrIndex.length == 2 ) {
		indexCode = arrIndex[0];
		indexName = arrIndex[1];
	}
	if( indexName == "" || indexName == undefined ) {
		alert( "검색하고자 하는 차트의 이름을 선택하세요." );
		return false;
	} else {
		if( !validateSpecialChar( indexName, "()+" ) ) {
			alert( "잘못된 값이 들어왔습니다.\n알맞는 차트의 이름을 선택하세요." );
			return false;
		}
	}
	
	// 연도확인
	var startYear = window.document.getElementById( "selStartYear" ).value;
	var endYear = window.document.getElementById( "selEndYear" ).value;
	
	if( startYear == "" || startYear == undefined || endYear == "" || endYear == undefined ) {
		alert( "연도를 선택하세요." );
		return false;
	} else {
		if( !validateNumber( startYear ) || !validateNumber( endYear ) ) {
			alert( "잘못된 값이 들어왔습니다.\n알맞는 연도를 선택하세요." );
			return false;
		} else {
			startYear = new Number( startYear );
			endYear = new Number( endYear );
			if( endYear < startYear ) {
				alert( "시작연도가 종료연도보다 큽니다.\n다시 선택하세요" );
				return false;
			}
		}
	}
	return returnValue;
}

function setSelectBox( req ) {
	
	if ( req.readyState == 4 ) {
		if ( req.status == 200 ) {   
			xmlDoc = req.responseXML;			
			var select = xmlDoc.getElementsByTagName( "select" );			
			//document.getElementById( this ).length = 1;
			document.getElementById( this ).length = 0;
			if ( select.length > 0 ) {
				for( var i=0; i<select.length; i++ ) {
					var node = document.createElement( "option" );
					var text = document.createTextNode( select[i].getElementsByTagName( "text" )[0].childNodes[0].nodeValue );
					node.setAttribute( "value", select[i].getElementsByTagName( "value" )[0].childNodes[0].nodeValue );
					node.appendChild( text );
					var result = document.getElementById( this );
					result.appendChild( node );				
				}
			} else {
//				var node = document.createElement( "option" );
//				var text = document.createTextNode( unescape( "답사지 정보가 없습니다" ) );
//				node.appendChild( text );
//				var result = document.getElementById( this );
//				result.appendChild( node );		
			}
		}
	}
}

function setYears( req ) {
	var xmlDoc = null;
	var startYear = new Number( 0 );
	var endYear = new Number( 0 );
	var node;
	var text;
	var result;
	if (req.readyState == 4) {
		if (req.status == 200) {   
			xmlDoc = req.responseXML;
			document.getElementById( "selEndYear" ).length = 0;
			document.getElementById( "selStartYear" ).length = 0;
			
			var select = xmlDoc.getElementsByTagName( "select" );
			for( var i=0; i<select.length; i++ ) {
				startYear = select[i].getElementsByTagName( "text" )[0].childNodes[0].nodeValue;
				endYear = select[i].getElementsByTagName( "value" )[0].childNodes[0].nodeValue;
				//alert( endYear );
			}
			for( var j = endYear; j>=startYear; j-- ) {
				node = document.createElement( "option" );
				text = document.createTextNode( j );
				node.setAttribute( "value", j );
				node.appendChild( text );
				result = document.getElementById( "selEndYear" );
				result.appendChild( node );
			}

			for( var k = startYear; k<=endYear; k++ ) {
				node = document.createElement( "option" );
				text = document.createTextNode( k );
				node.setAttribute( "value", k );
				node.appendChild( text );
				result = document.getElementById( "selStartYear" );
				result.appendChild( node );
			}
		} else {
			//alert("error: "+req.status);
		}
	}
}
