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','valu'+x);
	d.style.backgroundColor= "#ffffff";
	d.style.border="1px #009bff solid";
	d.style.color = "#000000";
	d.style.fontWeight = "bold";

	//d.setAttribute('onkeyup','checknum(this)');
	
	var e = document.createElement("input");
	e.setAttribute('type','text');
	e.setAttribute('size','10');
	e.setAttribute('name','freq'+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 xx=0;
	var yy=0;
	var zz;
	
 	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;
		
		
		var arr = a.split("-");
		if(arr.length>2 || (arr[0].length)=="" || (arr[1].length)=="")
		{
			alert("The number \""+a+"\" is not valid. For example: type 10-20");
			break;
		}
			
	
		var mid = (parseInt(arr[0]) + parseInt(arr[1]))/2;	
			
		if(isNaN(arr[0]) || isNaN(arr[1]) || arr == "")
		{
			alert("The number \""+a+"\" is invalid.For example: type 10-20");
			break;
		}
		
		xx = xx+(mid*b);
		yy = yy+parseInt(b);
	}
	zz = xx/yy;
	var tot = (xx+"/"+yy);
	var zz1 = Math.round(zz*100000)/100000;
	document.first.r1.value = tot;
	document.first.r2.value = zz1;	

}




function color(test)
{
	for(var j=2; j<10; j++)
	{
		var myI=document.getElementsByTagName("input").item(j);
		myI.style.backgroundColor=test;
	}
}


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