function isNumberKey(evt)
{
	var charCode = (evt.which) ? evt.which : event.keyCode
	if (charCode > 31 && (charCode < 48 || charCode > 57))
		return false;
	return true;
}
function uncheck_checkbox(checkbox_id)
{
	if($('#'+checkbox_id+':checked')){
		$("#"+checkbox_id+"").attr("checked", "");
	}
}
function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
  return false;
}
// Function used to request download
function request_record_download(id, umid, title, downloadExpiry) 
{	
	var downloadError = 0;	
	// Send request to download script, return with download url
	$.ajax({
		beforeSend: function() { // Hide download link and show loading link
			$('#request_download_'+id+'').hide();
			$('#download-loading-'+id+'').show();
		},
		type: "GET",
		timeout: "10000", // Milliseconds
		url: "/xml/record_download.php",
		dataType: "xml",
		data: "umid="+umid+"&title="+encodeURIComponent(title)+"&downloadExpiry="+downloadExpiry+"&free=1",
		success: function(xml) {
			$(xml).find('download').each(function(){ // Get download URL from XML
				var downloadReturn = $(this).text();
				downloadError = $(this).attr('error');
				if(downloadError == 0) // If no error has been found (an error code greater than 0 is returned if an error occurs) 
				{
					window.location = downloadReturn; // Forward window onto download URL
				}
				else
				{
					alert('There was an error. Please try again. '+downloadReturn); // Download URL failed, display error
				}
			});			
		},
		complete: function() { // Hide loading text and show download link
			$('#download-loading-'+id+'').hide();
			$('#request_download_'+id+'').show();
		},
		error: function(XMLHttpRequest) { // Alert with error if download request fails
			alert('There was an error. Please try again. '+XMLHttpRequest);
			$('#download-loading-'+id+'').hide();
			$('#request_download_'+id+'').show();
		}
	});	
}
function checkform(form)
{
	var formatValue;
	var option = -1;
	var umid;
	
	// Validation
	for (i=form.format.length-1; i > -1; i--) // Check download file format
	{
		if (form.format[i].checked) 
		{
			formatValue = form.format[i].value;
			option = i; i = -1;
		}
	}
	if (option == -1) 
	{
		alert("Please select a format");
		return false;
	}
	if(!($('#terms').attr('checked') == true)) // Check that user has agreed to terms/conditions
	{
		alert("Please tick the box to indicate you have read our User Agreement.");
		return false;
	}
	
	// Proccess download request
	if(formatValue == "wmv")
	{
		umid = form.wmvUmid.value;
	}
	else if(formatValue == "mov")
	{
		umid = form.movUmid.value;
	}
	request_record_download(form.id.value, umid, form.title.value, form.downloadExpiry.value);
	
	return false;
}
