

function popopen( url , width, height) {

	var features = "location=no, menubar=no, status=no, scrollbars=yes, resizable=yes, toolbar=no";

	if (window.screen.width > width) {
		features += ", left=" + (window.screen.width - width) / 2;
	} else {
		width = window.screen.width;
	}
	features += ", width=" + width;

	if (window.screen.height > height) {
		features += ", top=" + (window.screen.height - height) / 2;
	} else {
		height = window.screen.height;
	}
	features += ", height=" + height;

	return window.open(encodeURI(url), '', features);
}

function replaceAll( _targetStr_, _searchStr_, _replaceStr_ ){ 
	if (_targetStr_) {
		_targetStr_ = _targetStr_.split(_searchStr_).join(_replaceStr_);
	}
	return _targetStr_;
}

function dateStr( src ) {
	return src.replace('-', '年').replace('-', '月').replace(' ', '日');
}

function dateDateStr( src ) {
	return src.split(" ")[0].replace('-', '年').replace('-', '月') + '日';
}

function dateTimeStr( src ) {
	return src.replace('-', '年').replace('-', '月').replace(' ', '日').replace(':', '時').replace(':', '分') + '秒';
}

function tstmp_year( tstmp ) {
	if ( tstmp ) {
		return tstmp.split(" ")[0].split("-")[0];
	}
	return '';
}

function tstmp_month( tstmp ) {
	if ( tstmp ) {
		return tstmp.split(" ")[0].split("-")[1];
	}
	return '';
}

function tstmp_day( tstmp ) {
	if ( tstmp ) {
		return tstmp.split(" ")[0].split("-")[2];
	}
	return '';
}

function tstmp_hour( tstmp ) {
	if ( tstmp ) {
		return tstmp.split(" ")[1].split(":")[0];
	}
	return '';
}

function tstmp_minutes( tstmp ) {
	if ( tstmp ) {
		return tstmp.split(" ")[1].split(":")[1];
	}
	return '';
}

function tstmp_seconds( tstmp ) {
	if ( tstmp ) {
		return tstmp.split(" ")[1].split(":")[2];
	}
	return '';
}

function tstmp_shortdate( tstmp ) {
	var value = '';

	if ( tstmp ) {

		try {
			var word = tstmp.split(" ")[0].split("-");

			var month = word[1];
			if ((month) && (month.length < 1)) {
				month = '0' + month;
			}

			var day = word[2];
			if ((day) && (day.length < 1)) {
				day = '0' + day;
			}

			value = month + '.' + day;
		}
		catch(e) {}
	}

	return value;
}


function toInt( str ) {
	if (str) {
		str = str.replace(/^0/i, "");
		if (str) return parseInt(str);
	}
	return 0;
}


function str2Date( src ) {
	if (src) {
		var word = src.split("-");
		return new Date(toInt( word[0] ), toInt( word[1] ) - 1, toInt( word[2] ));
	}
	return null;
}

function tstmp2Date( src ) {
	if (src) {
		var word = src.split(" ");
		var dayword = word[0].split("-");
		var timeword = word[1].split(":");
		return new Date(toInt( dayword[0] ), toInt( dayword[1] ) - 1, toInt( dayword[2] ),
						toInt( timeword[0] ), toInt( timeword[1] ), toInt( timeword[2] ));
	}
	return null;
}


function birthday2Date( src ) {

	if (!src) return null;

	var today = new Date();
	var year = today.getYear();
	var checkdate = new Date(year, 1, 28);
	checkdate.setTime(checkdate.getTime() + 86400000);	//１日加算
	var uruu = (checkdate.getMonth() == 2);	//うるう年

	var word = src.split("-")
	var month = toInt( word[1] );
	var day = toInt( word[2] );
	if ((!uruu) && (month == 2) && (day == 29)) {
		month = 3;
		day = 1;
	}

	return new Date(year, month - 1, day);
}

