var username;
/* Initialize AJAX properties */
var ajaxCallbacks = {
	onCreate: function()
	{
		if( $( 'loading' ).tagName == 'DIV' )
		{
			var contentHeight = window.document.documentElement.scrollTop;
			var contentWidth = document.documentElement.clientWidth;
			Element.setStyle( 'loading', {left: (contentWidth/2) - 200 + 'px', top:contentHeight + 200 +'px'} );
		}
		Element.show( 'loading' );
	},
	onComplete: function() {
		if( Ajax.activeRequestCount == 0 )
		{
			Element.hide( 'loading' );
		}
	},
	onFailure: function() {
		Element.hide( 'loading' );
	},
	on404: function() {
		Element.hide( 'loading' );
	},
	onError: function() {
		Element.hide( 'loading' );
	},
	onException: function() {
		Element.hide( 'loading' );		
	}
};
Ajax.Responders.register( ajaxCallbacks );

/* Open a confirm Dialog */
function openConfirmDialog( message, urlAction )
{
	result = false;
	if( confirm( message ) )
	{
		if( username != 'demo' )
		{
			ajaxPrototype = new Ajax.Request( urlAction,
						{method: 'get', parameters: null} );
			result = true;
		}
	}
	return result;
}

function getNodeValue( obj, tag )
{
	return obj.getElementsByTagName( tag )[0].firstChild.nodeValue;
}

function selectItem( elementId, value )
{
	for( var i = 0; i < document.getElementById( elementId ).options.length; i++ )
	{
		if( document.getElementById( elementId ).options[i].value == value )
		{
			document.getElementById( elementId ).options[i].selected = true;
			break;
		}
	}
}

function onlyNumbers( field, e )
{
	var key;
	var keychar;

	if( window.event )
	   key = window.event.keyCode;
	else if (e)
		key = e.which;
	else
		return true;
	keychar = String.fromCharCode(key);

	// control keys
	if ((key==null) || (key==0) || (key==8) ||
	    (key==9) || (key==13) || (key==27) )
	   return true;

	// numbers
	else if ((("0123456789").indexOf(keychar) > -1))
	   return true;
	else
	   return false;
}

function sendEmail( urlAction )
{
	ajaxPrototype = new Ajax.Request( urlAction,
				{method: 'get',
   			onComplete: successfulSendEmail} );
}

function successfulSendEmail( request )
{
	var response = request.responseXML.getElementsByTagName( 'response' );
	var message = getNodeValue( response[0], 'message' );
	alert( message );
}

function openPopup( url, width, height, scrolling, menubar )
{
   var left = 99;
   var top = 99;
   if( width == null )
   {
        width = 500;
   }
   if( height == null )
   {
        height = 90;
   }
	if( scrolling == null )
	{
		scrolling = 'no';
	}
	if( menubar == null )
	{
		menubar = 'no';
	}
   window.open( url,'advertising', 'width='+width+', height='+height+', top='+top+', left='+left+', scrollbars='+scrolling+', status=no, toolbar=no, location=no, directories=no, menubar='+menubar+', resizable=yes, fullscreen=no');
}