function checnum(as)
{
	var dd = as.value;

	if(dd.lastIndexOf(' ')>=0){
	  dd = dd.replace(' ','');
	  as.value = dd;
	}
	
	if(isNaN(dd))
	{
		dd = dd.substring(0,(dd.length-1));
		as.value = dd;
	}		
}

var x=1;
function add()
{
 	var s = document.getElementById('a').getElementsByTagName("TBODY")[0];
 	var trow = document.createElement("tr");
 	var tcol1 = document.createElement("td");
 	var tcol2 = document.createElement("td");
	
	var d = document.createElement("input");
	d.setAttribute('type','text');
	d.setAttribute('size','10');
	d.setAttribute('name','xvalu'+x);
	d.setAttribute('onkeyup','checnum(this)');
	d.style.backgroundColor= "#ffffff";
	d.style.border="1px #009bff solid";
	d.style.color = "#000000";
	d.style.fontWeight = "bold";


	var e = document.createElement("input");
	e.setAttribute('type','text');
	e.setAttribute('size','10');
	e.setAttribute('name','yvalu'+x);
	e.setAttribute('onkeyup','checnum(this)');
	e.style.backgroundColor= "#ffffff";
	e.style.border="1px #009bff solid";
	e.style.color = "#000000";
	e.style.fontWeight = "bold";

	tcol1.appendChild(d);
	tcol2.appendChild(e);
	trow.appendChild(tcol1);
	trow.appendChild(tcol2);
	s.appendChild(trow);

x++;
}

function del()
	{
		if(x>1){
			var len = x.length;
			var s = document.getElementById('a').getElementsByTagName("TBODY")[0];
			s.removeChild(s.lastChild);
			x=x-1;		
		}
	}

function calculate()
{
	var aa = 0;
	var bb = 0;
	var cc = 0;
	var dd = 0;
	var ee = 0;	
 	for(var y=0; y<x; y++)
 	{
		
		var xxx = document.getElementById('a').getElementsByTagName("TBODY")[0];
		var a = xxx.getElementsByTagName("input").item(y*2).value;
		var b = xxx.getElementsByTagName("input").item(1+y*2).value;
		aa = aa + parseInt(a);
		bb = bb + parseFloat(b);
		cc = cc + (a*b);
		dd = dd + (a*a); 
		//ee = ee + (b*b); 
		var f = ((x*cc)-(aa*bb));
		var g = ((x*dd)-(aa*aa));	
		if(g==0)
		{
			document.first.r1.value = x;
			document.first.r2.value = "Invalid";	
			document.first.r3.value = "Invalid";
			document.first.r4.value = "Invalid";		
		}
		else{
			var ff = f/g;
			var gg = (bb-(ff*aa))/x;

			document.first.r1.value = x;
			var ff1 = Math.round(ff*100000)/100000;
			document.first.r2.value = ff1;	
			var gg1 = Math.round(gg*100000)/100000;
			document.first.r3.value = gg1;
			if(ff < 0)
			var hh = Math.round(gg1*100)/100 + '-' + Math.round(ff1*100)/100 + 'x';
			else
			var hh = Math.round(gg1*100)/100 + '+' + Math.round(ff1*100)/100 + 'x';
			document.first.r4.value = hh;		
		}
		
	}


}




function color(test)
{

	for(var j=2; j<10; j++)
	{
		var myI=document.getElementsByTagName("input").item(j);
		//myI.setAttribute("style",ch);
		myI.style.backgroundColor=test;
	}
}


function color1(test)
{
var myI=document.getElementsByTagName("table").item(0);
//myI.setAttribute("style",ch);
myI.style.backgroundColor=test;
}




