//////////////////////////////////////////////// BEGIN EMAILS STUFF ///////////////////////////////////////////////////////////////////

function makeEditable(id) {
	Event.observe(id, 'click', function(){edit($(id))}, false);
	Event.observe(id, 'mouseover', function(){showAsEditable($(id))}, false);
	Event.observe(id, 'mouseout', function(){showAsEditable($(id), true)}, false);
}

function edit(obj){
	Element.hide(obj);
	
	var textarea = '<div id="'+obj.id+'_editor"><input style="background-color: #C0C0C0;" id="'+obj.id+'_edit" name="'+obj.id+'" value="'+obj.innerHTML+'" size="40">&nbsp;&nbsp;&nbsp;';
	var button	 = '<a style="text-decoration:none;color:red" id="'+obj.id+'_save" href="javascript:void(0)"><b>SAVE</b></a> or <a style="text-decoration:none;color:red" id="'+obj.id+'_cancel" href="javascript:void(0)"><b>CANCEL</b></a></div>';
	
	new Insertion.After(obj, textarea+button);	
	document.getElementById(obj.id+'_edit').focus();
	document.getElementById(obj.id+'_edit').select();
	Event.observe(obj.id+'_save', 'click', function(){saveChanges(obj)}, false);
	Event.observe(obj.id+'_cancel', 'click', function(){cleanUp(obj)}, false);	
}

function showAsEditable(obj, clear){
	if (!clear){
		Element.addClassName(obj, 'editable');
	}else{
		Element.removeClassName(obj, 'editable');
	}
}

function saveChanges(obj){
	
	var new_content	=  escape($F(obj.id+'_edit'));

	obj.innerHTML	= "Saving...";
	cleanUp(obj, true);

	var success	= function(t){editComplete(t, obj);}
	var failure	= function(t){editFailed(t, obj);}

  	var url = AJAX_ACTIONS_MYADMIN + 'actionEmail.php';
	var pars = 'action=update_email&id='+obj.id+'&content='+new_content;
	var myAjax = new Ajax.Request(url, {method:'post', postBody:pars, onSuccess:success, onFailure:failure});
}

function cleanUp(obj, keepEditable){
	Element.remove(obj.id+'_editor');
	Element.show(obj);
	if (!keepEditable) showAsEditable(obj, true);
}

function editComplete(t, obj){
	obj.innerHTML	= t.responseText;
	showAsEditable(obj, true);
}

function editFailed(t, obj){
	obj.innerHTML	= 'Sorry, the update failed.';
	cleanUp(obj);
}

flagProcessed = function(id, flag) {
	new Ajax.Updater('tr_emails_'+id, AJAX_ACTIONS_MYADMIN + 'actionEmail.php', {
	  parameters: {
	  			  action: 'flag_processed',
	  			  id: id,
	  			  flag: flag
	  			  }
	});
}

flagSendFirstEmail = function(id) {
	new Ajax.Updater('flag_send_email_'+id, AJAX_ACTIONS_MYADMIN + 'actionEmail.php', {
	  parameters: {
	  			  action: 'flag_send_first_email',
	  			  id: id,
	  			  }
	});
}

flagSendSecondEmail = function(id) {
	new Ajax.Updater('second_email_flag_'+id, AJAX_ACTIONS_MYADMIN + 'actionEmail.php', {
	  parameters: {
	  			  action: 'flag_send_second_email',
	  			  id: id,
	  			  }
	});
}

flagResendEmail = function(id) {
	new Ajax.Updater('flag_send_email_'+id, AJAX_ACTIONS_MYADMIN + 'actionEmail.php', {
	  parameters: {
	  			  action: 'flag_resend_email',
	  			  id: id,
	  			  }
	});
	document.getElementById('email_date_send_'+id).innerHTML='';
}

