
var load_msg_downloads = "<div class=\"content_form_load_message\">loading...please wait</div>";

function content_edit_downloads_listen() {
	if (xmlhttp.readyState==4) {
		var listener_response_text = xmlhttp.responseText;

		form_body = document.getElementById('downloads_edit-body');
		form_body.innerHTML = listener_response_text;
		

	}
}

function content_edit_downloads(content_id) {
	url_var = "template=downloads&download_form=edit&content_id=" + content_id;

	if (xmlhttp)  {
		content_downloads_body_load_message('downloads_edit-body');
		show_content_form("content_form-downloads_edit");

		d=document;
		xmlhttp.open("POST", "listener_content.php", true);
		xmlhttp.onreadystatechange=content_edit_downloads_listen;
		xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		xmlhttp.send(url_var);
	}

}

function content_downloads_body_load_message(body_id) {
	form_body = document.getElementById(body_id);
	
	form_body.innerHTML = load_msg_downloads;

}


function content_delete_downloads_listen() {
	if (xmlhttp.readyState==4) {
		var listener_response = xmlhttp.responseText;

		form_body = document.getElementById('downloads_delete-body');
		form_body.innerHTML = listener_response;
	}
}

function content_delete_downloads(content_id) {
	url_var = "template=downloads&download_form=delete&content_id=" + content_id;
	
	if (xmlhttp)  {
		content_downloads_body_load_message('downloads_delete-body');
		show_content_form("content_form-downloads_delete");

		d=document;
		xmlhttp.open("POST", "listener_content.php", true);
		xmlhttp.onreadystatechange=content_delete_downloads_listen;
		xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		xmlhttp.send(url_var);
	}
}




// edit ajax

	// a couple examples/tests
	function test_click_listen(listener_response) {
		test_dv = document.getElementById('test_div');

		if (test_dv != null) {
			test_dv.innerHTML = listener_response;
		}
	}

	function test_click() {
		$.post("listener_content.php", { template: "downloads", download_form: "edit_ajax" }, 
			function(data){test_click_listen(data);});
	}


function ajax_downloads_edit(download_id) {
	view_tr_id = 'ajax_downloads_view_' + download_id;
	edit_tr_id = 'ajax_downloads_edit_' + download_id;
	
	view_tr = document.getElementById(view_tr_id);
	edit_tr = document.getElementById(edit_tr_id);
	
	if (view_tr != null) {
		view_tr.className = 'ajax_downloads_tr_hide';
	}
	
	if (edit_tr != null) {
		edit_tr.className = 'ajax_downloads_tr_show';
	}
}

function ajax_downloads_edit_cancel(download_id) {

	orig_field_id = 'ajax_downloads_edit_title_orig_' + download_id;
	edit_field_id = 'ajax_downloads_edit_title_' + download_id;
	
	orig_field = document.getElementById(orig_field_id);
	edit_field = document.getElementById(edit_field_id);
	
	edit_field.value = orig_field.value;
	
	view_tr_id = 'ajax_downloads_view_' + download_id;
	edit_tr_id = 'ajax_downloads_edit_' + download_id;
	
	view_tr = document.getElementById(view_tr_id);
	edit_tr = document.getElementById(edit_tr_id);
	
	if (view_tr != null) {
		view_tr.className = 'ajax_downloads_tr_show';
	}
	
	if (edit_tr != null) {
		edit_tr.className = 'ajax_downloads_tr_hide';
	}

}

function ajax_downloads_edit_save(download_id) {
	field_id = 'ajax_downloads_edit_title_' + download_id;
	field = document.getElementById(field_id);

	orig_field_id = 'ajax_downloads_edit_title_orig_' + download_id;
	orig_field = document.getElementById(orig_field_id);

	orig_field.value = field.value;
	
	$.post("listener_content.php", { template: "downloads", download_form: "edit_ajax_save", download_id: download_id, download_title: field.value }, 
		function(data){ajax_downloads_edit_save_callback(data);});

}


function ajax_downloads_edit_save_callback(data) {
	download = JSON.parse(data);
	
	title_div_id = 'ajax_downloads_view_title_' + download.id;
	view_tr_id = 'ajax_downloads_view_' + download.id;
	edit_tr_id = 'ajax_downloads_edit_' + download.id;
	
	title_div = document.getElementById(title_div_id);
	view_tr = document.getElementById(view_tr_id);
	edit_tr = document.getElementById(edit_tr_id);

	if (title_div != null) {
		title_div.innerHTML = download.value;
	}
	
	if (view_tr != null) {
		view_tr.className = 'ajax_downloads_tr_show';
	}
	
	if (edit_tr != null) {
		edit_tr.className = 'ajax_downloads_tr_hide';
	}

}



