var req;
var currSortColumn = null;
var syncer = "";
var certListTimeout = null;

function doListing(sortBy, callbackFunction, xmlRequestParam)
{
	if(syncer != "")
	{
		clearTimeout(certListTimeout);
		certListTimeout = setTimeout("doListing('"+sortBy+"', '"+callbackFunction+"')", 50);
		return;
	}
	syncer = "certList";
	
	var sortAsc = true;
	var sameSortCol = true;
	sortColImg = document.getElementById(sortBy+"SortImg");
	if(null != sortColImg && typeof sortColImg != 'undefined')
	{
		if(sortColImg.src.indexOf('off') != -1)
		{		
			sortColImg.src = sortColImg.src.replace("off.gif", "on.gif");
			sameSortCol = false;
		}
			
		if(sortColImg.src.indexOf('desc') != -1)
		{
			if(sameSortCol)
			{
				sortColImg.src = '/images/certification-directory/asc_on.gif';
				sortAsc = true;
			}
			else
			{
				sortAsc = false;
			}
		}
		else
		{	
			if(sameSortCol)
			{
				sortColImg.src = '/images/certification-directory/desc_on.gif';
				sortAsc = false;
			}
			else
			{
				sortAsc = true;
			}		
		}
	}

	requestXML("xmlRequest,sortBy,sortAsc", xmlRequestParam+","+sortBy+","+sortAsc, callbackFunction);
	
	if(null == currSortColumn)
	{
		//the default sorting column for both listings happens to be "name"
		//if this ever changes, that must be accounted for here
		currSortColumn = "name";
		sameSortCol = true;
	}

	currSortColImg = document.getElementById(currSortColumn+"SortImg");
	if(null != currSortColImg && typeof currSortColImg != 'undefined' && !sameSortCol)
	{
		currSortColImg.src = currSortColImg.src.replace("on.gif", "off.gif");
	}
	
	
	currSortColumn = sortBy;
}
	
function getCertificationListing(sortBy)
{
	doListing(sortBy, 'updateCertificationListing', 'certList');
}

function updateCertificationListing()
{
	// only if req shows "complete"
    if (req != null && req.readyState == 4) 
    {
    // only if "OK"
	    if (req.status == 200) 
	    {
	    	response  = req.responseXML.documentElement;
			if(response != null)
			{
				certTable = document.getElementById("certListTable");
				numRows = certTable.rows.length;
				
				certList = response.getElementsByTagName("DATA");
				for(i = 0;i < certList.length;i++)
				{
					backgroundColor = "#ffffff";
					if(i % 2 == 0)
					{
						backgroundColor = "#f0f0f0";
					}
					
					var name 		= certList[i].getAttribute("name");
					var org 		= certList[i].getAttribute("org");
					var certLevel 	= certList[i].getAttribute("certLevel");
					var expiration 	= certList[i].getAttribute("expiration");
					var address 	= certList[i].getAttribute("address");
					
					if(null == name)
					{
						name = "";
					}
					if(null == org)
					{
						org = "";
					}
					if(null == certLevel)
					{
						certLevel = "";
					}
					if(null == expiration)
					{
						expiration = "";
					}
					if(null == address)
					{
						address = "";
					}
					
					if("" != expiration)
					{
						var expirationArray = expiration.split("-");
						var expirationYear = expirationArray[0];
						var expirationMonth = expirationArray[1];
						var expirationDay = expirationArray[2];
						var expirationString = expirationMonth + "/" + expirationDay + "/" + expirationYear;
					}
					else
					{						
						var expirationString = "";
					}
					
					if(i+1 > numRows-1) //need to add row
					{
						newRow = certTable.insertRow(-1);
						newRow.bgColor = backgroundColor;

						newCell = newRow.insertCell(-1);
						newCell.innerHTML = name;

						newCell = newRow.insertCell(-1);
						newCell.innerHTML = org;

						newCell = newRow.insertCell(-1);
						newCell.innerHTML = certLevel;

						newCell = newRow.insertCell(-1);
						newCell.innerHTML = expirationString;

						newCell = newRow.insertCell(-1);
						newCell.innerHTML = address;
					}
					else //just replace the innerHTML
					{
						currRow = certTable.rows[i+1];
						currRow.cells[0].innerHTML = name;
						currRow.cells[1].innerHTML = org;
						currRow.cells[2].innerHTML = certLevel;
						currRow.cells[3].innerHTML = expirationString
						currRow.cells[4].innerHTML = address;
					}
				}
				
				//delete any extra rows
				for(i = 1;i <= numRows-(certList.length+1);i++)
				{
					certTable.deleteRow(-1);
				}					
			}
			
			loadingSpan = document.getElementById("listLoading");
			loadingSpan.style.display = 'none';
			certTable.style.display = '';
			
			syncer = "";
	    	req = null;
		}
		else
		{
			certTable = document.getElementById("certListTable");
			certTable.style.display = 'none';

			loadingSpan = document.getElementById("listLoading");
			loadingSpan.style.display = 'none';
			
			unavailSpan = document.getElementById("listUnavail");
			unavailSpan.innerHTML = "The certification listing is currently unavailable.";
		}
	}
	fitToScreen();
}

