/* ***** BEGIN LICENSE BLOCK *****
 * 
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is Netscape code.
 *
 * The Initial Developer of the Original Code is
 * Netscape Corporation.
 * Portions created by the Initial Developer are Copyright (C) 2003
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s): Marcio Galli <mgalli@mgalli.com>
 * Contributors(s): Elder Reami <reami@yahoo.com>
 *
 * ***** END LICENSE BLOCK ***** */ 

function XMLRemoteRequest() {
	this.XMLHttpComponent = this.getXMLHttpComponentInstance();
}

XMLRemoteRequest.prototype.getXMLHttpComponentInstance = function () {
	var xComp = false;

	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
	// Jscript gives us conditional compilation. we can cope with old IE
	// versions and security blocked creation of the objects.
	try {
		xComp = new ActiveXObject("Msxml2.XMLHttp");
		this.handleRequestAsString = ieRequestAsStringHandler;
		this.handleRequestDOM = ieRequestDOMHandler;
	} catch (e) {
		try {
			xComp = new ActiveXObject("Microsoft.XMLHTTP");
			this.handleRequestAsString = ieRequestAsStringHandler;
			this.handleRequestDOM = ieRequestDOMHandler;
		} catch (E)	 {
			xComp = false;
		}
	}
	@end @*/			
	if (!xComp && typeof XMLHttpRequest != 'undefined') {
		xComp = new XMLHttpRequest();
		this.handleRequestAsString = netscapeRequestAsStringHandler;
		this.handleRequestDOM = netscapeRequestDOMHandler;
	}
	return xComp;
}

XMLRemoteRequest.prototype.getRemoteDocument = function (urlString, cookie) {
	var rv =  this.handleRequestDOM(this.XMLHttpComponent, urlString, cookie);
        return rv;
}

XMLRemoteRequest.prototype.getRemoteDocumentString = function (urlString,
cookie) {
	return this.handleRequestAsString(this.XMLHttpComponent, urlString, cookie);
}

// Netscape specifics
function netscapeRequestDOMHandler(xmlComp, urlString, params) {
	var authcookie = '';
	var postdata = '';
	var body = null;

	for (prop in params) {

		if (prop == 'UID' || prop == 'Ticket' || prop == 'session') {
			if (authcookie != '') {
				authcookie = authcookie +'; ' + prop + '=' + params[prop];
			}
			else {
				authcookie = prop + '=' + params[prop];
			}
			// confirm(authcookie);
		}
		else if (prop == 'WDDXContent') {
			body = params[prop];
			body = escapeEntities(body);
			postdata = postdata + '&WDDXContent=' + body;
		}
		else {
			postdata = postdata + '&' + prop + '=' + params[prop];
		}
   }
	var url = urlString;
	if (body != null) {
		// url = url + '&WDDXContent=' + body; 
	}
    xmlComp.open('POST', url, false);
	if(authcookie != null) {
		xmlComp.setRequestHeader('Cookie', authcookie);
	}
	xmlComp.setRequestHeader('Content-Type',
		'application/x-www-form-urlencoded');
	xmlComp.send(postdata);
	if (xmlComp.responseXML) {
		return xmlComp.responseXML;
	}
	return null;
}

function netscapeRequestAsStringHandler(xmlComp, urlString, params) {
	var authcookie = '';
    var postdata = '';
	var body = null;

	for (prop in params) {
		if (prop == 'UID' || prop == 'Ticket' || prop == 'session') {
			if (authcookie != '') {
				authcookie = authcookie +'; ' + prop + '=' + params[prop];
			}
			else {
				authcookie = prop + '=' + params[prop];
			}
			// confirm(authcookie);
		}
		else if (prop == 'WDDXContent') {
			body = params[prop];
			body = escapeEntities(body);
			postdata = postdata + '&WDDXContent=' + body;
		}
		else {
			postdata = postdata + '&' + prop + '=' + params[prop];
		}
	}
	var url = urlString;
	if (body != null) {
		// url = url + '&WDDXContent=' + body; 
	}
	xmlComp.open('POST', url, false);
	if (authcookie != null) {
		xmlComp.setRequestHeader('Cookie', authcookie);
	}
	xmlComp.setRequestHeader('Content-Type',
		'application/x-www-form-urlencoded');
	xmlComp.send(postdata);

// alert('status: '+xmlComp.status+' '+xmlComp.statusText+url);
	if (xmlComp.responseText) {
		docString = xmlComp.responseText;
		return docString;
	}
	if (xmlComp.responseXML) {
		var dummyDoc = xmlComp.responseXML;
		var dummySerializer = new XMLSerializer();
		docString = dummySerializer.serializeToString(dummyDoc);
		return docString;
	}
	return null;
}

