// javascript Document

//Google Map 座標取得
//<![CDATA[

var geocoder = null;
// 地図ボタンを押されると実行されます
function showAddress(f,address) {

	//住所valueをグローバル変数に格納
	address = document.form1.state.value;
	if(document.form1.addr.value)address += document.form1.addr.value;
		address=address.replace(/\s/ig,'');//空白文字の削除
		address=address.replace(/　/ig,'');//全角空白文字の削除
		if(address=="北海道")address +="札幌";
	
	//Google Mpa 初期化
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"));
		// GClientGeocoderを初期化
		geocoder = new GClientGeocoder();
	}

	//地図座標の取得と表示
	if (geocoder) {
		//ジオコーディングのチェックなければ見つかるまでループして住所をとって行く
		addlen = address.length;
		geocoder.getLatLng( address , function(point) {
				
				if(document.getElementById("form2").my_x.value && document.getElementById("form2").my_y.value){
					f.show_x.value = document.getElementById("form2").my_x.value;
					f.show_y.value = document.getElementById("form2").my_y.value;
				}
				else {
					f.show_x.value = point.y;
					f.show_y.value = point.x;
				}
		});	
	}
	
	
}
//]]>
/*********************************************************
 入力チェック
*********************************************************/
function inputChk(f,addr){

	// フラグの初期化
	var flg = false;
	var error_mes = "恐れ入りますが、下記の内容をご確認ください\n\n";

	// 各項目のチェック
	if(f.attribute.selectedIndex == 0){
		error_mes += "・属性をご選択ください。\n\n";flg = true;		
	}
	
	if(!f.hall_name.value){
		error_mes += "・館名を入力してください。\n\n";flg = true;
	}
	
	if(f.state.selectedIndex == 0){
		error_mes += "・都道府県をご選択ください。\n\n";flg = true;		
	}
	
	if(!f.cont_pass.value){
		error_mes += "・画像認証をご記入して下さい。\n\n";flg = true;
	}
	
	var addr1 = document.getElementById("up_img1").value;
	if(addr1 && !addr1.match(/\.(jpg|jpeg)$/i)){
		error_mes += "・サムネイル画像をJPEG形式にしてください。\n\n";flg = true;	
	}
	
	var addr2 = document.getElementById("up_img2").value;
	if(addr2 && !addr2.match(/\.(jpg|jpeg)$/i)){
		error_mes += "・画像をJPEG形式にしてください。\n\n";flg = true;
	}

	// 判定（未入力と不正入力があればアラート表示して再入力を促し、次ページへ進めない）
	if(flg){
		window.alert(error_mes);return false;
	}
	else{
		
		var address=f.state.value;
		if(f.addr.value)address += f.addr.value;
		address=address.replace(/\s/ig,'');//空白文字の削除
		address=address.replace(/　/ig,'');//全角空白文字の削除
		
		showAddress(f,address);

		message = "この内容でよろしいでしょうか？";
		return confirm(message);
	}

}
var map = null;
var geocoder = null;

function ChangeMapForm(){
	
	//地図表示時の住所入力チェック
	if(!document.form1.state.value){alert("都道府県を選択してください。\n");return false;}
	if(!document.form1.addr.value){
		clickret =  confirm("都道府県以下の入力がありません。\nよろしいでしょうか？");
		if(!clickret)return false;
	}
	
	//住所valueをグローバル変数に格納
	address = document.form1.state.value;
	if(document.form1.addr.value)address += document.form1.addr.value;
		address=address.replace(/\s/ig,'');//空白文字の削除
		address=address.replace(/　/ig,'');//全角空白文字の削除
		if(address=="北海道")address +="札幌";
	//地図の表示
	if(document.getElementById("mapdissp").style.display=='none'){
		document.getElementById("mapdissp").style.display='block';
		document.getElementById("footer").style.margin='275px auto 0';
	}
	
	//Google Mpa 初期化
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"));
		//map.setCenter(new GLatLng(35.67431, 139.69082), 13);
		// GClientGeocoderを初期化
		geocoder = new GClientGeocoder();
	
	}

	//地図座標の取得と表示
	if (geocoder) {
		//ジオコーディングのチェックなければ見つかるまでループして住所をとって行く
		addlen = address.length;
		geocoder.getLatLng( address , function(point) {
			//if (point) {

				//コントローラー
				map.addControl(new GLargeMapControl());
				map.addControl(new GMapTypeControl());
				
				//自作アイコンの指定
				/*var icon = new GIcon();
        		icon.image = "../img/common/icon.png";
				icon.iconSize = new GSize(19,32);
        		icon.iconAnchor = new GPoint(19,32);*/
					
				//緯度経度の取得表示
				if(document.getElementById("form1").show_x.value && document.getElementById("form1").show_y.value){
					//編集の場合
					var f1zx = document.getElementById("form1").show_x.value;
					var f1zy = document.getElementById("form1").show_y.value;
					var mypoint = new GLatLng(f1zx,f1zy);
 					map.setCenter(mypoint, 13);
					var marker = new GMarker(mypoint);
					map.addOverlay(marker);
					 
				}else{
					//新規の場合
					map.setCenter(point, 9);
					var marker = new GMarker(point);
					map.addOverlay(marker);
					// marker.openInfoWindowHtml("<div style=\"color:#ff0000;\">" + address + "</div>");
					document.getElementById("form2").my_x.value = point.y;
					document.getElementById("form2").my_y.value = point.x;
				}
				
				
			//}
		});	
	}
	
	GEvent.addListener(map, 'click', function(overlay, point) {
      if (point) {
        document.getElementById("my2_x").innerHTML = point.y;
        document.getElementById("my2_y").innerHTML = point.x;
      }
    });

	
}

function ChangeMyClick(){
	if(document.getElementById("my2_x").innerHTML && document.getElementById("my2_y").innerHTML){
		document.getElementById("form2").my_x.value = document.getElementById("my2_x").innerHTML;
		document.getElementById("form2").my_y.value = document.getElementById("my2_y").innerHTML;
	}
	else{
		window.alert("地図をクリックしてください。");
	}
}

function ChangeMapClose(){
	if(document.getElementById("mapdissp").style.display=='block'){
		document.getElementById("mapdissp").style.display='none';
		document.getElementById("footer").style.margin='100px auto 0';
	}
}


//アーティスト入力
function ECal2(artist_name){
	
	if(artist_name){
      window.opener.document.form1.artist_name.value += artist_name+',';
	}
	else{
	  window.alert('大変申し訳ございません。エラーが発生しました。');
	}
}  
//アーティストクリア
function ResetECAll2(){
      window.opener.document.form1.artist_name.value = '';
	  window.location.reload(true);
}
function ResetEC2(){
	var opv = window.opener.document.form1.artist_name.value;
	var opt = opv.substr(0 , opv.length-1);
	var optindex = opt.lastIndexOf(',');
	if(optindex != -1){
	  window.opener.document.form1.artist_name.value = opt.substr(0,optindex)+',';
	}
	else{
	  window.opener.document.form1.artist_name.value = '';
	}
	
}	 