function loginAdmin() {


		if (!isLoggedIn()) {

			var loginData = {};
			loginData.loginUsername = prompt('Username:','');
			loginData.loginPassword = prompt('Password:','');
			$.post("events.php?command=loginAdmin",loginData,function(data) {
				

			});

		}
}

function deleteEvent(id) {


		var answer = confirm("Are you sure you wish to delete this event?");

		var eventData = {};

		eventData.id = id;

		if (answer) {
			
			$.post("events.php?command=deleteEvent",eventData,function(data) {

				mainTable();
			});

		}

}

function isLoggedIn() {


        var data = $.ajax({
                type: 'GET',
                url: 'events.php?command=isLoggedIn',
                dataType: 'json',
                data: {},
                async: false
        }).responseText;


        if (data == '1') {
                return true;
        } else {

                return false;
        }

}

function mainTable() {

	if (isLoggedIn()) {
	
		isLoggedInVar = true;	
	} else {
		isLoggedInVar = false;
	}
        
	var postData = {};

        postData.speaker = $("#speakerSelect").val();
        postData.audience = $("#audienceSelect").val();
        postData.topic = $("#topicSelect").val();
        postData.city =  $("#citySelect").val();
	postData.keyword = $("#keyword").val();
	postData.endDate = $("#endDate").val();
	postData.startDate = $("#startDate").val();


	var newItems = new Array();

	$.getJSON("events.php?command=getNew",function(data) {

		$.each(data, function(i,data) {

			newItems.push(data.id);
		});
	});

	$.post("events.php?command=filterQuery",postData,function(data) {


                        $("#myTable tr:gt(0)").remove();

                        $.each(data, function(i,data) {

                                var tr = document.createElement("tr");
				tr.setAttribute("id",data.id);

				var td = document.createElement("td");

	
				var checkbox = document.createElement("input");
				checkbox.setAttribute("type","checkbox");
				checkbox.setAttribute("name","print");
				checkbox.setAttribute("value",data.id);

				td.appendChild(checkbox);


                                if (jQuery.inArray(data.id,newItems) >= 0) {

					var newStar = document.createElement("img");
					newStar.setAttribute("src","images/newStar2.gif");
					newStar.setAttribute("alt","New!");
					newStar.setAttribute("title","Item is new in last 30 days.");
					td.appendChild(newStar);
				}
		
				if (isLoggedInVar) {	

					trashImg = document.createElement("img");
					trashImg.setAttribute("id","trashImg"+data.id);
					trashImg.setAttribute("onClick","deleteEvent(\""+data.id+"\")");
                                	trashImg.setAttribute("src","images/trash.png");
                                	trashImg.setAttribute("alt","Delete Event.");
                                	trashImg.setAttribute("title","Delete Event.");
                                	td.appendChild(trashImg);


                                	editImg = document.createElement("img");
                                	editImg.setAttribute("src","images/edit.png");
					editImg.setAttribute("onClick","window.open(\"edit.php?id="+data.id+"\")");
                                	editImg.setAttribute("alt","Edit Event.");
                                	editImg.setAttribute("title","Edit Event.");
                                	td.appendChild(editImg);

				}

				tr.appendChild(td);

                                $.each(data, function(i,data1) {

					
					if (i != "id" && i != "moderated" && i != "paidEvent" && i != "requiresRSVP" && i != "fundraiser") {
                                        	var td = document.createElement("td");

                                                td.appendChild(document.createTextNode(data1));

						if (i == "title" && data.paidEvent == 1) {

							dollarImg = document.createElement("img");
							dollarImg.setAttribute("src","images/dollarSmall.png");
							dollarImg.setAttribute("alt"," Requires Fee. ");
							dollarImg.setAttribute("title","Event requires a fee.");
							td.appendChild(dollarImg);
						} 

                                                if (i == "title" && data.requiresRSVP == 1) {

                                                        rsvpImg = document.createElement("img");
                                                        rsvpImg.setAttribute("src","images/telephoneSmall.png");
							rsvpImg.setAttribute("alt"," Event Requires RSVP. ");
							rsvpImg.setAttribute("title", "Event Requires an RSVP.");
                                                        td.appendChild(rsvpImg);
                                                }

						if (i == "title" && data.fundraiser == 1) {

                                                        fundraiserImg = document.createElement("img");
                                                        fundraiserImg.setAttribute("src","images/fundraiser.png");
                                                        fundraiserImg.setAttribute("alt"," Event is a fundraiser. ");
                                                        fundraiserImg.setAttribute("title", "Event is a fundraiser.");
                                                        td.appendChild(fundraiserImg);

						}

                                        	tr.appendChild(td);
                                        }
                                });

                                $("#myTable tbody").append(tr);

                        });

                        $("#myTable").trigger("update");

			var sorting = [[2,0]];
			$("#myTable").trigger("sorton",[sorting]);

			$("#myTable tr").mouseover(function() {
				$(this).addClass("over");
			
			});

			$("#myTable tr").mouseout(function() {
				$(this).removeClass("over");
			});


			$.getJSON("events.php?command=getNew",function(data) {


			});

			//add the click attribute to all td elements in the mainTable
			//that are not the first child of the tr element. 
			$("#myTable tr td").not(":first-child").click(function() {

				$("#printEventDiv").val($(this).parent().attr("id"));
				eventDialog($(this).parent().attr("id"));

			});

  
	},"json");



}

