// --------------------------------------------------------------------------------
// attach some useful methods to string objects
String.prototype.trim = function()
	{
	return this.replace(/^\s+|\s+$/g, '');
	}

String.prototype.reverse = function()
	{
	var strRev = '';
	for(var i=this.length; i>0; i--)
		strRev += this.charAt(i-1);
	return strRev;
	}

// --------------------------------------------------------------------------------
// set some global vars

var loginFormClicked = false;
var initialUsernameText = 'user-name';
var initialPasswordText = 'password';
	
// --------------------------------------------------------------------------------
// initialise - called on page load
function initialise()
	{
	var obj;
	if ( obj = document.getElementById('user_name') )
		{
		if ( obj.value.trim() == '' && loginFormClicked == false )
			{
			obj.value = initialUsernameText;
			}
		}
	if ( obj = document.getElementById('user_pass'))
		{
		if ( obj.value.trim() == '' && loginFormClicked == false  )
			{
			obj.type = 'text';
			obj.value = initialPasswordText;
			}
		}
	if ( obj = document.getElementById('event_popup') )
		{
		positionObject(obj);
		}
	}

// --------------------------------------------------------------------------------
function resizeEventHandler()
	{
	resizePageOverlay();
	}

// --------------------------------------------------------------------------------
function scrollEventHandler()
	{
	resizePageOverlay();
	}

// --------------------------------------------------------------------------------
function resizePageOverlay()
	{
	var obj;
	if ( obj = document.getElementById('page_overlay') )
		{
		var scrollOffset = getScrollOffset();
		obj.style.left = scrollOffset[0];
		obj.style.top = scrollOffset[1];
		}
	}

// --------------------------------------------------------------------------------
function getWindowSize()
	{
	var clientSize = new Array(0, 0);
	if (typeof( window.pageYOffset ) == 'number' )
		{
		//Netscape compliant
		clientSize[0] = self.innerWidth;
		clientSize[1] = self.innerHeight;
		}
	else if ( document.body && ( document.body.scrollLeft || document.body.scrollTop ) )
		{
		//DOM compliant
		clientSize[0] = document.body.clientWidth;
		clientSize[1] = document.body.clientHeight;
		}
	else if ( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) )
		{
		//IE6 standards compliant mode
		clientSize[0] = document.documentElement.clientWidth;
		clientSize[1] = document.documentElement.clientHeight;
		}
	return clientSize;
	}

// --------------------------------------------------------------------------------
function getScrollOffset()
	{
	var scrollOffset = new Array(0, 0);
	if ( document.body.scrollTop )
		{
		scrollOffset[0] = document.body.scrollLeft;
		scrollOffset[1] = document.body.scrollTop;
		}
	else
		{
		scrollOffset[0] = window.pageXOffset;
		scrollOffset[1] = window.pageYOffset;
		}
	return scrollOffset;
	}

// --------------------------------------------------------------------------------
function positionObject( obj )
	{
	var maxWidth = 800; // restrict max width of display area (set to 0 fo no restriction)
	var scrollOffset = getScrollOffset();
	// centre horizontally or set left position to 0 if object larger than window sioze
	var windowSize = getWindowSize();
	var windowWidth = maxWidth == 0 ? windowSize[0] : (maxWidth < windowSize[0] ? maxWidth : windowSize[0]);
	var windowHeight = windowSize[1];
	var left = parseInt((windowWidth - obj.clientWidth) / 2);
	if ( left < 0 ) left = 0;
	left += scrollOffset[0];
	// position vertically - if object is taller than window size set top position to current scrollTop
	var top = parseInt((windowHeight - obj.clientHeight) / 2);
	if ( top < 0 ) top = 0;
	top += scrollOffset[1];
	// now position the object
	obj.style.left = left;
	obj.style.top = top;
	}

