var th = ['','thousand','million', 'billion','trillion'];
// uncomment this line for English Number System
// var th = ['','thousand','million', 'milliard','billion'];

var dg = ['zero','one','two','three','four', 'five','six','seven','eight','nine']; var tn = ['ten','eleven','twelve','thirteen', 'fourteen','fifteen','sixteen', 'seventeen','eighteen','nineteen']; 

var tw = ['twenty','thirty','forty','fifty', 'sixty','seventy','eighty','ninety']; 

function toWords(s)
{ 
	s = s.toString();
	s = s.replace(/[\, ]/g,''); if (s != parseFloat(s)) return 'not a number'; var x = s.indexOf('.'); if (x == -1) x = s.length; if (x > 15) return 'too big'; var n = s.split(''); var str = ''; var sk = 0; for (var i=0; i < x; i++) {if ((x-i)%3==2) {if (n[i] == '1') {str += tn[Number(n[i+1])] + ' '; i++; sk=1;} else if (n[i]!=0) {str += tw[n[i]-2] + ' ';sk=1;}} else if (n[i]!=0) {str += dg[n[i]] +' '; if ((x-i)%3==0) str += 'hundred ';sk=1;} if ((x-i)%3==1) {if (sk) str += th[(x-i-1)/3] + ' ';sk=0;}} if (x != s.length) {var y = s.length; str += 'point '; for (var i=x+1; i<y; i++) str += dg[n[i]] +' '; str+=' paise';} 
	var xx=str.replace(/\s+/g,' ');
	document.getElementById("excise_duty_inwords").value=xx;
	
	}

 
 
 
function cal_vatcst(id)
{
  	var vat_cst = document.getElementById('vat_cst').value;
	
 	var total_taxable = document.getElementById('total_taxable').value;
 
	var total = (parseFloat(vat_cst) * parseFloat(total_taxable) * .01);
 	 if(!isNaN(total)){
   		document.getElementById("calvatcst").value =total.toFixed(2);
	 }
	 else{
		    	document.getElementById("calvatcst").value =0;
		 }
	 
	
}


function mrp_assessible_price(id)
{
  	var qnty = document.getElementById('qty_'+id).value;
 	var itemrate = document.getElementById('mrp_assessible_price_'+id).value;
	var total = parseFloat(qnty) * parseFloat(itemrate)
	if(!isNaN(total))
	{
		document.getElementById("total_assessible_"+id).value =total.toFixed(2);
		document.getElementById("total_amount_"+id).value =total.toFixed(2);
	}
	else
	{
		document.getElementById("total_assessible_"+id).value =0;
		document.getElementById("total_amount_"+id).value =0;
	}
	
}


function cal_perunit_price(id)
{
	var amount=document.getElementById('perunit_price_'+id).value;
 	var Discount=document.getElementById('discount_'+id).value;
  	var MRP_price=document.getElementById('mrp_price_'+id).value;
	var qty =parseFloat(document.getElementById('qty_'+id).value);
	var total= parseFloat(qty) * parseFloat(amount);
 	var rate_of_excise=document.getElementById('rate_of_exciseduty_'+id).value;
 	if(Discount!=0.00)
	{
		 
	 var TotalAssessibleValue=parseFloat(qty)* parseFloat(MRP_price)-(parseFloat(qty)* parseFloat(MRP_price) *parseFloat(Discount)*.01);	
  	document.getElementById('total_assessible_'+id).value=TotalAssessibleValue.toFixed(2);
	var Assessible_price=parseFloat(TotalAssessibleValue/qty);	
	document.getElementById('mrp_assessible_price_'+id).value=Assessible_price.toFixed(2) ;
 	 
	 document.getElementById("exciseduty_payable_amount_"+id).value =(document.getElementById('total_assessible_'+id).value *rate_of_excise)* .01;
	document.getElementById('total_amount_'+id).value=total.toFixed(2);
	
	}
	else
	{
		document.getElementById('mrp_assessible_price_'+id).value= amount;
			document.getElementById('total_assessible_'+id).value=amount* qty;
	var cal=(document.getElementById('total_assessible_'+id).value *rate_of_excise)* .01;
 		 document.getElementById("exciseduty_payable_amount_"+id).value =cal.toFixed(2);		
  		document.getElementById('total_amount_'+id).value=total.toFixed(2);
 	}
	 
	

  }
