function loadInfo(url, target, requestType) {
	// display loading graphic
	document.getElementById(target).innerHTML = '<br /><img src="img/ajax-loader.gif" id="ajaxLoader" /><br />';
	
	// if browser understands Ajax, create new XMLHttpRequest object
	if (window.XMLHttpRequest) {
			xhr = new XMLHttpRequest();
		} else if (window.ActiveXObject) {
			try {
				xhr = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) { }
		}
	// if XHR object was created, fetch the passed url and feed into the passed target div
	if (xhr) {
		xhr.onreadystatechange = function() {loadDone(url, target, requestType);};
		xhr.open("GET", url, true);
		xhr.send(null);
	}
	else {
		alert("Sorry, either you have JavaScript disabled or I couldn't create an XMLHttpRequest");
	}
	return false;
}  

function load(url, target, requestType) {
	// first clear the tabbed area
	document.getElementById('club_tabbed_box_1').innerHTML = '';
	// display loading graphic
	document.getElementById(target).innerHTML = '<br /><img src="img/ajax-loader.gif" /><br />';
	
	// if browser understands Ajax, create new XMLHttpRequest object
	if (window.XMLHttpRequest) {
			xhr = new XMLHttpRequest();
		} else if (window.ActiveXObject) {
			try {
				xhr = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) { }
		}
	// if XHR object was created, fetch the passed url and feed into the passed target div
	if (xhr) {
		xhr.onreadystatechange = function() {loadDone(url, target, requestType);};
		xhr.open("GET", url, true);
		xhr.send(null);
	}
	else {
		alert("Sorry, but I couldn't create an XMLHttpRequest");
	}
	return false;
}  

function loadDone(url, target, requestType) {
	// when the request is "Loaded"
	if (xhr.readyState == 4) {
		// if the http status is 'OK'
		if (xhr.status == 200) {
			if (requestType == 'Quotes') {
				var finalResponse = constructQuotes(xhr.responseText);
			} else if (requestType == 'Active') {
				var finalResponse = constructActiveLinks(xhr.responseText);
			} else if (requestType == 'Inactive') {
				var finalResponse = constructInactiveLinks(xhr.responseText);
			} else if (requestType == 'Reactivate') {
				var finalResponse = constructReactivateInfo(xhr.responseText);
			} else if (requestType == 'FeedTabs') {
				var finalResponse = constructTabs(xhr.responseText);
			} else if (requestType == 'News') {
				var finalResponse = constructNews(xhr.responseText);
			}
			document.getElementById(target).innerHTML = finalResponse;
		} else {
			document.getElementById(target).innerHTML = "Oops, an error:\n"+ xhr.status + "\n" +xhr.statusText;
		}
	}
}

function constructQuotes() {	
	var response = xhr.responseText;
	if (response.length > 0) {
		var stringOutput = "<em>";		
		var quotesArray = new Array();
		// split quotes into individual array cells
		var quotesArray = response.split("\n");
		var thisQuote = Math.floor(Math.random()* quotesArray.length);
		stringOutput += quotesArray[thisQuote];
		stringOutput += '</em>';
		return stringOutput;
	}
}	

