function GetPoint(rating, coeff) {
	if (rating<=1500) {
		var points=coeff*(0.22*rating+14);
		if (points <= 0) {
			return 0;
		}else{
			return parseInt(points, 10);
		}
	}else{
		return parseInt(coeff*(1511.26/(1+1639.28*Math.pow(2.71828, -0.00412*rating))), 10);
	}
}

function GetRating(points, coeff) {
	var rating = parseInt(Math.log((1511.26/(1639.28*points/coeff))-(1/1639.28))/(-0.00412),10);
	if (rating <= 1500) {
		return parseInt(((points/coeff)-14)/0.22,10);
	}else{
		return rating;
	}
}

function RatingToPoints() {
	var rating = parseInt(document.getElementById('rating_22').value,10);
	if (rating > 0 && rating <= 4000) {
	  document.getElementById('points_22').value = GetPoint(rating, 0.76);
	}else{
	  document.getElementById('points_22').value = 'Invalid Value';
	}

	rating = parseInt(document.getElementById('rating_33').value,10);
	if (rating > 0 && rating <= 4000) {
	  document.getElementById('points_33').value = GetPoint(rating, 0.88);
	}else{
	  document.getElementById('points_33').value = 'Invalid Value';
	}

	rating = parseInt(document.getElementById('rating_55').value,10);
	if (rating > 0 && rating <= 4000) {
	  document.getElementById('points_55').value = GetPoint(rating, 1);
	}else{
	  document.getElementById('points_55').value = 'Invalid Value';
	}
}

function PointsToRating() {
	var points = parseInt(document.getElementById('points').value,10);
	
	rating = GetRating(points, 0.76);
	if (rating > 0 && rating < 4000) {
		document.getElementById('rat_22').value = rating;
	}else{
		document.getElementById('rat_22').value = 'Not Possible';
	}

	rating = GetRating(points, 0.88);
	if (rating > 0 && rating < 4000) {
		document.getElementById('rat_33').value = rating;
	}else{
		document.getElementById('rat_33').value = 'Not Possible';
	}

	rating = GetRating(points, 1);
	if (rating > 0 && rating < 4000) {
		document.getElementById('rat_55').value = rating;
	}else{
		document.getElementById('rat_55').value = 'Not Possible';
	}
	
}

function CalculateSum(Atext, Btext, Ctext, Etext, Dtext, form)
{
var A = parseFloat(Atext);
var B = parseFloat(Btext);
var C = parseFloat(Ctext);
var E = parseFloat(Etext);
var D = parseFloat(Dtext);
var Mit = 0;
if (D >= 1 && D < 60) 
{
	Mit = B / (B + 400 + 85 * D);
}
else if (D >=60 && D <= 83) 
{
	Mit = B / (B - 22167.5 + 467.5 * D);
}
else
{
	D = 83;
	Mit = B / (B - 22167.5 + 467.5 * D);
}
var SSAvg = (1015 + C) * 1.1* 1.1;
var SSAvgDef = SSAvg * 0.9;
var SSAvgDefTPS = ((SSAvgDef + 770) / 6) * 2.0735;
var SSMax = (1040 + C) * 1.1 * 1.1; 
var SSMaxCrit = SSMax * 2;
var AnswerNoDef = (A + C) / ((1 - Mit) * 0.88);
var AnswerDef = (A + C) / ((1 - Mit) * 0.9);
var AnswerRF = (A + C) / ((1 - Mit) * 0.94 * 0.97);
var TDRWar = ((1 - ((1 - (Mit)) * (1 -(E / 100))) * 0.9 ) * 100);
var TDRPal = ((1 - ((1 - (Mit)) * (1 -(E / 100))) * 0.94 * 0.97 ) * 100);
var TDRDru = ((1 - ((1 - (Mit)) * (1 -(E / 100))) * 0.88 ) * 100);
form.Answer.value = AnswerNoDef.toFixed(2);
form.AnswerDef.value = AnswerDef.toFixed(2);
form.Answer2.value = AnswerRF.toFixed(2);
form.TDRWarrior.value = TDRWar.toFixed(2);
form.TDRPaladin.value = TDRPal.toFixed(2);
form.TDRDruid.value = TDRDru.toFixed(2);
}