var last_modified = "Thu, 01 Jun 1970 00:00:00 GMT";
// var cached_content = '';

var UA='';
if(navigator.userAgent.indexOf("Opera")!=-1)		 { UA='opera';     } 
else if(navigator.userAgent.indexOf("Safari")!=-1)	 { UA='safari';    }
//else if(navigator.userAgent.indexOf("Konqueror")!=-1){ UA='konqueror'; }
else if(navigator.userAgent.indexOf("Gecko")!=-1)    { UA='gecko';     }
else if(navigator.userAgent.indexOf("MSIE")!=-1)     { UA='ie';        }

var m_MouseStartX;
var m_MouseStartY;
var m_TableStartX;
var m_TableStartY;
var m_dragging = false;
var flagRequestFromMovePage = false;

var cgi = '/postdic/address_search.php';

function CheckAddressFromZip(){

    zip = document.Form1.ZIP1.value + document.Form1.ZIP2.value;
	SendData( 'KEY=ZIP&PAGE=0&ZIP=' + encodeURIComponent(zip) );
	
}

function CheckAddressFromTown(){

    town = document.Form1.ADDR1.value;
	SendData( 'KEY=TOWN&PAGE=0&TOWN=' + encodeURIComponent(town) );
	
}

function MovePage(key, zip, town, page){

	flagRequestFromMovePage = true;

	if(key=='ZIP'){
		SendData( 'KEY=ZIP&PAGE=' + page + '&ZIP=' + encodeURIComponent(zip) );
	} else if(key=='TOWN'){
		SendData( 'KEY=TOWN&PAGE=' + page + '&TOWN=' + encodeURIComponent(town) );
	}

}

function WriteResult( req ){

	// var elem = document.getElementById("RESULT");
	// var res = decodeURIComponent(req.responseText);
    // elem.innerHTML=res;

	var res = decodeURIComponent(req.responseText);

	if(res == 'INVALID_ACCESS'){
		alert("不正なアクセスです。"); return;
	} else if(res == 'ZIP_ERROR1'){
		alert("郵便番号が指定されていません。"); return;
	} else if(res == 'ZIP_ERROR2'){
		alert("郵便番号に使用できない文字が含まれています。"); return;
	} else if(res == 'ZIP_ERROR3'){
		alert("郵便番号は７文字以内で指定してください。"); return;
	} else if(res == 'TOWN_ERROR'){
		alert("町名が指定されていません。"); return;
	} else if(res == 'ZENKAKU_ERROR'){
		alert("町名は全角文字だけで指定してください。"); return;
	} else if(res == 'NO_DATA'){
		alert("該当する住所が見つかりませんでした。"); return;
	} else if(res == 'DB_ERROR'){
		alert("該当する住所が見つかりませんでした。"); return;
	}

	if(res != ''){
		var data = res.split('|')
		if(data[0] == 'complete'){
			AddressSetData(data[1], data[2], data[3]);
		} else {
			ShowPopupLayer(res);
		}
	}

}

function ShowPopupLayer( html ){

	var elem  = document.getElementById('OVER_LAYER');
	var ps = GetPageSize();
	with(elem.style){
		zIndex  = 101;
		width   = ps[0] + 'px';
		height  = ps[1] + 'px';
		left    = '0px';
		top     = '0px';
		display = '';
	}
	SetOpacity(elem, 0);

	if(!flagRequestFromMovePage){
		var elem;
		elem = document.getElementById("POPUP_POSITION");
		if(elem){
			var pos = new Array(2);
			GetPosition(elem, pos);
			var lx = pos[0] + 10;
			var ly = pos[1] + -20;
		} else {
			lx = 0;
			ly = 0;
		}
	}

	elem = document.getElementById("POPUP_RESULT");
	with(elem.style){
		zIndex  = 101;
		left    = lx + 'px';
		top     = ly + 'px';
		display = '';
	}
	elem.innerHTML = html;

	var dummy_layer = document.getElementById('DUMMY_LAYER');
	dummy_layer.style.left    = elem.style.left;
	dummy_layer.style.top     = elem.style.top;
	dummy_layer.style.width   = elem.offsetWidth;
	dummy_layer.style.height  = elem.offsetHeight;
	dummy_layer.style.display = 'block';

	flagRequestFromMovePage = false;

	//elem.onmousedown = onMouseDown;
	document.getElementById("POPUP_RESULT_HEADER").onmousedown = onMouseDown;
	document.onmousemove = onMouseMove;
	document.onmouseup   = onMouseUp;

}

function AddressSetData(zip, pref, address){

	var dum = zip.split('-');

	document.Form1.ZIP1.value = dum[0];
	document.Form1.ZIP2.value = dum[1];
	AddressSearchSelectList(document.Form1.PREF, pref);
	document.Form1.ADDR1.value = address;

	ClosePopupLayer();

}

