
function deleteMapping(part_num, warehouse_id, activity_id)
{
	var del_callback = {
		success:function(response){
			var data = YAHOO.lang.JSON.parse(response.responseText);
			if(data == "success"){
				location.reload(true);
			}
		},
		failure:function(response){
			//failure is not an option
			alert("An error has occured please try again later.");
		}
	};
	var request = YAHOO.util.Connect.asyncRequest('POST', baseIndexURL+"partnum_activity_mapping/deleteMapping", del_callback, "&part_num=" + part_num + "&warehouse_id=" + warehouse_id + "&activity_id=" + activity_id);
}


function initialize(response)
{

//add in an ajax processing thingy
YAHOO.util.Dom.setStyle('ajax_processing', 'display', 'block');


	YAHOO.util.Event.onDOMReady(function(){


var saveWarehouseId = function(callback, newValue){
	var record = this.getRecord();
	var column = this.getColumn();
	var oldValue = this.value;
	var datatable = this.getDataTable();
	var new_value = newValue;

	var old_part_num = record._oData.part_num;
	var old_warehouse_id = record._oData.warehouse_id;
	var old_activity_id = record._oData.activity_id;

	var save_callback = {
		success:function(o){
			var data = YAHOO.lang.JSON.parse(o.responseText);
			if(data == "success"){
				callback(true, new_value);
			}
			else{
				callback();
			}
		},
		failure:function(o){
			//errors are not an option
			callback();
		}
	};
	var request = YAHOO.util.Connect.asyncRequest('POST', baseIndexURL+"partnum_activity_mapping/saveWarehouseId", save_callback, "&part_num=" + old_part_num + "&warehouse_id=" + old_warehouse_id + "&activity_id=" + old_activity_id + "&new_value=" + new_value);
};

var saveActivityId = function(callback, newValue){
	var record = this.getRecord();
	var column = this.getColumn();
	var oldValue = this.value;
	var datatable = this.getDataTable();
	var new_value = newValue;

	var old_part_num = record._oData.part_num;
	var old_warehouse_id = record._oData.warehouse_id;
	var old_activity_id = record._oData.activity_id;

	var save_callback = {
		success:function(o){
			var data = YAHOO.lang.JSON.parse(o.responseText);
			if(data == "success"){
				callback(true, new_value);
			}
			else{
				callback();
			}
		},
		failure:function(o){
			//errors are not an option
			callback();
		}
	};
	var request = YAHOO.util.Connect.asyncRequest('POST', baseIndexURL+"partnum_activity_mapping/saveActivityId", save_callback, "&part_num=" + old_part_num + "&warehouse_id=" + old_warehouse_id + "&activity_id=" + old_activity_id + "&new_value=" + new_value);
};

var savePartNum = function(callback, newValue){
	var record = this.getRecord();
	var column = this.getColumn();
	var oldValue = this.value;
	var datatable = this.getDataTable();
	var new_value = newValue;

	var old_part_num = record._oData.part_num;
	var old_warehouse_id = record._oData.warehouse_id;
	var old_activity_id = record._oData.activity_id;

	var save_callback = {
		success:function(o){
			var data = YAHOO.lang.JSON.parse(o.responseText);
			if(data == "success"){
				callback(true, new_value);
			}
			else{
				callback();
			}
		},
		failure:function(o){
			//errors are not an option
			callback();
		}
	};
	var request = YAHOO.util.Connect.asyncRequest('POST', baseIndexURL+"partnum_activity_mapping/savePartNum", save_callback, "&part_num=" + old_part_num + "&warehouse_id=" + old_warehouse_id + "&activity_id=" + old_activity_id + "&new_value=" + new_value);
};


		//dom is ready here
		//load all mappings into a datatable
		//alert(baseIndexURL);
		callback = {
			success:function(response){
				data = YAHOO.lang.JSON.parse(response.responseText);
				
				/***custom formatters***/
				function deleteFormatter(elCell, oRecord, oColumn, oData)
				{
					data = oRecord.getData();
					
					//alert();
					//elCell.innerHTML = "asl;dfkjal;sdjkfakls;df";
					elCell.style.cursor = "pointer" ;
					elCell.innerHTML = "<img onclick='deleteMapping(\"" + data.part_num + "\", \""+ data.warehouse_id +"\", \"" + data.activity_id + "\");' src='" + base_url + "images/aim/delete.png' style='height:15px;'>";
					elCell.style.textAlign = 'center';
				}


				var columnDefs = [
					{key:"warehouse_id", label:"Warehouse ID"},
					{key:"part_num", label:"Part Number"},
					{key:"ShortDesc", label:"Short Description"},
					{key:"ActivityDescription", label:"Activity Description"},
					//{key:"activity_id", label:"Activity ID"},
					{key:"delete", label:"Delete", formatter:deleteFormatter}
				];

				var dataSrc = new YAHOO.util.DataSource(data);

				dataSrc.responseType = YAHOO.util.DataSource.TYPE_JSARRAY;

				dataSrc.responseSchema = {
					fields: ["part_num", "warehouse_id", "activity_id", "ShortDesc", "ActivityDescription"]
				};

				var activityTable = new YAHOO.widget.DataTable("mapping_table", columnDefs, dataSrc, {
					paginator: new YAHOO.widget.Paginator({rowsPerPage:10})
				});

				// Set up editing flow
				var highlightEditableCell = function(oArgs) {
					var elCell = oArgs.target;
					if(YAHOO.util.Dom.hasClass(elCell, "yui-dt-editable")) {
						this.highlightCell(elCell);
					}
				};
			    activityTable.subscribe("cellMouseoverEvent", highlightEditableCell);
		        activityTable.subscribe("cellMouseoutEvent", activityTable.onEventUnhighlightCell);
		        activityTable.subscribe("cellClickEvent", activityTable.onEventShowCellEditor);

				 //initialize new_row button
				 var btn = new YAHOO.widget.Button("new_row",{label:"Add New Row"});
				 btn.on("click", function(o){
					//D.get('new_row_form').submit();
					addNewMapping();
				 }, this, true);
				 //remove in an ajax processing thingy
				YAHOO.util.Dom.setStyle('ajax_processing', 'display', 'none');
			},
			failure:function(response){
				alert("An error has occured. \nReloading page.");
				//remove in an ajax processing thingy
				YAHOO.util.Dom.setStyle('ajax_processing', 'display', 'none');
				location.reload(true);
			}
		};
		var request = YAHOO.util.Connect.asyncRequest('POST', baseIndexURL+"partnum_activity_mapping/listMappings",
		 callback, "");
	});

	return true;
}