var randText=new Array();
var didYouKnowText=new Array();
var testimonialTitle;
var testimonialFullHeader;
var newWin;
var newDoc;
var demoWin;

function InsertCopyrightYear() {
	d = new Date();
	document.write("-" + d.getFullYear());
}

function OpenPrintWin(url) {
	closeup = window.open(url, 'closeup','scrollbars=yes,resizable=yes,menubar=yes,status=yes,screenX=0,screenY=0,width=660,height=450');
	closeup.focus();
	return false;
}

function QuestionFooter() {
	document.write("<DIV ALIGN=RIGHT STYLE=\"margin-top: 0.2em;\"><I><STRONG>Still Have Questions?</STRONG> - <A HREF=\"#top\">Go to Help Center</A> or <A HREF=\"#contact\">Contact Us</A></I></DIV>");
}

function DidYouKnowText() {
	if (document.referrer.indexOf('co.uk') != -1) { 
		document.write('<A HREF=\"http://www.amazon.co.uk/exec/obidos/external-search?tag=calculators08-21&keyword=calculator&mode=blended\"  target=\"_blank\">United Kingdom</A> customers may be able to find their calculator locally!');
	} 
	else if (document.referrer.indexOf('google.de') != -1) { 
		document.write('<A HREF=\"http://www.amazon.de/exec/obidos/redirect-home?tag=calculators05-21&site=home\" target=\"_blank\">German</A> customers may be able to find their calculator locally!');
	} 
	else {
		didYouKnowText[didYouKnowText.length] = "We have <EM>many</EM> calculator products that aren't yet listed on our website!";

		//we assume data in arrays is indexed from 1 so length indicates one extra element
		numItems = didYouKnowText.length - 1;

		ry = 0;

		if (numItems > 0) {
			var ry=Math.round(Math.random()*(numItems-1));
		}

		ry = ry + 1;

		document.write(didYouKnowText[ry]);
	}
}

function DisplayText(maxTextLen) {
	var txt;
	var sepLoc;
	var charToTrim;
	var tempStr;

	//we assume all arrays are indexed from 1 so length indicates one extra element
	numItems = randText.length - 1;

	if (numItems > 0) {
		var ry=Math.round(Math.random()*(numItems-1));

		ry = ry + 1;

		txt = randText[ry];

		//is there a maximum length limitation?
		if (maxTextLen > 0) {	
			//do we have to trim the string?
			charToTrim = txt.length - maxTextLen;
			if (charToTrim > 0) {
				//find string/author separator (if any)
				sepLoc = txt.indexOf("\" -- ");
				if (sepLoc >= 0) {
					//trim before the separator
					tempStr = txt.substring(0,sepLoc-charToTrim);					
					txt = tempStr + ' [more...]' + txt.substring(sepLoc,txt.length + 1);
				} else {
					//trim
					txt = txt.substring(0,txt.length - charToTrim) +  ' [more...]\"';
				}
			}
		}

		document.write(txt);
	}
}

function DisplayTestimonial() {
	if (randText.length > 0) {
		document.write('<A CLASS=\"testimonial\" HREF=\"javascript:DisplayMore()\" TITLE=\"Click to read more...\">');
		document.write('<STRONG><U>' + testimonialTitle + ':</U></STRONG><BR>');
		DisplayText(180);
		document.write('</A>');
	}
}

function DisplayMore() {
	var strHTML;
	var i;

	strHTML='<HTML>\n';
	strHTML+='<HEAD>\n';
	strHTML+='<TITLE>CalculatorSource - ' + testimonialTitle + '</TITLE>\n';
	strHTML+='<link rel=\"stylesheet\" type=\"text/css\" href=\"/lib/calculatorsource/genstyle.css\">\n';
	strHTML+='</HEAD>\n';
	strHTML+='<BODY>\n';
	strHTML+='<DIV CLASS=\"button-color\"><SMALL><B>' + testimonialFullHeader + '</B></SMALL></DIV>\n';

	//go in reverse to display latest at the top	
	for (i = randText.length - 1 ; i > 0; i--) {		
		strHTML+='<P CLASS=\"testimonial\">'+randText[i]+'\n';
	}

	strHTML+='</BODY>\n';
	strHTML+='</HTML>';

//	newWin=window.open('','testimonialWin','scrollbars=yes,resizable=yes,left=10,top=10,width=430,height=470');
	newWin=window.open('','testimonialWin','scrollbars=yes,resizable=yes,left=10,top=10,width=350,height=400');
	newWin.focus();
	newDoc=newWin.document;
	newDoc.write(strHTML);
	newDoc.close();
}

