
/*if (typeof addEvent != 'function')
{
 var addEvent = function(o, t, f, l)
 {
  var d = 'addEventListener', n = 'on' + t, rO = o, rT = t, rF = f, rL = l;
  if (o[d] && !l) return o[d](t, f, false);
  if (!o._evts) o._evts = {};
  if (!o._evts[t])
  {
   o._evts[t] = o[n] ? { b: o[n] } : {};
   o[n] = new Function('e',
    'var r = true, o = this, a = o._evts["' + t + '"], i; for (i in a) {' +
     'o._f = a[i]; r = o._f(e||window.event) != false && r; o._f = null;' +
     '} return r');
   if (t != 'unload') addEvent(window, 'unload', function() {
    removeEvent(rO, rT, rF, rL);
   });
  }
  if (!f._i) f._i = addEvent._i++;
  o._evts[t][f._i] = f;
 };
 addEvent._i = 1;
 var removeEvent = function(o, t, f, l)
 {
  var d = 'removeEventListener';
  if (o[d] && !l) return o[d](t, f, false);
  if (o._evts && o._evts[t] && f._i) delete o._evts[t][f._i];
 };
}*/

function cancelEvent(e, c)
{
 if (typeof e != 'undefined' && e!=null)
 {
	 e.returnValue = false;
	 if (e.preventDefault) e.preventDefault();
	 if (c)
	 {
	  e.cancelBubble = true;
	  if (e.stopPropagation) e.stopPropagation();
	 }
 }
};


function HTMLHttpRequest(myName, callback) { with (this)
{
 this.myName = myName;
 this.callback = callback;

  this.xmlhttp = null;
  this.iframe = null;
 window._ifr_buf_count |= 0;
 this.iframeID = myName + 'iframebuffer' + window._ifr_buf_count++;
  this.loadingURI = '';

// if (window.XMLHttpRequest && !window.ActiveXObject) xmlhttp = new XMLHttpRequest();
 if (!xmlhttp)
 {
  if (document.createElement && document.documentElement &&
   (window.opera || navigator.userAgent.indexOf('MSIE 5.0') == -1))
  {
   var ifr = document.createElement('iframe');
   ifr.setAttribute('id', iframeID);
   ifr.setAttribute('name', iframeID);
   ifr.style.visibility = 'hidden';
   ifr.style.position = 'absolute';
   ifr.style.width = ifr.style.Height = ifr.borderWidth = '0px'; 
//   ifr.style.width = ifr.style.Height = '100px'; 
//   ifr.style.pixelTop = "500px"
   
   ifr.src = 'blank.html';
   iframe = document.getElementsByTagName('body')[0].appendChild(ifr);
  }
  else if (document.body && document.body.insertAdjacentHTML)
  {
   document.body.insertAdjacentHTML('beforeEnd', '<iframe name="' + iframeID +
    '" id="' + iframeID + '" src="blank.html" style="display: none"></iframe>');
  }
  if (window.frames && window.frames[iframeID]) iframe = window.frames[iframeID];
  iframe.name = iframeID;
 }

 return this;
}};



HTMLHttpRequest.prototype.parseForm = function(form) { with (this)
{
 var str = '', gE = 'getElementsByTagName', inputs = [
  (form[gE] ? form[gE]('input') : form.all ? form.all.tags('input') : []),
  (form[gE] ? form[gE]('select') : form.all ? form.all.tags('select') : []),
  (form[gE] ? form[gE]('textarea') : form.all ? form.all.tags('textarea') : [])
 ];

 for (var i = 0; i < inputs.length; i++)
  for (j = 0; j < inputs[i].length; j++)
   if (inputs[i][j])
   {
    var plus = '++'.substring(0,1); // CodeTrim fix.
    str += escape(inputs[i][j].getAttribute('name')).replace(plus, '%2B') +
     '=' + escape(inputs[i][j].value).replace(plus, '%2B') + '&';
   }

 // Strip trailing ampersand, because we can :)
 return str.substring(0, str.length - 1);
}};



HTMLHttpRequest.prototype.xmlhttpSend = function(uri, formStr) { with (this)
{
 xmlhttp.open(formStr ? 'POST' : 'GET', uri, true);
 xmlhttp.onreadystatechange = function() {
  if (xmlhttp.readyState == 4)
  {
   if (callback) callback(xmlhttp.responseXML, xmlhttp.responseText, loadingURI);
   loadingURI = '';
  }
 };
 if (formStr && xmlhttp.setRequestHeader)
  xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

 if (xmlhttp.overrideMimeType)
  xmlhttp.overrideMimeType((/\.txt/i).test(uri) ? 'text/plain' : 'text/xml');
 xmlhttp.send(formStr);
 loadingURI = uri;
 return true;
}};


