//Populate the selection box with an array
function addOption(selectbox, whichArray) {
	for(i=0; i<whichArray.length; i++){
		var optn = document.createElement("option");
		optn.text = whichArray[i];
		optn.value = whichArray[i];
		optn.id = whichArray[i];
		selectbox.options.add(optn);
		//If the array is the state, call function to set the selected item
		if(whichArray[i] == selectedState){
			setSelected(i+1);
		}
		for(k=0; k<document.partnershipSelect.selectType.options.length; k++){
			if(document.getElementsByTagName("option")[k].value == selectedType){
				var x = document.partnershipSelect.selectType;
				x.selectedIndex = k;
			}
		}
	}	
}

//Set the drop down menu to show the selected item
function setSelected(setIndex) {
	var x = document.getElementById('selectState');
	x.selectedIndex = setIndex;
}

//Remove the options to write the new ones to the page on change
function removeAllOptions(selectbox, whichArray) {
	if(whichArray == newStateArray){		
		for(i=selectbox.options.length-1; i>=1; i--){
			selectbox.remove(i);
		}
	addOption(document.partnershipSelect.selectState, newStateArray);
	}
}

//Arrays to hold selection options
var newStateArray;
var schoolTypeArray = new Array();

//Write the content to the page
function createPartnershipsDiv(xmlDoc) {
	var schoolCatalog = xmlDoc.getElementsByTagName('schoolCatalog'); 
	var schoolType = schoolCatalog[0].getElementsByTagName("schoolType");
	var states = new Array();
	var statesCount = 0;
	
	
	for (j=0; j<schoolType.length; j++) {	//For each school type
	var featuredCount = 0;
		schoolTypeArray[j] = schoolType[j].getAttribute("name"); //Program name
		if(selectedType == schoolType[j].getAttribute("name") || selectedType == 'All partnerships') { //If a search and it matches, or all partnerships			
			var br = document.createElement('br');
			var schoolDiv = document.getElementById('schoolPartnerships'); //Container on page to hold all content
			var schoolTypeDiv = document.createElement('div');
			schoolTypeDiv.setAttribute('id',schoolType[j].getAttribute("name")+'Div');
			schoolDiv.appendChild(schoolTypeDiv);
			var schoolTypeText = document.createTextNode(schoolType[j].getAttribute("name")); //create text for the school type header
			var typeHeader = document.createElement('h2'); //create container for type header
			typeHeader.setAttribute('style','color: #537289;');
				typeHeader.appendChild(schoolTypeText); //append type text to type header
				schoolTypeDiv.appendChild(typeHeader);//append type to cell			
			var stMatch = new Array(); //Array of states that match
			var stMatchCount = 0;
			var stNoMatch = new Array(); //Array of non-matching states
			var stNoMatchCount = 0;
			var newMatchArray = new Array(); //New array to hold states to write to option
			var newMatchArrayCount = 0;
			var partnershipItem = schoolType[j].getElementsByTagName("school"); //get the schools with parternships for the type
			
			var nonFeatured = document.createElement('div');//Create one div to hold all non featured partnerships for the school type					
					var Featured = document.createElement('div'); //Container to hold all featured partnerships for school type
						Featured.setAttribute('id','Featured'+schoolType[j].getAttribute("name")); //Set the id
					var highlightHd = document.createElement('p'); //Container to hold featured language header
						highlightHd.setAttribute('class','featuredHD');
						highlightHd.setAttribute('className','featuredHD');
						highlightHd.setAttribute('style','color: #93a200');
					var highglightHdTxt = document.createTextNode('Featured Partnerships');
						highlightHd.appendChild(highglightHdTxt); //Append text to header
					Featured.appendChild(highlightHd); //Append header to Featured div
						Featured.setAttribute('class','partnershipFeatured');
						Featured.setAttribute('className','partnershipFeatured');
					schoolTypeDiv.appendChild(nonFeatured);	
			
			for(k=0; k<partnershipItem.length; k++) { //for each partnership
				var schoolState = partnershipItem[k].getAttribute("state"); //get the state of the school		
				
				//This allows us to add partnerships without a specific state - but we don't want "Any" to appear in the state menu
				if(partnershipItem[k].getAttribute("state")!="Any") {					
					states[statesCount]=schoolState; //add the state to the states array
				}
				
				if(selectedState == schoolState || selectedState == 'All states' || partnershipItem[k].getAttribute("state")=="Any") { //if the selected state matches the schools state or all states selected or there is an "Any" state
					stMatch[stMatchCount] = schoolType[j].getAttribute("name"); //Populate match array with the state
					stMatchCount++;
					
					//Create school header info
					var schoolName = document.createTextNode(partnershipItem[k].getAttribute("name")); 
					var schoolSep = document.createTextNode(" - ");
					var schoolColl = document.createTextNode(partnershipItem[k].getAttribute("coll"));
					
					var schoolDivName = partnershipItem[k].getAttribute("name").replace(/ /g,"");//Create div name for the school container						
						
					var schoolContainer = document.createElement('p');//Create to hold school info
						schoolContainer.setAttribute('id',schoolDivName);//Set the id to the school name 										
					var schoolHeader = document.createElement('p'); //create to hold school name
						schoolHeader.setAttribute('class','schoolHD');
						schoolHeader.setAttribute('className','schoolHD'); 
						schoolHeader.appendChild(schoolName);
						schoolHeader.appendChild(schoolSep);
						schoolHeader.appendChild(schoolColl);
					var schoolFact = partnershipItem[k].getElementsByTagName("fact"); //get the facts
					var schoolUL = document.createElement('ul'); //create ul to hold facts
					
					
					for(z=0; z<schoolFact.length; z++){ //for each fact
						var factLI = document.createElement('li'); //create li to hold copy
							factLI.setAttribute('id',schoolDivName+z);
						var factItem = schoolFact[z].getAttribute("copy").toString(); //get the copy of each fact						
							factLI.innerHTML = factItem;
						schoolUL.appendChild(factLI);//Append the li to the ul						
					}	
					
					if(partnershipItem[k].getAttribute("featured") != 0){ //If this is a featured partnership
						if(featuredCount == 0){ //And this is the first featured for this school type create a container for this								
							Featured.appendChild(schoolContainer); //write the school header to the cell
							schoolContainer.appendChild(schoolHeader); //Append the featured div to the whole container
							schoolContainer.appendChild(schoolUL);	 //Append the ul to the
							featuredCount++;
						}
						else {
							Featured.appendChild(schoolContainer); //write the school header to the cell
							schoolContainer.appendChild(schoolHeader); //Append the featured div to the whole container
							schoolContainer.appendChild(schoolUL); //Append the ul to the
						}						
					}
					
					if(featuredCount !=0){
						schoolTypeDiv.insertBefore(Featured, nonFeatured);
					}
					
					if(partnershipItem[k].getAttribute("featured") == 0) {					
						schoolContainer.appendChild(schoolHeader); //write the school header to the cell					
						schoolContainer.appendChild(schoolUL);
						nonFeatured.appendChild(schoolContainer);
					}
				
				}
				
				else { 
					stNoMatch[stNoMatchCount] = schoolType[j].getAttribute("name");
					stNoMatchCount++;
				}
				statesCount++; //next state array index
			}
			if(stMatch == ""){ 
				var noMatchTxt = document.createTextNode("We don't have any schools in our database that match your search.");
				var noMatchP = document.createElement('p');
				noMatchP.appendChild(noMatchTxt); //attach 	
				schoolTypeDiv.appendChild(noMatchP); //attach 
			}	
		prevType = selectedType;
		}
	}
	//send the array to the addOptions only the first time
	if(timesThrough < 1){
		addOption(document.partnershipSelect.selectType,schoolTypeArray);
	}
	createStateArray(states); //create the State array
	
	if(document.getElementById('Science/Technology/Engineering/MathDiv')) {
		var mseText = '<p>Teach For America is looking to fill the demand for math and science teachers in low-income communities. <a href="/about/special_initiatives/math_and_science_initiative.htm">Learn more about our math and science initiative</a><img src="/assets/images/content_linkArrow.gif" class="linkArrow" /></p>';
		var mseP = document.createElement('div');
		mseP.innerHTML = mseText;
		var mseContainer = document.getElementById('Science/Technology/Engineering/MathDiv');
		mseContainer.appendChild(mseP);
	}
}

function createStateArray(stateArray) {
	newStateArray = new Array();
	stateArray.sort();
	var prevState;	
	var newStateCount = 0;
	
	for(i=0; i<stateArray.length; i++){
		
		if(stateArray[i] != prevState) {
			newStateArray[newStateCount] = stateArray[i];
			prevState = stateArray[i];
			newStateCount++;
		}
	}
	removeAllOptions(document.partnershipSelect.selectState, newStateArray);
}
