// JavaScript Document
jQuery(document).ready(function() {

	 jQuery('#addOrganizationPost').click(function() {
		 jQuery('#loadingOrgs').html('<img src="/wp-content/plugins/orgs_news/indicator.gif">');								  	
		 jQuery.post("/wp-content/plugins/orgs_news/ajax_handle.php", { post_id: jQuery('#post_ID').attr('value'),org_id : jQuery('#organizationId').attr('value'),action:'add'},
					  function(data){
						  if(data.success)
						  {
								jQuery('#organizations').append('<tr  id="'+data.post_id+'-'+data.org_id+'">'+
				'<td><a href="/'+data.name+'.html" target="_blank">'+data.title+'</a></td>'+
				'<td><a href="#" onclick="return deletePostOrganiation(\''+data.post_id+'\',\''+data.org_id+'\')">Delete</a></td>'+
			  '</tr>	');
								jQuery('#loadingOrgs').html('');	
						  } //end if success
						  else alert("There was an error");
					  }, "json");
			});
	 jQuery("#organization").autocomplete("/wp-content/plugins/orgs_news/ajax_handle.php", { minChars:1,extraParams :{post_id: jQuery('#post_ID').attr('value'),action:'getOrgs'},onItemSelect : changeOrgId,maxItemsToShow  :200 });
	 
	 jQuery("#organization").result(changeOrgId);

	 
	 function changeOrgId(event, data, formatted) {
		 jQuery('#organizationId').attr('value',data[1]);
	 }
	
});

	function deletePostOrganiation(post_id,org_id) {
		var a=confirm("Are you sure you want to delete?");
		if(!a) return false;
		
		jQuery('#loadingOrgs').html('<img src="/wp-content/plugins/orgs_news/indicator.gif">');
		jQuery.post("/wp-content/plugins/orgs_news/ajax_handle.php", { post_id: post_id,org_id : org_id,action:'delete'},
					  function(data){
						  if(data.success)
						  {
								
							  jQuery('#organizations #'+data.post_id+'-'+data.org_id).remove();
							  jQuery('#loadingOrgs').html('');	
						  } //end if success
						  else alert("There was an error");
					  }, "json");
		return false;
	}

