/**************************************************************************
Script:	jPart.BingListEntry.js
Author:	SharejPoint.com
Purpose:  Entry data to a list jPart for designed to go along with Bing Map
Dependencies:
	jPointLoader.js has to be on the same page or form editor webpart
	jPart.BingList.js is optional to refresh map upon data entry
*************************************************************************/
//Configure webpart and store info into memory for later access
myjPart.ShowConfigDefaults = [	
	["LookupSiteURL","Absolute path to lookup list site location","/"]
    ,["LookupListName","Lookup list name","LookupList"] 
];
myjPart.setjPartOptions({Plugin:{Name:'Bing List Entry', Version:'2.0', Origin:'SharejPoint', Description:'Allows user with contributor rights to add new items to SharePoint list on a Bing map', Developer:'Samir Sijercic'}});

//Start Webpart logic code ----------------------------------- 
$(document).ready(function() { //Wait page to load and then apply webpart logic
	if(myjParts[JpartProcessing] != null)
	{
		var thisWebpart = myjParts[JpartProcessing];
		try
		{
			//jPart user code goes here ---------------------------
			if(thisWebpart.Options.Local != null)
			{
				var Options = thisWebpart.Options.Local;
				thisWebpart.MainDiv.append("<table id='myMapListEntryTable'></table>");		
				$("#myMapListEntryTable").append("<tr><td>Description:</td><td><input maxlength='63' type='text' id='DescriptionTxb' /></td></tr>");
				$("#myMapListEntryTable").append("<tr><td>Address:</td><td><input maxlength='256' type='text' id='AddressTxb' /></td></tr>");
				$("#myMapListEntryTable").append("<tr><td colspan='2'></td></tr>");
				$("#myMapListEntryTable").append("<tr><td colspan='2'><input id='submitMapListItem' type='button' value='submit' /></td></tr>");
				$("#submitMapListItem").click(function () { 
	      			saveListItem(Options.LookupSiteURL, Options.LookupListName);
	    		});
			}		
					
			//-----------------------------------------------------
		}catch(err){};
		JpartProcessing = JpartProcessing + 1;
	}
});

function saveListItem(siteURL, listName)
{
	if($("#DescriptionTxb").val().length == 0){
		alert("Description is required!");
	}
	else if($("#AddressTxb").val().length == 0){
		alert("Address is required!");
	}
	else
	{
		jP.Lists.setSPObject(siteURL, listName).addItem([{ id:0, Title:$("#DescriptionTxb").val(), Address:$("#AddressTxb").val()}]);
		//Call drawMap on BingList.js jPart
		if(typeof drawMap() !== "undefined"){drawMap();}
	}
	
}
//End Webpart logic code -----------------------------------