function createXML() {
	if (window.ActiveXObject) {
		var oXML = new ActiveXObject("Microsoft.XMLDOM");
		oXML.async = false;
		return oXML;
	} else if (document.implementation && document.implementation.createDocument) {
		var oXML = document.implementation.createDocument("","",null);
		return oXML;
	} else {
		return false;
	}
}

function selectProduct(oSource) {
	var sClass, sDestination;
	//var oSource = oLink;
	
	for (i=0;oSource.tagName!='TR'&&i<10;i++) {
		var oSource = oSource.parentNode;
	}
	
	var oSourceTable = oSource.parentNode;
	var oDestTable = document.getElementById('selected_products');
	var iDestRows = oDestTable.rows.length;
	var iSourceIndex = oSource.rowIndex;
	
	if (oDestTable.rows[iDestRows - 1].className == 'row-1')
		sClass = 'row-2';	
	else
		sClass = 'row-1';	
	
	var oDestination = moveRow(oSourceTable, oDestTable, iSourceIndex, iDestRows);
	oDestination.className = sClass;
	oDestination.cells[0].innerHTML = '<input type="hidden" name="pprRank_' + oSource.id + '" value="1" class="input-rank">' + 
												 '<input onclick="moveProduct(this, \'up\');" type="button" class="nav-button" value="U">' +
												 '<input onclick="removeProduct(this);" type="button" class="nav-button" value="-">' + 
												 '<input onclick="moveProduct(this, \'down\');" type="button" class="nav-button" value="D">';
	
	var oDestination = moveRow(oSourceTable, oDestTable, iSourceIndex, iDestRows + 1);
	oDestination.className = sClass;
	
	restStyles(oSourceTable, 1);
	resetRankings(document.products);
}
function removeProduct(oLink) {
	var sClass, sDestination;
	var oSource = oLink;
	
	for (i=0;oSource.tagName!='TR'&&i<10;i++) {
		var oSource = oSource.parentElement;
	}
	
	var oSourceTable = oSource.parentElement;
	var oDestTable = document.getElementById('displayed_products');
	var iDestRows = oDestTable.rows.length;
	var iSourceIndex = oSource.rowIndex;
	
	if (oDestTable.rows[iDestRows - 1].className == 'row-1')
		sClass = 'row-2';	
	else
		sClass = 'row-1';	
	
	var oDestination = moveRow(oSourceTable, oDestTable, iSourceIndex, iDestRows);
	oDestination.className = sClass;
	oDestination.cells[0].id = oSource.id;
	oDestination.cells[0].innerHTML = '<input onclick="selectProduct(this);" type="button" class="nav-button" value="+">';
	
	var oDestination = moveRow(oSourceTable, oDestTable, iSourceIndex, iDestRows + 1);
	oDestination.className = sClass;

	restStyles(oSourceTable, 2);
	resetRankings(oLink.form);
}

function moveRow(oSourceTable, oDestTable, iSourceIndex, iDestIndex) {
	var oSource = oSourceTable.rows[iSourceIndex];
	var oDestination = oDestTable.insertRow(iDestIndex);
	
	oDestination.className = oSource.className;

	for (i=0; i<oSource.cells.length; i++) {
		var oCell = oDestination.insertCell(i);
		oCell.className = oSource.cells[i].className;
		oCell.align = oSource.cells[i].align;
		oCell.innerHTML = oSource.cells[i].innerHTML;
	}	
	
	oSourceTable.deleteRow(iSourceIndex);
	
	return oDestination;
}
function moveProduct(oLink, sDirection) {
	var sClass, iTargetIndex;
	var oSource = oLink;
	
	for (i=0;oSource.tagName!='TR'&&i<10;i++) {
		var oSource = oSource.parentNode;
	}
	
	var oSourceTable = oSource.parentNode;
	var iSourceIndex = oSource.rowIndex;

	if (sDirection == 'up') {
		if (iSourceIndex < 4)
			return false;
		
		iSourceIndex = iSourceIndex - 2
		
		iTargetIndex = iSourceIndex + 4;
		moveRow(oSourceTable, oSourceTable, iSourceIndex, iTargetIndex)
		moveRow(oSourceTable, oSourceTable, iSourceIndex, iTargetIndex)
	} else {
		if (iSourceIndex >= oSourceTable.rows.length - 2)
			return false;
			
		iTargetIndex = iSourceIndex + 4;
		moveRow(oSourceTable, oSourceTable, iSourceIndex, iTargetIndex)
		moveRow(oSourceTable, oSourceTable, iSourceIndex, iTargetIndex)
	}
	
	restStyles(oSourceTable, 2);
	resetRankings(oLink.form);
}
function restStyles(oTable, iStart) {
	for (i=iStart;i<oTable.rows.length;i=i+2) {	
		if (i%4 == 3 || i%4 == 0)
			oTable.rows[i].className = 'row-1';
		else
			oTable.rows[i].className = 'row-2';
			
		oTable.rows[i+1].className = oTable.rows[i].className;
	}		
}
function resetRankings(oForm) {
	var sTag = 'pprRank_';
	var iCount = 0;

	for (i=oForm.elements.length-1;i>=0;i--) {
		if (oForm.elements[i].name.substr(0, sTag.length) == sTag)
			oForm.elements[i].value = iCount;
			
		iCount++;
	}
}

