
//function load() {
//	ajax = new Ajax;
//}

//window.onload = load;

//alert(1);
function applyLayoutHide() {
	layoutTicks();
}

function layoutTicks() {
	var hidden = 0;
	
	$('.boxmessage input').apply(function() {
		if (this.checked) hidden |= this.value;
	});
	
	if (1 & hidden) $('.boxmessage .breadcrumb').apply(function() { this.style.display = 'none'; });
	else $('.boxmessage .breadcrumb').apply(function() { this.style.display = ''; });
	
	if (2 & hidden) $('.boxmessage .pagetitle').apply(function() { this.style.display = 'none'; });
	else $('.boxmessage .pagetitle').apply(function() { this.style.display = ''; });
}

function editLayout() {
	var message = MessageBox.getEmptyMessage('700px', '520px');
	new MessageBox({title: 'Edit Page Layout', 
				    message: message,
				    callback: saveLayout, 
				    buttons: MessageBox.SAVE | MessageBox.CANCEL});
	
	
	var path = getPage();

	message.load({
			url: 'admin/layout/configure/' + path, onloaded: function() {
			layoutTicks();
			$('.layoutdemo').addEvent('mouseup', function() {
				$('.selectedlayout').removeClass('selectedlayout');
				$(this).addClass('selectedlayout');
				
				//alert($(this).$('input').value);
			});
		}
	});
}


function editDistributor() {
	
	var path = getPage();
	
	var message = MessageBox.getEmptyMessage('700px', '550px');
	
	new MessageBox({
		title: 'Edit Distributor',
		message: message,
		callback: saveDistributor,
		buttons: MessageBox.SAVE | MessageBox.CANCEL
	});
	
	
	
	message.load({url: 'admin/distributor/configure/' + path, onloaded: function() {
		
	}});
}


function resellerSaved() {
	var path = getPage();
	alert('Reseller Saved');
	$('#resellerbox').load({url: 'admin/distributor/reseller/' + path, onloaded: function() {
		
	}});
}


function addReseller() {
	var path = getPage();
	$('#resellerbox').load({url: 'admin/distributor/resellerform/0/' + path, onloaded: function(){} });
}

function deleteReseller(id) {
	if (confirm('Remove this reseller?')) {
		var path = getPage();
		new Ajax({url: 'admin/distributor/deleteReseller/' + id,
			  params: null,
			  callback: function() {
				  $('#resellerbox').load({url: 'admin/distributor/reseller/' + path, onloaded: function() {
						
					}});
			  }
		});
	}	
	else return false;
}



function editReseller1(id) {
	var path = getPage();
	$('#resellerbox').load({url: 'admin/distributor/resellerform/' + id + '/' + path, onloaded: function(){} });
}

function editReseller() {
	var path = getPage();
		
	var message = MessageBox.getEmptyMessage('700px', '550px');
	
	new MessageBox({
		title: 'Edit Reseller',
		message: message,
		callback: saveDistributor,
		buttons: MessageBox.CLOSE
	});
	
	
	
	message.load({url: 'admin/distributor/reseller/' + path, onloaded: function() {
		
	}});
}

function saveReseller(form) {
	path = getPage();
	
	new Ajax({url: 'admin/distributor/saveReseller/' + path,
		  params: form,
		  callback: resellerSaved
		});
	
	return false;
}


function saveDistributor(result) {
	if (result == MessageBox.SAVE) {
		var path = getPage();
		new Ajax({url: 'admin/distributor/save/' + path,
				  params: $('#distributorform')
				});
	}
}


function saveLayout(result) {
	if (result == MessageBox.SAVE) {
		var path = getPage();
		var layoutId = $('.selectedlayout input').value;
		
		var hidden = 0;
		
		$('.boxmessage input').apply(function() {
			if (this.checked) hidden |= this.value;
		});
		
		
		new Ajax({url: 'admin/layout/save/' + path, 
				  params: {layout: layoutId,
						   hidden: hidden
						   } 
				  }); 
		
	}
}


var admin = new Array();

