// JavaScript Document
// Created by Denny Brown, Expert Support Inc., 650-917-7892, dbrown@xs.com
// Creation date: May 29, 2007
// Copyright Expert Support Inc., All rights reserved.

function xsMailTo(whoTo, domainTo, linkText, subjectTo, bodyTo ) {
	// xsMailTo is a function that puts a mailto: link into a file without making the email address obviously visible to
	// robots that might be trying to harvest email addresses.
	// whoTo specifies the target email address without the domain part.
	// domainTo specifies the domain for the address.
	// linkText is what appears to the visitor in her browser.
	// subjectTo is an optional Subject for the message to be sent.
	// bodyTo is an optional Body for the message to be sent.
	// Sample usage: 
	//   <script language="JavaScript" type="text/javascript"> xsMailTo("webinquiry", "xs.com", "webInquiry at xs.com", "Free Developmental Edit",
	//                   "Please contact me about a free developmental edit on the document of my choice."); </script >
	if (!subjectTo) { subjectTo = ""; };
	if (!bodyTo) { bodyTo = ""; };
	document.write('<a href=\"mai' + 'lto:' + whoTo + '&#64;' + domainTo + '?subject=' + subjectTo + '&body=' + bodyTo
				   + '\" onMouseOver=\"window.status=\' \';return true\">' + linkText + '</a>');
	
} // xsMailTo


var debugging = false; // For turning debugging stuff on (true) or off (false).

// set two global variables to hold the object references to the two <div>s on the page.

function showNotes () {
	var iNotesObj = MM_findObj("invisibleNotes", document);
	var vNotesObj = MM_findObj("visibleNotes", document);
	// var iNotesObj = document.invisibleNotes;
	// var vNotesObj = document.visibleNotes;
	iNotesObj.style.visibility = "hidden";
	vNotesObj.style.visibility = "inherit";
} // end showNotes

function hideNotes () {
	var iNotesObj = MM_findObj("invisibleNotes", document);
	var vNotesObj = MM_findObj("visibleNotes", document);
	// var iNotesObj = document.invisibleNotes;
	// var vNotesObj = document.visibleNotes;
	iNotesObj.style.visibility = "inherit";
	vNotesObj.style.visibility = "hidden";
} // end showNotes

// library functions

