// --------------------------------------------------------- //
// daryo pigment 3.7
// --------------------------------------------------------- //
// Copyright (C) 2004,2010 Sebastien Fauconnet
// http://www.daryo.com/pigment/
// --------------------------------------------------------- //

// --------------------------------------------------------- //
function xmlhttpPost(strURL,id,fadestyle) {
// --------------------------------------------------------- //

	var xmlHttpReq = false;
	var self = this;

	// Mozilla/Safari
	if (window.XMLHttpRequest) {
		self.xmlHttpReq = new XMLHttpRequest();
		}

	// IE
	else if (window.ActiveXObject) {
		self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
		}

	self.xmlHttpReq.open('POST', strURL, true);

	if ((fadestyle != "update")&&(fadestyle != "failed_update")) {
	document.forms[id].style.opacity="0.5";
	document.forms[id].style.filter="alpha(opacity=50)";
}
	self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	self.xmlHttpReq.onreadystatechange = function() {
		if(self.xmlHttpReq.readyState == 4 && self.xmlHttpReq.status == 200) {
			updatepage(self.xmlHttpReq.responseText,id);
		}
	}
	self.xmlHttpReq.send(getquerystring(id));
}


// --------------------------------------------------------- //
function xmlhttpGet(strURL,id) {
// --------------------------------------------------------- //

	var xmlHttpReq = false;
	var self = this;

	// Mozilla/Safari
	if (window.XMLHttpRequest) {
		self.xmlHttpReq = new XMLHttpRequest();
		}

	// IE
	else if (window.ActiveXObject) {
		self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
		}
	self.xmlHttpReq.open('GET', strURL, true);
	self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	self.xmlHttpReq.onreadystatechange = function() {
		if(self.xmlHttpReq.readyState == 4 && self.xmlHttpReq.status == 200) {
		if (id != '0') updatepage(self.xmlHttpReq.responseText,id);
		}
	}

	self.xmlHttpReq.send(id);
}


// --------------------------------------------------------- //
function getquerystring(id) {
// --------------------------------------------------------- //

	var qstr = '';
	var elem = document.forms[id].elements;

	for(var i = 0; i < elem.length; i++) {
		qstr += elem[i].name+'='+ encodeURIComponent(elem[i].value) +'&';
	}

	return qstr;
}


// --------------------------------------------------------- //
function updatepage(str,id){
// --------------------------------------------------------- //

var newid = id+'_targetResult';

if (str.substring(0,11) == 'JavaScript:') document.getElementById(newid).innerHTML = eval(str.substring(11,(str.length)));
else document.getElementById(newid).innerHTML = str;
}


// --------------------------------------------------------- //
function xhr_expand(id,pingpage) {
// --------------------------------------------------------- //

	var newID = id + '_element_targetResult';
	var intElemSH = document.getElementById(newID).scrollHeight;
	var intElemBasic = document.getElementById(newID).clientHeight;
	if (intElemBasic < intElemSH) document.getElementById(newID).style.height = intElemSH+'px';

	var updatevisits = pingpage + '?element_id=' + id;
	xmlhttpGet(updatevisits,'0');
}


// --------------------------------------------------------- //
function xhr_loadANDexpand(id,request) {
// --------------------------------------------------------- //

	var newID = id + '_feedback';
	xmlhttpGet(request,newID);
	var newID = newID+'_targetResult';
	document.getElementById(newID).style.display = 'block';
}	




// --------------------------------------------------------- //
//	AIM Method for file upload
// --------------------------------------------------------- //

var callbackDestinationID="";

AIM = {
 
	frame : function(c) {
 
		var n = 'f' + Math.floor(Math.random() * 99999);
		var d = document.createElement('DIV');
		d.innerHTML = '<iframe style="display:none" src="about:blank" id="'+n+'" name="'+n+'" onload="AIM.loaded(\''+n+'\')"></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,cid, c) {
		callbackDestinationID=cid;
		AIM.form(f, AIM.frame(c));
		if (c && typeof(c.onStart) == 'function') {
			return c.onStart();
		} else {
			return true;
		}
	},
 
	loaded : function(id) {
		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;
		}
 
		if (typeof(i.onComplete) == 'function') {
			i.onComplete(d.body.innerHTML);
		}
	}
 
}

// --------------------------------------------------------- //
function startCallback() {
// --------------------------------------------------------- //
	return true;
}

// --------------------------------------------------------- //
function completeCallback(response) {
// --------------------------------------------------------- //
	document.getElementById(callbackDestinationID).innerHTML = response;

}