function checkDate( yy, mm, dd ) {
	var y, m ,d;
	return ((yy) && (!isNaN(y = toInt(yy))) && (y >= 2000) && (y <= 3000) &&
			(mm) && (!isNaN(m = toInt(mm))) && (m >= 1) && (m <= 12) &&
			(dd) && (!isNaN(d = toInt(dd))) && (d >= 1) && (d <= 31) &&
			(!isNaN(new Date(y, m-1 ,d))) );
}

function compareDateTime( y1, m1, d1, hh1, mm1, ss1, y2, m2, d2, hh2, mm2, ss2 ) {
	var d1 =  new Date(toInt(y1), toInt(m1), toInt(d1), toInt(hh1), toInt(mm1), toInt(ss1));
	var d2 =  new Date(toInt(y2), toInt(m2), toInt(d2), toInt(hh2), toInt(mm2), toInt(ss2));
	return d1-d2;
}

function checkTime( hh, mm, ss ) {
	var h, m ,s;
	return ((hh) && (!isNaN(h = toInt(hh))) && (h >= 0) && (h <= 23) &&
			(mm) && (!isNaN(m = toInt(mm))) && (m >= 0) && (m <= 59) &&
			(ss) && (!isNaN(s = toInt(ss))) && (s >= 0) && (s <= 59));
}

function compareTime( hh1, mm1, ss1, hh2, mm2, ss2 ) {
	var d1 =  new Date(2000, 0, 1, toInt(hh1), toInt(mm1), toInt(ss1));
	var d2 =  new Date(2000, 0, 1, toInt(hh2), toInt(mm2), toInt(ss2));
	return d1-d2;
}

function compareInttime( hh1, mm1, ss1, hh2, mm2, ss2 ) {
	var d1 =  new Date(2000, 0, 1, hh1, mm1, ss1);
	var d2 =  new Date(2000, 0, 1, hh2, mm2, ss2);
	return d1-d2;
}


/*
 * 
 */

function toPreText( str ) {
	return replaceAll(str,"\n","&nbsp;</BR>");
}



function photoLopen( photoLurl ) {
	//window.open(photoLurl, 'photo', 'width=400, height=300, menubar=no, toolbar=no, scrollbars=yes');
	window.open(photoLurl, 'photo', 'menubar=no, toolbar=no, scrollbars=yes');
	return true;
}


function getDictKeys( dict ) {

	var ret = null;

	if (dict) {
		for(key in dict) {
			if (!ret) {
				ret = new Array();
			}
			ret.push(key);
		}
	}

	return ret;
}


/*
 * 写真
 */


function getPhotoFilename(id){

	var frame = $(id);
	var doc = (frame.contentWindow.document) || (frame.contentDocument);

	if (doc) {

		var elm = doc.getElementById('filename');
		if (elm) {
			return elm.value;
		}
	}

	return '';
}


function photoTD( src ) {
	var photoLurl = "../photo/" + src;
	var photoSurl = "../photo/" + src;
	var width = 100;
	var height = 100;

	return "<img src=\"" + photoSurl + "\" width=\"" + width + "\" height=\"" + height + "\" onclick = \"photoLopen(\'" + photoLurl + "\')\"/>";
}


/*
 * hashを文字列
 */
function hashToString(hash) {
	var str = "";

	for (var key in hash) {
		if ( hash[key] ) {

			if ( str ) {
				str += ",";
			}

			str += key + " : " + hash[key];
			//str += "\"" + key + "\" : \"" + hash[key] + "\"";
		}
	}

	return "{ " + str + " }";
}


function escapeApos(str) {
	return replaceAll( str, "'", "\\'");
}

/*
 * arrayをJSON
 */