/*
function cal_exciseduty_payable_amount(id)
{
   	var total_assessible = document.getElementById('total_assessible_'+id).value;
 	var rate_of_exciseduty = document.getElementById('rate_of_exciseduty_'+id).value;
 	var total =parseFloat(( parseFloat(total_assessible) * parseFloat(rate_of_exciseduty))/100);
	 if(!isNaN(total)){
     document.getElementById("exciseduty_payable_amount_"+id).value =total.toFixed(2);
	 document.getElementById("perunit_price_"+id).value =total.toFixed(2);
	 }
	 else{
		      document.getElementById("exciseduty_payable_amount_"+id).value =0;
			 document.getElementById("perunit_price_"+id).value =0;

		 }
 }
*/
function cal_value_of_goods()
{
	var len=document.getElementById('rowcount').value;
 	var total=0;
   for(i=0;i<len;i++)
   {
     if(!isNaN(total))
	 {
  			total=parseFloat(total)+parseFloat(document.getElementById("Amount_"+i).value );
	 }
   }
	 if(!isNaN(total))
	document.getElementById("value_of_goods").value=total.toFixed(2);
	else
	document.getElementById("value_of_goods").value=0
 	//var X= toWords(total);
	
		document.getElementById("total_excise_duty").value=document.getElementById("excise_duty").value;
	var X= toWords(document.getElementById("excise_duty").value);
	document.getElementById("excise_duty_inwords").value=X;

}
function cal_Excise()
{
	var val=parseFloat(document.getElementById('hid_Excise').value);
	var value_of_good=parseFloat(document.getElementById('value_of_goods').value);
	var total=(value_of_good*val)*.01;
	document.getElementById("Excise").value=total.toFixed(2);
	document.getElementById("total_excise_duty").value=total.toFixed(2);
}
function cal_C_Cess()
{
	var val=document.getElementById('hid_C_Cess').value;
	var excise_duty=document.getElementById('Excise').value;
	var total=(excise_duty*val)*.01;
	document.getElementById("C_Cess").value=total.toFixed(2);
}

function cal_H_Cess()
{
	var val=document.getElementById('hid_H_Cess').value;
	var excise_duty=document.getElementById('Excise').value;
	var total=(excise_duty*val)*.01;
	document.getElementById("H_Cess").value=total.toFixed(2);
}


function cal_total_taxable(){
var value_of_goods=	document.getElementById("value_of_goods").value;
var excise_duty=	document.getElementById("Excise").value;
var edu_cess=	document.getElementById("C_Cess").value;
var she_cess=	document.getElementById("H_Cess").value;
var num=parseFloat(value_of_goods)+parseFloat(excise_duty)+parseFloat(edu_cess)+parseFloat(she_cess);
document.getElementById("total_taxable").value=num.toFixed(2);
}

function cal_grand_total()
{
 
 var total_amount=parseFloat(document.getElementById("total_taxable").value);
var vat=parseFloat(document.getElementById("vat_cst").value);
   if(!isNaN(total_amount)&& !isNaN(vat)){
	     
	var num= parseFloat(total_amount)+parseFloat(total_amount*vat *.01)
 	document.getElementById("grand_total").value=num.toFixed(2);
   }else{
	    document.getElementById("grand_total").value=0;
	   }
}

function ShowBranch(str)
{
if (str=="")
  {
  document.getElementById("PrefixInvoice").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("PrefixInvoice").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","BranchPrefix.php?q="+str,true);
xmlhttp.send();
}
function ShowSalesTax(amount,str,tab)
{
 if (str=="")
  {
  document.getElementById("SalesTaxRate").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("SalesTaxRate").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","SalesTaxRate.php?q="+str+"&tab="+tab+"&amount="+amount,true);
xmlhttp.send();
}