var editMode="Layout",countPro=7,cellWidth="75px",cellHeight="30px",inputWidth="50",divHeight="50",countStatus=6,showStatus="false",editColor="false",writable="false",separator=":",paramVal="",paramColor="",notEditBgColor="gray",SYS_BACKGROUND="white",SYS_EDIT="\u53ef",SYS_NOTEDIT="\u4e0d\u53ef",SYS_MODE_LAYOUT="Layout",SYS_MODE_VALUE="Value",mTable=null,cellObjs=null,cellVals=null,startCell=null,endCell=null,rowStart=0,colStart=0,rowEnd=0,colEnd=0,rowCount=0,colCount=0,colors="white blue lightblue green lightgreen orange red pink purple brown black".split(" "),
winWidth=getClientWidth(),winHeight=getClientHeight(),mouseX=0,mouseY=0,fgEditing="0",theImg,dragEvent=!1;function dataNode(b,a,d,c,e,g,f){this.row=b;this.col=a;this.location=d;this.value=c;this.color=e;this.cellEdit=g;this.status=f;this.size=4}function setData(b,a,d,c,e,g,f,h,k,l,m){if(null==m)return!1;$.post("entry.jsp",{FGNMLOC:b,NMLOC:a,FGCELLEDIT:d,CELLEDIT:c,FGVALUE:e,VALUE:g,FGCOLOR:f,COLOR:h,FGSTATUS:k,STATUS:l,WHERE:m},function(n){$("#FEEDBACK").html(n)})}
function editInput1(b,a,d){var c=b.parentNode,e=(c.row-1)*colCount+(c.col-1);if("true"==showStatus&&""!=cellVals[e].status){var g=document.createElement("img");g.src="../image/status_"+cellVals[e].status+".png";g.style.width="15px";g.style.height="15px";cellObjs[e].appendChild(g)}cellObjs[e].appendChild(document.createTextNode(b.value+(""==b.value||""==cellVals[e].value?"":separator)+cellVals[e].value));cellObjs[e].style.backgroundColor="1"==a.value?cellVals[e].color:notEditBgColor;setData("1",b.value,
"1",a.value,"0",null,"0",null,"0",null,"["+c.row+"_"+c.col+"]");cellVals[e].location=b.value;cellObjs[e].removeChild(b);cellObjs[e].removeChild(a);cellObjs[e].removeChild(d);fgEditing="0"}
function editInput2(b,a,d){var c=b.parentNode,e=(c.row-1)*colCount+(c.col-1);var g=""==b.value?"true"==editColor?a.value:SYS_BACKGROUND:a.value;if("true"==showStatus&&""!=cellVals[e].status){var f=document.createElement("img");f.src="../image/status_"+cellVals[e].status+".png";f.style.width="15px";f.style.height="15px";"true"==writable&&$(f).bind("dblclick",function(h){editStatus(h||event);return!1});cellObjs[e].appendChild(f)}cellObjs[e].appendChild(document.createTextNode(cellVals[e].location+(""==
cellVals[e].location||""==b.value?"":separator)+b.value));cellObjs[e].style.backgroundColor="1"==cellVals[e].cellEdit?g:notEditBgColor;setData("0",null,"0",null,"1",b.value,"1",g,"0",null,"["+c.row+"_"+c.col+"]");cellVals[e].value=b.value;cellVals[e].color=g;cellObjs[e].removeChild(b);cellObjs[e].removeChild(a);cellObjs[e].removeChild(d);fgEditing="0"}
function editRadio(b,a){if(null==b)return!1;b=b.srcElement;var d=b.parentNode;theImg.src="../image/status_"+b.statusVal+".png";setData("0",null,"0",null,"0",null,"0",null,"1",b.statusVal,"["+cellObjs[a].row+"_"+cellObjs[a].col+"]");cellVals[a].status=b.statusVal;d.parentNode.removeChild(d)}
function editTd(b){if(null==b||"1"==fgEditing)return!1;b=b.srcElement||b.target;var a=(b.row-1)*colCount+(b.col-1);if("TD"==b.tagName&&("1"==cellVals[a].cellEdit||editMode==SYS_MODE_LAYOUT)){for(fgEditing="1";cellObjs[a].hasChildNodes();)cellObjs[a].removeChild(cellObjs[a].firstChild);if(editMode==SYS_MODE_LAYOUT){var d=document.createElement("input");d.value=cellVals[a].location;d.style.border="solid gray 1px";d.style.width=inputWidth;b.appendChild(d);var c=document.createElement("select"),e=document.createElement("option");
e.value="1";"1"==cellVals[a].cellEdit&&(e.selected=!0);var g=document.createTextNode(SYS_EDIT);e.appendChild(g);c.appendChild(e);e=document.createElement("option");e.value="0";"0"==cellVals[a].cellEdit&&(e.selected=!0);g=document.createTextNode(SYS_NOTEDIT);e.appendChild(g);c.appendChild(e);b.appendChild(c);var f=document.createElement("button");f.style.width="25px";f.style.margin="1px";$(f).text("OK");$(f).bind("click",function(k){editInput1(d,c,f)})}else{d=document.createElement("input");d.value=
""==cellVals[a].value?paramVal:cellVals[a].value;d.style.border="solid gray 1px";d.style.width=inputWidth;b.appendChild(d);if("true"==editColor){var h=document.createElement("select");for(i=0;i<colors.length;i++)e=document.createElement("option"),e.value=colors[i],colors[i]==(0<paramColor.length?paramColor:cellVals[a].color)&&(e.selected=!0),e.style.backgroundColor=colors[i],g=document.createTextNode(colors[i]),e.appendChild(g),h.appendChild(e)}else h=document.createElement("hidden"),h.value=""==
paramColor?cellVals[a].color:paramColor;b.appendChild(h);f=document.createElement("button");f.style.width="25px";f.style.margin="1px";$(f).text("OK");$(f).bind("click",function(k){editInput2(d,h,f)})}b.appendChild(f);d.focus();d.focus()}}
function editBlock1(b,a,d){var c="";a=""==b.value?"true"==editColor?a.value:SYS_BACKGROUND:a.value;for(i=rowStart;i<=rowEnd;i++)for(j=colStart;j<=colEnd;j++)c=c+"["+i+"_"+j+"]";new Number(0);for(i=rowStart;i<=rowEnd;i++)for(j=colStart;j<=colEnd;j++){var e=(i-1)*colCount+(j-1);"1"==cellVals[e].cellEdit&&(theText=cellObjs[e].lastChild,theText.nodeValue=cellVals[e].location+(""==cellVals[e].location||""==b.value?"":separator)+b.value,cellObjs[e].style.backgroundColor=a,cellVals[e].color=a,cellVals[e].value=
b.value)}setData("0",null,"0",null,"1",b.value,"1",a,"0",null,c);d.parentNode.removeChild(d);fgEditing="0"}
function editBlock2(b,a){var d="";for(i=rowStart;i<=rowEnd;i++)for(j=colStart;j<=colEnd;j++)d=d+"["+i+"_"+j+"]";var c;new Number(0);for(i=rowStart;i<=rowEnd;i++)for(j=colStart;j<=colEnd;j++){for(c=(i-1)*colCount+(j-1);cellObjs[c].hasChildNodes();)cellObjs[c].removeChild(cellObjs[c].firstChild);if("true"==showStatus&&""!=cellVals[c].status){var e=document.createElement("img");e.src="../image/status_"+cellVals[c].status+".png";e.style.width="15px";e.style.height="15px";cellObjs[c].appendChild(e)}cellObjs[c].appendChild(document.createTextNode(cellVals[c].location+
(""==cellVals[c].location||""==cellVals[c].value?"":separator)+cellVals[c].value));"1"==b.value?(cellVals[c].cellEdit="1",cellObjs[c].style.backgroundColor=cellVals[c].color):(cellVals[c].cellEdit="0",cellObjs[c].style.backgroundColor=notEditBgColor)}setData("0",null,"1",b.value,"0",null,"0",null,"0",null,d);a.parentNode.removeChild(a);fgEditing="0"}
function editStatus(b){if(null==b)return!1;theImg=b.srcElement||b.target;if("IMG"==theImg.tagName){var a=(theImg.parentNode.row-1)*colCount+(theImg.parentNode.col-1),d=document.createElement("div");d.style.position="absolute";d.style.left=b.x;d.style.top=b.y;d.style.width=40*countStatus+50+"px";d.style.height="20px";d.align="center";d.style.border="solid black 1px";d.style.padding="5";d.style.backgroundColor="lightSkyBlue";for(i=0;i<=countStatus;i++){var c=document.createElement("input");c.type="radio";
c.statusVal=i;$(c).bind("click",function(e){editRadio(b,a)});d.appendChild(c);c=document.createElement("img");c.src="../image/status_"+i+".png";c.style.width="15px";c.style.height="15px";d.appendChild(c)}c=document.createElement("img");c.src="../image/status_Close.png";c.style.width="15px";c.style.height="15px";c.style.marginLeft="30px";$(c).bind("click",function(e){clearBlock(d)});d.appendChild(c);mTable.parentNode.appendChild(d)}}
function clearBlock(b){new Number(0);if(0<rowStart&&0<rowEnd&&0<colStart&&0<colEnd)for(i=rowStart;i<=rowEnd;i++)for(j=colStart;j<=colEnd;j++){var a=(i-1)*colCount+(j-1);cellObjs[a].style.backgroundColor="1"==cellVals[a].cellEdit?cellVals[a].color:notEditBgColor}b.parentNode.removeChild(b);fgEditing="0"}
function mouseDown(b){if(null==b||"1"==fgEditing)return!0;dragEvent=!0;for(startCell=b.srcElement?b.srcElement:b.target;"TD"!=startCell.tagName&&startCell.parentNode;)startCell=startCell.parentNode;if("TD"!=startCell.tagName)return startCell=null,!1;document.all?(mouseX=event.x,mouseY=event.y):(mouseX=b.pageX,mouseY=b.pageY)}
function mouseMove(b){if(null==b||!dragEvent||document.all&&mouseX==event.x&&mouseY==event.y||mouseX==b.pageX&&mouseY==b.pageY)return!1;for(endCell=b.srcElement?b.srcElement:b.target;"TD"!=endCell.tagName&&endCell.parentNode;)endCell=endCell.parentNode;if("TD"!=endCell.tagName||null==startCell||null==mTable||mouseX==b.x&&mouseY==b.y)return event.returnValue=!1;rowStart=new Number(parseInt(startCell.row)<=parseInt(endCell.row,10)?startCell.row:endCell.row);colStart=new Number(parseInt(startCell.col)<=
parseInt(endCell.col,10)?startCell.col:endCell.col);rowEnd=new Number(parseInt(startCell.row)<=parseInt(endCell.row,10)?endCell.row:startCell.row);colEnd=new Number(parseInt(startCell.col)<=parseInt(endCell.col,10)?endCell.col:startCell.col);for(i=0;i<cellObjs.length;i++)cellObjs[i].style.backgroundColor="1"==cellVals[i].cellEdit?cellVals[i].color:notEditBgColor;for(i=rowStart;i<=rowEnd;i++)for(b=(i-1)*colCount+(colEnd-1),j=(i-1)*colCount+(colStart-1);j<=b;j++)if("1"==cellVals[j].cellEdit||editMode==
SYS_MODE_LAYOUT)cellObjs[j].style.backgroundColor="lightblue";return!1}
function mouseUp(b){if(null==b)return!1;dragEvent=!1;for(endCell=b.srcElement?b.srcElement:b.target;"TD"!=endCell.tagName&&endCell.parentNode;)endCell=endCell.parentNode;if(null==startCell||mouseX==b.pageX&&mouseY==b.pageY||document.all&&mouseX==event.x&&mouseY==event.y)return startCell=null,!1;rowStart=new Number(parseInt(startCell.row)<=parseInt(endCell.row,10)?startCell.row:endCell.row);colStart=new Number(parseInt(startCell.col)<=parseInt(endCell.col,10)?startCell.col:endCell.col);rowEnd=new Number(parseInt(startCell.row)<=
parseInt(endCell.row,10)?endCell.row:startCell.row);colEnd=new Number(parseInt(startCell.col)<=parseInt(endCell.col,10)?endCell.col:startCell.col);b=(rowStart-1)*colCount+(colStart-1);var a=!1;for(i=rowStart;i<=rowEnd;i++){var d=(i-1)*colCount+(colEnd-1);for(j=(i-1)*colCount+(colStart-1);j<=d;j++)if("1"==cellVals[j].cellEdit||editMode==SYS_MODE_LAYOUT)a=!0}if(a){fgEditing="1";var c=document.createElement("div");d=1*inputWidth+100;var e=endCell.offsetLeft+endCell.offsetWidth,g=endCell.offsetTop+endCell.offsetHeight;
e=1*e+1*d+20>winWidth?e-d:e;g=1*g+1*divHeight+20>winHeight?g-divHeight:g;c.style.position="absolute";c.style.left=e;c.style.top=g;c.style.width=d+"px";c.style.height=divHeight+"px";c.align="center";c.style.border="solid black 1px";c.style.padding="5";c.style.backgroundColor="lightSkyBlue";if(a&&editMode==SYS_MODE_VALUE){var f=document.createElement("input");f.style.width=inputWidth;f.style.border="solid gray 1px";$(startCell).text().split(separator);f.value=null!=paramVal&&""!=paramVal?paramVal:cellVals[b].value;
c.appendChild(f);if("true"==editColor){var h=document.createElement("select");for(i=0;i<colors.length;i++)d=document.createElement("option"),d.value=colors[i],colors[i]==(0<paramColor.length?paramColor:cellVals[b].color)&&(d.selected=!0),d.style.backgroundColor=colors[i],a=document.createTextNode(colors[i]),d.appendChild(a),h.appendChild(d)}else h=document.createElement("hidden"),h.value=""==paramColor?cellVals[b].color:paramColor;c.appendChild(h);a=document.createElement("br");c.appendChild(a);a=
document.createElement("button");a.style.width="55px";a.style.margin="3px";$(a).text("OK");$(a).bind("click",function(l){editBlock1(f,h,c)});c.appendChild(a);a=document.createElement("button");a.style.width="55px";a.style.margin="3px";$(a).text("Cancel");$(a).bind("click",function(){clearBlock(c)});c.appendChild(a);mTable.parentNode.appendChild(c);f.focus()}if(editMode==SYS_MODE_LAYOUT){var k=document.createElement("select");d=document.createElement("option");d.value="1";"1"==cellVals[b].cellEdit&&
(d.selected="true");a=document.createTextNode(SYS_EDIT);d.appendChild(a);k.appendChild(d);d=document.createElement("option");d.value="0";"0"==cellVals[b].cellEdit&&(d.selected="true");a=document.createTextNode(SYS_NOTEDIT);d.appendChild(a);k.appendChild(d);c.appendChild(k);a=document.createElement("br");c.appendChild(a);a=document.createElement("button");a.style.width="55px";a.style.margin="1px";$(a).text("OK");$(a).bind("click",function(l){editBlock2(k,c)});c.appendChild(a);a=document.createElement("button");
a.style.width="55px";a.style.margin="3px";$(a).text("Cancel");$(a).bind("click",function(l){clearBlock(c)});c.appendChild(a);mTable.parentNode.appendChild(c);k.focus()}}startCell=null;return!1}
function adjustMatrix(){var b=document.getElementById("viewTable");if(null==b)return!1;var a=document.getElementsByTagName("iMatrix");if(null==a||0==a.length)return!1;if(null!=a[0].getAttribute("mode")&&(editMode=a[0].getAttribute("mode"),editMode!=SYS_MODE_LAYOUT&&editMode!=SYS_MODE_VALUE))return alert("The mode attribute can't be set as the price of anything but [Layout,Value]."),!1;null!=a[0].getAttribute("countPro")&&(countPro=new Number(a[0].getAttribute("countPro")));null!=a[0].getAttribute("cellWidth")&&
(cellWidth=a[0].getAttribute("cellWidth"),cellWidth=cellWidth.replace(/\D/gi,""));null!=a[0].getAttribute("cellHeight")&&(cellHeight=a[0].getAttribute("cellHeight"),cellHeight=cellHeight.replace(/\D/gi,""));null!=a[0].getAttribute("inputWidth")&&(inputWidth=a[0].getAttribute("inputWidth"),inputWidth=inputWidth.replace(/\D/gi,""));null!=a[0].getAttribute("countStatus")&&(countStatus=a[0].getAttribute("countStatus"));null!=a[0].getAttribute("showStatus")&&(showStatus=a[0].getAttribute("showStatus"));
null!=a[0].getAttribute("writable")&&(writable=a[0].getAttribute("writable"));null!=a[0].getAttribute("editColor")&&(editColor=a[0].getAttribute("editColor"));null!=a[0].getAttribute("separator")&&(separator=a[0].getAttribute("separator"));null!=a[0].getAttribute("paramVal")&&(paramVal=a[0].getAttribute("paramVal"));null!=a[0].getAttribute("paramColor")&&(paramColor=a[0].getAttribute("paramColor"));null!=a[0].getAttribute("notEditBgColor")&&(notEditBgColor=a[0].getAttribute("notEditBgColor"));a=b.parentNode;
var d=b.rows.length,c=b.rows[0].cells.length,e=d*c/countPro,g=0;cellObjs=Array(e);cellVals=Array(e);for(i=0;i<d;i++)for(j=0;j<c;j+=countPro)cellVals[g++]=new dataNode($(b.rows[i].cells[j]).text(),$(b.rows[i].cells[j+1]).text(),$(b.rows[i].cells[j+2]).text(),$(b.rows[i].cells[j+3]).text(),$(b.rows[i].cells[j+4]).text(),$(b.rows[i].cells[j+5]).text(),"true"==showStatus?$(b.rows[i].cells[j+6]).text():null);mTable=document.createElement("table");d=1;mTable.id="MatrixTable";mTable.style.border="solid black 1px";
mTable.style.borderCollapse="collapse";mTable.style.margin="10px";mTable.style.cellspacing="0";mTable.style.cellspadding="0";"true"==writable&&($(mTable).bind("mousedown",function(f){mouseDown(f||event)}),$(mTable).bind("mousemove",function(f){mouseMove(f||event);return!1}),$(mTable).bind("mouseup",function(f){mouseUp(f||event);return!1}),$(mTable).bind("dblclick",function(f){editTd(f||event);return!1}));for(i=0;i<e;){for(c=mTable.insertRow(mTable.rows.length);cellVals[i].row==d&&(cellObjs[i]=c.insertCell(c.cells.length),
cellObjs[i].style.width=cellWidth,cellObjs[i].style.height=cellHeight,cellObjs[i].style.border="solid lightgrey 1px",cellObjs[i].row=cellVals[i].row,cellObjs[i].col=cellVals[i].col,"true"==showStatus&&""!=cellVals[i].status&&(g=document.createElement("img"),g.src="../image/status_"+cellVals[i].status+".png",g.style.width="15px",g.style.height="15px","true"==writable&&"1"==cellVals[i].cellEdit&&editMode==SYS_MODE_VALUE&&$(g).bind("dblclick",function(f){editStatus(event||f);return!1}),cellObjs[i].appendChild(g)),
cellObjs[i].appendChild(document.createTextNode(cellVals[i].location+(""==cellVals[i].location||""==cellVals[i].value?"":separator)+cellVals[i].value)),cellObjs[i].style.backgroundColor="1"==cellVals[i].cellEdit?cellVals[i].color:notEditBgColor,i++,i!=e););if(i==e)break;d=cellVals[i].row}rowCount=mTable.rows.length;colCount=mTable.rows[0].cells.length;a.insertBefore(mTable,b);a.removeChild(b)};
