/*
	QuickAdmin (c) Copyright 2003 - 2005 by Brian Wojtczak.
	Rights and Ownership Reserved.  http://www.astrolox.com/
	
	This file also (c) Copyright by Lawyers Online Ltd.
	Rights Reserved.  http://www.lawyersonline.co.uk/
	
	Licensing Terms and Conditions Apply.  Contact Lawyers Online.
	
  --   ---------------------------------------------------------------
	
	$HeadURL: svn://HeBe/Customers/FSSLtd/trunk/includes/xmlhttp.js $
	$Id: xmlhttp.js 3176 2009-02-23 13:10:31Z qbnut $
	
  --   ---------------------------------------------------------------
	
	Thanks to:
	
	http://www.jibbering.com/2002/4/httprequest.html
	
	http://ajaxblog.com/archives/2005/06/01/async-requests-over-an-unreliable-network
	
	
	Also Useful Reading:
	
	http://www.w3.org/TR/XMLHttpRequest/
	
	http://www.sirensclef.com/js/xmlhttp.js
	
	http://formassembly.com/blog/ajax-not-all-about-xmlhttprequest/
	http://formassembly.com/blog/ajax-its-not-all-about-xmlhttprequest-part-ii/
	http://www.adaptivepath.com/ideas/essays/archives/000385.php
	
	http://developer.mozilla.org/en/docs/AJAX
	http://www.onlamp.com/pub/a/onlamp/2005/05/19/xmlhttprequest.html
	
	http://en.wikipedia.org/wiki/Not_Invented_Here
	
*/

function get_xmlhttp() {

var xmlhttp=false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
// JScript gives us Conditional compilation, we can cope with old IE versions.
// and security blocked creation of the objects.
 try {
  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
 } catch (e) {
  try {
   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  } catch (E) {
   xmlhttp = false;
  }
 }
@end @*/
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
	try {
		xmlhttp = new XMLHttpRequest();
	} catch (e) {
		xmlhttp=false;
	}
}
if (!xmlhttp && window.createRequest) {
	try {
		xmlhttp = window.createRequest();
	} catch (e) {
		xmlhttp=false;
	}
}

return xmlhttp;
}

function do_httpcall( page, callbackfunc_eval, callbackfunc_after, errorobj, async, postdata ) {
	
	// Set either of the callbackfunc variables to false if you don't want to use them.
	// Set errorobj to false if you don't want to use it.
	
	// -- X -------------------------------------------------------------------------- X --
	//       Set async to true. Only set it to false if you're in the unload handler.
	//       RECAP: SET async TO true
	// -- X -------------------------------------------------------------------------- X --
	
	// this code is based of the code written for jstabs,
	// created by Brian 'Astrolox' Wojtczak in the year 2008.
	
	// jsdebug( 'do_httpcall called' );
	
	var error = '';
	var emsg = '';
	
	var date = new Date();
	var now = date.getTime();
	
	page = page + '&jsrt=' + now;
	
	// jsdebug( 'JS HTTP Call to <a href="'+ page  +'">here</a>.' );
	
	try {
		var httpcall_done_callback=function() { // this is a new callback funcation
			
			try {
				// jsdebug( 'xmlhttp readyState is ' + xmlhttp.readyState );
				
				if (xmlhttp.readyState==4) {
					
					var result = xmlhttp.responseText;
					
					if (xmlhttp.status!=200) {
						
						jsdebug( 'HTTP status ' + xmlhttp.status + ' when asking for <a href="' + page + '">here</a>.' );
						emsg = 'The HTTP server reported an unexpected status code when trying to access JS content.';
						
					} else {
						
						// yes.. i call a callback from a callback - lol
						if ( callbackfunc_eval != false ) {
							callbackfunc_eval( result );
						} else {
							
							// jsdebug( 'result starts with "' + result.substr(0,6) +'"' );
							if ( 	result.substr(0,2) == 'js'
							||	result.substr(1,2) == 'js'
							||	result.substr(2,2) == 'js'
							) {
								eval( result );
							} else {
								jsdebug( 'The returned data doesn\'t start with the characters "js".' );
								emsg = 'The HTTP callback didn\'t return the requested Javascript code.';
							}
						}
						
					} // END if (xmlhttp.status!=200)
				} // END if (xmlhttp.readyState==4)
				
			} catch( e ) {
				emsg = e;
				jsdebug( 'do_httpcall callback Caught javascript Exception: ' + emsg );
			}
			
			if ( emsg != '' ) {
				
				jsdebug( 'Did not receive JS output from <a href="' + page + '">here</a>.' );
				
				if ( errorobj != false ) {
					
					error = error + '<div style="padding: 1em; border: 2px solid red;" class="fadedred">\n';
					error = error + '<h2>FATAL ERROR</h2>\n';
					error = error + '<p>The Javascript code caught an error, this could be due to transient server issues. </p>\n';
					error = error + '<p>' + emsg + '</p>\n';
					error = error + '<p>Please <a href="' + myurl + '">fully refresh</a> this page. ';
					error = error + 'If this persists please contact an administrator. </p>\n';
					error = error + '</div>\n';
					
					if ( typeof(result) != 'undefined' ) {
						error = error + '<div style="padding: 1em; border: 2px solid red; ';
						error = error + 'padding-bottom: 2em; border-top: 0px;" class="fadedblack">\n';
						error = error + result;
						error = error + '</div>\n';
					}
					
					errorobj.innerHTML = error;
				
				} else {
					
					error = error + '-- FATAL ERROR --\n';
					error = error + emsg;
					error = error + '\n';
					error = error + 'Shall I proceed by performing a full refresh?\n';
					
					if ( confirm( error ) ) {
						do_page_refresh();
					}
				}
			}
			
			if ( typeof(result) != 'undefined' || emsg != '' ) {
				
				// yes.. i call a callback from a callback - lol
				if ( callbackfunc_after != false ) {
					callbackfunc_after();
				}
				
			}
			
		} // END call back function 
		
		// do the actual call now
		
		var xmlhttp=get_xmlhttp();
		
		if ( postdata != null && postdata != false ) {
			xmlhttp.open( "POST", page, async );
			xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		} else {
			xmlhttp.open( "GET", page, async );
		}
		
		if ( async == true ) {
			xmlhttp.onreadystatechange=httpcall_done_callback;
		}
		
		if ( postdata != null && postdata != false ) {
			xmlhttp.send(postdata)
		} else {
			xmlhttp.send(null)
		}
		
		if ( async == false ) {
			httpcall_done_callback();
		}
		
	} catch( e ) {
		
		emsg = e;
		
		jsdebug( 'do_httpcall main Caught javascript Exception: ' + emsg );
		jsdebug( 'This was while attempting to receive JS output from <a href="' + page + '">here</a>.' );
		
		if ( errorobj != false ) {
			
			error = error + '<div style="padding: 1em; border: 2px solid red;" class="fadedred">\n';
			error = error + '<h2>FATAL ERROR</h2>\n';
			error = error + '<p>The Javascript code caught an error, this could be due to transient server issues. </p>\n';
			error = error + '<p>' + emsg + '</p>\n';
			error = error + '<p>Please re-select this tab, or <a href="' + myurl + '">fully refresh</a> this page. ';
			error = error + 'If this persists please contact an administrator. </p>\n';
			error = error + '</div>\n';
			
			errorobj.innerHTML = error;
			
		} else {
			error = error + '-- FATAL ERROR --\n';
			error = error + emsg;
			error = error + '\n';
			error = error + 'Shall I proceed by performing a full refresh?\n';
			
			if ( confirm( error ) ) {
				do_page_refresh();
			}
		}
		
		if ( callbackfunc_after != false ) {
			callbackfunc_after();
		}
		
		return false;
	}
}

