<%@ page language="java" contentType="text/html; charset=UTF-8" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="com.ampiere.dto.CodeValue" %>
<%@ page import="com.ampiere.util.AmpiereUtil" %>
<jsp:include page="/ampiere/jsp/common/includeYAHOOUI.jsp" flush="true" /> 
<html>
<head>
<title><bean:write name="locationActionForm" property="title"/></title>
<link type="text/css" rel="stylesheet" href="${pageContext.request.contextPath}/ampiere/css/import.css" />
<link type="text/css" rel="stylesheet" href="${pageContext.request.contextPath}/ampiere/css/window.css"/>
<script type="text/javascript" src="${pageContext.request.contextPath}/ampiere/js/window.js"></script>
<style type="text/css">
<!--
    .popupInfoTable {
        padding-top: 2px;
        padding-bottom: 1px;
    }
    .lbl {
        padding-right: 5px;
    }
-->
</style>
<script type="text/javascript">
function postalLookup() {
	var locationOptionObj = document.getElementById("locationOption");
	if (locationOptionObj != null) {
		locationOptionObj.value = "change";
        locationOptionObj.form.submit();
	}
}

function postalLookupPopup() {
	var windowNo = 0;
	var columnId = 0;
	var countryId = 0;
	var windowNoObj = document.getElementById("windowNo");
	if (windowNoObj != null) {
		windowNo = windowNoObj.value;
	}
	var columnIdObj = document.getElementById("column_ID");
	if (columnIdObj != null) {
		columnId = columnIdObj.value;
	}
	var countryIdObj = document.getElementById("countryId");
	if (countryIdObj != null) {
		countryId = countryIdObj.value;
	}
    var key = "";
    var postalObj = document.getElementById("fpostal");
    var postalAddObj = document.getElementById("fpostalAdd");
    if (postalObj != null) {
        key += postalObj.value;
    }
    if (postalAddObj != null) {
        key += postalAddObj.value;
    }

	var w = 800;
	var h = 600;
    var url = "/compiere/ampiere/InfoPostalAction";
	url += (".do?&windowNo=" + windowNo + "&value=" + key + "&column_ID=" + columnId + "&countryId=" + countryId);
	url = encodeURI(url);
	var childWnd = openPopup(url, "InfoPostal", w, h, 0);
	childWndArray[childWndCount++] = childWnd;
}

function countryChange(id) {
	var countryIdObj = document.getElementById("countryId");
	if (countryIdObj != null) {
		var locationOptionObj = document.getElementById("locationOption");
		if (locationOptionObj != null) {
			locationOptionObj.value = "";
		}

		countryIdObj.value = id;
		countryIdObj.form.submit();
	}
}

/**
 * Update location informations(do callout)
 */
function locationProcess() {
	if (! checkParent(window)) {
        window.close();
		return;
	}

	var columnName = "C_Location_ID";
    var targetObj = null;
    var updateObj = null;
    var onChangeObj = null;
    if (window.opener.top.WWindow) {
    	targetObj = (window.opener.top.WWindow.document.getElementById(columnName + "D"));
    	updateObj = (window.opener.top.WWindow.document.getElementById(columnName + "F"));
    } else {
	    targetObj = (window.opener.top.document.getElementById(columnName + "D"));
	    updateObj = (window.opener.top.document.getElementById(columnName + "F"));
    }
    if (targetObj != null && updateObj != null) {
		var locationIdObj = document.getElementById("locationId");
		var locationNameObj = document.getElementById("locationName");
		if (locationIdObj != null && locationNameObj != null) {
        	targetObj.value = locationIdObj.value;
            updateObj.value = locationNameObj.value;
            startUpdate(updateObj);
        }
    }

    window.close();
	window.opener.focus();
}	// locationProcess

/**
 * Save location information, and close location window
 */
