var timer=false;
var uploadedImgs = [];
function changeImage(imgId)
{
	document.getElementById('photo_id').value=imgId;
	document.getElementById('cat_photo').src='/pictures/'+imgId+'_thumb.jpg';
}

function refreshImages()
{
	var imgs='<table cellpadding="0" cellspacing="0">';
	for(var i in uploadedImgs){imgs += '<tr><td><img src="/uploadtemp/'+uploadedImgs[i]+'_thumb.jpg" align="middle" style="margin-bottom:8px;" /> <a href="#" onclick="if(confirm(\'Are you sure you want to delete this image?\')) removeImage('+uploadedImgs[i]+'); return false">Remove Image</a></td></tr>';}
	document.getElementById('img_holder').innerHTML=imgs+'</table>';
	document.getElementById('image_ids').value=uploadedImgs.join(',');
	document.getElementById('no_of_images').value=uploadedImgs.length;
}

function addImage(imgId)
{
	uploadedImgs[uploadedImgs.length]=imgId;
	refreshImages();
}

function removeImage(imgId)
{
	var upImgs = [];
	for(var i in uploadedImgs){if(uploadedImgs[i] != imgId) upImgs[upImgs.length] = uploadedImgs[i];}
	uploadedImgs = upImgs;
	refreshImages();
}

function onEndCrop( coords, dimensions )
{
	$('x1').value = coords.x1;
	$('y1').value = coords.y1;
	$('x2').value = coords.x2;
	$('y2').value = coords.y2;
	$('width').value = dimensions.width;
	$('height').value = dimensions.height;
}

var formChecks= {
	'addCat' : [
		['name','Please enter the name of the category',''],
		['base','Please select the first level category',''],
		['photo_id','Please select an image','0']
	],
	'editCat' : [
		['name','Please enter the name of the category',''],
		['base','Please select the first level category',''],
		['category_info','Please enter a category description',''],
		['photo_id','Please select an image','0']
	],
	'addItem' : [
		['name','Please enter the name of the category',''],
		['name_is_ok','That name is already taken, please make it unique.',''],
		['details','Please enter an item description',''],
		['category_id','Please select a category',''],
		['no_of_images','Please add at least 1 image',0]
	],
	'editItem' : [
		['name','Please enter the name of the category',''],
		['name_is_ok','That name is already taken, please make it unique.',''],
		['details','Please enter an item description',''],
		['category_id','Please select a category','']
	]
};

function formCheck(n)
{
	e=false;
	arr=formChecks[n];
	for(var i in arr)
	{
		el=document.getElementById(arr[i][0]);
		if(el && el.value == arr[i][2])
		{
			if(!e)
			{
				e = '';
				el.focus();
			}
			e += '* '+arr[i][1]+'\n';
		}
	}
	if(e) alert('Please correct the following before continuing...\n\n' + e);
	return e ? false : true;
}