var _msxml_progid = new Array('MSXML2.XMLHTTP.5.0','MSXML2.XMLHTTP.4.0','MSXML2.XMLHTTP.3.0','MSXML2.XMLHTTP','Microsoft.XMLHTTP');
var idFrame		= 0;
var iframeObjs	= [];
var DoCommunicateLocation = 0;
var DoCommunicateCommand = '';
function getXMLHTTPConnection()
{	
	var httpObj = null;
	var modeIframe = false;
	
	if (!modeIframe)
	{
		try
		{
			DoCommunicateLocation = 1001;
			httpObj = new XMLHttpRequest();
			DoCommunicateLocation = 1002;
		}
		catch(e)
		{
			DoCommunicateLocation = 1003;
			for(var i=0; i<_msxml_progid.length; ++i)
			{
				try
				{						
					DoCommunicateLocation = 1100 + i;
					httpObj = new ActiveXObject(_msxml_progid[i]);					
					DoCommunicateLocation = 1200 + i;
					if(httpObj){
						break;
					}	
				}
				catch(e)
				{	
				}
			}
			
			if (!httpObj)
			{
				DoCommunicateLocation = 1004;
				alert('Sorry, communication server error!\r\nTry later please.');				
				try
				{
					DoCommunicateLocation = 1005;
					httpObj = new iFrameXMLHttpRequest();
					DoCommunicateLocation = 1006;
				}
				catch (e) 
				{
					alert('Sorry, communication server error!\r\nTry later please.');
				}				
			}
		}		
	}
	else
	{
		try
		{			
			httpObj = new iFrameXMLHttpRequest();
		}
		catch (e) 
		{
			alert('Sorry, server communication error!\r\nTry later please.');
		}		
	}	
	
	return httpObj;
	
}
function iFrameXMLHttpRequest()
{	
	this.url				= '';	
	this.iframe				= null;
	this.setRequestHeader	= function(bstrHeader, bstrValue){this.headerRequest[bstrHeader] = bstrValue;};
	this.headerRequest		= [];
	this.responseText		= '';
	this.readyState			= '';
	this.status				= '';
	this.onreadystatechange	= function(){return false;};	
	this.sendForm			= null;
	this.method				= 'POST';
	this.isOpen				= false;
	this.iFrameMode			= true;
	
	this.open = function (bstrMethod, bstrUrl, varAsync, bstrUser, bstrPassword)
				{	
					if (document.body)
					{
						idFrame++;
						this.method					= bstrMethod;
						this.id						= 'iFC_' + bstrMethod + '_' + idFrame;						
						this.url					= bstrUrl;
						
						var displayIFrame = 'display:none;';
						//var displayIFrame = 'width:100px;height:100px;';
						
						try
						{	
							//IE
							this.iframe	= document.createElement("<iframe onload='_isLoaded(\"" + this.id + "\");' src='about:blank' name='"+this.id+"' id='"+this.id+"' style='" + displayIFrame + "'></iframe>");
							document.body.appendChild(this.iframe);
						}
						catch(e)
						{							
							try
							{
								//FireFox
								this.iframe	= document.createElement('iframe');
								this.iframe.setAttribute('name',this.id);
								this.iframe.setAttribute('id',this.id);
								this.iframe.setAttribute('style',displayIFrame);
								this.iframe.setAttribute('src','about:blank');
								this.iframe.setAttribute('onload','_isLoaded(\"" + this.id + "\");');
								document.body.appendChild(this.iframe);
							}
							catch(e)
							{
								//IE 5.0 PC and others
								try
								{
									iframeHTML="<iframe onload='_isLoaded(\"" + this.id + "\");' src='about:blank' name='"+this.id+"' id='"+this.id+"' style='" + displayIFrame + "'></iframe>";
									document.body.innerHTML+=iframeHTML;
									this.iframe	= document.frames[this.id];
								}
								catch(e)
								{
									alert('Sorry, server communication error!\r\nTry later please.');
								}
							}
						}
												
						if (this.iframe!=null)
						{							
							this.isOpen			= true;
							iframeObjs[this.id]	= this;
						}
						else
						{
							this.url			= '';
							iframeObjs[this.id] = null;
							this.id				= '';
							idFrame--;							
						}
					}
				}
	
	this.send = function(dataToSend)
				{		
					
					if (!this.isOpen) return false;
					if (!this.iframe) return false;
					
					var theDataToSend	= '';
					var theValueToSend	= '';
					var theEqual		= -1;		
					
					if (this.headerRequest["Content-Type"]!=null)
					{
						
						var splitArr;
						var theEnctype = '';
						var theCharset = '';
						
						try
						{
							splitArr = String(this.headerRequest["Content-Type"]).split(';')
							theEnctype = splitArr[0];
							theCharset = splitArr[1];
						}
						catch(e)
						{
							theEnctype = '';
							theCharset = '';
						}
					}
					
					
					if (dataToSend!='')
					{					
						theEqual = dataToSend.indexOf('=');						
						var theSendForm = "";
						theSendForm = '<form name="' + this.id + '_frm" ';
						theSendForm += ' action="' + this.url + '" ';
						theSendForm += ' method="' + this.method + '" ';						
						if (theEnctype!='')
							theSendForm += ' enctype="' + theEnctype + '" ';
						if (theCharset!='')
							theSendForm += ' accept-charset="' + theCharset + '" ';							
						theSendForm += ' target="' + this.id + '">\r\n';						
					}
												
					if (theEqual!=-1)
					{						
						theValueToSend		= dataToSend.substring(0,theEqual);
						theDataToSend		= dataToSend.substr(theEqual+1);
						
						theSendForm += '<input type="hidden" ';
						theSendForm += ' name="' + theValueToSend + '"';
						theSendForm += ' value="' + theDataToSend + '"/>\r\n';
						theSendForm += '</form>\r\n'
						
						try
						{
							this.iframe.document.open();
							this.iframe.document.write(theSendForm);
							this.iframe.document.close();
							
							if (!this.iframe.document) return false;
							if (!this.iframe.document.forms[0]) return false;
						
							this.sendForm = this.iframe.document.forms[0];
							
						}
						catch(e)
						{
							this.sendForm = document.createElement('form');
							this.sendForm.setAttribute('name',this.id + '_frm');							
							this.sendForm.setAttribute('id',this.id + '_frm');
							this.sendForm.setAttribute('action',this.url);
							this.sendForm.setAttribute('method',this.method);
							this.sendForm.setAttribute('target',this.id);
							document.body.appendChild(this.sendForm);
						}
						
						this.sendForm.submit();
						
					}		
					else
					{						
						this.iframe.src=this.url;						
					}
				}	
}

