NewPsicquicServiceDialog =  function(){
this.newPsicquicServiceDialog = null;

this.init = function(){
	YAHOO.util.Event.addListener($("cancelnewpsicquicserviceid"), 'click', 
			this.hideNewPsicquicService, this, true);
	YAHOO.util.Event.addListener('submitnewpsicquic', 'click', 
			this.onSubmitNewPsicquicServiceButtonClick, this,true);	
};

this.hideNewPsicquicService = function(){
	var newpsicquiccontainer = $("newpsicquicservicecontainer");
	newpsicquiccontainer.setStyle({display:"none"});
};

this.onSubmitNewPsicquicServiceButtonClick = function(){
	var t = this;
	var dataName = $("newpsicquicnameid");
	var hideUploadHandle = this.hideNewPsicquicService;
	var url = $("newpsicquicurlid");
	if(dataName && dataName.value.length ==0){
		alert("Please enter a name for the new PSICQUIC service.");
		return;
	}
	var newPsicquicHandler = {
			success: function(eventType){
		document.body.style.cursor = "default";
		var data = eval("(" + eventType.responseText + ")");
		var services = data.services;
		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.si = services[0].index;
			elOptNew.service = "1";// 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);
			t.hideNewPsicquicService();
		}		
	}
	}
	document.body.style.cursor = "wait";
	var postUrl =
		"/ReactomeGWT/entrypoint/elv/PSICQUICProxyServlet";
	YAHOO.util.Connect.asyncRequest("POST", postUrl, 
			newPsicquicHandler, "name="+dataName.value
								+ "&url=" + url.value
								+ "&action=newps");
};

 this.displayNewPsicquicServiceDialog = function(x, y){
		$("newpsicquicservicecontainer").style.display="block";
		
	   try{
	   if(this.newPsicquicServiceDialog == null){
		var newPsicquicServiceDialog = new YAHOO.widget.Dialog("newpsicquicservicecontainer",  
					            { width : "480px", 
					              fixedcenter : true, 
					              visible : false,  
					              constraintoviewport : true, 
					              draggable: false,
					              close: false,
					              buttons : [  { 
					            	  text:"Submit", 
					            	  handler:this.onSubmitNewPsicquicServiceButtonClick.bind(this),
					            	  isDefault:true },
					                          { 
					            		  text:"Cancel", 
					            		  handler:this.handleCancel 
					            		  } ] 
					             } );
		newPsicquicServiceDialog.callback = { 
				success: this.handleSuccess,
			     failure: this.handleFailure 
			     };
		// Render the Dialog
		newPsicquicServiceDialog.render();
		newPsicquicServiceDialog.element.style.visibility = "visible";
		newPsicquicServiceDialog.element.style.zIndex = "51";
		newPsicquicServiceDialog.element.style.left = x + "px";
		newPsicquicServiceDialog.element.style.top = y+ "px";
		this.newPsicquicServiceDialog = newPsicquicServiceDialog;
	   }else{
		   this.newPsicquicServiceDialog.render();
		   this.newPsicquicServiceDialog.element.style.visibility = "visible";
		   $("newpsicquicservicecontainer").style.display="block";
	   }
	   }catch(err){
		   alert("ERROR: " + err);
	   }
	};

	this.handleCancel = function() {
		//this.controlPanel.element.style.display="none";
		$("newpsicquicservicecontainer").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);
	};

	
};