var xmlhttp;
var kpoffset = 0;
var member = '';
var kpexcept = 0;
var kpdate = 0;
var all = 0;

var xmlhttp2;
var xmlhttp3;
var xmlhttp4;
var xmlhttp5;

function setAll(a)
{
	all = a;
}

function popupEvents(w,h)
{
	var posx = (screen.width - w)/2;
	var posy = (window.innerHeight - h)/2;
	
	document.getElementById('shade').className = 'shade-visible';
	document.getElementById('popup').className = 'popup-visible';
	document.getElementById('popup').style.width = w+'px';
	document.getElementById('popup').style.height = h+'px';
	document.getElementById('popup').style.left = posx+'px';
	document.getElementById('popup').style.top = posy+'px';
	getEvents();
}

function getEvents()
{
	// sending data here ...
   xmlhttp=null;
   
   if (window.XMLHttpRequest)
   {
      // code for all new browsers
      xmlhttp=new XMLHttpRequest();
   }
   else if (window.ActiveXObject)
   {
      // code for IE5 and IE6
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
   }

   if (xmlhttp!=null)
   {
      xmlhttp.open("POST","http://www.arubawineanddine.com/events/client.php");
      xmlhttp.onreadystatechange=showEvents;
      xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      xmlhttp.send('');
   }
   else
   {
		alert('Your browser does not support XMLHTTP');
   }
}

function showEvents()
{
   if (xmlhttp.readyState==4)
   {
      // 4 = "loaded"
      if (xmlhttp.status==200)
      {
         // 200 = OK
         if (xmlhttp.responseText != "")
         {
            document.getElementById('popup').innerHTML = xmlhttp.responseText;
         }
         xmlhttp.close;
			loadCalendar(0,member);
      }
      else
      {
			alert("Unable to retrieve event data: " + xmlhttp.status);
      }
   }
}

function loadCalendar(offset,memberid)
{
   document.getElementById('calendar').innerHTML = "<img src='http://www.arubawineanddine.com/events/images/loading4.gif'>";
	kpoffset = offset;
	member = memberid;

	varstring = "offset="+offset;
	varstring = varstring + "&member="+member;
	varstring = varstring + "&all="+all;

	// sending data here ...
   xmlhttp=null;
   
   if (window.XMLHttpRequest)
   {
      // code for all new browsers
      xmlhttp=new XMLHttpRequest();
   }
   else if (window.ActiveXObject)
   {
      // code for IE5 and IE6
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
   }

   if (xmlhttp!=null)
   {
      xmlhttp.open("POST","http://www.arubawineanddine.com/events/calendar.php");
      xmlhttp.onreadystatechange=display_calendar;
      xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      xmlhttp.send(varstring);
   }
   else
   {
		alert('Your browser does not support XMLHTTP');
   }
}

function display_calendar()
{
   if (xmlhttp.readyState==4)
   {
      // 4 = "loaded"
      if (xmlhttp.status==200)
      {
         // 200 = OK
         if (xmlhttp.responseText != "")
         {
            document.getElementById('calendar').innerHTML = xmlhttp.responseText;
         }
         else
         {
            document.getElementById('calendar').innerHTML = "Failed to generate calendar";
         }
			xmlhttp.close;
			if (kpoffset == 0) displayUpcoming(0,member,0,0,0,0,0);
      }
      else
      {		
			alert("Unable to load calendar: " + xmlhttp.status);
      }
   }
}

function displayEvent(index,jaar,maand,dag)
{
   document.getElementById('event-details').innerHTML = "<img src='http://www.arubawineanddine.com/events/images/loading4.gif'>";

	varstring = "index="+index;
	varstring = varstring + "&jaar="+jaar;
	varstring = varstring + "&maand="+maand;
	varstring = varstring + "&dag="+dag;
	varstring = varstring + "&all="+all;

	// sending data here ...
   xmlhttp2=null;
   
   if (window.XMLHttpRequest)
   {
      // code for all new browsers
      xmlhttp2=new XMLHttpRequest();
   }
   else if (window.ActiveXObject)
   {
      // code for IE5 and IE6
      xmlhttp2=new ActiveXObject("Microsoft.XMLHTTP");
   }

   if (xmlhttp2!=null)
   {
      xmlhttp2.open("POST","http://www.arubawineanddine.com/events/displayevent.php");
      xmlhttp2.onreadystatechange=display_event;
      xmlhttp2.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      xmlhttp2.send(varstring);
   }
   else
   {
		alert('Your browser does not support XMLHTTP');
   }
	loadPoster(index);
	
	if (index != 0) displayUpcoming(kpoffset,member,kpexcept,jaar,maand,dag,index)
}

function display_event()
{
   if (xmlhttp2.readyState==4)
   {
      // 4 = "loaded"
      if (xmlhttp2.status==200)
      {
         // 200 = OK
         if (xmlhttp2.responseText != "")
         {
            document.getElementById('event-details').innerHTML = xmlhttp2.responseText;
         }
         else
         {
            document.getElementById('event-details').innerHTML = "Failed to display event";
         }
			xmlhttp2.close;
      }
      else
      {		
			alert("Unable to display event: " + xmlhttp2.status);
      }
   }
}