function ajax_downloads_delete(download_id) {
	view_info_id = 'ajax_downloads_view_info_' + download_id;
	view_info = document.getElementById(view_info_id);

	delete_info_id = 'ajax_downloads_delete_info_' + download_id;
	delete_info = document.getElementById(delete_info_id);
	
	if (view_info != null) {
		if (delete_info != null) {
			delete_info.innerHTML = view_info.innerHTML;
		}
	}

	view_tr_id = 'ajax_downloads_view_' + download_id;
	delete_tr_id = 'ajax_downloads_delete_' + download_id;
	
	view_tr = document.getElementById(view_tr_id);
	delete_tr = document.getElementById(delete_tr_id);
	
	if (view_tr != null) {
		view_tr.className = 'ajax_downloads_tr_hide';
	}
	
	if (delete_tr != null) {
		delete_tr.className = 'ajax_downloads_tr_show';
	}
}

function ajax_downloads_delete_cancel(download_id) {
	view_tr_id = 'ajax_downloads_view_' + download_id;
	delete_tr_id = 'ajax_downloads_delete_' + download_id;
	
	view_tr = document.getElementById(view_tr_id);
	delete_tr = document.getElementById(delete_tr_id);
	
	if (view_tr != null) {
		view_tr.className = 'ajax_downloads_tr_show';
	}
	
	if (delete_tr != null) {
		delete_tr.className = 'ajax_downloads_tr_hide';
	}

}

function ajax_downloads_delete_save(download_id) {

	$.post("listener_content.php", { template: "downloads", download_form: "delete_ajax_save", download_id: download_id }, 
		function(data){ajax_downloads_edit_delete_callback(data);});
	
}

function ajax_downloads_edit_delete_callback(data) {
	response = JSON.parse(data);

	// one way, not my favorite
	//	content_edit_downloads(response.content_id)

	tbl_id = 'downloads_edit_table';
	tbl = document.getElementById(tbl_id);

	view_tr_id = 'ajax_downloads_view_' + response.id;
	edit_tr_id = 'ajax_downloads_edit_' + response.id;
	delete_tr_id = 'ajax_downloads_delete_' + response.id;
	
	view_tr = document.getElementById(view_tr_id);
	edit_tr = document.getElementById(edit_tr_id);
	delete_tr = document.getElementById(delete_tr_id);

	tbl.deleteRow(view_tr.rowIndex);
	tbl.deleteRow(edit_tr.rowIndex);
	tbl.deleteRow(delete_tr.rowIndex);
	
	//alert(view_tr.rowIndex + " | " + edit_tr.rowIndex + " | " + delete_tr.rowIndex);	
}

function ajax_downloads_add_save() {
	
	title_field_id = 'ajax_downloads_add_title';
	file_field_id = 'ajax_downloads_add_file';
	content_id_field_id = 'content_id';
	
	title_field = document.getElementById(title_field_id);
	file_field = document.getElementById(file_field_id);
	content_id_field = document.getElementById(content_id_field_id);
	
	$.post("listener_content.php", { template: "downloads", download_form: "add_ajax_save", download_title: title_field.value, download_file: file_field.value, content_id: content_id_field.value }, 
		function(data){ajax_downloads_add_callback(data);});
}

function ajax_downloads_add_callback(data) {
	response = JSON.parse(data);
	
	content_edit_downloads(response.content_id)
}


function ajax_downloads_move_top(download_id) {
	$.post("listener_content.php", { template: "downloads", download_form: "move_ajax_top", download_id: download_id }, 
		function(data){ajax_downloads_order_callback(data);});

}

function ajax_downloads_move_up(download_id) {
	$.post("listener_content.php", { template: "downloads", download_form: "move_ajax_up", download_id: download_id }, 
		function(data){ajax_downloads_order_callback(data);});

}

function ajax_downloads_move_down(download_id) {
	$.post("listener_content.php", { template: "downloads", download_form: "move_ajax_down", download_id: download_id }, 
		function(data){ajax_downloads_order_callback(data);});

}

function ajax_downloads_move_bottom(download_id) {
	$.post("listener_content.php", { template: "downloads", download_form: "move_ajax_bottom", download_id: download_id }, 
		function(data){ajax_downloads_order_callback(data);});

}

function ajax_downloads_order_callback(data) {
	//alert(data);
	response = JSON.parse(data);
	
	content_edit_downloads(response.content_id)

}