/**************
set the x and y coordinates
**************/
function setXY(e)
{
	yPos = Event.pointerY(e);
	xPos = Event.pointerX(e);
}

/**************
Show the working/saving icon
**************/
function startAjaxWorking()
{
	animationOffset = 15; // in pixels
	new Effect.Move($('ajaxWorking'),{duration:0.0, x: xPos-animationOffset, y: yPos-animationOffset, mode: 'absolute'});
}
function stopAjaxWorking()
{
	new Effect.Move($('ajaxWorking'),{duration:0.0, x: 0, y: -1000, mode: 'absolute'});
}

/**************
Show the ajax message window
**************/
function showAjaxMessage(thisMessage)
{
	$('ajaxMessage').update('<div id="ajaxMessageX" onclick="hideAjaxMessage();">X</div><p>' + thisMessage + '</p>');
	new Effect.Move($('ajaxMessage'),{duration:0.0, x: xPos, y: yPos, mode: 'absolute', queue: 'front'});
	//new Effect.BlindDown($('ajaxMessage'),{queue: 'end'});
	new Draggable('ajaxMessage');
}
function hideAjaxMessage()
{
	//new Effect.BlindUp($('ajaxMessage'),{queue: 'front'});
	new Effect.Move($('ajaxMessage'),{duration:0.0, x: 0, y: -1000, mode: 'absolute', queue:'end'});
}

/**************
Show an ajax div window
**************/
function showAjaxDiv(thisId,thisContent,showHideThis)
{
	if (showHideThis == 'no')
	{
		$(thisId).update(thisContent);
	}
	else
	{
		$(thisId).update(thisContent + '<p style="padding-left: 20px;">[<a href="javascript:hideAjaxDiv(\''+thisId+'\');">Hide This</a>]</p>');
	}
	new Effect.BlindDown($(thisId),{queue: 'end'});
}
function hideAjaxDiv(thisId)
{
	new Effect.BlindUp($(thisId),{queue: 'end'});
}

/**************
Standard Ajax Error Message
**************/
function ajaxError(thisMessage)
{
	alert(thisMessage);
}


/************************************/
/*** MOTORSPORTS FINDER FUNCTIONS ***/
/************************************/
function displayMotorsportsCategory(catid)
{
	new Ajax.Request('/ajax/motorsports.sd?a=displayMotorsportsCategory&catid='+catid, {
		method: 'get',
		// now submit the request
		onSuccess: function(transport) {
			if (transport.responseText.length == 0)
			{
				ajaxError("Error encountered.");
			}
			else
			{
				thisId = "motorsport"+catid;
				$(thisId).update(transport.responseText);
			}
		}
	})
}

function hideMotorsportsCategory(catid)
{
	new Ajax.Request('/ajax/motorsports.sd?a=hideMotorsportsCategory&catid='+catid, {
		method: 'get',
		// now submit the request
		onSuccess: function(transport) {
			if (transport.responseText.length == 0)
			{
				ajaxError("Error encountered.");
			}
			else
			{
				thisId = "motorsport"+catid;
				$(thisId).update(transport.responseText);
			}
		}
	})
}





function loadModels(thisMake)
{
	new Ajax.Request('/ajax/mount.sd?a=loadModels&make='+thisMake, {
			method: 'get',
		onSuccess: function(transport) {
			if (transport.responseText.length == 0)
			{
				ajaxError("Error. The models could not be shown.");
			}
			else
			{
				$('modelsSelect').update(transport.responseText);
			}
		}
	})
}


function searchModels(element)
{
	if ($('modelSearchResults').style.display == 'none')
	{
		Effect.BlindDown('modelSearchResults', {duration: 0.4});
	}
	regExpModels = /^[0-9a-zA-Z]+$/;
	if (!$(element).value.match(regExpModels))
	{
		$('modelSearchResultsContent').innerHTML = '<span style="color: #000;">Use only numbers and letters in your search.</span>';
	}
	else
	{
		ajaxUpdaterDelay(element,'modelSearchResultsContent','/ajax/mount.sd?a=searchModels&keyword='+element.value);
	}
}


function ajaxUpdaterDelay(element,idToUpdate,thisUrl)
{
	// Create a function to get the search results
	var func = function() { new Ajax.Updater(idToUpdate, thisUrl, { method: 'get' }); };

	// Check to see if there is already a timeout, and if so cancel it and create a new one
	if ( element.zid )
	{
		clearTimeout(element.zid);
	}
	element.zid = setTimeout(func,500);
}


function hideSearchModels()
{
	if ($('modelSearchResults').style.display != 'none')
	{
		Effect.BlindUp('modelSearchResults', {duration: 0.4});
	}
}


function displayTVSelected(tvid)
{
	new Ajax.Request('/ajax/mount.sd?a=displayTVSelected&tvid='+tvid, {
		method: 'get',
		// now submit the request
		onSuccess: function(transport) {
			if (transport.responseText.length == 0)
			{
				ajaxError("Error. The TV was not found.");
			}
			else
			{
				$('mountFirstBoxContent').update(transport.responseText);
				// now that the TV is showing let's show the options
				calculateTVSelected(tvid);
			}
		}
	})
}


function calculateTVSelected(tvid)
{
	new Ajax.Request('/ajax/mount.sd?a=calculateTVSelected&tvid='+tvid, {
		method: 'get',
		// now submit the request
		onSuccess: function(transport) {
			if (transport.responseText.length == 0)
			{
				ajaxError("Error. The TV was not found.");
			}
			else
			{
				$('mountSecondBoxContent').update(transport.responseText);
			}
		}
	})
}

