/*************************************************************
 * 
 * Snippets.js : File to get and manage snippets
 *
 * @author: joduba
 * @since: 27/03/2007
 *
 ************************************************************/

var rObj=new Array();

function snippetObj(conn, ph, url) {
	this.conn=conn;
	this.ph=ph;
	this.url=url;
	this.loading=false;
};

function writeSnippetResult(phid) {

	if (rObj[phid].conn.readyState == 4) {
		if (rObj[phid].conn.status==200) {
			rObj[phid].ph.innerHTML=rObj[phid].conn.responseText;
			rObj[phid].loading=false;
		}
	}
}

function loadPage(placeholder, url, repeat) {

	if (rObj[placeholder].conn==null) {
		if (myHTTPRequest(placeholder)==null)
			return;
	}
	if (!rObj[placeholder].loading)
		requestStart(placeholder,  "GET", true, function () { writeSnippetResult(placeholder); });

	if (repeat>0)
		setTimeout('loadPage("'+placeholder+'","'+url+'","'+repeat+'")', repeat*1000);
}

function snippet(placeholder, service, op, params, repeat) {

	// Check if there is a place for the result 
	var ph=document.getElementById(placeholder);
	if (!ph) { return; }	// empty placeholder

	// Build the URL 
	var url='/i-js/tag.php?iaw='+service+'&op='+op+'&'+params;
	rObj[placeholder]=new snippetObj(null, ph, url);

	loadPage(placeholder, url, repeat);
}


/*------------------------------- 
	AJAX HANDLERS 
--------------------------------*/

function myHTTPRequest(phid) {

	// Mozilla, Safari etc
	if (window.XMLHttpRequest) 
		rObj[phid].conn = new XMLHttpRequest()
	else if (window.ActiveXObject){ // if IE
		rObj[phid].conn = new ActiveXObject("Msxml2.XMLHTTP")
		if (!rObj[phid].conn) {
			rObj[phid].conn = new ActiveXObject("Microsoft.XMLHTTP")
		}
	}
	return rObj[phid].conn;
}

function requestStart(phid, reqType, async, rHandle, postargs) {
	try {
		rObj[phid].loading=true;
		rObj[phid].conn.onreadystatechange=rHandle;
		rObj[phid].conn.open(reqType, rObj[phid].url, async);
		if (reqType.toLowerCase() == "post") {
			rObj[phid].conn.setRequestHeader("Content-Type", 
				"application/x-www-form-urlencoded; charset=UTF-8");
			rObj[phid].conn.send(postargs);
		} else {
			rObj[phid].conn.send(null);
		}
	} catch (errv) {
		return;
	}
}

