// JavaScript Document

function sleep(milliseconds) {
  var start = new Date().getTime();
  for (var i = 0; i < 1e7; i++) {
    if ((new Date().getTime() - start) > milliseconds){
      break;
    }
  }
}

function showErrMess(mess, id)
{
	 $.ajax({
  type: "GET",
		success : function (t) {
			var htmlData = '<div class="errBlock">'+mess+'</div>';
		  $("#"+id).html(htmlData); 
		}
		});
}

function showConfMess(mess, id)
{
	 $.ajax({
  type: "GET",
		success : function (t) {
			var htmlData = '<div class="confBlock">'+mess+'</div>';
		  $("#"+id).html(htmlData); 
		}
		});
}

	$(function() {
		$("#tabs").tabs();
	});
	
function sDatepicker(id)
{
		$("#"+id).datepicker( {dateFormat: 'dd-mm-yy'} );
}

function loadingAnim(type,id)
{
	 var img;
	 if(type == 1)
		{
			 img = '<p align="center"><img src="/images/loadingAnimation.gif" alt="Loading, please wait ..." /></p>';
		}
		else if(type == 2)
		{
			 img = '<img src="/images/loadingAnimation.gif" alt="Loading, please wait ..." />';
		}
		else if(type == 3)
		{
/*			 img = '<tr><td colspan="6">';
			 img += '<p align="center"><img src="/images/loadingAnimation.gif" alt="Loading, please wait ..." /></p>';
				img += '</td></tr>';
*/		}
		return; //document.getElementById(id).innerHTML = img;
}

$(function() {
$("#dialog").dialog({	bgiframe: true,	height: 140,	modal: true,	autoOpen: false	});
});


function editUserProfile(uid)
{
  uid = parseInt(uid);
		
		$("#dialog").dialog('option', 'width', 500);
  $("#dialog").dialog('option', 'height', 450);
		$("#dialog").dialog('option', 'position', 'center');
  $("#dialog").dialog('option', 'title', 'Edit Profile Details');
  $("#dialog").dialog("open");
  $("#dialog").html('<p align="center"><img src="/images/loadingAnimation.gif" alt="Loading"/></p>');

		
	 $.ajax({
  type: "GET",
		url: "/user/edit/type/modal/uid/"+uid,
		success : function (t) {
			 $("#dialog").html(t);
		}
		});
		
}

function register()
{
	 $("#dialog").dialog('option', 'width', 450);
  $("#dialog").dialog('option', 'height', 450);
		$("#dialog").dialog('option', 'position', 'center');
  $("#dialog").dialog('option', 'title', 'Register to Site');
  $("#dialog").dialog("open");
  $("#dialog").html('<p align="center"><img src="/images/loadingAnimation.gif" alt="Loading"/></p>');
		
		$.ajax({
  type: "GET",
		url: "/user/register/type/modal",
		success : function (t) {
			 $("#dialog").html(t);
		}
		});
}

function registerAccount()
{
	 $("#dialog").prepend('<p align="center"><img src="/images/loadingAnimation.gif" alt="Loading"/></p><p align="center">Registering Account</p>');
		var formdata = $("#registerAccount").serialize();
		
		$.ajax({
  type: "POST",
		url: "/user/register/type/modal",
		data: formdata,
		success : function (t) {
			 $("#dialog").html(t);
				if(document.getElementById("forename").value != "")
				{
					  fn = true;
				}
				else
				{
					  fn = false;
				}
				if(document.getElementById("surname").value != "")
				{
					  sn = true;
				}
				else
				{
					  sn = false;
				}
				if(document.getElementById("email").value != "")
				{
					  em = true;
				}
				else
				{
					  em = false;
				}
				if(document.getElementById("username").value != "")
				{
					  un = true;
				}
				else
				{
					  un = false;
				}
				if(document.getElementById("school").value != "")
				{
					  sch = true;
				}
				else
				{
					  sch = false;
				}
				
				
				if(fn && sn && em && un && sch)
				{
					 var message = '<p>Congratulations you have registered to the site.</p>';
						message += '<p>An email has been sent to your email address.  Please check your email and click the confirmation link to activate your account.</p>';
	     dialogConfirm('400', '200', message);

				}
		}
		});
}

