/**
 * Author(s) : mtkim
 * Created Date : 2009.09.30
 * Description : 공간단위구분 선택시 세부공간단위를 가져온다. 
 * 
 * param
 * ------type : 전국, 수질영향권역, 행정구역(법정동)
 * 
 * return : 없음 
 */
function selectItem(){

	var frm = document.selectItemValue;
	var frm2 = document.waterQualityFrm;
	var type = frm2.selectArea.value;

	frm.selectItem.value = type;

	frm.doNm.value = "";
	frm.ctyNm.value = "";
	frm.dongNm.value = "";
	frm.riNm.value = "";
	frm.waterLarge.value = "";
	frm.waterMiddle.value = "";
	frm.waterSmall.value = "";
	
	frm2.selectAreaList.length = 0;

	if(type == "전국"){

		document.getElementById("rawDong").style.display = "block";
		document.getElementById("water").style.display = "none";
		
		frm2.doNm.options.length = 0;
		frm2.ctyNm.options.length = 0;
		frm2.dongNm.options.length = 0;
		frm2.riNm.options.length = 0;

		frm2.waterLarge.options.length = 0;
		frm2.waterMiddle.options.length = 0;
		frm2.waterSmall.options.length = 0;
		
		document.getElementById("gubunNm1").innerHTML = "";
		document.getElementById("gubunNm2").innerHTML = "";
		document.getElementById("gubunNm3").innerHTML = "";
		document.getElementById("gubunNm4").innerHTML = "";
	}else if(type == "수질영향권역"){
		
		document.getElementById("areaSearch").style.display = "none";
		document.getElementById("rawDong").style.display = "none";
		document.getElementById("water").style.display = "block";

		document.getElementById("gubunNm1").innerHTML = "대권역";
		document.getElementById("gubunNm2").innerHTML = "중권역";
		document.getElementById("gubunNm3").innerHTML = "소권역";
		document.getElementById("gubunNm4").innerHTML = "";

		frm.method = "post";
		frm.action = "/common/selectWater.do";
		frm.target = "dummy";
		frm.submit();	

	}else if(type == "행정구역(법정동)"){
		
		document.getElementById("areaSearch").style.display = "block";
		document.getElementById("rawDong").style.display = "block";
		document.getElementById("water").style.display = "none";

		document.getElementById("gubunNm1").innerHTML = "시도";
		document.getElementById("gubunNm2").innerHTML = "시군구";
		document.getElementById("gubunNm3").innerHTML = "읍면동";
		document.getElementById("gubunNm4").innerHTML = "동리";

		frm.method = "post";
		frm.action = "/common/selectRawDong.do";
		frm.target = "dummy";
		frm.submit();
	}
}


/**
 * Author(s) : mtkim
 * Created Date : 2009.9.30
 * Description : 중권역리스트 가져오기
 *
 * param : 
 * ------large : 대권역코드
 *
 * return : 없음
 * 
 */
function waterLargeSelect(large){
	
	var frm = document.selectItemValue;

	frm.waterLarge.value = large;

	frm.method = "post";
	frm.action = "/common/selectWater.do";
	frm.target = "dummy";
	frm.submit();	
}

/**
 * Author(s) : mtkim
 * Created Date : 2009.9.30
 * Description : 소권역리스트 가져오기
 *
 * param : 
 * ------large : 중권역코드
 *
 * return : 없음
 * 
 */
function waterMiddleSelect(middle){
	
	var frm = document.selectItemValue;

	frm.waterMiddle.value = middle;

	frm.method = "post";
	frm.action = "/common/selectWater.do";
	frm.target = "dummy";
	frm.submit();	
}

/**
 * Author(s) : mtkim
 * Created Date : 2009.9.30
 * Description : 시군구리스트 가져오기
 *
 * param : 
 *
 * return : 없음
 * 
 */
