//alumnilist.js

var gaAlumni = new Array(),goBD;
var gsMemoNLDelim = 'þ';
var gsUserSelected = ',';
var giUserSelectedCount = 0;
var gsSelHistory = '';
/*
Object.extend = function(destination, source) {
  for (property in source) {
    destination[property] = source[property];
  }
  return destination;
}

Object.extend(String.prototype, {
	AddHTMLSection: function(asNewPart) {
		var sResult='';
		sResult = this;
		if(sResult !='') {sResult += '<HR>'}
		return sResult += asNewPart;
	}
	
});
*/

function InitPage() {
	goBD = new BrowserDetector();
	TableHilite('tblYearReps',0);
	TableHilite('tblAlumList',0);
	BuildAlumniArray();


}

function ShowHelp(HelpTopic) {
	var sMsg='',sCaption='';
	switch(HelpTopic) {
		case "SELECTION":
			sCaption = 'Selecting Alumni for Mailing';
			sMsg = "By clicking the checkboxes beside a person's name on or off, you can control whether or not the link just below this table includes them in the \"Mail To\" list."
			break;
		default:
	}
	return MyOverLib(sMsg,sCaption,400,2,1,'#000000','#99aaff','#cc3300',3,'/favicon.gif');
}

function PopAlum(aiID) {
	var oAlum=null,sMsg='',sCaption='';
	oAlum = gaAlumni['ALUMID' + aiID];
	sMsg = oAlum.PopupHTML();
	sCaption = oAlum.NameAndNick;
	return MyOverLib(sMsg,sCaption,400,2,1,'#000000','#99aaff','#cc3300',3,'/favicon.gif');
}
function MyOverLib(asMsg,asCaption,aiWidth,aiCaptionSize,aiTextSize,asTextColor,asForeColor,asBGColor,aiBorder,asIcon) {
	var sWidth=''+aiWidth,sCapSize=''+aiCaptionSize,sTextSize=''+aiTextSize,sBorder=''+aiBorder;
	return overlib(asMsg,RIGHT,CAPTION,asCaption,CAPTIONSIZE,sCapSize,TEXTSIZE,sTextSize,TEXTCOLOR,asTextColor,FGCOLOR,asForeColor,BGCOLOR,asBGColor,BORDER,sBorder,WIDTH,sWidth,CAPICON,asIcon,VAUTO);
}

function BuildAlumniArray() {
	var xTB,aLines,sLine='',iLine=0,oAlumni;
	xTB = goBD.GetDocObject('txtAlumInfo');
	aLines = xTB.value.split("\n");
	aLines.shift();
	for(iLine=0;iLine<aLines.length;iLine++) {
		sLine = aLines[iLine];
		oAlumni = new Alumni(sLine);
		gaAlumni['ALUMID' + oAlumni.ID] = oAlumni;
	}
}

function AlumInList(AlumID) {
	return gsUserSelected.indexOf(',' + AlumID + ',');
}

function ToggleSelectedAlum(xCheckBox,AlumID) {
	var oAlum,iPosn,sFind;
	if(xCheckBox.checked) {
		gsSelHistory += 'Adding ' + AlumID;
		//gaUserSelected['ALUMID' + AlumID] = AlumID;
		if(AlumInList(AlumID)==-1) {
			gsUserSelected += '' + AlumID + ',';
			giUserSelectedCount++;
			gsSelHistory += "   OKAY";
		}
	} else {
		//gaUserSelected['ALUMID' + AlumID] = null;
		gsSelHistory += 'Removing ' + AlumID;
		iPosn = AlumInList(AlumID);
		if(iPosn>=0) {
			sFind = ',' + AlumID + ',';
			gsUserSelected = gsUserSelected.replace(sFind,',');
			giUserSelectedCount--;
			gsSelHistory += "   OKAY";
		}
	}
	var xAnchor = goBD.GetDocObject('MailLinkToAlumni');
	if(giUserSelectedCount == 0) {
		xAnchor.href  = "#";
		xAnchor.title = "0 selected";
		iCount = 0;
	} else {
		//<A HREF="mailto:secretariat@lwcb.ca?subject=feedback re: 'Before You Build' info">
		var aList = gsUserSelected.split(',');
		aList.shift();
		aList.pop();
		var iAlumID=-1,iAlum=0,iCount=0,sResult='mailto:';
		for(iAlum=0;iAlum<aList.length;iAlum++) {
			iAlumID = parseInt(aList[iAlum]);
			oAlum = gaAlumni['ALUMID' + iAlumID];
			if(oAlum.BadEmail==0) {
				iCount++;
				if(iCount>1) {sResult += ', '}
				sResult += oAlum.Email;
			}
		}
		sResult += '?subject=Kavalier Alumni Message';
		xAnchor.href = sResult;
		xAnchor.title = iCount + ' selected (with known valid Email addresses)';
	}
	gsSelHistory += "; List=[" + g + "];  Num = " + iCount + "\n";
	//alert(gsSelHistory);

}

