// school.js
// replace jQuery '$' shortcut with $vj 

$vj(document).ready(function(){
	var teamHeader = $vj.ajax({
		//url: baseURL + "proxy/xml.php?url=" + statsURL + "syndicated/schoolInfo/" + orgID + "/",
		url: proxyURL + "schoolinfo?oid=" + orgID,
		cache: false,
		success: handleHeaderReturn
		});
		
	var teamHeader = $vj.ajax({
		url: proxyURL + "teamslist?oid=" + orgID + "&year=" + statYear,
		cache: false,
		success: handleTeamsListReturn
		});
		
	/*  BreadNav Function  */
	var previousGames = $vj.ajax({
		url: proxyURL + 'orgbreadnav?qid=' + groupID + '&oid=' + orgID + "&year=" + statYear,
		cache: false,
		success: handleBreadNavTeam
		});
	
 });
function handleHeaderReturn(doc) {

	//  Used to parse the XML
	 var root = doc.documentElement;
	 var nds;
	 
	 var otid = 0;
	 //  Look for child nodes in the XML
	 if(root.hasChildNodes())  
	 {
		   nds=root.childNodes;
		   //  Look through the child nodes and find nodes named "item"
		   for (var i = 0; i < nds.length; i++)
		   {
			   if (nds[i].nodeName == "item")
			   {
			   	   	//  Look through the children of the nodes named "item" and find what we want
				   	if(nds[i].hasChildNodes())
				   	{
				   		var addr2 = "";  var oid = 0;  var portName = ""; var levelName = "";
				   		var phone = ""; var nickname = ""; var addr = ""; var city = "";
				   		var fax = ""; var state = ""; var zip = ""; var cLName = ""; var cEmail = "";
				   		var nm = ""; var cid = 0; var lid = 0; var logo = "";
				   		var tyr = statYear; sName = ""; lName = ""; 
				   		var cFName = "";
				   		var website = "";
					  	var sNds = nds[i].childNodes;
					  	for (var j=0;j<sNds.length;j++)
					  	{
					  		cond1 = sNds[j].nodeName;
					  		cond2 = sNds[j].hasChildNodes();
						  	if (sNds[j].nodeName == "OrganizationID" && sNds[j].hasChildNodes()) oid = sNds[j].firstChild.nodeValue;
						  	if (sNds[j].nodeName == "Name" && sNds[j].hasChildNodes()) nm = sNds[j].firstChild.nodeValue;
						  	if (sNds[j].nodeName == "tYear" && sNds[j].hasChildNodes()) tyr = sNds[j].firstChild.nodeValue;
						  	if (sNds[j].nodeName == "sName" && sNds[j].hasChildNodes()) sportName = sNds[j].firstChild.nodeValue;
						  	if (sNds[j].nodeName == "lName" && sNds[j].hasChildNodes()) levelName = sNds[j].firstChild.nodeValue;
						  	if (sNds[j].nodeName == "Nickname" && sNds[j].hasChildNodes()) nickname = sNds[j].firstChild.nodeValue;
						  	if (sNds[j].nodeName == "Address" && sNds[j].hasChildNodes()) addr = sNds[j].firstChild.nodeValue;
						  	if (sNds[j].nodeName == "Address2" && sNds[j].hasChildNodes()) addr2 = sNds[j].firstChild.nodeValue;
						  	if (sNds[j].nodeName == "City" && sNds[j].hasChildNodes()) city = sNds[j].firstChild.nodeValue;
						  	if (sNds[j].nodeName == "State" && sNds[j].hasChildNodes()) state = sNds[j].firstChild.nodeValue;
						  	if (sNds[j].nodeName == "Zip" && sNds[j].hasChildNodes()) zip = sNds[j].firstChild.nodeValue;
						  	if (sNds[j].nodeName == "Phone" && sNds[j].hasChildNodes()) phone = sNds[j].firstChild.nodeValue;
						  	if (sNds[j].nodeName == "Fax" && sNds[j].hasChildNodes()) fax = sNds[j].firstChild.nodeValue;
						  	if (sNds[j].nodeName == "Website" && sNds[j].hasChildNodes()) website = sNds[j].firstChild.nodeValue;
						  	if (sNds[j].nodeName == "ContactFirstName" && sNds[j].hasChildNodes()) cFName = sNds[j].firstChild.nodeValue;
						  	if (sNds[j].nodeName == "ContactLastName" && sNds[j].hasChildNodes()) cLName = sNds[j].firstChild.nodeValue;
						  	if (sNds[j].nodeName == "ContactEmail" && sNds[j].hasChildNodes()) cEmail = sNds[j].firstChild.nodeValue;
						  	if (sNds[j].nodeName == "DivisionID" && sNds[j].hasChildNodes()) cid = sNds[j].firstChild.nodeValue;
						  	if (sNds[j].nodeName == "ConferenceID" && sNds[j].hasChildNodes()) lid = sNds[j].firstChild.nodeValue;
						  	if (sNds[j].nodeName == "logo" && sNds[j].hasChildNodes()) logo = sNds[j].firstChild.nodeValue;
					  	}
					  	document.getElementById('teamTitle').innerHTML = nm;
					  	document.getElementById('address1').innerHTML = addr;
					  	document.getElementById('address2').innerHTML = addr2;
					  	if (addr2 == "") document.getElementById('address2row').style.display = "none";
					  	// if (addr2 == "") document.getElementById('address2row').setAttribute('style','display:none');
					  	document.getElementById('citystzip').innerHTML = city + ', ' + state + ' ' + zip;
					  	document.getElementById('primaryContact').innerHTML = '<strong>Primary Contact:</strong> <a href="mailto:' + cEmail + '">' + cFName + ' ' + cLName + '</a>';
					  	document.getElementById('phone').innerHTML = '<strong>Primary Phone:</strong> ' + phone;
					  	document.getElementById('fax').innerHTML = '<strong>Fax:</strong> ' + fax;
					  	document.getElementById('website').innerHTML = '<strong>Website:</strong> <a href="' + website + '" title="' + nm + '" target="_blank">' + website + '</a>';
		   				document.title = siteTitle + nm + ' - Teams List';
		   				document.getElementById("teamScopeName").innerHTML = statYear + ' ' + nm + ' Teams';
		   				if (logo != "") document.getElementById("schoollogo").innerHTML = '<img src="http://co.varvee.com/orglogos/' + logo + '" alt="School Logo" />';
		   				
		   				document.getElementById('schoolNav').innerHTML = '<a href="#" onmouseover="mopen(\'m4\')"  onmouseout="mclosetime()">'  + nm + '</a><div id="m4" onmouseover="mcancelclosetime()"  onmouseout="mclosetime()"></div>';
				   	}  // nds.subnodes
			   }
		   }
	 }

}

