 function extension_check(filename)
{

var filename = filename.toLowerCase();
var extensions = new Array(".png",".gif",".jpg", ".jpeg");
var fileextension = filename.slice(filename.lastIndexOf("."), filename.length)
var validextension = false;

for(var i=0;i<4;i++)
{
	if(extensions[i]==fileextension)
	{
		return true;
	}
	else
	{
		validextension = false;
	}
}
	return false;
}

function showHideCodes(id)
{
	more_element = document.getElementById('codes'+id);
	
	if(more_element.style.display == 'none')
	{
	// Element is not enabled, let's enable it
	// Set img to up now
	document.getElementById('codeSwitch'+id).src="/img/gui/buttons/arr_up.png";
	// Nice show it
	$("div#codes"+id).slideDown();
    // Add Class when extended - abandoned
    // $("#codes_li"+id).addClass('codesBorder');
	}
	else
	{
	// Codes are showing, put some damn clothes on them
	$("div#codes"+id).slideUp();
    // Remove Class - Adandoned
    //$("div#codes_li"+id).removeClass('codesBorder');
	document.getElementById('codeSwitch'+id).src="/img/gui/buttons/arr_down.png";
	}
}

function EvaluateForm()
{
	if(submitted == false)
	{
		if(totalFiles > 0)
		{
			submitted = true;
			document.getElementById('uploadButton').disabled = true; // Disable them from pressing it again
			document.getElementById('uploadButton').value="Uploading...";  // Set text
			document.getElementById('queuedi').innerHTML="Uploading Images"; // Change header text
			document.getElementById('queue').style.opacity = "0.4"; // CSS3 opacity
			document.getElementById('queue').style.filter = "alpha(opacity=40)";	// Same as above, but IE8
			document.getElementById('animate_upl').style.display = ''; // Show the animation
			$(".removeLink").attr("onclick", ""); // Disable the 'Remove' buttons
			$("#uploadButton").addClass("pulseButton"); // Adding a Class for CSS3 Animations
			HideErrorMessage(); // Hide any error messages we have
			$("input#fileinput").remove(); // Remove the 'real' input
			$("div#uploadBox").slideUp("slow"); // Slide up the now redundant input box
			return true;
		}
		else
			return false;
	}
	else
		return false;
}

function RemoveFromQueue(fileID)
{
	if(fileID)
	{
	
	// Disable the button
	$('#rmBtn' + fileID).attr('disabled', 'disabled');
	$('#rmBtn' + fileID).attr('onclick', '');
	
	// Sexy / of removal
	$("div#" + fileID).slideUp("medium");
	 
	// Physically remove div - needs a delay or it causes the animation above to not happen
	setTimeout(function () { $("div#" + fileID).remove(); }, 250);
	 
	// Get the file number
	var file_number = fileID.substring(4);
	
	// Remove the actual input box
	$("input#fileupload"+file_number).remove();
	
	// Free up the Slot
	Slots[file_number] = '';
	 
	// Take away one from totalfiles var
	totalFiles--;
	 
	// Remove the value in the array
	myFiles[file_number] = '';
	
	if(totalFiles == 0)
	{
		$("div#imageQueue").fadeOut("slow");
		$("div#uploadingSection").fadeOut("slow");
	}
	
	}
	
	UpdateQueueLimit();

}

function UpdateQueueLimit()
{

	element = document.getElementById('queueLimit');
	element.innerHTML='(' + totalFiles + '/8)';
	if(totalFiles == 8)
	{
		element.style.color='#74FFD3';
		$("label#inputlab").html('Image limit reached (8).');
	}
	else
	{
		element.style.color='';
		$("label#inputlab").html('Add another image &#8230;');
	}
}

