//{+netform"<form>によるGETとPOST"(動的に生成したフォームによるGETとPOST。)[+net,+ninjaiframe]

var X_FormSender_errorTimerID, X_FormSender_onloadCount = 0;

X_TEMP.X_FormSender_init = function(){
	X_FormSender = X_Class_override( X_NinjaIframe(), X_TEMP.X_FormSender_params );
	
	delete X_TEMP.X_FormSender_init;
	delete X_TEMP.X_FormSender_params;
	
	return X_FormSender;
};

/*
 * form 構築時に "><script> といった文字列の挿入を禁止するために " を エスケープする
 * TODO 改行文字を消す
 */
function X_FormSender_escapeQuote( str ){
	
	return X_String_toChrReferance( str );
};

X_TEMP.X_FormSender_params = {
	
			_busy     : false,
			_canceled : false,
			
			timeout   : 1000,
			isJump    : false, // ページを離脱するか？
			
			load : function( option ){
				//createURL
				var params  = option[ 'params' ] || {},
					target  = option[ 'target' ],
					html, k;
					
				target = target === '_self' ? '_parent' : target === '_blank' ? '_self' : target || '_self',
				html   = [
					'<form method="', X_FormSender_escapeQuote( option[ 'method' ] || 'GET' ), 
						'" action="', X_FormSender_escapeQuote( option[ 'url' ] || '' ), 
						'" target="', X_FormSender_escapeQuote( target ),
						'">' ];
				
				if( target === '_top' || target === '_parent' ) X_FormSender.isJump = true;
				if( 0 <= option[ 'timeout' ] ) X_FormSender.timeout = option[ 'timeout' ];
				
				for( k in params ){
					// TODO 使用すべきでない name
					html.push( '<input type="hidden" name="', X_FormSender_escapeQuote( k ), '" value="', X_FormSender_escapeQuote( params[ k ] || '' ), '">' );
					// TODO 改行を含む text には textarea
				};
				
				html.push( '</form><script>document.forms[0].submit();</script>' );
				
				X_FormSender
					[ 'refresh' ]( html.join( '' ) )
					[ 'listen' ]( [ 'ninjaload', 'ninjaerror' ], X_FormSender_iframeListener );
							
				X_FormSender._busy = true;
			},
			
			cancel : function(){
				X_FormSender.reset();
				X_FormSender._canceled = true;
			},
			
			reset : function(){
				X_FormSender._busy = X_FormSender._canceled = false;
				X_FormSender[ 'unlisten' ]( [ 'ninjaload', 'ninjaerror' ], X_FormSender_iframeListener );
				X_FormSender[ 'refresh' ]( '' );
				X_FormSender_errorTimerID && X_Timer_remove( X_FormSender_errorTimerID );
				X_FormSender_errorTimerID = X_FormSender_onloadCount = 0;
			}
		};

function X_FormSender_iframeListener( e ){
	var idoc;
	
	switch( e.type ){
		case 'ninjaload' :
			if( X_FormSender.isJump ){
				return;
			};
			
			if( ++X_FormSender_onloadCount === 1 ){
				X_FormSender_errorTimerID = X_FormSender[ 'asyncDispatch' ]( X_FormSender.timeout, { type : X_EVENT_ERROR, 'timeout' : true } );

				// TODO レスポンスの html にアクセスしたい場合
				// TODO samedomain or xiframe-sender
				
				idoc = this[ '_rawObject' ].contentDocument || this._iwin.document,
				
				X_FormSender[ 'asyncDispatch' ]( { type : X_EVENT_SUCCESS, response : idoc && idoc.body ? idoc.body.innerHTML : '' } );
			};
			break;
		case 'ninjaerror' :
			console.log( 'iframe onerror' );
			X_FormSender[ 'asyncDispatch' ]( X_EVENT_ERROR );
			break;
	};
	return X_CALLBACK_UN_LISTEN;
};

//}+netform