function any_pop(url,width,height)
	{
		newWin = window.open(url, "", "width="+ width +",height="+ height +",toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes");
		newWin.focus();
	}
	


function confirmDelete(delUrl) {
  if (confirm("Are you sure you want to delete")) {
    document.location = delUrl;
  }
}

function show(ii,jj)
{
if (document.getElementById(ii).checked)
	document.getElementById(jj).style.display = 'block';
else
	document.getElementById(jj).style.display = 'none';
}

function showRow(){
	var str;
	var srr1='';
	var tmp;
	var jj;
	var descr = new Array();
	var qty = new Array();
	var rate = new Array();
	var tot = new Array();
	var ii;
	d2=document.getElementById("itemCount");
	noOfRows=parseInt(d2.value);
	for (ii=0;ii<noOfRows;ii++)
	{
		jj=ii+1;
		tmp="itemDescription"+jj;
		d=document.getElementById(tmp);
		descr[ii]=d.value;
		tmp="itemQTY"+jj;
		d=document.getElementById(tmp);
		qty[ii]=d.value;
		tmp="itemRate"+jj;
		d=document.getElementById(tmp);
		rate[ii]=d.value;
		tmp="itemTotal"+jj;
		d=document.getElementById(tmp);
		tot[ii]=d.value;
	}
	noOfRows=noOfRows+1;
	d1=document.getElementById("rows");
	str=d1.innerHTML;
	//str=String(str).substring(0,String(str).length-16);
    alert(str);
	str=str+'<tr><td width=8 align=center>'
	str=str+noOfRows
	str=str+'</td><td width=65% align=center><INPUT type="text" id="itemDescription'+noOfRows+'" name="itemDescription'+noOfRows+'" size=50 maxlength=120 onfocus=actb(document.getElementById("itemDescription'+noOfRows+'"),customarray);></td>'
	str=str+'<td><INPUT type="text" style="text-align:center"  id="itemQTY'+noOfRows+'"  name="itemQTY'+noOfRows+'" size=3 onblur=calcTotal('+noOfRows+')></td>'
	str=str+'</td><td><INPUT type="text" style="text-align:center" id="itemRate'+noOfRows+'" name="itemRate'+noOfRows+'" size=3 onblur=calcTotal('+noOfRows+')></td>'
	str=str+'</td><td><INPUT type="text" style="text-align:right" id="itemTotal'+noOfRows+'" name="itemTotal'+noOfRows+'" size=6></td></tr>'
 //	str=str+'</tbody></table>'
	d2.value=noOfRows;
	d1.innerHTML=str;
	//d1=document.getElementById("span1");
	//str1=d1.innerHTML;
	//str1=str1+'<script>actb(document.getElementById("itemDescription'+noOfRows+'"),customarray);</script>'
	//d1.innerHTML=str1;
	for (ii=0;ii<noOfRows-1;ii++)
	{
		jj=ii+1;
		tmp="itemDescription"+jj;
		d=document.getElementById(tmp);
		d.value=descr[ii];
		tmp="itemQTY"+jj;
		d=document.getElementById(tmp);
		d.value=qty[ii];
		tmp="itemRate"+jj;
		d=document.getElementById(tmp);
		d.value=rate[ii];
		tmp="itemTotal"+jj;
		d=document.getElementById(tmp);
		d.value=tot[ii];
	}
	jj=jj+1;
	tmp="itemDescription"+jj;
	d=document.getElementById(tmp);
	d.focus();
	document.location.href="#toprow";
}

