/*
 * $RCSfile: XmlHttp.js,v $
 *
 * Gallery - a web based photo album viewer and editor
 * Copyright (C) 2000-2006 Bharat Mediratta
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA  02110-1301, USA.
 */

var async = true;
var xmlhttp = new Array();
var method = "POST";
var bLoaded = false;

// specify either a div tag for the reponse or a callback function
function loadXMLDoc(url, params, ref, div_id, myCallback) {
	xmlhttp[ref]=null;
	bLoaded = false;
	// code for Mozilla, etc.
	if (window.XMLHttpRequest) {
		xmlhttp[ref]=new XMLHttpRequest();
	}
	// code for IE
	else if (window.ActiveXObject) {
		xmlhttp[ref]=new ActiveXObject("Microsoft.XMLHTTP");
	}
	// perform the xmlhttp request
	if (xmlhttp[ref]!=null) {
		xmlhttp[ref].onreadystatechange=function () {
			if(xmlhttp[ref].readyState==4) {
				// Get data from the server's response
				if (typeof eval(myCallback) == 'function') {
					eval(myCallback + "('" + escape(xmlhttp[ref].responseText) + "')");
				}
				else {
					document.getElementById(div_id).innerHTML=xmlhttp[ref].responseText;
				}
				bLoaded = true;
			}
		}
		// SEND THE REQUEST
		if (method=="POST") {
			xmlhttp[ref].open("POST", url, async);
			xmlhttp[ref].setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			xmlhttp[ref].setRequestHeader("Content-length", params.length);
			xmlhttp[ref].setRequestHeader("Connection", "close");
			xmlhttp[ref].send(params);
		}
		else {
			xmlhttp[ref].open("GET", url + "?" + params, async);
			xmlhttp[ref].send("");
		}
	}
	else {
		alert("Your browser does not support XMLHTTP.");
	}
}

function SendHttpPost(xmlHttp, url, args, callback) {
    xmlHttp.open("POST", url, /* async */ async);
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", args.length);
	xmlHttp.setRequestHeader("Connection", "close");
    xmlHttp.onreadystatechange = function() { callback(xmlHttp); }
    xmlHttp.send(args);
}

function SendHttpGet(xmlHttp, url, callback) {
    xmlHttp.open("GET", url, /* async */ async);
    xmlHttp.onreadystatechange = function() { callback(xmlHttp); }
    xmlHttp.send("FOO");
}
function GetXmlHttp() {
    var xmlHttp = null;
    try {
		xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
		try {
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e) {
			try {
				xmlHttp = new XMLHttpRequest();
			} catch (e) {
				xmlHttp = false;
			}
		}
    }

    if (!xmlHttp && typeof XMLHttpRequest!='undefined') {
		xmlHttp = new XMLHttpRequest();
    }

    return xmlHttp;
}

// Removes leading whitespaces
function LTrim( value ) {

	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");

}

// Removes ending whitespaces
function RTrim( value ) {

	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");

}

// Removes leading and ending whitespaces
function trim( value ) {

	return LTrim(RTrim(value));

}
