/* javascript components: xml*/

var ELEMENT=0;
var ATTRIBUTE=1;
var TEXT=2;
/************
class Element
************/

Element.prototype.type=ELEMENT;
Element.prototype.name;
Element.prototype.attributes;
Element.prototype.nodes;
function Element(name){
	
	//alert("new Element:"+name);
	this.name=name;
	this.nodes=new Array();
	this.attributes=new Array();
	
};
Element.prototype.add=function(node){
	this.nodes[this.nodes.length]=node;
};
Element.prototype.addAttribute=function(key,value){
	this.attributes[this.attributes.length]=new Attribute(key,value);
};
Element.prototype.addText=function(string){
	this.nodes[this.nodes.length]=new Text(text);
};
Element.prototype.asXML=function(){
	if(this.nodes.length==0){	
		var buf="<"+this.name+" "+this.attributesAsXML()+"/>\n";
		return buf;
	}
	
	var buf="<"+this.name+" "+this.attributesAsXML()+">\n";
	
	for(var i=0;i<this.nodes.length;i++){
		buf+=this.nodes[i].asXML();
	}
		
	buf+="</"+this.name+">\n";
		
	return buf;
};

Element.prototype.attributesAsXML=function(){
	var str="";
	for(var i=0;i<this.attributes.length;i++){
		str+=" "+this.attributes[i].asXML();
	}
	return str;
};

/**************
class Attribute
**************/

Attribute.prototype.type=ATTRIBUTE;
Attribute.prototype.key;
Attribute.prototype.value;
function Attribute(key,value){
	
	this.key=key;
	this.value=value;
};
Attribute.prototype.asXML=function(){
	return this.key+"=\""+this.value+"\"";
};

/*********
class Text
*********/

Text.prototype.type=TEXT;
Text.prototype.value;
function Text(value){
	
	this.value=value;
};
Text.prototype.asXML=function(){
	return value;
};

/********
class XML
********/

function XML(){}
XML.load=function(file){
	var request;
	try{ // Internet Explorer
		request=new ActiveXObject("Microsoft.XMLDOM");
	}catch(ex){
		try{ // Firefox, Mozilla, Opera, ...
			request=new XMLHttpRequest();
		}catch(ex){
			alert(e.message);
			return;
		}
	}
	
	var browser=navigator.appName.toLowerCase();
	if(browser.indexOf("explorer")>0){
		request.async=false;
		request.load(file);
		return request;
	}else{
		request.open("GET",file,false);
		request.send(null);
		return request.responseXML;
	}
	
	
};
