/* Javascript functions used by the Image Upload system and admin pages
 * that use the system.
 *
 * Phil Stewart, 20050228
 * Copyright (c) 2005-2006 Room 101 Ltd
 *
 * PS 20060628: Added form name argument to allow more than one form on a page
 */

// Function to use in an admin page to open an image upload window.
// You must pass a valid category (as listed in upload-defs.pl),
// the name/id of the input control that will hold the image ID,
// PS 20060628: and optionally the name of the invoking form
function upload_window(image_category, target_control_id, form_name)
{
	// If form name is not passed, assume forms[0]
	if (!form_name)
	{
		form_name = 'forms[0]';
	}

	// Set a property on the parent window for the upload window to access
        eval('window.' + image_category + '_image_id = 0;');
        eval('window.' + image_category + '_target_control_id = \'' + target_control_id + '\';');

	// Open the upload window and give it focus
        var popup_window = window.open('upload_image.pl?category=' + image_category, '', 'width=770,height=515,scrollbars=no,status=yes');
        popup_window.focus();

	// Wait for the child window to alter the property created above
	// Note that we check for a running Interval timer first to prevent a
	// possible clash with a timer left over from an earlier window
	var current_timer = eval('window.' + image_category + '_timer_id');
	if (current_timer) {window.clearInterval(current_timer);}
	eval('window.' + image_category + '_timer_id = window.setInterval("wait_for_upload(\'' + image_category + '\', \'' + target_control_id + '\', \'' + form_name + '\')", 2000);');
	// IE Debug
//	alert(eval(image_category));
}

function upload_window_test(image_category, target_control_id, form_name)
{
	alert('club website');
	// If form name is not passed, assume forms[0]
	if (!form_name)
	{
		form_name = 'forms[0]';
	}

	// Set a property on the parent window for the upload window to access
        eval('window.' + image_category + '_image_id = 0;');
        eval('window.' + image_category + '_target_control_id = \'' + target_control_id + '\';');
	
	// Open the upload window and give it focus
        var popup_window = window.open('upload_image1.pl?category=' + image_category, '', 'width=770,height=515,scrollbars=no,status=yes');
        popup_window.focus();

	// Wait for the child window to alter the property created above
	// Note that we check for a running Interval timer first to prevent a
	// possible clash with a timer left over from an earlier window
	var current_timer = eval('window.' + image_category + '_timer_id');
	if (current_timer) {window.clearInterval(current_timer);}
	eval('window.' + image_category + '_timer_id = window.setInterval("wait_for_upload(\'' + image_category + '\', \'' + target_control_id + '\', \'' + form_name + '\')", 2000);');
	// IE Debug
//	alert(eval(image_category));
}


// Function executed when waiting for the upload window to return an image id
// Invoked by setInterval in the upload_window() function above
function wait_for_upload(image_category, target_control_id, form_name)
{
	var current_id_value = 0;
	eval('current_id_value = window.' + image_category + '_image_id;');

	if (current_id_value != 0)
	{
		// Once we've reached here, the upload window has completed
		// its work and given us the ID of the uploaded image

		// Set the value of the target control to the given id
		eval('window.document.' + form_name + '.' + target_control_id + '.value = ' + current_id_value);
//		alert(current_id_value + ' | ' + target_control_id);

		// Change the 'upload not present image' to the 'upload_present' image
		eval('document.' + image_category + '_upload_status.src = \'/images/upload-present.gif\'');

		// Clear this timeout
		window.clearInterval(eval('window.' + image_category + '_timer_id'));

		// Sleep? Timeout? Approx 3.5 seconds
		// Then do requested popup
	}
}

// DEBUG: Generic test function
//function test_function()
//{
//	alert(window.document.forms[0].GroupPhoto.value);
//}

// Function to return the id of the uploaded image to the parent form and close the window
// PS 20060519: No longer need to automaticly close window
function set_upload_image_id(image_category, upload_id)
{
	eval('window.opener.' + image_category + '_image_id = ' + upload_id + ';');
//	alert(eval('window.opener.' + image_category + '_image_id'));
//	window.setTimeout('window.close();', 3000);
}

// Function to check that an image has been uploaded when a user attempts
// to submit a new image for the photo gallery or a new ad button
// PS 20060628: Optional form_name arg defaults to forms[0] if not specified
function submit_upload_check(target_control_id, form_name)
{
	// If form name is not passed, assume forms[0]
	if (!form_name)
	{
		form_name = 'forms[0]';
	}

	var upload_id = eval('window.document.' + form_name + '.' + target_control_id + '.value');
	if (upload_id == 0)
	{
		alert("No image has been uploaded yet. Please upload an image to add to the gallery.");
		return false;
	}
	else
	{
		return true;
	}
}