flagResendSecondEmail = function(id) {
	new Ajax.Updater('second_email_flag_'+id, AJAX_ACTIONS_MYADMIN + 'actionEmail.php', {
	  parameters: {
	  			  action: 'flag_resend_second_email',
	  			  id: id,
	  			  }
	});
	document.getElementById('email_date_send_past_'+id).innerHTML = '';	
}
//////////////////////////////////////////////// END EMAILS STUFF ///////////////////////////////////////////////////////////////////



//////////////////////////////////////////////// BEGIN CATEGORIES STUFF ///////////////////////////////////////////////////////////////////
function switchCategories(disabled) {
		$('firstCategory').disabled = disabled;
		$('secondCategory').disabled = disabled;
		$('thirdCategory').disabled = disabled;
	if (!disabled)
		$('actionCategory').innerHTML = '';
}

function getCategories(id, categoryId, firstSelectedCategory, secondSelectedCategory, thirdSelectedCategory, pageLoad) {
	
	switch (id) {
		case 'firstCategory':
			if (categoryId == '-1')
				getCategory(id, '0', firstSelectedCategory);
			if (firstSelectedCategory > 0 && pageLoad == 0)
				getCategory('secondCategory', firstSelectedCategory, secondSelectedCategory);
			else 
				getCategory('secondCategory', categoryId, secondSelectedCategory);
			if (secondSelectedCategory > 0 && pageLoad == 0)
				getCategory('thirdCategory', secondSelectedCategory, thirdSelectedCategory);
			else 	
				getCategory('thirdCategory', '-1', thirdSelectedCategory);
			break;
		case 'secondCategory':
			getCategory('thirdCategory', categoryId, thirdSelectedCategory);
			break;
	}
}

function getCategory(id, parentCategoryId, selectedItem) {
	
	new Ajax.Updater(id, AJAX_ACTIONS_MYADMIN + 'category.php', {		
		parameters: 
					{ 
						action: 		'actionGetCategory',
						parentCategory: parentCategoryId,
						selected: selectedItem
					}
	});
}

function addCategory() {
	window.blur();
	if ($('firstCategory').value == '-1' && $('secondCategory').value == '-1' && $('thirdCategory').value == '-1') {
		alert('Please choose a parent category!');
		return;
	} else {
		var addHtmlCategories = '<b>';
		if ($('firstCategory').value != '-1') {
			var parentCategoryId = $('firstCategory').value;
			var levelCategory = 2;
			var firstSelectedIndex = $('firstCategory').selectedIndex;
			addHtmlCategories += $('firstCategory').options[firstSelectedIndex].text;
		}	
		if ($('secondCategory').value != '-1') {
			var parentCategoryId = $('secondCategory').value;
			var levelCategory = 3;
			var secondSelectedIndex = $('secondCategory').selectedIndex;
			addHtmlCategories += ' => ' + $('secondCategory').options[secondSelectedIndex].text;
		}	
		addHtmlCategories += '</b><br />';
	}
	var addHtml = '<BR />The category will be added under: ' + addHtmlCategories;
	$('actionCategory').innerHTML = addHtml + '<br /><input id="categoryValue" name="categoryValue" type="text" value="">&nbsp;<input type="button" value="insert" onclick="insertCategory(\'' + parentCategoryId + '\', \'' + levelCategory + '\');">&nbsp;<input type="button" value="cancel" onclick="switchCategories(false);">';
	$('categoryValue').focus();
	switchCategories(true);
}

function insertCategory(parentId, level) {
	if ($('categoryValue').value == '') {
		alert('Please enter category name!');
		return;
	}
	new Ajax.Updater('actionCategory', AJAX_ACTIONS_MYADMIN + 'category.php', {
		parameters: 
					{ 
						action: 		'actionInsertCategory',
						category:		$('categoryValue').value,
						parentCategory: parentId,
						level: 			level
					}
	});	
	switchCategories(false);
}