function doNmSelect(){
	
	var frm = document.selectItemValue;
	var str = "";

	for(i=0 ; i<document.waterQualityFrm.doNm.options.length ; i++){
		if(document.waterQualityFrm.doNm.options[i].selected)
			str = document.waterQualityFrm.doNm.options[i].text;
	}

	frm.admCd.value = document.waterQualityFrm.doNm.value;
	frm.doNm.value = str;
	frm.ctyNm.value = "";
	frm.dongNm.value = "";
	frm.riNm.value = "";

	frm.method = "post";
	frm.action = "/common/selectRawDong.do";
	frm.target = "dummy";
	frm.submit();	
}

/**
 * Author(s) : mtkim
 * Created Date : 2009.9.30
 * Description : 읍면동리스트 가져오기
 *
 * param : 
 *
 * return : 없음
 * 
 */
function ctyNmSelect(){
	
	var frm = document.selectItemValue;
	var str = "";

	for(i=0 ; i<document.waterQualityFrm.ctyNm.options.length ; i++){
		if(document.waterQualityFrm.ctyNm.options[i].selected)
			str = document.waterQualityFrm.ctyNm.options[i].text;
	}

	frm.admCd.value = document.waterQualityFrm.ctyNm.value;
	frm.ctyNm.value = str;
	frm.dongNm.value = "";
	frm.riNm.value = "";

	frm.method = "post";
	frm.action = "/common/selectRawDong.do";
	frm.target = "dummy";
	frm.submit();	
}

/**
 * Author(s) : mtkim
 * Created Date : 2009.9.30
 * Description : 동리리스트 가져오기
 *
 * param : 
 *
 * return : 없음
 * 
 */
function dongNmSelect(){
	
	var frm = document.selectItemValue;
	var str = "";

	for(i=0 ; i<document.waterQualityFrm.dongNm.options.length ; i++){
		if(document.waterQualityFrm.dongNm.options[i].selected)
			str = document.waterQualityFrm.dongNm.options[i].text;
	}

	frm.admCd.value = document.waterQualityFrm.dongNm.value;
	frm.dongNm.value = str;
	frm.riNm.value = "";

	frm.method = "post";
	frm.action = "/common/selectRawDong.do";
	frm.target = "dummy";
	frm.submit();	
}

/**
 * Author(s) : mtkim
 * Created Date : 2009.9.30
 * Description : 동리 선택
 *
 * param : 
 *
 * return : 없음
 * 
 */
function riNmSelect(){
	
	var frm = document.selectItemValue;
	var str = "";

	for(i=0 ; i<document.waterQualityFrm.riNm.options.length ; i++){
		if(document.waterQualityFrm.riNm.options[i].selected)
			str = document.waterQualityFrm.riNm.options[i].text;
	}

	frm.admCd.value = document.waterQualityFrm.riNm.value;
	frm.riNm.value = str;
}

/**
 * Author(s) : mtkim
 * Created Date : 2009.08.10
 * Description : 선택된 세부공간단위를 검색조건에 추가한다.
 *
 * param
 * 
 * return 
 * 
 */
