function SimplexP(m,n,lb){
	this.str;
	this.m=m;
	this.n=n;
	this.lb=lb;
	this.M=M;
	this.S=S;
	this.ff=ff;
	this.tabl=tabl;	
}



function M(){
	var A=[],b=[],tester;
	for (var j=1; j<=this.n+this.m+1; j++){
		for (var i=1; i<=this.m+1; i++){
			tester=eval('document.st.a'+i+'a'+j+'.value');
			if(isNaN(tester)){
				alert("Ошибка ввода данных!");
				return -1;
			}
			b.push(tester);
		}
		A.push(b);
		b=[];
	}
return A;
}

function S(){
var A=this.M();
var s="<table border=1 width=100 height=70><caption align='top'><h3>Выбор оптимальных стратегий</h3></caption><tr>";
for (var j=1; j<=this.n+this.m; j++)
   s+="<th width=1 height=1 >x"+j+"</th>";
s+="<th width=1 height=1 >value</th>";
s+="<th width=1 height=1 >basis</th></tr>";
for (var i=1; i<=this.m+1; i++){
   s+='<tr>';
   for (var j=1; j<=this.n+this.m+1; j++)
              s+="<td width=1 height=1><input type='text' name=a"+i+'a'+j+" value="+A[j-1][i-1]+" size=10>";
if (i<this.m+1)
  s+="<td width=1 height=1 ><input type='text' name=x"+i+" value=x"+this.lb[i-1]+" size=10></td></tr>"; 
 else 
  s+="<th width=1 height=1 >Z</th></tr>"; 
}
s+="</table><br>";
return s;
}



function ff(){
	Mr();
	var A=this.M(),tp=Infinity,par=new Array(this.m+this.n),maxmin=Infinity;
        if (A==-1) return;
	var a=A[0][this.m],ltp=[],vr=[],qar=new Array(this.m),sm=0,minmax=0,sp=0,sq=0;
	if (plombir) {
		this.str=''+this.S();
		plombir=0;
	}
	for (var s=1; s<this.n+this.m; s++)
		a=Math.max(a,A[s][this.m]);
	if (a<=0){
		//alert('Game over!Press OK to get results!');
		for (var ll=0; ll<this.m; ll++)
			par[lb[ll]-1]=-A[this.n+this.m][ll]/A[this.n+this.m][this.m];	
		for (var kk=0; kk<this.m; kk++)
			qar[kk]=A[this.n+kk][this.m]/A[this.n+this.m][this.m];	
		for (var j=0; j<this.n; j++){
			for (var i=0; i<this.m; i++)
				sp+=Mx[j][i]*qar[i];
			maxmin=Math.min(maxmin,sp);
			sp=0;
		}		
		for (var j=0; j<this.m; j++){
			for (var i=0; i<this.n; i++)
				sq+=Mx[i][j]*((par[i]) ? par[i]:0);
			minmax=Math.max(minmax,sq);
			sq=0;
		}		

		var ss='';
		ss+='<br>Player A: <br>';
		for (var rr=0; rr<this.m; rr++)
			ss=ss+'p<sub>'+(rr+1)+'</sub>='+qar[rr]+'<br>';
		ss=ss+'maximin='+maxmin+'<br>';
		ss+='<br>Player B: <br>';
		for (var ii=0; ii<this.n; ii++)
			ss=ss+'q<sub>'+(ii+1)+'</sub>='+((par[ii]) ? par[ii]:0)+'<br>';
		ss=ss+'minimax='+minmax+'<br>';
		for (var j=0; j<this.n; j++){
			for (var i=0; i<this.m; i++)
				sm+=Mx[j][i]*((par[j]) ? par[j]:0)*qar[i];
		}
		ss+='<br>'+'Game Amount='+sm;
		this.str+='<br>'+ss;
		document.write(this.str);	
	}
	else{
		for (var j=0; j<this.n+this.m; j++){
			if (A[j][this.m]==a){
				for (var i=0; i<this.m; i++){
					if (A[j][i]>0){
						tp=Math.min(tp,A[this.n+this.m][i]/A[j][i]);
						ltp.push(tp);
					}
					else
						ltp.push(Infinity);
				}
				for (var i=0; i<this.m; i++)
					if (ltp[i]==tp) break;
				break;
			}
		}
		this.lb[i]=j+1;
		var ed=1/A[j][i];
		for (var k=0; k<this.n+this.m+1; k++)
			A[k][i]*=ed;

		for (var p=0; p<this.m+1; p++){
			if (p!=i){
				for (var s=0; s<this.n+this.m+1; s++)
					vr[s]=A[s][i]*A[j][p];
				for (var l=0; l<this.n+this.m+1; l++)
					A[l][p]-=vr[l];

			}
		}

		for (var v=1; v<=this.n+this.m+1; v++){
			for (var u=1; u<=this.m+1; u++)
				eval("document.st.a"+u+"a"+v+".value="+A[v-1][u-1]);
			
		}
		for (var r=1; r<this.m+1; r++)
			eval("document.st.x"+r+".value="+" 'x'+this.lb[r-1]"); 
		this.str+=this.S();
		this.ff();
	}
}



function tabl(){
	var s="<form name='st'><table border=1 width=100 height=70><caption align='top'><h3>Выбор оптимальных стратегий</h3></caption><tr>";
	for (var j=1; j<=this.n+this.m; j++)
		s=s+"<th width=1 height=1 >x"+j+"</th>";
	s+="<th width=1 height=1 >value</th>";
	s+="<th width=1 height=1 >basis</th></tr>";
	for (var i=1; i<this.m+2; i++){
		s+='<tr>';
		if (i!=this.m+1){
			for (var j=1; j<this.n+this.m+2; j++){
				if ((j>this.n) && (j<=this.n+this.m)){
					if (j-i==this.n)
						s=s+"<td width=1 height=1><input type='text' name=a"+i+'a'+j+" value=1 size=10>";
					else
						s=s+"<td width=1 height=1><input type='text' name=a"+i+'a'+j+" value=0 size=10>";
				}
				else if (j==this.n+this.m+1)
					s=s+"<td width=1 height=1><input type='text' name=a"+i+'a'+j+" value=1 size=10>";
				else
					s=s+"<td width=1 height=1><input type='text' name=a"+i+'a'+j+" size=10>";
			}		
			s=s+"<td width=1 height=1 ><input type='text' name=x"+i+" value=x"+this.lb[i-1]+" size=10></td></tr>";
		}			 
		else{
			for (var j=1; j<this.n+this.m+2; j++){
				if ((j>this.n) && (j<=this.n+this.m+1))
					s=s+"<td width=1 height=1><input type='text' name=a"+i+'a'+j+" value=0 size=10>";
				else
					s=s+"<td width=1 height=1><input type='text' name=a"+i+'a'+j+" value=1 size=10>";
			}
			s+="<th width=1 height=1 >Z</th></tr></table>"; 
		}
	}
	s+="<br><input type='button' value='Заполни таблицу неотрицательными числами и жми на меня' onClick= ff()></form>";
	return s;
}