function saveProfileChanges(uid)
{
  uid = parseInt(uid);
		
		
		$("#dialog").prepend('<p align="center"><img src="/images/loadingAnimation.gif" alt="Loading"/></p><p align="center">Saving Changes</p>');
		
		var formdata = $("#editProfileForm").serialize();
				
		$.ajax({
  type: "POST",
		url: "/user/edit/type/modal/uid/"+uid,
		data: formdata,
		success : function (t) {
			 $("#dialog").html(t);
				if(document.getElementById("forename").value != "")
				{
					  fn = true;
				}
				else
				{
					  fn = false;
				}
				if(document.getElementById("surname").value != "")
				{
					  sn = true;
				}
				else
				{
					  sn = false;
				}
				if(document.getElementById("email").value != "")
				{
					  em = true;
				}
				else
				{
					  em = false;
				}
				if(document.getElementById("username").value != "")
				{
					  un = true;
				}
				else
				{
					  un = false;
				}
				
				
				if(fn && sn && em && un)
				{
					 var message = 'Congratulations your User Profile has been updated.';
	     dialogConfirm('auto', 'auto', message);

				}
		}
		});
}

function addEvent(eid, d, m, y)
{
  d = parseInt(d);
		m = parseInt(m);
		y = parseInt(y);
		
		$("#dialog").dialog('option', 'width', 750);
  $("#dialog").dialog('option', 'height', 550);
		$("#dialog").dialog('option', 'position', 'center');
  $("#dialog").dialog('option', 'title', 'Event Management - '+d+'/'+m+'/'+y);
  $("#dialog").dialog("open");
  $("#dialog").html('<p align="center"><img src="/images/loadingAnimation.gif" alt="Loading"/></p>');
		
		$.ajax({
  type: "GET",
		url: "/calendar/event-management/eid/"+eid+"/d/"+d+"/m/"+m+"/y/"+y,
		success : function (t) {
			 $("#dialog").html(t);
				
    setTimeout ( loadTinyMCE, 1 );
				sDatepicker("sDate");
				sDatepicker("eDate");

		}
		
		});
}

function saveEvent(eid)
{
  eid = parseInt(eid);
		
		
		$("#dialog").prepend('<p align="center"><img src="/images/loadingAnimation.gif" alt="Loading"/></p><p align="center">Saving Changes</p>');
		
		tinyMCE.triggerSave();
		var formdata = $("#eventManagementForm").serialize();
				
		$.ajax({
  type: "POST",
		url: "/calendar/event-management/eid/"+eid,
		data: formdata,
		success : function (t) {
			 $("#dialog").html(t);
				if(document.getElementById("subject").value != "")
				{
					  subj = true;
				}
				else
				{
					  subj = false;
				}
				if(document.getElementById("location").value != "")
				{
					  loc = true;
				}
				else
				{
					  loc = false;
				}
				if(document.getElementById("etype").value != 0)
				{
					  et = true;
				}
				else
				{
					  et = false;
				}
				if(document.getElementById("sDate").value != "")
				{
					  sd = true;
				}
				else
				{
					  sd = false;
				}
				if(document.getElementById("eDate").value != "")
				{
					  ed = true;
				}
				else
				{
					  ed = false;
				}
				if(document.getElementById("desc").value != "")
				{
					  desc = true;
				}
				else
				{
					  desc = false;
				}
				
				
				if(subj && loc && et && sd && ed && desc)
				{
					 var message = 'Congratulations the event has been saved to the calendar.';
	     dialogConfirm('auto', 'auto', message);
				}
				else
				{
					 setTimeout ( loadTinyMCE, 1 );
				}
		}
		});
}

function saveTeamEvent(eid,etype)
{
	 eid = parseInt(eid);
		etype = parseInt(etype);
		var user = $('#attendee').val();
		var attendee = "";
		
		if(user > 0)
		{
			 attendee = "/attendee/"+user;
		}
		
		$("#dialog").prepend('<p align="center"><img src="/images/loadingAnimation.gif" alt="Loading"/></p><p align="center">Saving Changes</p>');
		
		tinyMCE.triggerSave();
		var formdata = $("#teamNumbersForm").serialize();
				
		$.ajax({
  type: "POST",
		url: "/calendar/show-event/eid/"+eid+"/etype/"+etype+attendee,
		data: formdata,
		success : function (t) {
			 $("#dialog").html(t);
				showConfMess("Congratulations - You have successfully booked your teams onto this event","team-preamble");
		}
		});
}