function loadPoster(index)
{
   document.getElementById('poster').innerHTML = "<img src='http://www.arubawineanddine.com/events/images/loading4.gif'>";

	varstring = "index="+index;
	varstring = varstring + "&member="+member;

	// sending data here ...
   xmlhttp4=null;
   
   if (window.XMLHttpRequest)
   {
      // code for all new browsers
      xmlhttp4=new XMLHttpRequest();
   }
   else if (window.ActiveXObject)
   {
      // code for IE5 and IE6
      xmlhttp4=new ActiveXObject("Microsoft.XMLHTTP");
   }

   if (xmlhttp4!=null)
   {
      xmlhttp4.open("POST","http://www.arubawineanddine.com/events/poster.php");
      xmlhttp4.onreadystatechange=display_poster;
      xmlhttp4.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      xmlhttp4.send(varstring);
   }
   else
   {
		alert('Your browser does not support XMLHTTP');
   }
}

function display_poster()
{
   if (xmlhttp4.readyState==4)
   {
      // 4 = "loaded"
      if (xmlhttp4.status==200)
      {
         // 200 = OK
         if (xmlhttp4.responseText != "")
         {
            document.getElementById('poster').innerHTML = xmlhttp4.responseText;
				if (xmlhttp4.responseText != '&nbsp;')
				{
					document.getElementById('click-caption').innerHTML = '[Click image to Enlarge]';
				}
				else
				{
					document.getElementById('click-caption').innerHTML = '&nbsp;';
				}
         }
         else
         {
            document.getElementById('poster').innerHTML = "Failed to load poster";
         }
			xmlhttp4.close;
      }
      else
      {		
			alert("Unable to load poster: " + xmlhttp4.status);
      }
   }
}

function displayUpcoming(offset,memberid,except,jaar,maand,dag,index)
{
   document.getElementById('upcoming').innerHTML = "<img src='http://www.arubawineanddine.com/events/images/loading4.gif'>";

	kpoffset = offset;
	kpexcept = except;
	
	varstring = "offset="+offset;
	varstring = varstring + "&member="+member;
	varstring = varstring + "&except="+except;
	varstring = varstring + "&all="+all;
	varstring = varstring + "&jaar="+jaar;
	varstring = varstring + "&maand="+maand;
	varstring = varstring + "&dag="+dag;
	varstring = varstring + "&index="+index;
	
	if (jaar > 0 || maand >0 || dag > 0) kpdate = 1; // a valid date is sent, keep it not to call display event

	// sending data here ...
   xmlhttp3=null;
   
   if (window.XMLHttpRequest)
   {
      // code for all new browsers
      xmlhttp3=new XMLHttpRequest();
   }
   else if (window.ActiveXObject)
   {
      // code for IE5 and IE6
      xmlhttp3=new ActiveXObject("Microsoft.XMLHTTP");
   }

   if (xmlhttp3!=null)
   {
      xmlhttp3.open("POST","http://www.arubawineanddine.com/events/upcoming.php");
      xmlhttp3.onreadystatechange=display_upcoming;
      xmlhttp3.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      xmlhttp3.send(varstring);
   }
   else
   {
		alert('Your browser does not support XMLHTTP');
   }
}

function display_upcoming()
{
   if (xmlhttp3.readyState==4)
   {
      // 4 = "loaded"
      if (xmlhttp3.status==200)
      {
         // 200 = OK
         if (xmlhttp3.responseText != "")
         {
            document.getElementById('upcoming').innerHTML = xmlhttp3.responseText;
         }
         else
         {
            document.getElementById('upcoming').innerHTML = "Failed to display upcoming";
         }
			xmlhttp3.close;
			if (kpexcept == 0 && !kpdate) displayEvent(0,0,0,0);
      }
      else
      {		
			alert("Unable to display upcoming: " + xmlhttp3.status);
      }
   }
}

function submitNewsLetter()
{
	var error = '';
	var firstname = document.newsletter.firstname.value;
	var lastname = document.newsletter.lastname.value;
	var country = document.newsletter.country.value;
	var email = document.newsletter.email.value;
	var emailto = document.newsletter.emailto.value;
	
	if (firstname == '')
	{
		error = "First name is required\n";
	}
	if (lastname == '')
	{
		error = error + "Last name is required\n";
	}
	if (country == '')
	{
		error = error + "Country is required\n";
	}
	if (!checkValidation(email))
	{
		error = error + "Valid email address is required";
	}
	
	if (error != '')
	{
		alert(error);
		return;
	}
	
	varstring = "firstname="+firstname;
	varstring = varstring + "&lastname="+lastname;
	varstring = varstring + "&country="+country;
	varstring = varstring + "&email="+email;
	varstring = varstring + "&emailto="+emailto;

	// sending data here ...
   xmlhttp5=null;
   
   if (window.XMLHttpRequest)
   {
      // code for all new browsers
      xmlhttp5=new XMLHttpRequest();
   }
   else if (window.ActiveXObject)
   {
      // code for IE5 and IE6
      xmlhttp5=new ActiveXObject("Microsoft.XMLHTTP");
   }

   if (xmlhttp5!=null)
   {
      xmlhttp5.open("POST","http://www.arubawineanddine.com/events/submit_subscription.php");
      xmlhttp5.onreadystatechange=handle_subscription;
      xmlhttp5.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      xmlhttp5.send(varstring);
   }
   else
   {
		alert('Your browser does not support XMLHTTP');
   }
}

