﻿/* eBay call back */
function _cb_FindItemsAdvanced (root){
var html = [];
var items = [];
var size = [];

/* set up table and table header */
	html.push ('<table>');
	html.push ('<tr><th align="center">');
	
	if (root.CorrelationID == 'pcgslistings') html.push ('eBay\'s Most Expensive Certified Coins')
	else html.push ('Item Title');

	html.push ('</th><th># Bids</th><th>Current Bid</th></tr>');

	if (root.CorrelationID == 'carlslistings') size = 'small_text';
	else size = 'xx_small';
	items = root.SearchResult[0].ItemArray.Item;

/* iterate through search results */
	for (var i = 0; (i < root.TotalItems) && (items[i] != null); i++) {
		var item = items[i];
		var title = item.Title;
		var viewitem = item.ViewItemURLForNaturalSearch;
		var bids = item.BidCount || 0;
		var p = item.ConvertedCurrentPrice.Value.toFixed(2);
		var price = "";

/* price has decimal point and 2 zeros, insert comma to separate thousands, assume no million buck results */
		if (p.length > 6) {
			price = p.substr (0, p.length - 6) + ',';
			price = price + p.substr (p.length - 6);
		}
		else price = p;

/* create and push the table row */
		html.push('<tr><td align="left" class=' + size + '><a href="' + viewitem + '">' + title + '</a></td>');
		html.push('<td align="center" class='+ size + '>' + bids + '</td>');
		if (bids > 0)
			html.push('<td align="right" class='+ size + '>' + '<b>$' + price + '</b></td></tr>');
		else
			html.push('<td align="right" class='+ size + '>' + '$' + price + '</td></tr>');
	}

/* table finished */	
	html.push('</table>');

/* report errors */
	if (root.Errors != null)
		html.push ('FindItemsAdvanced error: ' + root.Errors[0].LongMessage);

/* div ID used as CorrelationID, add results there */
	document.getElementById (root.CorrelationID).innerHTML = html.join("");
}
/* End eBay FindItemsAdvanced call back */

/* eBay call back */
function _cb_FindPopularItems (root){
var html = [];
var items = [];
var size = [];

/* set up table and table header */
	html.push('<table>');
	html.push('<tr><th align="center">eBay\'s Most Popular Coins</th><th># Bids</th><th>Current Bid</th></tr>');

	size = 'xx_small';
	items = root.ItemArray.Item;

/* iterate through search results */
	for (var i = 0; items[i] != null; i++) {
		var item = items[i];
		var title = item.Title;
		var viewitem = item.ViewItemURLForNaturalSearch;
		var bids = item.BidCount || 0;
		var p = item.ConvertedCurrentPrice.Value.toFixed(2);
		var price = "";

/* price has decimal point and 2 zeros, insert comma to separate thousands, assume no million buck results */
		if (p.length > 6) {
			price = p.substr (0, p.length - 6) + ',';
			price = price + p.substr (p.length - 6);
		}
		else price = p;

/* create and push the table row */
		html.push('<tr><td align="left" class=' + size + '><a href="' + viewitem + '">' + title + '</a></td>');
		html.push('<td align="center" class='+ size + '>' + bids + '</td>');
		if (bids > 0)
			html.push('<td align="right" class='+ size + '>' + '<b>$' + price + '</b></td></tr>');
		else
			html.push('<td align="right" class='+ size + '>' + '$' + price + '</td></tr>');
	}

/* table finished */	
	html.push('</table>');

/* report errors */
	if (root.Errors != null)
		html.push ('FindItemsAdvanced error: ' + root.Errors[0].LongMessage);

/* div ID used as CorrelationID, add results there */
	document.getElementById (root.CorrelationID).innerHTML = html.join("");
}
/* End eBay FindPopularItems call back */

/* Intrinsic value calculator */

function isPrice (elem, helperMsg) {
	if (!isFloat (elem.value)) {
		alert (helperMsg);
		elem.focus ();
		return false;
		}
	return true;
	}

function parsePrice (id, name, errMsg) {
	var elem;

	elem = document.getElementById (id);
	
	if (isEmpty (elem, errMsg)) return -1;
	if (!isPrice (elem, name + ' is expected to be dollars and cents.')) return -1;

	return parseFloat (elem.value);
}

function calculateSilverValue(){
var i, p, ps, f, fs, o, os, v, vs, ts;

	p = parsePrice ('price_id', 'The Silver Price', 'Please enter the price of silver in dollars and cents.');
	if (p < 0) return false;
	f = parsePrice ('face_id', 'The Total Face Value', 'Please enter the total face value of your silver coins.');
	if (f < 0) return false;

	for (i = 0; i < document.silverForm.silverType.length; i++)
		if (document.silverForm.silverType[i].checked) {
			if (document.silverForm.silverType[i].value == '90') {
				o = 0.72338;
				ts = "90% silver coins";
			} else if (document.silverForm.silverType[i].value == '40') {
				o = 0.29584;
				ts = "40% silver clad JFKs";
			} else if (document.silverForm.silverType[i].value == 'morgan') {
				o = 0.77344;
				ts = "Morgan and/or Peace dollars";
			} else {
				o = 1.1252;
				ts = "War Nickels";
			}
		}
		
		/* face value of hoard */
		fs = valueToDollarStr (f, true);
		
		/* ounces of silver */
		o = o * f;
		os = valueToStr (o, true);
		
		/* prettify price */
		ps = valueToDollarStr (p, true);
		
		/* value of hoard */
		v = o * p;
		ov = valueToDollarStr (v, true);

		document.getElementById ('total_value_id').innerHTML = 
			fs + ' in face value of ' + ts + ' contain ' + os + ' oz of silver which at ' + 
			ps + '/oz is worth ' + ov + '.';
}
