//<SCRIPT TYPE="text/javascript" SRC="adQlib.js"></SCRIPT>

//###############################################################
//
//	外部組み込み
//
//###############################################################
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


//###############################################################
//
//	汎用
//
//###############################################################
//---------------------------------------------------------------
//	イメージを入れ替える
//---------------------------------------------------------------
function changeImg( image, target ) {
	document.getElementById(target).src = image;
}

//---------------------------------------------------------------
//	イメージを入れ替える
//---------------------------------------------------------------
function changeImgAndSize( image, target, width, height ) {
	document.getElementById(target).src		= image;
	document.getElementById(target).width	= width;
	document.getElementById(target).height	= height;
}

//---------------------------------------------------------------
//	チェックボックによる表示制御
//---------------------------------------------------------------
function CB_Icontrol( CBName, viewArea ){

	if(CBName.checked){
		document.all(viewArea).style.visibility = 'visible';

	} else {
		document.all(viewArea).style.visibility = 'hidden';
	}

}

//---------------------------------------------------------------
//	チェックボックによる移動
//---------------------------------------------------------------
function CB_moveURL( CBName, moveURL1, moveURL2 ){

	if(CBName.checked){
		document.location = moveURL1;  

	} else {
		document.location = moveURL2; 
	}

}

//---------------------------------------------------------------
//	利用規約チェックボック
//---------------------------------------------------------------
function CB_agreement(CBName){

	if( CBName.checked ){
		bt = '<A href="prov_entry.php">';
		bt += '<IMG src="images/bt_prov_entry.gif" width="138" height="36" alt="登録する"></A><BR><BR>'; 
		document.getElementById('link_prov_entry').innerHTML = bt; 

	} else {
		document.getElementById('link_prov_entry').innerHTML = ''; 
	}

}

//###############################################################
//
//	郵便番号変換
//
//###############################################################
//---------------------------------------------------------------
//	郵便番号>住所変換
//---------------------------------------------------------------
function connect_zip2add() {

	url		= 'zip2add.php';
	data	= 'zip=' + document.form.pro_zip.value;
	options	= { method: 'post', parameters: data, onComplete: write_zip2add };

	new Ajax.Request( url, options );
}


function write_zip2add(myRequest) {
	res = myRequest.responseText.split(",");

	if( res[0]!='0' ){
		document.form.pro_preID.value	= res[1]
		document.form.pro_address.value = res[2]

	} else {
		document.getElementById('error').innerHTML = 'error:'+res[1]+'<BR>';
	}
	//document.getElementById('message').innerHTML = res[0] + ':' + res[1] + ':' + res[2];
}

//---------------------------------------------------------------
//	住所>郵便番号変換
//---------------------------------------------------------------
function connect_add2zip() {

	url		= 'zip2add.php';
	data	= 'preID=' + document.form.pro_preID.value;
	data	+= '&address=' + document.form.pro_address.value;

	options	= { method: 'post', parameters: data, onComplete: write_add2zip };

	new Ajax.Request( url, options );
}

function write_add2zip(myRequest){

	res = myRequest.responseText.split(",");

	if( res[0]!='0' ){
  		document.form.pro_zip.value		= res[1];

	} else {
		document.getElementById('error').innerHTML = 'error:'+res[1]+'<BR>';
	}

}


//---------------------------------------------------------------
//	郵便番号>住所変換　２
//---------------------------------------------------------------
function connect_zip2add2() {

	url		= 'zip2add.php';
	data	= 'zip=' + document.form.zip.value;
	options	= { method: 'post', parameters: data, onComplete: write_zip2add2 };

	new Ajax.Request( url, options );
}


function write_zip2add2(myRequest) {
	res = myRequest.responseText.split(",");

	if( res[0]!='0' ){
		document.form.preID.value	= res[1]
		document.form.address.value = res[2]

	} else {
		document.getElementById('error').innerHTML = 'error:'+res[1]+'<BR>';
	}
	//document.getElementById('message').innerHTML = res[0] + ':' + res[1] + ':' + res[2];
}

