﻿// JScript File

// Address

// idx: index
// obj: control object
// Return: code_name|...
function SetAddress(idx)
{
	var strSelCode = "";
    var objAddress = null;
    var o;

    if (idx == 1)
    {
        objAddress = document.all[PREFIX+"dlsAddress2"];

        o = document.all[PREFIX+"dlsAddress2"];
        while (o.options.length) o.options.remove(0);
        o = document.all[PREFIX+"dlsAddress3"];
        while (o.options.length) o.options.remove(0);
        o = document.all[PREFIX+"dlsAddress4"];
        while (o.options.length) o.options.remove(0);

        o = document.all[PREFIX+"dlsAddress2"];
        o.style.width = "";
        o = document.all[PREFIX+"dlsAddress3"];
        o.style.width = "26px";
        o = document.all[PREFIX+"dlsAddress4"];
        o.style.width = "26px";

		o = document.all[PREFIX+"dlsAddress1"];
		strSelCode = o.options[o.selectedIndex].value;
    }
    else if (idx == 2)
    {
        objAddress = document.all[PREFIX+"dlsAddress3"];

        o = document.all[PREFIX+"dlsAddress3"];
        while (o.options.length) o.options.remove(0);
        o = document.all[PREFIX+"dlsAddress4"];
        while (o.options.length) o.options.remove(0);

        o = document.all[PREFIX+"dlsAddress3"];
        o.style.width = "";
        o = document.all[PREFIX+"dlsAddress4"];
        o.style.width = "26px";

		o = document.all[PREFIX+"dlsAddress1"];
		strSelCode = o.options[o.selectedIndex].value;
		o = document.all[PREFIX+"dlsAddress2"];
		strSelCode += o.options[o.selectedIndex].value;
    }
    else if (idx == 3)
    {
        objAddress = document.all[PREFIX+"dlsAddress4"];

        o = document.all[PREFIX+"dlsAddress4"];
        while (o.options.length) o.options.remove(0);

        o = document.all[PREFIX+"dlsAddress4"];
        o.style.width = "";

		o = document.all[PREFIX+"dlsAddress1"];
		strSelCode = o.options[o.selectedIndex].value;
		o = document.all[PREFIX+"dlsAddress2"];
		strSelCode += o.options[o.selectedIndex].value;
		o = document.all[PREFIX+"dlsAddress3"];
		strSelCode += o.options[o.selectedIndex].value;
    }

    if (objAddress != null)
    {
		var reqUrl = "../Common/Address.aspx?idx=" + (idx+1) + "&rgn=" + strSelCode;

		//XML Request
		var xmlRequest = new ActiveXObject("Microsoft.XMLHTTP");
		xmlRequest.open("POST", reqUrl, false);
		xmlRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		xmlRequest.send(null);

        if (xmlRequest.ResponseText.trim() != "")
        {
			var newOpt;
			var d = xmlRequest.ResponseText.split('|');

			for (var i=0; i<d.length; i++)
			{
				var r = d[i].split('_');
				newOpt = document.createElement("OPTION");
				newOpt.text = r[1];
				newOpt.value = r[0];
				objAddress.options.add(newOpt);
			}
		}
    }
}

// b: Optional - Fill Complete
function GetAddressCode(b)
{
	var bFillComplete = (b == null ? true : b);
	var strSelCode = "";
	var bSuccess;
	var o;

	// 시도
	o = document.all[PREFIX+"dlsAddress1"];
	if (bFillComplete)
	{
		bSuccess = false;
		if (o.options.length > 0 && o.selectedIndex > 0)
		{
			strSelCode += o.options[o.selectedIndex].value;
			bSuccess = true;
		}

		if (!bSuccess)
		{
			alert("시도를 선택하십시오.");
			o.focus();
			return "";
		}
	}
	else
	{
		strSelCode += o.options.length > 0 ? o.options[o.selectedIndex].value : "";
	}

	// 시군구
	o = document.all[PREFIX+"dlsAddress2"];
	if (bFillComplete)
	{
		bSuccess = false;
		if (o.options.length > 0 && o.selectedIndex > 0)
		{
			strSelCode += o.options[o.selectedIndex].value;
			bSuccess = true;
		}

		if (!bSuccess)
		{
			alert("시군구를 선택하십시오.");
			o.focus();
			return "";
		}
	}
	else
	{
		strSelCode += o.options.length > 0 ? o.options[o.selectedIndex].value : "";
	}

	// 읍면동
	o = document.all[PREFIX+"dlsAddress3"];
	if (bFillComplete)
	{
		bSuccess = false;
		if (o.options.length > 0 && o.selectedIndex > 0)
		{
			strSelCode += o.options[o.selectedIndex].value;
			bSuccess = true;
		}

		if (!bSuccess)
		{
			alert("읍면동을 선택하십시오.");
			o.focus();
			return "";
		}
	}
	else
	{
		strSelCode += o.options.length > 0 ? o.options[o.selectedIndex].value : "";
	}

	// 리
	o = document.all[PREFIX+"dlsAddress4"];
	if (bFillComplete && o.options.length > 0)
	{
		bSuccess = false;
		if (o.selectedIndex > 0)
		{
			strSelCode += o.options[o.selectedIndex].value;
			bSuccess = true;
		}

		if (!bSuccess)
		{
			alert("리를 선택하십시오.");
			o.focus();
			return "";
		}
	}
	else
	{
		strSelCode += o.options.length > 0 ? o.options[o.selectedIndex].value : "";
	}

	var strCode = RightPad(strSelCode, 10, '0');
	o = document.all[PREFIX+"hdAddress"];
	if (o) o.value = strCode;

	return strCode;
}

