/***************************************************************************
 *                                 ajax.js
 *                            -------------------
 *   begin                : Wednesday, Aug 09, 2006
 *   copyright            : (C) 2006-2008 Michael Fritscher
 *   email                : mfritscher@gmx.de
 *
 *   $Id: ajax.js, v0.6 2008-02-01 02:19 catweazle $
 *
 ***************************************************************************/

var ajax_html_loading = '<img src="scripts/ajax/loading.gif" alt="" style="width:16px;height:16px;vertical-align:middle;" />Anfrage wird bearbeitet...';
var ajax_div_loading = null;
var ajax_impl_type = 0;
var ajax_onload = '';

try
{
	var xmlhttp = new XMLHttpRequest();
	ajax_impl_type = 1; //most browsers incl. IE7
}
catch(e)
{
	try
	{
		var xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
		ajax_impl_type = 2; //IE Browser ( <= 6)
	}
	catch(e)
	{
		try
		{
			var xmlhttp = new ActiveXObject('Msxml2.XMLHTTP');
			ajax_impl_type = 3; //old IE xml
		}
		catch(e)
		{
			try
			{
				var xmlhttp = window.createRequest();
				ajax_impl_type = 4; //Ice Browser
			}
			catch(e)
			{
				ajax_impl_type = 0; //ajax not possible
			}
		}
	}
}
delete xmlhttp;
if ( window.attachEvent )
{
	window.attachEvent('onload', ajax_init);
}
else
{
	window.addEventListener('load', ajax_init, false);
}

function ajax_init()
{
	ajax_div_loading = document.createElement('DIV');
	with( ajax_div_loading.style )
	{
		position = 'absolute';
		bottom = '0px';
		left = '0px';
		border = '1px solid black';
		background = '#ffffcc';
		color = '#000000';
		padding = '2px';
		display = 'none';
	}
	ajax_div_loading.innerHTML = ajax_html_loading;
	document.body.appendChild(ajax_div_loading);
	ajax_parseDocument();
	if ( ajax_onload > '' ) eval(ajax_onload);
}

function ajax_create()
{
	var xmlhttp = null;

	switch( ajax_impl_type )
	{
		case 1:
			xmlhttp = new XMLHttpRequest();
			break;
		case 2:
			xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
			break;
		case 3:
			xmlhttp = new ActiveXObject('Msxml2.XMLHTTP');
			break;
		case 4:
			xmlhttp = window.createRequest();
			break;
	}
	return xmlhttp;
}

function ajax_loadElement(ele, url)
{
	ele.innerHTML = ajax_html_loading;

	var xmlhttp = ajax_create();
	xmlhttp.onreadystatechange = function()
	{
		if ( (xmlhttp.readyState == 4) && (xmlhttp.status == 200) )
		{
			ele.innerHTML = xmlhttp.responseText;
		}
	}
	xmlhttp.open('GET', url, true);
	xmlhttp.send(null);
}

function ajax_parseDocument()
{
	var i, els;
	els = document.getElementsByTagName('DIV');
	for(i=0; i<els.length; i++)
	{
		if ( els[i].getAttribute('src') > '' ) ajax_loadElement(els[i], els[i].getAttribute('src'));
	}
}

function ajax_navigate(url, obj)
{
	if ( obj == null )
		obj = event.srcElement;
	else if ( typeof(obj) == 'string' )
		obj = document.getElementById(obj);

	ajax_loadElement(obj, url);
}

