
// *********************** BEGIN BASE FUNCTIONS ***************************

// prepare/cleanup field contents
function clean(obj)
{
	if (obj.value != "")
	{
		// remove p or commas
			re = /P|,/g;
			var source = obj.value.replace(re,"");
		
		// round source to 2 significant decimal places
			var source = Math.round(source*100)/100;
			var source = source.toFixed(2);
			
		// write updated field value back to form	
			obj.value = source;
			
		// remove NAN if found
			chk4num(obj);
	}
	return(obj);
}

function calcVAT(obj1,obj2)
// calculates the amount of VAT to be added to the supplied NET figure
{
	// perform calculation (calculate VAT on Net source amount)
		var VAT = obj1.value * 12 / 100;
		
		// update field
		obj2.value = VAT ;
		
		clean(obj2);
		
	return(VAT);
}

function chk4num(obj1)
{
	if (obj1.value == "NaN")
	{
		obj1.value = "";
	}
	return(obj1);
}

function retotal(form)
{	
	// begin update 17f
	
		// clean fields
		clean(form.f12a);
		clean(form.f13a);
		clean(form.f14);
		clean(form.f15);
		
	var f12a = parseFloat(form.f12a.value,10);
		if (form.f12a.value==""){var f12a=0;}else{form.f12a.value = f12a.toFixed(2);}
	var f13a = parseFloat(form.f13a.value,10);
		if (form.f13a.value==""){var f13a=0;}else{form.f13a.value = f13a.toFixed(2);}
	var f14 = parseFloat(form.f14.value,10);
		if (form.f14.value==""){var f14=0;}else{form.f14.value = f14.toFixed(2);}
	var f15 = parseFloat(form.f15.value,10);
		if (form.f15.value==""){var f15=0;}else{form.f15.value = f15.toFixed(2);}
		
	var val = f12a + f13a + f14 + f15 ;
	form.f16a.value = val.toFixed(2);
	
	// end update 17f
	
	
	
	// begin update 16b
	
	var f12b = parseFloat(form.f12b.value,10);
		if (form.f12b.value==""){var f12b=0;}
	var f13b = parseFloat(form.f13b.value,10);
		if (form.f13b.value==""){var f13b=0;}
		
	var val = f12b + f13b ;
	form.f16b.value = val.toFixed(2);
	
	// end update 16b


	
	// begin update 17f
	
		// clean fields
		clean(form.f17a);
		clean(form.f17b);
		clean(form.f17c);
		clean(form.f17d);
		clean(form.f17e);
		
	var f17a = parseFloat(form.f17a.value,10);
		if (form.f17a.value==""){var f17a=0;}else{form.f17a.value = f17a.toFixed(2);}
	var f17b = parseFloat(form.f17b.value,10);
		if (form.f17b.value==""){var f17b=0;}else{form.f17b.value = f17b.toFixed(2);}
	var f17c = parseFloat(form.f17c.value,10);
		if (form.f17c.value==""){var f17c=0;}else{form.f17c.value = f17c.toFixed(2);}
	var f17d = parseFloat(form.f17d.value,10);
		if (form.f17d.value==""){var f17d=0;}else{form.f17d.value = f17d.toFixed(2);}
	var f17e = parseFloat(form.f17e.value,10);
		if (form.f17e.value==""){var f17e=0;}else{form.f17e.value = f17e.toFixed(2);}
		
	var val = f17a + f17b + f17c + f17d + f17e ;
	form.f17f.value = val.toFixed(2);
	
	// end update 17f
	
	
	
	// begin update 18a (original NET amount from 18b)
	
		// clean fields
			clean(form.f18b);
			
	var f18b = parseFloat(form.f18b.value,10);
		if (form.f18b.value==""){var f18b=0;}else{form.f18b.value = f18b.toFixed(2);}
		
	var val = (1/0.12) * f18b ;
	if (val==0.00){form.f18a.value="";}else{
	form.f18a.value = val.toFixed(2);}
	
	// end update 18a (original NET amount from 18b)
	
	
	
	// begin update 18c (original NET amount from 18d)
	
		// clean fields
			clean(form.f18d);
			
	var f18d = parseFloat(form.f18d.value,10);
		if (form.f18d.value==""){var f18d=0;}else{form.f18d.value = f18d.toFixed(2);}
		
	var val = (1/0.12) * f18d ;
	if (val==0.00){form.f18c.value="";}else{
	form.f18c.value = val.toFixed(2);}
	
	// end update 18c (original NET amount from 18d)
	
	
	
	// begin update 18e (original NET amount from 18f)
	
		// clean fields
			clean(form.f18f);
			
	var f18f = parseFloat(form.f18f.value,10);
		if (form.f18f.value==""){var f18f=0;}else{form.f18f.value = f18f.toFixed(2);}
		
	var val = (1/0.12) * f18f ;
	if (val==0.00){form.f18e.value="";}else{
	form.f18e.value = val.toFixed(2);}
	
	// end update 18e (original NET amount from 18f)
	
	
		
	// begin update 18i (original NET amount from 18j)
	
		// clean fields
			clean(form.f18j);
			
	var f18j = parseFloat(form.f18j.value,10);
		if (form.f18j.value==""){var f18j=0;}else{form.f18j.value = f18j.toFixed(2);}
		
	var val = (1/0.12) * f18j ;
	if (val==0.00){form.f18i.value="";}else{
	form.f18i.value = val.toFixed(2);}
	
	// end update 18i (original NET amount from 18j)
	
	
	
	// begin update 18p	
		// clean fields
			clean(form.f18a);
			clean(form.f18c);
			clean(form.f18e);
			clean(form.f18g);
			clean(form.f18i);
			clean(form.f18k);
			clean(form.f18m);
			clean(form.f18n);
			
	var f18a = parseFloat(form.f18a.value,10);
		if (form.f18a.value==""){var f18a=0;}else{form.f18a.value = f18a.toFixed(2);}
	var f18c = parseFloat(form.f18c.value,10);
		if (form.f18c.value==""){var f18c=0;}else{form.f18c.value = f18c.toFixed(2);}
	var f18e = parseFloat(form.f18e.value,10);
		if (form.f18e.value==""){var f18e=0;}else{form.f18e.value = f18e.toFixed(2);}
	var f18g = parseFloat(form.f18g.value,10);
		if (form.f18g.value==""){var f18g=0;}else{form.f18g.value = f18g.toFixed(2);}
	var f18i = parseFloat(form.f18i.value,10);
		if (form.f18i.value==""){var f18i=0;}else{form.f18i.value = f18i.toFixed(2);}
	var f18k = parseFloat(form.f18k.value,10);
		if (form.f18k.value==""){var f18k=0;}else{form.f18k.value = f18k.toFixed(2);}
	var f18m = parseFloat(form.f18m.value,10);
		if (form.f18m.value==""){var f18m=0;}else{form.f18m.value = f18m.toFixed(2);}
	var f18n = parseFloat(form.f18n.value,10);
		if (form.f18n.value==""){var f18n=0;}else{form.f18n.value = f18n.toFixed(2);}
	
	var val = f18a + f18c + f18e + f18g + f18i + f18k + f18m + f18n ;
	form.f18p.value = val.toFixed(2);

	// end update 18p
	
	
	
	// begin update 19 (sum of 17f, 18b,d,f,h,j,l and o)
		// clean fields
			clean(form.f18b);
			clean(form.f18d);
			clean(form.f18f);
			clean(form.f18h);
			clean(form.f18j);
			clean(form.f18l);
			clean(form.f18o);
	
	var f17f = parseFloat(form.f17f.value,10);
		if (form.f17f.value==""){var f17f=0;}else{form.f17f.value = f17f.toFixed(2);}
	var f18b = parseFloat(form.f18b.value,10);
		if (form.f18b.value==""){var f18b=0;}else{form.f18b.value = f18b.toFixed(2);}
	var f18d = parseFloat(form.f18d.value,10);
		if (form.f18d.value==""){var f18d=0;}else{form.f18d.value = f18d.toFixed(2);}
	var f18f = parseFloat(form.f18f.value,10);
		if (form.f18f.value==""){var f18f=0;}else{form.f18f.value = f18f.toFixed(2);}
	var f18h = parseFloat(form.f18h.value,10);
		if (form.f18h.value==""){var f18h=0;}else{form.f18h.value = f18h.toFixed(2);}
	var f18j = parseFloat(form.f18j.value,10);
		if (form.f18j.value==""){var f18j=0;}else{form.f18j.value = f18j.toFixed(2);}
	var f18l = parseFloat(form.f18l.value,10);
		if (form.f18l.value==""){var f18l=0;}else{form.f18l.value = f18l.toFixed(2);}
	var f18o = parseFloat(form.f18o.value,10);
		if (form.f18o.value==""){var f18o=0;}else{form.f18o.value = f18o.toFixed(2);}
		
	var val = f17f + f18b + f18d + f18f + f18h + f18j + f18l + f18o ;
	form.f19.value = val.toFixed(2);
	
	// end update 19 (sum of 17f, 18b,d,f,h,j,l and o)
	
	
	/* commented until I decide what to do about this. The section was removed by BIR from the form, but I think it is important to perform this calculation and inform the user of the result, so that the field 17a can be completed correctly on the next months return.

	// begin update 20E (calculates whether any input tax to be carried over to next month)
	
		// clean fields
			clean(form.f16b);
			clean(form.f19);
		
	var f16b = parseFloat(form.f16b.value,10);
		if (form.f16b.value==""){var f16b=0;}else{form.f16b.value = f16b.toFixed(2);}
	var f19 = parseFloat(form.f19.value,10);
		if (form.f19.value==""){var f19=0;}else{form.f19.value = f19.toFixed(2);}
		
	var val = f16b - f19 ;
	if (val>0.00){form.f20e.value="";}else{
	val = val-val-val;
	form.f20e.value = val.toFixed(2);}
	
	if (val==0.00){form.f20e.value="";}
	
	
	// end update 20E (calculates whether any input tax to be carried over to next month)
	*/
	
	
	// begin update 20f (sum of 20a,b,c,d and e)
		// clean fields
			clean(form.f20a);
			clean(form.f20b);
			clean(form.f20c);
			clean(form.f20d);
			clean(form.f20e);
	
		var f20a = parseFloat(form.f20a.value,10);
			if (form.f20a.value==""){var f20a=0;}else{form.f20a.value = f20a.toFixed(2);}
		var f20b = parseFloat(form.f20b.value,10);
			if (form.f20b.value==""){var f20b=0;}else{form.f20b.value = f20b.toFixed(2);}
		var f20c = parseFloat(form.f20c.value,10);
			if (form.f20c.value==""){var f20c=0;}else{form.f20c.value = f20c.toFixed(2);}
		var f20d = parseFloat(form.f20d.value,10);
			if (form.f20d.value==""){var f20d=0;}else{form.f20d.value = f20d.toFixed(2);}
		var f20e = parseFloat(form.f20e.value,10);
			if (form.f20e.value==""){var f20e=0;}else{form.f20e.value = f20e.toFixed(2);}
			
	var val = f20a + f20b + f20c + f20d + f20e ;
	if (val==0.00){form.f20f.value="";}else{
	form.f20f.value = val.toFixed(2);}
	
	// end update 20f (sum of 20a,b,c,d and e)
	
	
		
	// begin update 21 (19 - 20f)
			// clean fields
			clean(form.f19);
			clean(form.f20f);
	
		var f19 = parseFloat(form.f19.value,10);
			if (form.f19.value==""){var f19=0;}else{form.f19.value = f19.toFixed(2);}
		var f20f = parseFloat(form.f20f.value,10);
			if (form.f20f.value==""){var f20f=0;}else{form.f20f.value = f20f.toFixed(2);}
		
	var val = f19 - f20f ;
	if (val==0.00){form.f21.value="";}else{
	form.f21.value = val.toFixed(2);}
	
	// end update 21 (19 - 20f)
	
	
	
	// begin update 22 (16b - 21)
			// clean fields
			clean(form.f16b);
			clean(form.f21);
	
		var f16b = parseFloat(form.f16b.value,10);
			if (form.f16b.value==""){var f16b=0;}else{form.f16b.value = f16b.toFixed(2);}
		var f21 = parseFloat(form.f21.value,10);
			if (form.f21.value==""){var f21=0;}else{form.f21.value = f21.toFixed(2);}
		
	var val = f16b - f21 ;
	form.f22.value = val.toFixed(2);
	
	// end update 22 (16b - 21)
		
	
	
	// begin update 23g (sum of 23a,b,c,d,e and f)
		// clean fields
			clean(form.f23a);
			clean(form.f23b);
			clean(form.f23c);
			clean(form.f23d);
			clean(form.f23e);
			clean(form.f23f);
	
		var f23a = parseFloat(form.f23a.value,10);
			if (form.f23a.value==""){var f23a=0;}else{form.f23a.value = f23a.toFixed(2);}
		var f23b = parseFloat(form.f23b.value,10);
			if (form.f23b.value==""){var f23b=0;}else{form.f23b.value = f23b.toFixed(2);}
		var f23c = parseFloat(form.f23c.value,10);
			if (form.f23c.value==""){var f23c=0;}else{form.f23c.value = f23c.toFixed(2);}
		var f23d = parseFloat(form.f23d.value,10);
			if (form.f23d.value==""){var f23d=0;}else{form.f23d.value = f23d.toFixed(2);}
		var f23e = parseFloat(form.f23e.value,10);
			if (form.f23e.value==""){var f23e=0;}else{form.f23e.value = f23e.toFixed(2);}
		var f23f = parseFloat(form.f23f.value,10);
			if (form.f23f.value==""){var f23f=0;}else{form.f23f.value = f23f.toFixed(2);}
			
	var val = f23a + f23b + f23c + f23d + f23e + f23f ;
	if (val==0.00){form.f23g.value="";}else{
	form.f23g.value = val.toFixed(2);}
	
	// end update 23g (sum of 23a,b,c,d,e and f)
	
	
	
	// begin update 24 (f22 - f23g)
			// clean fields
			clean(form.f22);
			clean(form.f23g);
	
		var f22 = parseFloat(form.f22.value,10);
			if (form.f22.value==""){var f22=0;}else{form.f22.value = f22.toFixed(2);}
		var f23g = parseFloat(form.f23g.value,10);
			if (form.f23g.value==""){var f23g=0;}else{form.f23g.value = f23g.toFixed(2);}
		
	var val = f22 - f23g ;
	form.f24.value = val.toFixed(2);
	

	
	// end update 24 (f22 - f23g)
		
	
	
	// begin update 25d (sum of 25a,b and c)
		// clean fields
			clean(form.f25a);
			clean(form.f25b);
			clean(form.f25c);
	
		var f25a = parseFloat(form.f25a.value,10);
			if (form.f25a.value==""){var f25a=0;}else{form.f25a.value = f25a.toFixed(2);}
		var f25b = parseFloat(form.f25b.value,10);
			if (form.f25b.value==""){var f25b=0;}else{form.f25b.value = f25b.toFixed(2);}
		var f25c = parseFloat(form.f25c.value,10);
			if (form.f25c.value==""){var f25c=0;}else{form.f25c.value = f25c.toFixed(2);}
			
	var val = f25a + f25b + f25c ;
	form.f25d.value = val.toFixed(2);
	
	// end update 25d (sum of 25a,b and c)
	
	
	
	// begin update 26 (sum of f24 and f25d)
		// clean fields
			clean(form.f24);
			clean(form.f25d);
	
		var f24 = parseFloat(form.f24.value,10);
			if (form.f24.value==""){var f24=0;}else{form.f24.value = f24.toFixed(2);}
		var f25d = parseFloat(form.f25d.value,10);
			if (form.f25d.value==""){var f25d=0;}else{form.f25d.value = f25d.toFixed(2);}
			
	var val = f24 + f25d ;
	form.f26.value = val.toFixed(2);

	// end update 26 (sum of f24 and f25d)
	
	
	
	return(form);
}


