/* Simple AJAX Code-Kit (SACK) */
/* �2005 Gregory Wild-Smith */
/* www.twilightuniverse.com */
/* Software licenced under a modified X11 licence, see documentation or authors website for more details */



///////////////
// NetArc: Include Tracking - Keep Track Of What Files We Include Across Our Page Load, Don't Wan't To Double Nest
///////////////
var included_files = new Array();
var script_ids = new Array();


function remove_duplicate(id)
{
	var html_doc = document.getElementsByTagName('head').item(0);
	
	for (j=0;j<html_doc.childNodes.length;j++)
	{
		var obj = html_doc.childNodes[j];
		if (obj.nodeName.toUpperCase() == "SCRIPT")
		{
			var tmp_id = "<!--" + id + "-->";
			if (obj.text.substr(0, tmp_id.length) == tmp_id)
			{
				html_doc.removeChild(obj);
				j--;
			}
		}
	}
}




function _appendChild(node, text)
{ 
  if (node.canHaveChildren == null  ||   node.canHaveChildren)
	{ 
    node.appendChild(document.createTextNode(text)); 
  }
	else
	{ 
    node.text = text; 
  }
} 




function include_dom(script_file)
{
    var html_doc = document.getElementsByTagName('head').item(0);
    var js = document.createElement('script');
    js.setAttribute('language', 'javascript');
    js.setAttribute('type', 'text/javascript');
		js.setAttribute('src', script_file);
    html_doc.appendChild(js);
    return false;
}


function include_script(script, id)
{
    var html_doc = document.getElementsByTagName('head').item(0);
    var js = document.createElement('script');
		js.setAttribute('language', 'javascript');
    js.setAttribute('type', 'text/javascript');
		
		// Add Our Text Node, Remember IE And Other Browsers Handle This Differently
		_appendChild(js, "<!--" + id + "-->" + script);
		
		html_doc.appendChild(js);
				
    return false;
}


function in_array(needle, haystack)
{
    for (var i = 0; i < haystack.length; i++)
		{
        if (haystack[i] == needle)
				{
            return true;
        }
    }
    return false;	
}
function file_include_once(script_filename)
{
    if (!in_array(script_filename, included_files))
		{
        included_files[included_files.length] = script_filename;
        include_dom(script_filename);
    }
}
function script_include_once(script, id)
{
		// Quick Check To See If We Are Already Included
    if (!in_array(id, script_ids))
		{
        script_ids[script_ids.length] = id;
        include_script(script, id);
    }
		// Otherwise, Remove And ReInclude
		else
		{
			remove_duplicate(id);
			include_script(script, id);
		}
}
///////////////
// NetArc: End Include Tracking
///////////////




