/*

*	update_approxnumberofpages() is called to create the approx. number of pages based on the following criteria:

*	ManuscriptNumber

*	TypeandSizeofFinishedBook

*	ManuscriptNumberType

*/

function update_approxnumberofpages() {

	var approxNumPages = 0;

	var manuscriptNumber = document.getElementById('ManuscriptNumber').value;

	var manuscriptNumberType = 0;

	var typeandSizeofFinishedBook = document.getElementById('TypeandSizeofFinishedBook').value;

	//	Incase anyone puts in "### pages", then remove anything after the spaces...
	if(manuscriptNumber.indexOf(' ') != -1) {
		var tmpSplit = manuscriptNumber.split(' ');
		manuscriptNumber = tmpSplit[0];
	}

	if(manuscriptNumber == '') {

		//	Just skip it, it is probably loading...

	} else if(

		typeandSizeofFinishedBook == 1

		&& (manuscriptNumber < minPagesArray[typeandSizeofFinishedBook] || manuscriptNumber > maxPagesArray[typeandSizeofFinishedBook])

	) {

		alert("Number of pages for this Type and Size must be between " + minPagesArray[typeandSizeofFinishedBook] + " and " + maxPagesArray[typeandSizeofFinishedBook] + " pages");

		document.getElementById('ManuscriptNumber').focus();

	} else {

		for(var a = 0; a < document.getElementsByName('ManuscriptNumberType').length; a++) {

			if(document.getElementsByName('ManuscriptNumberType')[a].checked == true) {

				manuscriptNumberType = document.getElementsByName('ManuscriptNumberType')[a].value;

				break;

			}

		}



		if(manuscriptNumberType == 0) {

			//	Do some calculations here as necessary...

			approxNumPages = manuscriptNumber;

		} else if(manuscriptNumberType == 1) {

			approxNumPages = parseFloat(manuscriptNumber * pageNumberCalcArray[manuscriptNumberType][typeandSizeofFinishedBook]);

		} else if(manuscriptNumberType == 2) {

			approxNumPages = parseFloat(manuscriptNumber / pageNumberCalcArray[manuscriptNumberType][typeandSizeofFinishedBook]);

		}



		var remainderOfFour = (manuscriptNumber % 4);

		if(remainderOfFour != 0) {

			approxNumPages = parseFloat(parseFloat(approxNumPages) + parseFloat((4 - remainderOfFour)));

		}

	}



	document.getElementById('ApproxNumberOfPages').innerHTML = Math.ceil(approxNumPages);

}



/*

*	update_distributordiscount() is called to update the Results table (at the bottom) with the correct Distribution Discount value

*/

function update_distributordiscount() {

	var disDiscount = document.getElementById('DistributionDiscount').value;

	if(disDiscount < minDisDiscount || disDiscount > maxDisDiscount) {

		alert("Distribution discount must be between " + minDisDiscount + "% and " + maxDisDiscount + "%");

		document.getElementById('DistributionDiscount').focus();

	} else {

		for(var a = 0; a < totalResultsOptions; a++) {

			document.getElementById(a + '_DistributorDiscount').innerHTML = (parseFloat(disDiscount)).toFixed(2) + '%';

		}

	}

}



/*

*	update_authorprice() is called to update the Results table (at the bottom) with the correct Author Pricing value(s)

*/

function update_authorprice() {

	var retailPrice = document.getElementById('RetailPrice').value;

	var bestSellerAuthorPrice = 0.00;

	var premiumAuthorPrice = 0.00;

	var colorInteriorAuthorPrice = 0.00;

	var basicAuthorPrice = 0.00;



	var manuscriptNumber = document.getElementById('ManuscriptNumber').value;

	var typeandSizeofFinishedBook = document.getElementById('TypeandSizeofFinishedBook').value;

	var approxNumPages = parseFloat(document.getElementById('ApproxNumberOfPages').innerHTML);

	//	Incase anyone puts in "### pages", then remove anything after the spaces...
	if(manuscriptNumber.indexOf(' ') != -1) {
		var tmpSplit = manuscriptNumber.split(' ');
		manuscriptNumber = tmpSplit[0];
	}

	//	Calculate for Best Seller options...

	if(typeandSizeofFinishedBook >= 0 && typeandSizeofFinishedBook <= 6) {

		if(parseFloat(approxNumPages) > parseFloat(authorPricePageCountSeparator[typeandSizeofFinishedBook])) {

			bestSellerAuthorPrice = parseFloat(authorPriceBestSellerAboveSeparatorInitial[typeandSizeofFinishedBook] + (parseFloat(authorPriceBestSellerAboveSeparatorCalcPerPage[typeandSizeofFinishedBook] * approxNumPages)));

		} else {

			bestSellerAuthorPrice = parseFloat(authorPriceBestSellerBelowEqualSeparatorInitial[typeandSizeofFinishedBook]);

		}

	}

	document.getElementById('0_AuthorPrice').innerHTML = '$' + (parseFloat(bestSellerAuthorPrice)).toFixed(2);



	//	Calculate for Premium options...

	premiumAuthorPrice = parseFloat(parseFloat(bestSellerAuthorPrice) + parseFloat(1));

	document.getElementById('1_AuthorPrice').innerHTML = '$' + (parseFloat(premiumAuthorPrice)).toFixed(2);



	//	Calculate for Color Interior options...

	if(typeandSizeofFinishedBook >= 7 && typeandSizeofFinishedBook <= 9) {

		if(parseFloat(approxNumPages) > parseFloat(authorPricePageCountSeparator[typeandSizeofFinishedBook])) {

			colorInteriorAuthorPrice = parseFloat(authorPriceBestSellerAboveSeparatorInitial[typeandSizeofFinishedBook] + (parseFloat(authorPriceBestSellerAboveSeparatorCalcPerPage[typeandSizeofFinishedBook] * approxNumPages)));

		} else {

			colorInteriorAuthorPrice = parseFloat(authorPriceBestSellerBelowEqualSeparatorInitial[typeandSizeofFinishedBook]);

		}

	}

	document.getElementById('2_AuthorPrice').innerHTML = '$' + (parseFloat(colorInteriorAuthorPrice)).toFixed(2);



	//	Calculate for Basic options...

	basicAuthorPrice = parseFloat(parseFloat(bestSellerAuthorPrice) + parseFloat(2));

	document.getElementById('3_AuthorPrice').innerHTML = '$' + (parseFloat(basicAuthorPrice)).toFixed(2);

}