function populateStateSelect() {


	$.getJSON("events.php?command=getStateStore",function(data) {


		$.each(data, function(key, value) {
			//var option = document.createElement("option");
			//option.val = value.id
			//option.appendChild(document.createTextNode(value.state));
			//$("#stateSelect").append(option);
			$("#stateSelect").addOption(value.id, value.state);
			//Default select to Illinois
			$("#stateSelect").selectOptions("21");
		});
	});

}

function populateAudienceSelect() {

	$.getJSON("events.php?command=getAudienceStore",function(data) {

		$.each(data, function(key, value) {
			
			//var option = document.createElement("option");
			//option.val = value.id;
			//option.appendChild(document.createTextNode(value.audience));
			//$("#audienceSelect").append(option);

			$("#audienceSelect").addOption(value.id, value.audience);
			$("#audienceSelect").selectOptions("All");


		});

	});
}

function populateTopicSelect() {

        $.getJSON("events.php?command=getTopicStore",function(data) {

                $.each(data, function(key,value) {

                        var option = document.createElement("option");
                        option.val = value.id;
                        option.appendChild(document.createTextNode(value.topic));
                        $("#topicSelect").append(option);
                });
        });
}

function populateCitySelect() {

        $.getJSON("events.php?command=getCityStore",function(data) {

                $.each(data, function(key,value) {

                        var option = document.createElement("option");
                        option.val = value.id;
                        option.appendChild(document.createTextNode(value.city));
                        $("#citySelect").append(option);
                });
        });
}

function populateSpeakerSelect() {

        $.getJSON("events.php?command=getSpeakerStore",function(data) {

                $.each(data, function(key,value) {

                        var option = document.createElement("option");
                        option.val = value.id;
                        option.appendChild(document.createTextNode(value.speaker1));
                        $("#speakerSelect").append(option);
                });
        });
}

function eventDialog(id) {

	buildEventTable($("#eventTable"), id);
	$("#modalDiv").show("fast");
	$("#eventDiv").slideDown("fast");
}

function closeEventDialog() {

	//$("#eventDiv").hide("slide", {direction: "left"}, 500);
	$("#eventDiv").hide("slide");	
	$("#modalDiv").hide("fast");
	$("#eventTable tr:gt(0)").remove();


}


