var job_no;
var initTimerId;
var jobTimerId;
var portNo;
var relayCheckCnt;
var relayCheckTimerId;
var ipAddr;
var baseDir

var checkTimerId;
var check_no;

var tagName = "scs25";
var scs25 = document.getElementById( tagName );

function checkStart() {
    check_no = 0;
    checkTimerId = setInterval( "checkAppletReturn()",1000 );
}
function checkAppletReturn() {
    apl = document.getElementById("CheckJre2");
    var ap_rtn = apl.getReturn();
    // var ap_rtn = document.f.CheckJre2.getReturn();
    if( ap_rtn == 1 ) {
        clearInterval( checkTimerId );
        buttonSet( false );
    } else {
        buttonSet( true );
		if( check_no < 600 ) {
			check_no++;
		} else {
			clearInterval( checkTimerId );
		}
    }
}
function buttonSet( flag ) {
    document.f.loginBtn.disabled = flag;
}


function stopView( chid ) {
	var cmnd_str ="node_command f=9 channel_id=" + chid;
	imgViewLink( chid );
	scs25.execute( cmnd_str );

	changeStep( 2,chid ) ;
}


function setPortNo( pno ) {
	portNo = pno;
}
// init
function initialApp( chid,pno, ipaddr, basedir )
{
	portNo = pno ;
	ipAddr = ipaddr;
	baseDir = basedir;
	if(scs25==null){
		scs25=document.getElementById( tagName );
	}
	// アプレット内で Sleep して、初期化完了時にtrue がくる。    // false　はエラー
	// 引数：   タイムアウト時間(ms）
	if( scs25.waitExecuteEnable( 3000 ) ) {

		setHardId();
		if( scs25.waitExecuteEnable( 3000 ) ) {

			setXml();
			changeStep( 2,chid ) ;
			return true;
		} else {
			checkError( 'ESCS001'  );
			return false;
		}
	} else {
		checkError( 'init'  );
		return false;
	}
}
// みるボタンクリック
function viewApp( chid )
{
	if( scs25.waitExecuteEnable( 3000 ) ) {
		if( scs25.waitExecuteEnable( 3000 ) ) {
			step1View( chid ,true );
			relayCheckCnt = 0;
		} else {
			checkError( 'ESCS003'  );
		}
	} else {
        checkError( 'ESCS002'  );
    }
}


function checkError( dlgmsg ) {
	if( scs25.hasError() ) {
		window.alert( dlgmsg  + " error!\n" + scs25.getErrorMessage() );
	} else {
		window.alert( dlgmsg + " timeout!" );
	}
}

function relayDisplay() {
	var flag = false;
	if( relayCheckCnt <  6 ) {
		flag = scs25.isRelayEnable();
		relayCheckCnt++;
	} else {
		flag = scs25.isRelayEnable();
//		clearInterval( relayCheckTimerId );
	}
	if( flag ) {
		document.f.relay.value = "中継できます。";
	} else {
		document.f.relay.value = "中継できません。";
	}
}


var hid;
// set hard_id
function setHardId( ) {
	var now = new Date();
	hid = now.getTime();
	
	var cmnd_str ="node_command f=1 hard_id=" + hid;
	scs25.execute( cmnd_str );
}



function setXml()
{
	var xml_str = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n";
	xml_str += "<cross-domain-policy>\n";
	xml_str += "	<allow-access-from domain='*' to-ports=\"19808\" />\n";
	xml_str += "</cross-domain-policy>";
	scs25.setCrossDomainXML( xml_str );
}




// step change
function changeStep( mode,chid )
{
		switch( mode ) {
		case 1:
			document.getElementById("step1").style.display = "block";
			document.getElementById("step2").style.display = "none";
			document.getElementById("step3").style.display = "none";
			document.getElementById("step4").style.display = "none";
			document.getElementById("step5").style.display = "none";
			break;
		case 2:
			document.getElementById("step1").style.display = "none";
			document.getElementById("step2").style.display = "block";
			document.getElementById("step3").style.display = "none";
			document.getElementById("step4").style.display = "none";
			document.getElementById("step5").style.display = "none";
			break;
		case 3:
			document.getElementById("step1").style.display = "none";
			document.getElementById("step2").style.display = "none";
			document.getElementById("step3").style.display = "block";
			document.getElementById("step4").style.display = "none";
			document.getElementById("step5").style.display = "none";
			break;
		case 4:
			document.getElementById("step1").style.display = "none";
			document.getElementById("step2").style.display = "none";
			document.getElementById("step3").style.display = "none";
			document.getElementById("step4").style.display = "block";
			document.getElementById("step5").style.display = "none";
			break;
		case 5:
			document.getElementById("step1").style.display = "none";
			document.getElementById("step2").style.display = "none";
			document.getElementById("step3").style.display = "none";
			document.getElementById("step4").style.display = "none";
			document.getElementById("step5").style.display = "block";
			break;
		}
}

var timerID;
var status_cnt;
var now_chid;

// view
function step1View( chid,flag )
{
	changeStep( 3,chid ) ;

	var hn = document.f.hname.value;
	if( hn == "" ) {
		hn = 'applet';
	}
	viewAndRelay( chid,hn,flag );
	status_cnt = 0;
	timerId = setInterval( "checkConnect()", 400 );
}
// view and relay
function viewAndRelay( chid,hname,flag ) 
{
	var hn = hname;
	now_chid = chid;
	var cmnd_str;

    if( ipAddr == "" ) {
		cmnd_str ="node_command f=8 hard_id=" + hid + " usr_id=0 channel_id=" + chid + " public_level=" + flag + " handle_name=" + hn;
    } else {
  		cmnd_str ="node_command f=8 hard_id=" + hid + " usr_id=0 channel_id=" + chid + " public_level=" + flag + " handle_name=" + hn + " target=" + ipAddr ;
    }

	scs25.execute( cmnd_str );
}

// connect
function checkConnect() {
	//if( status_cnt > 1 ) {
	if( status_cnt > 2 ) {
		changeStep( 4,now_chid ) ;
	}
	var rtn_cd = scs25.isWmpReady();
	if( rtn_cd == 1 ) {

		// timer stop
		clearInterval( timerId );


		// wmp
		wmpViewLink( now_chid );

		changeStep( 5,now_chid );

		return true;

	} else {
		// error?!
		if( scs25.hasError() ) {
			window.alert( "Connect error!\n" + scs25.getErrorMessage() );

			// timer stop
			clearInterval( timerId );
			
			return false;
		}
		status_cnt++;
	}
}


// topology <-> BBS 
function rviewLink( url )
{
    if( url != "") {
        brw_rview.location.href=url;
    }
}

// img -> wmp
function wmpViewLink( chid ) 
{
	brw_wmp.location.href=baseDir+"brw_wmp.php?channel_id=" + chid ;
}

// wmp -> img
function imgViewLink( chid, base_dir ) 
{
	brw_wmp.location.href=baseDir+"brw_img.php?channel_id=" + chid ;
}


function AllStop()
{
	scs25.stop();

}





// HTTP通信用、共通関数
function createXMLHttpRequest( )
{
	var XMLhttpObject = null;
	try{
		XMLhttpObject = new XMLHttpRequest();
	}catch(e){
		try{
			XMLhttpObject = new ActiveXObject("Msxml2.XMLHTTP");
		}catch(e){
			try{
				XMLhttpObject = new ActiveXObject("Microsoft.XMLHTTP");
			}catch(e){
				return null;
			}
		}
	}
	// if (XMLhttpObject) XMLhttpObject.onreadystatechange = cbFunc;
	return XMLhttpObject;
}


