function numbersonly(myfield, e, dec)
{
	var key;
	var keychar;
	
	if (window.event)
	   key = window.event.keyCode;
	else if (e)
	   key = e.which;
	else
	   return true;
	keychar = String.fromCharCode(key);
	
	// control keys
	if ((key==null) || (key==0) || (key==8) || 
		(key==9) || (key==13) || (key==27) )
	   return true;
	
	// numbers
	else if ((("0123456789.").indexOf(keychar) > -1))
	   return true;
	
	// decimal point jump
	else if (dec && (keychar == "."))
	   {
	   myfield.form.elements[dec].focus();
	   return false;
	   }
	else
	   return false;
}



function sendQuery(obj){
	if($('#name').val != '' && $('#email').val() !='' && $('#phone').val() !='' && $('#address').val() !=''){
		//send
		//alert('Sending...');
		$.ajax({
		  url: "mail.php",
		  type: "POST",
		  data: ({name : $('#name').val(),
					email : $('#email').val(),
					phone : $('#phone').val(),
					address : $('#address').val(),
					grosspay : $('#grosspay').val(),
					taxpaid : $('#taxpaid').val(),
					year : $('#taxyear').val(),
					overpaid : $('#overpaid').val() }),
		  //async:false,
		  success: function(msg){
					 $( "#succes" ).dialog({
					height: 170,
					width: 450,
					modal: true
				});
			// alert('Išsiųsta! ... ');
		  }
		});
		$("#paidOver").dialog( "close" );
		return false;
	} else {
		$( "#fillin" ).dialog({
				height: 140,
				width: 300,
				modal: true
			});	
		return false;
	}
}
   
   
$(document).ready(function(){

	$('#submit').click(function(){
		if(($('#grosspay').val() !='') && ($('#taxpaid').val() != '')){
			calculatetax();
		} else {
			$( "#fillin" ).dialog({
				height: 140,
				width: 300,
				modal: true
			});
		}
		
	});
});
function calculatetax() {
	
	
	/* data is valid set up arrays */
	allowance = new Array(4535,4615,4615,4745,4895,5035,5225,6035,6475, 6475)
	firsttaxableamount = new Array(1880,1920,1960,2020,2090,2150,2230,0,0,0)
	firsttaxrate = new Array(10,10,10,10,10,10,10,0,0,0)
	secondtaxableamount = new Array(27520,27980,28540,29380,30310,31150,32370,34800,37400,37400)
	secondtaxrate = new Array(22,22,22,22,22,22,22,20,20,20)
	remaindertaxrate = new Array(40,40,40,40,40,40,40,40,40,40)
	
	/* display allowances and rates */
	document.frmtaxcalc.allowance.value = allowance[document.frmtaxcalc.taxyear.value]; 
	document.frmtaxcalc.firsttaxableamount.value = firsttaxableamount[document.frmtaxcalc.taxyear.value];
	document.frmtaxcalc.firsttaxrate.value = firsttaxrate[document.frmtaxcalc.taxyear.value];
	document.frmtaxcalc.secondtaxableamount.value = secondtaxableamount[document.frmtaxcalc.taxyear.value];
	document.frmtaxcalc.secondtaxrate.value = secondtaxrate[document.frmtaxcalc.taxyear.value];
	document.frmtaxcalc.remaindertaxrate.value = remaindertaxrate[document.frmtaxcalc.taxyear.value];
	
	/* set inital overpaid and underpaid */
	document.frmtaxcalc.underpaid.value = "0.00"
	document.frmtaxcalc.overpaid.value = "0.00"
	
	amounttotax = document.frmtaxcalc.grosspay.value - document.frmtaxcalc.allowance.value
	if (amounttotax <= 0) {amounttotax = 0}
	  
	amounttotaxatfirstrate = amounttotax
	if (amounttotaxatfirstrate <= 0) {amounttotaxatfirstrate = 0}
	if (amounttotaxatfirstrate >= document.frmtaxcalc.firsttaxableamount.value) {amounttotaxatfirstrate = document.frmtaxcalc.firsttaxableamount.value}
	taxatfirstrate = 0
	if (amounttotaxatfirstrate > 0) (taxatfirstrate = amounttotaxatfirstrate * (document.frmtaxcalc.firsttaxrate.value/100))
	
	amounttotaxatsecondrate = amounttotax - amounttotaxatfirstrate
	if (amounttotaxatsecondrate <= 0) {amounttotaxatsecondrate = 0}
	if (amounttotaxatsecondrate >= document.frmtaxcalc.secondtaxableamount.value) {amounttotaxatsecondrate = document.frmtaxcalc.secondtaxableamount.value}
	taxatsecondrate = 0
	if (amounttotaxatsecondrate > 0) (taxatsecondrate = amounttotaxatsecondrate * (document.frmtaxcalc.secondtaxrate.value/100))
	
	amounttotaxatremainderrate = amounttotax - amounttotaxatfirstrate - amounttotaxatsecondrate
	if (amounttotaxatremainderrate <= 0) {amounttotaxatremainderrate = 0}
	taxatremainderrate = 0
	if (amounttotaxatremainderrate > 0) (taxatremainderrate = amounttotaxatremainderrate * (document.frmtaxcalc.remaindertaxrate.value/100))
	
	/*
	alert("Amount to tax = " + amounttotax)
	alert("Amount to tax at first rate = " + amounttotaxatfirstrate)
	alert("Amount to tax at second rate = " + amounttotaxatsecondrate)
	alert("Amount to tax at remainder rate = " + amounttotaxatremainderrate)
	alert("Tax at First rate = " + taxatfirstrate)
	alert("Tax at Second rate = " + taxatsecondrate)
	alert("Tax at Remainder rate = " + taxatremainderrate)
	*/
	
	totaltax = taxatfirstrate + taxatsecondrate + taxatremainderrate
	
	if (totaltax - document.frmtaxcalc.taxpaid.value == 0) {
		
		$( "#paidCorrect" ).dialog({
			height: 130,
			width: 400,
			modal: true
		});
		//alert("Jūs esate sumokėję " + document.frmtaxcalc.taxpaid.value + " teisingą mokesčių sumą");
	}
	
	if (totaltax - document.frmtaxcalc.taxpaid.value > 0) {
	  underpaidto2decimal = new Number(totaltax - document.frmtaxcalc.taxpaid.value)
	  document.frmtaxcalc.underpaid.value = underpaidto2decimal.toFixed(2)
	  if (document.frmtaxcalc.underpaid.value > 0) {	
		  $('.amount').html(document.frmtaxcalc.underpaid.value);
		  $( "#paidLess" ).dialog({
			height: 150,
			width: 350,
			modal: true
		});
		$('#overpaid').val(document.frmtaxcalc.underpaid.value);
		//alert("Jūs esate nedamokėję " + document.frmtaxcalc.underpaid.value + "");
		}
	  }
	if (document.frmtaxcalc.taxpaid.value - totaltax > 0) {
	  overpaidto2decimal = new Number(document.frmtaxcalc.taxpaid.value - totaltax)
	  document.frmtaxcalc.overpaid.value = overpaidto2decimal.toFixed(2)
	  if (document.frmtaxcalc.overpaid.value >= 500) {
		   $('.amount').html(document.frmtaxcalc.overpaid.value);
			$('.amount2').html(Math.round(document.frmtaxcalc.overpaid.value/10));
		var overpaid =  $( "#paidOver" ).dialog({
			height: 430,
			width: 500,
			modal: true
		});
		//alert("Jūs esate permokėję £" + document.frmtaxcalc.overpaid.value + "! Nieko nelaukite, skambinkite telefonu 07982400077 arba rašykite mums pasinaudodami www.mokesciai.co.uk esančia forma.");
	  }
	  else if ((document.frmtaxcalc.overpaid.value > 50) && (document.frmtaxcalc.overpaid.value <= 500) ) {
			$('.amount').html(document.frmtaxcalc.overpaid.value);
		   $('.amount2').html(50);
		var overpaid =  $( "#paidOver" ).dialog({
			height: 430,
			width: 500,
			modal: true
		});
		//alert("Jūs esate permokėję £" + document.frmtaxcalc.overpaid.value + "! Nieko nelaukite, skambinkite telefonu 07982400077 arba rašykite mums pasinaudodami www.mokesciai.co.uk esančia forma.");
	  }
	  else if (document.frmtaxcalc.overpaid.value < 50) {
			$('.amount3').html(document.frmtaxcalc.overpaid.value);
		var overpaid =  $( "#paidOver2" ).dialog({
			height: 80,
			width: 300,
			modal: true
		});
		//alert("Jūs esate permokėję £" + document.frmtaxcalc.overpaid.value + "! Nieko nelaukite, skambinkite telefonu 07982400077 arba rašykite mums pasinaudodami www.mokesciai.co.uk esančia forma.");
	  }
	}
	
}







