function rnd6(x)
{
if(x>=.000001){return Math.round(x*1000000)/1000000}
else{return "<0.000001"}
}//<!--end.f.rnd6-->

function rnd2(x){return Math.round(x*100)/100}

function rnd4(x){return Math.round(x*1000000)/1000000}

function corrn()
{document.inp.elements[0].value=Math.floor(document.inp.elements[0].value)}

function corrp()
{
if(eval(document.inp.elements[1].value)<0){document.inp.elements[1].value=1*0}
if(eval(document.inp.elements[1].value)>100){document.inp.elements[1].value=100*1}
}

var n = 1*0;
var r = 1*0;
var p = 1*0;
var q = 1*0;

var DSRp30 = 1*0;
var DSRp12 = 1*0;
var BDRp5 = 1*0;
var BDRp2 = 1*0;
var PSp = 1*0;

function calc()
{
n = Math.round(document.inp.elements[0].value*1 * 0.76);
n12 = Math.round(document.inp.elements[0].value*1 * 12 * 0.76);

//r = document.r.elements[0].value*1;
DSRr30 = Math.ceil(n * 0.175);
DSRr12 = Math.ceil(n12 * 0.175);
BDRr5 = Math.ceil(n * 0.05);
BDRr2 = Math.ceil(n * 0.02);
PSr = Math.ceil(n12 * 0.02);

p = eval(document.inp.elements[1].value) / 100.0;
if(p > 1) { p = 1; }
q = 1-p;
if(n<r) {alert("k cannot be greater than n")};
if(p>1) {alert("p cannot be greater than 1")};
if(p<0) {alert("p cannot be a negative value")};
if(n<1) {alert("n must be an integer equal to or greater than 1")};
if(Math.floor(n)<n) {alert("n must be an integer")};

if(n < 1001) 
	{DSRp30 = exact(DSRr30, n, document.DSRp30.elements[0])}
else
	{DSRp30 = norm(DSRr30, n, document.DSRp30.elements[0])}

if(n12 < 1001) 
	{DSRp12 = exact(DSRr12, n12, document.DSRp12.elements[0])}
else
	{DSRp12 = norm(DSRr12, n12, document.DSRp12.elements[0])}

if(n < 1001) 
	{BDRp2 = exact(BDRr2, n, document.BDRp2.elements[0])}
else
	{BDRp2 = norm(BDRr2, n, document.BDRp2.elements[0])}

if(n < 1001) 
	{BDRp5 = exact(BDRr5, n, document.BDRp5.elements[0])}
else
	{BDRp5 = norm(BDRr5, n, document.BDRp5.elements[0])}

if(n12 < 1001)
	{PSp = exact(PSr, n12, document.PSp.elements[0])}
else
	{PSp = norm(PSr, n12, document.PSp.elements[0])}
	
	
PS(PSp);
DSR30(DSRp30, n);
DSR12(DSRp12, n, n12);
BDR2(BDRp2);
BDR5(BDRp5);

}//<!--end.f.calc-->

function PS(p)
{
	var text = "";
	if(p > 50.0)
		text = "In order to qualify for power seller status an eBay seller must have at least a 98% feedback rating during the past 12 months.  At the given sales volume you would have a " + p + "% chance per year of being ineligible based on feedback.  You may have overestimated the number of bad buyers.";
	else if(p > 1.0)
		text = "In order to qualify for power seller status an eBay seller must have at least a 98% feedback rating during the past 12 months.  At the given sales volume you would have a " + p + "% chance per year of being ineligible based on feedback.";
	else
		text = "It appears highly unlikely that feedback will be an issue with your power seller qualification.";
	document.getElementById('PS').firstChild.nodeValue = text;
}

function DSR30(p, n)
{
	var text = "After November 2008 eBay requires all sellers to have a minimum rating of 4.3 in all DSR categories.  ";
	if(n <= 20.0)
		text = text + "Those with less than 10 ratings in a 30 day period will rely on their 12 month rating instead.  ";

  	if(p > 50.0)
		text = text + "You have a " + p + "% chance of being suspended each month based on your 30 day DSR ratings.  You may have overestimated the number of bad buyers or need to increase your monthly sales volume.";
	else if(p > 1.0)
		text = text + "You have a " + p + "% chance of being suspended each month based on your 30 day DSR ratings.";
	else
		text = text + "It appears highly unlikely that you will be suspended due to your 30 day DSRs.";
	document.getElementById('DSR30').firstChild.nodeValue = text;
}

function DSR12(p, n, n12)
{
	var text = "";
	if(n <= 20.0)
		text = text + "Based on your monthly sales volume you may need to pay attention to your 12 month DSRs.  ";

  	if(p > 50.0)
		text = text + "You have a " + p + "% chance of being suspended based on your 12 month DSR ratings if you ever have less than 10 ratings for your 30 day DSRs.  You may have overestimated the number of bad buyers or need to increase your monthly sales volume.";
	else if(p > 1.0)
		text = text + "You have a " + p + "% chance of being suspended based on your 12 month DSR ratings if you ever have less than 10 ratings for your 30 day DSRs."
	else
		text = "It appears highly unlikely that you will be suspended due to your 12 month DSRs.";
	document.getElementById('DSR12').firstChild.nodeValue = text;
}

