var editButton = "<a class='btn btn-success' onClick='editScript(this)' ><i class='icon-edit icon-white'></i>Edit</a>";
+"  ";
var deleteButton = "<a class='btn btn-info' onClick='deleteScript(this)'><i class='icon-trash icon-white'></i>Delete</a>";
function loadScript(table, actionId) {

	table.dataTable().fnClearTable();
	$.post("loadScripts", {}, function(data) {
		if (data.success) {
			data = data.scripts;
			if (data == null) {
				return;
			}
			for ( var i = 0; i < data.length; i++) {
				addScriptTableRow(table, data[i], actionId);
			}
		} else {
			information(data.failedMessage);
		}

	}, "json");

}
function addScriptAction() {
	$('.btn-success').click(function(e) {
		e.preventDefault();
		editScript(this);
	});

	$('.btn-info').click(function(e) {
		e.preventDefault();
		deleteScript(this);
	});
}
function addScriptTableRow(table, data, actionId) {
	var viewButton = "<a class='btn btn-success' onClick='editScript(this)' ><i class='icon-zoom-in icon-white'></i>"
			+ $.i18n.prop('view') + "</a>";
	+"  ";
	var time = new Date(data.createDateTime);
	time.setTime(data.createDateTime);
	switch (actionId) {
	case 0:
		table.dataTable().fnAddData([ data.name, data.id, time ]);
		break;
	case 1:
		table.dataTable().fnAddData([ data.name, data.id, time, viewButton ]);
		break;
	case 2:
		table.dataTable().fnAddData(
				[ data.name, data.id, time, editButton + deleteButton ]);
		break;
	default:
		break;
	}

}

function deleteScript(obj) {
	var scriptId = getScriptId(obj);
	var c = confirm("Are you sure you want to delete this script?");
	if (c == true) {
		deleteAScript(scriptId);
	}
}



function editScript(obj) {
	var scriptId = getScriptId(obj);
	var scriptName = getScriptName(obj);
	window.open("editScript.jsp?name=" + scriptName + "&scriptId=" + scriptId);// need
	// to
	// refactor
	// ...modal
}
function viewScript(obj) {
	var scriptId = getScriptId(obj);
	var scriptName = getScriptName(obj);
	window.open("scriptview.jsp?name=" + scriptName + "&scriptId=" + scriptId);
}
function getScriptId(obj) {
	var row = obj.parentNode.parentNode;
	var tbody = row.parentNode;
	var scriptId = $(tbody).children("tr").eq(row.rowIndex - 1).children("td")
			.eq(1).text();
	return scriptId;
}
function getScriptName(obj) {
	var row = obj.parentNode.parentNode;
	var tbody = row.parentNode;
	var scriptName = $(tbody).children("tr").eq(row.rowIndex - 1)
			.children("td").eq(0).text();

	return scriptName;
}
