// begin on selection of either ATC or the description do this..

function sync_fields(live,row,Nature_of_Payment,ATC,TaxBase,TaxField,TaxReq,form)
{


	if (live.name == "Nature_of_Payment"+row)
	{
		ATC.value = live.value ;
	}else{
		Nature_of_Payment.value = ATC.value ;
	}


// begin set the tax rate

var x = ATC.value ;

	if (x == "WC 010" || x == "WI 010" || x == "WI 020" || x == "WI 030" || x == "WI 040" || x == "WI 050" || x == "WI 060" || x == "WI 070" || x == "WI 080" || x == "WI 140" || x == "WC 140" || x == "WI 141" || x == "WI 151" || x == "WI 152" || x == "WI 515" || x == "WC 515" || x == "WI 090" )
	{ 
		TaxField.value = "10%" ;
	}
	else
	{	
		if ( x == "xxxxx")
		{
			TaxField.value = "20%" ;
		}
		else
		{
			if ( x == "WI 091" || x == "WI 130" || x == "WI 159" || x == "WC 011" || x == "WI 011" || x == "WI 021" || x == "WI 031" || x == "WI 041" || x == "WI 051" || x == "WI 061" || x == "WI 071" || x == "WI 151" || x == "WI 153" || x == "WI 081" )
			{
				TaxField.value = "15%" ;
			}
			else
			{
				if (x == "WI 100" || x == "WC 100" || x == "WI 110" || x == "WC 110" || x == "WI 540" || x == "WC 540")
				{
					TaxField.value = "5%" ;
				}
				else
				{
					if (x == "WI 120" || x == "WC 120" || x == "WI 157" || x == "WC 157" || x == "WI 160" || x == "WC 160")
					{
						TaxField.value = "2%" ;
					}
					else
					{
						if (x == "WI 158" || x == "WC 158" || x == "WI 530" || x == "WI 535" || x == "WC 535" || x == "WI 610" || x == "WC 610" || x == "WI 630" || x == "WC 630" || x == "WI 640" || x == "WC 640")
						{
							TaxField.value = "1%" ;
						}
						else
						{
							if (x == "WI 156" || x == "WC 156")
							{
								TaxField.value = "1% of &frac12;" ;
							}
							else
							{
								if (x == "")
								{
									TaxField.value = "" ;
								}
							}
						}
					}
				}
			}
		}
	}
	// alert(TaxField.value);
	
	clean(TaxBase) ;
	
		//clean TaxField
		if (TaxField.value != ""){
			// remove p or commas
				re = /P|%|,/g;
				var source = TaxField.value.replace(re,"");
			
			
			// round source to 2 significant decimal places
				var source = Math.round(source*100)/100;
				var source = source.toFixed(2);
	
				
			var x = parseFloat(source,10);
			if (source==""){var x=0;}else{ x = x.toFixed(2);}
		
			if (TaxField.value == "NaN"){
				TaxField.value = "";
			}
				
		}
		
		
		
		
	var source = x.replace("%","");
	TaxReq.value = TaxBase.value * source / 100 ;
	clean(TaxReq) ;
	if (TaxField.value == "") {TaxReq.value = "" ; }
	
	calc(form) ;
	
	
	return(TaxField);
}

// end on selection of either ATC or the description do this..




// begin calculate amount witheld per row & total