function BDR2(p)
{
	var text = "";
	if(p > 50.0)
		text = "A seller with a buyer dissatisfaction rate of more than 2% may be penalized in best match search.  Based on your monthly sales you have a " + p + "% chance per month of having lowered search standings based on your 30 day BDR rate.  You may have overestimated the number of bad buyers or need to increase your sales volume.";
	else if(p > 1.0)
		text = "A seller with a buyer dissatisfaction rate of more than 2% may be penalized in best match search.  Based on your monthly sales you have a " + p + "% chance per month of having lowered search standings based on your 30 day BDR rate.  You may need to increase your monthly sales volume.";
	else
		text = "It appears highly unlikely that you will experience lowered search standing due to your buyer dissatisfaction rate.";
	document.getElementById('BDR2').firstChild.nodeValue = text;
}

function BDR5(p)
{
	var text = "";
	if(p > 50.0)
		text = "A seller with a buyer dissatisfaction rate of more than 5% may be required to offer PayPal and have those payments held for up to 21 days.  Often a seller will be suspended for 30 days immediately after reaching a BDR of 5% or more.  Based on your monthly sales volume you have a " + p + "% chance per month of having payments held or being suspended from eBay for 30 days.  You may have overestimated the number of bad buyers or need to increase your sales volume.";
	else if(p > 1.0)
		text = "A seller with a buyer dissatisfaction rate of more than 5% may be required to offer PayPal and have those payments held for up to 21 days.  Often a seller will be suspended for 30 days immediately after reaching a BDR of 5% or more.  Based on your monthly sales volume you have a " + p + "% chance per month of having payments held or being suspended from eBay for 30 days.";
	else
		text = "It appears highly unlikely that you will be suspended from eBay or have payments held due to your buyer dissatisfaction rate.";
	document.getElementById('BDR5').firstChild.nodeValue = text;
}


function exact(r, n, element)
{
var tabz = new Array(n+1);
var a = 1*0;
var b = 1*0;
var sum = 1*0;
var prob = 1*0;
var nr = 1*0;
var xct = 1*0;
//var pt1 = 1*0;
var pt2 = 1*0;
var temp1 = 1*0;
var temp5 = 1*0;
var temp9 = 1*0;

if(n > 1001) { element.value = "----"; return 0;}

//alert("n = " + n + " r = " + r)

for(i=1, factn=0; i<n; i++, factn+=Math.log(i));

for (var j=0; j<n+1; j++)
{

for(i=1, factr=0; i<j; i++, factr+=Math.log(i));
nr = n-j;
for(i=1, factnr=0; i<nr; i++, factnr+=Math.log(i));
a = factn-(factr+factnr);
a = Math.exp(a);
b = Math.pow(p, j)*Math.pow(q, n-j);
tabz[j] = a*b;
if(j==r)
{
xct = tabz[j];
temp1 = tabz[j];
}
}//<!--end.for-->


var ltx = 0*1;
var gtx = 0*1;

for (var j=0; j<r+1; j++){ltx+=tabz[j]*1}
temp5 = ltx;

for (var j=r; j<n+1; j++){gtx+=tabz[j]*1}
temp9 = gtx;

if (r == n) {temp5 = 1.0};
if (r == 0) {temp9 = 1.0};
if(r==0){temp5 = temp1}
if(r==n){temp9 = temp1}

element.value = rnd2(temp9 * 100);
return rnd2(temp9 * 100);
}//end.f.exact

function norm(r, n, element)
{
var mx = n*p;
var varx = n*p*q;
var sdx = Math.sqrt(varx);

var diff = r-mx;
var z = 1*0;

var temp2 = 1*0;
var temp3 = 1*0;
var temp12 = 1*0;
var temp15 = 1*0;
var temp16 = 1*0;

if(r>mx)
{
z = (diff-.5)/sdx;
z = Math.round(100*z)/100;
}
if (r<mx)
{
z = (1*diff+.5)/sdx;
z = Math.round(100*z)/100;
}
if (r==mx)
{
z = 1*0;
}

z = Math.abs(z);
var p2 = (((((.000005383*z+.0000488906)*z+.0000380036)*z+.0032776263)*z+.0211410061)*z+.049867347)*z+1;
p2 = Math.pow(p2, -16);
var p1 = p2/2;
temp15 = p1;

var otr = 1*0;
z = z+(1/sdx)*1;
var p2x = (((((.000005383*z+.0000488906)*z+.0000380036)*z+.0032776263)*z+.0211410061)*z+.049867347)*z+1;
p2x = Math.pow(p2x, -16);
otr = p2-p2x;
otr = otr/2;
temp2 = otr;

if(r>mx)
{
temp10 = temp15;
}
if(r<mx)
{
temp10 = 1 - temp15 + temp2 * 1;
}

//temp2 = rnd6(temp2);
//temp10 = rnd6(temp10);
//temp15 = rnd6(temp15);

if(r==mx)
{
temp15 = .5+(temp2 / 2)*1;
temp10 = temp15;
}

	element.value = rnd2(temp10 * 100);
	return rnd2(temp10 * 100);
}//<!--end.f.norm-->