function ajax_submit(frm, cb)
{
	var req, ele, i;
	var xmlhttp = ajax_create();
	
	if ( null == frm || frm.tagName == undefined )
	{
		if ( frm == null ) frm = window.event;
		if ( frm.srcElement ) frm = frm.srcElement; else frm = e.target;		
	}
	
	req = '';
	for(i=0; i<frm.elements.length; i++)
	{
		ele = frm.elements[i]
		if ( ele.disabled ) continue;
		if ( ele.type == undefined ) continue;
		if ( (ele.offsetWidth == 0 || ele.offsetHeight == 0) && ele.type != 'hidden' ) continue;
		if ( (ele.type == 'checkbox' || ele.type == 'radio') && !ele.checked ) continue;
		req += '&' + ele.name + '=' + escape(ele.value);
	}
	req = req.substr(1);

	if ( null == cb )
	{
		if ( frm.target > '' )
			ele = document.getElementById(frm.target);
		else
			ele = frm.parentNode;
	
		document.body.style.cursor = 'progress';
		ele.innerHTML = ajax_html_loading;
	
		xmlhttp.onreadystatechange = function()
		{
			if ( (xmlhttp.readyState == 4) && (xmlhttp.status == 200) )
			{
				ele.innerHTML = xmlhttp.responseText;
				document.body.style.cursor = 'auto';
			}
		}
	}
	else
	{
		document.body.style.cursor = 'progress';
		ajax_div_loading.style.display = 'block';
		
		xmlhttp.onreadystatechange = function()
		{
			if ( (xmlhttp.readyState == 4) && (xmlhttp.status == 200) )
			{
				eval(cb + '(\'' + xmlhttp.responseText + '\');');
				ajax_div_loading.style.display = 'none';
				document.body.style.cursor = 'auto';
			}
		}		
	}
	if ( frm.method.toLowerCase() == 'post' )
	{
		xmlhttp.open('POST', frm.action, true);
		//xmlhttp.setRequestHeader('Content-Type', this.encoding > '' ? this.encoding : 'application/x-www-form-urlencoded');
		xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		xmlhttp.send(req);
	}
	else
	{
		xmlhttp.open('GET', frm.action + '?' + req, true);
		xmlhttp.send(null);
	}
	return false;
}

function ajax_upload(frm, cb)
{
	document.body.style.cursor = 'progress';
	ajax_div_loading.style.display = 'block';

	if ( null == cb ) cb = 'alert';
	
	var fra = document.createElement('iframe');
	fra.id = frm.id + '_target';
	fra.name = fra.id
	fra.style.display = 'none';
	frm.parentNode.appendChild(fra);
	if ( document.frames ) document.frames[fra.id].name = fra.id;

	var doload = function()
	{
		if ( window.attachEvent )
			fra.detachEvent('onload', doload);
		else
			fra.removeEventListener('load', doload, false);

		fra.src = 'javascript:window.parent.' + cb + '(document.body.innerHTML); void(0);';
		setTimeout(function() { frm.parentNode.removeChild(fra); }, 0);

		document.body.style.cursor = 'auto';
		ajax_div_loading.style.display = 'none';
	}

	if ( window.attachEvent )
		fra.attachEvent('onload', doload);
	else
		fra.addEventListener('load', doload, false);
	
	frm.target = fra.id;
	frm.submit();
	
	return false;
}

function ajax_reload(obj)
{
	if ( typeof(obj) == 'string' ) obj = document.getElementById(obj);
	ajax_loadElement(obj, obj.getAttribute('src'));	
}

function ajax_getText(cb, url)
{
	ajax_div_loading.style.display = 'block';

	var xmlhttp = ajax_create();
	xmlhttp.onreadystatechange = function()
	{
		if ( (xmlhttp.readyState == 4) && (xmlhttp.status == 200) )
		{
			eval( cb + '(\'' + xmlhttp.responseText + '\');' );
			ajax_div_loading.style.display = 'none';
		}
	}
	xmlhttp.open('GET', url, true);
	xmlhttp.send(null);
}

function ajax_getVar(v, url)
{
	document.body.style.cursor = 'wait';
	ajax_div_loading.style.display = 'block';
	
	var xmlhttp = ajax_create();
	xmlhttp.open('GET', url, false);
	xmlhttp.send(null);
	if ( xmlhttp.status == 200 ) eval(v + '=' + xmlhttp.responseText);
	ajax_div_loading.style.display = 'none';
	document.body.style.cursor = 'auto';
}
