
YAHOO.namespace("bins");

var renameBinForm;
var newBinForm;
var binSummaryTable;

setBinSummaryTable = function ()
{
	binSummaryTable = document.getElementById('binSummaryTable');
}

function handleSelectedAction(action)
{
	if(action == "remove")
		removeFromBin();
	if(action == "deleteBin")
		confirmMultipleDelete();
}

function removeFromBin()
{
	document.theForm.page.value='collectionbin'; 
	document.theForm.mode.value='delete'; 
	document.theForm.submit();
}

function handleCheckboxSelect(checkbox)
{
	for(i=0; i<20; i++)
	{
		id = 'checkbox'+i;
		checkboxes = document.getElementsByName(id);
		if(checkboxes.length>0)
			checkboxes[0].checked = checkbox.checked;
	}
}

var createRenameBinForm = function()
{
	renameBinForm = new YAHOO.widget.Dialog("renameBinForm",
		{ width: "550px", 
	      fixedcenter: true, 
	      visible: false,  
	      modal: true,
	      constraintoviewport: true,
	      postmethod: "form",
	      buttons : [ { text:"Rename", handler:handleSubmit, isDefault:true }, 
	                  { text:"Cancel", handler:handleCancel } ]
	} ); 
	renameBinForm.render(document.body);
}

function showRenameBinForm(name,binId)
{
	document.getElementById('old_name').value = name;
	document.getElementById('rename_collection_id').value = binId;
	renameBinForm.show();
}

function createFindAndReplaceForm(binId)
{
	YAHOO.bins.findAndReplaceForm = new YAHOO.widget.Panel("findAndReplaceForm",
		{ width: "600px", 
		  height: "250px",
	      fixedcenter: true, 
	      visible: false,  
	      modal: true,
	      constraintoviewport: true
	} ); 
	YAHOO.bins.findAndReplaceForm.setHeader("Find and Replace"); 
	body = "<iframe src=appHome.gui?action=findAndReplaceForm&collectionBin="
		+ binId + " scrolling=no width=550 height=200 frameborder=0></iframe>"
	YAHOO.bins.findAndReplaceForm.setBody(body); 
	YAHOO.bins.findAndReplaceForm.setFooter("");
	YAHOO.bins.findAndReplaceForm.render(document.body);
	YAHOO.bins.findAndReplaceForm.show();
}

function createShareBinForm(binId)
{
	YAHOO.bins.shareBinForm = new YAHOO.widget.Panel("shareBinForm",
		{ width: "800px", 
		  height: "400px",
	      fixedcenter: true, 
	      visible: false,  
	      modal: true,
	      constraintoviewport: true
	} ); 
	YAHOO.bins.shareBinForm.setHeader("Share Bin"); 
	body = "<iframe src=appHome.gui?action=collectionBinShareForm&collection_id="
		+ binId + " scrolling=no width=750 height=350 frameborder=0></iframe>"
	YAHOO.bins.shareBinForm.setBody(body); 
	YAHOO.bins.shareBinForm.setFooter("");
	YAHOO.bins.shareBinForm.render(document.body);
	YAHOO.bins.shareBinForm.show();
}

var createNewBinForm = function()
{
	newBinForm = new YAHOO.widget.Dialog("newBinForm",
		{ width: "350px", 
	      fixedcenter: true, 
	      visible: false,  
	      modal: true,
	      constraintoviewport: true,
	      buttons : [ { text:"Create", handler:handleSubmit, isDefault:true }, 
	                  { text:"Cancel", handler:handleCancel } ]
	} ); 
	newBinForm.callback = { success: createBinSuccess, 
                  			failure: responseFailure }; 
    newBinForm.validate = function() { 
	    var data = this.getData(); 
	    var name = trim(data.newBin);
	    if (name.length == 0) { 
	        alert("Please enter a name for the new collection bin."); 
	        return false; 
	    } else { 
	        return true; 
	    } 
	}; 
	newBinForm.render(document.body);
}

