/*
domestic company

10000000  IT Rs 3000000 SC Rs ---    EC Rs 60000 SHEC Rs 30000 Total Rs 3090000
10100000  IT Rs 3030000 SC Rs 70000  EC Rs 62000 SHEC Rs 31000 Total Rs 3193000
10200000  IT Rs 3060000 SC Rs 140000 EC Rs 64000 SHEC Rs 32000 Total Rs 3296000
10300000  IT Rs 3090000 SC Rs 210000 EC Rs 66000 SHEC Rs 33000 Total Rs 3399000
10400000  IT Rs 3120000 SC Rs 280000 EC Rs 68000 SHEC Rs 34000 Total Rs 3502000
*/

var aytag;
var incometag;
var statustag;
var incometaxtag;
var surchargetag;
var educesstag;
var sheducesstag;
var totaltaxtag;

var ay;
var income;
var status;
var incometax;
var surcharge;
var educess;
var sheducess;
var totaltax;

var taxon1crore;

function init()
{
    aytag = document.getElementById("ay");
    incometag = document.getElementById("income");
    statustag = document.getElementById("status");
    incometaxtag = document.getElementById("incometax");
    surchargetag = document.getElementById("surcharge");
    educesstag = document.getElementById("educess");
    sheducesstag = document.getElementById("sheducess");
    document.getElementById("calcbutton").disabled = false;
}

function calculateTax()
{
    /* allow only numbers with decimal points in them */
    incometag.value = incometag.value.replace(/[^0-9.]/g, '');
    ay = aytag.value;
    
    calculateTax1();
    
    /* later */
    /*
      incometax = Math.round(incometax * 100)/100;
      surcharge = Math.round(surcharge * 100)/100;
      educess = Math.round(educess * 100)/100;
      totaltax = Math.round(totaltax * 100)/100;
    */
    incometaxtag.innerHTML = "Rs " + incometax;
    surchargetag.innerHTML = "Rs " + surcharge;
    educesstag.innerHTML = "Rs " + educess;
    sheducesstag.innerHTML = "Rs " + sheducess;
}

