var sTextTotal="";	
function fLoadXML(url,clearCache){
	var oThis=this;
	if(String(clearCache)=="undefined"){clearCache=false;}
	if(clearCache){
		ran=Math.random();
		this.aAjax = new Ajax(url+"?ran="+ran, {method: 'get'});
	} else {
		this.aAjax = new Ajax(url, {method: 'get'});
	}
	this.aAjax.xXml=null;
	this.aAjax.request();
	this.aAjax.onFailure=function (){
		alert(" fail loading XML "+url);
	}
	this.aAjax.onSuccess=function (){
		sTextTotal = "";
		oThis.xXml = this.transport.responseXML;
		oThis.aXml = oThis.parseXML(oThis.xXml);
		oThis.fSuccess();
	}
	return this;
}

fLoadXML.prototype.parseXML=function(xXml){
	aXml=new Array();
	aXml=create_object_structure(xXml);
	//get_attribute('subsection')
	return aXml;
}

var create_object_structure = function(stream) {
	var return_value = new cpaint_result_object();
	var node_name = '';
	var i         = 0;
	var attrib    = 0;
	if (stream.hasChildNodes() == true) {
	
	  for (i = 0; i < stream.childNodes.length; i++) {
	
		node_name = stream.childNodes[i].nodeName;
		node_name = node_name.replace(/[^a-zA-Z0-9_]*/g, '');
		// reset / create subnode
		if (typeof return_value[node_name] != 'object') {
		  return_value[node_name] = new Array();
		}
		
		if (stream.childNodes[i].nodeType == 1) {
		  var tmp_node  = create_object_structure(stream.childNodes[i]);
		  for (attrib = 0; attrib < stream.childNodes[i].attributes.length; attrib++) {
			tmp_node.set_attribute(stream.childNodes[i].attributes[attrib].nodeName, stream.childNodes[i].attributes[attrib].nodeValue);
		  }
		  
		  return_value[node_name].push(tmp_node);
			test="-node_type=element";
			test+="--node_name="+node_name;
		
		} else if (stream.childNodes[i].nodeType == 3) {
			test="-node_type=text";
			val=decode(String(stream.firstChild.data));
		 	 return_value.data  = val;
		  if(val!=="" && val!==" " && val!=="  " && val!==" " && val!=="   "){
				test+="--node_value=--"+val+"--l="+val.length;
				test+="--node_name="+node_name;
		  }
		} else if (stream.childNodes[i].nodeType == 4) {
			test="-node_type=cdata";
		  return_value.data  = decode(String(stream.firstChild.data));
		  if(return_value.data!==""){
			test+="--node_value="+return_value.data;
			test+="--node_name="+node_name;
		  }
		}
		//alert("test:"+test);
		sTextTotal+="  </br>-----"+test+"";
	  }
	}
	return return_value;
}

function cpaint_result_object() {
  this.id           = 0;
  this.data         = '';
  var __attributes  = new Array();
  
  /**
  * Returns a subnode with the given type and id.
  *
  * @access     public
  * @param      string    type    The type of the subnode. Equivalent to the XML tag name.
  * @param      string    id      The id of the subnode. Equivalent to the XML tag names id attribute.
  * @return     object
  */
  this.find_item_by_id = function() {
    var return_value  = null;
    var type    = arguments[0];
    var id      = arguments[1];
    var i       = 0;
    
    if (this[type]) {

      for (i = 0; i < this[type].length; i++) {

        if (this[type][i].get_attribute('id') == id) {
          return_value = this[type][i];
          break;
        }
      }
    }

    return return_value;
  }
  
  /**
  * retrieves the value of an attribute.
  *
  * @access   public
  * @param    string    name    name of the attribute
  * @return   mixed
  */
  this.get_attribute = function() {
    var return_value  = null;
    var id            = arguments[0];
    
    if (typeof __attributes[id] != 'undefined') {
      return_value = __attributes[id];
    }
    
    return return_value;
  }
  
  /**
  * assigns a value to an attribute.
  *
  * if that attribute does not exist it will be created.
  *
  * @access     public
  * @param      string    name    name of the attribute
  * @param      string    value   value of the attribute
  * @return     void
  */
  this.set_attribute = function() {
    __attributes[arguments[0]] = arguments[1];
  }
}
// =========================================================================
// =========================================================================
// =========================================================================

// CONSTANTS

// =========================================================================
// =========================================================================
// =========================================================================

//define the characters which constitute whitespace, and quotes
var whitespace = "\n\r\t ";
var quotes = "\"'";


  var decode = function(rawtext) {
    var plaintext = ''; 
    var i         = 0; 
    var c1        = 0;
    var c2        = 0;
    var c3        = 0;
    var u         = 0;
    var t         = 0;

    // remove special JavaScript encoded non-printable characters
    while (i < rawtext.length) {
      if (rawtext.charAt(i) == '\\'
        && rawtext.charAt(i + 1) == 'u') {
        
        u = 0;
        
        for (j = 2; j < 6; j += 1) {
          t = parseInt(rawtext.charAt(i + j), 16);
          
          if (!isFinite(t)) {
            break;
          }
          u = u * 16 + t;
        }

        plaintext += String.fromCharCode(u);
        i       += 6;
      
      } else {
        plaintext += rawtext.charAt(i);
        i++;
      }
    }

    // convert numeric data to number type
    if (plaintext != ''
      && plaintext.search(/^\s+$/g) == -1
      && !isNaN(plaintext) 
      && isFinite(plaintext)) {
      
      plaintext = Number(plaintext);
    }
  
    return plaintext;
  }

  function trim(str) {
	return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}
