	var map=null;//GMAP
	var color = "#FF0000";
	var olType = 0;//線orマーカーの種類
	var points = new Array();
	var linePoly = null;
	//マーカーのレイヤー
	var markers = new Array(5);
	var markersp = new Array(5);
	for(i =0;i<markers.length;i++){
		markers[i] = null;
		markersp[i] = null;
	}
	var sCenter_x = 139.705826;
	var sCenter_y = 35.659481;
	var sZoom	= 2;
	var mType	= G_SATELLITE_MAP;
	var editFlag=0;
	var resizeFlag=1;


//XML 位置情報取得系//////////////////////////////////////////////////// START
function createHttpRequest(){
  //for Win ie
  if(window.ActiveXObject) {
    try {
      // for MSXML2 or later
      return new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        // for old MSXML
        return new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e2) {
        return null;
      }
    }
  } else if(window.XMLHttpRequest) {
    // for other browsers
    return new XMLHttpRequest();
  } else {
    return null;
  }
}
//住所検索
function searchAddr(fileName,frm){

	//値取得
//	var frm = document.getElementById(formName);
	addr = frm.addr.value;
	if(frm.type[0].checked){
		type = frm.type[0].value;
	}else{
		type = frm.type[1].value;
	}

	// create XMLHttpRequest object
	var httpoj = createHttpRequest();
	data = 'addr=' + encodeURI(addr) + '&charset=UTF8' + '&series='+type+'&geosys=world';
	httpoj.open('POST', fileName, true);
	httpoj.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=UTF-8');
	httpoj.onreadystatechange = function() { 
		if (httpoj.readyState==4) {
			if (httpoj.status == 200) { 
				on_loaded(httpoj);
			}else{
				alert("取得エラー");
			}
		}
	}
	httpoj.send(data);
}


// call back function ロードされたときの処理
function on_loaded(oj){
	resxml  = oj.responseXML;
	candidates = resxml.getElementsByTagName('candidate');
	len = candidates.length;

if(len == 0){
		setCandidates("候補なし！！");
	return false;
}

	pnts_x = new Array(len);
	pnts_y = new Array(len);
	pnts_text = new Array(len);
	pnts_ilvl = new Array(len);

	if (len > 0) {
		//検索結果をすべて配列に入れる
		for (idx = 0; idx < len; idx++) {
//			candidate = candidates[idx];
			//各パラメータ取得
//			addr=candidate.getElementsByTagName('address')[0].firstChild.nodeValue;
//			ilvl=candidate.getElementsByTagName('iLvl')[0].firstChild.nodeValue;
//			x=parseFloat(candidate.getElementsByTagName('longitude')[0].firstChild.nodeValue);
//			y=parseFloat(candidate.getElementsByTagName('latitude')[0].firstChild.nodeValue);
//上のやり方だとうまくいかないから
		addr =resxml.getElementsByTagName('candidate')[idx].getElementsByTagName('address')[0].firstChild.nodeValue;
		ilvl =resxml.getElementsByTagName('candidate')[idx].getElementsByTagName('iLvl')[0].firstChild.nodeValue;
		x=parseFloat(resxml.getElementsByTagName('candidate')[idx].getElementsByTagName('longitude')[0].firstChild.nodeValue);
		y=parseFloat(resxml.getElementsByTagName('candidate')[idx].getElementsByTagName('latitude')[0].firstChild.nodeValue);
			pnts_x[idx] = x;
			pnts_y[idx] = y;
			pnts_text[idx] = addr;
			pnts_ilvl[idx] = ilvl;

	    }

		//候補が１つ以上の場合
		if(len>1){
			candidatelist="下記から選択してください<br>";
			for (idx = 0; idx < len; idx++) {
			candidatelist +="<a href=\"javascript:mapCenterAndZoom("+pnts_x[idx]+","+pnts_y[idx]+","+pnts_ilvl[idx]+")\">"+pnts_text[idx]+"</a><br>";
			}
			setCandidates(candidatelist);
		}else if(len=1){
//			clearCandidates()
			mapCenterAndZoom(pnts_x[0],pnts_y[0],pnts_ilvl[0]);
		}else{//候補なし
			

		}
  }
}

//候補情報を出力
function setCandidates(s){
	var d = $('candidate');
	d.innerHTML=s;
}

//XML 位置情報取得系//////////////////////////////////////////////////// END

