//--Copyright (c) 1999-2002 by A Plus Consultants, L.L.C.
//--No part of this script may be used for any purpose without prior
//--written permission. Contact richard@aplusconsultants.com.
/*
	JavaScript Functions:
	These functions perform generic functions that are usable in a variety
	of situations.

	This is a list of functions contained in this script:

	aBreakApart(sString, sDelim)
	ClearValues(oElement, sList)
	LTrim(str)
	RTrim(str)
	sTimeToFloat(sTime)
	sWhoseThere(bHex, sTo, sSubject, sBody, sBCC, sTokens)
	CreateMsg(sSuggested)
*/

function aBreakApart(sString, sDelim) {
/*
	Splits a string into an array, similar to string.split. There
	appears to be a bug in string.split() in Netscape version 4.04
	which this function bypasses.
*/
	var aArray = new Array();
	var iPos = -1;
	var sStr = sString;

	while (sStr.indexOf(sDelim) >= 0) {
		iPos = sStr.indexOf(sDelim);
		if (iPos == 0) {
			aArray[aArray.length] = "";
			sStr = sStr.substr(iPos + 1);
		}
		else {
			aArray[aArray.length] = sStr.substr(0, iPos);
			sStr = sStr.substr(iPos + 1);
		}
	}
	if (sStr.length > 0) {
		aArray[aArray.length] = sStr;
		sStr = "";
	}
	return aArray;
}

function ClearValues(oElement, sList) {
	var arFields = sList.split(",");
	var ii = 0;
	var oForm = oElement.form;
	for (ii = 0; ii < arFields.length; ii++) {
		if (oForm[arFields[ii]].type == "select-one") {
			oForm[arFields[ii]].selectedIndex = 0;
		}
		else {
			oForm[arFields[ii]].value = "";
		}
	}
}

function LTrim(str) {
/*
	PURPOSE: Remove leading blanks from our string.
	IN: str - the string we want to LTrim
	RETVAL: An LTrimmed string!
*/
	var sWS = new String(" \t\n\r\xA0");

	var s = new String(str);

	if (sWS.indexOf(s.charAt(0)) != -1) {
		//	We have a string with leading blank(s)...

		var j=0, i = s.length;

		//	Iterate from the far left of string until we
		//	don't have any more whitespace...
		while (j < i && sWS.indexOf(s.charAt(j)) != -1) {
			j++;
		}

		//	Get the substring from the first non-whitespace
		//	character to the end of the string...
		s = s.substring(j, i);
	}
	return s;
}

function RTrim(str) {
/*
	PURPOSE: Remove trailing blanks from our string.
	IN: str - the string we want to RTrim

	RETVAL: A right trimmed string!
*/
	//	We don't want to trim JUST spaces, but also tabs,
	//	line feeds, etc.  Add anything else you want to
	//	"trim" here in Whitespace
	var sWS = new String(" \t\n\r\xA0");

	var s = new String(str);

	if (sWS.indexOf(s.charAt(s.length-1)) != -1) {
		//	We have a string with trailing blank(s)...

		var i = s.length - 1;	//	Get length of string

		//	Iterate from the far right of string until we
		//	don't have any more whitespace...
		while (i >= 0 && sWS.indexOf(s.charAt(i)) != -1) {
			i--;
		}


		//	Get the substring from the front of the string to
		//	where the last non-whitespace character is...
		s = s.substring(0, i+1);
	}
	return s;
}

function sTimeToFloat(sTime) {
	//	Convert a time string to a floating point number. This is
	//	useful for comparing two time strings.
	var aTime = new Array();
	var reTime = new RegExp("([0-1]?[0-9]+):?([0-5]?[0-9]?) ?([AaPp]m?)", "i");

	aTime[0] = sTime.replace(reTime, "$1");
	aTime[1] = sTime.replace(reTime, "$2");
	aTime[2] = sTime.replace(reTime, "$3");

	if (aTime[1] == "") {
		aTime[1] = "0";
	}
	if (aTime[0] == "12") {
		if (aTime[2].indexOf("a") == 0) {
			aTime[0] = parseInt(aTime[0]) - 12;
		}
	}
	else {
		if (aTime[2].indexOf("p") == 0) {
			aTime[0] = parseInt(aTime[0]) + 12;
		}
	}
//	alert(parseInt(aTime[0]) + (aTime[1] / 60));
	return parseInt(aTime[0]) + (aTime[1] / 60);
}

