//parseCommunityServicesData 

//Public variables from Common.js
	//var county = "";
	//var IE = 0;
	//var Firefox = 0;
	//var xmlDoc;	

//Initialize Program Arrays for displaying specific programs by County
// 	The Category array are the Community Service Categories
// 	The Type List array are the CTF Program TypeIds that makeup the Category group
//  The strContentA array holds the dynamic html code for the corresponding Category group
//  The LeftCell and RightCell are used to hold data for table, row, cell formatting (4 lines per Program)

var CategoryArray = new Array();
	CategoryArray["0"] = new Array("JCPC");
	CategoryArray["1"] = new Array("Governor's One-on-One");
	
var TypeListArray = new Array();
	TypeListArray["0"] = new  Array("/|37|40|52|53|56|57|58|10|15|20|21|25|84|93|95|96|97|98|30|33|36|43|75|31|01|04|60|80|83|85|86|05|99|/");
	TypeListArray["1"] = new Array("/|45|/");
	
var strContentA = new Array();
	strContentA["0"]= new Array("");	
	strContentA["1"]= new Array("");
	
var LeftCell = new Array();
	LeftCell["0"] = new Array("");
	LeftCell["1"] = new Array("");
	LeftCell["2"] = new Array("");
	LeftCell["3"] = new Array("");

var RightCell = new Array();
	RightCell["0"] = new Array("");
	RightCell["1"] = new Array("");
	RightCell["2"] = new Array("");
	RightCell["3"] = new Array("");
	
var category = 0;
var CategoryItem=0;
var IDnbr = 0;
var lrow = 0;
var rrow = 0;
var strCategoryName = '';
var strProgramName = '';
var strTypeName;

//--------------------------------------------------------
function verify()
//--------------------------------------------------------
{
	// 0 Object is not initialized
 	// 1 Loading object is loading data
 	// 2 Loaded object has loaded data
 	// 3 Data from object can be worked with
 	// 4 Object completely initialized
 	if (xmlDoc.readyState != 4)
 	{
   		return false;
 	}
}

//--------------------------------------------------------
function importCommunityServicesXML(processCounty)
//--------------------------------------------------------
{
	county = processCounty;
		
	var xmlFile = '../XMLData/CommunityServices/JCPCProgramInfo.xml';
	//Check if IE(ActiveX)
	if (window.ActiveXObject) {
    	IE=1;
			xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
			//xmlDoc = new ActiveXObject("Microsoft.XMLHTTP");
			//xmlDoc = new ActiveXObject("Msxml2.XMLHTTP");
			xmlDoc.async="false";
    	xmlDoc.onreadystatechange=verify;
    	xmlDoc.load(xmlFile);
  		xmlObj=xmlDoc.documentElement;
	//Check if FireFox
	} else {
			Firefox=1;
  		xmlDoc = document.implementation.createDocument("", "", null);
			xmlDoc.load(xmlFile);
	}
	 if ( !xmlDoc.load(xmlFile) ) {        
	 	alert ("Browser is not IE or Firefox: failed to load Community Services XML data source!");    
	}

	CommunityServices();

}

//--------------------------------------------------------
function CommunityServices() 
//--------------------------------------------------------
{ 
	//Need to set a Time Delay for Firefox
	intTime = 0;
	if (Firefox == 1) intTime = 1000;
	setTimeout("parseCommunityServicesData()",intTime); 
}


//--------------------------------------------------------
function parseCommunityServicesData()
//--------------------------------------------------------
{
  var x = xmlDoc.getElementsByTagName('ROW');
	// Call parser function to build program list
	parseXMLData(county);
	// Complete list and replace DIVs
	for (CategoryItem=0;CategoryItem<strContentA.length;CategoryItem++) {
		// Write lines at the End of the Program list
		if (strContentA[CategoryItem] != "") { 
			strContentA[CategoryItem] = strContentA[CategoryItem] + '</table>';
		} else {
			strContentA[CategoryItem] = strContentA[CategoryItem] + '<table width="500" border="0" cellpadding="0" cellspacing="0" bgcolor="F3F3F3">';
			IDnbr = CategoryItem+20;
			strContentA[CategoryItem] = strContentA[CategoryItem] + '<tr id="' + IDnbr + '"><td width="10"></td>';
			strContentA[CategoryItem] = strContentA[CategoryItem] + '<td colspan=2>There are no ' + CategoryArray[CategoryItem] + ' Programs for  <b>' + county + '</b> county</td></tr>';
			strContentA[CategoryItem] = strContentA[CategoryItem] + '<tr><td colspan=3>&nbsp;</td></tr></table>';
		}
	
	// Write the DIV element contents for this Category
		var newdiv = document.createElement("div");
		newdiv.innerHTML = strContentA[CategoryItem];
		var container = document.getElementById("communityservices" + CategoryItem);
		container.appendChild(newdiv);
		
	} //End For		
	
} // End Function	