function DisplayHelp(helpEntry) {
	var helpText;
	var helpTitle;

	switch(helpEntry) {
		case "availability":
			helpTitle = "Availability";
			helpText = "<SMALL><A HREF=\"#preorder\">Pre-orders</A><BR><A HREF=\"#download\">Electronic Downloads</A><BR><A HREF=\"#notavail\">Not Available</A></SMALL><P>Availability indicates how much time is required to <I>ship</I> (not deliver) the item.  For example, if Availability states \"Usually ships in 2-3 business days\" you can expect the item to ship within 2-3 business days of your order.  Once the item ships, time in transit will depend on the shipping method you select.  Total time from order to delivery is <STRONG>availability + transit time</STRONG>. <P>For example, if Availability is 2-3 business days and 3 Day Select shipping method is chosen you can expect delivery within 5-6 business days (availability of 2-3 business days plus shipping time of 3 business days.)  If Next Day Air shipping method is chosen you can expect delivery within 3-4 business days (availability of 2-3 business days plus shipping time of 1 day). <P>We ship all items as quickly as possible and give special priority to expedited shipping methods.  Most orders with Availability of 2-3 business days or less actually ship same or next business day.  It is therefore possible, even likely, that your product will ship quicker than the listed Availability timeframe.  However, you should not expect, or depend on, shipment quicker than indicated by the Availability guideline.  This means that unless item Availability is \"Usually ships within 24 hours\" your order might not arrive next day or in two days even if Next Day Air or 2nd Day Air shipping is selected.<P>Although concepts of availability, shipping time, and delivery time are used by majority of online stores they are confusing for many internet shoppers.  If you have any additional questions please do not hesitate to <A HREF=\"mailto:sales@calculatorsource.com\">contact us</A>.<A NAME=\"preorder\"><H2 CLASS=\"button-color\">Pre-orders</H2></A>If Availability is \"Pre-order\" then we are accepting orders for the item even though it isn't yet available.  Pre-orders are shipped once we actually receive the product.  Actual release dates often change for new products and high demand can sometimes cause new items to be difficult to find shortly after release.  By pre-ordering you reserve a place in line for when product actually arrives thereby freeing yourself from worrying about changes in release dates, assuring yourself of fastest possible delivery, and greatly reducing the chance of finding the product out of stock.  Pre-orders are shipped in order received.<A NAME=\"download\"><H2 CLASS=\"button-color\">Electronic Downloads</H2></A>If Availability is \"Electronic Download\" then this item is available for download immediately after order; we do not physically ship the product.  Instructions for downloading your software are included on the Order Confirmation page which is displayed after you submit your order.<A NAME=\"notavail\"><H2 CLASS=\"button-color\">Not Available</H2></A>If Availability is \"Not available\" then we do not have the item in stock and  are not currently accepting orders for this product.  There is no 'Add to Cart' button for unavailable items.  Items are typically marked \"Not available\" only when we do not know when, or if, we'll be receiving additional product or when we do not anticipate having the product in stock in the foreseable future.  If you require an unavailable product you are welcome to contact us and we will notify you if it becomes available.  Note that you might not receive a response until/unless item is actually available.  All information we have on unavailable items can be be found on item pages.";
			break;
		default:
			helpTitle = "Unknown";
			helpText = "";
			break;
	}

	CreateWindow(helpTitle + ' Help', helpTitle, helpText);
}


