var throbber = '<img alt="Loading..." src="'+docmodeModule+'/images/progressThrobber1.gif" >';
var dataaaaa = '';
// This is the most basic js file and without this file nothing would work
// related to AJAX

// move HTML contents from one node to another node
// src and dest refer to the id name os the HTML nodes
function dojoCopyInnerHtml(src,dest){
	dojo.byId(dest).innerHTML = dojo.byId(src).innerHTML;
}
// moving from src to dest and then deleting the src contents
function dojoMoveInnerHtml(src,dest){
	dojoCopyInnerHtml(src, dest);
	dojo.byId(src).innerHTML = '';
}
// This is the fundamental function without which the whole of docmode would
// not work. To understand this is key

function dojoHttpGet(action, divId, callback){
// This function has 3 arguments action divId and callback
// This method is the AJAX call to an action servlet 
// On request to the servlet the server sends the response data back
// This data is stored in the HTML node whos id attribute is divId
// After the AJAX call has been made the user has the option to
// call a callback function
// The callback function is called only when the action servlet returns
// data and stores it in the HTML node divId. If call the dojoHttpGet
// function with only 2 arguments then no callback function is called.
// only the servlet is executed.	
	var contentNode = dojo.byId(divId);
	// get Node Variable
	var getObj = {
		// This is the dojo Toolkit AJAX object
		// consists of the url of AJAX call
		// documentModule is www.docmode.com
		// action can be any one of the actions specified in struts-config.xml
		// actions end with .do example /myCustomAction.do
		// so www.docmode.com/myCustomAction.do is url	
	    url: docmodeModule + action,
	    // return data as text
	    handleAs: "text",
	    load: function(data,args){
			// The load variable is a function that is executed
			// when data is received from the action servlet
			// fade out the node we're modifying ... decrease opacity
		    // over time
			if(contentNode != null){
				dojo.fadeOut({
					// node we are fading out
					node: contentNode,
					//at the end of fading out
					onEnd: function(){
						// set the data, fade it back in
						contentNode.innerHTML = data; 
						dojo.fadeIn({ 
							node: contentNode,
							onEnd: callback
							}).play();  
					}
				}).play();
			}
	    },
	    // if any error occurs, it goes here:
	    error: function(error,args){
	    	console.warn("error!",error);
	    }
	};
	// So getObj is created now make the ajax call
	dojo.xhrGet(getObj);
	// till the loading happens let the node content be the throbber image
	contentNode.innerHTML = throbber;
}

// This function is the same as the above with the exception that
// when the browser waits for the action servlet to return data
// there is no throbber image shown in the contentNode divId

function dojoSimpleHttpGet(action, divId, callback){
	var contentNode = dojo.byId(divId);
	var getObj = {
	    url: docmodeModule + action,
	    handleAs: "text",
	    load: function(data,args){
			// fade out the node we're modifying
			if(contentNode != null){
					contentNode.innerHTML = data; 
			}
	    },
	    // if any error occurs, it goes here:
	    error: function(error,args){
	    	console.warn("error!",error);
	    }
	};
	dojo.xhrGet(getObj);
	// No contentNode.innerHTML = throbber;
}


// Another variation of the above 2 AJAX calls
// see below comment
function dojoSimpleAddHttpGet(action, divId, callback){
	var contentNode = dojo.byId(divId);
	var getObj = {
	    url: docmodeModule + action,
	    handleAs: "text",
	    load: function(data,args){
			// fade out the node we're modifying
			if(contentNode != null){
					if(data!=null) {
					// HERE IS THE DIFFERENCE
					// Instead of replacing the old content in contentNode
					// we are adding the content to the existing content
					// this is used in chatboxes to update data	
					contentNode.innerHTML = contentNode.innerHTML+data;
					}
			}
	    },
	    // if any error occurs, it goes here:
	    error: function(error,args){
	    	console.warn("error!",error);
	    }
	};
	dojo.xhrGet(getObj);
}

// This is the POST version of the dojoHttpGet call
// the difference lying in the additional argument formname
// This AJAX call is made while submitting forms whereas
// dojoHttpGet call is made when doing simple things like
// clicking links
function dojoHttpPost(action, formname, divId, callback){
	var contentNode = dojo.byId(divId);
	// The AJAX object
	var postObj = {
		url: docmodeModule + action,
		// new field in the postObj ... this is the name of the HTML form	
		form: formname,
	    handleAs: "text",
	    load: function(data,args){
			// fade out the node we're modifying
			dojo.fadeOut({
				node: contentNode,
				onEnd: function(){
					contentNode.innerHTML = data; 
					dojo.fadeIn({ 
						node: contentNode,
						onEnd: callback
						}).play();
				}
			}).play();
	    },
	    // if any error occurs, it goes here:
	    error: function(error,args){
	    	console.warn("error!",error);
	    }
	};
	dojo.xhrPost(postObj);
	contentNode.innerHTML = throbber;
	return false;
}

function dojoSimpleHttpPost(action, formname, divId, callback){
	var contentNode = dojo.byId(divId);
	var postObj = {
		url: docmodeModule + action,
		form: formname,
	    handleAs: "text",
	    load: function(data,args){
			if(contentNode != null){
				contentNode.innerHTML = data; 
				if(callback!=null) callback();
			}
	    },
	    // if any error occurs, it goes here:
	    error: function(error,args){
	    	console.warn("error!",error);
	    }
	};
	dojo.xhrPost(postObj);
	return false;
}

function dojoGetData(action, formname,callback) {
	var postObj = {
		url: docmodeModule + action,
		form: formname,
	    handleAs: "text",
	    load: function(data,args){
			if(callback!=null) callback(data);			
	    },
	    // if any error occurs, it goes here:
	    error: function(error,args){
	    	console.warn("error!",error);
	    }
	};
	dojo.xhrPost(postObj);
	return '';
}
