var selindex="";

$(document).ready(function(){

	$(window).bind("load",function(){
		RefreshPosts();
		$('#icon').click(function(){
			window.open("../events.php");
			return false;
		});
	});
	
	
	$('.dtesel').eq(0).html("");
	$('.dtesel').eq(1).html("");
	
	for(i1=1;i1<=31;i1++){
		if(i1==0){opStr="<option selected=\"selected\">";}else{opStr="<option>";}
		$('.dtesel').eq(0).append(opStr+i1+"</option>");
	}
	
	for(i1=1;i1<=12;i1++){
		if(i1==0){opStr="<option selected=\"selected\">";}else{opStr="<option>";}
		$('.dtesel').eq(1).append(opStr+i1+"</option>");
	}
	
	$('.txtbox').val("");
	//$('.txtbox').eq(1).val("dd-mm-yyyy");
	
	$('#pge_mask').FullWindow();
	$('#pge_mask').fadeTo(100,0.8);
	$('#comment_box').centreLI(); 
	$('#login_box').centreLogIn();
	
	$(window).resize(function(){
		$('#pge_mask').FullWindow();
		$('#comment_box').centreLI();
		$('#login_box').centreLogIn();
	});
	
	
	$('#ulo').click(function(){
		$.post("scripts/php/process.php",{func: "logout"},function(){
			window.location="default.php";				  
		});
	});
	
	$('#postinfo').submit(function(){
		if(CheckForVals())
		{
			
			if($('.buttonformat').eq(0).val()=="Update Post")
			{
				//UPDATE
				f1=$('.txtbox').eq(0).val();
				f2=$('.txtbox').eq(1).val();
				f3=$('.txtbox').eq(2).val();
				f4=$('.txtbox').eq(3).val();
				f5=$('.txtbox').eq(4).val();
				
				pstInfo=new Array(f1,f2,f3,f4,f5);
				
				$.post("scripts/php/process.php",{
				func: "update", recordid: selindex, poInfo:pstInfo[0]+
				"{-}"+pstInfo[1]+"{-}"+pstInfo[2]+"{-}"+pstInfo[3]+"{-}"+pstInfo[4]},function(data){
					if(data)
					{
						selindex="";
						$('.buttonformat').eq(0).val("Submit Post");
						$('.txtbox').val("");
						$('#returnresult').text("Record updated successfully.");
						RefreshPosts();
					}
					else
					{
						$('#returnresult').text("Unable to update record.");
					}
				});	
			}
			
			else if($('.buttonformat').eq(0).val()=="Submit Post")
			{
				f1=$('.txtbox').eq(0).val();
				f2=$('.txtbox').eq(1).val();
				f3=$('.txtbox').eq(2).val();
				f4=$('.txtbox').eq(3).val();
				f5=$('.txtbox').eq(4).val();
				
				pstInfo=new Array(f1,f2,f3,f4,f5);
				
				
				$.post("scripts/php/process.php",{
				func: "addto",poInfo:pstInfo[0]+
				"{-}"+pstInfo[1]+"{-}"+pstInfo[2]+"{-}"+pstInfo[3]+"{-}"+pstInfo[4]},function(data){
					if(data)
					{
						$('.txtbox').val("");
						$('#returnresult').text("Record added successfully.");
						RefreshPosts();
					}
					else
					{
						$('#returnresult').text("Unable to add record.");
					}
				});	
			}
		}
		else
		{
			$('#returnresult').text("System currently under going some tests.");	
			//$('#returnresult').text("Please ensure all fields are filled in correctly.");	
		}
		return false;
	});
	
	
	
	
	$('.buttonformat').click(function(){
		if($(this).val()=="No")
		{
			$('#pge_wrap').css("display","none");
		}
		else if($(this).val()=="Yes")
		{
			$.post("scripts/php/process.php",{func: "removerec", recordid: selindex},function(data){
				$('#pge_wrap').css("display","none");
				RefreshPosts();				  
			});
		}
	});
	
	
	
	$('.buttonformat').eq(1).click(function(){
		$('.buttonformat').eq(0).val("Submit Post");									
	});
	
	$('#deletepost').submit(function(){
		return false;
	});
	

	$('#li_prompt').submit(function(){
		$('#returnresult').text("Processing...");
		if(CheckForVals())
		{
			i1=$('.txtbox').eq(0).val();
			i2=$('.txtbox').eq(1).val();
			$.post("scripts/php/chkli.php",{func: "chklog", f1: i1, f2: i2},function(data){
				if(data=="true")
				{
					$('#returnresult').text("Redirecting....");
					window.location="home.php";
				}
				else
				{
					$('#returnresult').text("Please ensure all fields are filled in correctly.");
				}
			});
		}
		else
		{
			$('#returnresult').text("Please ensure all fields are filled in correctly.");
		}
		return false;
	});

});

//Jquery functions
(function($) {
	$.fn.centreLI = function(){
		w_height=$(window).height()/2;
		w_width=$(window).width()/2;
		
		li_height=$(this).height()/2;
		li_width=$(this).width()/2;
		
		li_top=w_height-li_height;
		li_left=w_width-li_width;
		
		$(this).css({
			"top": li_top+"px",
			"left": li_left+"px"
		});
	};
	
	$.fn.centreLogIn = function(){
		w_height=$(window).height()/2;
		li_height=$(this).height()/2;
		li_top=w_height-li_height;
		
		$(this).css("top",li_top+"px");
	};
	
	$.fn.FullWindow = function(){
		w_height=$(window).height();
		w_width=$(window).width();
		$(this).css({
			"width": w_width+"px",
			"height": w_height+"px"
		});
	};
	
})(jQuery);

//javascript functions

function CheckEmail(inputemail) {
	AtPos = inputemail.indexOf("@");
	StopPos = inputemail.lastIndexOf(".");

	if (AtPos == -1 || StopPos == -1)
	{
		return false;
	}
	else
	{
		return true;	
	}

}

function CheckForVals()
{
	j=0
	$('.txtbox').each(function(i){
		if($(this).val()!="")
		{
			j++;	
		}
	});
	if(j==$('.txtbox').size())
	{
		return true;
	}
	else
	{
		return false;	
	}
}

function RefreshPosts()
{
		$('#bloginfo').html("<p>Refreshing Posts</p>");
		$('#bloginfo').css("background-position","center center");
		
		
		$('#bloginfo').load("posts.php #viewposts", function(){
			
			$('#bloginfo').css("background-position","-250px center");
			$('#returnresult').text("");
			
			$('li.ico_remove').click(function(){
				selindex=$(this).children().attr("rel");
				$('#pge_wrap').css("display","block");
				$('#comment_box').centreLI(); 
			});
			
			$('li.ico_edit').click(function(){
				selindex=$(this).children().attr("rel");		
				$.post("scripts/php/process.php",{func: "editrec", recordid: selindex},function(data){
					splData=data.split("{-}");					
					$('.txtbox').eq(0).val(splData[0]);
					$('.txtbox').eq(1).val(splData[1]);
					$('.txtbox').eq(2).val(splData[2]);
					$('.txtbox').eq(3).val(splData[3]);
					$('.txtbox').eq(4).val(splData[4]);
				});
				$('.buttonformat').eq(0).val("Update Post");
			});											 
		});
}