HTMLHttpRequest.prototype.iframeSend = function(uri, formRef) { with (this)
{

 //if (!document.readyState) return false;
 if (document.getElementById) var o = document.getElementById(iframeID).offsetWidth;

 if (formRef) 
 {
 		formRef.setAttribute('target', iframeID);
 		formRef.submit();
 }
 else
 {
  var ifrDoc = iframe.contentDocument || iframe.document;

  if (!window.opera && ifrDoc.location &&
   ifrDoc.location.href != location.href) ifrDoc.location.replace(uri);
  else iframe.src = uri;
 }

 loadingURI = uri;
 setTimeout(myName + '.iframeCheck()', (window.opera ? 250 : 100));
 return true;
}};


HTMLHttpRequest.prototype.iframeCheck = function() { with (this)
{

 doc = iframe.contentDocument || iframe.document;
 var il = iframe.location, dr = doc.readyState;
 if ((il && il.href ? ( il.href.match(loadingURI.replace("\?", "\\?")) || il.href.match('Error')) : 1) 
	  && (dr == 'complete' || (!document.getElementById && dr == 'interactive'))
	 )
 {
  var cbDoc = doc.documentElement || doc;
  if (callback) callback(cbDoc,
   (cbDoc.innerHTML || (cbDoc.body ? cbDoc.body.innerHTML : '')), loadingURI);

  if( typeof iframe.CopyContent_Completed != 'undefined' )
  {
  	iframe.CopyContent_Completed();
  }
   
  loadingURI = '';
 }
 else setTimeout(myName + '.iframeCheck()', 50);
}};


// *** PUBLIC METHODS ***

HTMLHttpRequest.prototype.load = function(uri) { with (this)
{

 if (!uri || (!xmlhttp && !iframe)) return false;
 if (xmlhttp) return xmlhttpSend(uri, '');
 else if (iframe) return iframeSend(uri, null);
 else return false;
}};


HTMLHttpRequest.prototype.submit = function(formRef, evt) { with (this)
{
 evt = evt || window.event;
 if (!formRef || (!xmlhttp && !iframe)) return false;

 var method = formRef.getAttribute('method'), uri = formRef.getAttribute('action');

 if (method && method.toLowerCase() == 'post')
 {
  if (xmlhttp) { cancelEvent(evt); return xmlhttpSend(uri, parseForm(formRef)) }
  else if (iframe) return iframeSend(uri, formRef);
  else return false;
 }
 else
 {
  // For GET requests, append ?querystring or &querystring to the GET uri and
  // forward it to the load() function. Always cancel form submit().
  cancelEvent(evt);
  return load(uri + (uri.indexOf('?') == -1 ? '?' : '&') + parseForm(formRef));
 }
}};

function RemoteFileLoader (myName) {
	this.myName = myName;
	this.threads = [];
	this.loadingIDs = {};
	this.onload = null;
};

RemoteFileLoader.prototype.getThread = function(destId) {
	with (this) {
		// Locates a thread that's EITHER: loading into the specified destination, OR: idle.
		// If none are match that condition, create a new one.

		var thr = -1;

		for (var id in loadingIDs) {
			// Same destination?
			if (id == destId) {
				thr = loadingIDs[id];
				break;
			}
		}
		if (thr == -1) for (var t = 0; t < threads.length; t++) {
			// Idle?
			if (!threads[t].loadingURI) {
				thr = t;
				break;
			}
		}
		if (thr == -1) {
			// Create a new HTMLHttpRequest with its own name as a reference to its array entry,
			// and no callback function as yet ;).
			thr = threads.length;
			threads[thr] = new HTMLHttpRequest(myName + '.threads[' + thr + ']', null);
			// Record this thread as loading for this destination, so it can be aborted later.
			loadingIDs[destId] = thr;
		}
		// Assign our callback function; it passed the destination to copyContent.
		threads[thr].callback = new Function('doc', 'text', 'uri', 'with (' + myName + ') { ' +
			'copyContent(doc, text, "' + destId + '"); if (onload) onload(doc, uri, "' + destId + '");  }');
		return threads[thr];
	}
};

RemoteFileLoader.prototype.loadInto = function(uri, destId) {
	return this.getThread(destId).load(uri);
};

RemoteFileLoader.prototype.submitInto = function(formRef, destId, event) {
	return this.getThread(destId).submit(formRef, event);
};

RemoteFileLoader.prototype.copyContent = function(docDOM, docText, destId) {
	var src = docDOM ?
		(docDOM.getElementsByTagName ?
			docDOM.getElementsByTagName('body')[0] :
			(docDOM.body ? docDOM.body : null) ) :
		null;
	var dest = document.getElementById ? document.getElementById(destId) :
		(document.all ? document.all[destId] : null);
	if (!dest || (!src && !docText)) return;

	if (src && src.innerHTML) dest.innerHTML = src.innerHTML;
	else if (src && document.importNode) {
		while (dest.firstChild) dest.removeChild(dest.firstChild);
		for (var i = 0; i < src.childNodes.length; i++)
			dest.appendChild(document.importNode(src.childNodes.item(i), true));
	} else if (docText) {
		if (docText.match(/(<body>)(.*)(<\/body>)/i)) docText = RegExp.$2;
		dest.innerHTML = docText;
	}
	/*if( docDOM.document.scripts.length > 0 )
	{
		var i;
		for(i=0; i<docDOM.document.scripts.length; i++)
			eval(docDOM.document.scripts[i].innerHTML);
	}*/
};