function showEvent(eid, title, etype)
{
	 eid = parseInt(eid);
		etype = parseInt(etype);
		
		if(etype == 3)
		{
			 width = 750;
				height = 550;
		}
		else
		{
			 width = 650;
				height = 500;
		}
		
		$("#dialog").dialog('option', 'width', width);
  $("#dialog").dialog('option', 'height', height);
		$("#dialog").dialog('option', 'position', 'center');
		$("#dialog").dialog('option', 'resizable', false);
  $("#dialog").dialog('option', 'title', title);
  $("#dialog").dialog("open");
  $("#dialog").html('<p align="center"><img src="/images/loadingAnimation.gif" alt="Loading"/></p>');
		
		$.ajax({
  type: "GET",
		url: "/calendar/show-event/eid/"+eid+"/etype/"+etype,
		success : function (t) {
			 $("#dialog").html(t);
		}
		});
		
}

function dialogConfirm(width, height, message)
{
	 message = '<div id="dialogConfirmMess"><span>'+message+'</span></div>';
	
	 $("#dialog").dialog("close");
		$("#dialog").dialog('option', 'width', width);
  $("#dialog").dialog('option', 'height', height);
		$("#dialog").dialog('option', 'position', 'center');
  $("#dialog").dialog('option', 'title', 'Operation Successful');
		$('#dialog').dialog('option', 'buttons', { "Ok": function() { $(this).dialog("close"); } });
		$('#dialog').dialog('option', 'resizable', false);
  $("#dialog").dialog("open");
		$("#dialog").html(message);
		$('#dialog').bind('dialogclose', function(event, ui) {
  location.href="";
		});
}

function bookEvent(eid, uid)
{
		eid = parseInt(eid);
		uid = parseInt(uid);
		var user = $('#attendee').val();
		var attendee = "";
		
		if(user > 0)
		{
			 attendee = "/attendee/"+user;
		}
		
		$.getJSON("/calendar/book-event/format/json/uid/"+uid+"/eid/"+eid+attendee, function(data)
  {
			 if(!data.result)
				{
					 return false;
				}
				else
				{
					 var confirmBooking = '<div class="confBlock">You have booked a place on this event. An email has been sent to you to confirm your booking.</div>';
					 $("#book-event").html(confirmBooking);
						if(data.places > 0)
						{
							 if(data.places > 0)
								{
									 var freePlaces = data.places - data.attendees;
										if(freePlaces > 0)
										{
  							   var updatePlaces = freePlaces + ' / ' + data.places
  		  						$("#places").html(updatePlaces);
										}
										else
										{
											 $("#places").html('0');
										}
								}
						}
				}
		});
}

function deleteEvent(eid)
{
	 eid = parseInt(eid);
		var confDel = confirm("Are you sure you wish to delete this event");
		if(confDel)
		{
			 $.getJSON("/calendar/delete-event/format/json/eid/"+eid, function(data)
				{
  		  if(!data.result)
		  		{
			  		 return false;
		  		}
		  		else
			  	{
							 dialogConfirm('auto', 'auto', "The event has been deleted")
						}
  		});
		}
}

function viewAttendees(eid, uid, etype)
{
	 eid = parseInt(eid);
		uid = parseInt(uid);
		etype = parseInt(etype);
		var uidUrl = "";
		if(uid > 0)
		{
			 uidUrl = "/uid/"+uid;
		}
		width = 750;
		if(etype == 3)
		{
			 width = 950;
		}
		
		$("#dialog").dialog('option', 'width', width);
		$("#dialog").dialog('option', 'position', 'center');
		
		$.ajax({
  type: "GET",
		url: "/calendar/view-attendees/eid/"+eid+"/etype/"+etype+uidUrl,
		success : function (t) {
			 $("#calendar-event").html(t);
		}
		});
}

function removeAttendee(uid, eid, etype)
{
	 uid = parseInt(uid);
		eid = parseInt(eid);
		
		var confDel = confirm("Are you sure you wish to remove this person from attending this event?");
		if(confDel)
		{
			 viewAttendees(eid, uid, etype)
		}
}

function showTeams(etype,d,m,y)
{
	 etype = parseInt(etype);
		d = parseInt(d);
		m = parseInt(m);
		y = parseInt(y);
		
		$.ajax({
  type: "GET",
		url: "/calendar/add-teams/etype/"+etype+"/d/"+d+"/m/"+m+"/y/"+y,
		success : function (t) {
			 $("#places-teams").html(t);
		}
		});
}

