/* 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) % 15 + 1; // quotes are numbered 1 to 15

	var quote, who1, who2, link, page = 1;
	if (num == 1)
	{
		quote = "I can not think of any other program that is so simple to use and fairly self-explanatory ... this wonderful program saved the organization and me at tax time last year.";
		who1 = "Ruby Black"
		who2 = "Executive Director"
		link = "ruby";
	}
	else if (num == 2)
	{
		quote = "Thanks for your product Dan. I appreciate the easy learning curve of your product. I was able to use it within 5 minutes of downloading it."
		who1 = "Gloria Flora";
		who2 = "Executive Director";
		link = "gloria";
	}
	else if (num == 3) 
	{
		quote = "The version of DONATION I am now using as pledge secretary for my church is fantastic. It is well designed, easy to use with the user very definitely in mind, and extremely functional. In my 40 years as a computer programmer, I worked mostly on midsized computers with very stable operating systems. I have been so disillusioned by the PC software coming out of large organizations that it is extremely refreshing to work with software that actually </em>works<em> and is intuitive. Thank you.";
		who1 = "Cathy Washington"
		who2 = "";
		link = "cathyw";
	}
	else if (num == 4) 
	{
		quote = "Having no experience with spreadsheets, I would have been in serious trouble but for your software. Over the past three weeks, I have found that your program is easy to understand, easy to operate and extraordinarily useful in helping me to keep detailed track of the givings at our church."
		who1 = "Caroline Leishman";
		who2 = "Envelopes&nbsp;Secretary";
		link = "carolinel";
		page = 2;
	}
	else if (num == 5)
	{
		quote = "Although we are a <strong>very</strong> small church I have found your program to be an invaluable aid in keeping my records.<br><br>Before I got the program I was ready to give up my volunteer position as treasurer. Now I'm going to continue doing the job.";
		who1 = "Diana Morine";
		who2 = "Wolfville Ridge United Baptist Church"
		link = "dianam";
		page = 2;
	}
	else if (num == 6) 
	{
		quote = "Let me tell you how delighted I was with your program. I found it in an on-line search for an inexpensive (hopefully free) program to process the donations to our church. I WAS ABSOLUTELY AMAZED WITH EVERYTHING I WORKED WITH!!!! The range of information was incredible and very user friendly to learn and use! There was nothing I wanted that wasn't available.";
		who1 = "Carol Brown";
		who2 = "Past Treasurer";
		link = "carolb";
		page = 2;
	}
	else if (num == 7) 
	{
		quote = "Just wanted to pass on kudos for a job well done. I just finished using your program to generate receipts for last year's donations. I also downloaded the latest version and checked out some of the new features. Pretty much everything that I could want is in there.";
		who1 = "Lyndon R. Kok";
		who2 = "Collector of Funds";
		link = "lyndon";
		page = 2;
	}
	else if (num == 8)
	{
		quote ="I just had to send you a message to say &quot;THANK-YOU&quot; for creating such a terrific program to track donations! Throughout the years, our agency has had a very outdated, not-so-efficient method of maintaining donations. Things are so much better since we changed to your program!"
		who1 = "Allyson Scott";
		who2 = "";
		link = "ascott";
		page = 2;
	}
	else if (num == 9)
	{
		quote = "We are using DONATION and the user loves it because it is so easy and fun for her to use. I love it because it is pretty much maintenance free and since it is so easy to use and so well documented I don't have to deal with any questions from the user. Thanks again for creating software the way it should be - elegant yet simple and functional."
		who1 = "John R. Centofani";
		who2 = "";
		link = "johnc";
		page = 2;
	}
	else if (num == 10)
	{
		quote = "Being a non-profit on a tight budget, DONATION was an answer to prayers. After using it, I find it is probably better than any other program on the market."
		who1 = "Germaine R. Carter";
		who2 = "";
		link = "germainec";
	}
	else if (num == 11)
	{
		quote = "I was so pleased to find this software, I didn't know it existed, it is the most user friendly software I have ever used. Looking forward to using this software program for a long time. "
		who1 = "A Church Treasurer";
		who2 = "";
		link = "ctnb";
	}
	else if (num == 12)
	{
		quote = "Just wanted to share with you a wonderful resource ... If you're needing to capture, track, and provide end-of-year reciepts for contributions and giving, you will be amazed at the ease of this system."
		who1 = "Luis A. Perez"
		who2 = "The BridGe Network of House & Micro Churches"
		link = "BridGe"
	}
	else if (num == 13)
	{
		quote = "Your product just gets better and better.  It is so user friendly and how you have it set up just makes sense for the average user.  It’s simple enough yet detailed enough and you can customize it to fit your needs so easily."
		who1 = "Kathy Landwehr"
		who2 = "New Covenant Community Church"
		link = "landwehr"
	}
	else if (num == 14)
	{
		quote = "I really enjoy working with your software. It is truly remarkable. You seem to have anticipated what needs there are and have met them beautifully."
		who1 = "Cynthia Heidman"
		who2 = "Port Sydney Bible Chapel"
		link = "heidman"
	}
	else if (num == 15)
	{
		quote = "Your software has made my busy life as a teacher and mother so much easier.  I can volunteer my time to the church without it being a burden - thanks to this software."
		who1 = "Jodie McArthur"
		who2 = "Camilla-Laurel-Mono Centre Pastoral Charge"
		link = "mcarthur"
	}
	if (quote != null)
	{
		quote = "<em>" + quote + "</em><br /><br />" +
				"<span class=\"quoteAuthor\">- <a href=\"http://www.software4nonprofits.com/testimonials";
		if (page == 2) quote += "2";
		quote += ".htm#" + link + "\">" + who1 + "</a>";
		if (who2 > "")
			quote += "<br />" + who2;
		quote += "<br /><a href=\"http://www.software4nonprofits.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 null;
}

// extract the host part (e.g. www.software4nonprofits.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, "DonPopup", 
					"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;
	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 (search == "referred_by_techfinder")
			source = "TechSoupTechFinder";
		else if (source == "DiscountBible")
			reseller = "DiscountBible.com";
		else if (source == "ChurchPower")
			reseller = "ChurchPower.com";
		else if (source == "CAAMinistries")
			reseller = "CAA Ministries";
		if (reseller)
		{
			var page
			if (window.location.pathname == "/register.htm")
				page = "this"
			else
				page = "the REGISTER"

			alert(
			"Welcome to Software4Nonprofits.com / Cooperstock Software.\n\n" +
			"You are coming in from " + reseller + ", with which " +
			"Software4Nonprofits.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 || document.referrer == "")
			source = ""
		else
			source = hostFromUrl(document.referrer).toLowerCase();
		// ignore referrals from my own site(s); standardize other ones
		if (source.indexOf("software4nonprofits") >= 0 ||
			source.indexOf("freedonationsoftware") >= 0)
		{
			source = null;
		}
		else if (source.indexOf("techsoup") >= 0)
			source = "TechSoup";
		else if (source.indexOf("donarius") >= 0 || 
				 source.indexOf("nuverb") >= 0)
			source = "Donarius Comparison";
		else if (source.indexOf("nonprofitexpert") >= 0)
			source = "noprofitexpert.com";
		else if (source.indexOf("charityvillage") >= 0)
			source = "CharityVillage";
		else if (source.indexOf("google") >= 0)
			source = "Google";
		else if (source.indexOf("ccmag") >= 0)
			source = "CCMag";
		else if (source.indexOf("churchtreasurers.ca") >= 0)
			source = "Church Treasurers of Canada";
	}
	if (source)
	{
		// set the source cookie, to expire in 30 days
		var expires = new Date();
		expires.setTime(expires.getTime() + 30 * 24 * 60 * 60 * 1000);
		expires = expires.toUTCString();
		var cookie = 'source=' + source + '; path=/; expires=' + expires;
		document.cookie = cookie;
	}
}
