/**
 * AJAX Functions
 *
 * Common ajax functions
 *
 * @author gUncle Development [http://www.guncle.com]
 * @version 2.0 2/11/2010
 */


/**
 * loads prayer/praise into editable form for editor
 * @param note = id of note to be edited
 */
function editorNote(note) {
	loading(1);
	$.ajax({
		type: "POST",
		url: "ajax.php",
		data: ({
			action: "editorNote",
			note: note
		}),
		cache: false,
		dataType: "text",
		complete: function(){ loading(0); },
		success: function(response){
			var res = splitResponse(response);
			if( res ) {
				var form = '<br /><textarea id="noteForm_'+res['id']+'" rows="3" cols="75">'+res['text']+'</textarea>';
				form += '<br /><input type="button" class="button" value="Update" onclick="saveEditorNote(\''+res['id']+'\');" /><br />';
				$("#note_"+res['id']).html(form);
			}
		}
	});
}


/**
 * saves list sort order
 * @param table = table of list
 * @param id = id of list item
 * @param direction = up or down
 */
function listSort(table,id,direction) {
	loading(1);
	var order = '';
	var rows = document.getElementById('listTable').getElementsByTagName('tr');
	for( var i=0; i<rows.length; i++ ) {
		if( rows[i].id.substr(0,9) == 'listItem_' ) {
			order += rows[i].id.substr(9)+'|';
		}
	}
	order = order.substr(0,order.length-1);
	$.ajax({
		type: "POST",
		url: "ajax.php",
		data: ({
			action: "listSort",
			table: table,
			order: order,
			id: id.substr(9),
			direction: direction
		}),
		cache: false,
		dataType: "text",
		complete: function(){ loading(0); },
		success: function(response){
			var res = splitResponse(response);
			if( res['list'] ) {
				$("#items").html(res['list']);
			}
		}
	});
}


/**
 * sorts html table
 * @param type = type of column for setting session variable
 * @param column = column name to sort
 */
function sortTable(type,column) {
	$.ajax({
		type: "POST",
		url: "ajax.php",
		data: ({
			action: "sortTable",
			type: type,
			column: column
		}),
		cache: false,
		dataType: "text",
		success: function(){
			window.location.reload();
		}
	});
}



/**
 * marks note as viewed
 * @param id = id of user
 */
function viewedNote(id) {
	loading(1);
	$.ajax({
		type: "POST",
		url: "ajax.php",
		data: ({
			action: "viewedNote",
			id: id
		}),
		cache: false,
		dataType: "text",
		complete: function(){ loading(0); },
		success: function(response){
			var res = splitResponse(response);
			if( res ) {
				$("#note"+res['note']).fadeOut('slow');
			}
		}
	});
}


function splitResponse(response) {
	if( response ) {
		var responseArray = response.split('|~');
		var newArray = new Array();
		for( var i=0; i<responseArray.length; i++ ) {
			tempArray = responseArray[i].split(':~');
			newArray[tempArray[0]] = tempArray[1];
		}
		if( newArray['appError'] ) {
			showDialog('Application Error',newArray['appError'],'error');
			return false;
		}
		else if( newArray['error'] ) {
			showDialog('Error',newArray['error'],'error');
		}
		else if( newArray['warning'] ) {
			showDialog('Warning',newArray['warning'],'warning');
		}
		else {
			return newArray;
		}
	}
	else {
		return false;
	}
}
