var appfansurl = "appfans/appfans.xml";
var numofimages = 15;
var tagnames = new Array("small", "large");

function loadappfans(appfansurl) {

	xmlhttp = null;
	xmlbody = null;

	if (window.XMLHttpRequest) {

		xmlhttp = new XMLHttpRequest();

	} else if (window.ActiveXObject) {

		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");

	}

	if (xmlhttp != null) {

		xmlhttp.onreadystatechange = statechange;
		xmlhttp.open("GET", appfansurl, true);
		xmlhttp.send(null);

	} else {

		handleproblem();

	}

}

function statechange() {

	if (xmlhttp.readyState == 4) {

		if (xmlhttp.status == 200) {

			xmlbody = xmlhttp.responseText;

			if (window.ActiveXObject) {

				var doc = new ActiveXObject("Microsoft.XMLDOM");
				doc.async = "false";
				doc.loadXML(xmlbody);

			} else if (window.XMLHttpRequest) {

				var parser = new DOMParser();
				var doc = parser.parseFromString(xmlbody,"text/xml");

			} else {

				handleproblem();

			}

			processxml(doc);

		} else {

			handleproblem();

		}

	}

}

function processxml(doc) {

	var numofimagesinxmlbody = doc.documentElement.getElementsByTagName("image").length;

	if (numofimagesinxmlbody == 0) {

		handleproblem();

	} else {

		for (C02 = 0; C02 < tagnames.length; C02++) {

			eval("image" + tagnames[C02] + " = new Array()");

		}

		if (numofimagesinxmlbody > numofimages) {

			var randoms = new Array();

			while (randoms.length < numofimages) {

				var randomnum = Math.floor(Math.random() * numofimagesinxmlbody);

				if (!inarray(randomnum, randoms)) {

					randoms[randoms.length] = randomnum;

				}

			}

			for (C01 = 0; C01 < randoms.length; C01++) {

				for (C02 = 0; C02 < tagnames.length; C02++) {

					if (doc.documentElement.getElementsByTagName("image")[randoms[C01]].getElementsByTagName(tagnames[C02])[0]) {

						eval("image" + tagnames[C02] + "[" + C01 + "] = doc.documentElement.getElementsByTagName(\"image\")[" + randoms[C01] + "].getElementsByTagName(\"" + tagnames[C02] + "\")[0].childNodes[0].nodeValue");

					} else {

						eval("image" + tagnames[C02] + "[" + C01 + "] = null");

					}

				}

			}

		} else {

			for (C01 = 0; C01 < numofimagesinxmlbody; C01++) {

				for (C02 = 0; C02 < tagnames.length; C02++) {

					if (doc.documentElement.getElementsByTagName("image")[C01].getElementsByTagName(tagnames[C02])[0]) {

						eval("image" + tagnames[C02] + "[" + C01 + "] = doc.documentElement.getElementsByTagName(\"image\")[" + C01 + "].getElementsByTagName(\"" + tagnames[C02] + "\")[0].childNodes[0].nodeValue");

					} else {

						eval("image" + tagnames[C02] + "[" + C01 + "] = null");

					}

				}

			}

		}

		var appfansprimary = Math.floor(Math.random() * imagesmall.length);

		document.getElementById("header").style.minWidth = "697px";
		document.getElementById("header").style.maxWidth = "697px";
		document.getElementById("header").style.height = "248px";
		document.getElementById("header").style.marginTop = "5px";
		document.getElementById("header").style.backgroundColor = "#000000";
		document.getElementById("header").style.backgroundImage = "none";

		var innerhtml = "<div id=\"appfans01\">";
		innerhtml += "<table width=\"330\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\">";

		for (C01 = 0; C01 < imagesmall.length; C01++) {

			if (C01 == 0) {

				innerhtml += "<tr>";

			} else if ((C01 % 5) == 0) {

				innerhtml += "</tr><tr>";

			}

			innerhtml += "<td><a href=\"#\" onclick='return switchimages(" + C01 + ");'><img src=\"" + imagesmall[C01] + "\" width=\"64\" height=\"80\" alt=\"Appalachian Fan\" /></a></td>";

			if ((C01 + 1) == imagesmall.length) {

				innerhtml += "</tr>";

			}

		}

		innerhtml += "</table>";
		innerhtml += "</div>\n\n";
		innerhtml += "<div id=\"appfans04\">\n\n";
		innerhtml += "<div id=\"appfans03\">\n\n";
		innerhtml += "</div>\n\n";
		innerhtml += "</div>\n\n";

		document.getElementById("header").innerHTML = innerhtml;

		writeappfans(imagelarge[appfansprimary]);

		document.getElementById("appfans03").innerHTML = document.getElementById("appfansdefault").innerHTML;

	}

}

function switchimages(id) {

	writeappfans(imagelarge[id]);
	return false;

}

function writeappfans(large) {

	if (large != null) {

		document.getElementById("appfans04").style.backgroundImage = "url(" + large + ")";

	} else {

		document.getElementById("appfans04").style.backgroundImage = "none";

	}

	return;

}

function handleproblem() {

	document.getElementById("header").innerHTML = "<img src=\"images/home.jpg\" width=\"700\" height=\"263\" alt=\"Appalachian State University\" />";

}

function inarray(element, array) {

	for (C01 = 0; C01 < array.length; C01++) {

		if (element == array[C01]) {

			return true;

		}

	}

	return false;

}
