
// pQuizId, pLevel 可省略
function Connect6(pId, pLeft, pTop, pQuizId, pLevel)
{
    // alert(pId);
    this.istartX = 23; // 棋盤起始X坐標28,一格23半格11, new version, 棋盤起始X坐標34,一格23半格11  => 34-11=23
    this.istartY = 25; // 棋盤起始Y坐標29,              new version, 棋盤起始Y坐標36,一格23半格11  => 36-11=25
    this.diffX = 24;   // 相對距離
    this.diffY = 24;
    this.mv = new Array();
    this.originmv = '';
    this.format = '';
    this.filter = 0;  //number limits, default is no limit
    this.box = -1;    //mark1
    this.box1 = -1;   //mark2
    this.timer = 0;
    this.moveValue = 0;  //當前下到第幾手
    this.boxValue = 1;
    this.numValue = 1;
    this.boardID = pId;
    this.piecesHTML = '';
    this.boardObj = document.getElementById("board"+this.boardID);
    this.left = pLeft;
    this.top = pTop;
    this.sPm;
    this.controlObj = document.getElementById("control"+this.boardID);
    this.debug = 0;

		this.B_T=0;
		this.B_L3=0;
		this.B_D3=0;
		this.B_L2=0;
		this.W_T=0;
		this.W_L3=0;
		this.W_D3=0;
		this.W_L2=0;
	
    this.drawlines = new Array();	
	this.messageObj = document.getElementById("message"+this.boardID);
	this.messages = "";
	this.QuizId = pQuizId;
	this.QuizLevel = pLevel;
	this.ThreatsCount; // 計算 Threats 個數
	
	this.jg;
	if (document.getElementById("threats"+this.boardID)!=undefined) {
		this.jg = new jsGraphics("threats" + this.boardID);
	}
	this.exitquiz = false; 
	
	this.showThreats = false;
	this.showLive3 = false;
	this.showDead3 = false;
	this.showLive2 = false;
	this.showlinecolor = 3; // 3 means all color draw line, 1->black, 2->white

	this.lgcount = 0;
	this.lgmove = new Array();
	this.lgrotatelog = new Array();
	
	// time serial
	this.mv_t = new Array(); 
	this.serialObj = document.getElementById("serial"+this.boardID);
	this.showSerial = false;
 	
	this.setCookie = function setCookie(c_name, value, expiredays)
	{
		var exdate = new Date()
		exdate.setTime(exdate.getTime() + (expiredays * 24 * 3600 * 1000))
		document.cookie = c_name + "=" + escape(value) +
		((expiredays == null) ? "" : "; expires=" + exdate)
	}
	
	this.getCookie = function getCookie(c_name)
	{
		if (document.cookie.length > 0) {
			c_start = document.cookie.indexOf(c_name + "=")
			if (c_start != -1) {
				c_start = c_start + c_name.length + 1
				c_end = document.cookie.indexOf(";", c_start)
				if (c_end == -1) 
					c_end = document.cookie.length
				return unescape(document.cookie.substring(c_start, c_end))
			}
		}
		return null
	}
	
	this.checkCookie = function checkCookie()
	{
		var temp = this.getCookie('T');
		if ( temp== '1')  this.showThreats = true;
		else this.showThreats = false;
		
		if (this.getCookie('L3') == '1')  this.showLive3 = true;
		else this.showLive3 = false;
		
		if (this.getCookie('D3') == '1')  this.showDead3 = true;
		else this.showDead3 = false;
		
		if (this.getCookie('L2') == '1')  this.showLive2 = true;
		else this.showLive2 = false;
		
		// line color
		if (this.getCookie('LC') != null)  this.showlinecolor = this.getCookie('LC');
		else this.showlinecolor=3;
		if (this.showlinecolor==0) this.showlinecolor=3;
		
		this.switchLines(0);
		this.linecontrol(this.showlinecolor);
	}
	
    this.init = function init()
    {
    	// 顯示在 panel 左下角上面的文字
        this.piecesHTML += '<div id="step" class="step">';
        this.piecesHTML +=  Math.floor(((this.moveValue+1)/2)) + "/"+Math.floor(((this.mvLen+1)/2));
        this.piecesHTML += '</div>';
    } //  當前手數/總共手數

    this.initPiece = function initPiece()
    {
        var start = new Array(); // (Role，X，Y)
        start[start.length] = new Array(0 , 4, 0);
        start[start.length] = new Array(1 , 4, 1);
        return start;
    }

	// none -> 沒外框, block -> 有外框
    this.move = function move(i, j, k) // i for x, j for y, k for piece
    {
        var stepNo = (Math.floor((k+1)/2)+1);
        var left = (this.istartX + this.diffX*(i))+"px";
        var top = (this.istartY + this.diffY*(j))+"px";
        this.piecesHTML += ' <div id="piece'+k+'" ';
        if(k == 362 || k == 363)
            this.piecesHTML += ' class="box" ';
        else if(k%4 == 1 || k%4 == 2)
            this.piecesHTML += ' class="piece pieceW" ';
        else
            this.piecesHTML += ' class="piece pieceB" ';
        this.piecesHTML += ' style="left:'+left+';top:'+top+';"> ';
        if(this.numValue == 1 && k <= 361 && stepNo > this.filter) this.piecesHTML += stepNo;
        this.piecesHTML += '</div>';
    }

	this.feedLine = function feedLine(x1, y1, x2, y2, type) {
		this.drawlines.push(new Array(x1, 18-y1, x2, 18-y2, type)); 
	}
	
	this.mouseClicked = function mouseClicked(event, lgmode, lggid)
    {
    	x=event.clientX;
    	y=event.clientY;
    	if(x>=28 && y>=28 && x<=483 && y<=483){
    	    var samePos = 0;
    	    var posX = parseInt((x-30)/24);
    	    var posY = parseInt((y-30)/24);
    	    var comString = posX + '-' + posY;
    	    
    	    for(i = 0 ; i < this.moveValue ; i++)
    	    {
    	    	if(this.mv[i] == comString){
    	    	    samePos = 1;
    	    	    break;
    	    	}   
    	    }
    	    
    	    if(samePos == 0)
    	    {
    	    	this.mv = this.mv.slice(0,this.moveValue);
    	    	this.mv.push(posX + '-' + posY);
    	        this.moveValue = this.moveValue+1;
    	        this.mvLen = this.moveValue;
    	        this.goMove(1000);
				
				if (lgmode==1) 
				{
					this.lgcount += 1;
					this.lgmove.push(new Array(posX, 18-posY));
					// this.lgmove += this.toUpperChar(posX);
					// this.lgmove += this.toUpperChar(18-posY);
					
					if (this.lgcount>=2)
					{
						    // redirect!!
						    link_url = "http://www.littlegolem.net/jsp/game/game.jsp?gid=";
        					link_url += lggid;
							link_url += "&move="
							
							var length = this.lgmove.length;
							var movestr = "";
							for (var i=0; i<length; i++)
							{
								var temp = this.lgmove.pop();
								// restore the position.
								for (var j=this.lgrotatelog.length-1; j>=0; j--)
								{
									switch (this.lgrotatelog[j])
									{
										case 2:
										  temp = this.getPos(2,temp);
										break;
										case 1:
										  temp = this.getPos(1,temp);
										break;
										case 3:
										  temp = this.getPos(3,temp);
										break;
										case 4:
										  temp = this.getPos(4,temp);
										break;
									}
								}
								
								
								movestr += this.toUpperChar(temp[0]);
								movestr += this.toUpperChar(temp[1]);
							}
							link_url += movestr.toLocaleLowerCase();
        					
        					// window.open(redirect_url,'GenerateGameRecord','width=800,height=600,toolbar=1,menubar=1,scrollbars=1,location=1,status=1,resizable=1');
							window.open(link_url, '_top');
					}
				}
    	    }
    	}    	
    }

    this.moveP = function moveP(src, dst, i)
    {
        this.move(src, dst, i);
    }

    this.start = function start()
    {
        this.piecesHTML = '';
    }

    this.load = function load(da,format,origin,mirror,filter)
    {
    	// da 為棋譜
    	// alert(da);
        this.mv = new Array();
        this.originmv = origin;//original input
        this.format = format;//input format type
        if (filter>0)
        	this.filter = filter;//limit number after it
        this.sPm = da;
        this.pm = da.split(';');
        var t = [];
        for(i = 0; i < this.pm.length; i++)
        {
            t = [];
            var a = this.pm[i].split('');
            if (a.length != 0)
            {
                t[0] = a[2].charCodeAt(0) - 97;
                t[1] = '-';
                t[2] = 18 - (a[3].charCodeAt(0) - 97);
                this.mv[i - 1] = t.join('');
				
				// get time serial.
				if ((i-1)%2==0)
				{
					if (a[5]=='C' || a[5]=='c')
					{
						var b = ""; // time
						for (j=7; j<a.length; j++)
						{
							if (a[j]==']') break;
							b += a[j];
						}
						this.mv_t.push(b);
					}	else this.mv_t.push("&nbsp;"); // no comment.
				} 
				
            }
        }
		
        this.mvLen = this.mv.length;
		// this.goMove(1000);
		if (mirror == 1)
        {
        	this.rotate(2);//goMove first, then rotate mirror
        	this.mirror = 1;
        }

		this.controlObj.innerHTML = this.controlline();
        this.controlObj.style.left = this.left;
        this.controlObj.style.top = this.top;
		
		// check cookie first.
		this.checkCookie();
    }

    this.goMove = function goMove(offset)
    {
        if(this.timer != 0)  this.play(); // 自動播放
        this.gotoMove(offset); // 不然就是走 offset 步
        this.boardObj.innerHTML = this.piecesHTML;
        this.boardObj.style.left = this.left;
        this.boardObj.style.top = this.top;
		
		if (!this.exitquiz) this.drawQuiz();
		if (this.isSomelinesShow()) {
			this.drawLinesFunc();
		}
		
		this.mark();
    }

    this.gotoMove = function gotoMove(offset) // 走 offset 步
    {
        var cur = parseInt(this.moveValue); // 即是資料庫上的第幾步
        if(cur>=1) offset*=2;
        else {
           offset=(offset-1)*2+1;
        }

        var n = cur + offset;
        if(this.filter >0 && (this.filter*2-1) > n)
        {
        	n = (this.filter * 2) -1;
        	if(this.timer != 0) this.play();
        }
        
        else if(this.filter == 0 && n < 1 || offset == -1000)
        {
            n = 0;
            if(this.timer != 0) this.play();
        }
        if(n > this.mvLen || offset == 1000)
        {
            n = this.mvLen;
            if(this.timer != 0) this.play();
        }
        if(n == 0) this.box = this.box1 = -1;
        this.start(); // 每次都要全跑一次
        this.moveValue = n;
        for(i = 0; i < n; i++)
        {
            var m = this.mv[i].split('-');   //the form is like 9-9
            var src = parseInt(m[0]);
            var dst = parseInt(m[1]);
            this.box = this.mv[i];    // mark1
			this.box1 = this.mv[i-1]; //mark2
            if (i == 0)
            	this.box1 = -1;
            this.moveP(src, dst, i);  //actually draw the pieces
        }
        
        // this.init();
		if (this.isSomelinesShow() || this.showSerial) {
			this.calcThreads();
		}
    }
	
	this.isSomelinesShow = function isSomelinesShow()
	{
		if (this.showThreats) return true;
		if (this.showLive3) return true;
		if (this.showDead3) return true;
		if (this.showLive2) return true;
		return false;
	}

    this.mark = function mark() // boxValue, 0 for not mark, 1 for mark
    {
        if(this.boxValue == 1 )
        {
        	if (this.box != -1)
        	{
        		var m = this.box.split('-');
	            var bi = parseInt(m[0]);
	            var bj = parseInt(m[1]);
	            this.move(bi, bj, 362);
	        }

	        if (this.box1 != -1)
	        {
	            var n = this.box1.split('-');
	            var ci = parseInt(n[0]);
	            var cj = parseInt(n[1]);
	            this.move(ci, cj, 363);
        	}
        }
    }

    this.changeMark = function changeMark()
    {
        if(this.boxValue == 1)
        {
            this.boxValue = 0;
        }
        else
        {
            this.boxValue = 1;
        }
        //
        this.reset();
    }

    this.reset = function reset()
    {
        var timer = this.timer;
        this.goMove(0);
        if(timer)
        {
            this.timer = setTimeout("Connect6"+this.boardID+".autoPlay()", 1000);
        }
    }

    this.num = function num() // boxValue, 0 for not mark, 1 for mark
    {
        this.reset();
        //
        if(this.numValue == 0)
        {
            this.piecesHTML += '<div id="numCtrl" class="numCtrl">';
        }
        else
        {
            this.piecesHTML += '<div id="numCtrl" class="num2Ctrl">';
        }
    }

    this.changeNum = function changeNum()
    {
        if(this.numValue == 1 )
        {
            this.numValue = 0;
        }
        else
        {
            this.numValue = 1;
        }
        //
        this.num();
    }

    this.getPos = function getPos(flag, pos)
    {
        if(flag == 1) // mirror x
        {
            if (pos[0] >=0 && pos[0] <= 18)
    		    pos[0] = 18-pos[0];
        }
        else if(flag == 2) // mirror y
        {
            if (pos[1] >=0 && pos[1] <= 18)
    		    pos[1] = 18-pos[1];
        }
        else if(flag == 3) // clockwise
        {
            var t_src  = 9 - pos[0];  //shift to (9,9)
	        var t_dst  = 9 - pos[1];
	        pos[0] = t_dst + 9;
	        pos[1] = 9 - t_src;
        }
        else // counter clockwise
        {
            var t_src  = pos[0] - 9;  //shift to (9,9)
	        var t_dst  = pos[1] - 9;
	        pos[0] = t_dst + 9;
	        pos[1] = 9 - t_src;
        }

        return pos;
    }

    this.rotate = function rotate(flag)
    {
        for(i = 0; i < this.mvLen; i++)
        {
            var m = this.mv[i].split('-');   //the form is like 9-9
            var pos = new Array(parseInt(m[0]), parseInt(m[1]));
            pos = this.getPos(flag, pos);
            this.mv[i] = (pos[0] + '-' + pos[1]);
            this.box = this.mv[i];    //mark1
			this.box1 = this.mv[i-1]; //mark2
            if (i == 0) this.box1 = -1;
            this.moveP(pos[0], pos[1], i);  //actually draw the pieces
        }
		
		// 畫線也要跟著旋轉
		if (this.jg != undefined && this.isSomelinesShow()) {
			var newdrawlines = new Array();
			var xx1,yy1,xx2,yy2,type;
			for (var i = 0; i < this.drawlines.length; i++) {
				var target = this.drawlines[i];
				type = target[4];
				var pos = new Array(target[0], target[1]); // xx1,yy1 pair
				pos = this.getPos(flag,pos);
				xx1 = pos[0];
				yy1 = pos[1];
				
				pos = new Array(target[2], target[3]); // xx2,yy2 pair
				pos = this.getPos(flag,pos);
				xx2 = pos[0];
				yy2 = pos[1];
				
				newdrawlines.push(new Array(xx1,yy1,xx2,yy2,type));
			}
			this.drawlines = newdrawlines;
			this.drawLinesFunc();
			this.exitquiz = true;
		}	
		
		// LG 點資料也要更新
		this.lgrotatelog.push(flag);
	
		if (this.lgcount==1)
		{
			var pos = this.lgmove.pop();
            pos = this.getPos(flag, pos);
			this.lgmove.push(pos);
		}
		
	
		
        this.reset();
    }

    this.autoPlay = function autoPlay()
    {
        this.timer = setTimeout("Connect6"+this.boardID+".autoPlay()", 1000);
        this.gotoMove(1);
        this.boardObj.innerHTML = this.piecesHTML;
        this.boardObj.style.left = this.left;
        this.boardObj.style.top = this.top;
        this.piecesHTML = "";
    }

    this.play = function play()
    {
        if(this.timer == 0)
        {
            this.autoPlay();
        }
        else
        {
            clearTimeout(this.timer);
            this.timer = 0;
        }
    }

    this.showgif = function showgif()
    {
    	var send = new Array ();
    	var pre_link = 'http://www.connect6.org/board/img_generator/template.php?input=';
    	for (i=0; i< this.sPm.length/6; i+=1)
    		send[i] = this.sPm.substr(6*i,6);

    	var send_string = '';
    	
    	if (this.moveValue != 0)
	    	for (j=0; j< this.moveValue; j++)
	    		send_string = send_string.concat(send[j]);

		send_string = pre_link.concat(send_string);
    	window.open(send_string,'Connect6','width=670,height=550');
    }
    
    this.editpage = function editpage()
    {
    	if (this.mirror == 1)
    		param = 'http://www.connect6.org/board/img_generator/img.php?mirror=1&format='+this.format+'&input='+this.originmv;
    	else
    		param = 'http://www.connect6.org/board/img_generator/img.php?format='+this.format+'&input='+this.originmv;
    	window.open(param,'GenerateGameRecord','width=800,height=600,toolbar=1,menubar=1,scrollbars=1,location=1,status=1,resizable=1');
    }
	
	function getElementsByClassName(needle){
		var my_array = document.getElementsByTagName("*");
		var retvalue = new Array();
		var i;
		var j;
		
		for (i = 0, j = 0; i < my_array.length; i++) {
			var c = " " + my_array[i].className + " ";
			if (c.indexOf(" " + needle + " ") != -1) 
				retvalue[j++] = my_array[i];
		}
		return retvalue;
	}

	this.switchLines = function switchLines(pType)
	{	
	    if (pType==0) {
			// skip
		} else
		if (pType==2) {
			this.showLive3 = !this.showLive3;
			if (this.showLive3) this.setCookie("L3", 1, 365);
			else this.setCookie("L3", 0, 365);
		}
		else if (pType == 3) {
			this.showDead3 = !this.showDead3;
			if (this.showDead3) this.setCookie("D3", 1, 365);
			else this.setCookie("D3", 0, 365);
		}
		else if (pType == 4) {
			this.showLive2 = !this.showLive2;
			if (this.showLive2) this.setCookie("L2", 1, 365);
			else this.setCookie("L2", 0, 365);
		}
		else {
			this.showThreats = !this.showThreats;
			if (this.showThreats) this.setCookie("T", 1, 365);
			else this.setCookie("T", 0, 365);
		}
		
		if (this.showLive3) document.getElementById('show_live3').className="show_live3_press";
			else document.getElementById('show_live3').className="show_live3";
		if (this.showDead3) document.getElementById('show_dead3').className="show_dead3_press";
			else document.getElementById('show_dead3').className="show_dead3";
		if (this.showLive2) document.getElementById('show_live2').className="show_live2_press";
			else document.getElementById('show_live2').className="show_live2";
		if (this.showThreats) document.getElementById('show_threats').className="show_threats_press";
			else document.getElementById('show_threats').className="show_threats";
			
		this.jg.clear();
		this.reset();
	}
	
	this.copyMove = function copyMove(way)
    {
    	//way1: SGF標準格式( ;B[HH];W[HI];B[IH] )
    	//way2: LG標準格式( ;B[H8];W[H9];B[I8] )
    	//way3: 座標格式( h8 h9 i8 )
    	//way4: SGF簡化格式( hh HI ih )
        var move='';
        for(i = 0 ; i < this.moveValue ; i++)
        {
            if((way == 1) || (way == 2))
            {
            	if((i%4)==1||(i%4)==2) move += ';W[';
                else move += ';B[';
            }
	    var m = this.mv[i].split('-');   //the form is like 12-9  
            var pos = new Array(parseInt(m[0]), parseInt(m[1]));            
            
            var c = toUpperChar(pos[0]);
            if(way == 3) c = c.toLowerCase();
            if((way == 4) && !(i&1)) c = c.toLowerCase();
            move += c;
            
            var c = 19-pos[1];
            if((way == 1) || (way == 4)) c = toUpperChar(18-pos[1]);
            if((way == 4) && !(i&1)) c = c.toLowerCase();
            move += c;
            
            
            
            if((way == 1) || (way == 2))
            {
            	move += ']';
            }
            
        }
        switch (way)
        {
        	case 1:
        		format = "stdsgf";
        		break;
        	case 2:
        		format = "lgstdsgf";
        		break;
        	case 3:
        		format = "lgsimplesgf";
        		break;
        	case 4:
        		format = "simplesgf";
        		break;
        }
        link_url = "http://www.connect6.org/board/img_generator/img.php?format="+format;
        output_move = "&input="+move;
        redirect_url = link_url+output_move;
        window.open(redirect_url,'GenerateGameRecord','width=800,height=600,toolbar=1,menubar=1,scrollbars=1,location=1,status=1,resizable=1');
    }
    
    this.copyToClipBoard = function copyToClipBoard(txt) 
    {     
    	if(window.clipboardData) 
    	{             
            window.clipboardData.clearData();
            window.clipboardData.setData("Text", txt);
        } else if(navigator.userAgent.indexOf("Opera") != -1) 
        {          
            window.location = txt;     
        } else if (window.netscape) {
            try {               
                netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
            } catch (e) {
                 alert("被瀏覽器拒絕！\n請在瀏覽器地址欄輸入'about:config'並按ENTER\n然後將'signed.applets.codebase_principal_support'設置為'true'");
            }          
            var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);
            if (!clip)
                 return;
            var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);
            if (!trans)
                 return;
            trans.addDataFlavor('text/unicode');
            var str = new Object();
            var len = new Object();
            var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
            var copytext = txt;
            str.data = copytext;
            trans.setTransferData("text/unicode",str,copytext.length*2);
            var clipid = Components.interfaces.nsIClipboard;
            if (!clip)
                return false;
            clip.setData(trans,null,clipid.kGlobalClipboard);
         }
         alert('已經複製');
    }	
    
    this.toUpperChar = function toUpperChar(n)
    {
        switch(n)
        {
            case 0: return 'A';
            case 1: return 'B';
            case 2: return 'C';
            case 3: return 'D';
            case 4: return 'E';
            case 5: return 'F';
            case 6: return 'G';
            case 7: return 'H';
            case 8: return 'I';
            case 9: return 'J';
            case 10: return 'K';
            case 11: return 'L';
            case 12: return 'M';
            case 13: return 'N';
            case 14: return 'O';
            case 15: return 'P';
            case 16: return 'Q';
            case 17: return 'R';
            case 18: return 'S';
            default : return 'Z';
        }	
    	return 'Z';
    }
    
    
	
    this.controlline = function controlline()
    {
        var str = "";
        str += '<div id="info" class="info"></div>';
        str += '<div id="below_bg" class="below_bg"></div>';
        str += '<div id="logo" class="logo" title="前往CYC遊戲大聯盟"><a href="http://www.cycgame.com/" TARGET="_blank"><img name="a" src="./images/connect6/logo.gif" width=50 height=53 border=0></a></div>';
        str += '<div id="start" class="start" onclick="Connect6'+this.boardID+'.goMove(-1000);" onmouseover="this.className=\'startOver\';" onmouseout="this.className=\'start\';" title="跳到最前"></div>';
        str += '<div id="prev10" class="prev10" onclick="Connect6'+this.boardID+'.goMove(-4);" onmouseover="this.className=\'prev10Over\';" onmouseout="this.className=\'prev10\';"  title="往前四步"></div>';
        str += '<div id="prev1" class="prev1" onclick="Connect6'+this.boardID+'.goMove(-1);" onmouseover="this.className=\'prev1Over\';" onmouseout="this.className=\'prev1\';"  title="往前一步"></div>';
        str += '<div id="next1" class="next1" onclick="Connect6'+this.boardID+'.goMove(1);" onmouseover="this.className=\'next1Over\';" onmouseout="this.className=\'next1\';"  title="往後一步"></div>';
        str += '<div id="next10" class="next10" onclick="Connect6'+this.boardID+'.goMove(4);" onmouseover="this.className=\'next10Over\';" onmouseout="this.className=\'next10\';"  title="往後四步"></div>';
        str += '<div id="end" class="end" onclick="Connect6'+this.boardID+'.goMove(1000);" onmouseover="this.className=\'endOver\';" onmouseout="this.className=\'end\';"  title="跳到最後"></div>';
        str += '<div id="rotate1" class="rotate1" onclick="Connect6'+this.boardID+'.rotate(2)" onmouseover="this.className=\'rotate1over\';" onmouseout="this.className=\'rotate1\';"  title="上下鏡射"></div>';
        str += '<div id="rotate2" class="rotate2" onclick="Connect6'+this.boardID+'.rotate(1)" onmouseover="this.className=\'rotate2over\';" onmouseout="this.className=\'rotate2\';"  title="左右鏡射"></div>';
        str += '<div id="rotate3" class="rotate3" onclick="Connect6'+this.boardID+'.rotate(4)" onmouseover="this.className=\'rotate3over\';" onmouseout="this.className=\'rotate3\';"  title="往左旋轉"></div>';
        str += '<div id="rotate4" class="rotate4" onclick="Connect6'+this.boardID+'.rotate(3)" onmouseover="this.className=\'rotate4over\';" onmouseout="this.className=\'rotate4\';"  title="往右旋轉"></div>';
        str += '<div id="boxCtrl" class="boxCtrl" onclick="Connect6'+this.boardID+'.changeMark()" onmouseover="this.className=\'box2Ctrl\';" onmouseout="this.className=\'boxCtrl\';"  title="落子標示"></div>';
        str += '<div id="autoPlay" class="autoPlay" onclick="Connect6'+this.boardID+'.play()" onmouseover="this.className=\'autoPlaypress\';" onmouseout="this.className=\'autoPlay\';" title="自動播放"></div>';
        str += '<div id="numCtrl" class="num2Ctrl" onclick="Connect6'+this.boardID+'.changeNum()" onmouseover="this.className=\'numCtrl\';" onmouseout="this.className=\'num2Ctrl\';" title="手數標示"></div>';
        str += '<div id="showgif" class="showgif" onclick="Connect6'+this.boardID+'.showgif()" onmouseover="this.className=\'showgifpress\';" onmouseout="this.className=\'showgif\';" title="列印圖片"></div>';
        str += '<div id="edit" class="editpage" onclick="Connect6'+this.boardID+'.editpage()" onmouseover="this.className=\'editpagepress\';" onmouseout="this.className=\'editpage\';" title="編輯網頁"></div>';
        str += '<div id="doc" class="doc" onclick="window.open(\'http://www.connect6.org/web/index.php?option=com_content&task=view&id=123\',\'Connect6\',\'width=800,height=600,toolbar=1,menubar=1,scrollbars=1,location=1,status=1,resizable=1\')" onmouseover="this.className=\'docpress\';" onmouseout="this.className=\'doc\';"  title="六子棋動態棋譜使用說明"></div>';
        str += '<div id="home" class="home" onclick="window.open(\'http://www.connect6.org/web/index.php?lang=tw\',\'Connect6\',\'width=800,height=600,toolbar=1,menubar=1,scrollbars=1,location=1,status=1,resizable=1\')" onmouseover="this.className=\'homepress\';" onmouseout="this.className=\'home\';"  title="前往六子棋首頁"></div>';

		// threats.
		str += '<div id="show_threats" class="show_threats" onmouseover="this.className=\'show_threats_over\';" onmouseout="Connect6'+this.boardID+'.switchLines(0)" onclick="Connect6'+this.boardID+'.switchLines(1)" title="迫著"></div>';
	    str += '<div id="show_live3" class="show_live3" onclick="Connect6'+this.boardID+'.switchLines(2)" onmouseover="this.className=\'show_live3_over\';" onmouseout="Connect6'+this.boardID+'.switchLines(0)" title="活三"></div>';
		str += '<div id="show_dead3" class="show_dead3" onclick="Connect6'+this.boardID+'.switchLines(3)" onmouseover="this.className=\'show_dead3_over\';" onmouseout="Connect6'+this.boardID+'.switchLines(0)" title="死三"></div>';
		str += '<div id="show_live2" class="show_live2" onclick="Connect6'+this.boardID+'.switchLines(4)" onmouseover="this.className=\'show_live2_over\';" onmouseout="Connect6'+this.boardID+'.switchLines(0)" title="活二"></div>';		
		str += '<div id="show_all_line" class="show_all_linepress" onclick="Connect6'+this.boardID+'.linecontrol(3)" onmouseover="this.className=\'show_all_lineover\';" onmouseout="Connect6'+this.boardID+'.linecontrol(Connect6'+this.boardID+'.showlinecolor)"  title="黑白都顯示"></div>';		
		str += '<div id="show_black_line" class="show_black_line" onclick="Connect6'+this.boardID+'.linecontrol(1)" onmouseover="this.className=\'show_black_lineover\';" onmouseout="Connect6'+this.boardID+'.linecontrol(Connect6'+this.boardID+'.showlinecolor)"  title="只顯示黑"></div>';		
		str += '<div id="show_white_line" class="show_white_line" onclick="Connect6'+this.boardID+'.linecontrol(2)" onmouseover="this.className=\'show_white_lineover\';" onmouseout="Connect6'+this.boardID+'.linecontrol(Connect6'+this.boardID+'.showlinecolor)"  title="只顯示白"></div>';		
		return str;
		
    }
	
	this.linecontrol = function linecontrol(color) //color : 1->black, 2->white, 3->all color is default
	{
		all_line_obj = document.getElementById('show_all_line');
		black_line_obj = document.getElementById('show_black_line');
		white_line_obj = document.getElementById('show_white_line');
		
	        if (color == 1)
		{
			all_line_obj.className = "show_all_line";
			black_line_obj.className = "show_black_linepress";
			white_line_obj.className = "show_white_line";
		}
		else if (color == 2)
		{
			all_line_obj.className = "show_all_line";
			black_line_obj.className = "show_black_line";
			white_line_obj.className = "show_white_linepress";
		}  else {
			color = 3;
			all_line_obj.className = "show_all_linepress";
			black_line_obj.className = "show_black_line";
			white_line_obj.className = "show_white_line";
		}

		this.showlinecolor = color;
		this.setCookie('LC', color, 365);
		
		this.jg.clear();
		this.reset();
		this.drawLinesFunc();
	}
	
	////////////////////////////////////////////////////////////////////////////////////
	// 測驗相關
	// 在右邊列印問題
	this.drawQuiz = function drawQuiz() 
	{
		if (this.messageObj==undefined) return; // not quiz mode.

		this.messages = "id: " + this.QuizId + " (level:" + this.QuizLevel + ")<br />\n";

		this.messages += "<h2>請問";
		if ((this.sPm.length-2)/4%2==1) this.messages += "黑子";
		else  this.messages += "白子";
		this.messages += "有多少條活四或死四？</h2><br />\n";
		this.messages += this.selectline();
        this.messageObj.innerHTML = this.messages;
        this.messageObj.style.left = this.left + 550;
        this.messageObj.style.top = this.top + 50 ;
	}

	// 確認答案正確
	this.verify = function verify(pAns) 
	{
	   this.goMove(1000);
	   this.drawLinesFunc();
	   
	   this.messages = "Level:" + this.QuizLevel + " Id:" + this.QuizId +"<br />\n";
	   if (pAns!=this.ThreatsCount) {
	   		this.messages += "<h2><font color=red>答錯了!答案為 " + this.ThreatsCount +"</font></h2>\n";
			this.QuizLevel = this.QuizLevel-1;
			if (this.QuizLevel<1) this.QuizLevel=1;
	   }
	   else {
	   		this.messages += "<h2><font color=blue>答對了! 答案就是 " + this.ThreatsCount + "</font></h2>\n";
			this.QuizLevel = this.QuizLevel+1;
			if (this.QuizLevel>20) this.QuizLevel=20;
	   }

	   // 進行下一題之按鈕
	   this.messages += "<form action=\"./edu.php\" method=post>\n";
	   this.messages += "<input type=\"hidden\" name=\"lv\" value=\"" + this.QuizLevel +"\" />\n"; 
	   this.messages += "<input type=\"image\" src=./images/edu/next.jpg />\n";
	   this.messages += "</form>\n";
       this.messageObj.innerHTML = this.messages;
	}
	
	// 提供右邊按答案的圖案
	this.selectline = function selectline()
	{
		var str="";
        str += '<div id="btn1" class="btn1" onclick="Connect6'+this.boardID+'.verify(1);" onmouseover="this.style.cursor=\'pointer\';this.style.cursor=\'hand\';"></div>';
        str += '<div id="btn2" class="btn2" onclick="Connect6'+this.boardID+'.verify(2);" onmouseover="this.style.cursor=\'pointer\';this.style.cursor=\'hand\';"></div>';
        str += '<div id="btn3" class="btn3" onclick="Connect6'+this.boardID+'.verify(3);" onmouseover="this.style.cursor=\'pointer\';this.style.cursor=\'hand\';"></div>';
        str += '<div id="btn4" class="btn4" onclick="Connect6'+this.boardID+'.verify(4);" onmouseover="this.style.cursor=\'pointer\';this.style.cursor=\'hand\';"></div>';
        str += '<div id="btn5" class="btn5" onclick="Connect6'+this.boardID+'.verify(5);" onmouseover="this.style.cursor=\'pointer\';this.style.cursor=\'hand\';"></div>';

		return str;
	}
	
	// sort deadlines
	this.sortLines = function sortLines()
	{
		var four_stones = new Array();
		var three_live_stones = new Array();
		var three_dead_stones = new Array();
		var two_stones = new Array();
		
		var linescount = this.drawlines.length;
		var temp;
		for (i = 0; i < linescount; i++) {
			temp = this.drawlines.pop();
			if (temp[6]>=4) four_stones.push(temp);
			else if (temp[6]==3 && temp[5]==0) three_live_stones.push(temp);
			else if (temp[6]==3 && temp[5]==1) three_dead_stones.push(temp);
			else two_stones.push(temp);
		}
		
		// L2
		while (two_stones.length>0) {
			temp = two_stones.pop();
			this.drawlines.push(temp);
		}
		
		// D3
		while (three_dead_stones.length>0) {
			temp = three_dead_stones.pop();
			this.drawlines.push(temp);
		}
		
		// L3
		while (three_live_stones.length>0) {
			temp = three_live_stones.pop();
			this.drawlines.push(temp);
		}
		
		// Ts
		while (four_stones.length>0) {
			temp = four_stones.pop();
			this.drawlines.push(temp);
		}
	}
	
	////////////////////////////////////////////////////////////////////////////////////
	// 畫線
	// 1: Threat按鈕：只顯示死四、活四、死五等
    // 2: 活三按鈕：只顯示活三等
    // 3: 活二按鈕：只顯示活二等
    // 4: 死三按鈕：只顯示死三等
	this.drawLinesFunc = function drawLinesFunc() 
	{
		if (this.jg==undefined) return;
		if (!this.isSomelinesShow()) return;
		
	    var target;
		this.jg.clear();	 
		var lastx1, lasty1, lastx2, lasty2, lasttype=-1;

		this.sortLines();
		
		var count=1;
		this.ThreatsCount=0;
		for (var i=0; i<this.drawlines.length; i++) {
		    target = this.drawlines[i];
			// 判斷是否重疊
		    var offsetx=0, offsety=0;
			var xx1 = target[0];
			var yy1 = target[1];
			var xx2 = target[2];
			var yy2 = target[3];
			var type = target[4];
			var deadlive = target[5]; // 0:live 1:dead
			var scount = target[6]; // stones
			var lastcolor = target[7]; // lastcolor, 1->B, 2->W, 3->all draw
			

			stroke_width = 4;
			
			if (scount>=4) {
				this.jg.setColor("#ff0000");
				stroke_width = 4;
				if (!this.showThreats) continue;
			}
			else if (scount==3 && deadlive==0) {
				this.jg.setColor("#00ff00");
				stroke_width = 4;
				if (!this.showLive3) continue;
			}
			else if (scount==3 && deadlive==1) {
				this.jg.setColor("#00ff00");
				stroke_width = 2;
				if (!this.showDead3) continue;
			}
			else {
				this.jg.setColor("#0000ff");
				stroke_width = 4;
				if (!this.showLive2) continue;
			}
			
			this.jg.setStroke(stroke_width);
			
			if (target[4]==lasttype) {
				// 不用再錯開
				count++;
			} else { count=1; this.ThreatsCount++; }
			
			lastx1 = xx1; lasty1 = yy1; lastx2 = xx2; lasty2 = yy2;
			lasttype = target[4];

            xx1 = (10 + this.left + this.istartX + this.diffX*(xx1) + offsetx );
            yy1 = (10 + this.top + this.istartY + this.diffY*(yy1) + offsety );
            xx2 = (10 + this.left + this.istartX + this.diffX*(xx2) + offsetx );
            yy2 = (10 + this.top + this.istartY + this.diffY*(yy2) + offsety );
			switch (target[4]) {
				case 0: // THREAT_VERTICAL ?qry=21228
					xx1 = xx1 - stroke_width/2;
					xx2 = xx2 - stroke_width/2;
				break;
				case 1: // THREAT_HORITICAL
					yy1 = yy1 - stroke_width/2;
					yy2 = yy2 - stroke_width/2;
				break;
				case 2: // THREAT_VERTICAL ?qry=21228
				case 3: // THREAT_HORITICAL
					xx1 = xx1 - stroke_width/2;
					xx2 = xx2 - stroke_width/2;
					yy1 = yy1 - stroke_width/2;
					yy2 = yy2 - stroke_width/2;
				break;
						
			}
			if (this.showlinecolor == 3 || this.showlinecolor == lastcolor)
				this.jg.drawLine(xx1,yy1,xx2,yy2);
		}
		this.jg.paint();
	}
	
	// 新增，並過濾
	this.InsertNewLine = function InsertNewLine(newdrawlines, left, ttop, right, down, pType, deadlive, count, lastcolor)
	{
		
		if (deadlive == 1 && count == 2) return; // 死二		
		// if (deadlive==0 && count>=4) deadlive=1; // convert to thread.		
		for (var i=0; i<newdrawlines.length; i++) 
		{
			temp = newdrawlines[i];
			
			if (pType!=temp[4]) continue;
			
			// the cases that never be the same.
			switch (pType) 
			{
				case 0:
				  if (temp[0]!=left) continue;
				  if (temp[0]!=right) continue;
				break;
				case 1:
				  if (temp[1]!=ttop) continue;
				  if (temp[1]!=down) continue;
				break;
				case 2:
				  if (temp[0]+temp[1]!=left+ttop) continue;
				break;
				case 3:
				  if (temp[0]-temp[1]!=left-ttop) continue;
				break;
			}
			
			if (temp[0]==left && temp[1]==ttop && temp[2]==right && temp[3]==down) {
				return; // the same.
			} 
/*			
			// 進來短的例子
			if (left>=temp[0] && left<=temp[2] && ttop>=temp[1] && ttop<=temp[3] &&
				right>=temp[0] && right<=temp[2] && down>=temp[1] && down<=temp[3] &&
				count<temp[6] 
			) {
				return; 
			} else 
			if (left>=temp[0] && left<=temp[2] && ttop<=temp[1] && ttop>=temp[3] &&
				right>=temp[0] && right<=temp[2] && down<=temp[1] && down>=temp[3] &&
				count<temp[6] 
			) {
				return;
			}
			
			// 進來長的，取代短的例子
			if (temp[0]>=left && temp[0]<=right && temp[1]>=ttop && temp[1]<=down &&
				temp[2]>=left && temp[2]<=right && temp[3]>=ttop && temp[3]<=down &&
				count>temp[6] 
			) {
				if (temp[5] == deadlive) {
					newdrawlines[i][0] = left;
					newdrawlines[i][1] = ttop;
					newdrawlines[i][2] = right;
					newdrawlines[i][3] = down;
					newdrawlines[i][4] = pType;
					newdrawlines[i][5] = deadlive;
					newdrawlines[i][6] = count;
					newdrawlines[i][7] = lastcolor;
					return;
				} 
			} else if (temp[0]>=left && temp[0]<=right && temp[1]<=ttop && temp[1]>=down &&
				temp[2]>=left && temp[2]<=right && temp[3]<=ttop && temp[3]>=down &&
				count > temp[6] 
			) {
				if (temp[5] == deadlive) { 
					newdrawlines[i][0] = left;
					newdrawlines[i][1] = ttop;
					newdrawlines[i][2] = right;
					newdrawlines[i][3] = down;
					newdrawlines[i][4] = pType;
					newdrawlines[i][5] = deadlive;
					newdrawlines[i][6] = count;
					newdrawlines[i][7] = lastcolor;
					return;
				} 
			}
*/				
		}
		
		newdrawlines.push(new Array(left, ttop, right, down, pType, deadlive, count, lastcolor));
		return;
	}
	
	this.calcLineNum = function calcLineNum(deadlive, count, lastcolor, way){
		
		if(deadlive == 0){
			switch (count){
				case 2:
					if(lastcolor == 1)	this.B_L2 += way;
					else this.W_L2 += way;
					break;
				case 3:
					if(lastcolor == 1)	this.B_L3 += way;
					else this.W_L3 += way;
					break;
				case 4:
				case 5:
					if(lastcolor == 1)	this.B_T += way;
					else this.W_T += way;
					break;
				default:
				 	break;	
			}
		}else if(deadlive == 1){
			switch (count){
				case 3:
					if(lastcolor == 1)	this.B_D3 += way;
					else this.W_D3 += way;
					break;
				case 4:
				case 5:
					if(lastcolor == 1)	this.B_T += way;
					else this.W_T += way;
				default:
					break;
			}	
		}	
						
	}
	
	this.calcOneLine = function calcOneLine(virtualB, newdrawlines, lastcolor, pType, count, distance, ttop, left, down, right,way){
			var deadlive = 0;  // 0:live   1:dead
			var leftspacecount = 0; //計算左方有幾個空格 (用以判斷是死是活)
			var rightspacecount = 0; //同上,算右方(or下方)
			var op_color = 3 - lastcolor;
			var offX=0, offY=0;
			//if(count < 2) return;
			
			if (pType==0) { // THREAT_VERTICAL
				offX=0;
				offY=1;
			} else if (pType==1) { // THREAT_HORITICAL
				offX=1;
				offY=0;
			} else if (pType==2) { // THREAT_PLUS_DIAGONAL
				offX=1;
				offY=-1;
			} else if (pType==3) { // THREAT_MINUS_DIAGONAL
				offX=1;
				offY=1;
			}
			
			x=left-offX;
			y=ttop-offY;
			while(virtualB[x][y] != op_color && virtualB[x][y] != 3){
				x-=offX;
				y-=offY;
				leftspacecount++;
				if(leftspacecount >= 4) break;	
			}
			x=right+offX;
			y=down+offY;
			while(virtualB[x][y] != op_color && virtualB[x][y] != 3){
				x+=offX;
				y+=offY;
				rightspacecount++;
				if(rightspacecount >= 4) break;	
			}
				
			
			if (leftspacecount + 2 + distance + rightspacecount < 6) return;
			if (count < 5){
				if(distance > 2) deadlive = 1;
				else if(leftspacecount < 2) deadlive = 1;
				else if(rightspacecount < 2) deadlive = 1;	
				else if(leftspacecount + 2 + distance + rightspacecount < 8) deadlive = 1;			
			} else if(count == 5){
					if(leftspacecount == 0 || rightspacecount == 0) deadlive = 1;	
			}
		
			if (count>=2) 
			{
				if(way == 0)
					this.InsertNewLine(newdrawlines, left-1, ttop-1, right-1, down-1, pType, deadlive, count, lastcolor); 
				else {
					this.calcLineNum(deadlive, count, lastcolor, way);
				}
			}
			
	}
		   											    	
	this.calcOneNode_2 = function calcOneNode_2(virtualB, newdrawlines, lastcolor, pType, i, j, way){
		//way0: 正常畫線,   way-1: 只count數目,做扣1動作,    way 1: 只count數目,做加1動作
		var count=0;
		var ttop=-1, down=-1, left=-1, right=-1;
		var lastdeadlive=-1, lastscount=-1;
		var deadlive=1;
		var offX=0, offY=0;
//		var counted=0;
		var distance=-1;
		var op_color=3-lastcolor;
		var nowi,nowj;
		if (pType==0) { // THREAT_VERTICAL
			offX=0;
			offY=1;
		} else if (pType==1) { // THREAT_HORITICAL
			offX=1;
			offY=0;
		} else if (pType==2) { // THREAT_PLUS_DIAGONAL
			offX=1;
			offY=-1;
		} else if (pType==3) { // THREAT_MINUS_DIAGONAL
			offX=1;
			offY=1;
		}
	
		if(virtualB[i][j] != lastcolor) return;

		count = 1;
		ttop = j;
		left = i;
		down = j;
		right = i;
		nowi = i;
		nowj = j;
		for (k=1; k<6; k++) {
			nowi += offX;
			nowj += offY;
			if (virtualB[nowi][nowj] == op_color || virtualB[nowi][nowj] == 3) break;
			if (virtualB[nowi][nowj] == lastcolor) {
					down = nowj;
					right = nowi;
					distance = k-1;  //判斷第一子到最後一子中間有幾空格
			  	count++;
			}
		}

		nowi = i;
		nowj = j;
		for(k=4;k>0;k--){
			  nowi -= offX;
			  nowj -= offY;
			  if(virtualB[nowi][nowj] == op_color || virtualB[nowi][nowj] == 3) break;
		    if(virtualB[nowi][nowj] == lastcolor && distance < k) return;
		}

		if(count > 1){
			this.calcOneLine(virtualB, newdrawlines, lastcolor, pType, count, distance, ttop, left, down, right,way); //check是否是L2,L3,D3,T 若是,則加入畫線
		}
								
	}
	
	this.calcOneNode = function calcOneNode(virtualB, newdrawlines, lastcolor, pType){
		// i=j=0 為 Virutal.. (1,1) 對應到棋譜之 (0,0)
		// 因為要判斷在邊界的連子...
		for (i=0; i<21; i++) {
			for (j=0; j<21; j++) {
				this.calcOneNode_2(virtualB, newdrawlines, lastcolor, pType, i, j, 0);
			}
		}
	}
	
	// 計算各種 Threads 並儲存到 this.drawlines
	this.calcThreads = function calcThreads() {
		if (!this.isSomelinesShow() && !this.showSerial) return;
		
		// prepare virtual board.
		var virtualB = [];
		for (i=0; i<24; i++) {
			virtualB[i] = [];
			for (j=0; j<24; j++) {
				if (j>0 && j<20 && i>0 & i<20) virtualB[i][j]=0;
				else virtualB[i][j]=3; // 邊界外, i=j=0 以及 i=j=19
			}
		}
		
		// fill the board.
		for (i = 0; i < this.moveValue; i++) {
			var m = this.mv[i].split('-'); //the form is like 9-9
			if (i == 0) {
				virtualB[parseInt(m[0])+1][parseInt(m[1])+1] = 1; // black
			}
			else if (((i - 1) / 2 % 2) < 1) {
				virtualB[parseInt(m[0])+1][parseInt(m[1])+1] = 2; // white
			}
			else {
				virtualB[parseInt(m[0])+1][parseInt(m[1])+1] = 1; // black
			}
    }
		
		// calc threads here!!
		var newdrawlines = new Array();
		
		// 雙方都畫
		this.calcOneNode(virtualB, newdrawlines, 1, 0); // THREAT_VERTICAL
		this.calcOneNode(virtualB, newdrawlines, 1, 1); // THREAT_HORITICAL
		this.calcOneNode(virtualB, newdrawlines, 1, 2); // THREAT_PLUS_DIAGONAL
		this.calcOneNode(virtualB, newdrawlines, 1, 3); // THREAT_MINUS_DIAGONAL
		
		this.calcOneNode(virtualB, newdrawlines, 2, 0); // THREAT_VERTICAL
		this.calcOneNode(virtualB, newdrawlines, 2, 1); // THREAT_HORITICAL
		this.calcOneNode(virtualB, newdrawlines, 2, 2); // THREAT_PLUS_DIAGONAL
		this.calcOneNode(virtualB, newdrawlines, 2, 3); // THREAT_MINUS_DIAGONAL


		this.drawlines = newdrawlines;
	}
	
	
	
	// show serial table.
	this.setSerialShow = function setSerialShow() 
	{
		this.showSerial = true;
		this.showSerialTable();
	}
	
	this.showSerialTable = function showSerialTable() 
	{
		if (!this.showSerial) return;
		if (this.serialObj==undefined) return;
		if (this.mv_t.length<=0) return;
		
		var mv_count = new Array();
		
		this.goMove(-1000);
		
		var serial_table = "";
		var download_link = "<a href=/board/display.php?showserial=2&input=";


		// prepare virtual board.
		var virtualB = [];
		for (i=0; i<24; i++) {
			virtualB[i] = [];
			for (j=0; j<24; j++) {
				if (j>0 && j<20 && i>0 & i<20) virtualB[i][j]=0;
				else virtualB[i][j]=3; // 邊界外, i=j=0 以及 i=j=19
			}
		}

		// collect info of lines.
		var totallength = this.mv.length;

		for (i=0; i<totallength; i++)
		{
			var m = this.mv[i].split('-'); //the form is like 9-9
			
			//把第i步所下的點四個Direction的L2,D3,L3,T先扣掉
			for(pType = 0; pType < 4 ; pType++){
					var offX=0, offY=0;
					if (pType==0) { // THREAT_VERTICAL               |
						offX=0;
						offY=1;
					} else if (pType==1) { // THREAT_HORITICAL       ─
						offX=1;
						offY=0;
					} else if (pType==2) { // THREAT_PLUS_DIAGONAL   /
						offX=1;
						offY=-1;
					} else if (pType==3) { // THREAT_MINUS_DIAGONAL  \
						offX=1;
						offY=1;
					}	

					var a = parseInt(m[0])+1;
					var b = parseInt(m[1])+1;										

					while(a > 0 && b > 0 && a < 20 && b < 20){
						a-=offX;
						b-=offY;	
					}
					while(a>=0 && b>=0 && a<=20 && b<=20){
						this.calcOneNode_2(virtualB, 0, 1, pType, a, b, -1);
						this.calcOneNode_2(virtualB, 0, 2, pType, a, b, -1);
						a+=offX;
						b+=offY;	
					}

			}
		
			//setMove
			if (i == 0) {
				virtualB[parseInt(m[0])+1][parseInt(m[1])+1] = 1; // black
			}
			else if (((i - 1) / 2 % 2) < 1) {
				virtualB[parseInt(m[0])+1][parseInt(m[1])+1] = 2; // white
			}
			else {
				virtualB[parseInt(m[0])+1][parseInt(m[1])+1] = 1; // black
			}
						
			//把第i步所下的點的四個Direction的L2,D3,L3,T加上去
			var m = this.mv[i].split('-'); //the form is like 9-9
			for(pType = 0; pType < 4 ; pType++){
					var offX=0, offY=0;
					if (pType==0) { // THREAT_VERTICAL               |
						offX=0;
						offY=1;
					} else if (pType==1) { // THREAT_HORITICAL       ─
						offX=1;
						offY=0;
					} else if (pType==2) { // THREAT_PLUS_DIAGONAL   /
						offX=1;
						offY=-1;
					} else if (pType==3) { // THREAT_MINUS_DIAGONAL  \
						offX=1;
						offY=1;
					}	

					var a = parseInt(m[0])+1;
					var b = parseInt(m[1])+1;									
						
					while(a > 0 && b > 0 && a < 20 && b < 20){
						a-=offX;
						b-=offY;	
					}
					
					while(a>=0 && b>=0 && a<=20 && b<=20){
						this.calcOneNode_2(virtualB, 0, 1, pType, a, b, 1);
						this.calcOneNode_2(virtualB, 0, 2, pType, a, b, 1);
						a+=offX;
						b+=offY;	
					}
			}			
			
			//把結果push進Array
			if((i % 2) == 0){
				mv_count.push(new Array(this.B_T, this.B_L3, this.B_D3, this.B_L2, this.W_T, this.W_L3, this.W_D3, this.W_L2));
			}
		}

		this.goMove(1000);
		
		// draw table.
		serial_table += "<table border=1 width=90%>";
		serial_table += "<tr>";
		serial_table += "<td rowspan=2>Step</td><td rowspan=2>Time</td>";
		serial_table += "<td colspan=4>Black</td><td colspan=4>White</td>";
		serial_table += "</tr><tr>";
		serial_table += "<td>T&nbsp;</td><td>L3</td><td>D3</td><td>L2</td>";
		serial_table += "<td>T&nbsp;</td><td>L3</td><td>D3</td><td>L2</td>";
		serial_table += "</tr>";
		for (var i=0; i<this.mv_t.length; i++)
		{
			serial_table += "<tr>";
			
			serial_table += "<td>";
			serial_table += (i+1);
			serial_table += "</td>";
			
			download_link += (i+1);
			download_link += ";";
			
			serial_table += "<td>";
			serial_table += this.mv_t[i];
			serial_table += "</td>";
			
			if (this.mv_t[i]!="&nbsp;") download_link += this.mv_t[i];
			download_link += ";";
			
			var mv_count_tmp = mv_count[i];
			serial_table += "<td>";
			serial_table += mv_count_tmp[0];
			serial_table += "</td>";
			serial_table += "<td>";
			serial_table += mv_count_tmp[1];
			serial_table += "</td>";
			serial_table += "<td>";
			serial_table += mv_count_tmp[2];
			serial_table += "</td>";
			serial_table += "<td>";
			serial_table += mv_count_tmp[3];
			serial_table += "</td>";
			serial_table += "<td>";
			serial_table += mv_count_tmp[4];
			serial_table += "</td>";
			serial_table += "<td>";
			serial_table += mv_count_tmp[5];
			serial_table += "</td>";
			serial_table += "<td>";
			serial_table += mv_count_tmp[6];
			serial_table += "</td>";
			serial_table += "<td>";
			serial_table += mv_count_tmp[7];
			serial_table += "</td>";
			
			serial_table += "</tr>";
			
			download_link += mv_count_tmp[0];
			download_link += ";";
			download_link += mv_count_tmp[1];
			download_link += ";";
			download_link += mv_count_tmp[2];
			download_link += ";";
			download_link += mv_count_tmp[3];
			download_link += ";";
			download_link += mv_count_tmp[4];
			download_link += ";";
			download_link += mv_count_tmp[5];
			download_link += ";";
			download_link += mv_count_tmp[6];
			download_link += ";";
			download_link += mv_count_tmp[7];
			download_link += ";";
			
			download_link += "_";
		}
		
		serial_table += "</table>";
		download_link += "> export </a>";
		serial_table = download_link + serial_table;
		
		this.serialObj.innerHTML = serial_table;
		
	}
	
}