/*
function selectReplacement(obj) {

      // append a class to the select

      obj.className += ' replaced';

      // create list for styling

      var ul = document.createElement('ul');

      ul.className = 'selectReplacement';

      var opts = obj.options;

      for (var i=0; i<opts.length; i++) {

        var selectedOpt;

        if (opts[i].selected) {

          selectedOpt = i;

          break;

        } else {

          selectedOpt = 0;

        }

      }

      for (var i=0; i<opts.length; i++) {

        var li = document.createElement('li');

        var txt = document.createTextNode(opts[i].text);

        li.appendChild(txt);

        li.selIndex = opts[i].index;

        li.selectID = obj.id;

        li.onclick = function() {

          selectMe(this);

        }

        if (i == selectedOpt) {

          li.className = 'selected';

          li.onclick = function() {

            this.parentNode.className += ' selectOpen';

            this.onclick = function() {

              selectMe(this);

            }

          }

        }

        if (window.attachEvent) {

          li.onmouseover = function() {

            this.className += ' hover';

          }

          li.onmouseout = function() {

            this.className = 

              this.className.replace(new RegExp(" hover\\b"), '');

          }

        }

        ul.appendChild(li);

      }

      // add the input and the ul

      obj.parentNode.appendChild(ul);

    }
	*/

    function selectMe(obj) {

      var lis = obj.parentNode.getElementsByTagName('li');

      for (var i=0; i<lis.length; i++) {

        if (lis[i] != obj) { // not the selected list item

          lis[i].className='';

          lis[i].onclick = function() {

            selectMe(this);

          }

       } else {

          setVal(obj.selectID, obj.selIndex);

          obj.className='selected';

          obj.parentNode.className = 

            obj.parentNode.className.replace(new RegExp(" selectOpen\\b"), '');

          obj.onclick = function() {

            obj.parentNode.className += ' selectOpen';

            this.onclick = function() {

              selectMe(this);

            }

          }

        }

      }

    }

    function setVal(objID, selIndex) {

      var obj = document.getElementById(objID);

      obj.selectedIndex = selIndex;

    }

    function setForm() {

      var s = document.getElementsByTagName('select');

      for (var i=0; i<s.length; i++) {

        selectReplacement(s[i]);

      }

    }

    function closeSel(obj) {

      // close the ul

    }

    window.onload = function() {

      (document.all && !window.print) ? null : setForm();

    }

