/* ==================================================================================================== */
/* SLEEP AUTOFORM ===================================================================================== */
/* ==================================================================================================== */

var oSleepInput = null; 
var oSleepTimer = null;
function sleepAutoForm(oInput)
{
	oSleepInput = oInput;
	window.clearTimeout(oSleepTimer);
	oSleepTimer = window.setTimeout('prepareAutoForm()', 250);
}

/* ==================================================================================================== */
/* PREPARE AUTOFORM =================================================================================== */
/* ==================================================================================================== */

function prepareAutoForm()
{
	var oInput = oSleepInput;

	// delete old list
	$('autoform_' + oInput.id).innerHTML = '';
	$('autoform_' + oInput.id).hide();

	var bSearch = false;
	if(oInput.value != '')
	{
		bSearch = true;
	}

	if(bSearch)
	{
		// show loader
		$('loader_' + oInput.id).show();

		var sParams = '';
		sParams += '?task=get_autoform';
		sParams += '&type=' 	+ oInput.id;
		sParams += '&search=' 	+ oInput.value;

		var objAjax = new Ajax.Request
		(
			'/system/extensions/oldie/calendar.ajax.php',
			{
				method :	'post',
				parameters 	: sParams,
				onSuccess 	: prepareAutoFormCallback
			}
		);
	}
}

/* ==================================================================================================== */
/* PREPARE AUTOFORM CALLBACK ========================================================================== */
/* ==================================================================================================== */

function prepareAutoFormCallback(oResponse)
{
	var aResult = oResponse.responseText.evalJSON();

	// get oInput
	var oInput = oSleepInput;

	var i = 0;
	var iHeight = 0;
	aResult.each(function(sSearchResult)
	{
		// color choice
		var sColor = '#EEEEEE';
		if(i % 2 == 0)
		{
			sColor = '#FFFFFF'
		}

		// create action onclick
		var sOnclick = '$(\'' + oInput.id + '\').value = this.innerHTML; $(\'autoform_' + oInput.id + '\').hide(); $(\'' + oInput.id + '\').focus();';
		
		if(oInput.id == 'save[foreward_emails_id]')
		{
			sOnclick = '$(\'' + oInput.id + '\').value = \'' + sInsert + '\' + this.innerHTML; $(\'autoform_' + oInput.id + '\').hide(); $(\'' + oInput.id + '\').focus();';
		}

		// create html
		var sHtml = '';
		sHtml += '<div onclick="' + sOnclick + '" onmouseover="this.style.backgroundColor = \'#BFCBD7\';" onmouseout="this.style.backgroundColor = \'' + sColor + '\';" style="cursor:pointer; background-color:' + sColor + '; height:15px;">';
		sHtml += 	sSearchResult['title'];
		sHtml += '</div>';

		iHeight = iHeight + 15;

		// insert
		$('autoform_' + oInput.id).insert({bottom: sHtml});

		i++;
	});

	if(iHeight < 180)
	{
		$('autoform_' + oInput.id).style.height = iHeight + 'px';
	}
	else
	{
		$('autoform_' + oInput.id).style.height = '180px';
	}

	// only show if result
	if(i != 0)
	{
		$('autoform_' + oInput.id).show();
	}

	// hide loader
	$('loader_' + oInput.id).hide();
}

function closeAutoForm(sFormName)
{
	$('autoform_' + sFormName).hide();
}

/* ==================================================================================================== */
/* SWITCH TO MONTH ==================================================================================== */
/* ==================================================================================================== */
function switchToMonth(sDate, sSite)
{
	var strRequestUrl = '/system/extensions/oldie/calendar.ajax.php?task=switchtomonth&date=' + sDate + '&site=' + sSite;
	var objAjax = new Ajax.Request(
		strRequestUrl,
		{
			method : 'get',
			onComplete : switchToMonthCallback
		}
	); 
}

/* ==================================================================================================== */
/* SWITCH TO MONTH CALLBACK =========================================================================== */
/* ==================================================================================================== */
function switchToMonthCallback(objResponse)
{
	var oData = objResponse.responseText.evalJSON();

	$('divCalendarSheetNew').update(oData.calendar);
	$('divCalendarTitleNew').update(oData.month);
}

function switchMonth(sDir, sDiv) {

	var strRequestUrl = '/system/extensions/oldie/calendar.ajax.php?div='+sDiv+'&dir='+sDir;

	var objAjax = new Ajax.Request(
		strRequestUrl,
		{
			method : 'get',
			onComplete : switchMonthCallback
		}
	); 
	
}

function switchMonthCallback(objResponse)
{
	var oData = objResponse.responseText.evalJSON();

	$('divCalendarSheet'+oData.div).update(oData.calendar);
	$('divCalendarTitle'+oData.div).update(oData.month);
}

function saveReminder()
{
	var iEntry = $F('reminder_entry');
	var sEmail = $F('reminder_email');
	var iDays = $F('reminder_days');

	var strRequestUrl = '/system/extensions/oldie/calendar.ajax.php?task=reminder&entry='+iEntry+'&email='+sEmail+'&days='+iDays;

	var objAjax = new Ajax.Request(
		strRequestUrl,
		{
			method : 'get',
			onComplete : saveReminderCallback
		}
	);
}

function saveReminderCallback(oResponse) {
	var oData = oResponse.responseText.evalJSON();

	if(oData.success == 1) {
		$('divCalendarReminderMessage').update('<span style="color: green;">'+oData.message+'</span>');
	} else {
		$('divCalendarReminderMessage').update('<span style="color: red;">'+oData.message+'</span>');
	}
	
}

function updateLocation() {

	var iCountry = $F('country_id');
	var iZip = $F('zip');

	if(iCountry == 1) {

		if(iZip.length == 5) {
			var strRequestUrl = '/system/extensions/oldie/calendar.ajax.php?task=location&zip='+iZip+'&country_id='+iCountry;
	
			var objAjax = new Ajax.Request(
								strRequestUrl,
								{
									method : 'get',
									onComplete : updateLocationCallback
								}
			);
		}

		$('city').disable();
		$('state').disable();
		
		$('city').addClassName('disabled');
		$('state').addClassName('disabled');
		
		

	} else {

		$('city').enable();
		$('state').enable();

		$('city').removeClassName('disabled');
		$('state').removeClassName('disabled');

	}

}

function updateLocationCallback(oResponse) {
	var oData = oResponse.responseText.evalJSON();

	$('city').value = oData.city;
	$('state').value = oData.state;

}

function toggleReminder() {
	$('divCalendarReminder').toggle();
}

function checkCountry(oElement) {
	
	if($F(oElement) == 4) {
		$('circuit_container').show();
	} else {
		$('circuit_container').hide();
	}
	
}