function DisplayException(exceptionType, exceptionCode) {
	//exceptions are stored in the byte array of the exception code
	//TODO: For now just process few exceptions without generic byte array manipulation

	var exceptionText;
	var exceptionTitle;
	var indexText;

	exceptionText = "<P>Due to unusual physical characteristics and/or limitations imposed on us, special conditions apply to the purchase of this item.  Specific deviations from <A HREF=\"http://www.calculatorsource.com/info.html\" TARGET=\"info\">our regular policies</A> are detailed on this page.  We work hard to keep exceptions to a minimum and understand that they can be confusing.  Please <A HREF=\"http://www.calculatorsource.com/help.html#contact\" TARGET=\"info\">contact us</A> with any questions prior to ordering."

	indexText = "<SMALL>";

	switch(exceptionType) {
		case "ship":
			exceptionTitle = "Shipping";
			if (exceptionCode == 1) {
				indexText = indexText + "<A HREF=\"#s1\">Additional Charge for Non-Ground Shipping</A><BR>";

				exceptionText = exceptionText + "<A NAME=\"s1\"><H2 CLASS=\"button-color\">Additional Charge for Non-Ground Shipping</H2></A><P>Although <B>this item will ship at our standard rate via Ground</B> within the continental United States, due to this item's size, an additional charge will be required for all non-standard shipments. This includes: <UL><LI>3 Day Select, 2nd Day Air, Next Day Air, and other expedited shipments.<LI>Shipments to locations outside of the United States.<LI>Shipments to Alaska, Hawaii, Puerto Rico, and U.S. Protectorates.<LI>Shipments to APO/FPO addresses.<LI>Shipments to P.O. Boxes and other addresses that can only be delivered to by the U.S. Postal Service (we may be able to ship these orders via Parcel Post without a surcharge, but on most occasions this will not be possible.)<LI>Any other shipment that cannot be serviced by UPS Ground or equivalent.</UL>We will contact you with a shipping quote upon receipt of your order. If your order is likely to be affected by this we encourage you to include instructions in the comments field on the order form (e.g. do not confirm quote, confirm only if cost is above $xx.xx, etc.) to expedite processing of your order.";
			}

			break;

		case "return":
			exceptionTitle = "Return";
			if ((exceptionCode == 1) || (exceptionCode == 3)) {
				indexText = indexText + "<A HREF=\"#r1\">Returns Subject to a Restocking Fee</A><BR>";

				exceptionText = exceptionText + "<A NAME=\"r1\"><H2 CLASS=\"button-color\">Returns Subject to a Restocking Fee</H2></A><P>This item may be returned per our <A HREF=\"http://www.calculatorsource.com/info.html#return\" TARGET=\"info\">Return Policy</A> with the following modifications:<UL><LI>Unopened - 15% restocking fee will apply.<LI>Opened - 25% restocking fee will apply.";

				if (exceptionCode == 1) {
					exceptionText = exceptionText + "<LI>Defective Returns - 15% restocking fee will apply.<LI>Defective Exchanges - Customer pays return freight; we pay for shipment of the replacement item.";
				}

				exceptionText = exceptionText + "</UL>";
			}
			if ((exceptionCode == 2) || (exceptionCode == 3)) {
				indexText = indexText + "<A HREF=\"#r2\">Defective Items Must Be Serviced by the Manufacturer</A><BR>";

				exceptionText = exceptionText + "<A NAME=\"r2\"><H2 CLASS=\"button-color\">Defective Items Must Be Serviced by the Manufacturer</H2></A><P>Due to manufacturer requirements we are unable to exchange this item if it proves to be defective.  The item will be repaired or exchanged by the manufacturer warranty program.  Please contact the manufacturer for details about their program and any costs that may be involved.";
			}
			if ((exceptionCode == 4) || (exceptionCode == 5) || (exceptionCode == 6) || (exceptionCode == 7)) {
				indexText = indexText + "<A HREF=\"#r4\">This Product Cannot Be Returned</A><BR>";

				exceptionText = exceptionText + "<A NAME=\"r4\"><H2 CLASS=\"button-color\">This Product Cannot Be Returned</H2></A><P>Due to the nature of our agreement with the manufacturer this product cannot be returned once purchased.";
			}

			break;

		default:
			exceptionText = "Unknown";
			exceptionTitle = "";
			break;
	}

	indexText = indexText + "</SMALL>";

	exceptionText = indexText + exceptionText;

	CreateWindow('Item Specific ' + exceptionTitle + ' Policy Addendum', 'Item Specific ' + exceptionTitle + ' Policy Addendum', exceptionText);
}