function locationSave() {
	if (! checkParent(window)) {
        window.close();
		return;
	}

	var locationOptionObj = document.getElementById("locationOption");
	if (locationOptionObj != null) {
		locationOptionObj.value = "save";
        locationOptionObj.form.submit();
	}
}
</script>
</head>

<body id="popup" onunload="closeMyChildren();" <% if ("popup".equals(request.getAttribute("locationInit"))) { %> onload="postalLookupPopup();" <% } else if ("close".equals(request.getAttribute("locationInit"))) { %> onload="locationProcess();" <% } %>>

<html:form style="margin:0;padding:0;" target="_self" action="/ampiere/LocationAction.do" enctype="application/x-www-form-urlencoded">
<html:hidden styleId="windowNo" name="locationActionForm" property="windowNo"/>
<html:hidden styleId="columnName" name="locationActionForm" property="columnName"/>
<html:hidden styleId="column_ID" name="locationActionForm" property="column_ID"/>
<html:hidden styleId="locationOption" name="locationActionForm" property="locationOption"/>
<html:hidden styleId="countryId" name="locationActionForm" property="countryId"/>
<html:hidden styleId="postalId" name="locationActionForm" property="postalId"/>
<html:hidden styleId="locationId" name="locationActionForm" property="locationId"/>
<html:hidden styleId="locationName" name="locationActionForm" property="locationName"/>

	<table width="100%" >
	<tr>
	<td>
<div id="header">
		<h1><bean:write name="locationActionForm" property="title"/></h1>
</div>
	</td>
    <td valign="top" width="18">
    <div id="progressMsg" style="padding-top:1px;display:none;"><img src="${pageContext.request.contextPath}/ampiere/images/Loading-0.gif" /></div>
    </td>
    </tr>
	</table>

<!-- body -->
<div id="body" >

	<table cellspacing="0" cellpadding="0" class="popupInfoTable">
