

/* * * * * * * * * * * * * * * * * * * * * * */



/* Loads the fragment and inserts response html into given element.
 * If callback function is given, will call it after finished
 */
 
 function loadFragmentInToElement(fragment_url, element_id, callback, method) {
	if(!method) method = "POST";
	
	var data, url;
	if(method == "POST") {
		 data = fragment_url.split("?")[1];
		 url = fragment_url.split("?")[0];
	} else {
		 data = "";
		 url = fragment_url;		
	}
	
	if(method != "GET" && method != "POST") alert("Error: invalid method when executing remote query. Use GET or POST.");
	
	if(method == "GET") if(! isValidFragment(fragment_url)) return false;
	
	var element = document.getElementById(element_id);
	
	if(! isValidFragment(fragment_url)) return false;
	document.body.style.cursor = "progress";

	if(!callback) {
		callback = function() {};
	} 

	// Mootools 1.2 acepts get || post
	if(method == "GET") method = "get";	
	if(method == "POST") method = "post";	

	var req = new Request.HTML({  
		method: method,  
		url: url,  
		data: data,  
		update: element,  
		onComplete: callback  
		}).send();  
	
	document.body.style.cursor = "auto";
	return true;
}
 
/* Loads the fragment and calls the callback function with
 * response as a parameter
 */
function loadFragment(fragment_url, callback, method) {
	var ret;
	if(!method) method = "POST";
	if(method != "GET" && method != "POST") alert("Error: invalid method when executing remote query. Use GET or POST.");
	
	if(method == "GET") if(! isValidFragment(fragment_url)) return false;

	document.body.style.cursor = "progress";
	
	var data, url;
	if(method == "POST") {
			 data = fragment_url.split("?")[1];
			 url = fragment_url.split("?")[0];
	} else {
		 data = "";
		 url = fragment_url;		
	}


	// Mootools 1.2 acepts get || post
	if(method == "GET") method = "get";	
	if(method == "POST") method = "post";	
	
	var req = new Request({  
		method: method,  
		url: url,  
		data: data,  
		onComplete: callback  
		}).send();  
	
	document.body.style.cursor = "auto";
	return true;
}

/* Returns url base */
function GetBaseRef() {
	 var baseref = location.href.substring(0,location.href.lastIndexOf("/") + 1);
	 return baseref;
}

/* 
 * Internet Explorer maximum URL length is 2,083 Characters, 
 * though RFC 2616 that defines HTTP does not set limitations to URL size
 * Ref: http://support.microsoft.com/default.aspx?scid=KB;en-us;q208427
 * This function checks if the fragment length is valid
 * TODO: I DONT KNOW IF THIS APPLIES TO IE6???
 */
function isValidFragment(fragment_url) {
	if(! self.GetBaseRef) {
		alert("Error: GetBaseRef function not included.... ");
		return false;
	}

	var strFullUrl = GetBaseRef() + fragment_url;
	
	if( strFullUrl.length > 2083) {
		alert("Error: URL length " + strFullUrl.length  + " in remote query exceeds 2083 characters, which is the limit in IE");
		return false;		
	} else {
		return true;	
	}	
}