function addItem(type, frm, frm2, parent){
	
	if(type == '행정구역(법정동)'){

		if(frm.doNm.value == ""){
			alert("선택된 항목이 없습니다.");
			return;
		}

		var selectItem = frm.doNm.value;
		if (frm.ctyNm.value != "")
			selectItem += "/" + frm.ctyNm.value;
		if (frm.dongNm.value != "")
			selectItem += "/" + frm.dongNm.value;
		if (frm.riNm.value != "")
			selectItem += "/" + frm.riNm.value;

		if(parent != 'pop'){

			if (frm.riNm.value != "")
				frm.admCd.value = frm.admCd.value;
			else if (frm.dongNm.value != "")
				frm.admCd.value = frm.admCd.value.substring(0, 8);
			else if (frm.ctyNm.value != "")
				frm.admCd.value = frm.admCd.value.substring(0, 5);
			else
				frm.admCd.value = frm.admCd.value.substring(0, 2);

		}

		var len = frm2.selectAreaList.options.length;

		for(i=0 ; i<len ; i++){

			if(frm2.selectAreaList.options[i].value == frm.admCd.value){
				alert("이미 선택된 항목입니다.");
				if(parent == 'popup')
					winFocus.focus();
				return;
			}
		}
		
		frm2.selectAreaList.options[len] = new Option(selectItem);
		

		if (frm.riNm.value != "")
			frm2.selectAreaList.options[len].value = frm.admCd.value;
		else if (frm.dongNm.value != "")
			frm2.selectAreaList.options[len].value = frm.admCd.value.substring(0, 8);
		else if (frm.ctyNm.value != "")
			frm2.selectAreaList.options[len].value = frm.admCd.value.substring(0, 5);
		else
			frm2.selectAreaList.options[len].value = frm.admCd.value.substring(0, 2);

		if(parent == "popup"){
			onChange();
			frm.admCd.value = "";
			frm.doNm.value = "";
		}
	}

	if(type == "수질영향권역"){

		if(frm2.waterLarge.value == "")
			return;

		var waterNm = "";
		var waterCd = "";
		
		for(i=0 ; i<frm2.waterLarge.options.length ; i++){
			if(frm2.waterLarge.options[i].selected){
				waterNm = frm2.waterLarge.options[i].text;
				waterCd = frm2.waterLarge.options[i].value;
			}
		}

		if(frm2.waterMiddle.value != ""){
			for(i=0 ; i<frm2.waterMiddle.options.length ; i++){
				if(frm2.waterMiddle.options[i].selected){
					waterNm += "/" + frm2.waterMiddle.options[i].text;
					waterCd += frm2.waterMiddle.options[i].value;
				}
			}
		}
		
		if(frm2.waterSmall != undefined && frm2.waterSmall.value != ""){
			for(i=0 ; i<frm2.waterSmall.options.length ; i++){
				if(frm2.waterSmall.options[i].selected){
					waterNm += "/" + frm2.waterSmall.options[i].text;
					waterCd += frm2.waterSmall.options[i].value;
				}
			}
		}

		var len = frm2.selectAreaList.options.length;
		
		for(i=0 ; i<len ; i++){
			if(frm2.selectAreaList.options[i].value == waterCd){
				alert("이미 선택된 항목입니다.");
				return;
			}
		}

		frm2.selectAreaList.options[len] = new Option(waterNm, waterCd);

	}
}


/**
 * Author(s) : mtkim
 * Created Date : 2009.9.28
 * Description : 공간단위 검색(팝업)시 공간단위항목추가
 *
 * param
 * -----doNm : 선택된 시도
 * -----ctyNm : 선택된 시군구
 * -----dongNm : 선택된 읍면동
 * -----riNm : 선택된 동리
 * -----admCd : 선택된 공간단위코드
 *
 * return : 없음
 * 
 */
function addSearchItem(doNm, ctyNm, dongNm, riNm, admCd){
	
	var frm = document.selectItemValue;

	frm.doNm.value = doNm;
	frm.ctyNm.value = ctyNm;
	frm.dongNm.value = dongNm;
	frm.riNm.value = riNm;
	
	if (riNm != "")
		frm.admCd.value = admCd;
	else if (dongNm != "")
		frm.admCd.value = admCd.substring(0, 8);
	else if (ctyNm != "")
		frm.admCd.value = admCd.substring(0, 5);
	else
		frm.admCd.value = admCd.substring(0, 2);
	
	addItem("행정구역(법정동)", document.selectItemValue, document.waterQualityFrm, 'popup');
}

/**
 * Author(s) : mtkim
 * Created Date : 2009.08.10
 * Description : 선택된 세부공간단위를 검색조건에서 삭제한다.
 *
 * param
 *
 * return
 * 
 */
function deleteItem(frm){

	var x = false;

	for(i=0 ; i<frm.selectAreaList.options.length ; i++){
		if(frm.selectAreaList.options[i].selected == true){
			frm.selectAreaList.remove(i);
			i-=1;
			x = true;
		}
	}
}