function calcTotal(jj){
	var sTotal=0;
	var tot=0;

	var ii;
	d2=document.getElementById("itemCount");
	noOfRows=parseInt(d2.value);
    for (ii=1;ii<=noOfRows;ii++)
	{
		str="itemQty"+ii;
		d1=document.getElementById(str);
		str="itemRate"+ii;
		d2=document.getElementById(str);
		str="itemTotal"+ii;
		d3=document.getElementById(str);
		if (!isNaN(d1.value) && !isNaN(d2.value))
		{
			if (parseInt(d1.value)>1000000000)
			{
			alert("You cannot enter value greater than 1,000,000,000");
			d1.value="0";
			return;
			}
			if (d2.value>1000000000)
			{
			alert("You cannot enter value greater than 1,000,000,000");
			d2.value="0";
			return;
			}
			tot=d1.value*d2.value;
			d3.value=formatNumber(tot,2,"",".","","","-","");
			sTotal=sTotal+tot;
		}
		else
			d3.value="";
	}
	d4=document.getElementById("invoiceSubTotal");
	d4.value=formatNumber(sTotal,2,"",".","","","-","");
	calcTaxes();

}

function calcTaxes()
{
	var sT=0;
	var gT=0;
	var tt=0;
	var strTaxNames="";
	var strTaxValues="";
	var tax1=0;
	var tax2=0;
	var tax3=0;
	var tax4=0;

	ta1=document.getElementById("C1");
	ta2=document.getElementById("C2");
	tr1=document.getElementById("T2");
	tr2=document.getElementById("T4");
	t1=document.getElementById("T1");
	t2=document.getElementById("T3");


	d4=document.getElementById("invoiceSubTotal");
	sT=parseFloat(d4.value);

	if (!((tr1.value=="0")||(isNaN(tr1.value))||(tr1.value=="")))
	{
		if (ta1.checked)
		{
			strTaxNames=strTaxNames+t1.value+" ("+tr1.value+"%):<br>";
			tax1=sT*parseFloat(tr1.value)/100;
			strTaxValues=strTaxValues+formatNumber(tax1,2,"",".","","","-","")+"<br>";
		}
	}
	if (!((tr2.value=="0")||(isNaN(tr2.value))||(tr2.value=="")))
	{
		if (ta2.checked)
		{
			strTaxNames=strTaxNames+t2.value+" ("+tr2.value+"%):<br>";
			tax2=sT*parseFloat(tr2.value)/100;
			strTaxValues=strTaxValues+formatNumber(tax2,2,"",".","","","-","")+"<br>";
		}
	}

	gT=sT+tax1+tax2+tax3+tax4;
	tt=tax1+tax2+tax3+tax4;
	d1=document.getElementById("taxNames");
	d1.innerHTML=strTaxNames;
	d1=document.getElementById("taxValues");
	d1.innerHTML=strTaxValues;
	d1=document.getElementById("invoiceGrandTotal");
	d1.value=formatNumber(gT,2,"",".","","","-","");
	d1=document.getElementById("invoiceTotalTax");
	d1.value=formatNumber(tt,2,"",".","","","-","");
    calcBalance();
}

function calcBalance(){
	var sTotal=0;
	var tot=0;

    d1=document.getElementById("invoiceGrandTotal");
    d2=document.getElementById("invoicePaid");
    d3=document.getElementById("invoiceDue");

    if (!isNaN(d1.value) && !isNaN(d2.value))
    {
    	if (d2.value>1000000000)
    	{
    	alert("You cannot enter value greater than 1,000,000,000");
    	d2.value="0";
    	return;
    	}
    	tot=Number(d1.value)-Number(d2.value);
    	d3.value=formatNumber(tot,2,"",".","","","-","");
    }
}

function formatNumber(num,dec,thou,pnt,curr1,curr2,n1,n2)
{
	var x = Math.round(num * Math.pow(10,dec));
	if (x >= 0)
		n1=n2='';
	var y = (''+Math.abs(x)).split('');
	var z = y.length - dec;
	if (z<0)
		z--;
	for(var i = z; i < 0; i++)
		y.unshift('0');
	y.splice(z, 0, pnt);
	while (z > 3)
		{
		z-=3; y.splice(z,0,thou);
		}
	var r = curr1+n1+y.join('')+n2+curr2;
	return r;
}