function trim(sString)
{
	while (sString.substring(0,1) == ' ')
	{
		sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == ' ')
	{
		sString = sString.substring(0,sString.length-1);
	}
		return sString;
}

var handleSubmit = function() {
	this.submit();
};

var handleCancel = function() {
	this.cancel();
};

var createBinSuccess = function(o) {
	if(o.responseText != "success")
	{
		createErrorPanel(o,"Create Bin Failed");
	}
	else
	{
		openInNewTab('Loading', 'appHome.gui?action=collectionBin');
	}
}

function createErrorPanel(o,header) {
	YAHOO.bins.errorPanel = new YAHOO.widget.Panel("errorPanel", 
		{ 	width:"300px", 
			visible:false, 
			draggable:true, 
			close:true,
			modal:true,
			fixedcenter: true,
			constraintoviewport:true  } ); 
	YAHOO.bins.errorPanel.setHeader(header); 
	YAHOO.bins.errorPanel.setBody(o.responseText); 
	YAHOO.bins.errorPanel.setFooter(""); 
	YAHOO.bins.errorPanel.render(document.body); 
	YAHOO.bins.errorPanel.show();
}

var responseFailure = function(o) {
	document.getElementById("main_body").innerHTML=o.responseText;
}

function confirmBinDelete(binName, binId) {
	YAHOO.bins.dialog = 
		new YAHOO.widget.SimpleDialog("dialog", 
		  { width: "300px",
			fixedcenter: true,
			visible: false,
			draggable: false,
			close: true,
			modal:true, 
			constraintoviewport: true,
			buttons: [ { text:"Yes", handler: function() { this.hide(); deleteBin(binId); }, isDefault:true },
					   { text:"No",  handler: function() { this.hide();} } ]
		  } );
	YAHOO.bins.dialog.setHeader("Warning!");
	YAHOO.bins.dialog.setBody("Are you sure you want to delete " + binName + "?");
	YAHOO.bins.dialog.cfg.setProperty("icon",YAHOO.widget.SimpleDialog.ICON_WARN); 
	YAHOO.bins.dialog.render(document.body); 
	YAHOO.bins.dialog.show();
}

function deleteBin(binId)
{
	if(document.theForm != null)
	{
	  	document.theForm.mode.value='purge'; 
		document.theForm.page.value='collectionbin'; 
		document.theForm.submit();
	}
	else
	{
		row = document.getElementById('row_'+binId);
		sUrl = 'deleteBin.mas?bin='+binId;
		var callback = 
		{ 
		  success: responseDeleteSuccess, 
		  failure: responseFailure,
		  argument: row
		};
		var transaction = YAHOO.util.Connect.asyncRequest('GET', sUrl, callback, null); 
	}
}

var responseDeleteSuccess = function(o) {
	if(o.responseText != "success")
	{
		createErrorPanel(o,"Delete Failed");
	}
	else
	{
		binSummaryTable.deleteRow(o.argument.rowIndex);
	}
}

function confirmMultipleDelete() {
	YAHOO.bins.dialog = 
		new YAHOO.widget.SimpleDialog("dialog", 
		  { width: "300px",
			fixedcenter: true,
			visible: false,
			draggable: false,
			close: true,
			modal:true, 
			constraintoviewport: true,
			buttons: [ { text:"Yes", handler: function() { this.hide(); deleteMultipleBins(); }, isDefault:true },
					   { text:"No",  handler: function() { this.hide();} } ]
		  } );
	YAHOO.bins.dialog.setHeader("Warning!");
	YAHOO.bins.dialog.setBody("Are you sure you want to delete the selected bins?");
	YAHOO.bins.dialog.cfg.setProperty("icon",YAHOO.widget.SimpleDialog.ICON_WARN); 
	YAHOO.bins.dialog.render(document.body); 
	YAHOO.bins.dialog.show();
}

function deleteMultipleBins()
{
	bins = document.getElementsByName('bins');
	for(i=0; i<bins.length; i++)
	{
		if(bins[i].checked)
			deleteBin(bins[i].value);
	}
}


