// JavaScript Document

// Get Data Functions

function GetTest(){
	var resultStr = "";
	
	$.ajax({
		type : 'POST',
		url : 'dbget.php',
		dataType : 'json',
		data: {get: "test"},
		success : function(result){
								if(result){
									if(isAdmin){ 
										resultStr = resultStr + "<table width='100%'>"
										resultStr = resultStr + "<thead><tr><th colspan='6' align='left'>Manage Test</th></tr></thead>";
										for(i=0;i<result.length;i++){
											var id = result[i].id;
											var foo = result[i].foo;
											var bar = result[i].bar;
											var bazz = result[i].bazz;
											
											resultStr = resultStr + "<tr id='row_" + id + "'>";
											resultStr = resultStr + "<td>" + (i+1) + "</td><td>" + foo + "</td><td>" + bar + "</td><td>" + bazz + "</td>";
											resultStr = resultStr + '<td><a href="javascript:;" onclick="currentRow = ' + id + '; fillTestbox('+id+'); $.fn.colorbox({width:\'80%\', inline:true, href:\'#EditTestDiv\', open:true});">Edit</a></td>';
											resultStr = resultStr + '<td><a href="javascript:;" onclick="DeleteDb(\'test\',' + id + ');">Delete</a></td>';
										}
										resultStr = resultStr + "</table>";
										
										updateResults(resultStr); 
									}
									else { LoadModules(result); }
								}
								else{
									displayResults("No data found");
								}
		          },
		error : 	function(XMLHttpRequest, textStatus, errorThrown) {
               	$('#results').html("Error: " + textStatus);
            	}
  });	
}

function GetArticle(gType){
	var resultStr = "";
	
	$.ajax({
		type : 'POST',
		url : 'dbget.php',
		dataType : 'json',
		data: {get: "article", type: gType},
		success : function(result){
								if(result){
									if(isAdmin){ 
										resultStr = resultStr + "<table class='admin-table' cellspacing='1' cellpadding='2' width='100%'>"
										resultStr = resultStr + "<thead><tr><th colspan='7' align='left'>Manage News</th></tr></thead>";
										for(i=0;i<result.length;i++){
											var id = result[i].article_id;
											var type = result[i].type;
											var title = result[i].title;
											var content = result[i].content;
											var date = result[i].posted_date;
												
											resultStr = resultStr + "<tr class='rowA' id='row_" + id + "'>";
											resultStr = resultStr + "<td>" + (i+1) + "</td><td>" + type + "</td><td>" + title + "</td><td class='hidden'>" + content + "</td><td>" + date + "</td>";
											resultStr = resultStr + '<td><a href="javascript:;" onclick="currentRow = ' + id + '; fillArtbox('+id+'); $.fn.colorbox({width:\'80%\', inline:true, href:\'#EditArticleDiv\',open:true});">Edit</a></td>';
											resultStr = resultStr + '<td><a href="javascript:;" onclick="DeleteDb(\'article\',' + id + ');">Delete</a></td></tr>';
											resultStr = resultStr + "<tr class='rowB'><td colspan='7'>" + content + "</td></tr>";
										}
										resultStr = resultStr + "</table>";
										
										updateResults(resultStr);	
									}
									else { LoadModules(result); }
								}
								else{
									displayResults("No data found");
								}
		          },
		error : 	function(XMLHttpRequest, textStatus, errorThrown) {
               	$('#results').html("Error: " + textStatus);
            	}
  });	
	
}

// Post Data Functions
function PostTest(pFoo, pBar, pBazz){
		
	var postObj = {
			action: "test",
			foo: pFoo,
			bar: pBar,
			bazz: pBazz
	}
		
	Post(postObj);
}

function PostArticle(pType, pTitle, pContent){

	var postObj = {
		action: "article",
		type: pType,
		title: pTitle,
		content: pContent
	}
	
	Post(postObj);
	
}

function Post(pData){
	$.ajax({
		type : 'POST',
		url : 'dbpost.php',
		dataType : 'json',
		data: pData,
		success : function(data){
									$('#results').html(data.msg);
		            },
			error : 	function(XMLHttpRequest, textStatus, errorThrown) {
                	$('#results').html("Error: " + textStatus);
            }
  });	
}

// Delete Functions
function DeleteData(dAction, dRow){
		var postObj = {
		action: dAction,
		row: dRow
	}
	
	Delete(postObj);
}

function Delete(pData){
	$.ajax({
		type : 'POST',
		url : 'dbdelete.php',
		dataType : 'json',
		data: pData,
		success : function(data){
									$('#results').html(data);
		            },
			error : 	function(XMLHttpRequest, textStatus, errorThrown) {
                	$('#results').html("Error: " + textStatus);
            }
  });	
}

// Edit Functions
function EditTest(pRow, pFoo, pBar, pBazz){
		
	var postObj = {
			action: "test",
			row: pRow,
			foo: pFoo,
			bar: pBar,
			bazz: pBazz
	}
		
	Edit(postObj);
}

function EditArticle(pRow, pType, pTitle, pContent){

	var postObj = {
		action: "article",
		row: pRow,
		type: pType,
		title: pTitle,
		content: pContent
	}
	
	Edit(postObj);
	
}

function Edit(pData){
	$.ajax({
		type : 'POST',
		url : 'dbedit.php',
		dataType : 'json',
		data: pData,
		success : function(data){
									$('#results').html(data.msg);
		            },
			error : 	function(XMLHttpRequest, textStatus, errorThrown) {
                	$('#results').html("Error: " + textStatus);
            }
  });	
}

function logout(){
	$.getJSON("logout.php",
  	function(data){
			$('.username').html("Guest");
			$('.logout').hide();      
			isAdmin = false;
			$('.admin').addClass('hidden');
			$('.admin').addClass('invisible');			
    });
	window.location = "admin.html";
}
