

/*
//This file requires NETJOHNHENRY because it uses the delegate function therefore this code is commented
try
{ var test = (NETJOHNHENRY.name == 'NETJOHNHENRY'); }
catch (e)
{ NETJOHNHENRY = {}; }
*/

NETJOHNHENRY.Ajax = new function(){
	// namespace
	this.ns = 'NETJOHNHENRY';
	this.nsRef = NETJOHNHENRY;
	this.name = 'Ajax';
	this.description = 'This object deals with Ajax calls. Main function NETJOHNHENRY.Ajax.load.';
	
	this.load  = function () {	
		if(arguments.length <=0 ) {alert('AJAX call failed: missing arguments.');return;};  
		if(arguments[0].url == undefined) {alert('AJAX call failed: missing url.');return;};  
		if(arguments[0].onloadfunction == undefined) {alert('AJAX call failed: missing onload function.');return;};  
		
		window.status = 'Loading page...';
		var o = arguments[0];
		
		o.method =  (o.method == undefined)? 'GET': o.method;
		o.send__parameters = (o.send__parameters == undefined)? '': o.send__parameters ;	
		o.is_asynchronous  =  (o.is_asynchronous == undefined) ? true : o.is_asynchronous;
		
		var send = function( xmlhttp , parent){		
			xmlhttp.onreadystatechange = function() { call__onloadfunction(o.onloadfunction, xmlhttp); };			
			xmlhttp.open(o.method, o.url, true);			
			xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			xmlhttp.setRequestHeader("Content-length", o.send__parameters.length);
			xmlhttp.setRequestHeader("Connection", "close");
			xmlhttp.send(o.send__parameters);			
		}	
		//native XMLHttpRequest 
		if (window.XMLHttpRequest) { send(new XMLHttpRequest(), this); } 
		//IE/Windows ActiveX 
		else if (window.ActiveXObject) 
		{		
			var xmlhttp=false;
		
			try 
				{ xmlhttp= new ActiveXObject("Msxml2.XMLHTTP"); }
			catch (e) 
			{ 
				try { xmlhttp= new ActiveXObject("Microsoft.XMLHTTP"); } catch (E) { xmlhttp = false; } 
			}		
			
			if (xmlhttp)
			{				
				send(xmlhttp, this);
			}
			else
			{
				alert("Sorry but your browser doesn't support AJAX. Please use a different browser (ie. Firefox at http://www.mozilla.com) or try to get the latest release of your current browser.");
			}
		}  
	}
	this.load.parameters = {url:'',method:'GET', is_asyncronous:true, send__parameters:'',onloadfunction: ''}
	this.load.description = 'Usage: <code>NETJOHNHENRY.Ajax.load({url:"partial.html", onloadfunction: functionname, [method: "GET", is_asyncronous: true, send__parameters:"first=1&second=2&name=name"]});</code>\nRemarks: <code>onloadfunction</code> is the function that will be called when the Ajax call has received the remote file, this function will be passed as a parameter the XMLHttpRequest object. Use XMLHttpRequest_Object.responseText() or XMLHttpRequest_Object.responseXML() to get the content. <code>send__parameters</code> are optional parameters that can be passed to the page to be load. This allows to achieve a form post/get of parameters';		

	
	var call__onloadfunction = function (onloadfunction, xmlobj)
	{		
		if(xmlobj.readyState != 4) { return(xmlobj.readyState)};		
		if(xmlobj.status == 200) {	
			window.status = '';
			onloadfunction ( xmlobj );
		} else 
		{			
			alert('There was a problem retrieving the XML data:\n' + xmlobj.status);
		}		
	}		
	this.start_navigating_by_ajax = function(){			
		
		if(arguments.length <=0 ) {alert('start_navigating_by_ajax failed: missing arguments.');return;};  
		if(arguments[0].onloadfunction == undefined) {alert('start_navigating_by_ajax failed failed: missing onload function.');return;};  		
		
		var o = arguments[0];
		
		o.method =  (o.method == undefined)? 'GET': o.method;
		o.send__parameters = (o.send__parameters == undefined)? this.start_navigating_by_ajax.parameters.send__parameters : o.send__parameters ;	
		o.is_asynchronous  =  (o.is_asynchronous == undefined) ? this.start_navigating_by_ajax.parameters.is_asyncronous : o.is_asynchronous;
		o.folder = (o.folder == undefined) ? this.start_navigating_by_ajax.parameters.folder : o.folder;
		o.file_extension = (o.file_extension == undefined) ? this.start_navigating_by_ajax.parameters.file_extension : o.file_extension;
		o.onchangefunction = (o.onchangefunction == undefined)? {}:o.onchangefunction;
		if(o.urlconstructfunction == undefined) {
			o.urlconstructfunction = function( pagename ){return o.folder + pagename + o.file_extension; }
		};
		var parent = NETJOHNHENRY.Ajax;
		
		var parse_location = function(){
					
			try{				
				if(parent.current_location == undefined || location.href.toString() != parent.current_location){
					var pagename = (location.href.indexOf('#')<0) ? undefined : location.href.split('#')[1];
						
					if(pagename == undefined && (o.default_page !='' && o.default_page != undefined)) 
					{
						pagename = o.default_page;
					}
					if(pagename == undefined){return;}
	
					o.url = o.urlconstructfunction( pagename );				
					o.onchangefunction();
					parent.load( o );					
					parent.current_location = location.href;	
					
					try{console.log(parent.current_location);}catch(e){}
				}
			}
			catch(e){		
				try{console.log(e);}catch(e){}
				window.clearInterval(interval);
			}
		}
		var interval = setInterval(parse_location, this.start_navigating_by_ajax.parameters.interval_time);
	}
	//Override this object values only if default values are different. Please pass nothing or another object instead.
	this.start_navigating_by_ajax.parameters = {folder: 'partials/', file_extension: '.html', url:'', default_page:'', method:'GET', interval_time: 1000, is_asyncronous:true, send__parameters:'',onloadfunction: ''}	
	this.start_navigating_by_ajax.description = 'This function once called the first time will start checking if the url is changed at 1 second interval. If it\'s changed it will try to load the page asyncronously, and call the <code>onloadfunction</code> specified in the parameters.'

}