function getProducts(oForm) {
	var sFilter = oForm.filter.value;

	if (sFilter.length == 1)
		return false;
	
	document.getElementById('loading').style.display = 'block';
	
	var sURL = 'xml_call.asp?CallID=2&posPositionID=' + oForm.posPositionID.value + '&filter=' + oForm.filter.value + '&pcType=' + oForm.pcType.value;
//alert(sURL)
	var sXML = loadAJAX(sURL, 'writeProductTable');
}

function getEvents(oForm) {

	document.getElementById('loading').style.display = 'block';
	//alert(oForm.posPositionID.value + ':' + oForm.sitEventSiteID.value  )
	var sURL = 'xml_call.asp?CallID=3&posPositionID=' + oForm.posPositionID.value + '&sitEventSiteID=' + oForm.sitEventSiteID.value + '&' + new Date().getMilliseconds();
//document.write(sURL); return;
	var sXML = loadAJAX(sURL, 'writeEventTable');
}

function writeProductTable(oXML) {

	var xProducts = oXML.getElementsByTagName('product');

	if (xProducts.length == 0) {
		alert('No products found');
	} else {				
		var sHTML, sStyle, sDescription;
	
		sHTML = '<table cellpadding="2" cellspacing="2" borber="0" width="100%" id="displayed_products">' + 
				  '<tr>' + 
				  '<td class="heading" width="5%"></td>' + 
				  '<td class="heading" width="5%">ID</td>' + 
				  '<td class="heading">Description</td>' +
				  '<td class="heading" width="11%" align="right" id="status">' + xProducts.length + '&nbsp;products</td>' +
				  '</tr>'

		for (i=0;i<xProducts.length;i++) {
			if (i % 2)
				sStyle = 'row-1';
			else
				sStyle = 'row-2';

			if (isFilled(xProducts[i].text))
				//sDescription = xProducts[i].text.replace(sFilter, '<b>' + sFilter + '</b>')
				sDescription = xProducts[i].text
			else
				sDescription = '<i>No description entered</i>'
				
			sHTML = sHTML + 
					  '<tr class="' + sStyle + '" id="' + xProducts[i].getAttribute('pcID') + '">' + 
					  '<td class="text" align="center"><input onclick="selectProduct(this);" type="button" class="nav-button" value="+"></td>' + 
					  '<td class="text" align="right">' + xProducts[i].getAttribute('pcID') + '</td>' + 
					  '<td class="text" align="left" colspan="2">' + xProducts[i].getAttribute('pcDescription') + '</td>' + 
					  '</tr>' +
					  '<tr class="' + sStyle + '">' + 
					  '<td class="text" align="center"></td>' + 
					  '<td class="text" align="right"></td>' + 
					  '<td class="text-small" align="left" colspan="2">' + sDescription + '</td>' + 
					  '</tr>';
		}
		
		sHTML = sHTML + 
				  '</table>';

		document.getElementById('product_display').innerHTML = sHTML;
	}
	
	document.getElementById('loading').style.display = 'none';
}

function writeEventTable(oXML) {
	var xEvents = oXML.getElementsByTagName('event');

	if (xEvents.length == 0) {
		alert('No events found');
	} else {				
		var sHTML, sStyle, sDescription;
	
		sHTML = '<table cellpadding="2" cellspacing="2" borber="0" width="100%" id="displayed_events">' + 
				  '<tr>' + 
				  '<td class="heading" width="5%"></td>' + 
				  '<td class="heading" width="5%">ID</td>' + 
				  '<td class="heading">Description</td>' +
				  '<td class="heading" width="11%" align="right" id="status">' + xEvents.length + '&nbsp;products</td>' +
				  '</tr>'

		for (i=0;i<xEvents.length;i++) {
			if (i % 2)
				sStyle = 'row-1';
			else
				sStyle = 'row-2';

			if (isFilled(xEvents[i].text))
				//sDescription = xProducts[i].text.replace(sFilter, '<b>' + sFilter + '</b>')
				sDescription = xEvents[i].text
			else
				sDescription = '<i>No description entered</i>'
				
			sHTML = sHTML + 
					  '<tr class="' + sStyle + '" id="' + xEvents[i].getAttribute('evtEventID') + '">' + 
					  '<td class="text" align="center"><input onclick="selectEvent(this);" type="button" class="nav-button" value="+"></td>' + 
					  '<td class="text" align="right">' + xEvents[i].getAttribute('evtEventID') + '</td>' + 
					  '<td class="text" align="left" colspan="2">' + xEvents[i].getAttribute('evtTitle') + '</td>' + 
					  '</tr>' +
					  '<tr class="' + sStyle + '">' + 
					  '<td class="text" align="center"></td>' + 
					  '<td class="text" align="right"></td>' + 
					  '<td class="text-small" align="left" colspan="2">' + sDescription + '</td>' + 
					  '</tr>';
		}
		
		sHTML = sHTML + 
				  '</table>';

		document.getElementById('event_display').innerHTML = sHTML;
	}
	
	document.getElementById('loading').style.display = 'none';
}

