/* NOTE TO MYSELF: Copy this to both www and secure-www! */

function insertQuote(num)
{
	if (!num) return;

	if (!canWriteId())
	{
		return; // Netscape 4
	}

	// randomize the quote shown, but base it on the hour and a passed-in
	// number
	var hour = new Date().getHours();
	var num = (hour + num) % 13 + 1; // quotes are numbered 1 to 13

	var quote, who1, who2, link, page = 1;
	if (num == 1)
	{
		quote = "";
		who1 = "Name"
		who2 = "Position"
		link = "link";
		page = 1;
	}
	if (quote != null)
	{
		quote = "<em>" + quote + "</em><br /><br />" +
				"<span class=\"quoteAuthor\">- <a href=\"http://www.html-messagebox.com/testimonials";
		if (page == 2) quote += "2";
		quote += ".htm#" + link + "\">" + who1 + "</a>";
		if (who2 > "")
			quote += "<br />" + who2;
		quote += "<br /><a href=\"http://www.html-messagebox.com/testimonials.htm\">See all testimonials</a>";
		quote += "</span>"
		writeId(quote, "quote");
	}
}

function canWriteId()
{
	return document.getElementById || document.all;
}

function writeId(text, id)
{
	var x = null;
	if (document.getElementById)
	{
		x = document.getElementById(id);
		x.innerHTML = ''; // fixes IE Mac bug
	}
	else if (document.all)
	{
		x = document.all[id];
	}
	if (x != null)
	{
		/*
		x.style.padding = "3px";
		x.style.border = "1px solid #999999";
		x.style.marginLeft = "40px";
		x.style.marginRight = "40px";
		*/
		x.innerHTML = text;
	}
}

function getSpecificCookie(name)
{
	var cookie = document.cookie;
	var clen = cookie.length;
	var i = 0;
	name += "=";
	while (i < clen)
	{
		var start = i + name.length;
		if (cookie.substring(i, start) == name)
		{
			var end = cookie.indexOf(";", start);
			if (end == -1)
				end = clen;
			return cookie.substring(start, end);
		}
		i = cookie.indexOf(" ", i) + 1;
		if (i == 0) break;
	}
	return "";
}

function setSpecificCookie(name, value, expiryDays)
{
	// set the cookie, to expire in expiryDays days
	var expires = new Date();
	expires.setTime(expires.getTime() + expiryDays * 24 * 60 * 60 * 1000);
	expires = expires.toUTCString();
	var cookie = name + '=' + value + '; path=/; expires=' + expires;
	document.cookie = cookie;
}
// extract the host part (e.g. www.html-messagebox.com) from 
// a URL (e.g. a referrer)
function hostFromUrl(data) {
    var e=/^((http|https):\/)?\/?([^:\/\s\?]+).*/;

    if (data.match(e)) {
		return RegExp.$3;
    }
    else {
        return  "";
    }
}

function openpopup(popurl) {
	var winpops = window.open(popurl, "HmbPopup", 
					"width=800px,height=550px,toolbar,location,status,scrollbars,menubar,resizable");
	winpops.focus()
}

// trim leading and trailing spaces from a value, and return it
function trimSpaces(val) {
	while (val.charAt(0) == " ")
		val = val.substring(1);
	while (val.charAt(val.length - 1) == " ")
		val = val.substring(0, val.length - 1);
	return val;
}

// Trim a form field, put the value back into the field if it changed, 
// and return the field's value.
function trimField(field)
{
	var val = field.value;
	if (val == null) val = ""
	var val2 = trimSpaces(val);
	if (val2 != val)
		field.value = val2;
	return val2;
}

// return whether an email address is valid (true for valid, false for invalid)
function validateEmail(val)
{
	if (window.RegExp) // test for whether Regular Expressions supported
	{
		var regstr =
			"^[a-zA-Z0-9_\\-\\.]+\\@([a-zA-Z0-9\\-]+\\.){1,4}[a-zA-Z]{2,6}$"
		var reg = new RegExp(regstr)
		if (reg.test(val))
			return true;
	}
	else // no regular expressions, use simple string tests
	{
		// only valid if in form foo@bar.baz, where each element is at
		// least 1 char long, and baz is at least 2 chars long
		p=val.indexOf('@')
		if (p >= 1 && p < val.length - 1)
		{
			q = val.indexOf('.', p + 2)
			if (q >= p + 2 && q < val.length - 2)
			{
				q = val.indexOf('@', p + 1) // no 2nd @
				if (q < 1)
					return true;
			}
		}
	}
	return false;
}

function parseSearch()
{
	var search = window.location.search;
	var source = null;
	var reseller = null;
	if (search.length > 1)
	{
		search = search.substr(1);
		var arr = search.split("&");
		for (var i = 0; i < arr.length; ++i)
		{
			if (arr[i].substr(0, 7) == "source=")
			{
				source = arr[i].substr(7);
				break;
			}
		}
		if (reseller)
		{
			var page
			if (window.location.pathname == "/register.htm")
				page = "this"
			else
				page = "the REGISTER"

			alert(
			"Welcome to HTML-MessageBox.com / Cooperstock Software.\n\n" +
			"You are coming in from " + reseller + ", with which " +
			"HTML-MessageBox.com has a resale agreement.\n\n" +
			"To be sure we can track your evaluation or purchase, PLEASE " + 
			"fill in the Registration form on " + page + " page if you " +
			"decide to download the evaluation version of the program. " +
			"You get that evaluation version from the DOWNLOAD page.\n\n" +
			"Doing that will allow us to connect your payment, if you " +
			"eventually decide to purchase the program, to the fact that " +
			"you came to this website from " + reseller + ".")
		}
	}
	if (!source && !getSpecificCookie("source"))
	{
		if (document.referrer == null)
			source = ""
		else
			source = hostFromUrl(document.referrer).toLowerCase();
		// ignore referrals from my own site(s); standardize other ones
		if (source.indexOf("html-messagebox") >= 0 ||
			source.indexOf("htmlmessagebox") >= 0)
		{
			source = null;
		}
	}
	if (source)
	{
		// set the source cookie, to expire in 30 days
		setSpecificCookie("source", source, 30)
	}
}