function editCategory() {
	window.blur();
	if ($('firstCategory').value == '-1' && $('secondCategory').value == '-1' && $('thirdCategory').value == '-1') {
		alert('Please choose a category to edit!');
		return;
	} else {
		var editHtmlCategories = '<b>';
		if ($('firstCategory').value != '-1') {
			var updatedCategoryId = $('firstCategory').value;
			var firstSelectedIndex = $('firstCategory').selectedIndex;
			var updatedCategory = $('firstCategory').options[firstSelectedIndex].text;
			editHtmlCategories += $('firstCategory').options[firstSelectedIndex].text;
		}
		if ($('secondCategory').value != '-1') {
			var updatedCategoryId = $('secondCategory').value;
			var secondSelectedIndex = $('secondCategory').selectedIndex;
			var updatedCategory = $('secondCategory').options[secondSelectedIndex].text;
			editHtmlCategories += ' => ' + $('secondCategory').options[secondSelectedIndex].text;
		}	
		if ($('thirdCategory').value != '-1') {
			var updatedCategoryId = $('thirdCategory').value;
			var thirdSelectedIndex = $('thirdCategory').selectedIndex;
			var updatedCategory = $('thirdCategory').options[thirdSelectedIndex].text;
			editHtmlCategories += ' => ' + $('thirdCategory').options[thirdSelectedIndex].text;
		}
		editHtmlCategories += '</b><br />';
	}
	var editHtml = '<BR />The category will be edited: ' + editHtmlCategories;
	$('actionCategory').innerHTML = editHtml + '<br /><input id="categoryValue" name="categoryValue" type="text" value="'+updatedCategory+'">&nbsp;<input type="button" value="update" onclick="updateCategory(\'' + updatedCategoryId + '\');">&nbsp;<input type="button" value="cancel" onclick="switchCategories(false);">';
	$('categoryValue').focus();
	switchCategories(true);
}

function deleteCategory(undelete) {
	window.blur();
	if ($('secondCategory').value == '-1' && $('thirdCategory').value == '-1') {
		alert('Please choose a category to delete! You cannot delete a main category!');
		return;
	} else {
		var deleteHtmlCategories = '';
		if ($('firstCategory').value != '-1') {
			var deletedCategoryId = $('firstCategory').value;
			var firstSelectedIndex = $('firstCategory').selectedIndex;
			deleteHtmlCategories += $('firstCategory').options[firstSelectedIndex].text;
		}
		if ($('secondCategory').value != '-1') {
			var deletedCategoryId = $('secondCategory').value;
			var secondSelectedIndex = $('secondCategory').selectedIndex;
			deleteHtmlCategories += ' => ' + $('secondCategory').options[secondSelectedIndex].text;
		}	
		if ($('thirdCategory').value != '-1') {
			var deletedCategoryId = $('thirdCategory').value;
			var thirdSelectedIndex = $('thirdCategory').selectedIndex;
			deleteHtmlCategories += ' => ' + $('thirdCategory').options[thirdSelectedIndex].text;
		}
	}
	if (undelete) {
		var deleteHtml = 'Are you sure you want to undo delete category: ' + deleteHtmlCategories;
	} else {
		var deleteHtml = 'Are you sure you want to delete category: ' + deleteHtmlCategories;
	}
	if (confirm(deleteHtml)) {
		markDeletedCategory(deletedCategoryId, undelete);		
	}
}


function updateCategory(updatedCategoryId) {
	if ($('categoryValue').value == '') {
		alert('Please enter category name!');
		return;
	}
	new Ajax.Updater('actionCategory', AJAX_ACTIONS_MYADMIN + 'category.php', {		
		parameters: 
					{ 
						action: 		'actionUpdateCategory',
						category:		$('categoryValue').value,
						categoryId: 	updatedCategoryId
					}
	});	
	switchCategories(false);
}