function sack(file)
{
	this.AjaxFailedAlert = "Your browser does not support the enhanced functionality of this website, and therefore you will have an experience that differs from the intended one.\n";
	this.requestFile = file;
	this.method = "POST";
	this.URLString = "";
	this.encodeURIString = true;
	this.execute = false;

	this.onLoading = function() { };
	this.onLoaded = function() { };
	this.onInteractive = function() { };
	this.onCompletion = function() { };

	this.createAJAX = function()
	{
		try
		{
			this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (err)
			{
				this.xmlhttp = null;
			}
		}
		if(!this.xmlhttp && typeof XMLHttpRequest != "undefined")
			this.xmlhttp = new XMLHttpRequest();
		if (!this.xmlhttp)
		{
			this.failed = true; 
		}
	};
	
	this.setVar = function(name, value)
	{
		if (this.URLString.length < 3)
		{
			this.URLString = name + "=" + value;
		}
		else
		{
			this.URLString += "&" + name + "=" + value;
		}
	}
	
	this.encVar = function(name, value){
		var varString = encodeURIComponent(name) + "=" + encodeURIComponent(value);
	return varString;
	}
	
	this.encodeURLString = function(string){
		varArray = string.split('&');
		for (i = 0; i < varArray.length; i++){
			urlVars = varArray[i].split('=');
			if (urlVars[0].indexOf('amp;') != -1){
				urlVars[0] = urlVars[0].substring(4);
			}
			varArray[i] = this.encVar(urlVars[0],urlVars[1]);
		}
	return varArray.join('&');
	}
	
	this.runResponse = function(){
		eval(this.response);
	}
	
	
	this.runAJAX = function(urlstring)
	{
		this.responseStatus = new Array(2);
							
		if (this.failed && this.AjaxFailedAlert)
		{ 
			alert(this.AjaxFailedAlert); 
		} 
		else 
		{
			if (this.element)
			{
				this.elementObj = document.getElementById(this.element);
			}
			else
			{
				this.elementObj = null;
			}
									
			if (urlstring)
			{ 
				if (this.URLString.length)
				{
					this.URLString = this.URLString + "&" + urlstring; 
				} 
				else 
				{
					this.URLString = urlstring; 
				}
			}
			if (this.encodeURIString)
			{
				var timeval = new Date().getTime(); 
				this.URLString = this.encodeURLString(this.URLString);
				//this.setVar("rndval", timeval);
			}


			if (this.xmlhttp) 
			{
				var self = this;
				
				if (this.method == "GET") 
				{				
					var totalurlstring = this.requestFile + "?" + this.URLString;
					this.xmlhttp.open(this.method, totalurlstring, true);
				} 
				else 
				{
					this.xmlhttp.open(this.method, this.requestFile, true);
				}
				if (this.method == "POST")
				{
  				try 
					{
						this.xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded')  
					} 
					catch (e) 
					{}
				}

								
				this.xmlhttp.onreadystatechange = function()
				{
					switch (self.xmlhttp.readyState)
					{
						case 1:
							self.onLoading();
						break;
						case 2:
							self.onLoaded();
						break;
						case 3:
							self.onInteractive();
						break;
						case 4:
							self.response = self.xmlhttp.responseText;
							self.responseXML = self.xmlhttp.responseXML;
							self.responseStatus[0] = self.xmlhttp.status;
							self.responseStatus[1] = self.xmlhttp.statusText;
							self.onCompletion();
							if(self.execute){ self.runResponse(); }
							
							if (self.elementObj)
							{
								var elemNodeName = self.elementObj.nodeName;
								elemNodeName.toLowerCase();
								if (elemNodeName == "input" || elemNodeName == "select" || elemNodeName == "option" || elemNodeName == "textarea")
								{
									self.elementObj.value = self.response;
								} 
								else 
								{
									///////////////
									// NetArc: Modified this loader to execute javascript tags, so we can nest javascript
									///////////////
									self.elementObj.innerHTML = "";
									var specTag = '<script type="text/javascript">';
									var specTagEnd = '</script>';
									var TagEnd = 0;
		
									// Top Layer Search
									for (i = 0; i < self.response.length; i++)
									{
										if (self.response.substring(i, i + specTag.length) == specTag)
										{
											// Bot Layer Search
											for (j = i + specTag.length; j < self.response.length; j++)
											{
												if (self.response.substring(j, j + specTagEnd.length) == specTagEnd)
												{
													//self.elementObj.innerHTML = self.elementObj.innerHTML + self.response.substring(TagEnd, i);
													TagEnd = j + specTagEnd.length;
													var Block = self.response.substring(i + specTag.length, j);
													// Include Our Script With Its Somewhat Unique ID
													script_include_once(Block, self.requestFile + "_" + i + "_" + j);
													i = TagEnd;
													break;
												}
											}
										}
									}
									self.elementObj.innerHTML = self.response;//self.elementObj.innerHTML + self.response.substring(TagEnd, self.response.length);
									// NetArc - End Nested Javascript Loader
								}
							}
							/*
							else
							{
								if (self.element)
									alert("Element '" + self.element + "' Not Found!");
								else
									alert("Element Not Found!");
							}
							*/
							self.URLString = "";
						break;
					}
				};
				this.xmlhttp.send(this.URLString);
			
			}
		}

	};

	this.createAJAX();
}