function ClosePopupLayer(){

	document.getElementById("POPUP_RESULT_HEADER").onmousedown = onMouseDown;
	document.onmousemove = "";
	document.onmouseup   = "";

	document.getElementById("POPUP_RESULT").style.display='none';
	document.getElementById('OVER_LAYER').style.display='none';
	document.getElementById('DUMMY_LAYER').style.display='none';

	flagRequestFromMovePage = false;

}

function AddressSearchSelectList(Field, param){

	if(param != ''){
	    for(var i=0;i<Field.length-1;i++){
	        if(Field.options[i].value==param){
	            Field.selectedIndex=i;
	            break;
	        }
	    }
	}

}

function SendData( postdata ){

	var req = CreateXMLHTTP();
  	if( !req ){ return; }
	req.onreadystatechange = function(){
    	GetResponse( req, WriteResult );
  	}

	req.open( 'post', cgi, true );
  	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=SHIFT_JS");
	req.send( postdata );

}

function SetOpacity(elem, opacity){

	if(UA=='ie'){
		elem.style.filter='Alpha(opacity=' + opacity + ')';
	} else if(UA=='gecko' || UA=='opera'){
		opacity=opacity/100;
		elem.style.opacity=opacity;
	} else if(UA=='safari'){
		opacity=opacity/100;
		elem.style.opacity=opacity;
	}

}

function onWindowResize(){

	var ps = GetPageSize();
	var elem=document.getElementById('OVER_LAYER');
	with(elem.style){
		zIndex  = 101;
		width   = ps[0] + 'px';
		height  = ps[1] + 'px';
		left    = '0px';
		top     = '0px';
	}

}


function CreateXMLHTTP(){
	if( window.XMLHttpRequest ){ 		// IE以外
    	return new XMLHttpRequest();
  	} else if( window.ActiveXObject ){	// IE用(バージョンにより異なる)
    	try {
      		return new ActiveXObject("MSXML2.XMLHTTP");
    	} catch (e) {
      		try {
        		return new ActiveXObject("Microsoft.XMLHTTP");
      		} catch(e2) {
        		return null;
      		}
    	}
  	}
	return null
}

function GetResponse( req, callback ){

 	if( req.readyState == 4  && req.status == 200 ){
		if( req.responseText ){ callback(req); }
        if(req.getAllResponseHeaders().match("Last-Modified")){ last_modified = req.getResponseHeader("Last-Modified"); }
  	}

}

function GetPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

function GetPosition(elem, pos){

	var p,x,y;
	if(elem){
		x=elem.offsetLeft;
		y=elem.offsetTop;
		p=elem.offsetParent;
		while(p != null) {
			x+=p.offsetLeft;
			y+=p.offsetTop;		
			p=p.offsetParent;
		}
		// Mac IE Adjustment
		if (document.all && navigator.appVersion.indexOf("Mac") >= 0){
			y+=parseInt(document.body.topMargin);
			x+=parseInt(document.body.leftMargin);
		}
	} else {
		x=0;
		y=0;
	}

	pos[0]=x;
	pos[1]=y;

}

function onMouseDown(e) {

	if(m_dragging){ return true; }

	if(this.id == 'POPUP_RESULT_HEADER'){
		m_dragging = true;
	} else {
		m_dragging = false;
		return true;
	}

	if(window.event){ //IE
		m_MouseStartX = event.x + document.body.scrollLeft;
		m_MouseStartY = event.y + document.body.scrollTop;
		event.returnValue = false;
		event.cancelBubble = true;
	} else {
		m_MouseStartX = e.pageX;
		m_MouseStartY = e.pageY;
	}

	elem = document.getElementById("POPUP_RESULT");
	m_TableStartX = parseInt(elem.style.left);
	m_TableStartY = parseInt(elem.style.top);

	return false;

}

function onMouseMove(e) {

	if(!m_dragging) { return true; }

	if(window.event){ //IE
		event.returnValue = false;
		event.cancelBubble = true;
		newX = event.x + document.body.scrollLeft;
    	newY = event.y + document.body.scrollTop;
    } else {
		newX = e.pageX
		newY = e.pageY
    }
	
	var x = m_TableStartX + (newX - m_MouseStartX);
	var y = m_TableStartY + (newY - m_MouseStartY);

	elem = document.getElementById("POPUP_RESULT");
	with(elem.style){
		left    = x + 'px';
		top     = y + 'px';
	}

	var dummy_layer = document.getElementById('DUMMY_LAYER');
	dummy_layer.style.left    = elem.style.left;
	dummy_layer.style.top     = elem.style.top;
	dummy_layer.style.width   = elem.offsetWidth;
	dummy_layer.style.height  = elem.offsetHeight;
	dummy_layer.style.display = 'block';

	return false;

}

function onMouseUp(e) {

	m_dragging = false;
	return false;

}

