/* Global functions available in all pages */
var allOK;

function setupAJAX()
{
  // create a boolean variable to check for a valid Internet Explorer instance.
  var xmlhttp = false;
  // check if we are using IE.
  try
  {
    // if the Javascript version is greater than 5.
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    // you are using Microsoft Internet Explorer.
  }
  catch (e)
  {
    try
    {
      // if not, then use the older active x object.
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      // you are using Microsoft Internet Explorer.
    }
    catch (E)
    {
      // else we must be using a non-IE browser.
      xmlhttp = false;
    }
  }

  //if we are using a non-IE browser, create a javascript instance of the object.
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined')
  {
    xmlhttp = new XMLHttpRequest();
    // you are not using Microsoft Internet Explorer.
  }
  return xmlhttp;
}


function AJAXRequest(serverPage, objID, getOrPost, str, callbackFunction)
{
  xmlhttp = setupAJAX();

  showLoadingIndicator(objID);

  var obj = document.getElementById(objID);

  if (getOrPost == "get")
  {
    xmlhttp.open("GET", serverPage);
    xmlhttp.onreadystatechange = function()
    {
      if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
      {
        obj.innerHTML = xmlhttp.responseText;
        callbackFunction();
      }
    }
    xmlhttp.send(null);
  }
  else
  {
    xmlhttp.open("POST", serverPage, true);
    xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
    xmlhttp.onreadystatechange = function()
    {
      if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
      {
        obj.innerHTML = xmlhttp.responseText;
        callbackFunction();
      }
    }
    xmlhttp.send(str);
  }
}


function isAlphaNumericOnly(checkString)
{
	var regex=/^[0-9A-Za-z]+$/;

	return regex.test(checkString);
}


function getPositionTop(obj)
{
    var top = 0;
    while(obj)
    {
        top += obj.offsetTop;
        obj = obj.offsetParent;
    }
    return top;    
}


function getPositionLeft(obj)
{
    var left = 0;
    while(obj)
    {
        left += obj.offsetLeft;
        obj = obj.offsetParent;
    }
    return left;    
}


function centerObjectTop(objectHeight)
{
  var availHeight;
  var yOffset;

  if (typeof(window.innerWidth) == 'number')
    availHeight = window.innerHeight;
  else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight))
    availHeight = document.documentElement.clientHeight;
  else if (document.body && (document.body.clientWidth || document.body.clientHeight))
    availHeight = document.body.clientHeight;

  if (self.pageYOffset)
  {
    // all except Explorer
    yOffset = self.pageYOffset;
  }
  else if (document.documentElement && document.documentElement.scrollTop)
  {
    // Explorer 6 Strict
    yOffset = document.documentElement.scrollTop;
  }
  else if (document.body)
  {
    // all other Explorers
    yOffset = document.body.scrollTop;
  }

  var top = ((availHeight / 2) + yOffset) - (objectHeight / 2);
  return top;
}


function centerObjectLeft(objectWidth)
{
  var availWidth;

  if (typeof(window.innerWidth) == 'number')
    availWidth = window.innerWidth;
  else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight))
    availWidth = document.documentElement.clientWidth;
  else if (document.body && (document.body.clientWidth || document.body.clientHeight))
    availWidth = document.body.clientWidth;

  var left = (availWidth / 2) - (objectWidth / 2);
  return left;
}


function showLoadingIndicator(objectID)
{
  document.getElementById(objectID).innerHTML = "<img src='/images/loading.gif' />";
}


function showPopupDiv(ajaxUrl, divHeight, divWidth)
{
  var divPopupTransparentCover = document.getElementById('divPopupTransparentCover');
  var divPopupInner = document.getElementById('divPopupInner');
  
  divPopupTransparentCover.style.width = '100%';
  divPopupTransparentCover.style.height = '100%';
  divPopupTransparentCover.style.top = '0px';
  divPopupTransparentCover.style.left = '0px';
  divPopupTransparentCover.style.display = "block";

  surroundHeight = divHeight + 50;
  surroundWidth = divWidth + 50;

  divPopupInner.style.width = surroundWidth;
  divPopupInner.style.height = surroundHeight;
  divPopupInner.style.top = centerObjectTop(surroundHeight) + "px";
  divPopupInner.style.left = centerObjectLeft(surroundWidth) + "px";
  divPopupInner.style.display = "block";

  AJAXRequest(ajaxUrl, 'divPopupInner', 'get', '', function(){});
}