function ShowErrorMessage(message)
{		
	// Need to hide the 'no queue' message or it just overlaps somehow
	if(totalFiles == 0)
	{
			//Edited
			$("div#imageQueue").fadeIn();
			$("div#uploadingSection").fadeOut();
	}	
	
	// Put the message at the start of the queue
	setTimeout(function () { $("div#queue").prepend('\n\n<div id="errorDiv" style="display: none; padding-bottom: 10px;" class="queuedFile">\n<div class="leftside"><p class="red spaced">' + message + '</p></div><div class="rightside"><a href="#" class="nodecorate" onclick="javascript:HideErrorMessage();"><div class="removeButton-error"><span class="removeText-error">Okay</span></div></a></div></div>'); }, 650);
	
	// Now show it
	setTimeout(function () { $("div#errorDiv").slideDown(); }, 700);

}

function HideErrorMessage()
{

	// Hide it
	setTimeout(function () { $("div#errorDiv").slideUp(); },  200);
	// Remove that div after  2.2 seconds
	setTimeout(function () { $("div#errorDiv").remove(); },  450);
	
	// Bring it back if it needs to be back
	if(totalFiles == 0)
	{
			$("div#imageQueue").fadeOut("slow");
			$("div#uploadingSection").fadeOut("fast");
	}

}

function EvaluateFile(file)
{
	
	if(extension_check(file) == true)
	{
	
		// Check the file isn't already in our queue
		if(totalFiles < 8)
		{
		
			already_up = false;
			too_large = false;
		
			for(i=1; i<=8; i++)
			{
			
				if(file == myFiles[i])
				{
				
				//Error me boy!
				ShowErrorMessage("Error! Duplicate Image Added");
				
				already_up = true;
				
				break;
				
				}
			
			}
		
			if(already_up == false)
			{
		
			for(i=1; i<=8; i++)
			{
				if(Slots[i] == 'in_use')
				{
				// Do nothing
				}
				else
				{
				
				myFiles[i] = file;
				
				var slash = file.lastIndexOf("\\"); 

				var fakeName = file.slice(slash+1, file.length)
				
				if(fakeName.length > 48) fakeName = fakeName.substring(0, 48) + '...';
								
				$("div#queue").append('\n\n<div style=\"display: none;\" id=\"file' + i + '\" class=\"queuedFile\">\n<div class=\"leftside\"><p class=\"aqua\">' + fakeName + '</p></div><div class=\"rightside\"><a href=\"#\" class=\"nodecorate removeLink\" id=\"rmBtnfile' + i + '\" onclick=\"javascript:RemoveFromQueue(\'file' + i + '\');\"><div class=\"removeButton\"><span class=\"removeText\">Remove</span></div></a></div></div>');

				// Make a new input and copy the value of the real one
				// We need to change the position of the already existing one and add a new one in its place
				document.getElementById('fileinput').id='fileupload'+i;
				fileVar = document.getElementById('fileupload'+i);
				fileVar.name='fileupload'+i;
				fileVar.className='hidden';
				fileVar.style.position='fixed';
				fileVar.style.left='0px';
				fileVar.style.top='0px';
								
				// We need a new div to take its place
				$("div#browse").append('<input onchange="javascript: EvaluateFile(this.value);" onclick="javascript: HideErrorMessage();" id="fileinput" name="fileinput" type="file" size="1" />');
				
								
				// Show the 'pretend' value in our input
				$("label#inputlab").html('Add another image &#8230;');
			
				// Show our new entry
				$("div#file"+i).show("slow");
				// Set it so this slot is now in-use
				Slots[i] = 'in_use';
				
				break;
				}
			}

			totalFiles++;
			
			
			if(totalFiles > 0)
			{
				setTimeout(function () { $("div#uploadingSection").fadeIn("slow"); }, 200);
				setTimeout(function () { $("div#imageQueue").slideDown("medium"); }, 500);
			}
				   
			}
				  
		}
		else
		{
		
		// This shows we're unhappy about having >8 files in the queue
		ShowErrorMessage('Maximum Images (8) Reached');
		
		}

	}
	else
	{
		ShowErrorMessage('Please select a .jpg .png or .gif image.');
	}
	
	// Update the total number of files in use lol
	UpdateQueueLimit();
	document.getElementById('fileinput').value='';
}
