Go Back   CodingForums.com > :: Client side development > JavaScript programming > Post a JavaScript

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-04-2002, 02:07 PM   PM User | #1
tommysphone
Regular Coder

 
Join Date: Jun 2002
Posts: 262
Thanks: 0
Thanked 0 Times in 0 Posts
tommysphone is an unknown quantity at this point
VAT Calculator

This is a smart little chunk of code, used to calculate vat in £ sterling.

<html>
<head>
<title>vat calculator</title>
</head>
<body>

<p>This will calculate the VAT contained within a total amount, or the VAT charged on a
net amount.</p>
<script language="javascript">

// starts calculation
function calculate() {
var vat, total, vatTotal, finalTotal;

var frm = document.calculator;

total = evalnum( frm.total.value );
if (total == "NaN") total=0;
if (total == 0) {
alert("Please enter an ammount first.");
frm.total.focus()
return
}
else
frm.total.value = evalpound( total );
if (frm.chkvat.checked) {
vatTotal = total * (7/47);
finalTotal = total - vatTotal;
frm.calcvat.value = evalpound( vatTotal );
frm.calctotal.value = evalpound( finalTotal );
}
else {
vatTotal = total * 0.175;
frm.calcvat.value = evalpound( vatTotal );
frm.calctotal.value = evalpound( total + vatTotal );
}
}

</script>
<script language="JavaScript">
//----------------------------------------------------------------------------
// Scripts for VAT calculator
//----------------------------------------------------------------------------

var frm = document.calculator;

// ----------------------------------------------------------------------------
function selectBox(frmE, selectText) {

var i;
var selectIndex = 0;

for(var i=0;i<document.calculator[frmE].length;i++) {
if(document.calculator[frmE].options[i].value == selectText) { selectIndex = i;}
}

return selectIndex;
}

// ----------------------------------------------------------------------------
function evalnum(numstr) {

var i,c,neg;
var ret="";
var p=false;

neg = 1;
if (numstr.indexOf("-") >=0) { neg = -1; }

for (i=numstr.length-1; i>=0; i--) {
c=numstr.charAt(i);

if (c == ".") {
if (p == false) p=true;
else c="";
}

if ((c < "0" || c > "9") && c != ".") c="";

ret = c + ret;
}

if (ret=="" || ret=="NaN") ret="0";
return parseFloat(ret*neg);

}

// ----------------------------------------------------------------------------
function evalpound(num) {

var i,l,d;
var nums;
var ret;

nums = String(Math.round(num*100));

while (nums.length <3) nums = "0" + nums;

l = nums.length-3;
ret = "." + nums.charAt(l+1) + nums.charAt(l+2);

d=0;
for (i=l; i>=0; i--) {
ret = nums.charAt(i) + ret;
d++;
if (d==3 && i>0) {
ret="," + ret;
d=0;
}
}

ret = "£" + ret;
return ret;

}

</script>


<form name="calculator">
<table align="center" width="440" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="440" height="50"><table width="440" height="50" border="0" cellspacing="0"
cellpadding="0">
<tr>
<td width="26" height="20"></td>
<td width="393" height="20" bgcolor="#BDC4D5"><table width="390" height="100" border="0"
cellspacing="0" cellpadding="2">
<tr>
<td bgcolor="#BDC4D5"><table width="390" border="0" cellspacing="0" cellpadding="3">
<tr>
<td colspan="2"></td>
<td></td>
</tr>
<tr>
<td class="calcbody" width="220" valign="top">Input amount:</td>
<td class="calcbody" width="150"><input type="text" class="calcinput" name="total" title
min="0" max format value size="9" style="width: 100px; text-align: right;"
onchange="calculate(this)"></td>
<td class="calcbody" width="20"></td>
</tr>
<tr>
<td class="calcbody" width="220" valign="top">Inclusive of VAT?</td>
<td class="calcbody" width="150"><input type="checkbox" class="calcinput" name="chkvat"
title onchange="calculate(this)" value="ON"><script language="javascript">document.calculator.chkvat.checked = false</script>Yes</td>
<td class="calcbody" width="20"></td>
</tr>
<tr>
<td class="calcbody"></td>
<td class="calcbody"><table width="100" height="22" border="0" cellpadding="0"
cellspacing="0">
<tr>
<td width="100" height="22" class="calcbody" align="center"><a class="calcbutton"
href="javascript:calculate(this)">Calculate</a></td>
</tr>
</table>
</td>
</tr>
<tr>
<td colspan="2" class="calcsubtitle"><b>Results</b></td>
<td></td>
</tr>
<tr>
<td class="calcbody" width="220" valign="top">VAT:</td>
<td class="calcbody" width="150"><input type="text" class="calcinput" name="calcvat" title
min max format value size="9" style="width: 100px; text-align: right;" readonly="true"></td>
<td class="calcbody" width="20"></td>
</tr>
<tr>
<td class="calcbody" width="220" valign="top">Amount:</td>
<td class="calcbody" width="150"><input type="text" class="calcinput" name="calctotal"
title min max format value size="9" style="width: 100px; text-align: right;"
readonly="true"></td>
<td class="calcbody" width="20"></td>
</tr>
</table>
</td>
</tr>
</table>
</td>
<td width="21" height="20"></td>
</tr>
</table>
</td>
</tr>
</table>
</form>

<p>&nbsp;</p>
</body>
</html>
__________________
Therapy is expensive, popping bubble wrap is cheap, you choose.
tommysphone is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:23 AM.


Advertisement
Log in to turn off these ads.