// safety message to stop accidental deletions
function update_my_listing(apt, action, mybuttons) {
	if (action == 'quote_remove') {
		listing_actions(apt, action, mybuttons);
	} else {
		listing_actions(apt, action, mybuttons);
	}
}	

function redraw_buttons(mybuttons) { 
	if (dojo.byId("detailsButtonContainer")) {
	 	dojo.xhrGet( { 
 			url: "do/update_listing.php?function=redrawButtons&apt_id=" + mybuttons, 
 	    	handleAs: "text",
	   	  	timeout: 10000, 
   		  	load: function(response, ioArgs) { 
    	   		dojo.byId("detailsButtonContainer").innerHTML = response; 
    	   		dojo.byId("detailsButtonContainer2").innerHTML = response; 
    	 		return response; 
    	 	},
    	error: function(response, ioArgs) { 
    		console.error("HTTP status code: ", ioArgs.xhr.status); 
    		return response; 
    		}
    	});
	} else {
		return true;
	}
}

// actually does the listing changes
function listing_actions(apt, action, mybuttons) {
	var contentNode = dojo.byId("listedApt[" + apt + "]");
	if (!dojo.byId("listedApt[" + apt + "]")) {
		dojo.xhrGet({
	    	url: "do/update_quote.php?" + action + "=" + apt, 
			handleAs: "text",
			load: function(data,args){
				update_quote_list(apt);
				redraw_buttons(apt);
			},
			// if any error occurs, it goes here:
			error: function(error,args){
				console.warn("error!",error);
			}
		});
	} else {
	var myHeight = dojo.byId("listedApt[" + apt + "]").offsetHeight;
//	contentNode.innerHTML = draw_loader_large(myHeight);
		dojo.xhrGet({
	    	url: "do/update_quote.php?" + action + "=" + apt, 
			handleAs: "text",
			load: function(data,args){
				// fade out the node we're modifying
				dojo.fadeOut({
					node: contentNode,
					onEnd: function(){
					  // set the data, fade it back in
					  contentNode.innerHTML = data; 
					  dojo.fadeIn({node: contentNode}).play();    
					  update_quote_list(apt);
					  fade_out_message(apt);
						redraw_buttons(mybuttons);
					}
				}).play();
			},
			// if any error occurs, it goes here:
			error: function(error,args){
				console.warn("error!",error);
			}
		});
	}
} 
// fades out advisory message and collapses div
function fade_out_message(apt) {
	var messageContentNode = dojo.byId("messageText[" + apt + "]");
	dojo.fadeOut({
		node: messageContentNode,
		delay:5000,
		properties:{
					height: { end: 0, unit:"px" },
					padding: { end: 0, unit:"px" },
					margin: { end: 0, unit:"px" }
					}
	}).play();
}
// simply resets the quote list box
function update_quote_list(apt) { 
	var myHeight = document.getElementById("quoteRequestListing").offsetHeight;
	dojo.byId("quoteRequestListing").innerHTML = draw_loader_small(myHeight);
 	dojo.xhrGet( { 
 		url: "do/update_listing.php?function=update_quote_list", 
     	handleAs: "text",
     	timeout: 10000, 
     	load: function(response, ioArgs) { 
       		dojo.byId("quoteRequestListing").innerHTML = response; 
     		return response; 
     	},
    error: function(response, ioArgs) { 
    	console.error("HTTP status code: ", ioArgs.xhr.status); 
    	return response; 
    	}
    });
}

// simply resets the quote listing (not the box)
function update_apartment_quote_listing() { 
 	dojo.xhrGet( { 
 		url: "do/update_listing.php?function=update_apartment_quote_list", 
     	handleAs: "text",
     	timeout: 10000, 
     	load: function(response, ioArgs) { 
       		dojo.byId("selectedApartmentsQuoteList").innerHTML = response; 
     		return response; 
     	},
    error: function(response, ioArgs) { 
    	console.error("HTTP status code: ", ioArgs.xhr.status); 
    	return response; 
    	}
    });
}

function update_my_listing(apt, action) {
	if (action == 'quote_remove') {
		listing_actions(apt, action);
	} else {
		listing_actions(apt, action);
	}
}	

// function for function - for expandability
function modify_quote(apt, action, mybuttons) { 
	update_my_listing(apt, action, mybuttons);
}
// function for function - for expandability
function modify_quote_apartment_listing(apt, action) { 
	var contentNode = dojo.byId("listedApt[" + apt + "]");
		dojo.xhrGet({
	    	url: "do/update_quote.php?" + action + "=" + apt, 
			handleAs: "text",
			load: function(data,args){
				update_quote_list(apt);
				update_apartment_quote_listing();
			},
			// if any error occurs, it goes here:
			error: function(error,args){
				console.warn("error!",error);
			}
		});
} 
// draws a large loader gif while loading
function draw_loader_large(myHeight) {
	var loaderLarge = '<div align="center" style="height:' + myHeight + '; background:url(images/loader_large.gif) 50% 50% no-repeat;" valign="center"></div>';
	return loaderLarge;
}
// draws a small loader gif while loading
function draw_loader_small(myHeight) {
	var loaderSmall = '<div align="center" style="height:' + myHeight + 'px; background:url(images/loader_small.gif) 50% 50% no-repeat;" valign="center"></div>';
	return loaderSmall;
}