// IE specifics
function ieRequestDOMHandler(xmlComp, requestString, params) {
  var authcookie = '';
  var postdata = '';
  var body = null;
	  for (prop in params) {
		if (prop == 'UID' || prop == 'Ticket' || prop == 'session') {
			if (authcookie != '') {
				authcookie = authcookie +'; ' + prop + '=' + params[prop];
			}
			else {
				authcookie = prop + '=' + params[prop];
			}
			// confirm(authcookie);
		}
		else if (prop == 'WDDXContent') {
			body = params[prop];
			body = escapeEntities(body);
			postdata = postdata + '&WDDXContent=' + body;
		}
		else {
			postdata = postdata + '&' + prop + '=' + params[prop];
		}
	 }
	var url = requestString;
	if (body != null) {
		// url = url + '&WDDXContent=' + body; 
	}
	xmlComp.open('POST', url, false);
	if(authcookie != null) {
		xmlComp.setRequestHeader('Cookie', authcookie);
	}
	xmlComp.setRequestHeader('Content-Type',
		'application/x-www-form-urlencoded');
	xmlComp.send(postdata);
	return xmlComp.responseXML;
}

function ieRequestAsStringHandler(xmlComp, requestString, params) {
  var authcookie = '';
  var postdata = '';
  var body = null;
	  for (prop in params) {
		if (prop == 'UID' || prop == 'Ticket' || prop == 'session') {
			if (authcookie != '') {
				authcookie = authcookie +'; ' + prop + '=' + params[prop];
			}
			else {
				authcookie = prop + '=' + params[prop];
			}
			// confirm(authcookie);
		}
		else if (prop == 'WDDXContent') {
			body = params[prop];
			body = escapeEntities(body);
			postdata = postdata + '&WDDXContent=' + body;
		}
		else {
			postdata = postdata + '&' + prop + '=' + params[prop];
		}
	 }

	var url = requestString;
	if (body != null) {
		// url = url + '&WDDXContent=' + body; 
	}
	xmlComp.open('POST', url, false);
	if(authcookie!=null) {
		xmlComp.setRequestHeader('Cookie', authcookie);
	}
	xmlComp.setRequestHeader('Content-Type',
		'application/x-www-form-urlencoded');
	xmlComp.send(postdata);

	return xmlComp.responseText;
}

// noninteractive test for whether XMLHTTPRequest()
// functionality is present.
function HasXMLHTTPRequest() {
	var xComp = false;
	try {
		// native? (Mozilla and friends)
		xComp = new XMLHttpRequest();
		return true;
	} catch (e) {
		// Microsoft ActiveX implementation?
		try {
			xComp = new ActiveXObject("Microsoft.XMLHTTP");
			return true;
		} catch (e) {
			return false;
		}
	}
	return false;
}

function testXMLHTTPRequest() {
   var xComp=null;
   xComp = new XMLHttpRequest();
	alert('1');
	if(xComp) return true;

   xComp = new ActiveXObject("Microsoft.XMLHTTP");
	alert('2');
	if(xComp) return true;

   return false;
}

//
// getWddxPacket
//
function getWddxPacket(loaddata, params) {

// for (prop in params) {
//		confirm("Property: "+prop+"\nValue: "+params[prop]+"\n");
// }
	var x = new XMLRemoteRequest();
	doc = x.getRemoteDocumentString(loaddata, params);
// confirm('getWddxPacket: '+doc);
	return doc;
}

function escapeEntities(str) {
  var A = new Array();

  	A = str.split("&lt;");
  	str = A.join("%26lt%3B");

  	A = str.split("&gt;");
  	str = A.join("%26gt%3B");

  	A = str.split("&quot;");
  	str = A.join("%26quot%3B");

  	A = str.split("&apos;");
  	str = A.join("%26apos%3B");

  	A = str.split("&amp;");
  	str = A.join("%26amp%3B");

	return str;
}