//地図の移動
function mapCenterAndZoom(x,y,ilvl){
    ilvl = 7 - ilvl;
    if (ilvl < 0) { ilvl = 0; }
	setCandidates("");//クリア
	point = new GPoint(x, y);
	map.centerAndZoom(point, ilvl);

}

//高さをとる？
function getWindowHeight() {
	if (window.self && self.innerHeight) {
		return self.innerHeight;
	}
	if (document.documentElement && document.documentElement.clientHeight) {
		return document.documentElement.clientHeight;
	}
	//ダミー地
	return 800;
}
function getWindowWidth() {
	if (window.self && self.innerWidth) {
		return self.innerWidth-20;//ＦＦの場合の幅調整
	}
	if (document.documentElement && document.documentElement.clientWidth) {
		return document.documentElement.clientWidth;
	}
	//ダミー値
	return 500;
}


//マップを画面にあわせる
function mapResize(){
	mapDivResize();
	if(map != null){
	//	map.onResize();
	}
}
//マップ領域のサイズ調整
function mapDivResize(){
	if(resizeFlag == 1){
		var h = getWindowHeight();
		var w = getWindowHeight();
		if(h >0){
			h = h-130;
		}
		w=getWindowWidth();
		if(w>0){
			w = w-270;
		}
//		var mape = $('map');
		var mape = document.getElementById("map");
		mape.style.height = h + "px";
		mape.style.width = w + "px";
	}
}



//データに保存するよう
function goSubmit(){
	var lineData = "";
	var markersData ="";
	var p = 0;


	for (i=0;i<points.length;i++) {
		lineData+=points[i].x+","+ points[i].y +"\t";
	}

	for (i=0;i<markers.length;i++) {
		mp = markers[i];

		if(mp != null){
			mppoint = mp.getPoint()
			markersData+=i+","+mppoint.x+","+ mppoint.y +"\t";
		}
	}

	

	flag = true;
	if(document.titleForm.title.value == ""){
		alert("タイトルを入力してください");
		flag = false;
	}
	if(document.form1.passwd.value == ""){
		alert("パスワードを入力してください。再編集時に必要になります");
		flag = false;
	}
	if(document.form1.passwd.value.match(/[^A-Za-z0-9]/)){
		alert("パスワードは英数字で入力してください");
		flag = false;
	}

	var mapType = map.getCurrentMapType();


	if(G_NORMAL_MAP == map.getCurrentMapType()){
		mapTypeS ="G_NORMAL_MAP";
	}else if(G_SATELLITE_MAP == map.getCurrentMapType()){
		mapTypeS ="G_SATELLITE_MAP";
	}else if(G_HYBRID_MAP == map.getCurrentMapType()){
		mapTypeS ="G_HYBRID_MAP";
	}else if(G_SATELLITE_TYPE == map.getCurrentMapType()){
		mapTypeS ="G_SATELLITE_TYPE";
	}else if(G_HYBRID_TYPE == map.getCurrentMapType()){
		mapTypeS ="G_HYBRID_TYPE";
	}
	document.form1.mapType.value=mapTypeS;

	LatLngObj = map.getCenterLatLng();
	document.form1.center.value=LatLngObj.x +","+ LatLngObj.y;
	document.form1.zoom.value=map.getZoomLevel();
	document.form1.lineData.value=lineData;
	document.form1.markersData.value=markersData;
	document.form1.title.value=document.titleForm.title.value;

	//markers.length
	if(flag){
		document.form1.submit();
		return true;
	}else{
//		return false;
	}
}



