/**
*
*  AJAX IFRAME METHOD (AIM)
*  http://www.webtoolkit.info/
*
**/

AIM = {

	frame : function(c) {

		var n = 'f' + Math.floor(Math.random() * 99999);
		var d = document.createElement('DIV');


		d.style.border="1 solid black";
		d.id=n+"_div";

		
		d.innerHTML = '<iframe style="display:none" src="about:blank" id="'+n+'" name="'+n+'" onload="AIM.loaded(\''+n+'\',\''+c.target+'\')"></iframe>';
		document.body.appendChild(d);

		var i = document.getElementById(n);
		if (c && typeof(c.onComplete) == 'function') {
			i.onComplete = c.onComplete;
		}

		return n;
	},

	form : function(f, name) {
		f.setAttribute('target', name);
	},

	submit : function(f, c) {
		document.body.style.cursor='wait';
		AIM.form(f, AIM.frame(c));
		var e = document.createElement('input');
		e.type='hidden';
		e.name='ajax';
		e.value=c.target;
		f.appendChild(e);
		var e = document.createElement('input');
		e.type='hidden';
		e.name='aim';
		e.value=1;
		f.appendChild(e);
		if (c && typeof(c.onStart) == 'function') {
			return c.onStart();
		} else {
			return true;
		}
	},

	loaded : function(id, target) {
		var i = document.getElementById(id);
		if (i.contentDocument) {
			var d = i.contentDocument;
		} else if (i.contentWindow) {
			var d = i.contentWindow.document;
		} else {
			var d = window.frames[id].document;
		}
		if (d.location.href == "about:blank") {
			return;
		}

		var t=document.getElementById(target);
		if (! t) {
			d=document.getElementById(id+"_div");
			if (d) document.body.removeChild(d);
			return;
		}
 

		var s=new String;
		if (d.all) s=d.body.innerText;
		else s=d.body.textContent;

// alert(window.frames[id].document.body.innerText);
		if ((s.length > 0) &&
			(s.indexOf('<NOSCRIPT>') == 1) &&
			(s.lastIndexOf('</NOSCRIPT>') == s.length-11)) {
			// Removing "noindex" tags
			s=s.slice(11,s.length-11);
		} else s=d.body.innerHTML;

		t.innerHTML = s;

    		var scripts=t.getElementsByTagName('script');
  		for (i = 0; i < scripts.length; i++) {
			if (window.execScript) window.execScript(scripts.item(i).innerHTML, 'javascript');
			else window.eval(scripts.item(i).innerHTML);
		};

		if (typeof(i.onComplete) == 'function') {
			i.onComplete(d.body.innerHTML, target);
		}

		d=document.getElementById(id+"_div");
		if (d) document.body.removeChild(d);
		
		document.body.style.cursor='default';
	}

}