function constructActiveLinks() {	
	var response = xhr.responseText;
	if (response.length > 0) {
		var stringOutput = "<h2 id=\"activeInactive\">Active ISU Sports Clubs: </h2><p align=\"left\"></p>\n";		
		var clubNamesArray = new Array();
		// split club names into individual array cells
		var clubNamesArray = response.split("\n");
		
		// alphabetize array before processing
		clubNamesArray.sort();
		for (var i = 0; i < clubNamesArray.length; i++) {
			if (clubNamesArray[i].substring(0, 7) == "Active:") {
				// first capture length index number
				var endPos = clubNamesArray[i].length;
				// then decrement one
				endPos--;
				// then capture just this club's name by finding the exact club name as a substring
				var thisClubName = clubNamesArray[i].substring(7, endPos);
				// need to strip out spaces, commas, periods, and quotes for href use
				var thisClubLink = thisClubName; 
				thisClubLink = thisClubLink.replace(/ /g, "");
				thisClubLink = thisClubLink.replace(/,/g, "");
				thisClubLink = thisClubLink.replace(/\./g, "");
				thisClubLink = thisClubLink.replace(/'/g, "");
				stringOutput += '<a href="clubinfo/' + thisClubLink + '.txt" class="clubTab" onclick="load(\'clubinfo/' + thisClubLink + '.txt\', \'club_tabbed_box_1\', \'FeedTabs\'); return false;">' + thisClubName + '</a>\n';
				if (((i+1) != clubNamesArray.length)) {
					stringOutput += ' | ';
				}
			}
		}	
		
		return stringOutput;
	}
}	

function constructInactiveLinks() {	
	var response = xhr.responseText;
	if (response.length > 0) {
		var stringOutput = "<h2 id=\"activeInactive\">Inactive ISU Sports Clubs: </h2>\n";		
		var clubNamesArray = new Array();
		// split club names into individual array cells
		var clubNamesArray = response.split("\n");
		// alphabetize array before processing
		clubNamesArray.sort();
		for (var i = 0; i < clubNamesArray.length; i++) {
			if (clubNamesArray[i].substring(0, 9) == "Inactive:" ) {
				stringOutput += '<a href="reactivateinfo.txt" onclick="load(\'reactivateinfo.txt\', \'club_tabbed_box_1\', \'Reactivate\'); return false;">' + clubNamesArray[i].substring(9) + '</a>\n';
				if (((i+1) != clubNamesArray.length)) {
					stringOutput += ' | ';
				}
			}
		}	
		return stringOutput;
	}
}	

function constructReactivateInfo() {	
	var response = xhr.responseText;
	if (response.length > 0) {
		return xhr.responseText;
	}
}

function constructTabs() {	
	// this function creates the neat tabbed interface, filled with each club's information
	var response = xhr.responseText;
	if (response.length > 0) {
		var stringOutput = "";		
		var clubInfoArray = new Array();
		// split newline sections into individual array cells
		var clubInfoArray = response.split("\n");
		
		// Sectional keyphrases are: (and each section is followed by a blank line)
		var clubName = "IMPORTANT:Club Name:";
		var clubInfo = "IMPORTANT:Club Info:";
		var clubSched = "IMPORTANT:Club Schedule:";
		var clubOffs = "IMPORTANT:Current Officers:";
		var clubPics = "IMPORTANT:Pics:";
			
		for (var i = 0; i < clubInfoArray.length; i++) {
			
			// construct header navigation
			if (clubInfoArray[i].substring(0, 20) == clubName) {
				// i was an IMPORTANT:line, so advance to next line
				i++;
				// insert club name
				stringOutput += '\n<h2>' + clubInfoArray[i] + '</h2>\n';
				// insert div opening and tabbed navigation
				stringOutput += '<div class="club_tabbed_area">\n';
				stringOutput += '\t<ul class="clubTabs">\n';
				stringOutput += '\t\t<li><a href="#" title="club_content_1" class="clubtab active">About Us</a></li>\n';
				stringOutput += '\t\t<li><a href="#" title="club_content_2" class="clubtab">Activity Schedule</a></li>\n';
				stringOutput += '\t\t<li><a href="#" title="club_content_3" class="clubtab">Our Officers</a></li>\n';
				stringOutput += '\t\t<li><a href="#" title="club_content_4" class="clubtab">Our Pictures</a></li>\n';
				stringOutput += '\t</ul>\n';
			}
			
			if (clubInfoArray[i].substring(0, 20) == clubInfo) {
				// i was an IMPORTANT:line, so advance to next line
				i++;
				stringOutput += '<div id="club_content_1" class="clubContentArea">\n\t<ul>\n';
				while (!(clubInfoArray[i].substring(0, 24) == clubSched)) {
					if (!(clubInfoArray[i].replace(/^\s+|\s+$/g, '') == '')) {
						if (clubInfoArray[i+2].substring(0, 24) == clubSched) {
							stringOutput += '\t\t<li class="last-child">' + clubInfoArray[i] + '</li>\n';
						} else {
							stringOutput += '\t\t<li>' + clubInfoArray[i] + '</li>\n';
						}
					}
					// need to advance within this section
					i++;
				}
				stringOutput += '\t</ul>\n</div>\n';
			}
			
			if (clubInfoArray[i].substring(0, 24) == clubSched) {
				// i was an IMPORTANT:line, so advance to next line
				i++;
				stringOutput += '<div id="club_content_2" class="clubContentArea">\n\t<ul>\n';
				while (!(clubInfoArray[i].substring(0, 27) == clubOffs)) {
					if (!(clubInfoArray[i].replace(/^\s+|\s+$/g, '') == '')) {
						if (clubInfoArray[i+2].substring(0, 27) == clubOffs) {
							stringOutput += '\t\t<li class="last-child">' + clubInfoArray[i] + '</li>\n';
						} else {
							stringOutput += '\t\t<li>' + clubInfoArray[i] + '</li>\n';
						} 
					}
					// need to advance within this section
					i++;
				}
				stringOutput += '\t</ul>\n</div>\n';
			}
			
			if (clubInfoArray[i].substring(0, 27) == clubOffs) {
				// i was an IMPORTANT:line, so advance to next line
				i++;
				stringOutput += '<div id="club_content_3" class="clubContentArea">\n\t<ul>\n';
				while (!(clubInfoArray[i].substring(0, 15) == clubPics)) {
					if (!(clubInfoArray[i].replace(/^\s+|\s+$/g, '') == '')) {
						if (clubInfoArray[i+2].substring(0, 15) == clubPics) {
							stringOutput += '\t\t<li class="last-child">' + clubInfoArray[i] + '</li>\n';
						} else {
							stringOutput += '\t\t<li>' + clubInfoArray[i] + '</li>\n';
						}
					}
					// need to advance within this section
					i++;
				}
				stringOutput += '\t</ul>\n</div>\n';
			}
			
			if (clubInfoArray[i].substring(0, 15) == clubPics) {
				// i was an IMPORTANT:line, so advance to next line
				i++;
				stringOutput += '<div id="club_content_4" class="clubContentArea">\n';
				stringOutput += '\t<ul>\n';
				while (i <= clubInfoArray.length) {
					if (i <= clubInfoArray.length) {
						stringOutput += '\t\t<li class="last-child"><img src="clubpics/' + clubInfoArray[i] + '" /></li>\n';
					} else {
						stringOutput += '\t\t<li><img src="clubpics/' + clubInfoArray[i] + '" /></li>\n';
					}
					// need to test for end and break out of while loop cuz otherwise get one last undefined pic slipped in
					if (i = clubInfoArray.length) break;
					// need to advance within this section
					i++;
				}
				stringOutput += '\t</ul>\n</div>\n';
			}

		}	

		stringOutput += '</div>\n';
		return stringOutput;
	} /* end if statement */
	/* 		
				 // This is the actual core tabbed interface to build and return (keep this for reference in case the script above gets messed up)
					
					<h2>' + clubInfoArray[i] + '</h2>
					<div class="tabbed_area">
					<ul class="tabs">
						<li><a href=\"#\" title=\"content_1\" class=\"active\">About Us</a></li>
						<li><a href=\"#\" title=\"content_2\">Activity Schedule</a></li>
						<li><a href=\"#\" title=\"content_3\">Who is in?</a></li>
						<li><a href=\"#\" title=\"content_4\">See Pics</a></li>
					</ul>

					<div id="content_1" class="contentArea">
						<ul>
							<li>The Idaho State University Rodeo team has been a member of the National Intercollegiate Rodeo Association, Rocky Mountain Region for over 20 years.</li>
							<li>The main goal of the ISU Rodeo Team is to  help students obtain their collegiate/educational goals through participation on a rodeo team. </li>
							<li>The most important goal as a student and rodeo athlete is to be successful in completing a post secondary degree at Idaho State University.</li>
							<li>As a member of the NIRA, team members compete against other rodeo athletes from Colleges and Universities throughout Idaho and Utah. We participate in 10 rodeos each year to qualify for a chance to travel to Casper, WY in June for the College National Finals Rodeo. Practices are conducted Tuesdays and Thursdays from 4 p.m. - 8 p.m. during the months of September - October and February - March. Practices are held at the Bannock County Fairgrounds. When the weather is nice we rodeo outside and during the spring, practice is in the new covered arena. Team meetings are held every Thursday of practice and twice each month when practices are not taking place.</li>
							<li>Membership varies from year to year with the average being 10-15 members. Everyone can participate in practice and competitions.Point teams consist of 6 men and 4 women. These teams are chosen on the number of points accumulated throughout the year.</li>
							<li class="last-child">The team hosts a NIRA sanctioned rodeo in the fall of each year. On top of competing, members are required to participate in fund raising activities and promotional events.</li>
						</ul>
					</div>
					
					<div id="content_2" class="contentArea">
						<ul>
							<li>2009 Spring Schedule:</li>
							<li>Feb. 13 - Cowboy Ball Fund Raising Event</li>
							<li>Feb. 27-28 - Southern Utah University/Dixie State Rodeo in Hurricane, UT</li>
							<li>Mar. 6-7 - Weber City Rodeo at Golden Spike Arena, Ogden UT</li>
							<li>Mar. 26 - Regional Rodeo #2 - Heber City, UT</li>
							<li>Mar. 27-28 - Utah Valley Rodeo - Heber City, UT</li>
							<li>Apr. 3-4 - CSI Rodeo - Twin Falls, ID</li>
							<li>Apr. 8-11 - Assisting with DNCFR activities, ISU Holt Arena</li>
							<li class="last-child">June 14 - 20 - College National Finals Rodeo, Casper, WY</li>
						</ul>
					</div>
					
					<div id="content_3" class="contentArea">
						<ul>
							<li>Current Officers:
								Trevor Townsend, President
								Tiffany Kuzmich, Vice President
								Kasie Roe, Secretary
								Jessea Kack, Treasurer</li>
							<li>Adviser
								Angela Askey - askeange@isu.edu or 282-3216</li>
							<li>Assistant Coach
								Ali Shiozawa</li>
							<li class="last-child">If interested in joining the ISU Rodeo Team please contact Advisor, Angela Askey.</li>
						</ul>
					</div>
					
				<div id="content_4" class="contentArea">
					<ul>
						<li class="last-child"><img src="clubpics/rodeo1.JPG" width="480" height="360" alt="Rodeo Club 2009" /></li>
					</ul>
				</div>
				
				</div>
	}
	*/
}	

function constructNews() {	
	// this function just outputs text from news.txt, especially outputting converting nl 2 br
	var response = xhr.responseText;
	if (response.length > 0) {
		var stringOutput = "";		
		var newsArray = new Array();
		// split newline sections into individual array cells
		var newsArray = response.split("\n");
			
		for (var i = 0; i < newsArray.length; i++) {	
				stringOutput += '\n<p style="font-weight:bold;">' + newsArray[i] + '</p>\n';
		}

		return stringOutput;
	} /* end if statement */
}
