 /****************************************************************
  *                                                              *
  *  xml2hash                                                    *
  *  ------------                                                *
  *                                                              *
  *  This script generates simple objects (hash) from xml file.  *
  *  Work on method post and get.				 *
  *                                                              *
  *  Version 0.5	                                         *
  *  Copyright (c) 2006 Gabriel Perez S.                         *
  *                                                              *
  *  Website: http://blondlibre.org/project/xml2hash             *
  *  Email:   gabriel@blondbeyond.com		                 *
  *                                                              *
  *  This library is free software; you can redistribute         *
  *  it and/or modify it under the terms of the GNU              *
  *  Lesser General Public License as published by the           *
  *  Free Software Foundation; either version 2.1 of the         *
  *  License.						         *
  *                                                              *
  *  This library 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 Lesser General Public       *
  *  License for more details.                                   *
  *                                                              *
  *  You should have received a copy of the GNU Lesser           *
  *  General Public License along with this library;             *
  *  Inc., 59 Temple Place, Suite 330, Boston,                   *
  *  MA 02111-1307 USA                                           *
  *                                                              *
  ****************************************************************/

function myXMLHttpRequest () {
        var xmlhttplocal;
        try { xmlhttplocal = new ActiveXObject ("Msxml2.XMLHTTP") }
        catch (e) {
                try { xmlhttplocal = new ActiveXObject ("Microsoft.XMLHTTP") }
                catch (E) { xmlhttplocal = false; }
        }
        if (!xmlhttplocal && typeof XMLHttpRequest != 'undefined') {
                try { var xmlhttplocal = new XMLHttpRequest (); }
                catch (e) { var xmlhttplocal = false; alert ('couldn\'t create xmlhttp object'); }
        }
        return (xmlhttplocal);
}

function postXml2Array(obj,data) {

	var xmlhttp = new myXMLHttpRequest ();
	var i;
	var url = obj.url;
	if (!data) {
		data = obj.data;
	}


	if (xmlhttp && url) {
		xmlhttp.open('POST',url,false);
		xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		
		var vsend;
		for (i in data) {
			if (!vsend)
				vsend = i+'='+data[i];
			else
				vsend += '&'+i+'='+data[i];
		}

		xmlhttp.send(vsend);
		
		if (xmlhttp.readyState == 4) {
			obj.plain = xmlhttp.responseText;
			obj.xml = Xml2Array_drive(xmlhttp.responseXML.documentElement);

			if (obj.onrun)
				eval(obj.onrun());
			else
				return obj.xml;
		}	
	}
	else {
		alert('Couldn\'t create XmlHttpRequest');
	}
}


function getXml2Array (obj) {

	var String;
	var xmlhttp = new myXMLHttpRequest ();
	var xml;
	var url;

	if (obj.urlbase) {
		url = obj.urlbase+'/'+obj.url;
	} else {
		url = obj.url;
	}
	
	if (xmlhttp && url) {
		xmlhttp.onreadystatechange = function () {
			if (xmlhttp.readyState == 4) {
				String = xmlhttp.responseText;
				if (String.length >= 0) {
					obj.plain = xmlhttp.responseText;
					var xmlobj = xmlhttp.responseXML.documentElement;
					if (xmlobj) {
						obj.xml = Xml2Array_drive(xmlobj);
						if (obj.onrun) {
							eval(obj.onrun());
						}
					}
				}
				else {
					alert("ERROR: No tiene permisos para esta operacion");
				}
			}
		}
		xmlhttp.open ("GET", url, true);
		xmlhttp.send (null);
		
	}
	else {
		alert('Couldn\'t create XmlHttpRequest');
	}
}



function Xml2Array_drive (o) {
	var f = new Object();
	var name = o.nodeName;
	if (o.hasChildNodes()) {
		var c; // count
		f[name] = new Object();
		for (c = 0; c < o.childNodes.length; c++) {
			var a = Xml2Array_drive(o.childNodes[c]);
			if (typeof(a) == 'string') {
				f[name] = a;
			}
			else if (typeof(a) == 'object') {
				var cname = o.childNodes[c].nodeName;

				if (!f[name][cname]) {
					if (typeof(a[cname]) != 'string') {
						f[name][cname] = new Array(a[cname]);
					}
					else {
						f[name][cname] = a[cname];
					}
				}
				else {
					if (typeof(f[name][cname]) == 'object')
						f[name][cname].push(a[cname]);
					else {
						if (typeof(f[name][cname]) == 'string') {
							var txt = f[name][cname];
							f[name][cname] = new Array();
							if (a[cname])
								f[name][cname].push(a[cname]);
							else
								f[name][cname].push(txt);

						}
						else {
							alert('no: '+name+' -- '+typeof(f[name][cname]));
						}
					}
				}
			}
		}
	}
	else {
		if (typeof(o.data) == 'string') {
			f = o.data;
		}
		else if (typeof(o.data) == 'numeric') {
			f = o.data;
		}
	}
	return f;
}