//;ID	Name	StartYear	YearRep	Email	BadEmail	Active	Rookie	Nick	Comment	LastUpdated	email2	WebURL	PostKavies	InspMsg
//2	Mike Sharpe	sharpem73@hotmail.com	0	0	0	Happy	Snare 1992, 1994-1995; Pit Tech 1996-1997; Bass Tech 1998-1999	2002.09.22			Marched in 2000 with Kingston Grenadiers Senior Corps. I am now marching snare for the Syracuse Brigadiers Senior Corps.	Be Proud, be very proud to be the only Open Class Corps in Canada.  Have a great time in Europe, represent well!

function Alumni(asAlumniLine) {
	this.aParts = asAlumniLine.split("\t");
	this.ID          = parseInt(this.aParts[0]);
	this.msPopupHTML = '';
	this.NameAndNick = '';
	this.PopupHTML = pfnAlumniPopupHTML;
}

function pfnFixEmail(asRawEmail) {
	var sResult = asRawEmail;
	if(sResult != '') {
		//sResult.replace(/\[\{at\]\}/ , '@');
		sResult = sResult.replace(/\[\{at\]\}/ , '@');
	}
	return sResult;
}

function pfnFixMemo(asRawMemo) {
	var sResult = asRawMemo;
	if(sResult!='') {
		sResult = sResult.replace(/\xFE/gi, "<BR>\n");
		sResult = sResult.replace(/<BR>\n<BR>\n/gi, "<BR>\n&nbsp;<BR>\n");
	}
	return sResult;
}


function pfnAlumniPopupHTML() {
	if(this.msPopupHTML=='') {
		var sTemp='',sSection='';
		var aParts = this.aParts;
		this.Name        = aParts[1];
		this.StartYear   = aParts[2];
		this.YearRep     = aParts[3];
		this.Email       = pfnFixEmail(aParts[4]);
		this.BadEmail    = parseInt(aParts[5]);
		this.Deceased    = parseInt(aParts[6]);
		this.Active      = parseInt(aParts[7]);
		this.Rookie      = parseInt(aParts[8]);
		this.Nick        = aParts[9];
		this.Comment     = pfnFixMemo(aParts[10]);
		this.LastUpdated = aParts[11];
		this.email2      = pfnFixEmail(aParts[12]);
		this.WebURL      = aParts[13];
		this.PostKavies  = pfnFixMemo(aParts[14]);
		this.InspMsg     = pfnFixMemo(aParts[15]);

		this.aParts = null; // free memory
		this.NameAndNick = this.Name;
		if(this.Nick != '') {
			this.NameAndNick += ' &nbsp; (aka "' + this.Nick + '")';
		}

		sSection = 'Alumni ID ' + this.ID + ' (email last updated: ' + this.LastUpdated + ')';
		sTemp = AppendHTMLSection(sTemp, sSection);
		sSection = "<B>First Season</B>: " + this.StartYear;
		sTemp = AppendHTMLSection(sTemp, sSection);
		//sTemp = AppendHTMLSection("<B>First Season:</B> " + this.FirstYear, sSection);
		if(this.Deceased) {
			sSection = this.Name + " is deceased";
		} else {
			sSection = "<B>Email</B>: " + this.Email;
			if(this.email2 != '') {
				sSection += ' or ' + this.email2;
			}
			if(this.BadEmail != 0) {
				sSection += " &nbsp; (doesn't work)";
			}
		}
		sTemp = AppendHTMLSection(sTemp, sSection);
		if(this.WebURL != '') {
			sTemp = AppendHTMLSection(sTemp, "<B>Website:</B> " + this.WebURL);
		}
		sTemp = AppendHTMLSection(sTemp, "<B>What I Did</B>: " + this.Comment);
		if(this.PostKavies != '') {
			sTemp = AppendHTMLSection(sTemp, "<B>Post-Kavies</B>: " + this.PostKavies);
		}
		if(this.InspMsg != '') {
			sTemp = AppendHTMLSection(sTemp, "<B>Note to all</B>: " + this.InspMsg);
		}
		this.msPopupHTML = sTemp;
		this.Comment   =''; //freeing memory
		this.PostKavies='';
		this.InspMsg   ='';
	}
	return this.msPopupHTML;
}

function AppendHTMLSection(asOriginal, asNewPart) {
	var sResult=asOriginal;
	if(sResult !='') {sResult += '<HR>'}
	return sResult += asNewPart;
}
