var currentEntryNum=0;

$(document).ready(function(){
  $.preloadCssImages(); //A jQuery plugin for preloading images.
});







function addAnotherEntry()
{
	currentEntryNum++;
	var entryHTML = $('.addUserEntry').html();
	entryHTML = entryHTML.replace(/id0/g,"id"+ currentEntryNum);    //A Regular Expression to replace all IDs with proper numbering
	entryHTML = entryHTML.replace(/addUsername0/g,"addUsername"+ currentEntryNum);  //A Regular Expression to replace all addUsernames with proper numbering
	$('#main').append(entryHTML);
}





function addUser()
{
	var addUserName =	$('#addUserName').val();
	var addRealName =	$('#addRealName').val();

	if (!(addUserName || addRealName)) {
	$('#addError').empty();
	$('#addError').append('Please enter a user name.');
	}
	else {
		setUpBigContentArea();
		var url = "form.php?addUserName=" + escape($('#addUserName').val() ) + "&addRealName=" + escape($('#addRealName').val());
		
		$(".content").load(url, function(){
				});
				
	}
}




function findUser()
{
	var findUserName =	$('#findUserName').val();
	var findRealName =	$('#findRealName').val();


	if (!(findUserName || findRealName)) {
		$('#findError').empty();
		$('#findError').append('Please enter a user name.');
	}
	else // USER NAME OR REAL NAME ENTERED - START THE SEARCH
		{
				setUpBigContentArea();
				
				
				//$('.content').append(findUserName + ' and ' + findRealName);
				
				
				$.get("ajaxFindUser.php", {
					findUserName: findUserName,
					findRealName: findRealName
				}, function(data){
					$('.content').empty();
					$('.content').append(data);
				});
				
				
			
		}

}
function setUpBigContentArea()
{
				$('.frontpageContentBox').fadeOut("fast");
				//$('.frontpageContentBox').css('visibility', 'collapse');
				$('.frontpageContentBox').css('height', '0px'); //Even though the box has been collapsed, it still occupies its original height.		
				$('#hiddenBox').slideDown("slow");
				$('#hiddenBox').css('visibility', 'visible');
				$('#contentFind').css('visibility', 'visible');
}



function addUserSubmit()
{
	var userName =	$('#hiddenUserName').val();
	var realName =	$('#hiddenRealName').val();
	var dynamicIteratorID=0;
	var dynamicIteratorAddUsername=0;
	
	var formatDynamicEntries="add_submit.php?userName=" + userName + "&realName=" + realName;
		for (var x=0;x<20;x++)
		{
			dynamicIteratorID = $("#id"+x).val();
			dynamicIteratorAddUsername = $('#addUsername'+x).val();
			
			if (dynamicIteratorID && dynamicIteratorAddUsername) {
				formatDynamicEntries += "&id" + x + "=" + dynamicIteratorID;
				formatDynamicEntries += "&addUsername" + x + "=" + dynamicIteratorAddUsername;
			}
			
		}
	$.get(formatDynamicEntries,  function(data){
					$('.content').empty();
					$('.content').append(data);
				});
	
	
	/*$.get("add_submit.php", {
					userName: userName,
					realName: realName
				}, function(data){
					//$('.content').empty();
					$('.content').append(data);
				});*/
	
}






function resetAll ()
{
	//THIS FUNCTION SHOULD RESET EVERYTHING BACK TO NORMAL - COULD ALSO BE EXECUTED ON BACK BUTTON PRESS
	// Just refreshes the page for now (CSS / Display issues with the following code)
				/*$('.frontpageContentBox').fadeIn("fast");
				//$('.frontpageContentBox').css('visibility', 'collapse');
				$('.frontpageContentBox').css('height', '0px'); //Even though the box has been collapsed, it still occupies its original height.		
				$('#hiddenBox').slideUp("slow");
				$('#hiddenBox').css('visibility', 'hidden');
				$('#contentFind').css('visibility', 'hidden');*/

location.reload(true);
}