function _isLoaded(idObj)
{	
	var el	= iframeObjs[idObj];
	
	if (!el) return false;	
	if (el.iframe==null) return false;	
	
	var responseText	= resultsAvailable(el.iframe);
	el.responseText		= responseText;
	el.readyState		= 4;
	el.status			= 200;
	el.onreadystatechange();
	
}

function resultsAvailable(ele) 
{	
	
	var theData = '';
	
	if (ele==null) return theData;	
	
	var domDoc = null;
	var IFrameObj = ele;
	
	if (IFrameObj.contentDocument) 
	{
		// For NS6
		domDoc = IFrameObj.contentDocument; 
	} else if (IFrameObj.contentWindow) {
		// For IE5.5 and IE6
		domDoc = IFrameObj.contentWindow.document;
	} else if (IFrameObj.document) {
		// For IE5
		domDoc = IFrameObj.document;
	} else {
		return '';
	}
		
	var src = domDoc.getElementsByTagName ? domDoc.getElementsByTagName('body')[0] : (domDoc.body ? domDoc.body : null);
	if (!src) return '';
	
	if (src.innerHTML)	
	{			
		theData = src.innerHTML;
	}
	else if (src.childNodes)
	{		
		for (var i = 0; i < src.childNodes.length; i++)
			theData += src.childNodes.item(i);
	}
	return theData;
}