function do_plain_httpcall( page, callbackfunc_eval, callbackfunc_after, errorobj, async, postdata ) {
	
	// Set either of the callbackfunc variables to false if you don't want to use them.
	// Set errorobj to false if you don't want to use it.
	
	// -- X -------------------------------------------------------------------------- X --
	//       Set async to true. Only set it to false if you're in the unload handler.
	//       RECAP: SET async TO true
	// -- X -------------------------------------------------------------------------- X --
	
	// this code is used within chain_upload.js and dynamic_list.js
	// created by Craig Jones, 2008.
	// this code is based of the code written for jstabs,
	// created by Brian 'Astrolox' Wojtczak in the year 2008.
	
	var error = '';
	var emsg = '';
	
	var date = new Date();
	var now = date.getTime();
	
	// page = page + '&jsrt=' + now;
	
	try {
		var httpcall_done_callback=function() { // this is a new callback funcation
			
			try {
				// jsdebug( 'xmlhttp readyState is ' + xmlhttp.readyState );
				
				if (xmlhttp.readyState==4) {
					
					var result = xmlhttp.responseText;
					
					if (xmlhttp.status!=200) {
						
						// jsdebug( 'HTTP status ' + xmlhttp.status + ' when asking for <a href="' + page + '">here</a>.' );
						emsg = 'The HTTP server reported an unexpected status code when trying to access JS content.';
						
					} else {
						
						// yes.. i call a callback from a callback - lol
						if ( callbackfunc_eval != false ) {
							callbackfunc_eval( result );
						}
						
					} // END if (xmlhttp.status!=200)

				} // END if (xmlhttp.readyState==4)
				
			} catch( e ) {
				emsg = e;
				// jsdebug( 'do_httpcall callback Caught javascript Exception: ' + emsg );
			}
			
			if ( emsg != '' ) {
				
				// jsdebug( 'Did not receive JS output from <a href="' + page + '">here</a>.' );
				emsg = 'Did not receive JS output from ' + page;
				
			}
			
			if ( typeof(result) != 'undefined' || emsg != '' ) {
				
				// yes.. i call a callback from a callback - lol
				if ( callbackfunc_after != false ) {
					callbackfunc_after();
				}
				
			}
			
		} // END call back function 
		
		// do the actual call now
		
		var xmlhttp=get_xmlhttp();
		
		if ( postdata != null && postdata != false ) {
			xmlhttp.open( "POST", page, async );
			xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		} else {
			xmlhttp.open( "GET", page, async );
		}
		
		if ( async == true ) {
			xmlhttp.onreadystatechange=httpcall_done_callback;
		}
		
		if ( postdata != null && postdata != false ) {
			xmlhttp.send(postdata)
		} else {
			xmlhttp.send(null)
		}
		
		if ( async == false ) {
			httpcall_done_callback();
		}
		
	} catch( e ) {
		
		emsg = e;
		
		// jsdebug( 'do_plain_httpcall main Caught javascript Exception: ' + emsg );
		// jsdebug( 'This was while attempting to receive JS output from <a href="' + page + '">here</a>.' );
		
		if ( callbackfunc_after != false ) {
			callbackfunc_after();
		}
		
		return false;
	}
}

function do_page_refresh() {
	
	do_refresh_causing_reload=true;		// let the other code know whats going on here
	window.location=myurl;			// do full page refresh
}