function sWhoseThere(bHex, sTo, sSubject, sBody, sBCC, sTokens) {
/*
	This is a pretty sophisticated mailer. It provides a choice
	of two replacements for spaces in the subject and body of
	an e-mail. It also takes a list of token / replacement pairs
	which it will replace into either the subject or body to
	customize the mails.

	Depending on whether bHex is true, spaces in the subject and
	body will be replaced by either a hex representation or the
	plus sign. Outlook favors hex and Juno favors the plus. I'm
	not aware of others.

	Above, I have defined global variables for the subject and
	body portions with tokens in them. These are then used in the
	function call. This eliminates the need to repeat these strings
	on a page, since they can be passed as variable references. I
	could create as many of these as I need with different names
	and then use where needed.

	The token/replacement list pair is delimited with the caret (^)
	for the pair and the upright (|) for the token and the
	replacement. The function assumes the use of a surrounding
	pair of lower case v's to define the token in the subject or
	body.

	Finally, the To and Bcc e-mail addresses can be passed in an
	invalid form with a space instead of the @. The function looks
	for	this and converts it to valid form.

	NOTE:	I couldn't use ^ to define a token in a regular
	expression, so I used 'v'.

	01/23/2002: Fixed issue of question marks and ampersands truncating
	message in Outlook. We are now replacing these hex codes.
	01/23/2002: Fixed issue of replacement tokens appearing in
	message window.
	01/23/2002: Moved the test for message length prior to doing the
	hex replacements for spaces, question marks, and ampersands. It
	appears that the three-character hex code is considered a single
	character in Outlook.
*/
	var ii = 0;
	var reAmp = /\&/gi;
	var reSp = /\s/gi;
	var reQ = /\?/gi;
	var sOrigBody = sBody;

	if (arguments.length < 5) {
		sBCC = "";
	}
	if (arguments.length < 4) {
		sBody = "";
	}
	if (arguments.length < 3) {
		sSubject = "";
	}

	if (bHex) {
		bHex = confirm("For users of Microsoft Outlook e-mail software, click 'OK', otherwise click 'Cancel'.");
	}
//	var iMax = bHex ? 300 : 400;
	var iMax = bHex ? 325 : 400;
	var sRepl = bHex ? "%20" : "+";
//	alert(sBody);
	if (sTokens) {
		//	If a token list is passed, then perform the
		//	replacements.
		var arPairs = sTokens.split("^");
		for (ii = 0; ii < arPairs.length; ii++) {
			var arPair = arPairs[ii].split("|");
//			alert(arPair);
			var reNew = new RegExp("v" + arPair[0] + "v", "g");
//			alert(reNew);
			sBody = sBody.replace(reNew, arPair[1]);
			sOrigBody = sOrigBody.replace(reNew, arPair[1]);
//			alert(sBody);
			sSubject = sSubject.replace(reNew, arPair[1]);
		}
	}
	//	Correct the e-mail addresses.
	sTo = sTo.replace(reSp, "@");
	sBCC = sBCC.replace(reSp, "@");
	//	Now replace spaces with its replacement value.
	sSubject = sSubject.replace(reSp, sRepl);
	var iLen = sSubject.length + sBody.length;
//	alert(sBody.length);
	sBody = sBody.replace(reSp, sRepl);
	sBody = sBody.replace(reQ, "%3F");
	sBody = sBody.replace(reAmp, "%26");
	if (iLen > iMax) {
		var bOk = CreateMsg(sOrigBody);
		alert("The prepared message has been shortened due to a limitation of your e-mail software.\n\nSee the browser window that just opened for the suggested message.");
		var iTrim = iLen - iMax;
		var iTrim = (iLen > iMax) ? iMax : iLen;
//		sBody = sBody.substr(0, sBody.length - iTrim);
		sBody = sBody.substr(0, iTrim);
//		alert(sBody.length + ": " + sBody);
	//	sBody = "";
	}
	return "mailto:" + sTo + "?Subject=" + sSubject + "&Body=" + sBody;
//	return "mailto:" + sTo + "?Subject=" + sSubject + "&BCC=" + sBCC + "&Body=" + sBody;
	for (ii = 10; ii < document.links.length -10; ii++) {
		alert(document.links[ii].href);
	}
	return "";
}

function CreateMsg(sSuggested) {
	var w;
	var re = new RegExp("\%0D", "gi");
	w = window.open("MsgPop.htm", "msg", "height=240,scrollbars=yes,width=480");
	var d = w.document;
	sSuggested = sSuggested.replace(re, "<br>");
	d.write("<html>");
	d.write("<head><title>Suggested Message from Main Street Central</title></head>");
	d.write("<body>");
	d.write("<p style='color: Blue'>You may copy the message below into your e-mail program window and then close this window.");
	d.write("<p style='color: Red'>Note that if you close this window before completing the paste, Windows may erase the copied text from your clipboard.");
	d.write("<h2 align='Center'>Suggested Message</h2>");
	d.write("<p>" + sSuggested + "</p>");
	d.write("<p align='Center'><a href='JavaScript:window.close()'>Close Window</a></p>");
	d.write("</body>");
	d.write("</html>");
	d.close;
	return true;
}
/*
Change History:
02/16/2002:	Standardized coding and comments for compression.
*/