var currentRpan=0;
var osplits=new Array();
// start position for each panel (master,0,1,2,...7=9 in total)
var xpan=new Array();
for (i=0;i<9;i++) {
	xpan[i]=0;
}
// number of lines for each panel (master,0,1,2,...7=9 in total)
var xnr=new Array();
for (i=0;i<9;i++) {
	xnr[i]=0;
}
function showRpan(j)
{
	for (i=0;i<8;i++) {
		panelId='rpan'+i;
		if (i<j || i>=j+3) {
			document.all[panelId].style.visibility='hidden';
			document.all[panelId].style.display='none';
		}
		else {
			document.all[panelId].style.top=(i-j)*180;
			document.all[panelId].style.visibility='visible';
			document.all[panelId].style.display='inline';
		}
	}
	currentRpan=j;
	splitRedraw();
}
function wzDrop()
{
	var ok=false;

	if (event.x>=499) {
		if (event.y>=30 && event.y<180) {
			splitReset(dd.obj.id, currentRpan+1);
			ok=true;
		}
		else if (event.y>=210 && event.y<360) {
			splitReset(dd.obj.id, currentRpan+2);
			ok=true;
   	}
		else if (event.y>=390 && event.y<540) {
			splitReset(dd.obj.id, currentRpan+3);
			ok=true;
   	}
	}
	else if ((event.x>=62 && event.x<428) && (event.y>=30 && event.y<480)) {
		splitReset(dd.obj.id, 0);
		ok=true;
   }

	if (ok)
		splitRedraw();
	else
		dd.obj.moveTo( dd.obj.defx, dd.obj.defy );
}
function osplitGetIdStr()
{
	var idstr='oline'+this.id;
	if (this.subid>0)
		idstr+='_'+this.subid;

	return idstr;
}
function osplit(xordline,xqua,xzyg)
{
	this.panel=0; // master
	this.id=xordline;
	this.subid=0;
	this.tqua=xqua;
	this.qua=xqua;
	this.zyg=xzyg; // Ζυγιζομενο ναι/οχι
	this.idstr=osplitGetIdStr;
}
function targetExists(idn,paneln)
{
	var n=osplits.length;
	for (var i=0;i<n;i++) {
		if (osplits[i].id==idn && osplits[i].panel==paneln)
			return i;
	}
	return -1;
}
function splitReset(ids,j)
{
	var ar=ids.substr(5).split('_');
	var idn=ar[0];
	var subidn=(ar.length==2?ar[1]:0);

	var n=osplits.length;
	for (var i=0;i<n;i++) {
		if (osplits[i].id!=idn || osplits[i].subid!=subidn)
			continue;

		// found
		if (osplits[i].panel==j)
			return;
		else if (osplits[i].panel==0 && j>0 && (osplits[i].qua>1 || osplits[i].zyg)) {
			var url='/ishop/restau_info.php?FUNCTION=11&ORDLINE='+osplits[i].id+'&QUA='+osplits[i].qua+'&ZYG='+osplits[i].zyg;
			var args=null;
			ret=window.showModalDialog(url,args,'help:no;dialogHeight:400px;dialogWidth:500px;');
			if (ret!=undefined) {
				if (ret[0]==0) { // partial
					if ((e1=targetExists(idn,j)) >= 0) {
						osplits[e1].qua += parseFloat(ret[1]);
						e1s='oline'+idn+'_'+j;
						document.all[e1s].children[0].cells[2].innerText=osplits[e1].qua;

						osplits[i].qua -= parseFloat(ret[1]);
						document.all[ids].children[0].cells[2].innerText=osplits[i].qua;
					}
					else {
						var o2=new osplit(idn, osplits[i].qua, osplits[i].zyg);
						o2.qua=parseFloat(ret[1]);
						o2.tqua=osplits[i].tqua;
						o2.subid=j;
						o2.panel=j;
						osplits[n]=o2;

            	   osplits[i].qua -= parseFloat(ret[1]);
						document.all[ids].children[0].cells[2].innerText=osplits[i].qua;

						odiv=document.createElement('DIV');
						odiv.id=o2.idstr();//'oline'+o2.id+'_'+o2.subid;
						odiv.className='SplitLine';
						odiv.innerHTML=document.all[ids].innerHTML;
						odiv.children[0].cells[2].innerText=ret[1];
						document.body.appendChild(odiv);
						ADD_DHTML(odiv.id);
					}
				}
				else { // ret[0]=1 --> ALL
					if (osplits[i].qua==osplits[i].tqua) {
						osplits[i].panel=j;
					}
					else { // lesser quantity
						if ((e1=targetExists(idn,j)) >= 0) { // exists: do further checks
							if (osplits[i].qua + osplits[e1].qua == osplits[i].tqua) {
								// Sum of quantities=Total: reset master quantity, remove subid
								osplits[i].qua=osplits[i].tqua;
								osplits[i].panel=j;
								document.all[ids].children[0].cells[2].innerText=osplits[i].qua;

								osplits.splice(e1,1);
								e1s='oline'+idn+'_'+j;
								document.body.removeChild(document.all[e1s]);
							}
							else { // add quantity to subid, remove master
								osplits[e1].qua += osplits[i].qua;
								e1s='oline'+idn+'_'+j;
								document.all[e1s].children[0].cells[2].innerText=osplits[e1].qua;

								osplits.splice(i,1);
								document.body.removeChild(document.all[ids]);
							}
						}
						else { // subid does not exist: convert master to subid
							osplits[i].subid=j;
							osplits[i].panel=j;

							odiv=document.createElement('DIV');
							odiv.id='oline'+idn+'_'+j;
							odiv.className='SplitLine';
							odiv.innerHTML=document.all[ids].innerHTML;
							odiv.children[0].cells[2].innerText=ret[1];
							document.body.appendChild(odiv);
							ADD_DHTML(odiv.id);

							document.body.removeChild(document.all[ids]);
						}
					}
				}
			}
		}
		else {
			if ((e1=targetExists(idn,j)) >= 0) {
				// remove old div
				document.body.removeChild(document.all[ids]);
				// Sum of quantities=Total:
				// reset master quantity (if exists), remove subid, create master if not exists
				osplits[e1].qua += osplits[i].qua;
				// old id string
				e1s=osplits[e1].idstr();
				document.all[e1s].children[0].cells[2].innerText=osplits[e1].qua;
				if (osplits[e1].qua==osplits[e1].tqua && osplits[e1].subid>0) {
					osplits[e1].subid=0;
					id2='oline'+idn; // new id string
					document.all[e1s].id=id2;
					ADD_DHTML(id2);
				}
				// remove old array entry
				osplits.splice(i,1);
			}
			else {
				if (osplits[i].subid>0)
					osplits[i].subid=j;
				osplits[i].panel=j;
				e1s=osplits[i].idstr(); //'oline'+idn+'_'+j; // new id string
				if (e1s!=ids) {
					document.all[ids].id=e1s;
					ADD_DHTML(e1s);
				}
			}
		}
		break;
	}
}
function splitDone()
{
   var f1=document.maindata;

   // Find min panel
   var minPanel=8;
   var n=osplits.length;
   for (var i=0;i<n;i++) {
      if (osplits[i].panel < minPanel)
         minPanel=osplits[i].panel;
   }

   // Reparse, attaching values
   for (i=0;i<n;i++) {
      var o1=document.createElement('INPUT');
  	   o1.type='hidden';
  	   o1.name='PANEL[]';
  	   o1.value=osplits[i].panel - minPanel;
  	   f1.appendChild(o1);

  	   var o1=document.createElement('INPUT');
  	   o1.type='hidden';
  	   o1.name='QUA[]';
  	   o1.value=osplits[i].qua;
  	   f1.appendChild(o1);

  	   var o1=document.createElement('INPUT');
  	   o1.type='hidden';
  	   o1.name='TQUA[]';
  	   o1.value=osplits[i].tqua;
  	   f1.appendChild(o1);

  	   var o1=document.createElement('INPUT');
  	   o1.type='hidden';
  	   o1.name='ID[]'; // order line
  	   o1.value=osplits[i].id;
  	   f1.appendChild(o1);
   }
   f1.submit();
}
function resetScrollbars()
{
	// Refresh scrollbars
	for (i=0;i<9;i++) {
		dh=(i==0?15:5);
		th=(i==0?450:150);
		if (xnr[i]<=dh)
			h=th;
		else
			h=dh*th/xnr[i];
		if (h<15)
			h=15;
			
		if (xnr[i]==0)
			sh=0;
		else
			sh=th*xpan[i]/xnr[i];

		id='sc'+i+'Div';
		if ((obj=document.all[id])!=undefined) {
			obj.style.height=h;
			obj.style.top=sh;
		}

		// color of buttons
		if (i>0) {
			id='pnav'+i;
			if ((obj=document.all[id])!=undefined)
				obj.style.color=(xnr[i]==0?'#C0C0C0':'#80FF80');
		}
	}
}
function splitRedraw()
{
	// reset number of lines per panel
	for (var i=0;i<9;i++)
		xnr[i]=0;

	// Scan once: how many in each panel?
	var n=osplits.length;
	for (i=0;i<n;i++) {
		o1=osplits[i];
		xnr[o1.panel]++;
	}
	
	// Recalc xpan
	for (i=0;i<9;i++) {
		dh=(i==0?15:5);
		if (xnr[i]<=dh)
			xpan[i]=0;
		else if (xpan[i] > (xnr[i] - dh))
			xpan[i]=xnr[i]-5;
	}

	// Reparse osplits
	var xc=new Array(9); // panels master,0,1,2,...7=9 in total
	for (i=0;i<9;i++)
		xc[i]=0;

	n=osplits.length;
	for (i=0;i<n;i++) {
		o1=osplits[i];
		id=o1.idstr();

		showdiv=false;
		if (o1.panel==0) {
			if (xc[0] >= xpan[0] && (xc[0] - xpan[0]) < 15) {
				showdiv=true;
				x=62;
				y=30 + (xc[0] - xpan[0])*30;
			}
			xc[0]++;
		}
		else if (o1.panel>=(currentRpan+1) && o1.panel<=(currentRpan+3)) {
         if (xc[o1.panel] >= xpan[o1.panel] && (xc[o1.panel] - xpan[o1.panel]) < 5) {
				showdiv=true;
				x=499;
				y=(o1.panel-currentRpan-1)*180 + 30 + (xc[o1.panel] - xpan[o1.panel])*30;
			}
			xc[o1.panel]++;
		}

      obj=dd.elements[id];
		if (showdiv) {
			document.all[id].style.width=(o1.panel==0?366:296);
			obj.show();
			obj.defx=x;
         obj.defy=y;
         obj.moveTo( obj.defx, obj.defy );
		}
		else {
			obj.hide();
		}
	}

	resetScrollbars();
}
function splitUp(j) {
	if (xpan[j]>0) {
		xpan[j]--;
		splitRedraw();
	}
}
function splitDown(j) {
	dh=(j==0?15:5);
	if (xpan[j]<xnr[j]-dh) {
		xpan[j]++;
		splitRedraw();
	}
}
function onSplitLoad() {
	resetScrollbars();
}
