
if( typeof util == 'undefined' || !util ) { util={}; };

//-----------------------------------------------------------------------------
// 組み込みのStringを拡張
//-----------------------------------------------------------------------------
String.prototype.trim = function()
{
	return this.replace(/(^\s*)|(\s*$)/g, '');
}

//-----------------------------------------------------------------------------
// 組み込みのArrayを拡張
//-----------------------------------------------------------------------------
Array.prototype.indexOf = function( value )
{
	var i = this.length;
	while( i-- )	if( this[i] == value ) return i;
	return -1;
};
Array.prototype.remove = function( value )
{
	var i = this.length;
	while( i-- )	if( this[i] == value ) this.slice(i,1);
	return value;
};
Array.prototype.contains = function( value )
{
	return 0 >= this.indexOf( value )?true:false;
}


/**
 * 
 * 
 * 
 * 
 */
util.lang = function()
{
	//-------------------------------------------------------------------------
	// メソッド　非公開
	//-------------------------------------------------------------------------
	
	/**
	 * XMLHttpRequestオブジェクトの生成
	 */
	function _createXMLHTTP()
	{
		var result = null;
		
		if( window.ActiveXObject ) {
			try {
				result = new ActiveXObject( 'Msxml2.XMLHTTP' );
			}
			catch( e ) {
				try {
					result = new ActiveXObject( 'Microsoft.XMLHTTP' );
				}
				catch( ee ) {}
			}
		}
		else if( window.XMLHttpRequest ) {
			try {
				result = new XMLHttpRequest();
			}
			catch( e ) {}
		}
		
		return result;
	}
	
	//-------------------------------------------------------------------------
	// メソッド　公開
	//-------------------------------------------------------------------------
	return {
		/**
		 * クラス定義の継承関数
		 */
		extend : function( subclass, superclass, overrides )
		{
			var temp = function() {};
			temp.prototype = superclass.prototype;
			
			subclass.prototype = new temp();
//			subclass.prototype.constructor = subclass;
//			subclass.prototype.superclass             = superclass.prototype;
//			subclass.prototype.superclass.constructor = superclass;
			
			subclass.prototype.constructor   = subclass;
			subclass.superclass               = superclass.prototype;
			superclass.prototype.constructor = superclass;
			
			if( overrides ) {
				for( var i in overrides ) {
					subclass.prototype[i] = overrides[i];
				}
			}
		}
		
		/**
		 * 何も処理をしないイベントハンドラ
		 */
		,invalidEventHandle : function() { return false; }
		
		/**
		 * 範囲選択無効化
		 */
		,disableSelection : function( element )
		{
		    element.onselectstart       = util.lang.invalidEventHandle;
		    element.unselectable        = "on";
		    element.style.MozUserSelect = "none";
		    element.style.cursor        = "default";
		}
		
		,disablePaste :function (element)
		{
			element.onpaste = util.lang.invalidEventHandle;
		}
		
		/**
		 * 半角数字を全角へ変換
		 */
		,_CONST_HANKAKU_NUM : '0123456789:'
		,_CONST_ZENKAKU_NUM : '０１２３４５６７８９：'
		,hanToZenNum : function( hankaku )
		{
			var result = '';
			
			for( var i=0; i<hankaku.length; i++ ) {
				
				var index = this._CONST_HANKAKU_NUM.indexOf(hankaku.charAt(i));
				if( (-1) != index ) {
					result += ( this._CONST_ZENKAKU_NUM.charAt(index) );
				}
			}
			
			return result;
		}
		
		/**
		 * HTTPサービス呼び出し
		 * 　path             XML HTTPサービスのURL(+パラメータ)
		 * 　callBack(p1,p2)　XML HTTPオブジェクト
		 * 　               　p2は状態　0:正常
		 * 　                          -1:XMLHttpRequest生成エラー
		 * 　               　　　　　 -2:通信関連エラー
		 */
		,callServiceForXMLHTTP : function( path, callBack )
		{
			var xmlHttp = _createXMLHTTP();
			if( !xmlHttp ) { callBack(null,-1); return; }
			
			try {
				xmlHttp.open( 'GET', path, true );
				xmlHttp.onreadystatechange = function() {
					if( 4 == xmlHttp.readystate ) {
						if( 200 <= xmlHttp.status && 300 > xmlHttp.status )
							callBack(xmlHttp,0);
						else
							callBack(xmlHttp,-2);
						xmlHttp = null;		// この時点で不要となったため、循環参照を解除する
					}
				};
				dt = new Date;
				xmlHttp.setRequestHeader("If-Modified-Since", dt.toUTCString());
				xmlHttp.send(null);
			}
			catch( e ) {
				xmlHttp.onreadystatechange = function() {};
				callBack(xmlHttp,-2);
			}
		}
		
		/**
		 * 電話番号書式チェックチェック(かなり簡易的)
		 * 
		 * @return nullはNGを表す。
		 */
		,checkTelNo : function( src )
		{
		    return src.match(/^[0-9]+$/);
		}
		
		/**
		 * E-Mailアドレス書式チェック
		 * 
		 * @return nullはNGを表す。
		 */
		,checkMailAddress : function( src )
		{
		    return src.match(/^[A-Za-z0-9]+[\w\.\-_]+@[\w\.\-_]+\w{2,}$/);
		}
		
		/**
		 * 識別コード書式チェック (9999-9999-9999)
		 * 
		 * @return nullはNGを表す。
		 */
		,chkFmtDescCode : function( src )
		{
			var result = src.match(/^\d{4}-\d{4}-\d{4}$/);
			if( !result ) result = src.match(/^\d{12}$/);
			return (result && 0>=result.length)?null:result;
		}
		
		/**
		 * サブミット(POST用)
		 * 
		 * url   : 遷移先のURL
		 * datas : キー名と値の連想配列
		 */
		,doSubmit : function( url, datas )
		{
			var form = document.createElement( 'form' );
			form.method = 'POST';
			form.action = url;
			document.appendChild( form );
			
			try {
				for( var data in datas ) {
					var input = document.createElement( 'input' );
					input.type  = 'hidden';
					input.name  = data;
					input.value = datas[ data ];
					form.appendChild( input );
				}
				
//if( console && console.log ) console.log( 'util.lang.doSubmit url='+url+', param='+datas );
				
				form.submit();
			}
			finally {
				document.removeChild( form );
			}
		}
		
		/**
		 * 文字列変換
		 * 　JavaScriptの文字列をHTML文書で正しく表示できるように文字を変換する。
		 * 　・\　を　&yen;
		 */
		,escapeJS2HTML : function( src )
		{
			return src.replace(/\\/g,'&yen;');
		}
	};
}();