// --------------------------------------------------------------------------------
function launchHelpWindow( topic )
	{
	var windowFeatures = 'height=500, width=350, menubar=no, resizable=no, scrollbars=no, status=no, toolbar=no';
	var helpWindow = window.open('./gigsty-help.php?topic='+topic, 'helpWindow', windowFeatures, false);
	if ( helpWindow )
		{
		helpWindow.focus();
		return false;
		}
	return true;
	}

// --------------------------------------------------------------------------------
function clearLoginForm()
	{
	var obj;
	if ( obj = document.getElementById('user_name') )
		{
		if ( obj.value.trim() == initialUsernameText && loginFormClicked == false )
			{
			obj.value = '';
			}
		}
	if ( obj = document.getElementById('user_pass') )
		{
		if ( obj.value.trim() == initialPasswordText && loginFormClicked == false )
			{
			obj.type = 'password';
			obj.value = '';
			}
		}
	loginFormClicked = true;
	return true;
	}

// --------------------------------------------------------------------------------
function launchEventPopup( eventID )
	{
	var obj = document.getElementById('event_popup');
	if ( obj && parseInt(eventID) > 0 )
		{
		obj.init = function()
			{
			positionObject(this);
			};
		var url = 'ajax-fetch-event.php?event_id='+eventID;
		ajaxLaunchRequest(url, obj);
		document.getElementById('event_popup_when_label').innerHTML = '';
		document.getElementById('event_popup_when_value').innerHTML = '';
		document.getElementById('event_popup_what_label').innerHTML = '';
		document.getElementById('event_popup_what_value').innerHTML = '';
		document.getElementById('event_popup_where_label').innerHTML = '';
		document.getElementById('event_popup_where_value').innerHTML = '';
		showPopup('event_popup');
		}
	return false;
	}

// --------------------------------------------------------------------------------
function showPopup( objName )
	{
	if ( document.getElementById(objName) )
		{
		document.getElementById('page_overlay').style.display='block';
		document.getElementById(objName).style.zIndex = 100;
		document.getElementById(objName).style.visibility = 'visible';
		return true;
		}
	return false;
	}

// --------------------------------------------------------------------------------
function hidePopup( objName )
	{
	if ( document.getElementById(objName) )
		{
		document.getElementById('page_overlay').style.display='none';
		document.getElementById(objName).style.visibility = 'hidden';
		return true;
		}
	return false;
	}

// --------------------------------------------------------------------------------
function initialiseHttpRequestObj( targetObject )
	{
	var httpRequestObj;
	if (window.XMLHttpRequest)
		{ // Mozilla, Safari, ...
		httpRequestObj = new XMLHttpRequest();
		if (httpRequestObj.overrideMimeType)
			{
			httpRequestObj.overrideMimeType('text/xml');
			}
		} 
	else if (window.ActiveXObject)
		{ // IE
		try 
			{
			httpRequestObj = new ActiveXObject("Msxml2.XMLHTTP");
			}
		catch (e)
			{
			try 
				{
				httpRequestObj = new ActiveXObject("Microsoft.XMLHTTP");
                }
			catch (e) {}
			}
		}
	if ( httpRequestObj )
		{
		httpRequestObj.onreadystatechange = function()
			{
			if ( httpRequestObj.readyState == 4 )
				{
				if ( httpRequestObj.status == 200 )
					{
					var response = httpRequestObj.responseText;
					response = response.split(/[\r\n]+/);
					for(var i=0; i<response.length; i++ )
						{
						var line = response[i].trim();
						if ( ! line.match(/^[\<\]]/)  && line != '' )
							{
							eval(line);
							}
						}
					if ( targetObject.init )
						{
						targetObject.init();
						}
					}
				}
			};
		}
	return httpRequestObj;
	}

// --------------------------------------------------------------------------------
function ajaxLaunchRequest( url, targetObject )
	{
	var httpRequestObj = initialiseHttpRequestObj(targetObject);
	if ( httpRequestObj )
		{
		httpRequestObj.open('GET', url, true);
		httpRequestObj.send('');
		return true;
		}
	return false;
    }