function arrayToJSON(array) {

	var str = "";
	var idx;
	for(idx=0;idx < array.length;idx++) {
		var val = array[idx];

		if (idx > 0) {
			str += ", ";
		}

		if ((val instanceof Array) || (typeof(val) == [])) {
			str += arrayToJSON(val);
		}
		else if ((val instanceof Object) || (typeof(val) == "object")) {
			str += hashToJSON(val);
		}
		else if ((val instanceof String) || (typeof(val) == "string")) {
			str += "'" + escapeApos(val) + "'";
		} else {
			str += String(val);
		}
	}

	return "[ " + str + " ]";
}


/*
 * hashをJSON
 */
function hashToJSON(hash) {
	var str = "";
	var val;

	for (var key in hash) {

		val = hash[key];
		if ((val != undefined) && (val != null)) {

			if ( str ) {
				str += ",";
			}

			if ((val instanceof Array) || (typeof(val) == [])) {
				str += "'" + key + "' : " + arrayToJSON(val);
			}
			else if ((val instanceof Object) || (typeof(val) == "object")) {
				str += "'" + key + "' : " + hashToJSON(val);
			}
			else if ((val instanceof String) || (typeof(val) == "string")) {
				str += "'" + key + "' : '" + escapeApos(val) + "'";
			}
			else {
				str += "'" + key + "' : " + val;
			}
		}
	}

	return "{ " + str + " }";
}



/*
 * Trim
 */
function trim(str) {
	if ( str ) {
		str = str.replace(/^\s+|\s+$/g, "");
	}
	return str;
}


/*
 * Emailチェック
 */