/*

*	update_retailprice() is called to update the Results table (at the bottom) with the correct Retail Pricing value(s)

*/

function update_retailprice() {

	var retailPrice = document.getElementById('RetailPrice').value;

	for(var a = 0; a < totalResultsOptions; a++) {

		document.getElementById(a + '_RetailPrice').innerHTML = '$' + (parseFloat(retailPrice)).toFixed(2);

	}

}



/*

*	update_royltyprice() is called to update the Results table (at the bottom) with the correct Your Royalty value(s)

*/

function update_royltyprice() {

	var retailPrice = document.getElementById('RetailPrice').value;

	var bestSellerAuthorPrice = 0.00;

	var premiumAuthorPrice = 0.00;

	var colorInteriorAuthorPrice = 0.00;

	var basicAuthorPrice = 0.00;



	var manuscriptNumber = document.getElementById('ManuscriptNumber').value;

	var typeandSizeofFinishedBook = document.getElementById('TypeandSizeofFinishedBook').value;

	var approxNumPages = parseFloat(document.getElementById('ApproxNumberOfPages').innerHTML);


	//	Incase anyone puts in "### pages", then remove anything after the spaces...
	if(manuscriptNumber.indexOf(' ') != -1) {
		var tmpSplit = manuscriptNumber.split(' ');
		manuscriptNumber = tmpSplit[0];
	}
	
	var curRetailPrice = 0.00;

	var curDisDiscount = 0.00;

	var curAuthorPrice = 0.00;

	var curYourRoyalty = 0.00;



	for(var a = 0; a < totalResultsOptions; a++) {

		curRetailPrice = 0.00;

		curRetailPrice = document.getElementById(a + '_RetailPrice').innerHTML;

		curRetailPrice = curRetailPrice.substring(1);



		curDisDiscount = 0.00;

		curDisDiscount = document.getElementById(a + '_DistributorDiscount').innerHTML;

		curDisDiscount = curDisDiscount.substring(0, (curDisDiscount.length) - 1);



		curAuthorPrice = 0.00;

		curAuthorPrice = document.getElementById(a + '_AuthorPrice').innerHTML;

		curAuthorPrice = curAuthorPrice.substring(1);



		curYourRoyalty = 0.00;

		curYourRoyalty = parseFloat(curRetailPrice);

        if(a != 3) {

				curYourRoyalty = parseFloat(curYourRoyalty) - (parseFloat(curRetailPrice) * (parseFloat(curDisDiscount) / parseFloat(100)));

        }

		curYourRoyalty = parseFloat(curYourRoyalty) - parseFloat(curAuthorPrice);


		if(a == 3) {
			document.getElementById(a + '_YourRoyalty').innerHTML = '-';
		} else {
			document.getElementById(a + '_YourRoyalty').innerHTML = '$' + (parseFloat(curYourRoyalty)).toFixed(2);
		}
	}

}



/*

*	update_data_hidden() is called to very last and clears the unnecessary values in the Results table (at the bottom)

*/

function update_data_hidden() {

	var typeandSizeofFinishedBook = document.getElementById('TypeandSizeofFinishedBook').value;



	if(typeandSizeofFinishedBook >= 0 && typeandSizeofFinishedBook <= 6) {

		document.getElementById('2_RetailPrice').innerHTML = '-';

		document.getElementById('2_AuthorPrice').innerHTML = '-';

		document.getElementById('2_DistributorDiscount').innerHTML = '-';

		document.getElementById('2_YourRoyalty').innerHTML = '-';

	} else if(typeandSizeofFinishedBook >= 7 && typeandSizeofFinishedBook <= 9) {

		document.getElementById('0_RetailPrice').innerHTML = '-';

		document.getElementById('0_AuthorPrice').innerHTML = '-';

		document.getElementById('0_DistributorDiscount').innerHTML = '-';

		document.getElementById('0_YourRoyalty').innerHTML = '-';

		document.getElementById('1_RetailPrice').innerHTML = '-';

		document.getElementById('1_AuthorPrice').innerHTML = '-';

		document.getElementById('1_DistributorDiscount').innerHTML = '-';

		document.getElementById('1_YourRoyalty').innerHTML = '-';

		document.getElementById('3_RetailPrice').innerHTML = '-';

		document.getElementById('3_AuthorPrice').innerHTML = '-';

		document.getElementById('3_DistributorDiscount').innerHTML = '-';

		document.getElementById('3_YourRoyalty').innerHTML = '-';

	}

}



function update_blank_data() {

		document.getElementById('3_DistributorDiscount').innerHTML = '-';

		//document.getElementById('3_YourRoyalty').innerHTML = ' ';

}



/*

*	update_finalresults() is called at startup and on updates to update the values in the Results table (at the bottom)

*/

function update_finalresults() {

	update_approxnumberofpages();

	update_retailprice();

	update_authorprice();

	update_distributordiscount();

	update_royltyprice();

	update_data_hidden();

	update_blank_data();

}