function calc(form)
{	

	
		// begin clean all right column fields in preparation for totaling
		
		var TaxReq1 = parseFloat(form.TaxReq1.value,10);
		if (form.TaxReq1.value==""){var TaxReq1=0;}else{form.TaxReq1.value = TaxReq1.toFixed(2);}

		var TaxReq2 = parseFloat(form.TaxReq2.value,10);
		if (form.TaxReq2.value==""){var TaxReq2=0;}else{form.TaxReq2.value = TaxReq2.toFixed(2);}
		
		var TaxReq3 = parseFloat(form.TaxReq3.value,10);
		if (form.TaxReq3.value==""){var TaxReq3=0;}else{form.TaxReq3.value = TaxReq3.toFixed(2);}
		
		var TaxReq4 = parseFloat(form.TaxReq4.value,10);
		if (form.TaxReq4.value==""){var TaxReq4=0;}else{form.TaxReq4.value = TaxReq4.toFixed(2);}
		
		var TaxReq5 = parseFloat(form.TaxReq5.value,10);
		if (form.TaxReq5.value==""){var TaxReq5=0;}else{form.TaxReq5.value = TaxReq5.toFixed(2);}
		
		var TaxReq6 = parseFloat(form.TaxReq6.value,10);
		if (form.TaxReq6.value==""){var TaxReq6=0;}else{form.TaxReq6.value = TaxReq6.toFixed(2);}
		
		var TaxReq7 = parseFloat(form.TaxReq7.value,10);
		if (form.TaxReq7.value==""){var TaxReq7=0;}else{form.TaxReq7.value = TaxReq7.toFixed(2);}
		
		var TaxReq8 = parseFloat(form.TaxReq8.value,10);
		if (form.TaxReq8.value==""){var TaxReq8=0;}else{form.TaxReq8.value = TaxReq8.toFixed(2);}
		
		var TaxReq9 = parseFloat(form.TaxReq9.value,10);
		if (form.TaxReq9.value==""){var TaxReq9=0;}else{form.TaxReq9.value = TaxReq9.toFixed(2);}
		
		var TaxReq10 = parseFloat(form.TaxReq10.value,10);
		if (form.TaxReq10.value==""){var TaxReq10=0;}else{form.TaxReq10.value = TaxReq10.toFixed(2);}
		
		var TaxReq11 = parseFloat(form.TaxReq11.value,10);
		if (form.TaxReq11.value==""){var TaxReq11=0;}else{form.TaxReq11.value = TaxReq11.toFixed(2);}
		
		var TaxReq12 = parseFloat(form.TaxReq12.value,10);
		if (form.TaxReq12.value==""){var TaxReq12=0;}else{form.TaxReq12.value = TaxReq12.toFixed(2);}
		
		var TaxReq13 = parseFloat(form.TaxReq13.value,10);
		if (form.TaxReq13.value==""){var TaxReq13=0;}else{form.TaxReq13.value = TaxReq13.toFixed(2);}
		
		var TaxReq14 = parseFloat(form.TaxReq14.value,10);
		if (form.TaxReq14.value==""){var TaxReq14=0;}else{form.TaxReq14.value = TaxReq14.toFixed(2);}
		
		var TaxReq15 = parseFloat(form.TaxReq15.value,10);
		if (form.TaxReq15.value==""){var TaxReq15=0;}else{form.TaxReq15.value = TaxReq15.toFixed(2);}
		
		var TaxReq16 = parseFloat(form.TaxReq16.value,10);
		if (form.TaxReq16.value==""){var TaxReq16=0;}else{form.TaxReq16.value = TaxReq16.toFixed(2);}
		
		var TaxReq17 = parseFloat(form.TaxReq17.value,10);
		if (form.TaxReq17.value==""){var TaxReq17=0;}else{form.TaxReq17.value = TaxReq17.toFixed(2);}
		
		var TaxReq18 = parseFloat(form.TaxReq18.value,10);
		if (form.TaxReq18.value==""){var TaxReq18=0;}else{form.TaxReq18.value = TaxReq18.toFixed(2);}
		
		var TaxReq19 = parseFloat(form.TaxReq19.value,10);
		if (form.TaxReq19.value==""){var TaxReq19=0;}else{form.TaxReq19.value = TaxReq19.toFixed(2);}
		
		var TaxReq20 = parseFloat(form.TaxReq20.value,10);
		if (form.TaxReq20.value==""){var TaxReq20=0;}else{form.TaxReq20.value = TaxReq20.toFixed(2);}

		var TaxReq21 = parseFloat(form.TaxReq21.value,10);
		if (form.TaxReq21.value==""){var TaxReq21=0;}else{form.TaxReq21.value = TaxReq21.toFixed(2);}
		
		var TaxReq22 = parseFloat(form.TaxReq22.value,10);
		if (form.TaxReq22.value==""){var TaxReq22=0;}else{form.TaxReq22.value = TaxReq22.toFixed(2);}
				
		// end clean all right column fields in preparation for totaling
		
		// begin add all right column for total
		var source2 = TaxReq1 + TaxReq2 + TaxReq3 + TaxReq4 + TaxReq5 + TaxReq6 + TaxReq7 + TaxReq8 + TaxReq9 + TaxReq10 + TaxReq11 + TaxReq12 + TaxReq13 + TaxReq14 + TaxReq15 + TaxReq16 + TaxReq17 + TaxReq18 + TaxReq19 + TaxReq20 + TaxReq21 + TaxReq22 ;
		
		var source2 = source2.toFixed(2) ;
		form.f14.value = source2 ;
		
		// end add all right column for total
		
	// end calculate amount witheld per row and total
	

// begin subtract payments already made

	var field = form.f15.value ;
	var field = parseFloat(field,10);
		if (form.f15.value==""){var field=0;}else{var field = field.toFixed(2);}
	
	
	form.f16.value = source2 - field ;
	
	// cosmetic clean for f15
	var f15 = parseFloat(form.f15.value,10);
			if (form.f15.value==""){var f15=0;}else{form.f15.value = f15.toFixed(2);}
	
	// begin update 17d (sum of 17a,b and c)
		// clean fields
			clean(form.f17a);
			clean(form.f17b);
			clean(form.f17c);
	
		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 val = f17a + f17b + f17c ;
	form.f17d.value = val.toFixed(2);
	
	// end update 17d (sum of 17a,b and c)
	
	
	// begin update 18 (sum of f16 and f17d)
		// clean fields
			clean(form.f16);
			clean(form.f17d);
	
		var f16 = parseFloat(form.f16.value,10);
			if (form.f16.value==""){var f16=0;}else{form.f16.value = f16.toFixed(2);}
		var f17d = parseFloat(form.f17d.value,10);
			if (form.f17d.value==""){var f17d=0;}else{form.f17d.value = f17d.toFixed(2);}
			
	var val = f16 + f17d ;
	form.f18.value = val.toFixed(2);

	// end update update 18 (sum of f16 and f17d)
	
	
return(form);
}
// end subtract payments already made