function CreateWindow (titleText, headingText, bodyText) {
	strHTML='<HTML>\n';
	strHTML+='<HEAD>\n';
	strHTML+='<TITLE>CalculatorSource - ' + titleText + '</TITLE>\n';
	strHTML+='<link rel=\"stylesheet\" type=\"text/css\" href=\"/lib/calculatorsource/genstyle.css\">\n';
	strHTML+='</HEAD>\n';
	strHTML+='<BODY>\n';

	strHTML+='<H1 CLASS=\"button-color\">' + headingText + '</H1>\n';
	strHTML+='<P>' + bodyText + '\n';

	strHTML+='</BODY>\n';
	strHTML+='</HTML>';

	newWin=window.open('','helpWin','scrollbars=yes,resizable=yes,left=10,top=10,width=350,height=370');
	newWin.focus();
	newDoc=newWin.document;
	newDoc.write(strHTML);
	newDoc.close();
}

function newwindowfull (link, name) {

var options = 'toolbar=no,resizable=yes,scrollbars=yes,width=400,height=400';

open(link, name, options);

}

function WritePdaText(type) {
	var pdaText;
	var palm;
	var pocket;	
	var palmText;
	var pocketText

	pdaText = "";
	palm = false;
	pocket = false;

	palmText = "<A HREF=\"http://www.amazon.com/exec/obidos/external-search?tag=calculator-20&keyword=palm+pilot&mode=electronics\" TARGET=\"_blank\">Palm Pilots</A>";
	pocketText = "<A HREF=\"http://www.amazon.com/exec/obidos/external-search?tag=calculator-20&keyword=pocket+pc&mode=electronics\" TARGET=\"_blank\">Pocket PCs</A>";

	switch(type) {
		case "isbundle":
			//DISCONTINUED
			//pda product offered in an Infinity Softworks hardware/software bundle
			pdaText = "Save time with your choice of selected <A HREF=\"http://www.infinitysw.com/products/bundles.html\" TARGET=\"_blank\">devices with this calculator already installed</A> or b";
			//both Palm and Pocket PC are supported
			palm = true;
			pocket = true;
			break;
		case "palm":
			//palm only device
			palm = true;
			break;
		case "pocket":
			//pocket only device
			pocket = true;
			break;
		case "palm-pocket":
			//palm and pocket device
			palm = true;
			pocket = true;
			break;
	}

	if (palm || pocket) {
		pdaText = pdaText + "Shop an extensive selection of  ";

		if (palm && pocket) {
			pdaText = pdaText + palmText + " and " + pocketText;
		} else {
			if (palm) {
				pdaText = pdaText + palmText;
			} else {
				pdaText = pdaText + pocketText;
			}
		}

		pdaText = pdaText +  " at incredible prices to find the perfect device for you!";

		pdaText = "<P><B>Don't have a PDA yet?</B>  " + pdaText;

		document.write(pdaText);
	}
}

function OpenDemoWindow (demoURL) {
	var left = (screen.width/2) - 375;
	var top = (screen.height/2) - 286;

	demoWin = window.open(demoURL, 'demoWin', 'toolbar=no,location=no,directories=no,status=no,menub ar=no,scrollbar=no,resizable=no,copyhistory=yes,width=750,height=575,left='+left+', top='+top+',screenX='+left+',screenY='+top+'');
}