function checkEmail(str) {

	/* E-mail形式の正規表現パターン */
	/* @が含まれていて、最後が .(ドット)でないなら正しいとする */
	var reg=/[!#-9A-~]+@+[a-z0-9]+.+[^.]$/i;

	return ((str != "") && (str.match(reg)));
}


/*
 * 半角チェック
 */
function checkHankaku(str) {

	/* 半角英数字(0-9)、四則演算子(+-/*)、ピリオド(.)、カンマ(,、:/_~&)のみ */
	var match = str.match(/[0-9a-zA-Z\+\-\/\*\,\.\:\/\_\~\& ]+/g);
	return (match == str);
}


/*
 * 半角数字チェック
 */
function checkNum(str) {
	return  (!str.match(/[^0-9]/g));
}

/*
 * 電話番号チェック
 */
function checkTel(str) {
	return  (!str.match(/[^0-9\-]/g));
}


/*
 * 全角チェック
 */
function checkZenkaku(str) {

	for(var i=0; i<str.length; i++) {
//		var len = escape(str.charAt(i)).length;
		var len = encodeURI(str.charAt(i)).length;
		if (len  < 4) {
//alert(str.charAt(i)+":"+len)
			return false;
		}
	}

	return true;
}

/*
 * 半角数字→数字
 */
function toZenkakuNum(str) {
	var table=['０','１','２','３','４','５','６','７','８','９'];
	var ret="";
	for(var i=0; i<str.length; i++) {
		ret += table[str.charAt(i) - '0'];
	}
	return ret;
}


/*
 * 全角住所→英数字と記号を半角に
 */
function toHankakuAddress(str) {

	var half = 
		"0123456789" + 
		"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
		"abcdefghijklmnopqrstuvwxyz" +
		"---+_@., ";

	var full = 
		"０１２３４５６７８９" +
		"ＡＢＣＤＥＦＧＨＩＪＫＬＭＮＯＰＱＲＳＴＵＶＷＸＹＺ" +
		"ａｂｃｄｅｆｇｈｉｊｋｌｍｎｏｐｑｒｓｔｕｖｗｘｙｚ" +
		"ー‐－＋＿＠．，　";

	var ret="";
	for(var i=0; i<str.length; i++) {
		var c = str.charAt(i);
		var num = full.indexOf(c, 0);
		if (num >= 0) {
			ret += half.charAt(num);
		} else {
			ret += c;
		}
	}

	return ret;
}






//-----------------------------------------------------

function getRadioval(num, tag) {

	var ret = '';
	var idx;

	for(idx=0;idx < num;idx++) {
		var id = tag + '_' + idx;
		if ( $( id ).checked ) {
			ret = $( id ).value;
		}
	}

	return ret;
}



function escapeUnicode(str) {
  return str.replace(/[^ -~]|\\/g, function(m0) {
    var code = m0.charCodeAt(0);
    return '\\u' + ((code < 0x10)? '000' : 
                    (code < 0x100)? '00' :
                    (code < 0x1000)? '0' : '') + code.toString(16);
  });
}


function addMethod(object, name, fn){
    // 指定されたオブジェクトには，
    // 既に同じ名前のメソッドが定義されている
    // かもしれないので，一時変数に保存しておく
    var old = object[ name ];

    // オブジェクトのプロパティに関数定義を上書き．
    object[ name ] = function(){
    // 呼び出しされたメソッドの引数の数と関数に定義されている
    // 引数の数が一致したとき呼び出されたメソッドを適用する．
    if ( fn.length == arguments.length )
        return fn.apply( this, arguments );

    // 呼び出されたメソッドと，定義時の引数の数が
    //一致しなかった場合には，一時保存しておいた
    // 同名のプロパティをチェック．
    // 一時保存されたプロパティが関数オブジェクトだった場合には
    // プロパティに引数を適用 
    else if ( typeof old == 'function' )
        return old.apply( this, arguments );
    };
}


/*
 * 郵便番号から住所を取得
 */
function getAddFromZip( zip ) {

	var url = "/common/address/";
	var reqparam = "json&zip=" + zip;
	var ret = ['', ''];

	new Ajax.Request(
		url,
		{
			"method": "get", 
			"parameters": reqparam + "&request_dummy=" + Math.random(),
			"asynchronous": false,
			onSuccess: function(request) {
				//alert(request.responseText);

				var json; 
				eval("json=" + replaceAll(request.responseText,"\n","\\n")); 

				var add = "";
				if ( json.commonResponse.value1 ) {
					add += json.commonResponse.value1;
				}
				if ( json.commonResponse.value2 ) {
					add += json.commonResponse.value2;
				}
				if ( add ) {
					ret[0] = add;
				}
				if ( json.commonResponse.value3 ) {
					ret[1] = json.commonResponse.value3;
				}

			},
			onComplete: function(request) {},
			onFailure: function(request) {}, 
			onException: function (xmlhttp, e) {} 
		}
	); 

	return ret;
}


/*
 * 住所から緯度経度を取得
 */
function getPosFromAdd( add ) {

	var url = "/common/position/";
	var reqparam = "json&address=" + add;
	var ret = ['', ''];

//	alert(url);

	new Ajax.Request(
		url,
		{
			"method": "get", 
			"parameters": reqparam + "&request_dummy=" + Math.random(),
			"asynchronous": false,
			onSuccess: function(request) {
//				alert(request.responseText);

				var json; 
				eval("json=" + replaceAll(request.responseText,"\n","\\n")); 

				if ( json.commonResponse.value1 ) {
					ret[0] = json.commonResponse.value1;
				}
				if ( json.commonResponse.value2 ) {
					ret[1] = json.commonResponse.value2;
				}

			},
			onComplete: function(request) {},
			onFailure: function(request) {}, 
			onException: function (xmlhttp, e) {/*alert(e.name + ': '+ e.message);*/} 
		}
	); 

	return ret;
}


/*
 * 住所から全国地方公共団体コードを取得
 */
function getAreacodeFromAdd( add ) {

	var url = "/common/areacode/";
	var reqparam = "json&address=" + add;
	var ret = ['', ''];

	new Ajax.Request(
		url,
		{
			"method": "get", 
			"parameters": reqparam + "&request_dummy=" + Math.random(),
			"asynchronous": false,
			onSuccess: function(request) {
				//alert(request.responseText);

				var json; 
				eval("json=" + replaceAll(request.responseText,"\n","\\n")); 

				if ( json.commonResponse.value1 ) {
					ret[0] = json.commonResponse.value1;
					if (ret[0].length >= 2) {
						ret[1] = ret[0].substring(0,2);
					}
				}
			},
			onComplete: function(request) {},
			onFailure: function(request) {}, 
			onException: function (xmlhttp, e) {} 
		}
	); 

	return ret;
}


/*
 * ユーザーの存在確認
 */
function isExistUser( id ) {

	var url = "/user/exist/" + id;
	var reqparam = "json";
	var ret = false;

	new Ajax.Request(
		url,
		{
			"method": "get", 
			"parameters": reqparam + "&request_dummy=" + Math.random(),
			"asynchronous": false,
			onSuccess: function(request) {
				//alert(request.responseText);
				ret = true;
			},
			onComplete: function(request) {},
			onFailure: function(request) {}, 
			onException: function (xmlhttp, e) {} 
		}
	); 

	return ret;
}

function checkNewUser( userid ) {

	var errmsg = "";

	//ユーザーＩＤ(半角英数字)
	if ( ! userid ) {
		errmsg += "・ユーザーＩＤを入力してください。\n";
	}
	else if ( ! checkHankaku(userid) ) {
		errmsg += "・ユーザーＩＤを半角英数字で入力してください。\n";
	}
	else if ( isExistUser(userid) ) {
		errmsg += "・ご指定のユーザーＩＤは既に使用されています。\n";
	}

	return errmsg;
}


/*
 * 各パーツのDisable設定
 */
function setDisable(obj, val) {

	if (!obj.tagName) return;

	try {
		var childlist = obj.childNodes;
	
		if ((childlist) && (childlist.length > 0)) {
			for(var i=0;i < childlist.length;i++) {
				setDisable(childlist[i], val);
			}
		}
	} catch(e) {};

	switch(obj.tagName.toLowerCase()) {

	case 'input':
		if (obj.getAttribute('type')) {
			switch(obj.getAttribute('type').toLowerCase()) {

			case 'text':
				obj.readOnly = val;
/*
				if (val) {
					obj.style.borderWidth = 0;
				} else {
					obj.style.borderWidth = 1;
				}
*/
				break;

			case 'radio':
				obj.readOnly = val;
				if ((val) && (obj.checked)) {
					obj.disabled = false;
				} else {
					obj.disabled = val;
				}
				break;

			default:
				obj.disabled = val;
				break;
			}
		}
		break;

	case 'textarea':
		obj.readOnly = val;
/*
		if (val) {
			obj.style.borderWidth = 0;
		} else {
			obj.style.borderWidth = 1;
		}
*/
		break;

	case 'select':
		obj.disabled = val;
		break;

	case 'iframe':
		var frame = $(obj.id);
		var doc = (frame.contentWindow.document) || (frame.contentDocument);
		if (doc) {
			setDisable(doc, val);
		}

		obj.disabled = val;
		break;
	}
}

function setDisableAll(obj, val) {

	if (!obj.tagName) return;

	try {
		var childlist = obj.childNodes;
	
		if ((childlist) && (childlist.length > 0)) {
			for(var i=0;i < childlist.length;i++) {
				setDisableAll(childlist[i], val);
			}
		}
	} catch(e) {};

	obj.disabled = val;
}

/*
 * URL
 */
function getLandmarkURL(id) {

//	var url = "./spotview.php?id=" + id;
	var url = "/spot/" + id + "/";
	if (location.host == 'localhost') {
		url = './landmarkview.html?id=' + id;
	}

	return url;
}

function getTopicURL(id) {

//	var url = "//topicview.php?id=" + id;
	var url = "/topicview/" + id;
	if (location.host == 'localhost') {
		url = "./topicview.html?id=" + id;
	}

	return url;
}