function getCertifiedTrainerListing(sortBy)
{
	doListing(sortBy, 'updateCertifiedTrainerListing', 'certTrainList');
}

function updateCertifiedTrainerListing()
{
	// only if req shows "complete"
    if (req != null && req.readyState == 4) 
    {
    // only if "OK"
	    if (req.status == 200) 
	    {
	    	response  = req.responseXML.documentElement;
			if(response != null)
			{
				certTable = document.getElementById("certTrainListTable");
				numRows = certTable.rows.length;
				
				xwrTrainers = response.getElementsByTagName("XT");
				for(i = 0;i < xwrTrainers.length;i++)
				{
					populateCertTrainRow("XT", certTable, i+1, xwrTrainers[i], numRows, "#bbd7f6", "black_heading");
				}

				xwrCertTrainers = response.getElementsByTagName("XCT");
				for(i = 0;i < xwrCertTrainers.length;i++)
				{
					currRowIdx = xwrTrainers.length+i+1;
					backgroundColor = "#ffffff";
					if(currRowIdx % 2 == 0)
					{
						backgroundColor = "#f0f0f0";
					}
					populateCertTrainRow("XCT", certTable, currRowIdx, xwrCertTrainers[i], numRows, backgroundColor, "black_heading");
				}
				
				xwrAffTrainers = response.getElementsByTagName("XAT");
				for(i = 0;i < xwrAffTrainers.length;i++)
				{
					currRowIdx = xwrCertTrainers.length+xwrTrainers.length+i+1;
					backgroundColor = "#ffffff";
					if(currRowIdx % 2 == 0)
					{
						backgroundColor = "#f0f0f0";
					}
					populateCertTrainRow("XAT", certTable, currRowIdx, xwrAffTrainers[i], numRows, backgroundColor, "black_text");
				}
				
				totNumTrainRows = xwrAffTrainers.length+xwrCertTrainers.length+xwrTrainers.length
				//delete any extra rows
				for(i = 1;i <= numRows-(totNumTrainRows+1);i++)
				{
					certTable.deleteRow(-1);
				}					
			}
			
			syncer = "";
	    	req = null;
		}
		else
		{
			certTable = document.getElementById("certTrainListTable");
			certTable.style.display = 'none';
			
			unavailSpan = document.getElementById("listUnavail");
			unavailSpan.innerHTML = "The certified trainer listing is currently unavailable.";
		}
	}
	fitToScreen();
}

