function setPage(p) {
	page=p;
	firstResult= (page-1)*maxResults;
}

function noRenewItem(id, version) {
		if (!confirm("Are you sure?\n\nYour listing will be displayed through the remainder of your purchase and your credit card will not be charged again."))
		{ 
			return false;
		}
		  busy();
		  var api= "http://www.manta.com/adserver/flapi";
		  //ecnext.byId('norenew-link-'+id).innerHTML= 'Cancelling listing...';
		  dojo.xhrPost({
			url: api,
			sync: false,
			content: {
				"action": "noRenewItem",
				"id": id,
				"version": version,
				"ts": (new Date()).getTime()
			},
			load: function(responseObject, ioArgs){
				//console.dir(responseObject);
				//ecnext.byId('norenew-link-'+id).innerHTML= 'Cancelled listing';
				listAdInstances();
				return responseObject;
			},
			error: function(response, ioArgs){
				alert("Error cancelling renewal: " + response);
				//ecnext.byId('norenew-link-'+id).innerHTML= 'Cancel listing';
				ready();
				return response;
			},
			handleAs: "json"
		  });
		}
		
function cancelItem(id, version) {
		if (!confirm("Are you sure?\n\nYour listing will be removed immediately and your credit card will not be charged."))
		{ 
			return false;
		}
		  busy();
		  var api= "http://www.manta.com/api/adserver/flapi";
		  //ecnext.byId('norenew-link-'+id).innerHTML= 'Cancelling listing...';
		  dojo.xhrPost({
			url: api,
			sync: false,
			content: {
				"action": "cancelItem",
				"id": id,
				"version": version,
				"ts": (new Date()).getTime()
			},
			load: function(responseObject, ioArgs){
				//console.dir(responseObject);
				//ecnext.byId('norenew-link-'+id).innerHTML= 'Cancelled listing';
				listAdInstances();
				return responseObject;
			},
			error: function(response, ioArgs){
				alert("Error cancelling renewal and removing from site: " + response);
				//ecnext.byId('norenew-link-'+id).innerHTML= 'Cancel listing';
				ready();
				return response;
			},
			handleAs: "json"
		  });
		}


function FLDelete(id, version, items) {
		  busy();
		  var api= "http://www.manta.com/adserver/flapi";

		  //build list of item instances
		  var itemIntances= new Array();
		  
		  if(items != undefined) {
			  for (var i=0; i<items.length; i++) {
				  itemIntances[i]= {"itemInstanceId": items[i].itemInstanceId, "itemInstanceVersion": items[i].itemInstanceVersion};
			  }
		  }
		  
		  dojo.xhrPost({
			url: api,
			sync: false,
			content: {
				"action": "deleteManyInstances",
				"adInstances": dojo.toJson([{"adInstanceId": id, "adInstanceVersion": version}]),
				"itemInstances": dojo.toJson(items),
				"ts": (new Date()).getTime()
			},
			load: function(responseObject, ioArgs){
				//console.dir(responseObject);
				listAdInstances();
				return responseObject;
			},
			error: function(response, ioArgs){
				handleXhrError(ioArgs.xhr.responseText, "Error deleting ad");
				ready();
				return response;
			},
			handleAs: "json"
		  });
		}