function buildEventTable(element, id) {

	$.getJSON("events.php?command=getEvent&id="+id,function(data) {

                var trTitle = document.createElement("tr");
                var titleHeader = document.createElement("td")
                var title = document.createElement("td");

                title.appendChild(document.createTextNode(data.title));
                titleHeader.appendChild(document.createTextNode("Title"));
		titleHeader.setAttribute("class","eventTitleHeader");
		title.setAttribute("class","eventTitleHeader");
                trTitle.appendChild(titleHeader);
                trTitle.appendChild(title);
                element.append(trTitle);

                var trSponsor = document.createElement("tr");
                var sponsorHeader = document.createElement("td")
                var sponsor = document.createElement("td");
		sponsorHeader.setAttribute("class","eventTableHeader");
        	sponsor.setAttribute("class","eventTableText"); 
	 	sponsor.appendChild(document.createTextNode(data.sponsors));
                sponsorHeader.appendChild(document.createTextNode("Sponsor(s)"));

                trSponsor.appendChild(sponsorHeader);
                trSponsor.appendChild(sponsor);
                element.append(trSponsor);

                var trLocation = document.createElement("tr");
                var locationHeader = document.createElement("td")
                var location = document.createElement("td");
              	location.setAttribute("class","eventTableText"); 
	       	//document.createTextNode(data.address1 + " " + data.address2 + " " data.address3);

		location.appendChild(document.createTextNode(data.address1));
		location.appendChild(document.createElement("br"));
		location.appendChild(document.createTextNode(data.address2));
		location.appendChild(document.createElement("br"));
		location.appendChild(document.createTextNode(data.address3));
		location.appendChild(document.createElement("br"));
		location.appendChild(document.createTextNode(data.city+", "+data.state+" "+data.zip));
                locationHeader.appendChild(document.createTextNode("Location"));
		locationHeader.setAttribute("class","eventTableHeader");	
                trLocation.appendChild(locationHeader);
                trLocation.appendChild(location);
                element.append(trLocation);

                var trTime = document.createElement("tr");
                var timeHeader = document.createElement("td")
                var time = document.createElement("td");
		timeHeader.setAttribute("class","eventTableHeader");
		time.setAttribute("class","eventTableText");
                time.appendChild(document.createTextNode(data.date));
                timeHeader.appendChild(document.createTextNode("Time"));

                trTime.appendChild(timeHeader);
                trTime.appendChild(time);
                element.append(trTime);

                var trSpeaker = document.createElement("tr");
                var speakerHeader = document.createElement("td")
                var speaker = document.createElement("td");
		speakerHeader.setAttribute("class","eventTableHeader");
		speaker.setAttribute("class","eventTableText");
                speaker.appendChild(document.createTextNode(data.speakers));
                speakerHeader.appendChild(document.createTextNode("Speaker(s)"));

                trSpeaker.appendChild(speakerHeader);
                trSpeaker.appendChild(speaker);
                element.append(trSpeaker);

                var trTopic = document.createElement("tr");
                var topicHeader = document.createElement("td")
                var topic = document.createElement("td");
		topic.setAttribute("class","eventTableText");
		topicHeader.setAttribute("class","eventTableHeader");
                topic.appendChild(document.createTextNode(data.topic));
                topicHeader.appendChild(document.createTextNode("Topic"));
		topicHeader.setAttribute("class","eventTableHeader");
                trTopic.appendChild(topicHeader);
                trTopic.appendChild(topic);
                element.append(trTopic);

                var trAudience = document.createElement("tr");
                var audienceHeader = document.createElement("td")
                var audience = document.createElement("td");
		audience.setAttribute("class","eventTableText");
		audienceHeader.setAttribute("class","eventTableHeader");
                audience.appendChild(document.createTextNode(data.audience));
                audienceHeader.appendChild(document.createTextNode("Audience"));

                trAudience.appendChild(audienceHeader);
                trAudience.appendChild(audience);
                element.append(trAudience);

		if ((data.paidEvent == 1) || (data.requiresRSVP == 1)) {

			var trNotes = document.createElement("tr");
			var notesHeader = document.createElement("td");
			var notes = document.createElement("td");
			notesHeader.setAttribute("class","eventTableHeader");
			notesHeader.appendChild(document.createTextNode("Notes"));

			if (data.paidEvent == 1) {
		
				dollarImg = document.createElement("img");
                                dollarImg.setAttribute("src","images/dollarSmall.png");
                                dollarImg.setAttribute("alt"," Requires Fee. ");
                                dollarImg.setAttribute("title","Event requires a fee.");
				notes.appendChild(dollarImg);

			}

			if (data.requiresRSVP == 1) {

				rsvpImg = document.createElement("img");
                                rsvpImg.setAttribute("src","images/telephoneSmall.png");
                                rsvpImg.setAttribute("alt"," Event Requires RSVP. ");
                                rsvpImg.setAttribute("title", "Event Requires an RSVP.");
				notes.appendChild(rsvpImg);

			}


			if (data.fundraiser == 1) {

                                fundraiserImg = document.createElement("img");
      				fundraiserImg.setAttribute("src","images/fundraiser.png");
                                fundraiserImg.setAttribute("alt"," Event is a fundraiser. ");
                                fundraiserImg.setAttribute("title", "Event is a fundraiser.");
                                notes.appendChild(fundraiserImg);

			}

			trNotes.appendChild(notesHeader);
			trNotes.appendChild(notes);
			element.append(trNotes);
		}

		var trDescription = document.createElement("tr");
		var descriptionHeader = document.createElement("td");
		var description = document.createElement("td");
		description.setAttribute("class","eventTableText");
		descriptionHeader.setAttribute("class","eventTableHeader");
		descriptionHeader.innerHTML = "<br />Summary";
	
		description.innerHTML = data.description;
		trDescription.appendChild(descriptionHeader);
		trDescription.appendChild(description);
		element.append(trDescription);


	});
}

function getValue(varname)
{
  // First, we load the URL into a variable
  var url = window.location.href;

  // Next, split the url by the ?
  var qparts = url.split("?");

  // Check that there is a querystring, return "" if not
  if (qparts.length == 0)
  {
    return "";
  }

  // Then find the querystring, everything after the ?
  var query = qparts[1];

  // Split the query string into variables (separates by &s)
  var vars = query.split("&");

  // Initialize the value with "" as default
  var value = "";

  // Iterate through vars, checking each one for varname
  for (i=0;i<vars.length;i++)
  {
    // Split the variable by =, which splits name and value
    var parts = vars[i].split("=");
    
    // Check if the correct variable
    if (parts[0] == varname)
    {
      // Load value into variable
      value = parts[1];

      // End the loop
      break;
    }
  }
  
  // Convert escape code
  value = unescape(value);

  // Convert "+"s to " "s
  value.replace(/\+/g," ");

  // Return the value
  return value;
}