function populateCertTrainRow(tagValue, certTable, rowIndex, currTrainerData, totNumRows, backgroundColor, rowClass)
{
	var instructorLedTrainingPage = "xm8_reg_training.html";
	var contactUsPage = "../corporate/contactus.html";
	var xactwarePage = "http://www.xactware.com";
	
	var name 		= currTrainerData.getAttribute("name");
	var org 		= currTrainerData.getAttribute("org");
	var trainerType	= currTrainerData.getAttribute("trainerType");
	var product 	= currTrainerData.getAttribute("product");
	var location 	= currTrainerData.getAttribute("location");
	var website 	= currTrainerData.getAttribute("website");
	var numStudents	= currTrainerData.getAttribute("numStudents");
	var feedback 	= currTrainerData.getAttribute("feedback");
	var trainerId 	= currTrainerData.getAttribute("trainerId");
	
	if(null == name)
	{
		name = "";
	}
	if(null == org)
	{
		org = "";
	}
	if(null == trainerType)
	{
		trainerType = "";
	}
	if(null == product)
	{
		product = "";
	}
	if(null == location)
	{
		location = "";
	}
	if(null == website)
	{
		website = "";
	}
	if(null == numStudents)
	{
		numStudents = "";
	}
	if(null == feedback)
	{
		feedback = "";
	}
	
	if("XT" == tagValue)
	{ 
		name = '<a href="' + contactUsPage + '">' + name + '</a>';
		org = '<a href="' + xactwarePage + '">' + org + '</a>';
		location = '<a href="' + instructorLedTrainingPage + '">' + location + '</a>';
	}
	if("XCT" == tagValue)
	{		
		name = '<a href="certified_trainer_info.html?' + trainerId + '">' + name + '</a>';
		if("" != website)
		{
			org = '<a href="' + website + '">' + org + '</a>';
		}
	}
	
	if(rowIndex > totNumRows-1) //need to add row
	{
		newRow = certTable.insertRow(-1);
		newRow.className = rowClass;
		newRow.bgColor = backgroundColor;
		newCell = newRow.insertCell(-1);
		newCell.innerHTML = name;
		newCell = newRow.insertCell(-1);
		newCell.innerHTML = org;
		newCell = newRow.insertCell(-1);
		newCell.innerHTML = trainerType;
		newCell = newRow.insertCell(-1);
		newCell.innerHTML = product;
		newCell = newRow.insertCell(-1);
		newCell.innerHTML = location;
		newCell = newRow.insertCell(-1);
		newCell.innerHTML = numStudents;
		newCell.align = "center";
		newCell = newRow.insertCell(-1);
		newCell.innerHTML = feedback;
		newCell.align = "center";
	}
	else //just replace the innerHTML
	{
		currRow = certTable.rows[rowIndex];
		currRow.className = rowClass;
		currRow.bgColor = backgroundColor;
		currRow.cells[0].innerHTML = name;
		currRow.cells[1].innerHTML = org;
		currRow.cells[2].innerHTML = trainerType;
		currRow.cells[3].innerHTML = product
		currRow.cells[4].innerHTML = location;
		currRow.cells[5].innerHTML = numStudents;
		currRow.cells[6].innerHTML = feedback;
	}
}

function getTrainer(trainerId)
{
	
	if(syncer != "")
	{
		clearTimeout(certListTimeout);
		certListTimeout = setTimeout("getTrainer('"+trainerId+"')", 50);
		return;
	}
	syncer = "certList";
	
	requestXML("xmlRequest,trainerId", "getTrainer,"+trainerId, "updateTrainer");
}