<%
	if ("true".equals(request.getAttribute("addressLinesLocalReverse"))) {
%>
		<tr>
			<td class="lbl"><bean:write name="locationActionForm" property="lcountry"/></td>
			<td>
			<html:select name="locationActionForm" property="countrySelected" onchange="countryChange(this.value)">
			<bean:define id="countryList" name="locationActionForm" property="countryList"></bean:define>
			<html:options collection="countryList" property="code" labelProperty="value"/>
			</html:select>
			</td>
		</tr>
<%
		if ("true".equals(request.getAttribute("postal"))) {
%>
		<tr>
			<td class="lbl"><bean:write name="locationActionForm" property="lpostal"/></td>
			<td><html:text styleId="fpostal" name="locationActionForm" property="fpostal" size="10"></html:text></td>
		</tr>
<%
		}
		if ("true".equals(request.getAttribute("postalAdd"))) {
%>
		<tr>
			<td class="lbl"><bean:write name="locationActionForm" property="lpostalAdd"/></td>
			<td><html:text styleId="fpostalAdd" name="locationActionForm" property="fpostalAdd" size="10"></html:text></td>
		</tr>
		<tr>
			<td></td>
			<td><html:button onclick="postalLookup();" property="lookupPostal"><bean:write name="locationActionForm" property="lpostalLookup"/></html:button></td>
		</tr>
<%
		}
		if ("true".equals(request.getAttribute("region"))) {
%>
		<tr>
			<td class="lbl"><bean:write name="locationActionForm" property="lregion"/></td>
			<td>
			<html:select name="locationActionForm" property="regionSelected">
			<bean:define id="regionList" name="locationActionForm" property="regionList"></bean:define>
			<html:options collection="regionList" property="code" labelProperty="value"/>
			</html:select>
			</td>
		</tr>
<%
		}
		if ("true".equals(request.getAttribute("city"))) {
%>
		<tr>
			<td class="lbl"><bean:write name="locationActionForm" property="lcity"/></td>
			<td><html:text name="locationActionForm" property="fcity" size="30"></html:text></td>
		</tr>
<%
		}
%>
		<tr>
			<td class="lbl"><bean:write name="locationActionForm" property="laddress1"/></td>
			<td><html:text name="locationActionForm" property="faddress1" size="40"></html:text></td>
		</tr>
		<tr>
			<td class="lbl"><bean:write name="locationActionForm" property="laddress2"/></td>
			<td><html:text name="locationActionForm" property="faddress2" size="40"></html:text></td>
		</tr>
		<tr>
			<td class="lbl"><bean:write name="locationActionForm" property="laddress3"/></td>
			<td><html:text name="locationActionForm" property="faddress3" size="40"></html:text></td>
		</tr>
		<tr>
			<td class="lbl"><bean:write name="locationActionForm" property="laddress4"/></td>
			<td><html:text name="locationActionForm" property="faddress4" size="40"></html:text></td>
		</tr>
<%
	} else {
%>
		<tr>
			<td class="lbl"><bean:write name="locationActionForm" property="laddress1"/></td>
			<td><html:text name="locationActionForm" property="faddress1" size="40"></html:text></td>
		</tr>
		<tr>
			<td class="lbl"><bean:write name="locationActionForm" property="laddress2"/></td>
			<td><html:text name="locationActionForm" property="faddress2" size="40"></html:text></td>
		</tr>
		<tr>
			<td class="lbl"><bean:write name="locationActionForm" property="laddress3"/></td>
			<td><html:text name="locationActionForm" property="faddress3" size="40"></html:text></td>
		</tr>
		<tr>
			<td class="lbl"><bean:write name="locationActionForm" property="laddress4"/></td>
			<td><html:text name="locationActionForm" property="faddress4" size="40"></html:text></td>
		</tr>
<%
		if ("true".equals(request.getAttribute("city"))) {
%>
		<tr>
			<td class="lbl"><bean:write name="locationActionForm" property="lcity"/></td>
			<td><html:text name="locationActionForm" property="fcity" size="30"></html:text></td>
		</tr>
<%
		}
		if ("true".equals(request.getAttribute("region"))) {
%>
		<tr>
			<td class="lbl"><bean:write name="locationActionForm" property="lregion"/></td>
			<td>
			<html:select name="locationActionForm" property="regionSelected">
			<bean:define id="regionList" name="locationActionForm" property="regionList"></bean:define>
			<html:options collection="regionList" property="code" labelProperty="value"/>
			</html:select>
			</td>
		</tr>
<%
		}
		if ("true".equals(request.getAttribute("postal"))) {
%>
		<tr>
			<td class="lbl"><bean:write name="locationActionForm" property="lpostal"/></td>
			<td><html:text styleId="fpostal" name="locationActionForm" property="fpostal" size="10"></html:text></td>
		</tr>
<%
		}
%>
		<tr>
			<td class="lbl"><bean:write name="locationActionForm" property="lcountry"/></td>
			<td>
			<html:select name="locationActionForm" property="countrySelected" onchange="countryChange(this.value)">
			<bean:define id="countryList" name="locationActionForm" property="countryList"></bean:define>
			<html:options collection="countryList" property="code" labelProperty="value"/>
			</html:select>
			</td>
		</tr>
<%
	}
%>

	</table>


<!-- body-footer -->
<div class="bottomMenuRight">
<table>
	<tr>
	<td>
		<input id="ok" type="image" src="${pageContext.request.contextPath}/ampiere/images/Ok32.png" onclick="locationSave();" alt="<bean:write name='locationActionForm' property='submitBtnTitle'/>"/>
		<span>OK</span>	
		<input id="cancel" type="image" src="${pageContext.request.contextPath}/ampiere/images/Cancel32.png" onclick="window.close();return false;" alt="<bean:write name='locationActionForm' property='closeBtnTitle'/>"/>
		<span>キャンセル</span>
	    </td>
	</tr>
	</table>
</div>
<!-- /body-footer -->
</div>
<!-- /body -->

</html:form>
</body>
</html>