//--------------------------------------------------------
function parseXMLData(county)
//--------------------------------------------------------
{
	var strCounty = " ";
	var strTypeId = " ";
	var x = xmlDoc.getElementsByTagName('ROW');
	// x is the number of Row Tags in the XML file
	//Loop thru each Row and check for County and Type of program
	for (i=0; i<x.length; i++) 
	{
		strProgramName="";
		//Loop thru all Child Nodes within the Row and look for County and Type match
		for (j=0; j<x[i].childNodes.length; j++) 
		{	
			//Need to check is Node Type is 1, since some browsers generate other node types
			if (x[i].childNodes[j].nodeType == 1) 
			{
				if (x[i].childNodes[j].nodeName == "COUNTY") {
					strCounty = x[i].childNodes[j].firstChild.nodeValue;
					CategoryItem = 99;
				}
				if (x[i].childNodes[j].nodeName == "TYPEID") {
					strTypeId = x[i].childNodes[j].firstChild.nodeValue;
					if (strCounty==county) 
					{
						// Check the category Type List for a match
						for (category=0;category<CategoryArray.length;category++) 
						{
							if(strTypeId.match(TypeListArray[category])) 
							{
								CategoryItem = category;
							}
						} //END For
						
					} // END If County
				} else 
					{ 
						if (CategoryItem != 99) 
								{ 
								label=x[i].childNodes[j].nodeName;
								value="";
								if(x[i].childNodes[j].firstChild) 
								{
									value=x[i].childNodes[j].firstChild.nodeValue;
							  }
							  //Call ProcessData Function to write html for label and value			
								processData(label,value);
							
						} //End if Category found
						
	    	} //End If Type list match 
				
			} // End If nodeType = 1
			
		} //End For j (end of specific Row data)
			// If there was a Category Type match, then generate the Program list
			if (CategoryItem != 99)
				generateProgramData();
			//If we've reached the end of the County based on county sort, then we can skip the remaining Rows
			if(strCounty > county)
				i=x.length;
				
	} //For End i (end of all Rows)
	
} //Function End


//--------------------------------------------------------
function processData(label,value)
//--------------------------------------------------------
{
	//Check for New Program label
	if(label=='PROGRAM') 
	{
		// If this is the First Program, then write Category name and table tags
		if(strContentA[CategoryItem] == "") 
		{
			strContentA[CategoryItem] = strContentA[CategoryItem] + '<table width="500" border="0" cellpadding="0" cellspacing="0" bgcolor="F3F3F3">';
			IDnbr = CategoryItem+20;
			strContentA[CategoryItem] = strContentA[CategoryItem] + '<tr id="' + IDnbr + '"><td width="10"></td>';
			strContentA[CategoryItem] = strContentA[CategoryItem] + '<td colspan=2>These are the ' + CategoryArray[CategoryItem] + ' Programs for  <b>' + county + '</b> county</td></tr>';
			strContentA[CategoryItem] = strContentA[CategoryItem] + '<tr><td colspan=3>&nbsp;</td></tr>';
			
		} //End If First Program
		
		//Save this Program name and reset row cell counters
		strProgramName = value;
		rrow = 0;
		lrow = 0;	

	} //End If Program label

	//Read the other Labels and store the data values
  if(value!="(null)" && value!="(none)" && value!="" && value!= 'Other') 
	{
		//There are some Service names that need to be skipped from printing.
		//   Any Service name that is the same as the Type name, then skip it.
		//   Service name of Other can be skipped.
		if(label == 'TYPENAME') 
		{
			strTypeName = value;
			RightCell[rrow] = value;
			rrow = rrow +1;
		} else if(label == 'SERVICENAME1' || label == 'SERVICENAME2') 
		{
			if(value != strTypeName) 
			{	
				RightCell[rrow] = value;
				rrow = rrow + 1;
			}
		} else if(label != 'PROGRAM')
		{
				LeftCell[lrow] = value;
				lrow = lrow + 1;
		} //End if	
						
	} //End If value
	
} //End Function



//--------------------------------------------------------
function generateProgramData()
//--------------------------------------------------------
{
	//Write splitter
	strContentA[CategoryItem] = strContentA[CategoryItem] + '<tr><td colspan=3>';
	strContentA[CategoryItem] = strContentA[CategoryItem] + '<img src="../resources/graphics/county_services/banners/table_spliter.gif" width="500" height="7">';
	strContentA[CategoryItem] = strContentA[CategoryItem] + '</td></tr>';

	//Write Program Name
	strContentA[CategoryItem] = strContentA[CategoryItem] + '<tr><td width="10">&nbsp;</td>';
	strContentA[CategoryItem] = strContentA[CategoryItem] + '<td colspan=2><font size="-2"><b><i>';
	strContentA[CategoryItem] = strContentA[CategoryItem] + strProgramName;
	strContentA[CategoryItem] = strContentA[CategoryItem] + '<i></b></font></td></tr>';  
	
	//Write Program information lines
	var row = 0;
	var lastrow = lrow;
	if (lastrow < rrow) lastrow = rrow;
	for (row=0;row<lastrow;row++) 
	{
		strContentA[CategoryItem] = strContentA[CategoryItem] + '<tr><td width="10">&nbsp;</td><td><font size="-2">'; 
		if (row < lrow)
			strContentA[CategoryItem] = strContentA[CategoryItem] + LeftCell[row];
			
		strContentA[CategoryItem] = strContentA[CategoryItem] + '&nbsp;</font></td><td><font size="-2">';
		if (row < rrow)
			strContentA[CategoryItem] = strContentA[CategoryItem] + RightCell[row];
		
		strContentA[CategoryItem] = strContentA[CategoryItem] + '&nbsp;</font></td></tr>';
	} //END For
	
	//Write Space row 
	strContentA[CategoryItem] = strContentA[CategoryItem] + '<tr><td colspan=3>&nbsp;</td></tr>';

} //END Function


//--------------------------------------------------------
function switch_tab_newslist(intThisId, strBase, intTotalIds)
//--------------------------------------------------------
{
	document.getElementById(strBase+intThisId).style.display='block';
	document.getElementById(strBase+'Tab'+intThisId).style.backgroundColor='#FFFFFF';
	document.getElementById(strBase+'Tab'+intThisId).style.zIndex='60';
	for (var i=0; i<=intTotalIds; ++i)
	{
		if (i != intThisId)
		{
			document.getElementById(strBase+i).style.display='none';
			document.getElementById(strBase+'Tab'+i).style.background='#EBF0FA';
			document.getElementById(strBase+'Tab'+i).style.zIndex='40';
		} //End If
	} //End For
} //End Function
