function getCookie(name) { // use: getCookie("name");
   var re = new RegExp(name + "=([^;]+)");
   var value = re.exec(document.cookie);
   return (value != null) ? unescape(value[1]) : "";
}

var today = new Date();
var expiry = new Date(today.getTime() + 365 * 5 * 24 * 60 * 60 * 1000);

function setCookie(name, value) { // use: setCookie("name", value);
   document.cookie=name + "=" + escape(value) + "; expires=" + expiry.toGMTString() + "; domain: .wordsmith.org; path=/";
}

function validate()
{
   document.redirect_form.submit();
   return true;
}

function focusElement(formName, elemName) {
	var elem = document.forms[formName].elements[elemName];
	elem.focus();
	//elem.select();
}

function valid_realname(aName)
{
	// convert initials to uppercase
   var a = aName.value.split(/\s+/g); // split the name into an array of words
   for (i = 0 ; i < a.length ; i++ ) {
      a[i] = a[i].substr(0, 1).toUpperCase() + a[i].substring(1, a[i].length);
   }
	aName.value = a.join(' '); // put it back together

	if ((aName.value == "") || (aName.value.indexOf(' ') == -1)){ // first condition makes sure it isn't blank, second makes sure full name is entered 
		alert("Please enter full name.");
		setTimeout("focusElement('" + aName.form.name + "', '" + aName.name + "')", 0); // http://answers.oreilly.com/topic/131-how-to-auto-focus-an-invalid-text-field-entry-with-javascript/
		///aName.focus();
		return (false);
	}
	return (true);
}

function valid_username(anEmail)
{
	anEmail.value = anEmail.value.replace(/\s+/g,"");
	//var emailPattern = /^[(a-z0-9._\%\+'\/&=-])+@[A-Z0-9.-]+\.[A-Z]{2,6}$/i;
	  var emailPattern = /^([a-z0-9_\-\.\%\+\&\=\/])+\@([a-z0-9_\-\.])+\.([a-z]{2,6})$/i;

	if (! emailPattern.test(anEmail.value)){
		alert("Invalid email address: " + anEmail.value);
		setTimeout("focusElement('" + anEmail.form.name + "', '" + anEmail.name + "')", 0); // http://answers.oreilly.com/topic/131-how-to-auto-focus-an-invalid-text-field-entry-with-javascript/
		//anEmail.focus();
		return (false);
	}
	return true;
}

function clearDefault(o, val)
{
	if (o.value == val)
	{
		o.value = "";
	}
}

function displayDefault(o, val)
{
	if (o.value == "")
	{
		o.value = val;
	}
}

function showAd(count) {
	subad  = '<form method="post" action=/awad/subscribe.cgi>';
	subad += '<b>Sign up for A.Word.A.Day</b><br><br>';
	subad += '<table border="0">';
	subad += '<tr><td>Name</td><td><INPUT TYPE="text" name="realname" SIZE="15"></td></tr>';
	subad += '<tr><td>Email</td><td><INPUT TYPE="text" name="username" SIZE="15"></td></tr>';
	subad += '<tr><td> </td><td><INPUT TYPE="submit" VALUE="Subscribe"></FORM></td></tr>';
	subad += '<tr><td colspan="2"></td> </tr>';
	subad += '<tr><td colspan="2"><tt>"The most welcomed, most enduring piece of daily mass e-mail in cyberspace."<br><b>-The New York Times<';
	subad += '/b></tt></td></tr>';
	subad += '</table>';
	subad += '</form>';

	ad = new Array(3);

	ad[0]  = '<a href="/awad/gift.html">Gift Subscription</a><br><br>';
	ad[0] += 'Share the joy of words with a friend: send a gift subscription of A.Word.A.Day.';

	ad[1]  = '<a href="/awad/add.html">A.Word.A.Day on Your Site</a><br><br>';
	ad[1] += 'With just a single line of HTML code, you can have the daily word appear on your Web site.';

	ad[2]  = '<a href="/anagram/display.html">Topical Anagrams on Your Site</a><br><br>';
	ad[2] += 'Display daily animated anagrams on your Web site.';

	index = Math.floor(Math.random() * ad.length);
	document.write(ad[index]);
}

