//	script_common.js contains JavaScript functions used by the TomLewis.net site.
//	****************************************
//	globally establish the calling document's lastModified date
	var lm = new Date(document.lastModified);
	var ludate = formatDate(lm);
//	global variables to support on-line shopping
	var currency; // will be set to CAD, USD, etc.
//	****************************************
function setCurrency(choice) {
	if (choice.value == "CAD") {currency = "CAD"} else {currency = "USD"};
	var today = new Date();
	var expiry = new Date();
	expiry.setTime(today.getTime() + (1000*60*60*24*90));	/* 90 days hence */
	setCookie("currency", currency, expiry, "/");
	alert("Thank you.\nWe have saved your currency choice as " + currency + ".");
	return;
}
//
//	"getCookieVal" retrieves a specific value from a cookie
function getCookieVal(offset) {
	var endstr = document.cookie.indexOf (";", offset);
	if ( endstr == -1 )
		endstr = document.cookie.length;
	return(unescape(document.cookie.substring(offset, endstr)));
}
//
//	"getCookie" retrieves a cookie from users browser
function getCookie(name) {
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while ( i < clen ) {
		var j = i + alen;
		if ( document.cookie.substring(i, j) == arg ) return(getCookieVal (j));
		i = document.cookie.indexOf(" ", i) + 1;
		if ( i == 0 ) break;
	}
	return(null);
}
//
//	"setCookie" stores a cookie in users browser
function setCookie(name,value,expires,path,domain,secure) {
	document.cookie = name + "=" + escape (value) +
									((expires) ? "; expires=" + expires.toGMTString() : "") +
									((path) ? "; path=" + path : "") +
									((domain) ? "; domain=" + domain : "") +
									((secure) ? "; secure" : "");
}
//
//	"deleteCookie" removes a cookie from users browser by setting an expiry date in the past
function deleteCookie(name,path,domain) {
	if ( GetCookie(name) ) {
		document.cookie = name + "=" +
										((path) ? "; path=" + path : "") +
										((domain) ? "; domain=" + domain : "") +
										"; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}
//
//	"getContinueShoppingURL" sets the Continue Shopping URL for the PayPal shopping cart to the current page
function getContinueShoppingURL(form){
	form.shopping_url.value = window.location.href;
}
//
//	"setReturns" parses the root directory for the current page (next_version, or not)
//	Used to construct PayPal "return" and "cancel_return" URLs
//	DGS 2009-12-15
function setReturns(form) {
	var folder = location.href.substring(0,location.href.lastIndexOf("/")+1);
	form.element("return").value = folder + "paypal_return.htm";
	form.cancel_return.value = folder + "paypal_cancel.htm";
}

//
//	"viewCart" generates (therefore slightly more secure) the code to display the PayPal shopping cart
function viewCart() {
	var view = '';
	view += '<form target="paypal" name="PPview1" action="https://www.paypal.com/cgi-bin/webscr" method="post">';
	view += '<input type="hidden" name="business" value="tom@tomlewis.net" />';
	view += '<input type="hidden" name="cmd" value="_cart" />';
	view += '<input type="hidden" name="display" value="1" />';
	view += '<input name="submit" type="image" src="images/button-viewcart.gif" alt="PayPal - The safer, easier way to pay online" width="88" height="18" border="0" />';
	view += '</form>';
	document.write(view);
}
//
//	"viewCart2" generates (therefore slightly more secure) the code to display the PayPal shopping cart
//	from the menu on the "lyrics" pages. The only difference is the address of the button image.
function viewCart2() {
	var view = '';
	view += '<form target="paypal" name="PPview2" action="https://www.paypal.com/cgi-bin/webscr" method="post">';
	view += '<input type="hidden" name="business" value="tom@tomlewis.net" />';
	view += '<input type="hidden" name="cmd" value="_cart" />';
	view += '<input type="hidden" name="display" value="1" />';
	view += '<input name="submit" type="image" src="../images/button-viewcart.gif" alt="PayPal - The safer, easier way to pay online" width="88" height="18" border="0" />';
	view += '</form>';
	document.write(view);
}
//
//	"addtoCart" generates (therefore slightly more secure) the code to add an item to the PayPal shopping cart
//	It expects an item identifier, description, weight in Kg, price in a specified currency
function addtoCart(medium, item_id, desc, mass, priceUSD, priceCAD) {
	var intro_line = '';
	var button_line = '';
	var add = '';
	var chosencurrency = getCookie("currency");
	var chosenprice = (chosencurrency == "USD")?priceUSD:priceCAD;
	if (chosencurrency == null) {
		intro_line += '<br /><strong>Purchase the ' + medium + ' <em>' + desc + '</em>.</strong><br /><a href="choose_currency.htm">Click here to choose between $' + priceUSD + ' USD and $' + priceCAD + ' CAD.</a>';
		button_line += '';
	} else {
		intro_line += '<br /><strong>Purchase the ' + medium + ' <em>' + desc + '</em></strong> for $' + chosenprice + ' ' + chosencurrency + ' [<a href="choose_currency.htm">Change currency.</a>]';
		button_line += '<input name="submit" type="image" src="images/button-addtocart.gif" alt="PayPal - The safer, easier way to pay online" width="88" height="18" border="0" onclick="getContinueShoppingURL(this.form); setReturns(this.form);" />';
		var today = new Date();
		var expiry = new Date();
		expiry.setTime(today.getTime() + (1000*60*60*24*90));	/* refresh cookie, always 90 days hence */
		setCookie("currency", chosencurrency, expiry, "/");
	}
	add += intro_line;
	add += '<form target="paypal" name="PPadd" action="https://www.paypal.com/cgi-bin/webscr" method="post">';
	add += '<input type="hidden" name="business" value="tom@tomlewis.net" />';
	add += '<input type="hidden" name="cmd" value="_cart" />';
	add += '<input type="hidden" name="add" value="1" />';
	add += '<input type="hidden" name="no_shipping" value="2" />';
	add += '<input type="hidden" name="item_number" value="' + item_id + '" />';
	add += '<input type="hidden" name="item_name" value="' + desc + '" />';
	add += '<input type="hidden" name="weight" value="' + mass + '" />';
	add += '<input type="hidden" name="weight_unit" value="kgs" />';
	add += '<input type="hidden" name="amount" value="' + chosenprice + '" />';
	add += '<input type="hidden" name="currency_code" value="' + chosencurrency + '" />';
	add += '<input type="hidden" name="shopping_url" value="http://tomlewis.net/" />';
	add += '<input type="hidden" name="return" value="http://tomlewis.net/" />';
	add += '<input type="hidden" name="cbt" value="Return to the Tom Lewis web site." />';
	add += '<input type="hidden" name="cancel_return" value="http://tomlewis.net/" />';
	add += button_line;
	add += '</form>';
	document.write(add);
}
//
//	****************************************
//	"pageFooter" displays a bottom of page message:
//			- request for feedback to the webmaster
//			- Copyright information.
//		If the "updated" parameter is passed,
//			it will also output a standard string
//			of last-modified date and time.
//	Syntax: pageFooter("updated");
//		by David Shapiro 2004-09-08 17:37:30
//		added webmaster@tomlewis.net 2007-09-26 16:03:20
function pageFooter(updated){
	var hash = document.location.href.indexOf("#");
	var wholename = (hash > -1)?document.location.href.substring(0,hash):document.location.href
	var filename = document.location.pathname;
	var copyyear = ludate.substring(0,4);
	var footer = "";
	/*footer += '<table width="100%" border="0" cellspacing="0" cellpadding="3" bgcolor="#E0FFFF"><tr><td class="footer">';*/
	footer += 'The URL of this page is ';
	footer += wholename;
	footer += '<br />Please report any problems with this page to the <a href="mailto:webmaster@tomlewis.net?subject=Problem report for TomLewis.net page '+filename+'&nbsp;'+ludate+'">webmaster</a>.';
	footer += '<br />Copyright &copy;&nbsp;'+copyyear+'&nbsp;Self-Propelled Music.';
	footer += '&nbsp;All rights reserved.';
	if (updated == 'updated') {
		footer += '&nbsp;<i>Last updated&nbsp;:&nbsp;'+ludate+"</i>";
	}
	/*footer += '</td></tr></table>';*/
	document.write(footer);
}
// ****************************************
//	"formatDate" takes a date object and formats it as yyyy-mm-dd hh:mm:ss
//	Syntax: formatDate(date_object);
//		script by Bernhard Friedrich; should work in all browsers
//		adapted by David Shapiro 2000-02-13 10:28:45
function formatDate(a){
	year=a.getYear();year=((year<1000)?((year<70)?2000:1900):0)+year;
	month=a.getMonth()+1;month=((month<10)?'0':'')+month;
	day=a.getDate();day=((day<10)?'0':'')+day;
	hour=a.getHours();hour=((hour<10)?'0':'')+hour;
	minute=a.getMinutes();minute=((minute<10)?'0':'')+minute;
	second=a.getSeconds();second=((second<10)?'0':'')+second;
	var formatted_date = "";
	formatted_date += year+'-'+month+'-'+day+' '+hour+':'+minute+':'+second; 
	return formatted_date;
}
// ****************************************
//	Construct an e-mail call, not easily crawled by web spiders
//	usage: contact('left','right','tld','subj','link')
function contact(left,right,tld,subj,link) {
	text=(link)?link:left+'&#64;'+right+'.'+tld;
	var line='<a href="mailto:'+left+'&#64;'+right+'.'+tld+'?subject='+subj+'">'+text+'<\/a>';
	document.write(line);
}

