UploadFIFileDialog =  function(){
this.uploadFIFileDialog = null;

this.init = function(){
	YAHOO.util.Event.addListener($("cancelinteractionuploadid"), 'click', 
			this.hideInteractionFileUpload, this, true);
	YAHOO.util.Event.addListener('submitinteractiondata', 'click', 
			this.onSubmitFIButtonClick, this,true);	
};

this.hideInteractionFileUpload = function(){
	var uploadcontainer = $("uploadinteractionfilecontainer");
	uploadcontainer.setStyle({display:"none"});
};
this.onSubmitFIButtonClick = function(){
	var t = this;
	var dataName = $("userinteractiondatanameid");
	var hideUploadHandle = this.hideInteractionFileUpload;
	
	if(dataName && dataName.value.length ==0){
		alert("Please enter a name for this data set.");
		return;
	}
	var fiHandler = {
		upload: function(o){
			var psicquicList = $("dblistid");
			if(psicquicList){
				// Add a label for the new dataset to the dropdown of interaction databases
				 var elOptNew = document.createElement('option');
				 elOptNew.text = dataName.value;
				 elOptNew.value = dataName.value + "Option";
				 elOptNew.id = dataName.value + "Option";
				 elOptNew.service = "3"; // A magic number used to tell the psicquicproxyservlet where to look
				 					   // for the interaction data. 3 indicates data stored in a local db
				 						// (and not a remote psicquic service)
				 psicquicList.add(elOptNew, null);
				// hideUploadHandle();
				 t.hideInteractionFileUpload();
			}		
		}
	};
	try{
		var form = document.getElementById("uploadinteractionfileform");
		if(form){
			YAHOO.util.Connect.setForm(form, true);
	        // This servlet receives the user data and stores it in the db.
			var postUrl =
        		"/ReactomeGWT/entrypoint/elv/FunctionalInteractionsServlet";
			YAHOO.util.Connect.asyncRequest("POST", postUrl, 
					fiHandler, "name="+dataName.value);
		}else{
			alert("Form not retrieved");
		}
	}catch(err){
		alert("Error " + err);
	}
 };

 this.displayUploadFIFileDialog = function(x, y){
    	//YAHOO.util.Dom.removeClass("dialog1", "yui-pe-content");
		$("uploadinteractionfilecontainer").style.display="block";
		
	   try{
	   if(this.uploadFIFileDialog == null){
		var uploadFIFileDialog = new YAHOO.widget.Dialog("uploadinteractionfilecontainer",  
					            { width : "480px", 
					              fixedcenter : true, 
					              visible : false,  
					              constraintoviewport : true, 
					              draggable: false,
					              close: false,
					              buttons : [  { 
					            	  text:"Submit", 
					            	  handler:this.onSubmitFIButtonClick.bind(this),
					            	  isDefault:true },
					                          { 
					            		  text:"Cancel", 
					            		  handler:this.handleCancel 
					            		  } ] 
					             } );
		uploadFIFileDialog.callback = { 
				success: this.handleSuccess,
			     failure: this.handleFailure 
			     };
		// Render the Dialog
		uploadFIFileDialog.render();
		uploadFIFileDialog.element.style.visibility = "visible";
		uploadFIFileDialog.element.style.zIndex = "51";
		uploadFIFileDialog.element.style.left = x + "px";
		uploadFIFileDialog.element.style.top = y+ "px";
		this.uploadFIFileDialog = uploadFIFileDialog;
	   }else{
		   this.uploadFIFileDialog.render();
		   this.uploadFIFileDialog.element.style.visibility = "visible";
		   $("uploadinteractionfilecontainer").style.display="block";
	   }

	   }catch(err){
		   alert("ERROR: " + err);
	   }
	};
	this.handleCancel = function() {
		//this.controlPanel.element.style.display="none";
		$("uploadinteractionfilecontainer").style.display="none";
	};
	this.handleSuccess = function(o) {
		var response = o.responseText;
		response = response.split("<!")[0];
		document.getElementById("resp").innerHTML = response;
	};
	this.handleFailure = function(o) {
		alert("Submission failed: " + o.status);
	};
};