//地図の初期化
function mapInit(){

	//マップのサイズに合わせる
	mapDivResize();

	map = null;
	map = new GMap(document.getElementById("map"));
	map.addControl(new GLargeMapControl());
	map.addControl(new GMapTypeControl());
	
	map.centerAndZoom(new GPoint(sCenter_x,sCenter_y), sZoom);
	map.setMapType(mType);

	if(editFlag){
		addEvent();//イベント追加
	}
	drawLine();
	initDrawMarker();
	writeCoordinates();
}
//イベントの追加
function addEvent(){
	//イベントの追加
	// クリックイベント
	GEvent.addListener(map, "click", function(overlay, point) {

	if(olType == 0){
		if(point != null){


			points.push( point );
			if( points.length > 1 ) {
	if(linePoly!=null){
		map.removeOverlay(linePoly);
	}
				drawLine();
			}
		}
	}else{//Markerの処理
		anum = olType -1;

		if(markers[anum] != null){
			map.removeOverlay(markers[anum]);//マーカー削除
		}
		var icon = getNumIcon(olType);
		markers[anum] = new GMarker(point, icon);
		map.addOverlay(markers[anum]);
	}
	});

	 // 種別の切り替わり
	GEvent.addListener(map, "maptypechanged", function() {
		var maptype = map.getCurrentMapType();
		if( maptype == G_NORMAL_MAP ) {
			color = "#FF0000";
		}
		else {
			color = "#FF0000";
		}

		if( points.length > 1 ) {
			map.removeOverlay(linePoly);
			drawLine();
		}
	});
	//移動時の処理
	GEvent.addListener(map, "moveend", function() {
		writeCoordinates();
    });

}
//中心座標を　書き出す
function writeCoordinates(){
	if(editFlag){
		var center = map.getCenterLatLng();
		var frm = document.getElementById("coordinates");
		frm.x.value = center.x;
		frm.y.value = center.y;
	}
}
function setCoordinates(){
	var frm = document.getElementById("coordinates");
	var x = parseFloat(frm.x.value);
	var y = parseFloat(frm.y.value);
	
	centerAt(x,y);

}
function centerAt(x,y){
	if(!isNaN(x) & !isNaN(y)){
		if(x <= 180 & x >= -180 & y <= 180 & y >= -180){
			map.centerAtLatLng(new GPoint(x,y));
			return;
		}
	}
	alert("座標は適切な値を入力してください");
	return;
}

//番号付きマーカーの生成
function getNumIcon(num){
	var numIcon = new GIcon();
	numIcon.image = "http://chizuz.com/img/icon/icon"+num+".png";
	numIcon.shadow = "http://chizuz.com/img/icon/iconshadow.png";
	numIcon.iconSize = new GSize(20, 35);
	numIcon.shadowSize = new GSize(37, 34);
	numIcon.iconAnchor = new GPoint(10, 35);
	numIcon.infoWindowAnchor = new GPoint(5, 1);
	return numIcon;
}

function drawLine(){

	if(points.length <1){
		return;
	}

	// 線を描く
	linePoly = new GPolyline(points,color,6, 0.6 );
	map.addOverlay(linePoly);


	var dist = 0;
	var p = 0;
	while( p != (points.length - 1) ) {
		dist += Gdistance(points[p], points[++p]);
	}
	dis=document.getElementById("distance");
	if(dis != null){
		dis.innerHTML = (dist/1000) + "km";
	}
}
//ラインをひとつ戻す
function undoLine(){
	if( points.length > 0 ) {
		points.pop();
		map.removeOverlay(linePoly);
		drawLine();
	}
}
//マーカーを描画
function initDrawMarker(){

	for(i=0;i<markers.length;i++){
		if(markersp[i] != null){
				var icon = getNumIcon(i+1);
				markers[i] = new GMarker(new GPoint(markersp[i].x,markersp[i].y), icon);
			map.addOverlay(markers[i]);
		}
	}
}

//ＳＮＤ追加分
function setOverLayType(type){
	olType = type;

	var d = $('navinavi');
	var msg="";
	if(olType == 0){
		msg ="地図をクリックすると線が描かれます。";
	}else{
		msg ="地図をクリックするとマーカー["+olType+"]が描かれます。";
	}
	d.innerHTML=msg;
}


//線　マーカーの削除
function delMarker(num){
	if(num == 0){
		clearLine();
		return fales;
	}
	anum = num -1;
	if(markers[anum]!=null){
		map.removeOverlay(markers[anum]);
		markers[anum] = null;
	}
}
//線を削除
function clearLine() {
	points = new Array();
	if(linePoly!=null){
		map.removeOverlay(linePoly);
	}
	document.getElementById("distance").innerHTML = "0km";
	linePoly = null;
}
//距離を計算
function Gdistance( from , to ) {
 var from_x = from.x * Math.PI / 180;
 var from_y = from.y * Math.PI / 180;
 var to_x   = to.x * Math.PI / 180;
 var to_y   = to.y * Math.PI / 180;
 var deg = Math.sin(from_y) * Math.sin(to_y) + Math.cos(from_y) * Math.cos(to_y) * Math.cos(to_x-from_x);
 var dist = 6378140 * (Math.atan( -deg / Math.sqrt(-deg * deg + 1)) + Math.PI / 2);
 return Math.round(dist);
}