function handleTeamsListReturn(doc) {
	
	 var tbody = document.getElementById("schoolTeamsData");
	 //  Delete all the existing rows.  IMPORTANT!!!
	 while (tbody.rows.length > 0) {     tbody.deleteRow(0);    }
	 
		 //  Used to parse the XML
	 var root = doc.documentElement;
	 var nds;
	 var row = 0;
	 var otid = 0;
	 var ys = ""
	 
	 //season = "display_as_column" //uncomment to disable intermediate headings
	 
	 //  Look for child nodes in the XML
	 if(root.hasChildNodes())  
	 {
		   nds=root.childNodes;
		   //  Look through the child nodes and find nodes named "item"
		   for (var i = 0; i < nds.length; i++)
		   {
			   if (nds[i].nodeName == "item")
			   {
			   	   	//  Look through the children of the nodes named "item" and find what we want
				   	if(nds[i].hasChildNodes())
				   	{
				   		var tid = 0; var tr = ""; var cd = 0; var dd = 0; var sid = 0;
				   		var sc = ""; var tn = ""; var cn = ""; var dn = ""; var ln = "";
				   		var tyr = statYear; var DR = false;  var sn = "";
					  	sNds = nds[i].childNodes;
					  	for (var j=0;j<sNds.length;j++)
					  	{
					  		//alert(sNds[j].nodeName);
							if (sNds[j].hasChildNodes())
					  		{
					  			tagName = doc.documentElement.childNodes[i].childNodes[j].nodeName
								value = doc.documentElement.childNodes[i].childNodes[j].firstChild.nodeValue
								if (tagName == "tid" ) tid = value;
							  	if (tagName == "teamRecord" ) tr = value;
							  	if (tagName == "ConferenceID" ) cd = value;
							  	if (tagName == "DivisionID" ) dd = value;
							  	if (tagName == "Season" ) ys = value;
							  	if (tagName == "SportCode" ) sc = value;
							  	if (tagName == "Name" ) tn = value;
							  	if (tagName == "cName" ) cn = value;
							  	if (tagName == "dName" ) dn = value;
							  	if (tagName == "lName" ) ln = value;
							  	if (tagName == "SportName" ) sn = value;
							  	if (tagName == "SportID" ) sid = value;
							  	if (tagName == "lName" ) ln = value;
							  	if (tagName == "tYear" ) tyr = value;
							  	if (tagName == "dispRecord") DR = (value == 'RECORD')
							  
							}

						}
						tn = ((typeof(ln) != 'undefined') ? ln : '')  + ' ' + ((typeof(sn) != 'undefined') ? sn : '');
						//  This evaluates false in IE 7 - I think it's a problem with inArray(), so I added the 1 == 1 part
						if ( 1 == 1 || (tn != "" && $vj.inArray(sid,validSports))) {
						  	newRow = tbody.insertRow(tbody.rows.length);
						  	qw = row % 2;
							newRow.setAttribute('class', rc[qw] );
							newRow.setAttribute('className', rc[qw] );
							if (tid == teamID) { newRow.setAttribute('class', 'hiliteRow' ); newRow.setAttribute('className','hiliteRow'); }
							if (newRow )
							{
								col=0
								//if this is the first record in a season in this list, 
								if (season != "display_as_column" && ys != season) //this puts in the intermediate season headings... 
								{
									season = ys; //assign the season of the current sport to the current season
									newRow.setAttribute('class', 'tableSubHead' );//make it a heading
									newRow.setAttribute('className', 'tableSubHead' ); //thjs is really the wrong color so: ...
									newRow.style.backgroundColor = "#002e6a" //there needs to be a class in the stylesheet to support this... but as there isn't, It's here.
									newCell = newRow.insertCell(0);//add a cell
									newCell.setAttribute("style", 'text-align:center;');//make it centered
									newCell.style.textAlign = "center";
									newCell.setAttribute("colspan", '3');//make it go all the way accross
									newCell.colSpan = 3;//make it go all the way accross
									newCell.innerHTML = ys //put the season name in it
									newRow = tbody.insertRow(tbody.rows.length); //then add a new row again, for this record
									row++; //add the new row to the total.
								}
								
								newRow.setAttribute('class', rc[row % 2] );
								newRow.setAttribute('className', rc[row % 2] );
								if (tid == teamID) { newRow.setAttribute('class', 'hiliteRow' ); newRow.setAttribute('className','hiliteRow'); }
							
								if (season == "display_as_column")  //this works, essentially, as an else for the code above that adds the "season" headings
								{
									$vj('#seasonsubhead').show() //this is set to hidden, we use json to show it, so that there will be headings for all the data
									newCell = newRow.insertCell(col++);
									newCell.setAttribute("style", 'text-align:center;');
									newCell.style.textAlign = "center";
									newCell.innerHTML = ys;
								}
								
								
								newCell = newRow.insertCell(col++); 
								if ($vj.inArray(sid,validSports)) {
									lnk_t = '<a href="' + sc + '/team.php?tid=' + tid + '&sid=' + sid + '&yid=' + tyr + '">';
									lnk_d = '<a href="' + sc + '/overview.php?cid=' + dd + '&sid=' + sid + '&yid=' + tyr + '">';
									lnk_c = '<a href="' + sc + '/overview.php?cid=' + dd + '&lid=' + cd + '&sid=' + sid + '&yid=' + tyr + '">';
								}
								else {
									lnk_t = '<a class="disabled" href="javascript:noSportWarn(sc);">';
									lnk_d = '<a class="disabled" href="javascript:noSportWarn(sc);">';
									lnk_c = '<a class="disabled" href="javascript:noSportWarn(sc);">';
									
								}
								
								newCell.innerHTML = lnk_t + " " + tn + ((DR) ? ' (' + tr + ')' : ' ') + ' </a>';
								newCell = newRow.insertCell(col++); //L
								newCell.setAttribute("style", 'text-align:center;');
								newCell.style.textAlign = "center";
								newCell.innerHTML = lnk_d + dn + "</a>";
								newCell = newRow.insertCell(col++); //T
								newCell.setAttribute("style", 'text-align:center;');
								newCell.style.textAlign = "center";
								newCell.innerHTML = lnk_c + cn + '</a>';
							}
							row++;
						}
				   	}  // nds.subnodes
			   }
		   }
	 }

}


function noSportWarn() {
	alert("We apologize, but this sport is not currently supported by the system.");
}