function editContent(block) {
	var message = MessageBox.getEmptyMessage('700px', '520px');
	
	message.load({url: 'admin/content/editForm/' + getPage() + '/' + block, onloaded: startMCE});
	
	new MessageBox({title: 'Edit Page Contents', 
					message: message,
					callback: function() {
						
						new Ajax ({url: 'admin/content/save/' + getPage() + '/' + block,
								   method: 'POST',
								   params: {content: tinyMCE.activeEditor.save()}
							
						});	
		
					}, 
					buttons: MessageBox.SAVE | MessageBox.CANCEL
					});
	
}

function editWidgets() {
	var message = MessageBox.getEmptyMessage('700px', '520px');
	new MessageBox({title: 'Edit Page Contents', 
				    message: message,
				    callback: saveWidgets, 
				    buttons: MessageBox.SAVE | MessageBox.CANCEL});
	

	message.load({url: 'admin/widgets/configure/' + getPage()
		
	});
}

function getPage() {
	var path = window.location.href.replace($('base').href, '');
	
	
	
	if (path == '') path = 'Home';
	else if (path == 'au/') path = 'au/home';
	
	return path.replace(/\//g, '.');
}

function saveWidgets(result) {
	if (result == MessageBox.SAVE) {
	var path = getPage();
	var widgets = 0;
	
	$('#messagedialogmessage input[name="widgets[]"]').apply(function() {
			if (this.checked) {
				//alert('checked' + this.value);
				widgets |= this.value;
			}
	});
	
		
	changes.widgets = widgets;
	
	new Ajax({url: 'admin/widgets/save/' + path, params: changes, resultMode: Ajax.RESULT_TEXT, callback: function(text) {
	}}); 
	}
}

var changes = new Object;
function editWidget(el, id) {
	var path = getPage();
	path = path.replace(/\//g, '.');
	
	if ($('#selectedwidget')) $('#selectedwidget').id = '';
	el.id = 'selectedwidget';
	
	$('#editwidget').load(
		{url: 'admin/widgets/getWidgetOptions/' + id + '/' + path, 
		 onloaded: function() {
		
			$('#editwidget input,#editwidget select,#editwidget textarea').addEvent('click', function() {
				if (!changes[id]) changes[id] = new Object;
				
				if (this.className == 'list') {
					var list = [];
					$('#editwidget input[name="' + this.name + '"]').apply(function() { if (this.checked) list.push(this.value)});
					changes[id][this.name.split('[]')[0]] = list.join(',');
				}
				else if (this.name.indexOf('[]') != -1) {
					if (parseInt(this.value) == this.value) {
						var total = 0;
						$('#editwidget input[name="' + this.name + '"]').apply(function() { if (this.checked) total += parseInt(this.value)});
						changes[id][this.name.split('[]')[0]] = total;
					}
					else {
						var str = '';
						$('#editwidget input[name="' + this.name + '"]').apply(function() { if (this.checked) str += this.value + ','});
						changes[id][this.name.split('[]')[0]] = str;
					}
				}
				else  changes[id][this.name] = this.value;
			});
		}
	});
}

function startMCE() {
	tinyMCE.init({
		// General options
		mode : "textareas",
		theme : "advanced",
		plugins : "safari,pagebreak,style,layer,table,save,advhr,advimage,emotions,iespell,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,inlinepopups",

		// Theme options
		theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,formatselect,bullet",
		theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,|,preview",
		theme_advanced_buttons3 : "spellchecker,hr,image,removeformat,|,charmap,iespell,advhr",
		theme_advanced_buttons4 : "spellchecker,|,del,ins,",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		theme_advanced_statusbar_location : "bottom",
		theme_advanced_resizing : false,
		theme_advanced_disable : "strikethrough, cleanup, outdent, indent,remove",
		//spellchecker_languages : "+English=en",

		onchange_callback: 'mceChange',
		// Example word content CSS (should be your site CSS) this one removes paragraph margins
		//content_css : 'http://hughes-castell.com/editable/httpdocs/css/mce.css',
		document_base_url: document.getElementsByTagName('head')[0].getElementsByTagName('base')[0].href,

		// 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"

	});
	
	
}


var imageType = 1;

function chooseHeaderImage(img) {
	
	if (imageType == 1) $('#chooseheaderimage').src = 'img/headers/' + img;
	else if (imageType == 2) $('#chooseheaderimage').src = 'img/landingheaders/' + img;
	else if (imageType == 3) $('#chooseheaderimage').src = 'img/sideimages/' + img;
	else if (imageType == 4) $('#chooseheaderimage').src = 'img/sideimages/' + img;
}

function editHeader(type) {
	if (!type) type = 1;
	if (type == 'header') type = 1;
	imageType = type;
	var message = MessageBox.getEmptyMessage('950px', '380px');
	message.id = 'activemessage';
	
	message.load({url: 'admin/content/editHeaderForm/' + type + '/' + getPage()});
	
	new MessageBox({title: 'Edit Header', 
					message: message,
					callback: function(result) {
						if (result == MessageBox.SAVE) {
							new Ajax({
								url: 'admin/content/saveHeader/' + type + '/' + getPage() ,
								params: {image: $('#messagedialogmessage select[name="header"]').value, alt: $('#messagedialogmessage input[name="title"]').value }
							});
						}
					},
					buttons: MessageBox.SAVE | MessageBox.CANCEL
					});
}


function addHeaderFileFile() {

	new Ajax({
		url: 'admin/content/uploadHeader/',
		params: {file: $('#messagedialogmessage input[name="uploadfile"]').value, type: imageType},
		callback: function() {
			$('#activemessage').load({url: 'admin/content/editHeaderForm/' + imageType + '/' + getPage()});
		}
	});
	
	
}

function editLandingHeader() {
	editHeader(2);
}


function editSideImage(num) {
	editHeader(parseInt(num)+2);
}

function landingPages() {
	var message = MessageBox.getEmptyMessage('520px', '200px');
	message.id = 'landingbox';
	
	new MessageBox({title: 'Landing pages',
		message: message,
		buttons: MessageBox.CLOSE
	});
	
	message.load({url: getLang() + '/admin/pages/landing/'});
}

function setLandingPagePublished(checked) {
	if (checked) {
		$('#landingpagewarning').style.display = 'block';
	}
	else {
		$('#landingpagewarning').style.display = 'none';
	}
	
} 

function deleteLandingPage(form) {
	if (confirm('Delete this page?')) {
		var id = $('#landingpageform select[name="id"]').value;
		
		new Ajax({
			url: '/admin/pages/deletelanding/' + id,
			callback: function() {
				$('#landingbox').load({url: getLang() + '/admin/pages/landing/'});
			}
		});
	}
}

function selectLandingPage(id) {
	if (id == '') {
		$('#landingpagedelete').style.display = 'none';
		$('#landingpageform input[name="name"]').value = '';
		$('#landingpageform input[name="url"]').value = '';
		$('#landingpageform input[name="published"]').checked = false;
		$('#landingpageform input[name="published"]').disabled = false;
		$('#landingpageform input[name="name"]').disabled = false;
		$('#landingpageform input[name="url"]').disabled = false;
		$('#landingpageform input[type="submit"]').style.display = 'block';
	}
	else {
		new Ajax({
			url: getLang() + '/admin/pages/getPage/' + id,
			callback: function(result) {
				$('#landingpageform input[name="name"]').value = result.name;
				$('#landingpageform input[name="url"]').value = result.url;
				
				if (result.published == 1) {
					$('#landingpageform input[name="published"]').checked = true;
					$('#landingpageform input[name="published"]').disabled = true;
				//	$('#landingpageform input[name="name"]').disabled = true;
					$('#landingpageform input[name="url"]').disabled = true;
				//	$('#landingpageform input[type="submit"]').style.display = 'none';
					$('#landingpagedelete').style.display = 'none';
				}
				else {					
					$('#landingpageform input[name="published"]').checked = false;
					$('#landingpageform input[name="published"]').disabled = false;
					$('#landingpageform input[name="name"]').disabled = false;
					$('#landingpageform input[name="url"]').disabled = false;
					$('#landingpageform input[type="submit"]').style.display = 'block';
					$('#landingpagedelete').style.display = 'block';
				}
			}
		});
	}
}

function saveLandingPage()  {
	new Ajax({url: getLang() + '/admin/pages/saveLanding/', method: 'POST', 
			  params: $('#landingpageform'),
			  callback: function(result) {
				  alert(result.message);
				  
				  if (result.status == 1) {
					  window.location.href = '/' + $('#landingpageform input[name="url"]').value;
				  }
			  }
	});	
}

function addDynamicBlock() {
	var current = new Array();
	for (var i = 0; i < $('#currentblocks').options.length; i++) {
		current.push($('#currentblocks').options[i].value);
	}
	
	$('#blockoptions').empty();	
	$('#blockoptions').load({url: getLang() + '/admin/blocks/getAvailable/' + getPage(), params: {exclude: current.join(',')}});
}

function removeDynamicBlock() {
	if ($('#currentblocks').selectedIndex > -1) $('#availableblocks').add($('#currentblocks').options[$('#currentblocks').selectedIndex]);
}

function addDynamicBlock2(el) {
	if ($('#availableblocks').selectedIndex > -1) $('#currentblocks').add($('#availableblocks').options[$('#availableblocks').selectedIndex]);
}


function moveUp() {
	var selected = $('#currentblocks').options[$('#currentblocks').selectedIndex];
	if (selected.previousSibling) $('#currentblocks').insertBefore(selected, selected.previousSibling);
}

function moveDown() {
	var selected = $('#currentblocks').options[$('#currentblocks').selectedIndex];
	if (selected.nextSibling && selected.nextSibling.nextSibling) $('#currentblocks').insertBefore(selected, selected.nextSibling.nextSibling);
	else $('#currentblocks').appendChild(selected);
}


function editDynamicBlocks() {
	//alert('aa');
	var message = MessageBox.getEmptyMessage('700px', '520px');
	new MessageBox({title: 'Edit Page Blocks', 
				    message: message,
				    callback: saveBlocks, 
				    buttons: MessageBox.SAVE | MessageBox.CANCEL});
	
	
	message.load({url: getLang() + '/admin/blocks/editForm/' + getPage()});

}

var dynamicBlockOptions = new Array();
function saveBlocks(result) {
	if (result == MessageBox.SAVE) {
		
		//Get selected blocks and the order.
			
		
		
		for (var i = 0; i < $('#currentblocks').options.length; i++) {
			if (!dynamicBlockOptions[$('#currentblocks').options[i].value]) {
				dynamicBlockOptions[$('#currentblocks').options[i].value] = new Array();
			}
			
			dynamicBlockOptions[$('#currentblocks').options[i].value]['order'] = i;
		}
		
		new Ajax({
			url: getLang() + '/admin/blocks/save/' + getPage(),
			callback: function() {},
			params: dynamicBlockOptions
		});
	}
}


function editNews() {
	var message = MessageBox.getEmptyMessage('700px', '695px');
	
	new MessageBox({title: 'Edit News', 
	    message: message,
	    callback: saveBlocks,
	    buttons: MessageBox.CLOSE});
	
	message.load({url: getLang() + '/admin/news/editForm/', onloaded: startMCE});
}


function editArticle(el, id) {
	$('#editnews .selected').removeClass('selected');
	$(el).addClass('selected');
	
	new Ajax({url: 'admin/news/getArticle/' + id,
			  method: 'POST',
			  resultMode: Ajax.RESULT_OBJECT,
			  callback: function(result) {
				$('#editnews input[name="title"]').value = result.title;
				$('#editnews input[name="id"]').value = result.id;
				
				$('#editnews input[name="published"]').checked = (result.published == 1);
				$('#editnews input[name="worldwide"]').checked = (result.langId == 0);
				
				$('#editnews input[name="event"]').checked = (result.event == 1);
				
				isEvent((result.event == 1));
				if (result.event == 1) {
					
				
					var startDate = result.eventStartDate.split(' ');
				
					$('#editnews input[name="startdate"]').value = startDate[0];
					if (parseInt(startDate[1].split(':')[0],10) > 8) $('#editnews select[name="starthours"]').value = startDate[1].split(':')[0];
					$('#editnews select[name="startminutes"]').value = startDate[1].split(':')[1];
				
					var endDate = result.eventEndDate.split(' ');
				
					$('#editnews input[name="enddate"]').value = endDate[0];
					if (parseInt(endDate[1].split(':')[0],10) > 8) $('#editnews select[name="endhours"]').value = endDate[1].split(':')[0];
					$('#editnews select[name="endminutes"]').value = endDate[1].split(':')[1];
				}
				else {
					$('#editnews input[name="startdate"]').value = '';
					$('#editnews input[name="enddate"]').value = '';
				}
				tinyMCE.activeEditor.execCommand('mceSetContent',false,result.content);
				
			  }
	});
}

function validateDate(date) {
	var parts = date.split('/');	
	if (parts.length != 3) return false;	
	var date = new Date(parts[2], parts[1]-1, parts[0]);	
	if (parts[2] == date.getFullYear() && parts[1] == (date.getMonth() + 1) && parts[0] == date.getDate()) return true;
	else return false;		
}


function saveNews() {
	
	if ($('#editnews input[name="event"]').checked) {
		//check the dates!
		if (!validateDate($('#editnews input[name="startdate"]').value)) {	
			alert('invalid start date'); 
			return;
		}
	}
	
	new Ajax({url: getLang() + '/admin/news/save/' +  $('#editnews input[name="id"]').value,
			  method: 'POST',
			  params: {title: $('#editnews input[name="title"]').value, 
					   content: tinyMCE.activeEditor.save(), 
					   published: ($('#editnews input[name="published"]').checked ? 1 : 0),
					   worldwide: ($('#editnews input[name="worldwide"]').checked ? 1 : 0),
					   event: ($('#editnews input[name="event"]').checked ? 1 : 0),
					   image: $('#editnews input[name="image"]').value,
					   eventStartDate: 
						   	(
						   		$('#editnews input[name="startdate"]').value + 
						   		' ' + 
						   		$('#editnews select[name="starthours"]').value + 
						   		':' + $('#editnews select[name="startminutes"]').value),		   
					   eventEndDate: ($('#editnews input[name="enddate"]').value + ' ' + $('#editnews select[name="endhours"]').value + ':' + $('#editnews select[name="endminutes"]').value)		   
					 },
					   
			  callback: function() {
				alert('Saved.');
			  }
	});
	
}


function selectProduct() {
	var message = MessageBox.getEmptyMessage('200px', '50px');
	
	new MessageBox({title: 'Select Product',
				    callback: setProduct,
				    buttons: MessageBox.OK | MessageBox.CANCEL,
				    message: message
				 });
	
	message.load({url: 'admin/content/productForm/' + getPage()});
}

function setProduct(result) {
	if (result == MessageBox.OK) {
		new Ajax({url: 'admin/content/setProduct/' +  getPage(),
			  method: 'POST',
			  params: {productid: $('#messagedialogmessage select[name="product"]').value},
			  callback: function() {
				//alert('Saved.');
			  }
		});
	}
}


function getLang() {
	var path = getPage();
	
	var parts = path.split('.');
	if (parts[0].match(/^[a-z]{2}$/) || parts[0].match(/^[a-z]{2}[A-Z]{2}$/) ){
		return parts[0];
	}
	else return 'enGB';
}


function editHeaders() {
	var message = MessageBox.getEmptyMessage('500px', '400px');
	
	new MessageBox({title: 'Edit Headers',
					message: message,
					buttons: MessageBox.SAVE | MessageBox.CANCEL,
					callback: saveSeo
				   });
	
	message.load({url: 'admin/content/seoForm/' + getPage()});
}

function saveSeo(result) {
	if (result == MessageBox.SAVE) {
		new Ajax({url: 'admin/content/saveSeo/' + getPage(),
				  params: $('#messagedialogmessage form')});
	}
}




function editProducts() {
	var message = MessageBox.getEmptyMessage('520px', '420px');
	
	new MessageBox({title: 'Edit File Sets',
		message: message,
		buttons: MessageBox.SAVE | MessageBox.CANCEL,
		callback: saveProduct
	   });
	
	message.load({url: 'admin/content/editProductForm/' + getPage()});
}

function saveProducts() {
	
}


function editProduct(id) {
	new Ajax({url: 'admin/content/getProductInfo/' + id,
			  resultMode: Ajax.RESULT_OBJECT,
			  callback: function(result) {
			    if (result.name) {
			    	$('#messagedialogmessage input').item(0).value = result.name;
			    	if (result.specification) $('#specification').firstChild.nodeValue = result.specification;
			    	else $('#specification').firstChild.nodeValue = 'None';
			    	
			    	for (var i = 1; i <= 4; i++) {
			    		if (result['img' + i] == 1) {
			    			$('#img' + i).empty();
			    			var img = new Image();
			    			img.src = 'img/products/' + id + '/thumbs/' + i + '.jpg';
			    			$('#img' + i).add(img);
			    		}
			    		else {
			    			$('#img' + i).empty();
			    			$('#img' + i).add('None');
			    		}
			    	}
			    	
			    	
			    }
			    else {
			    	$('#messagedialogmessage input').item(0).value = '';
			    	$('#specification').firstChild.nodeValue = 'None';
			    	
			    	$('#img1').empty();
			    	$('#img2').empty();
			    	$('#img3').empty();
			    	$('#img4').empty();
			    	
			    	$('#img1').add('None');
			    	$('#img2').add('None');
			    	$('#img3').add('None');
			    	$('#img4').add('None');
			    }
			  }
		});
}

function saveProduct(result) {
	if (result == MessageBox.SAVE) {
	new Ajax({url: 'admin/content/saveProduct',
			  method: 'POST',
			  params: {id: $('#messagedialogmessage select').item(0).value,
		               name: $('#messagedialogmessage input').item(0).value,
		               specification: $('#messagedialogmessage input').item(1).value,
		               img1: $('#messagedialogmessage input').item(2).value,
		               img2: $('#messagedialogmessage input').item(3).value,
		               img3: $('#messagedialogmessage input').item(4).value,
		               img4: $('#messagedialogmessage input').item(5).value
					  },
					callback: function() {
						alert('saved');
					}
	});
	
	return false;
	}
}



function deleteNews(id, element) {
	if (confirm('Remove this news article?')) {
		new Ajax({url: 'admin/news/delete/' + id});
		$(element).parent().parent().remove();
		return false;
	}
}

function setFeaturedDownload(id, catId, checked, element) {
	
	//alert(element.name);
	
	var count = 0;
	$('input[name="featureddownload_2[]"]').apply(function() {
		if (this.checked) count++;
	});
	

	if (count > 15) {
		element.checked = false;
		alert('There can only be 15 featured downloads per category.');
		
	}
	else {
	
	//alert(id + ':: ' + checked);
	var type = 0;
	if (checked) type = 1;
	
	
	new Ajax({url: getLang() + '/admin/downloads/setFeatured/' + id + '/' + catId + '/' + type,
		  callback: function() {
			//$('#downloadsbox').load({url: 'admin/downloads/editDownloadsForm/'});
		  }
});
	
	}
}


function editDownloads() {
	var message = MessageBox.getEmptyMessage('920px', '560px');
	message.id = 'downloadsbox';
	
	new MessageBox({title: 'Edit Downloads',
		message: message,
		buttons: MessageBox.CLOSE | MessageBox.SAVE,
		callback: function(result) {
			if (result == MessageBox.SAVE) {
				saveDownload();
				return false;
			}
		}
	});
	
	message.load({url: getLang() + '/admin/downloads/editDownloadsForm/'});
}


function editDownloads2(type) {
	var message = $('#downloadsbox');
	message.load({url: getLang() + '/admin/downloads/editDownloadsForm/' + type + '?' + Math.random()});
}

function toggleDlCatAdmin(id) {
	var cat = $('.downloadcategoryadmin' + id);
	if (cat.style.display =='block') cat.style.display = 'none';
	else if (cat.style.display == 'none') cat.style.display = 'block';
}

function deleteDownload(id, catId) {
	if (confirm('Remove this download?')) {
		new Ajax({url: getLang() + '/admin/downloads/remove/' + id + '/' + catId,
				  callback: function() {
					$('#downloadsbox').load({url: 'admin/downloads/editDownloadsForm/'});
				  }
		});
	}
}


function editDownload(id, categoryId) {
	if (id == null) {
		$('#adddownload select').value = '';
		$('#adddownload input[name="id"]').value = '';
		$('#adddownload input[name="title"]').value = '';
	}
	else {
		new Ajax({url: getLang() + '/admin/downloads/getInfo/' + id + '/' + categoryId, 
				  callback: function(result) {
					$('#adddownload select').value = result.categoryId;
					$('#adddownload input[name="id"]').value = result.id + '-' + result.categoryId;
					$('#adddownload input[name="title"]').value = result.title;
				  }
		});
	}
}

function saveDownload() {
		new Ajax({url: getLang() + '/admin/downloads/save/', method: 'POST', 
			params: {
				id: $('#adddownload input[name="id"]').value,
				title: $('#adddownload input[name="title"]').value,
				description: $('#adddownload input[name="description"]').value,
				uri: $('#adddownload input[name="uri"]').value,
				categoryId: $('#adddownload select').value
			},
			callback: function() { alert('Saved'); $('#downloadsbox').load({url: getLang() + '/admin/downloads/editDownloadsForm/' + $('#adddownload select').value}); }
		
		});	
}


function isEvent(checked) {		
	$('#eventdetails div input,#eventdetails div select').apply(function() {
		this.disabled = !checked;
	});
}


function editJobs() {
	var message = MessageBox.getEmptyMessage('720px', '600px');
	message.id = 'jobsbox';
	
	new MessageBox({title: 'Edit Jobs',
		message: message,
		buttons: MessageBox.CLOSE
	});
	
	message.load({url: getLang() + '/admin/jobs/editJobsForm/', onloaded: startMCE});
}

function saveJob(form) {
	$(form).$('textarea').value = tinyMCE.activeEditor.save();
	new Ajax({url: getLang() + '/admin/jobs/save/', params: form, callback: function() { alert('Saved.'); }})
}

function editJob(el, id) {
	
	$('.selected').removeClass('selected');
	$(el).addClass('selected');
	
	
	if (id == null) {
		$('#messagedialogmessage input[name="job[id]"]').value = '';
		$('#messagedialogmessage input[name="job[title]"]').value = '';
		$('#messagedialogmessage input[name="job[visible]"]').checked = false;
		
		tinyMCE.activeEditor.execCommand('mceSetContent', false, '');
	}
	else {
		
	
	
	new Ajax({url: 'admin/jobs/getDetails/' + id,
		callback: function(result) {
	
		
			for (var r in result) {
				if (r == 'visible') {
					var vis = (result[r] == 1);
					$('#messagedialogmessage input[name="job[' + r + ']"]').checked = vis; 
				}
				else if (r == 'description') {
					
				}
				else {
					$('#messagedialogmessage input[name="job[' + r + ']"]').value = result[r];
				}
			}
			tinyMCE.activeEditor.execCommand('mceSetContent', false, result.description);
		}
	});
	
	}
}


function deleteJob(id) {
	if (confirm('Remove this job?')) {

	new  Ajax({url: getLang() + '/admin/jobs/remove/' + id, callback: function() {
		$('#jobsbox').load({url: getLang() + '/admin/jobs/editJobsForm/', onloaded: startMCE});
	}});
	
	}
}


