/*
    If the div/span (divId) to be populated exists, inserts the template referred by 'theUrl'.
*/

function doBind(theUrl, divId) {
	if (dojo.byId(divId)) {
		dojo.io.bind({
			url: theUrl, 
			method: 'POST',
			useCache: false,
			load: function(type, data, evt){ dojo.byId(divId).innerHTML=data; },
			error: function(type, error) { alert('There was an error! '+error.message); }
		});
	}
}

/*
    If the div/span (divId) to be populated exists, inserts the template referred by 'theUrl'.
    Call the afterUpdateElement function ufter load.
*/

function doBindWithAfterUpdateElement(theUrl, divId, afterUpdateElement) {
	if (dojo.byId(divId)) {
		dojo.io.bind({
			url: theUrl, 
			method: 'POST',
			useCache: false,
			load: function(type, data, evt){
				dojo.byId(divId).innerHTML=data;
				if(afterUpdateElement) afterUpdateElement();				
			},
			error: function(type, error) { alert('There was an error! '+error.message); }
		});
	}
}

function doJSONBind(theUrl, errorDivId, afterJSONBindFunction) {
		var bindArgs = {
			url: theUrl, 
			load: function(type, data, evt) {},
			error: function(type, error) {
				if (dojo.byId(errorDivId)) {
					dojo.byId(errorDivId).innerHTML = "There was an error retrieving via JSON " + error.message;
				} else {
					alert("There was an error retrieving via JSON " + error.message);
				}
			},
			mimetype: "text/json"//,
			//content: params
		};

		var req = dojo.io.bind(bindArgs);
		dojo.event.connect(req, "load", this, afterJSONBindFunction);
}