function hidePopupDivs()
{
  var divPopupTransparentCover = document.getElementById('divPopupTransparentCover');
  var divPopupInner = document.getElementById('divPopupInner');
  
  document.getElementById('divPopupTransparentCover').innerHTML = "";
  document.getElementById('divPopupTransparentCover').style.display = "none";
  document.getElementById('divPopupInner').innerHTML = "";
  document.getElementById('divPopupInner').style.display = "none";
  document.getElementById('divPopupClearCover').innerHTML = "";
  document.getElementById('divPopupClearCover').style.display = "none";
  document.getElementById('divSearchPopupInner').innerHTML = "";
  document.getElementById('divSearchPopupInner').style.display = "none";
}


function trim(str)
{
	return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}


function isValidEmail(str)
{
  str = trim(str);
  var at="@"
  var dot="."
  var lat=str.indexOf(at)
  var lstr=str.length
  var ldot=str.indexOf(dot)

  if (str.indexOf(at)==-1)
    return false

  if (str.indexOf(at)==0 || str.indexOf(at)==lstr)
    return false

  if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
    return false

  if (str.indexOf(at,(lat+1))!=-1)
    return false

  if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
    return false

  if (str.indexOf(dot,(lat+2))==-1)
    return false
		
  if (str.indexOf(" ")!=-1)
    return false

  if (str.charAt(lstr-1) == dot)
  	return false


  return true					
}


function urlencode(str)
{
	str = escape(str);
	str = str.replace('+', '%2B');
	str = str.replace('%20', '+');
	str = str.replace('*', '%2A');
	str = str.replace('/', '%2F');
	str = str.replace('@', '%40');
	return str;
}


function showWelcomePopup()
{
	showPopupDiv('/ajax_welcome_popup.php', 350, 450);
}


function showMaydaySignupPopup()
{
	showPopupDiv('/ajax_mayday_signup_popup.php', 500, 500);
}


function maydaySignupPopupDecline()
{
	AJAXRequest('/ajax_mayday_signup_popup_decline.php', 'divNothing', 'get', '', function(){});
}


function maydaySignupPopupParticipate()
{
	AJAXRequest('/ajax_mayday_signup_popup_participate.php', 'divNothing', 'get', '', function(){});
}

function addVenueFavourite(venueID, divID)
{
	AJAXRequest('/venue/ajax/ajax_add_venue_favourite.php?venue_id=' + venueID + '&div_id=' + divID, divID, 'get', '', function(){});
}


function removeVenueFavourite(venueID, divID)
{
	AJAXRequest('/venue/ajax/ajax_remove_venue_favourite.php?venue_id=' + venueID + '&div_id=' + divID, divID, 'get', '', function(){});
}


function showSearchPopup()
{
  var divPopupClearCover = document.getElementById('divPopupClearCover');
  var divSearchPopupInner = document.getElementById('divSearchPopupInner');
  
  divPopupClearCover.style.width = '100%';
  divPopupClearCover.style.height = '100%';
  divPopupClearCover.style.top = '0px';
  divPopupClearCover.style.left = '0px';
  divPopupClearCover.style.display = "block";

  divSearchPopupInner.style.display = "block";

  AJAXRequest('/search/ajax_search_popup.php', 'divSearchPopupInner', 'get', '', function() { document.frmSearchPopup.s.focus(); });
}


function changeSelectedSearchPopupOption(selectedOption)
{
	switch (selectedOption)
	{
		case 'venue':
			document.getElementById('search_popup_guidance').innerHTML = 'try a suburb name, post code or venue name';
			document.frmSearchPopup.method = 'post';
			document.getElementById('sbmSearch').href = "/search/"
			document.getElementById('s').focus();
			break;
		case 'suburb':
			document.getElementById('search_popup_guidance').innerHTML = 'try a suburb name or post code';
			document.frmSearchPopup.method = 'post';
			document.frmSearchPopup.action = "/search/";
			document.getElementById('s').focus();
			break;
		case 'blog':
			document.getElementById('search_popup_guidance').innerHTML = 'search text in blog posts and titles';
			document.frmSearchPopup.method = 'get';
			document.frmSearchPopup.action = "/blog/";
			document.getElementById('s').focus();
			break;
	}
}


function isValidEmail(str)
{
	str = trim(str);
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)

	if (str.indexOf(at)==-1)
		return false

	if (str.indexOf(at)==0 || str.indexOf(at)==lstr)
		return false

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
		return false

	if (str.indexOf(at,(lat+1))!=-1)
		return false

	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
		return false

	if (str.indexOf(dot,(lat+2))==-1)
		return false

	if (str.indexOf(" ")!=-1)
		return false

	if (str.charAt(lstr-1) == dot)
		return false

	return true					
}