function updateTrainer(trainerId)
{
	// only if req shows "complete"
    if (req != null && req.readyState == 4) 
    {
    // only if "OK"
	    if (req.status == 200) 
	    {
	    	response  = req.responseXML.documentElement;
	    	if(response != null && response.getElementsByTagName("XCT").length > 0)
			{
				var trainer = response.getElementsByTagName("XCT")[0];
				
				var name 			= trainer.getAttribute("name");
				var org 			= trainer.getAttribute("org");
				var trainerCity 	= "";
				var trainerState 	= "";
				var trainerZip 		= "";
				var product 		= trainer.getAttribute("product");
				var certDate 		= "";
				var address 		= "";
				var contactCity 	= "";
				var contactState 	= "";
				var contactZip	 	= "";
				var phone		 	= "";
				var email		 	= "";
				var website 		= trainer.getAttribute("website");
				var other		 	= "";
				var feedback	 	= trainer.getAttribute("feedback");
				var numStudents	 	= trainer.getAttribute("numStudents");
				var studentsSince 	= "";
				
				var trainerInfo = null;
				var trainerInfoTags = trainer.getElementsByTagName("TRAINER_INFO");
				if(null != trainerInfoTags && typeof trainerInfoTags != 'undefined' && trainerInfoTags.length > 0)
				{
					trainerInfo = trainerInfoTags[0];
				}
				if(null != trainerInfo)
				{
					trainerCity = trainerInfo.getAttribute("city");
					trainerState = trainerInfo.getAttribute("state");
					trainerZip = trainerInfo.getAttribute("zip");
					certDate = trainerInfo.getAttribute("certDate");
				}

				var contactInfo = null;
				var contactInfoTags = trainer.getElementsByTagName("CONTACT_INFO");
				if(null != contactInfoTags && typeof contactInfoTags != 'undefined' && contactInfoTags.length > 0)
				{
					contactInfo = contactInfoTags[0];
				}
				if(null != contactInfo)
				{
					address = contactInfo.getAttribute("address");
					contactCity = contactInfo.getAttribute("city");
					contactState = contactInfo.getAttribute("state");
					contactZip = contactInfo.getAttribute("zip");
					phone = contactInfo.getAttribute("phone");
					email = contactInfo.getAttribute("email");
					other = contactInfo.getAttribute("other");
				}

				var studentInfo = null;
				var studentInfoTags = trainer.getElementsByTagName("STUDENT_INFO");
				if(null != studentInfoTags && typeof studentInfoTags != 'undefined' && studentInfoTags.length > 0)
				{
					studentInfo = studentInfoTags[0];
				}
				if(null != studentInfo)
				{
					studentsSince = studentInfo.getAttribute("studentsSince");
				}
				
				var nameElement = document.getElementById("trainerName");
				if(null != nameElement && typeof nameElement != 'undefined' && null != name)
				{
					nameElement.innerHTML = name;
				}
				
				var orgElement = document.getElementById("trainerOrg");
				if(null != orgElement && typeof orgElement != 'undefined' && null != org)
				{
					orgElement.innerHTML = org;
				}
				
				var trainerCityElement = document.getElementById("trainerCity");
				if(null != trainerCityElement && typeof trainerCityElement != 'undefined' && null != trainerCity)
				{
					trainerCityElement.innerHTML = trainerCity;
				}
				
				var trainerStateElement = document.getElementById("trainerState");
				if(null != trainerStateElement && typeof trainerStateElement != 'undefined' && null != trainerState)
				{
					trainerStateElement.innerHTML = trainerState;
				}
				
				var trainerZipElement = document.getElementById("trainerZip");
				if(null != trainerZipElement && typeof trainerZipElement != 'undefined' && null != trainerZip)
				{
					trainerZipElement.innerHTML = trainerZip;
				}
				
				var productElement = document.getElementById("trainerProduct");
				if(null != productElement && typeof productElement != 'undefined' && null != product)
				{
					productElement.innerHTML = product;
				}
				
				var certDateElement = document.getElementById("trainercertDate");
				if(null != certDateElement && typeof certDateElement != 'undefined' && null != certDate)
				{
					certDateElement.innerHTML = certDate;
				}
				
				var addressElement = document.getElementById("contactAddress");
				if(null != addressElement && typeof addressElement != 'undefined' && null != address)
				{
					addressElement.innerHTML = address;
				}
				
				var contactCityElement = document.getElementById("contactCity");
				if(null != contactCityElement && typeof contactCityElement != 'undefined' && null != contactCity)
				{
					contactCityElement.innerHTML = contactCity;
				}
				
				var contactStateElement = document.getElementById("contactState");
				if(null != contactStateElement && typeof contactStateElement != 'undefined' && null != contactState)
				{
					contactStateElement.innerHTML = contactState;
				}
				
				var contactZipElement = document.getElementById("contactZip");
				if(null != contactZipElement && typeof contactZipElement != 'undefined' && null != contactZip)
				{
					contactZipElement.innerHTML = contactZip;
				}
				
				var phoneElement = document.getElementById("contactPhone");
				if(null != phoneElement && typeof phoneElement != 'undefined' && null != phone)
				{
					phoneElement.innerHTML = phone;
				}
				
				var emailElement = document.getElementById("contactEmail");
				if(null != emailElement && typeof emailElement != 'undefined' && null != email)
				{
					emailElement.innerHTML = '<a href="mailto:'+email+'">'+email+'</a>';
				}
				
				var websiteElement = document.getElementById("contactWebsite");
				if(null != websiteElement && typeof websiteElement != 'undefined' && null != website)
				{
					websiteElement.innerHTML = '<a href="'+website+'" target="_blank">'+website+'</a>';
				}
				
				var otherElement = document.getElementById("contactOther");
				if(null != otherElement && typeof otherElement != 'undefined' && null != other)
				{
					otherElement.innerHTML = other;
				}
				
				var feedbackElement = document.getElementById("studentFeedback");
				if(null != feedbackElement && typeof feedbackElement != 'undefined' && null != feedback)
				{
					feedbackElement.innerHTML = feedback;
				}
				
				var numStudentsElement = document.getElementById("numStudents");
				if(null != numStudentsElement && typeof numStudentsElement != 'undefined' && null != numStudents)
				{
					numStudentsElement.innerHTML = numStudents;
				}
				
				var studentsSinceElement = document.getElementById("studentsSince");
				if(null != studentsSinceElement && typeof studentsSinceElement != 'undefined' && null != studentsSince)
				{
					studentsSinceElement.innerHTML = studentsSince;
				}
				
			}
			syncer = "";
	    	req = null;
	    }
	}
}