function ajaxFile(args){
	/*---- LOCAL / PRIVATE VARIABLES ----*/
	var form_name = args.form_name;
	var upload_field = args.upload_field;
	var destination = args.destination;
	var attachedF = args.attachedF;
	var uploadedF = args.uploadedF;
	var liveURL = args.liveURL;
	
	/*----- ACCESSORS -----*/
	this.setform_name = function(val){
		form_name = val;
	}
	this.getform_name = function(){
		return form_name;
	}
	this.setupload_field = function(val){
		upload_field = val;
	}
	this.getupload_field = function(){
		return upload_field;
	}
	this.setdestination = function(val){
		destination = val;
	}
	this.getdestination = function(){
		return destination;
	}
	this.setattachedF = function(val){
		attachedF = val;
	}
	this.getattachedF = function(val){
		return attachedF;
	}
	this.setuploadedF = function(val){
		uploadedF = val;
	}
	this.getuploadedF = function(){
		return uploadedF;
	}
	this.setliveURL = function(val){
		liveURL = val;
	}
	this.getliveURL = function(){
		return liveURL;
	}
	
	this.setform_name(form_name);
	this.setupload_field(upload_field);
	this.setdestination(destination);
	this.setattachedF(attachedF);
	this.setuploadedF(uploadedF);
	this.setliveURL(liveURL);
		
	function buildAttachedFilesBox(objData, rebuild){
		var file_ext = getFileExt(objData);
		var filename = "TEXT_icon.gif";
		switch (file_ext) {
			case "jpg":
			case "jpeg":
			case "gif":
			case "png":
				filename = "IMG-file.png";
				break;
			case "doc":
				filename = "WORD-icon.gif";
				break;
			case "pdf":
				filename = "PDF-Logo.gif";
				break;
			case "xls":
				filename = "EXCEL-file.png";
				break;
			default:
				filename = "";
				break;
		}
		if (filename != "") {
			$("#" + uploadedF).append('<p id="upFiles_' + objData + '"><img src="/apps/tissue-tracking/images/system/icons/' + filename + '" height="15px" align="left" /> <a href="' + liveURL + objData + '" target="_blank">' + objData + '</a> - Expiration Date: <input name="' + objData + '_expired" id="' + objData + '_expired" class="dateField" value="" /> - <a href="#' + uploadedF + '" id="upFiles_' + objData + '_href" onClick="ajax_delete_file(\'' + objData + '\', \'' + uploadedF + '\', \'' + attachedF + '\', \'' + encodeURI(destination) + '\')">remove</a></p>').show();
			$('.dateField').datepicker();
			if (rebuild != 1) {
				if ($("#" + attachedF).val() == "") 
					$("#" + attachedF).val(objData);
				else {
					$("#" + attachedF).val($("#" + attachedF).val() + "," + objData);
				}
					
			}
		}
	}
	
	function rebuildAttachedFilesBox(){
		var files = $("#" + attachedF).val();
		var fileList = files.split(",");
		if (fileList.length) {
			for (i = 0; i < fileList.length; i++) {
				buildAttachedFilesBox(fileList[i], 1);
			}
		}
		else {
			buildAttachedFilesBox(fileList, 1);
		}
		
	}
	
	// check if there are any files currently attached, if so rebuild the attachment box (used for page refreshes)
	if ($("#" + this.getattachedF()).val() != "") {
		rebuildAttachedFilesBox();
	}
	
	
		
	/*----- MAIN METHOD - Sets the iframe and appends it to the body and gets everything ready to start accepting file uploads -----*/
	this.ajax_upload = function(){
	
		var jForm = $("form:first");
		//var jForm = $("form[name='" + this.getform_name() + "']");
		this.uploadF = $("#" + this.getupload_field());
		
		this.uploadF.change(function(objEvent){
			var jThis = $(this);
			
			var strName = ("uploader" + (new Date()).getTime());
			
			var jFrame = $("<iframe height=\"50px\" name=\"" + strName + "\" src=\"about:blank\" />");
			
			jFrame.css("display", "block");
			jFrame.css("background", "#ccc");
			
			
			jFrame.load(function(objEvent){
			
				var objUploadBody = window.frames[strName].document.getElementsByTagName("body")[0];
				
				var jBody = $(objUploadBody);
				
				var objData = eval("(" + jBody.html() + ")");
				
				buildAttachedFilesBox(objData, 0);
				
				setTimeout(function(){
					jFrame.remove();
				}, 100);
			}); // END jFrame load
			var filename = validateExtension(jThis.val());
			
			
			if (filename) {
				$("body:first").append(jFrame);
				$("input[name='file_destination']").remove();
				jForm.append('<input type="hidden" name="file_destination" value="'+destination+'" />');
				jForm.attr("action", "helpers/ajax_upload/actions/upload.cfm").attr("method", "post").attr("enctype", "multipart/form-data").attr("encoding", "multipart/form-data").attr("target", strName);
				jForm.submit();
				jForm.attr("action", "").attr("method", "post").attr("enctype", "multipart/form-data").attr("encoding", "multipart/form-data").attr("target", "");
			}
			else {
				var file_ext = getFileExt(jThis.val());
				alert("You are not allowed to upload ." + file_ext + " files. Please try again!");
			}
		}); // END jFORM.change
	} // END FUNCTION
	
}

function validateExtension(f) {
		var file_ext = getFileExt(f);
		var filename = false;
		switch (file_ext) {
			case "jpg":
			case "jpeg":
			case "gif":
			case "png":
			case "doc":
			case "pdf":
			case "xls":
				filename = true;
				break;
			default:
				filename = false;
				break;
		}
		return filename;
}

function getFileExt(fname){
		var filename = String(fname);
		if (filename.length == 0) 
			return "";
		var dot = filename.lastIndexOf(".");
		if (dot == -1) 
			return "";
		var extension = filename.substr(dot + 1, filename.length);
		extension = String(extension);
		
		return extension.toLowerCase();
}

function removeValue(list, value){
	return list.replace(new RegExp(value + ',?'), '')
}

function ajax_delete_file(filename, uploadedF, attachedF, destination){
		var z = confirm("Are you sure you want to delete " + filename + "?");
		if (z) {
			var args = {};
			args.filename = filename;
			args.file_location = decodeURI(destination);
			$("#" + uploadedF + " > p").remove(":contains(" + filename + ")");
			$("#" + attachedF).val(removeValue($("#" + attachedF).val(), filename));
			
			$.post('helpers/ajax_upload/ajaxFile.cfc?method=deleteFile&returnFormat=plain', args, function(data, status){
			
			});
		}
		return false
		
}