var httpxmlObjStat = 0;

function DoCommunicate(rootPath, commands) {
	var httpxmlObj = 0;
	var result = '';
	var gettingStatus = false;
	DoCommunicateLocation = 1;
	DoCommunicateCommand = commands;
	if (commands.indexOf('command=new_global_status') == -1) {
		DoCommunicateLocation = 2;
		httpxmlObj = getXMLHTTPConnection();
		DoCommunicateLocation = 3;
	} else {
		DoCommunicateLocation = 4;
		if (!(httpxmlObjStat)) {
			DoCommunicateLocation = 5;
			httpxmlObjStat = getXMLHTTPConnection();
		}
		DoCommunicateLocation = 6;
		httpxmlObj = httpxmlObjStat;
		DoCommunicateLocation = 7;
		gettingStatus = true;
		DoCommunicateLocation = 8;
	}	
	DoCommunicateLocation = 9;
	if (rootPath.toLowerCase().indexOf('.asp') == -1) {
		DoCommunicateLocation = 10;
		httpxmlObj.open('POST',rootPath + 'Includes/server/AsyncComm.asp',gettingStatus);
		DoCommunicateLocation = 11;
	} else {
		DoCommunicateLocation = 12;
		httpxmlObj.open('POST',rootPath,false);
		DoCommunicateLocation = 13;
	}
	DoCommunicateLocation = 14;
	httpxmlObj.onreadystatechange = HandleStateChange;

	DoCommunicateLocation = 22;
	httpxmlObj.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=ISO-8859-1");
	DoCommunicateLocation = 23;
	httpxmlObj.send(commands);
	DoCommunicateLocation = 24;
	
	if (commands.indexOf('command=new_global_status') == -1) {
		DoCommunicateLocation = 25;
		result = httpxmlObj.responseText;
		DoCommunicateLocation = 26;
	} else {
		DoCommunicateLocation = 27;
		
		try {
			result = top.GlobalStatus;
			var txt = result.toString();
		} catch (e) {
			result = GlobalStatus;
		}
			
		DoCommunicateLocation = 28;
	}
	
	DoCommunicateLocation = 29;
	httpxmlObj = 0;
	DoCommunicateLocation = 30;
	return result;
}

function doRemoveGarbage(aux1) {
	var aux2 = aux1;
	var lOld = aux2.length+1;
	var lNew = aux2.length;
	while (lOld > lNew) {
		lOld = lNew;
		aux2 = aux2.replace(/<!-- [^>]*>/, '').replace('\n', '').replace('\r', '');
		lNew = aux2.length;
	}
	return aux2;
}

function HandleStateChange () {
		var sAux = ''
		DoCommunicateLocation = 15;
		if (httpxmlObjStat.readyState != 4) { 
			DoCommunicateLocation = 16;
			return ; }
		try {
			DoCommunicateLocation = 17;
			sAux = httpxmlObjStat.responseText;
			DoCommunicateLocation = 18;
			var tmpsAux=doRemoveGarbage(sAux);
			if (tmpsAux.indexOf('<') == -1) {
				DoCommunicateLocation = 19;
				if (sAux.length > 0) {
					DoCommunicateLocation = 20;
					try {
						top.GlobalStatus=tmpsAux;
						top.myDebug=sAux;
						DoCommunicateLocation = 21;
					} catch(ee) {
						GlobalStatus=tmpsAux;
						myDebug=sAux;
						DoCommunicateLocation = 21;
					}
				}
			} else {
				
				if (!(tmpsAux.indexOf('doExit()') == -1)) {
					try {
						top.myDebug='doExit()';
						DoCommunicateLocation = 211;
					} catch(ee) {
						myDebug='doExit()';
						DoCommunicateLocation = 212;
					}
				}
			
			}
		} catch(e) {
		}
	};