function MM_findObj(n, d) { //v4.0
	// Find and return an object in document d with name n
	// debugging: alert("MM_findObj w. n = " + n);
	var p,i,x;  if(!d) d=document; 
	if((p=n.indexOf("?"))>0&&parent.frames.length) {
    	d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
	if(!(x=d[n])&&d.all) x=d.all[n]; 
	for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
	for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
	if(!x && document.getElementById) x=document.getElementById(n); 
	// debugging: if (x) {alert("x.src = " + x.src)} else {alert("x is null.")};
	return x;
}; // MM_findObj

function onLoadInit () {
	// call this as an onLoad handler in the <body> tag of your page to initialize.
	hideNotes ();
	// loadAlert ();
}; // onLoadInit

// utility functions

function setDebuggingAttributes () {
	var debugTableObject = MM_findObj("debugTable", document);
	if (debugTableObject && debugTableObject.style) {
		// alert ("Found the debugTableObject: " + debugTableObject.id);
		if (debugging) {
			debugTableObject.style.visibility = "inherit";
		} else {
			debugTableObject.style.visibility = "hidden";
		};
	} else {
		alert ("Couldn't find the debugTableObject: ");
	};
}; // setDebuggingAttributes


function rangeCheck (index, name, low, high) {
		if ((index < low) || (index > high)) {
			alert(name + " index is out of range: " + index);
			return (false);
		} else {
			return (true);
		}
}; // rangeCheck

function debugDisplay (fieldObject, val) {
	if (debugging) {fieldObject.value = formatDollars(val)};
}; // debugDisplay

function setRadioIndex (radioButtonObject) {
	for (i = 0; i < radioButtonObject.length; i++) {
		if (radioButtonObject[i].checked) return (Number(radioButtonObject[i].value));
	};
	alert("No button checked for " + radioButtonObject.name);
	return(-1);
}; // setRadioIndex

function checkRadioValue (radioButtonObject, val) {
	// Mark as checked the radio button whose value is val.
	for (i = 0; i < radioButtonObject.length; i++) {
		if (Number(radioButtonObject[i].value) == val) {return (radioButtonObject[i].checked = true);};
	};
	alert("No value for " + radioButtonObject.name + " equal to " + val);
	return(-1);
}; // checkRadioValue

function formatDollars(txt){
	txt = Number(txt);
	txt = Math.round(txt*100)/100;
	txt = String(txt);
	if(txt.indexOf('.')==-1){
		txt += '.';
	};
	
	num_zeros = 3 - txt.length + txt.indexOf('.');
	for(i=0;i < num_zeros;i++){
		txt += '0';
	};
	return "$" + txt;
}; // formatDollars

function toggleDebugging () {
	if (debugging) {debugging = false} else {debugging = true};
	setDebuggingAttributes();
}; // toggleDebugging

function loadAlert () {
	alert (loadedMessage);
}; // loadAlert


loadedMessage = "kme.js has been loaded.";

// The main calculate function

function calculate () {
	// map page fields to js objects, indices, and values
	
	// Input objects
	var bookSizeObject = document.calculator.fBookSize;
	var bookSizeIndex = setRadioIndex (bookSizeObject); 
	
	var insideInkObject = document.calculator.fPageInk;
	var insideInkIndex = setRadioIndex (insideInkObject); 
	
	var insidePaperObject = document.calculator.fPagePaper;
	var insidePaperIndex = setRadioIndex (insidePaperObject);
	
	var nPagesObject = document.calculator.fnPages;
	var numberOfPages = (Number(nPagesObject.options[nPagesObject.selectedIndex].value));
	
	var quantityObject = document.calculator.fQuantity;
	var quantity = Number(quantityObject.value);
	
	var coverPaperObject = document.calculator.fCoverPaper;
	var coverPaperIndex = setRadioIndex (coverPaperObject);
	
	var outCoverInkObject = document.calculator.fOutCoverInk;
	var outCoverInkIndex = setRadioIndex (outCoverInkObject);
	
	var inCoverInkObject = document.calculator.fInCoverInk;
	var inCoverInkIndex = setRadioIndex (inCoverInkObject);
	
	var faceTrimObject = document.calculator.fFaceTrim;
	var faceTrimIndex = setRadioIndex (faceTrimObject);
	
	// Result objects
	var coverTotalObject = document.calculator.fCoverTotal;
	var pageTotalObject = document.calculator.fPageTotal;
	var setupBindingObject = document.calculator.fSetupBindingTotal;
	var discountObject = document.calculator.fDiscountTotal;
	var totalObject = document.calculator.fTotal;
	
	// debugging objects
	var iInkValueObject = document.calculator.fInsideInkValue;
	var iPaperValueObject = document.calculator.fInsidePaperValue;
	var iPerPageValueObject = document.calculator.fInsidePerPageValue;
	var CPValueObject = document.calculator.fCPValue;
	var OCInkValueObject = document.calculator.fOCInkValue;
	var ICInkValueObject = document.calculator.fICInkValue;
	var FTValueObject = document.calculator.fFTValue;
	var CoverPerPageValueObject = document.calculator.fCoverPerPageValue;
	var setupValueObject = document.calculator.fSetupValue;
	var bindingValueObject = document.calculator.fBindingValue;
	
	// the div tag objects for hiding options
	var iGlossyObject = MM_findObj("iGlossy", document);
	var cGlossyObject = MM_findObj("cGlossy", document);

	// calculation variables
	var iInk, iPaper, iPerPage, CP, OCInk, ICInk, FT, cPerPage;
	
	// alert("bookSizeIndex = " + bookSizeIndex + "; insideInkIndex = " + insideInkIndex + "; insidePaperIndex = " + insidePaperIndex);
	// alert("numberOfPages = " + numberOfPages + "; quantity = " + quantity);
	
	noGlossyRule(bookSizeIndex, coverPaperObject, insidePaperObject); // check the noGlossyRule. Then reset the value, in case it changed.
	insidePaperIndex = setRadioIndex (insidePaperObject);
	coverPaperIndex = setRadioIndex (coverPaperObject);
	
	if (iGlossyObject && cGlossyObject) {
		// alert("Found the glossy objects: " + iGlossyObject.id + " & " + cGlossyObject.id);
		if (bookSizeIndex == booklet) {
			iGlossyObject.style.visibility = "hidden";
			cGlossyObject.style.visibility = "hidden";
		} else {
			iGlossyObject.style.visibility = "inherit";
			cGlossyObject.style.visibility = "inherit";
		};
	} else {
		alert("Couldn't find the glossy objects. ");
	};
	
	if (rangeCheck (bookSizeIndex, "bookSize", 0, 1)) {
		if (rangeCheck (insideInkIndex, "insideInk", 0, 1) ) {
			iInk = insideInk[bookSizeIndex][insideInkIndex];
			debugDisplay(iInkValueObject, iInk);
		};
		if (rangeCheck (insidePaperIndex, "insidePaper", 0, 2)) {
			iPaper = insidePaper[bookSizeIndex][insidePaperIndex];
			debugDisplay(iPaperValueObject, iPaper);
		};
		iPerPage = iInk + iPaper;
		debugDisplay(iPerPageValueObject, iPerPage);
		if (rangeCheck (coverPaperIndex, "coverPaper", 0, 5)) {
			CP = coverPaper[bookSizeIndex][coverPaperIndex];
			debugDisplay(CPValueObject, CP);
		};
		if (rangeCheck (outCoverInkIndex, "outCoverInk", 0, 1)) {
			OCInk = oCoverInk[bookSizeIndex][outCoverInkIndex];
			debugDisplay(OCInkValueObject, OCInk);
		};
		if (rangeCheck (inCoverInkIndex, "inCoverInk", 0, 2)) {
			ICInk = iCoverInk[bookSizeIndex][inCoverInkIndex];
			debugDisplay(ICInkValueObject, ICInk);
		};
		if (rangeCheck (faceTrimIndex, "faceTrim", 0, 1)) {
			FT = faceTrim[faceTrimIndex];
			// alert("FT = " + FT + "; faceTrimIndex = " + faceTrimIndex);
			debugDisplay(FTValueObject, FT);
		};
		cPerPage = CP + OCInk + ICInk + FT;
		debugDisplay(CoverPerPageValueObject, cPerPage);
	};
	
	var thePageTotal = quantity * numberOfPages * iPerPage;
	var theCoverTotal = quantity * cPerPage;
	var theSetupCharge = setupCharge(numberOfPages);
	debugDisplay(setupValueObject, theSetupCharge);
	var theBindingCharge = bindingCharge(quantity);
	debugDisplay(bindingValueObject, theBindingCharge);
	var theSetupBindingTotal = theSetupCharge+ theBindingCharge;
	var theDiscountTotal = - discountAmount (thePageTotal + theCoverTotal + theBindingCharge);
	var theTotal = thePageTotal + theCoverTotal + theSetupBindingTotal + theDiscountTotal;
	
	coverTotalObject.value = formatDollars(theCoverTotal);
	pageTotalObject.value = formatDollars(thePageTotal);
	setupBindingObject.value = formatDollars(theSetupBindingTotal);
	discountObject.value = formatDollars(theDiscountTotal);
	totalObject.value = formatDollars(theTotal);

}; // calculate