function markDeletedCategory(deletedCategoryId, undelete) {
	new Ajax.Updater('actionCategory', AJAX_ACTIONS_MYADMIN + 'category.php', {		
		parameters: 
					{
						action: 		'actionDeleteCategory',
						categoryId: 	deletedCategoryId,
						undelete: undelete
					}
	});	
	switchCategories(false);
}
//////////////////////////////////////////////// END CATEGORIES STUFF ///////////////////////////////////////////////////////////////////




//////////////////////////////////////////////// BEGIN PROGRAMS STUFF ///////////////////////////////////////////////////////////////////

function switchTabs(id) {
	
	var tabs = new Array('details', 'category', 'descriptions', 'editor', 'image', 'scan', 'screenshots', 'trailer', 'tags');
	
	tabs.each(function(item) {
		var menuItem = item + 'Li';
		if (item == id) {
			$(item).style.marginLeft = '0';
			$(menuItem).className = 'on';
			window.blur();
		} else {
			$(item).style.marginLeft = '-5000';
			$(menuItem).className = 'normal';
		}
	});
}

//////////////////////////////////////////////// END PROGRAMS STUFF ///////////////////////////////////////////////////////////////////

function submitForm(itemId, action, programName) {

	$('programId').value = itemId;
	
	if (action == 'delete') {
		if (confirm('Are you sure you want to delete program: "' + programName + '"?')) {
			$('searchForm').submit();
		}
	}
	
	if (action == 'edit') {
		$('searchForm').submit();
	}

	if (action == 'activate' || action == 'undo') {
		Modalbox.show(AJAX_ACTIONS_MYADMIN + 'commentPopup.php?commentId=' + itemId + '&action=' + action, {title: 'Edit Comment'}); return false;
	} else {
		$('searchForm').submit();
	}

}

function submitContactForm(itemId, action, name) {

	$('contactId').value = itemId;
	
	if (action == 'view' || action == 'undo') {
		Modalbox.show(AJAX_ACTIONS_MYADMIN + 'contactPopup.php?contactId=' + itemId + '&action=' + action, {title: 'Edit Contact'}); return false;
	}
	$('searchForm').submit();

}

function actionComment(commentId, action) {
	new Ajax.Updater('resultMessage', AJAX_ACTIONS_MYADMIN + 'actionComment.php', {
		parameters: 
					{						
						comment: 	commentId,
						action:		action 
					}
	});
	$('searchForm').submit();
}

function actionContact(contactId, action) {
	new Ajax.Updater('resultMessage', AJAX_ACTIONS_MYADMIN + 'actionContact.php', {
		parameters: 
					{						
						contact: 	contactId,
						action:		action 
					}
	});
	$('searchForm').submit();
}

function removeMedia(media, id, mediaType) {
	new Ajax.Updater(id, AJAX_ACTIONS_MYADMIN + 'actionMedia.php', {
		parameters: 
					{						
						media: 	media,
						action:	'remove',
						type: mediaType
					}
	});
}

//autocomplete tags
function update_on_select(id, data) {
	$(id).setValue(data);
}

function setup() {
		tinyMCE.init({
			// General options
			relative_urls : false, 
			remove_script_host : false,
			document_base_url : '<?=URL?>',
			mode : "textareas",
			theme : "advanced",
			plugins : "safari,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,wordcount",
	
			// Theme options
			theme_advanced_buttons1 : "copy,|, code",
			theme_advanced_buttons2 : "",
			theme_advanced_buttons3 : "",
			theme_advanced_toolbar_location : "top",
			theme_advanced_toolbar_align : "left",
			theme_advanced_statusbar_location : "bottom",
			theme_advanced_resizing : true,
	
			// Example content CSS (should be your site CSS)
			//content_css : "css/content.css",
	
			// Drop lists for link/image/media/template dialogs
			template_external_list_url : "lists/template_list.js",
			external_link_list_url : "lists/link_list.js",
			external_image_list_url : "lists/image_list.js",
			media_external_list_url : "lists/media_list.js",
	
			// Replace values for the template plugin
			template_replace_values : {
				username : "Some User",
				staffid : "991234"
			}
		});
	}