function refreshGalleryDisplay()
{
	gallery = document.forms['editPage'].elements['IMAGE_GALLERY'].value;
	if ( gallery.length > 2 )
	{
		galleryArray = gallery.split(';');
		DISPLAY_CODE = '';
		for ( gs = 0 ; gs < galleryArray.length ; gs++ )
		{
			gImgInfo = galleryArray[gs].split('|');
			imgPath = "/_media/img/general/" + gImgInfo[1];
			if ( gImgInfo[1] == "none" || gImgInfo[1] == "" || gImgInfo[1] == "undefined" )
			{
				imgPath = "/_framework/gfx/msc/pxl.gif";	
			}
			thisGalleryString = '<div style="position: relative; float: left; clear: both; padding-bottom: 10px;"><img border="1" src="' + imgPath + '" width="100" align="left"></div>';
			thisGalleryString += '<div style="position: relative; float: left"><Div style="position: relative; float: left">' + createGalleryControls(gImgInfo[0]) + '</div>';
			thisGalleryString += '<Div style="position: relative; float: left; padding-left: 8px; clear: both; width: 230px;"><b>Caption:<BR></b> ' + gImgInfo[2] + '</div>';
			thisGalleryString += '</div><BR>';
			DISPLAY_CODE += thisGalleryString;
		}
	}
	else
	{
		DISPLAY_CODE = "No Images Defined";
	}
	window.document.getElementById('GALLERY_DISPLAY').innerHTML = DISPLAY_CODE;	
}

function createGalleryControls(id)
{
	deleteButton = '&nbsp;[&nbsp;<a href="javascript:deleteGalleryImage(' + id + ');">Delete</a>&nbsp;]';
	editButton = '&nbsp;[&nbsp;<a href="javascript:popper(\'/' + WMSROOT + '/content/pop_gallery.php?action=edit&gImgID=' + id + '\',\'GalleryImagePicker\',\'400\',\'600\');">Edit</a>&nbsp;]';
	return "&nbsp;&nbsp;<nobr>" + editButton + deleteButton + "</nobr>";
}

function deleteGalleryImage(id)
{
	gallery = document.forms['editPage'].elements['IMAGE_GALLERY'].value;
	galleryArray = gallery.split(';');
	galleryString = '';
	gImgCount = 0;
	for ( gg = 0 ; gg < galleryArray.length ; gg++ )
	{
		gImgInfo = galleryArray[gg].split('|');

		if ( parseInt(gImgInfo[0]) != parseInt(id) )
		{
			if ( gg > 0 && gImgCount > 0 )
			{
				galleryString += ";";
			}
			galleryString += galleryArray[gg];
			gImgCount++;
		}
	}
	document.forms['editPage'].elements['IMAGE_GALLERY'].value = galleryString;
	refreshGalleryDisplay();
	
}

function loadGalleryForm()
{
	if ( action == "edit" )
	{
		existingImages = window.opener.document.forms['editPage'].elements['IMAGE_GALLERY'].value;
		galleryArray = existingImages.split(';');
		for ( gg = 0 ; gg < galleryArray.length ; gg++ )
		{
			gImgInfo = galleryArray[gg].split('|');
			form = document.forms['galleryImage'];
			if ( parseInt(gImgInfo[0]) == parseInt(gImgID) )
			{
				document.images['DISPLAY_IMAGE'].src = "/_media/img/general/" + gImgInfo[1];
				document.getElementById('NAME_IMAGE').innerHTML = "&nbsp;" + gImgInfo[1];
				form.elements['IMAGE'].value = gImgInfo[1];
				form.elements['caption'].value = gImgInfo[2];
			}
		}
	}
}

function saveGalleryImage(giID)
{
	filename = document.forms[0].elements['IMAGE'].value;
	caption = document.forms[0].elements['caption'].value;
	galleryString = window.opener.document.forms[0].elements['IMAGE_GALLERY'];
	existingGImgs = galleryString.value;
	
	if ( existingGImgs.length > 0 )
	{
		galleryArray = existingGImgs.split(';');
		galleryArrayCount = galleryArray.length;
	}
	else
	{
		galleryArrayCount = 0;
	}
	newGalleryString = '';
	
	if ( action == 'new' )
	{	
		newImg = galleryArrayCount + "|" + filename + "|" + caption;
	
		if ( existingGImgs == '' )
		{	
			newGalleryString = newImg;
		}
		else
		{
			newGalleryString = existingGImgs + ";" + newImg;
		}
	}
	else
	{
		for ( gg = 0 ; gg < galleryArrayCount ; gg++ )
		{
			imgInfo = galleryArray[gg].split('|');

			if ( parseInt(imgInfo[0]) == parseInt(giID) )
			{
				newGalleryString += giID + "|" + filename + "|" + caption;
			}
			else
			{
				newGalleryString += galleryArray[gg];
			}
			
			if ( gg < galleryArrayCount - 1)
			{
				newGalleryString += ";";
			}			
		}
	}
	
	galleryString.value = newGalleryString;
	window.opener.refreshGalleryDisplay();
	window.close();
}