function selectEvent(oSource) {
	var sClass, sDestination;
	//var oSource = oLink;
	
	for (i=0; oSource.tagName!='TR' && oSource.tagName!='TBODY' && i<10; i++) {
		oSource = oSource.parentNode;
	}
	
	var oSourceTable = oSource.parentNode;
	var oDestTable = document.getElementById('selected_events');
	var iDestRows = oDestTable.rows.length;
	var iSourceIndex = oSource.rowIndex;
	
	if (oDestTable.rows[iDestRows - 1].className == 'row-1')
		sClass = 'row-2';	
	else
		sClass = 'row-1';	
		
	var oDestination = moveRow(oSourceTable, oDestTable, iSourceIndex, iDestRows);
	oDestination.className = sClass;
	oDestination.cells[0].innerHTML = '<input type="hidden" name="pevRank_' + oSource.id + '" value="1" class="input-rank">' + 
												 '<input onclick="moveEvent(this, \'up\');" type="button" class="nav-button" value="U"> ' +
												 '<input onclick="removeEvent(this);" type="button" class="nav-button" value="-"> ' + 
												 '<input onclick="moveEvent(this, \'down\');" type="button" class="nav-button" value="D">';
	
	var oDestination = moveRow(oSourceTable, oDestTable, iSourceIndex, iDestRows + 1);
	oDestination.className = sClass;
	
	restStyles(oSourceTable, 1);
	resetEventRankings(document.events);
}

function resetEventRankings(oForm) {
	var sTag = 'pevRank_';
	var iCount = 0;
	for (i=oForm.elements.length-1;i>=0;i--) {
		if (oForm.elements[i].name.substr(0, sTag.length) == sTag)
			oForm.elements[i].value = iCount;
			
		iCount++;
	}
}

function removeEvent(oLink) {
	var sClass, sDestination;
	var oSource = oLink;
	
	for (i=0;oSource.tagName!='TR'&&i<10;i++) {
		var oSource = oSource.parentElement;
	}
	
	var oSourceTable = oSource.parentElement;
	var oDestTable = document.getElementById('displayed_events');
	var iDestRows = oDestTable.rows.length;
	var iSourceIndex = oSource.rowIndex;
	
	if (oDestTable.rows[iDestRows - 1].className == 'row-1')
		sClass = 'row-2';	
	else
		sClass = 'row-1';	
	
	var oDestination = moveRow(oSourceTable, oDestTable, iSourceIndex, iDestRows);
	oDestination.className = sClass;
	oDestination.cells[0].id = oSource.id;
	oDestination.cells[0].innerHTML = '<input onclick="selectEvent(this);" type="button" class="nav-button" value="+">';
	
	var oDestination = moveRow(oSourceTable, oDestTable, iSourceIndex, iDestRows + 1);
	oDestination.className = sClass;

	restStyles(oSourceTable, 2);
	resetEventRankings(oLink.form);
}

function moveEvent(oLink, sDirection) {
	var sClass, iTargetIndex;
	var oSource = oLink;
	
	for (i=0;oSource.tagName!='TR'&&i<10;i++) {
		var oSource = oSource.parentNode;
	}
	
	var oSourceTable = oSource.parentNode;
	var iSourceIndex = oSource.rowIndex;

	if (sDirection == 'up') {
		if (iSourceIndex < 4)
			return false;
		
		iSourceIndex = iSourceIndex - 2
		
		iTargetIndex = iSourceIndex + 4;
		moveRow(oSourceTable, oSourceTable, iSourceIndex, iTargetIndex)
		moveRow(oSourceTable, oSourceTable, iSourceIndex, iTargetIndex)
	} else {
		if (iSourceIndex >= oSourceTable.rows.length - 2)
			return false;
			
		iTargetIndex = iSourceIndex + 4;
		moveRow(oSourceTable, oSourceTable, iSourceIndex, iTargetIndex)
		moveRow(oSourceTable, oSourceTable, iSourceIndex, iTargetIndex)
	}
	
	restStyles(oSourceTable, 2);
	resetEventRankings(oLink.form);
}
function autoSubmit(oForm) {
	if (confirm('The timeout has expired, please click OK to save your changes, or Cancel to refresh the page.')) {
		oForm.submit();
	} else {
		window.location.reload();
	}
}
function setSessionTimeout(iTotal) {
	var iRemainder = iTotal - 10000;
	setTimeout('autoSubmit(document.forms[0], 10000)', iRemainder);
}