/**
 * Author(s) : mtkim
 * Created Date : 2009.8.10
 * Description : 측정소나 측정항목 추가
 *
 * param : 
 * -------key : 측정소인지 측정항목인지 구분값
 * 
 *
 * return : 없음
 * 
 */
function addItems(key){
	
	var id = key;
	var select;
	var addSelect;
	
	if(id=='placeAdd'){
		select = document.getElementById("place");
		addSelect = document.getElementById("place2");
	}else if(id=='listAdd'){
		select = document.getElementById("list");
		addSelect = document.getElementById("list2");
	}

	var str = "";
	var a = 0;
	for(var i=0; i<select.options.length; i++){
		if(select.options[i].selected){
			var flag=0;

			if(select.options[i].value != ""){
				if(addSelect.options.length != 0 ){
					for(var j=0; j<addSelect.options.length; j++){
						if(addSelect.options[j].value==select.options[i].value){
							a++;
							if(a != 1){
								str += ", ";
							}
							str += select.options[i].text;
							flag=1;
						}
					}
				}
				if(flag==0){
					addSelect.options[addSelect.length]=new Option(select.options[i].text,select.options[i].value);
					select.options[i].selected = false;
				}
			}
		}
	}
}

/**
 * Author(s) : mtkim
 * Created Date : 2009.8.10
 * Description : 측정소나 측정항목 삭제
 *
 * param : 
 * -------key : 측정소인지 측정항목인지 구분값
 * 
 *
 * return : 없음
 * 
 */
function deleteItems(key){
 
	var id = key;
	var select;
	var addSelect;

	if(id=='placeDel'){
		select = document.getElementById("place");
		addSelect = document.getElementById("place2");
	}else if(id=='listDel'){
		select = document.getElementById("list");
		addSelect = document.getElementById("list2");
	}
	for(var i=0; i<addSelect.options.length; i++){
		if(addSelect.options[i].selected){

			addSelect.remove(i);
			i=-1;
		}
	}

}


function isNumber(value)
{

	if(isNaN(value) ) {
		return false;
	} else {
		return true;
	}
}

String.prototype.replaceAll = replaceAll;
function replaceAll(strValue1, strValue2){
  var strTemp = this;
  while(1){
    if( strTemp.indexOf(strValue1) != -1 )
       strTemp = strTemp.replace(strValue1, strValue2);
    else
       break;
  }
   return strTemp;
 
}


/**
 * Author(s) : mtkim
 * Created Date : 2009.7.27
 * Description : 파일다운로드
 *
 * param 
 * -----form : 폼이름
 * -----fileNm : 파일명
 * -----filePath : 파일경로
 *
 * return : 없음
 * 
 */
function fileDown(form, fileNm, filePath){

	var frm = form;

	frm.file1Nm.value = fileNm;
	frm.file1Path.value = filePath;

	frm.method = "post";
	frm.action = "/common/fileDown.do";
	frm.submit();

}

/**
 * Author(s) : mtkim
 * Created Date : 2009.7.28
 * Description : 엔터키 체크
 *
 * param  : 없음
 *
 * return : 없음
 * 
 */
function chkEnter() {
	alert(window.event.keyCode == 13);
    return window.event.keyCode == 13;
}



/**
 * Author(s) : mtkim
 * Created Date : 2009.9.28
 * Description : 행정구역(법정동) 검색창띄우기
 *
 * param  : 없음
 *
 * return : 없음
 * 
 */
var winFocus;

function popSearch(){

	winFocus = window.open("/front/waterQuality/popSearch.jsp", "search", "width=400, height=388, left=500");
	
}


/**
 * Author(s) : mtkim
 * Created Date : 2009.07.27
 * Description : trim() 함수
 *
 * param : 없음
 *
 * return : 
 * ------- 값의 문구를 제거하여 리턴
 * 
 */

String.prototype.trim = function()
{
    return this.replace(/(^\s*)|(\s*$)/g, ""); 
}