function updateDisplayingCounts() {
			var total= 0;
			for(var state in FLADs_result.counts)
			{
				if(state == 'DELETED'){continue;}
				total+= FLADs_result.counts[state];
				if(ecnext.byId('flcounts-'+state) != undefined)
				{
					ecnext.byId('flcounts-'+state).innerHTML= FLADs_result.counts[state];
				}
				if(ecnext.byId('flfilter-'+state) != undefined)
				{
					ecnext.byId('flfilter-'+state).text= ecnext.byId('flfilter-default-'+state).text + ' (' + FLADs_result.counts[state] + ')';
				}
				if(review_state != undefined && review_state == state) {
					pagination= calculate_pagination(firstResult, maxResults, FLADs_result.counts[state]);
					ecnext.byId('fllow').innerHTML= pagination.low;
					ecnext.byId('flhi').innerHTML= pagination.hi;
					ecnext.byId('fltotal').innerHTML= pagination.total;
				}
			}
			ecnext.byId('flcounts-ALL').innerHTML= total;
			ecnext.byId('flfilter-ALL').text= ecnext.byId('flfilter-default-ALL').text + ' (' + total + ')';
			if(review_state == undefined || review_state == '' || review_state == 'ALL')
			{
				pagination= calculate_pagination(firstResult, maxResults, total);
				ecnext.byId('fllow').innerHTML= pagination.low;
				ecnext.byId('flhi').innerHTML= pagination.hi;
				ecnext.byId('fltotal').innerHTML= pagination.total;
			}
			
			update_pagination_links();
		}
		function update_pagination_links() {
			var html= '';
			for(var i=1; i<=pagination.pages; i++)
			{
				if(page == i) {
					html+= i.toString();
				}
				else {
					html+= ' <a href="#page'+i+'" onclick="setPage('+i+'); listAdInstances(); return false;">'+i+'</a> ';
				}
			}
			ecnext.byId('pagination').innerHTML= html;
		}
		
		function calculate_pagination(start, per_page, total) {  //start is a 0 based index
			var low= start+1;
			var hi= start+per_page;
			if(hi > total) {
				hi= total;
			}
			if(total==0) {
				low= 0;
			}
			var pages= Math.ceil(total / per_page);
			if(pages < 1) {
				pages= 1;
			}
			var retobj= {"hi": hi, "low": low, "total": total, "pages": pages, "per_page": per_page};
			return retobj;
		}
		
		function loadTable(table, obj, header) {
			var start= 0;
			if(header != undefined && header){start=1;}
			
			for(var r=start; r < obj.length + start; r++)
			{
				if(r < table.rows.length) //row exists
				{
					//delete existing row
					table.deleteRow(r);
				}
				//add new row
				var tr= table.insertRow(r);
				tr.className= obj[r-start].rowclass;
				
				//load cells into new row
				var data_row= obj[r-start].data;
				for(var d=0; d < data_row.length; d++)
				{
					var td= tr.insertCell(d);
					td.innerHTML= data_row[d];
				}
			}
			
			//remove excess rows
			while(table.rows.length > start + obj.length)
			{
				table.deleteRow(start + obj.length);
			}
		}
		
		
		
		function getRootIndustriesForCategory(p) {
		  var api= "http://www.manta.com/adserver/flapi";
		  if(example_links_category[p.categoryId] == undefined) {
			  dojo.xhrGet({
				url: api,
				sync: false,
				content: {
					"testAdmin": '1',
					"testSubId": "admin_page_test",
					"action": "getRootIndustriesForCategory",
					"categoryId": p.categoryId,
					"ts": (new Date()).getTime()
				},
				load: function(responseObject, ioArgs){
					example_links_category[p.categoryId]= responseObject.result;
					showExampleCallback();
					return responseObject;
				},
				error: function(response, ioArgs){
					return response;
				},
				handleAs: "json"
			  });
		  }
		  else {
		  	showExampleCallback();
		  }
		}
		function lookupCitiesByMsa(p) {
		  var api= "http://www.manta.com/adserver/flapi";
		  if(example_links_msa[p.ecMsaCd] == undefined) {
			  dojo.xhrGet({
				url: api,
				sync: false,
				content: {
					"testAdmin": '1',
					"testSubId": "admin_page_test",
					"action": "lookupCitiesByMsa",
					"ecMsaCd": p.ecMsaCd,
					"ts": (new Date()).getTime()
				},
				load: function(responseObject, ioArgs){
					example_links_msa['C'+p.ecMsaCd]= responseObject.result;
					showExampleCallback();
					return responseObject;
				},
				error: function(response, ioArgs){
					return response;
				},
				handleAs: "json"
			  });
		  }
		  else {
		  	showExampleCallback();
		  }
		}
		function showExample(p) {
			busy();
			example_link_msa= 'C'+p.ecMsaCd;
			example_link_category= p.categoryId;
			
			lookupCitiesByMsa({"ecMsaCd":p.ecMsaCd});			
			getRootIndustriesForCategory({"categoryId":p.categoryId});
		}
		
		function popPage(url)
		{
			if (popWin && !popWin.closed && popWin.focus){
				popWin.focus();
			}
			else {
				popWin = window.open(url,'','width=800,height=600');
			}
		}
		function openExampleWindow(url)
		{
			var exampleWindow= window.open(url);
			if(exampleWindow == undefined)
			{
				alert("The example page has been blocked by your popup blocker.\nPlease adjust your pop-up blocker settings.");
			}
			else
			{
				exampleWindow.focus();
			}
		}
		
		function showExampleCallback() {
			//console.warn('showExampleCallback()');
			if(example_links_category[example_link_category] != undefined && example_links_msa[example_link_msa] != undefined)
			{
				var ind= example_links_category[example_link_category][0];
				var city= example_links_msa[example_link_msa][0].ecCityCd;
				//console.debug('city_cd:'+city);
				//console.debug('ind_cd:'+ind);
				
				var ind_level= 1;
				if(ind.length == 1){ind_level=2}
				else if(ind.length == 2){ind_level=3}
				else if(ind.length == 5){ind_level=4}
				else if(ind.length >= 8){ind_level=5} 
				
				example_links_msa[example_link_msa]= undefined;
				example_links_category[example_link_category]= undefined;
				
				ready();
				openExampleWindow('http://www.manta.com/mb_5'+ind_level+'_'+ind+'_'+city);
			}
				
		}
		
		if (typeof ecnext.table == "undefined") {
					ecnext.table = {};
				}
				ecnext.table.showFirstNRows = function (element_or_id, n) {
					var table = ecnext.byId(element_or_id);
					if(n == undefined) {n=-1}
					if(n<0 || n > table.rows.length)
					{
						n= table.rows.length;
					}
					
					for(var r=0; r<table.rows.length; r++)
					{
						if(r<n) {
							table.rows[r].style.display= '';
						}
						else {
							table.rows[r].style.display= 'none';
						}
					}
				}
				function CollapseTable(table, show_rows) {
					for(var r=0; r<=table.rows.length; r++)
					{
						table.rows[r].style.display= '';
					}
				}
				function showCountDetails() {
					ecnext.table.showFirstNRows('count_details');
					ecnext.byId('show_count_details').style.display= 'none';
					ecnext.byId('hide_count_details').style.display= '';
				}
				function hideCountDetails() {
					ecnext.table.showFirstNRows('count_details', 1);
					ecnext.byId('show_count_details').style.display= '';
					ecnext.byId('hide_count_details').style.display= 'none';
				}
				
				function busy() {
	ecnext.byId('loading_table_overlay_container').style.display= '';
}
function ready() {
	ecnext.byId('loading_table_overlay_container').style.display= 'none';
}
				