function addMoreTeams(id,etype,d,m,y)
{
	 id = parseInt(id);
		id2 = id+1;
	 etype = parseInt(etype);
		d = parseInt(d);
		m = parseInt(m);
		y = parseInt(y);
	
	 $.ajax({
  type: "GET",
		url: "/calendar/add-teams/etype/"+etype+"/d/"+d+"/m/"+m+"/y/"+y+"/nteams/"+id,
		success : function (t) {
			 $("#team"+id).html(t);
				document.getElementById("num_teams").value = id2;
		}
		});
}

function emailTeamMember(cid,name)
{
	 cid = parseInt(cid);
		
		$("#dialog").dialog('option', 'width', 650);
  $("#dialog").dialog('option', 'height', 600);
		$("#dialog").dialog('option', 'position', 'center');
  $("#dialog").dialog('option', 'title', 'Email '+name);
  $("#dialog").dialog("open");
  $("#dialog").html('<p align="center"><img src="/images/loadingAnimation.gif" alt="Loading"/></p>');
		
		$.ajax({
  type: "GET",
		url: "/contacts/email/cid/"+cid,
		success : function (t) {
			 $("#dialog").html(t);
		}
		});
}

function sendEmail(cid)
{
	 cid = parseInt(cid);
		
		$("#dialog").prepend('<p align="center"><img src="/images/loadingAnimation.gif" alt="Loading"/></p><p align="center">Registering Account</p>');
		var formdata = $("#emailContactForm").serialize();
		
		$.ajax({
  type: "POST",
		url: "/contacts/email/cid/"+cid,
		data: formdata,
		success : function (t) {
			 $("#dialog").html(t);
				if(document.getElementById("subject").value != "")
				{
					  s = true;
				}
				else
				{
					  s = false;
				}
				if(document.getElementById("body").value != "")
				{
					  b = true;
				}
				else
				{
					  b = false;
				}
				
				if(s && b)
				{
					 var message = '<p>Congratulations your email has been sent.</p>';
	     dialogConfirm('200', '200', message);

				}
		}
		});
}

function listBasicUsers(role)
{
	 $("#dialog").dialog('option', 'width', 650);
  $("#dialog").dialog('option', 'height', 450);
		$("#dialog").dialog('option', 'position', 'center');
  $("#dialog").dialog('option', 'title', 'List Basic Users');
  $("#dialog").dialog("open");
  $("#dialog").html('<p align="center"><img src="/images/loadingAnimation.gif" alt="Loading"/></p>');
		
		$.ajax({
  type: "GET",
		url: "/user/level/role/"+role,
		success : function (t) {
			 $("#dialog").html(t);
		}
		});
}

function myFavourites(sid, id)
{
	 sid = parseInt(sid);
		id = parseInt(id);
		
		if(sid > 0 && id > 0)
		{
			 $("#myFavourites").html('<img src="/images/loading_circle.gif" title="Loading" alt="Loading"  />');
			 $.getJSON("/favourites/manage/format/json/sid/"+sid+"/id/"+id, function(data)
				{
  		  if(data.result)
		  		{
			  		 $("#myFavourites").html('<img title="'+data.label+'Favourites" alt="Add to Favourites" src="/images/icons/24x24/star_'+data.type+'.jpg" />');
								
								if($('#fav_'+data.fid).length ) 
								{
          $('#fav_'+data.fid).remove();
										var rowCount = $('#fav-list tr').length;
								  if(rowCount == 1)
							 		{ 
												$("#favourites").html('<p>You have no favourite pages listed.</p>');
								  }
        } 
		  		}
  		});
		}
}

function viewFavourites()
{
	 $("#dialog").dialog('option', 'width', 750);
  $("#dialog").dialog('option', 'height', 550);
		$("#dialog").dialog('option', 'position', 'center');
  $("#dialog").dialog('option', 'title', 'Show My Favourite Pages');
  $("#dialog").dialog("open");
  $("#dialog").html('<p align="center"><img src="/images/loadingAnimation.gif" alt="Loading"/></p>');
		
		$.ajax({
  type: "GET",
		url: "/favourites/index/format/modal",
		success : function (t) {
			 $("#dialog").html(t);
		}
		});
}



