var fileUpload_tbody = null; // holds the tbody of the table we add and remove uploadable rows to
var fileUpload_current = 0; // current number of files uploaded
var fileUpload_total = 0; // the total of files going to upload

function initFileUpload() {
	
	fileUpload_tbody = document.getElementById("upload_table").getElementsByTagName("TBODY")[0]; // use caps for IE6
	addFileUpload();
	
}

function addFileUpload() {
	
	var tr = document.createElement("TR");
	tr.setAttribute("height", "30");
	
	var td1 = document.createElement("TD");
	td1.innerHTML = "<a href=\"#\" onclick=\"removeFileUpload(this)\">Remove</a>";
	
	var td2 = document.createElement("TD");
	td2.innerHTML = "<iframe src=\"upload_form.php\" style=\"width:100%; height:40px; border:none;\" frameborder=\"no\"></iframe>";
	
	tr.appendChild(td1);
	tr.appendChild(td2);
	fileUpload_tbody.appendChild(tr);
	
}

function removeFileUpload(obj) {
	
	// my over done remove a row, but it works beautifully
	
	var row_count = fileUpload_tbody.rows.length; // get the number of rows
	var tr = obj.parentNode.parentNode; // get the tr by the passed element
	
	for(var i = 0; i < row_count; i++) {
		if(tr == fileUpload_tbody.rows[i]) {
			fileUpload_tbody.deleteRow(i); // remove the row
			break;
		}
	}
	
}

function checkFileUpload() {

	var flag = true; // flag to determine if everything went smoothly
	var row_count = fileUpload_tbody.rows.length;
	var upload_form = document.forms["upload_table_form"];
	upload_form.btn1.disabled = upload_form.btn2.disabled = true; // disable the add a row and upload buttons
	upload_form.btn1.value = upload_form.btn2.value = "Submitting..."; // tell them the status
	
	for(var i = 0; i < row_count; i++) {
		
		var iframe = fileUpload_tbody.rows[i].getElementsByTagName("IFRAME")[0];
		var form = iframe.contentWindow.document.forms[0]; // get the form in the iframe
		
		if(form.the_file.value == '') {
			alert("File #" + (i + 1) + " is empty");
			upload_form.btn1.disabled = upload_form.btn2.disabled = false;
			upload_form.btn1.value = "Add Image";
			upload_form.btn2.value = "Upload Images(s)";
			flag = false;
			break;
		}
		
		if(form.the_file.value.lastIndexOf("png") == -1) {
			alert("Only .png extensions acceptable");
			upload_form.btn1.disabled = upload_form.btn2.disabled = false;
			upload_form.btn1.value = "Add Image";
			upload_form.btn2.value = "Upload Images(s)";
			flag = false;
			break;
		}
		
	}
	
	// stop the script if false
	if(!flag) { return; }
	
	// we loop through again to disable the Remove buttons... we do it here because we wanted to check if the flag variable was good to go
	// to disable the onclick for the Remove button, simply set a new onclick function to do nothing
	for(var i = 0; i < row_count; i++) {
		fileUpload_tbody.rows[i].getElementsByTagName("a")[0].onclick = function() {  }
	}
	
	fileUpload_total = fileUpload_tbody.rows.length;
	statusFileUpload();
	
}

function statusFileUpload() {
	
	var iframe = fileUpload_tbody.rows[fileUpload_current].getElementsByTagName("IFRAME")[0];
	var form = iframe.contentWindow.document.forms;
	var body = iframe.contentWindow.document.body;
	
	// does the form exist?
	if(form.length == 1) {
		
		// 'submitted' is a hidden input field in the form.
		// if its set to true, then the form has not been uploaded.
		// we check to see if the form has not been submitted and submit it
		if(form[0].submitted.value == 0) {
			form[0].submitted.value = 1; // it now will be submitted
			//form[0].path.value = "path/to/dir/"; // this is relative of the 'upload_form.php'
			form[0].submit();
			body.innerHTML = "<em>Uploading...</em>"; // DON'T use the span tags or it will conflict!
		}

	// if it doesn't exist, it must be done
	// we check if we see the span tag because even though the uploaded image could've been over the limit size, we want to continue to upload the other images
	} else if(body.innerHTML.toLowerCase().indexOf("span") != -1) {

		fileUpload_current++;
		if(fileUpload_current == fileUpload_total) {
			window.location.reload(true);
			return;
		}

	}
	

	// keep looping
	setTimeout("statusFileUpload()", 500);
	
}

window.onload = initFileUpload;