function calculateTax1()
{
    var slab = new Array();
    var rate = new Array();
    var surchargerate;
    var educessrate;
    var sheducessrate;
    var i;
    income = incometag.value;
    status = statustag.value;
    
    if (ay == "1011") {
	if (status == "woman") {
	    slab[0] = 190000;
	    slab[1] = 300000;
	    slab[2] = 500000;
	    rate[0] = 0.10;
	    rate[1] = 0.20;
	    rate[2] = 0.30;
	    surchargerate = 0.00;
	    educessrate = 0.02;
	    sheducessrate = 0.01;
	} else if (status == "senior") {
	    slab[0] = 240000;
	    slab[1] = 300000;
	    slab[2] = 500000;
	    rate[0] = 0.10;
	    rate[1] = 0.20;
	    rate[2] = 0.30;
	    surchargerate = 0.00;
	    educessrate = 0.02;
	    sheducessrate = 0.01;
	} else if (status == "individual") {
	    slab[0] = 160000;
	    slab[1] = 300000;
	    slab[2] = 500000;
	    rate[0] = 0.10;
	    rate[1] = 0.20;
	    rate[2] = 0.30;
	    surchargerate = 0.00;
	    educessrate = 0.02;
	    sheducessrate = 0.01;
	} else if (status == "firm") {
	    slab[0] = 0;
	    rate[0] = 0.30;
	    surchargerate = 0.00;
	    educessrate = 0.02;
	    sheducessrate = 0.01;
	} else if (status == "domestic") {
	    slab[0] = 0;
	    rate[0] = 0.30;
	    if (income > 10000000) {
		surchargerate = 0.10;
	    } else {
		surchargerate = 0.00;
	    }
	    educessrate = 0.02;
	    sheducessrate = 0.01;
	    taxon1crore = 3090000;
	} else if (status == "foreign_royalty") {
	    slab[0] = 0;
	    rate[0] = 0.50;
	    if (income > 10000000) {
		surchargerate = 0.025;
	    } else {
		surchargerate = 0.00;
	    }
	    educessrate = 0.02;
	    sheducessrate = 0.01;
	    taxon1crore = 5278750;
	} else if (status == "foreign_other") {
	    slab[0] = 0;
	    rate[0] = 0.40;
	    if (income > 10000000) {
		surchargerate = 0.025;
	    } else {
		surchargerate = 0.00;
	    }
	    educessrate = 0.02;
	    sheducessrate = 0.01;
	    taxon1crore = 4223000;
	} else if (status == "coop") {
	    slab[0] = 0;
	    slab[1] = 10000;
	    slab[2] = 20000;
	    rate[0] = 0.10;
	    rate[1] = 0.20;
	    rate[2] = 0.30;
	    surchargerate = 0.00;
	    educessrate = 0.02;
	    sheducessrate = 0.01;
	} else if (status == "localauthority") {
	    slab[0] = 0;
	    rate[0] = 0.30;
	    surchargerate = 0.00;
	    educessrate = 0.02;
	    sheducessrate = 0.01;
	}
    } else if (ay == "1112") {
	if (status == "woman") {
	    slab[0] = 190000;
	    slab[1] = 500000;
	    slab[2] = 800000;
	    rate[0] = 0.10;
	    rate[1] = 0.20;
	    rate[2] = 0.30;
	    surchargerate = 0.00;
	    educessrate = 0.02;
	    sheducessrate = 0.01;
	} else if (status == "senior") {
	    slab[0] = 240000;
	    slab[1] = 300000;
	    slab[2] = 500000;
	    rate[0] = 0.10;
	    rate[1] = 0.20;
	    rate[2] = 0.30;
	    surchargerate = 0.00;
	    educessrate = 0.02;
	    sheducessrate = 0.01;
	} else if (status == "individual") {
	    slab[0] = 160000;
	    slab[1] = 300000;
	    slab[2] = 500000;
	    rate[0] = 0.10;
	    rate[1] = 0.20;
	    rate[2] = 0.30;
	    surchargerate = 0.00;
	    educessrate = 0.02;
	    sheducessrate = 0.01;
	} else if (status == "firm") {
	    slab[0] = 0;
	    rate[0] = 0.30;
	    surchargerate = 0.00;
	    educessrate = 0.02;
	    sheducessrate = 0.01;
	} else if (status == "domestic") {
	    slab[0] = 0;
	    rate[0] = 0.30;
	    if (income > 10000000) {
		surchargerate = 0.075;
	    } else {
		surchargerate = 0.00;
	    }
	    educessrate = 0.02;
	    sheducessrate = 0.01;
	} else if (status == "foreign_royalty") {
	    slab[0] = 0;
	    rate[0] = 0.50;
	    if (income > 10000000) {
		surchargerate = 0.025;
	    } else {
		surchargerate = 0.00;
	    }
	    educessrate = 0.02;
	    sheducessrate = 0.01;
	} else if (status == "foreign_other") {
	    slab[0] = 0;
	    rate[0] = 0.40;
	    if (income > 10000000) {
		surchargerate = 0.025;
	    } else {
		surchargerate = 0.00;
	    }
	    educessrate = 0.02;
	    sheducessrate = 0.01;			
	} else if (status == "coop") {
	    slab[0] = 0;
	    slab[1] = 10000;
	    slab[2] = 20000;
	    rate[0] = 0.10;
	    rate[1] = 0.20;
	    rate[2] = 0.30;
	    surchargerate = 0.00;
	    educessrate = 0.02;
	    sheducessrate = 0.01;
	} else if (status == "localauthority") {
	    slab[0] = 0;
	    rate[0] = 0.30;
	    surchargerate = 0.00;
	    educessrate = 0.02;
	    sheducessrate = 0.01;
	}
    }
    incometax = 0.00;
    for (i = 0; i < (slab.length - 1); i++) {
	if (income > slab[i]) {
	    incometax = incometax + ((Math.min(income, slab[i + 1]) - slab[i]) * rate[i]);
	}
    }
    if (income > slab[i]) {
	incometax = incometax + ((income - slab[i]) * rate[i]);
    }
    incometax = Math.round(incometax);

    surcharge = incometax * surchargerate;
    incometax = incometax + surcharge;
    incometax = Math.round(incometax);
/*
    if (status == "domestic" || status == "foreign_royalty" || status == "foreign_other") {
	if ((income > 10000000) && (surcharge > (income - 10000000))) {
		surcharge = (income - 10000000);
    		incometax = incometax + surcharge;
		incometax = Math.round(incometax);
		console.log(surcharge);
		console.log(incometax);
		console.log(income - 10000000);
	}
    }
*/
    
    educess = incometax * educessrate;
    sheducess = incometax * sheducessrate;
    incometax = incometax + educess + sheducess;
    incometax = Math.round(incometax);
}


// a should not exceed b by more than 5 units
// if a > b
// and a - b > 5