function SetAddressArgs(idx, dls1, dls2, dls3, dls4)
{
    var strSelCode = "";
    var objAddress = null;
    var o;

    if (idx == 1)
    {
        objAddress = document.all[dls2];

        o = document.all[dls2];
        while (o.options.length) o.options.remove(0);
        o = document.all[dls3];
        while (o.options.length) o.options.remove(0);
        o = document.all[dls4];
        while (o.options.length) o.options.remove(0);

        o = document.all[dls2];
        o.style.width = "";
        o = document.all[dls3];
        o.style.width = "26px";
        o = document.all[dls4];
        o.style.width = "26px";

		o = document.all[dls1];
		strSelCode = o.options[o.selectedIndex].value;
    }
    else if (idx == 2)
    {
        objAddress = document.all[dls3];

        o = document.all[dls3];
        while (o.options.length) o.options.remove(0);
        o = document.all[dls4];
        while (o.options.length) o.options.remove(0);

        o = document.all[dls3];
        o.style.width = "";
        o = document.all[dls4];
        o.style.width = "26px";

		o = document.all[dls1];
		strSelCode = o.options[o.selectedIndex].value;
		o = document.all[dls2];
		strSelCode += o.options[o.selectedIndex].value;
    }
    else if (idx == 3)
    {
        objAddress = document.all[dls4];

        o = document.all[dls4];
        while (o.options.length) o.options.remove(0);

        o = document.all[dls4];
        o.style.width = "";

		o = document.all[dls1];
		strSelCode = o.options[o.selectedIndex].value;
		o = document.all[dls2];
		strSelCode += o.options[o.selectedIndex].value;
		o = document.all[dls3];
		strSelCode += o.options[o.selectedIndex].value;
    }

    if (objAddress != null)
    {
		var reqUrl = "../Common/Address.aspx?idx=" + (idx+1) + "&rgn=" + strSelCode;

		//XML Request
		var xmlRequest = new ActiveXObject("Microsoft.XMLHTTP");
		xmlRequest.open("POST", reqUrl, false);
		xmlRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		xmlRequest.send(null);

        if (xmlRequest.ResponseText.trim() != "")
        {
			var newOpt;
			var d = xmlRequest.ResponseText.split('|');

			for (var i=0; i<d.length; i++)
			{
				var r = d[i].split('_');
				newOpt = document.createElement("OPTION");
				newOpt.text = r[1];
				newOpt.value = r[0];
				objAddress.options.add(newOpt);
			}
		}
    }
}

// b: Optional - Fill Complete
function GetAddressCodeArgs(b, dls1, dls2, dls3, dls4, hdnAddress)
{
	var bFillComplete = (b == null ? true : b);
	var strSelCode = "";
	var bSuccess;
	var o;

	// 시도
	o = document.all[dls1];
	if (bFillComplete)
	{
		bSuccess = false;
		if (o.options.length > 0 && o.selectedIndex > 0)
		{
			strSelCode += o.options[o.selectedIndex].value;
			bSuccess = true;
		}

		if (!bSuccess)
		{
			alert("시도를 선택하십시오.");
			o.focus();
			return "";
		}
	}
	else
	{
		strSelCode += o.options.length > 0 ? o.options[o.selectedIndex].value : "";
	}

	// 시군구
	o = document.all[dls2];
	if (bFillComplete)
	{
		bSuccess = false;
		if (o.options.length > 0 && o.selectedIndex > 0)
		{
			strSelCode += o.options[o.selectedIndex].value;
			bSuccess = true;
		}

		if (!bSuccess)
		{
			alert("시군구를 선택하십시오.");
			o.focus();
			return "";
		}
	}
	else
	{
		strSelCode += o.options.length > 0 ? o.options[o.selectedIndex].value : "";
	}

	// 읍면동
	o = document.all[dls3];
	if (bFillComplete)
	{
		bSuccess = false;
		if (o.options.length > 0 && o.selectedIndex > 0)
		{
			strSelCode += o.options[o.selectedIndex].value;
			bSuccess = true;
		}

		if (!bSuccess)
		{
			alert("읍면동을 선택하십시오.");
			o.focus();
			return "";
		}
	}
	else
	{
		strSelCode += o.options.length > 0 ? o.options[o.selectedIndex].value : "";
	}

	// 리
	o = document.all[dls4];
	if (bFillComplete && o.options.length > 0)
	{
		bSuccess = false;
		if (o.selectedIndex > 0)
		{
			strSelCode += o.options[o.selectedIndex].value;
			bSuccess = true;
		}

		if (!bSuccess)
		{
			alert("리를 선택하십시오.");
			o.focus();
			return "";
		}
	}
	else
	{
		strSelCode += o.options.length > 0 ? o.options[o.selectedIndex].value : "";
	}

	var strCode = RightPad(strSelCode, 10, '0');
	o = document.all[hdnAddress];
	if (o) o.value = strCode;

	return strCode;
}