//---------------------------------------------------------------
//	住所>郵便番号変換　２
//---------------------------------------------------------------
function connect_add2zip2() {

	url		= 'zip2add.php';
	data	= 'preID=' + document.form.preID.value;
	data	+= '&address=' + document.form.address.value;

	options	= { method: 'post', parameters: data, onComplete: write_add2zip2 };

	new Ajax.Request( url, options );
}

function write_add2zip2(myRequest){

	res = myRequest.responseText.split(",");

	if( res[0]!='0' ){
  		document.form.zip.value		= res[1];

	} else {
		document.getElementById('error').innerHTML = 'error:'+res[1]+'<BR>';
	}

}

//###############################################################
//
//	GoogleMap
//
//###############################################################
//---------------------------------------------------------------
//	緯度経度のプレビュー画面を開く
//---------------------------------------------------------------
function Open_previewMap(){
	var lon = document.form.map_longitude.value;
	var lat = document.form.map_latitude.value;
	Win = window.open( 'preview_map.php?lon='+lon+'&lat='+lat, 'previewMap', 'width=386,height=376' );
}


//---------------------------------------------------------------
//	緯度経度の調査画面を開く
//---------------------------------------------------------------
function Open_exCoordinate(){
	var lon = document.form.map_longitude.value;
	var lat = document.form.map_latitude.value;
	Win = window.open( 'examine_coordinate.php?lon='+lon+'&lat='+lat, 'exCoordinate', 'width=420,height=500' );
}
// http://localhost/okugaikoukoku.com/member/edit_property.php?lon=35.7&lat=139

//---------------------------------------------------------------
//	Google Map の表示
//---------------------------------------------------------------
function googlemap_load(lat,lon) {

	if (GBrowserIsCompatible()) {

		var map = new GMap2(document.getElementById("map"));
		var point = new GLatLng(lat,lon);

		map.setCenter(point, 14);
		map.addControl(new GSmallZoomControl());
		map.addControl(new GMapTypeControl());	// タイプコントローラー
		map.disableDragging();
		
		// map.addControl(new GLargeMapControl()); // コントローラー


		var marker = new GMarker(point);
		map.addOverlay(marker);

	}
}


//---------------------------------------------------------------
//	Google Map をつかって緯度、経度を表示する
//---------------------------------------------------------------
function googlemap_exCoordinate_load(lat,lon) {
	if (GBrowserIsCompatible()) {
		var map = new GMap2(document.getElementById('map'));
		var point = new GLatLng(lat,lon);

		map.setCenter(point, 14);
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());

	   GEvent.addListener( map, 'click', function( overlay, point ) {
			if (point) {
				document.form.map_latitude.value	= point.y;
				document.form.map_longitude.value	= point.x;
			}
		});
	}
}

//---------------------------------------------------------------
//	住所を経度緯度に変換
//---------------------------------------------------------------
function searchPoint(){

	var address = ID2pre( document.form.pro_preID.value, 1 ) + document.form.pro_address.value;
	document.getElementById('message').innerHTML = '';

	gGeo = new GClientGeocoder();
	gGeo.getLatLng( address, checkLatLng ); 

	function checkLatLng(point){
		if( point ){
			googlemap_exCoordinate_load(point.y,point.x)
			document.form.map_latitude.value	= point.y;
			document.form.map_longitude.value	= point.x;

		} else {
			document.getElementById('message').innerHTML = 'Error:住所->緯度経度の変換に失敗しました';
			return;

		}
	}
}


