var obj;
var ajaxResponseType;
var strConflicts;
var hndConflictPopup = '';
var txtPreviousConflict = ''; //Stored here to be used by conflicts popup.
var txtNewConflict = ''; //Stored here to be used by conflicts popup.
var glbWorkshopID;
var glbUserID;
var removedConflicts;
var glbYOffset = 0;
var baseUrl;

function checkUncheck(workshopID, state, str)
{
	var elements;
	var elem;
	var ID;
	var chkID;
	var spanID;

	chkID = 'chk' + workshopID;
	spanID = 'span' + workshopID;

	elements = document.all;

	for(var i = 0; i < elements.length; i++)
	{
		elem = elements[i];
		ID = elem.id;

		if(ID == chkID)
			elem.checked = state;

		if(ID == spanID)
			elem.innerHTML = str;
	}
}

function openBioPopup(id)
{
	var url;
	var options;
	
	url = 'wBios.asp?id=' + id;
	options = 'width=640, height=400fullscreen=no, toolbar=no, location=no, status=no, menubar=no, scrollbars=yes, resizable=no';

	window.open(url, '', options);
}
function openDescriptionPopup(workshopID, speakerID, showID)
{
	var url;
	var options;
	
	url = 'workshopDescriptionPopup.asp?wkspID=' + workshopID + '&speakerID=' + speakerID + '&sid=' + showID;
	options = 'scrollbars=yes,resizable=yes,width=650,height=500';

	window.open(url,'Description',options);
}
function resolveConflicts(resolution, userID, showID, workshopID)
{
	var inx;

	switch(resolution)
	{
		case 'keepPrevious':
			checkUncheck(workshopID, false, 'Check to Add');
			break;
		case 'keepNew':
			ajaxResponseType = 'removeConflicts';
			ajaxSetUrl(baseUrl + 'ajaxSchedulerRemoveWorkshopConflicts.asp');
			ajaxRequest('userID=' + glbUserID + '&workshopID=' + workshopID);

			for(inx = 0; inx < removedConflicts.length; inx++)
				checkUncheck(removedConflicts[inx], false, 'Check to Add');

			insertWorkshop(userID, showID, workshopID);
			checkUncheck(workshopID, true, 'Added to Schedule');
			break;
		case 'keepBoth':
			insertWorkshop(userID, showID, workshopID);
			checkUncheck(workshopID, true, 'Added to Schedule');
			break;
	}
}
function getConflicts() //Called from conflicts popup window.
{
	hndConflictPopup.document.getElementById('conflictPrevious').innerHTML = txtPreviousConflict;
	hndConflictPopup.document.getElementById('conflictNew').innerHTML = txtNewConflict;
}
function openConflictsPopup(url)
{
	if(!hndConflictPopup.closed && hndConflictPopup.location)
		hndConflictPopup.location.href = url;
	else
	{
		hndConflictPopup = window.open(url,'','toolbar=no, menubar=no, scrollbars=yes, resizable=no, width=645, height=220');
		
		if(!hndConflictPopup.opener)
			hndConflictPopup.opener = self;
	}
	if(window.focus)
		hndConflictPopup.focus();
}
function printSchedulePopup(showID, userID, showName)
{
	var url;

	url = 'printSchedulePopup.asp' + '?showID=' + showID + '&userID=' + userID + '&showName=' + showName;
	window.open(url,'','toolbar=no, menubar=no, scrollbars=yes, resizable=yes, width=645, height=600');
}
function windowPrint()
{
	window.print();
}
function checkConflicts(userID, workshopID)
{
	ajaxResponseType = 'checkConflicts';
	ajaxSetUrl(baseUrl + 'ajaxSchedulerWorkshopConflicts.asp');
	ajaxRequest('userID=' + userID + '&workshopID=' + workshopID);

	if(strConflicts == 'OK')
		return 'OK';
	else
		return strConflicts;
}
function changeAttendState(showID, userID, workshopID, chkObj)
{
	var conflictsData;

	if(!chkObj.checked)
	{
		removeWorkshop(userID, showID, workshopID);
		elements = document.all;
		checkUncheck(workshopID, false, 'Check to Add');
	}
	else
	{
		conflictsData = checkConflicts(userID, workshopID);

		if(conflictsData == 'OK')
		{
			insertWorkshop(userID, showID, workshopID);
			checkUncheck(workshopID, true, 'Added to Schedule');
		}
		else
		{
			glbWorkshopID = workshopID;
			glbUserID = userID;
			txtPreviousConflict = conflictsData;
			txtNewConflict = '&nbsp;&nbsp;&nbsp;' + document.getElementById('time' + workshopID).value + '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp';
			txtNewConflict = txtNewConflict + document.getElementById('Topic' + workshopID).value;
			openConflictsPopup(baseUrl + 'conflictsPopup.asp?userID=' + userID + '&showID=' + showID + '&workshopID=' + workshopID);
		}
	}
	return false;
}
function removeWorkshopAndRedraw(userID, showID, workshopID, topic)
{
	if(confirm('Are you sure you want to remove "' + topic + '"?'))
	{
		removeWorkshop(userID, showID, workshopID);
		displayAllDays(userID, showID, 't');
	}
}
function removeWorkshop(userID, showID, workshopID)
{
	ajaxResponseType = 'removeWorkshop';
	ajaxSetUrl(baseUrl + 'ajaxSchedulerRemoveWorkshop.asp');
	ajaxRequest('userID=' + userID + '&showID=' + showID + '&workshopID=' + workshopID);
	displayAllDays(userID, showID, 't');
}
function insertWorkshop(userID, showID, workshopID)
{
	ajaxResponseType = 'insertWorkshop';
	ajaxSetUrl(baseUrl + 'ajaxSchedulerInsertWorkshop.asp');
	ajaxRequest('userID=' + userID + '&showID=' + showID + '&workshopID=' + workshopID);
}
function ajaxResponse(data)
{
	if(ajaxResponseType == 'displaySchedule')
		obj.innerHTML = data.replace(/\^/g, "&rsquo;"); // Replace apostrophies we took out on the server!
	else if(ajaxResponseType == 'removeWorkshop' || ajaxResponseType == 'insertWorkshop' || ajaxResponseType == 'removeWorkshopAndRedraw')
	{
		if(data == 'failed')
			alert(data);
	}
	else if(ajaxResponseType == 'checkConflicts')
		strConflicts = data;
	else if(ajaxResponseType == 'removeConflicts')
		removedConflicts = data.split(",");
}
function doLoad(page, showID, userID, base, remove)
{
	var result;

	baseUrl  = base;

	result = ajaxInitialize();

	if(result == false)
		alert('AJAX initialization failed!');

	if(page == 'scheduler')
		displayAllDays(userID, showID, remove);

}
function displayAllDays(userID, showID, remove)
{
	var elem;

	elem = document.all;
	ajaxSetUrl(baseUrl + 'ajaxSchedulerSelectedWorkshopsByDay.asp');
	ajaxResponseType = 'displaySchedule';

	for(var i = 0; i < elem.length; i++)
	{
		if(left(elem[i].id, 3) == 'day')
		{
			obj = elem[i];
			ajaxRequest('remove=' + remove + '&userID=' + userID + '&showID=' + showID + '&showDate=' + right(elem[i].id, elem[i].id.length - 3));
		}
	}
}
function left(str, n)
{
	if (n <= 0)
		return "";
	else if (n > String(str).length)
		return str;
	else
		return String(str).substring(0,n);
}
function right(str, n)
{
	if (n <= 0)
	   return "";
	else if (n > String(str).length)
	   return str;
	else
	{
	   var iLen = String(str).length;
	   return String(str).substring(iLen, iLen - n);
	}
}

