//
// My little AJAX (Patch/Frame) Work
// 
function getElement(id){
	if(document.getElementById){
		getElement = function(id){ return document.getElementById(id); }
	}else if(document.all){
		getElement = function(id){ return document.all[id]; };
	}else if(document.layers){
		getElement = function(id){ return document.layers[id]; };
	}else{
		getElement = function() { return null; }
	}

	// When we get here, the getElement function has been replaced.
	// So we return the result of the new function.
	return getElement(id);
}


function getdata(what,where, id) { // get data from source (what)
	
	AARead(what, null, where);

}


function getdata2(what,where, id) { // get data from source (what)

if (id == null) id = "xyz";

 try {
   id = window.XMLHttpRequest?new XMLHttpRequest():
  		new ActiveXObject("Microsoft.XMLHTTP");
 }
 catch (e) { /* do nothing */ }

 id.onreadystatechange = function()  // when request finished, call the function to put result to destination DIV
 {
 	  triggered(where, id);
 }
 id.open("GET", what);
 id.send(null);
  return false;
}

function triggered(id, req) { // put data returned by requested URL to selected DIV
  if (req.readyState == 4) if (req.status == 200)
    document.getElementById(id).innerHTML =req.responseText;
}


var http_request = false;
// makePOSTRequest('post.php', poststr);

function makePOSTRequest(url, parameters) {
   http_request = false;
   if (window.XMLHttpRequest) { // Mozilla, Safari,...
      http_request = new XMLHttpRequest();
      if (http_request.overrideMimeType) {
      	// set type accordingly to anticipated content type
         //http_request.overrideMimeType('text/xml');
         http_request.overrideMimeType('text/html');
      }
   } else if (window.ActiveXObject) { // IE
      try {
         http_request = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) {
         try {
            http_request = new ActiveXObject("Microsoft.XMLHTTP");
         } catch (e) {}
      }
   }
   if (!http_request) {
      alert('Cannot create XMLHTTP instance');
      return false;
   }
   
   http_request.onreadystatechange = alertContents;
   http_request.open('POST', url, true);
   http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   http_request.setRequestHeader("Content-length", parameters.length);
   http_request.setRequestHeader("Connection", "close");
   http_request.send(parameters);
}

function alertContents() {
   if (http_request.readyState == 4) {
      if (http_request.status == 200) {
         alert(http_request.responseText);
         result = http_request.responseText;
         document.getElementById('myspan').innerHTML = result;            
      } else {
         alert('There was a problem with the request.');
      }
   }
}

//ued_encode() will take an array as its argument and return the data encoded in UED format - as a string.
//http://www.openjs.com/scripts/data/ued_url_encoded_data/
function ued_encode(arr,current_index) {
	var query = ""
	if(typeof current_index=='undefined') current_index = '';

	if(typeof(arr) == 'object') {
		var params = new Array();
		for(key in arr) {
			var data = arr[key];
			var key_value = key;
			if(current_index) {
				key_value = current_index+"["+key+"]"
			}

			if(typeof(data) == 'object') {
				if(data.length) { //List
					for(var i=0;i<data.length; i++) {
						params.push(key_value+"[]="+ued_encode(data[i],key_value)); //:RECURSION:
					}
				} else { //Associative array
					params.push(ued_encode(data,key_value)); //:RECURSION:
				}
			} else { //String or Number
				params.push(key_value+"="+encodeURIComponent(data));
			}
		}
		query = params.join("&");
	} else {
		query = encodeURIComponent(arr);
	}

	return query;
}







/**
	AnAA - Ajax Framework
	Version 1.8
	www.anaa.eu
	(c) 2007-2009 Sandrine Takis and Denis Sureau.
	Free under the GNU GPL 2.0 Licence.
	
	Msxml2.XMLHTTP.4.0
	Msxml2.XMLHTTP
*/	

var AACaching = false;


function AACreate() 
{
    var request = false;
        try {
            request = new ActiveXObject('Msxml2.XMLHTTP');
        }
        catch (err2) {
            try {
                request = new ActiveXObject('Microsoft.XMLHTTP');
            }
            catch (err3) {
		try {
			request = new XMLHttpRequest();
		}
		catch (err1) 
		{
			request = false;
		}
		}
	}
    return request;
}


/**
	AA Read
	Load a file, text or XML
*/	

function AARead(url, fun, element)
{ 
	var xhr = AACreate();
	var ext = url.substr(url.length - 3);
	var isXML =  (ext == "xml");	

	xhr.onreadystatechange=function()
	{ 
		if(xhr.readyState == 4)
		{
			if(xhr.status == 200)
			{
				var content;
				if(isXML) 
					content = xhr.responseXML;
				else
					content = xhr.responseText;
					
					getElement(element).innerHTML=content;
			}
		} 
	}; 

	if(AACaching == false)
		url = url + "?nocache=" + Math.random();
	xhr.open("GET", url , true);
	xhr.send(null); 
} 

/**
	Read an XML file with any extension
*/

function AALoadXML(url, fun, element)
{ 
	var xhr = AACreate();
	xhr.onreadystatechange=function()
	{ 
		if(xhr.readyState == 4)
		{
			fun(xhr.responseXML, element);
		} 
	}; 
	xhr.open("GET", url , true);
	xhr.send(null); 
} 

/**
	AAWrite
	url:	the script
	data:	the string to pass to the script
		it is a list of pairs x=y separated by &
*/	

function AAWrite(url, data, fun)
{ 
	var xhr = AACreate();

	xhr.onreadystatechange=function()
	{ 
		if(xhr.readyState == 4)
		{
			if(fun != null) fun(xhr.responseText);
		}
	}; 
	xhr.open("POST", url, true);		
	xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xhr.send(data); 
} 
	

function AAGetBody(content)
{
	var x = content.indexOf("<body");
	if(x == -1) return "";
		
	x = content.indexOf(">", x);
	if(x == -1) return "";

	var y = content.lastIndexOf("</body>");
	if(y == -1) return "";
	
	return content.slice(x + 1, y);
}

function AAPutHTML(content, target)
{
	target.innerHTML = AAGetBody(content);
}

/**
	Loads a HTML page
	Put the content of the body tag into the current page.
	Arguments:
		url of the other HTML page to load
		id of the tag that has to hold the content
*/		

function AALoadHTML(url, fun, storage, param)
{
	var xhr = AACreate();
	xhr.onreadystatechange=function()
	{ 
		if(xhr.readyState == 4)
		{
			if(xhr.status == 200)
			{
				storage = document.getElementById(storage);
				storage.innerHTML = AAGetBody(xhr.responseText);
				fun(storage, param);
			}
		} 
	}; 

	if(AACaching == false)
		url = url + "?nocache=" + Math.random();
	xhr.open("GET", url , true);
	xhr.send(null); 

} 


/**
	Send a HEAD request,
	call a function with the value 
*/

function AAHead(url, key, fun, element)
{ 
	var xhr = AACreate();

	xhr.onreadystatechange = function() 
	{
		if(xhr.readyState == 4) 
		{
			var value;
			if (xhr.status == 200)
			{ 
				 value = xhr.getResponseHeader(key);
			}
			else 
			{
				if(xhr.status==404) 
					value = url + " doesn't exist!";
				else 
					value = "Error, status is " + xhr.status;
			}
			fun(value, element);		
		}
	}
	xhr.open("HEAD", url, true);
	xhr.send(null); 
} 

/**
	Stacks onload calls
*/

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}