//---------------------------------------------------------------
//	経度緯度を住所に変換
//---------------------------------------------------------------
function searchAddress(){

	var latlng = document.form.map_latitude.value + ',' + document.form.map_longitude.value;

	document.getElementById('message').innerHTML = '';

	gGeo = new GClientGeocoder();
	gGeo.getLocations( latlng, checkAddress );

	function checkAddress(res){
		if( res.Status.code != 200 ){
			document.getElementById('message').innerHTML = 'Error:緯度経度->住所の変換に失敗しました';
			return;

		} else {
			var address = res.Placemark[0].address;

			// document.getElementById('message').innerHTML += address + "<BR>\n";

			address		= address.replace( /^日本/gi, '' );

			var add1 = address.match(/^.*[都|道|府|県]/i);
			var add2 = address.replace(/^.*[都|道|府|県]/gi, '' );

			var preID = ID2pre(add1,2);

			document.form.pro_preID.value	= preID;
			document.form.pro_address.value = add2;

		}
	}
}


//---------------------------------------------------------------
//	preIDから県を帰す
//---------------------------------------------------------------
function test(){

	document.getElementById('message').innerHTML = '';

	var str = '日本東京都新宿区榎町７７−１０';


	str = str.replace(/^日本/gi, '' )+ "<BR>\n";

	var add1 = str.match(/^.*[都|道|府|県]/i);
	var add2 = str.replace(/^.*[都|道|府|県]/gi, '' );
	var add3 = ID2pre(add1,2);

	document.getElementById('message').innerHTML += add1 + "<BR>\n";
	document.getElementById('message').innerHTML += add2 + "<BR>\n";
	document.getElementById('message').innerHTML += add3 + "<BR>\n";

	document.getElementById('message').innerHTML += str + "<BR>\n";

}



//---------------------------------------------------------------
//	IDから県名　相互変換
//---------------------------------------------------------------
function ID2pre(pre,mode){

	Prefecture = new Array(100);
	Prefecture[1] = '北海道';
	Prefecture[2] = '青森県';
	Prefecture[3] = '岩手県';
	Prefecture[4] = '宮城県';
	Prefecture[5] = '秋田県';
	Prefecture[6] = '山形県';
	Prefecture[7] = '福島県';
	Prefecture[8] = '茨城県';

	Prefecture[9] = '栃木県';
	Prefecture[10] = '群馬県';
	Prefecture[11] = '埼玉県';
	Prefecture[12] = '千葉県';
	Prefecture[13] = '東京都';
	Prefecture[14] = '神奈川県';
	Prefecture[15] = '新潟県';
	Prefecture[16] = '富山県';
	Prefecture[17] = '石川県';

	Prefecture[18] = '福井県';
	Prefecture[19] = '山梨県';
	Prefecture[20] = '長野県';
	Prefecture[21] = '岐阜県';
	Prefecture[22] = '静岡県';
	Prefecture[23] = '愛知県';
	Prefecture[24] = '三重県';
	Prefecture[25] = '滋賀県';
	Prefecture[26] = '京都府';

	Prefecture[27] = '大阪府';
	Prefecture[28] = '兵庫県';
	Prefecture[29] = '奈良県';
	Prefecture[30] = '和歌山県';
	Prefecture[31] = '鳥取県';
	Prefecture[32] = '島根県';
	Prefecture[33] = '岡山県';
	Prefecture[34] = '広島県';
	Prefecture[35] = '山口県';

	Prefecture[36] = '徳島県';
	Prefecture[37] = '香川県';
	Prefecture[38] = '愛媛県';
	Prefecture[39] = '高知県';
	Prefecture[40] = '福岡県';
	Prefecture[41] = '佐賀県';
	Prefecture[42] = '長崎県';
	Prefecture[43] = '熊本県';
	Prefecture[44] = '大分県';

	Prefecture[45] = '宮崎県';
	Prefecture[46] = '鹿児島県';
	Prefecture[47] = '沖縄県';
	Prefecture[99] = 'ネットワーク';

	if(mode==1){
		return Prefecture[pre];

	} else if(mode==2){
		for(n=1;n<100;n++){
			if( pre == Prefecture[n] ){
				return n;
				break;
			}
		}
		return 0;
	}

}