function showBookAd() {
	ad = new Array(4);

	ad[0]  = '<a href="http://amazon.com/o/asin/0471718459/ws00-20"><img src="/awad/images/another.jpg" class="centered" border="0" height="150" width="100" hspace="0"></a><br>';
	ad[0] += '<a href="/awad/book2.html">Another&nbsp;Word&nbsp;A&nbsp;Day</a>: An All-new Romp through Some of the Most Unusual and Intriguing Words in English (John Wiley & Sons)<br><br>';
	ad[0] += 'A book by Anu Garg, the founder of Wordsmith.org.<br><br>';
	ad[0] += '"Just the thing." -Washington Post<br><br>';
	ad[0] += 'Find it at <a href="http://amazon.com/o/asin/0471718459/ws00-20">Amazon.com</a> or a <a href="/awad/book2.html">bookstore</a> in your country.';

	ad[1]  = '<a href="http://amazon.com/o/asin/0471230324/ws00-20"><img src="/awad/images/bookcover.jpg" class="centered" border="0" height="153" width="99" hspace="0"></a><br>';
	ad[1] += '<a href="/awad/book.html">A&nbsp;Word&nbsp;A&nbsp;Day</a>: A Romp through Some of the Most Unusual and Intriguing Words in English (John Wiley & Sons)<br><br>';
	ad[1] += 'A bestseller by Anu Garg, the founder of Wordsmith.org.<br><br>';
	ad[1] += '"A delightful, quirky collection." -New York Times<br><br>';
	ad[1] += 'Find it at <a href="http://amazon.com/o/asin/0471230324/ws00-20">Amazon.com</a> or a <a href="/awad/book.html">book shop</a> in your country.';

	ad[2]  = '<a href="http://amazon.com/o/asin/0452288614/ws00-20"><img src="/awad/images/book3.jpg" class="centered" border="0" height="153" width="100" hspace="0"></a><br>';
	ad[2] += '<a href="http://amazon.com/o/asin/0452288614/ws00-20">The Dord, the Diglot, and an Avocado or Two: </a>';
	ad[2] += 'The Hidden Lives and Strange Origins of Common and Not-So-Common Words (Penguin Books)<br><br>';
	ad[2] += 'A new book by Anu Garg, the founder of Wordsmith.org.<br><br>';
	ad[2] += 'Get your copy today at <a href="http://amazon.com/o/asin/0452288614/ws00-20">Amazon.com</a> or a <a href="/awad/book3.html">bookstore</a> in your country.';

	// This is duplicate just to make this ad appear more often
	ad[3]  = '<a href="http://amazon.com/o/asin/0452288614/ws00-20"><img src="/awad/images/book3.jpg" class="centered" border="0" height="153" width="100" hspace="0"></a><br>';
	ad[3] += '<a href="http://amazon.com/o/asin/0452288614/ws00-20">The Dord, the Diglot, and an Avocado or Two: </a>';
	ad[3] += 'The Hidden Lives and Strange Origins of Common and Not-So-Common Words (Plume)<br><br>';
	ad[3] += 'A new book by Anu Garg, the founder of Wordsmith.org.<br><br>';
	ad[3] += 'Get your copy today at <a href="http://amazon.com/o/asin/0452288614/ws00-20">Amazon.com</a> or a <a href="/awad/book3.html">bookstore</a> in your country.';

	index = Math.floor(Math.random() * ad.length);
	document.write(ad[index]);
}

function showBookAd_banner() {
	ad = new Array(4);

	ad[0]  = '<a href="http://amazon.com/o/asin/0471718459/ws00-20"><img src="/awad/images/another.jpg" class="centered" border="0" height="150" width="100" hspace="5"></a><br>';
	ad[0] += '<a href="/awad/book2.html">Another&nbsp;Word&nbsp;A&nbsp;Day</a>: An All-new Romp through Some of the Most Unusual and Intriguing Words in English (John Wiley & Sons)<br><br>';
	ad[0] += 'A book by Anu Garg, the founder of Wordsmith.org.<br><br>';
	ad[0] += '"Just the thing." -Washington Post<br><br>';
	ad[0] += 'Find it at <a href="http://amazon.com/o/asin/0471718459/ws00-20">Amazon.com</a> or a <a href="/awad/book2.html">bookstore</a> in your country.<br>';

	ad[1]  = '<a href="http://amazon.com/o/asin/0471230324/ws00-20"><img src="/awad/images/bookcover.jpg" class="centered" border="0" height="153" width="99" hspace="5"></a><br>';
	ad[1] += '<a href="/awad/book.html">A&nbsp;Word&nbsp;A&nbsp;Day</a>: A Romp through Some of the Most Unusual and Intriguing Words in English (John Wiley & Sons)<br><br>';
	ad[1] += 'A bestseller by Anu Garg, the founder of Wordsmith.org.<br><br>';
	ad[1] += '"A delightful, quirky collection." -New York Times<br><br>';
	ad[1] += 'Find it at <a href="http://amazon.com/o/asin/0471230324/ws00-20">Amazon.com</a> or a <a href="/awad/book.html">book shop</a> in your country.<br>';

	ad[2]  = '<a href="http://amazon.com/o/asin/0452288614/ws00-20"><img src="/awad/images/book3.jpg" class="centered" border="0" height="153" width="100" hspace="5"></a><br>';
	ad[2] += '<a href="http://amazon.com/o/asin/0452288614/ws00-20">The Dord, the Diglot, and an Avocado or Two: </a><br>';
	ad[2] += 'The Hidden Lives and Strange Origins of Common and Not-So-Common Words (Penguin Books)<br><br>';
	ad[2] += 'A new book by Anu Garg, the founder of Wordsmith.org.<br><br>';
	ad[2] += 'Get your copy today at <a href="http://amazon.com/o/asin/0452288614/ws00-20">Amazon.com</a> or a <a href="/awad/book3.html">bookstore</a> in your country.<br><br>';

	// This is duplicate just to make this ad appear more often
	ad[3]  = '<a href="http://amazon.com/o/asin/0452288614/ws00-20"><img src="/awad/images/book3.jpg" class="centered" border="0" height="153" width="100" hspace="5"></a><br>';
	ad[3] += '<a href="http://amazon.com/o/asin/0452288614/ws00-20">The Dord, the Diglot, and an Avocado or Two: </a><br>';
	ad[3] += 'The Hidden Lives and Strange Origins of Common and Not-So-Common Words (Plume)<br><br>';
	ad[3] += 'A new book by Anu Garg, the founder of Wordsmith.org.<br><br>';
	ad[3] += 'Get your copy today at <a href="http://amazon.com/o/asin/0452288614/ws00-20">Amazon.com</a> or a <a href="/awad/book3.html">bookstore</a> in your country.<br><br>';

	index = Math.floor(Math.random() * ad.length);
	// document.write('<p style="border: solid thin; background-color: #FFFFFF; border-color: #99CCFF; border-style: solid;text-align: left;padding: 5px;">');
	document.write(ad[index]);
	// document.write('</p>');
}