function handle_subscription()
{
   if (xmlhttp5.readyState==4)
   {
      // 4 = "loaded"
      if (xmlhttp5.status==200)
      {
         // 200 = OK
         if (xmlhttp5.responseText != "")
         {
            document.getElementById('resultarea').innerHTML = "<span class='submit-error'>"+xmlhttp5.responseText+"</SPAN>";
         }
         else
         {
            document.getElementById('resultarea').innerHTML = "<span class='thank-you'>Thank you ...</span>";
				document.newsletter.reset();
         }
			xmlhttp5.close;
			setTimeout('restoreButton()',3000);
      }
      else
      {		
			alert("Unable to send supscription: " + xmlhttp5.status);
      }
   }
}

function restoreButton()
{
	document.getElementById('resultarea').innerHTML = "<input type='button' value='Subscribe' name='subscribe' class='subscription-submit-button' onClick='submitNewsLetter()'>";
}

function checkValidation(emailaddr)
{
	var valid = true;

	if (stringEmpty(emailaddr))
	{
		valid = false; // There is no input value entered
	}
	else if (noAtSign( emailaddr ))
	{
		valid = false; // Does not contain an '@' character
	}
	else if (nothingBeforeAt(emailaddr))
	{
		valid = false; // Must contain at least one character before the '@' character
	}
	else if (noLeftBracket(emailaddr))
	{
		valid = false; // contains a right square bracket ']',\nbut no corresponding left square bracket '['
	}
	else if (noRightBracket(emailaddr))
	{
		valid = false; // contains a left square bracket '[',\nbut no corresponding right square bracket ']'
	}
	else if (noValidPeriod(emailaddr))
	{
		valid = false; // Must contain a period ('.') character
	}
	else if (noValidSuffix(emailaddr))
	{
		valid = false; // Must contain a two, three or four character suffix
	}

	return valid;
}

function stringEmpty (formField)
{
	// CHECK THAT THE STRING IS NOT EMPTY
	if ( formField.length < 1 )
	{
	  return ( true );
	}
	else
	{
	  return ( false );
	}
}

function noAtSign (formField)
{
	// CHECK THAT THERE IS AN '@' CHARACTER IN THE STRING
	if (formField.indexOf ('@', 0) == -1)
	{
	  return ( true )
	}
	else
	{
	  return ( false );
	}
}

function nothingBeforeAt (formField)
{
	// CHECK THERE IS AT LEAST ONE CHARACTER BEFORE THE '@' CHARACTER
	if ( formField.indexOf ( '@', 0 ) < 1 )
	{
	  return ( true )
	}
	else
	{
	  return ( false );
	}
}

function noLeftBracket (formField)
{
	// IF EMAIL ADDRESS IN FORM 'user@[255,255,255,0]', THEN CHECK FOR LEFT BRACKET
	if ( formField.indexOf ( '[', 0 ) == -1 && formField.charAt (formField.length - 1) == ']')
	{
	  return ( true )
	}
	else
	{
	  return ( false );
	}
}

function noRightBracket (formField)
{
	// IF EMAIL ADDRESS IN FORM 'user@[255,255,255,0]', THEN CHECK FOR RIGHT BRACKET
	if (formField.indexOf ( '[', 0 ) > -1 && formField.charAt (formField.length - 1) != ']')
	{
	  return ( true );
	}
	else
	{
	  return ( false );
	}
}

function noValidPeriod (formField)
{
	// IF EMAIL ADDRESS IN FORM 'user@[255,255,255,0]', THEN WE ARE NOT INTERESTED
	if (formField.indexOf ( '@', 0 ) > 1 && formField.charAt (formField.length - 1 ) == ']') return ( false );

	// CHECK THAT THERE IS AT LEAST ONE PERIOD IN THE STRING
	if (formField.indexOf ( '.', 0 ) == -1) return ( true );

	return ( false );
}

function noValidSuffix(formField)
{
	// IF EMAIL ADDRESS IN FORM 'user@[255,255,255,0]', THEN WE ARE NOT INTERESTED
	if (formField.indexOf('@', 0) > 1 && formField.charAt(formField.length - 1) == ']')
	{
	  return ( false );
	}

	// CHECK THAT THERE IS A TWO OR THREE CHARACTER SUFFIX AFTER THE LAST PERIOD
	var len = formField.length;
	var pos = formField.lastIndexOf ( '.', len - 1 ) + 1;
	if ( ( len - pos ) < 2 || ( len - pos ) > 4 )
	{
	  return ( true );
	}
	else
	{
	  return ( false );
	}
}