// *********************** END BASE FUNCTIONS ***************************


// ********************** BEGIN MAIN FUNCTIONS **************************

// begin calculate vat of objName1. Put vat in objName2
function getVAT(obj1,obj2,form)
{
		// clean field
			clean(obj1);

		// calculate VAT
			calcVAT(obj1,obj2);
			
		// remove 0.00 if found
			if (obj2.value == 0.00)
			{
				obj2.value = "";
			}
		// retotal sums in main column
			retotal(form);
			
		return(form);
}


// ********************** END MAIN FUNCTIONS **************************

//  BEGIN Funtion to make focus jump in TIN box


<!-- This code makes the jump from textbox one to textbox two -->
function move(form)
{
var letters = form.f4a.value.length +1;
if (letters <= 3)
{form.f4a.focus()}
else
{form.f4b.focus()}
}

function move2(form)
{
var letters = form.f4b.value.length +1;
if (letters <= 3)
{form.f4b.focus()}
else
{form.f4c.focus()}
}

function move3(form)
{
var letters = form.f4c.value.length +1;
if (letters <= 3)
{form.f4c.focus()}
else
{form.f4d.focus()}
}

//  END Funtion to make focus jump in TIN box


// begin location to submit form to, depending on which button was pressed

function task(target){

// the next 3 lines are the main lines of this script
//remember to leave action field blank when defining the form 
	if(target == "create"){
		document.form.action="/bir-form-download/BIR-2550M-Rev07-form.php";
		document.form.target="_self";
	}
	if(target == "save") {
		document.form.action="";
		document.form.target="_self";
	}
 
}

// end location to submit form to, depending on which button was pressed