function DocumentLoader (sName, sURLSuffix, sDocumentQualifier, sIntoId) {
	/*
	alert(
		"Name : " + sName +
		"\nURLSuffix : " + sURLSuffix +
		"\nDocumentQualifier : " + sDocumentQualifier +
		"\nTargetId : " + sIntoId);
	*/
	this.name = sName;
	this.FileLoader = new RemoteFileLoader(sName + '.FileLoader');
	this.HtmlTextMessage = "<div class='WaitMessage' id='{_Message_ID_\}' style='DISPLAY:; Z-INDEX:450; FILTER:alpha(opacity:65); LEFT:0px; MARGIN:0px; WIDTH:100%; TOP:0px; HEIGHT:100%;' ><table width=100% height=100% border=0 cellpadding=0 cellspacing=0><tr height=100%><td width=100% align=middle valign=center><span class='WaitMessageText'>{_Message_Text_}</span></td></tr></table></div>";
	this.URLSuffix = sURLSuffix;
	this.DocumentQualifier = sDocumentQualifier;
	this.IntoId = sIntoId;
	if (!this.IntoId) {
		this.IntoId = 'DocumentContainer_' + sDocumentQualifier;
	}
	this.method = null;
	this.uri = null;
	this.formRef = null;
};

DocumentLoader.prototype.clearContent = function() {
	//alert("URL : " + sURL);
	with (this) {
		var oDoc = document.getElementById(IntoId);
		if( oDoc ) oDoc.innerHTML = '';
	}
};

DocumentLoader.prototype.launch = function(sIntoId) {
	with(this) {
		var sDestId = IntoId;
		if (typeof sIntoId != 'undefined') {
			sDestId = sIntoId;
		}
		switch (method) {
			case 'submit':
				RaiseEvent('www.activportal.mc:DocumentLoader', 'beforeonsubmit', this);
				FileLoader.submitInto(formRef, sDestId, null);
				break;
			case 'load':
				RaiseEvent('www.activportal.mc:DocumentLoader', 'beforeonload', this);
				FileLoader.loadInto(uri, sDestId);
				break;
		}
	}
};

DocumentLoader.prototype.load = function(sURL) {
	//alert("URL : " + sURL);
	with (this) {
		var sMessage = "Loading please wait...";
		if (typeof WaitMessage_Text != 'undefined' && WaitMessage_Text!='') sMessage = WaitMessage_Text;
		doDisplayMessage(sMessage);
		method = 'load';
		//formRef = oForm;
		uri = sURL;
		if (uri.indexOf("?") >= 0) {
			uri += "&URLSuffix=" + URLSuffix;
			uri += "&DocumentQualifier=" + DocumentQualifier
		} else {
			uri += "?URLSuffix=" + URLSuffix;
			uri += "&DocumentQualifier=" + DocumentQualifier;
		}
		//alert("URL : " + uri);
		setTimeout(name + '.launch();',1);
		return true;
		//return FileLoader.loadInto(sURL,IntoId);
	}
};

DocumentLoader.prototype.submit = function(oForm) {
	with(this) {
		var sMessage = "Please wait loading...";
		if( typeof WaitMessage_Text != 'undefined' ) sMessage = WaitMessage_Text;
		doDisplayMessage(sMessage)
		method = 'submit';
		formRef = oForm;
		setTimeout(name + '.launch();',1);
		return true;
		//return FileLoader.submitInto(oForm,IntoId,event);
	}
};

DocumentLoader.prototype.submitInto = function(oForm,sInto) {
	with(this) {
		var sMessage = "Please wait loading...";
		if (typeof WaitMessage_Text != 'undefined') sMessage = WaitMessage_Text;
		doDisplayMessage(sMessage)
		method = 'submit';
		formRef = oForm;
		setTimeout(name + ".launch('" + sInto + "');",1);
		return true;
		//return FileLoader.submitInto(oForm,IntoId,event);
	}
};

DocumentLoader.prototype.doDisplayMessage = function(sText) {
	with(this) {
		var oDoc = document.getElementById(IntoId);
		if (oDoc) {
			var oSpan = document.createElement("SPAN");
			var sMessage = HtmlTextMessage.replace("\{_Message_ID_\}",DocumentQualifier + '_WaitMsg');
			sMessage = sMessage.replace("\{_Message_Text_\}", sText);
			oSpan.innerHTML = sMessage;
			$(oSpan).getChildren()[0].style.width = oDoc.offsetWidth;
			$(oSpan).getChildren()[0].style.Height = oDoc.offsetHeight;
			oDoc.innerHTML = oSpan.innerHTML;
		}
	}
};