// begin cleanup field contents of PASSED VALUE

function clean_value(val)
{
	if (val != ""){		
		
		// round source to 2 significant decimal places
			var val = Math.round(val*100)/100;
			var val = val.toFixed(2);

			
		var x = parseFloat(val,10);
		if (val==""){var x=0;}else{val = x.toFixed(2);}
	
		if (val == "NaN"){
			val = "";
		}
			
	}
	return(val);
}

// end cleanup contents of PASSED VALUE



// begin 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);

			
		var x = parseFloat(source,10);
		if (source==""){var x=0;}else{obj.value = x.toFixed(2);}
	
		if (obj.value == "NaN"){
			obj.value = "";
		}
			
	}
	return(obj);
}

// end prepare/cleanup field contents














//  BEGIN Funtion to make focus jump to next TIN box

function move(form)
{
var letters = form.f5a.value.length +1;
if (letters <= 3)
{form.f5a.focus()}
else
{form.f5b.focus()}
}

function move2(form)
{
var letters = form.f5b.value.length +1;
if (letters <= 3)
{form.f5b.focus()}
else
{form.f5c.focus()}
}

function move3(form)
{
var letters = form.f5c.value.length +1;
if (letters <= 3)
{form.f5c.focus()}
else
{form.f5d.focus()}
}

//  END Funtion to make focus jump to 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-1601-E-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