// libXML 2.1 - UF 2009-07-10


function idgXMLObject (paramXML) {

  this.responseReader=function () { };
  this.commObject=new idgCommObject ();
  this.commObject.setResponseListener (this.loadedXML.bind (this));
  this.XML=new Object ();
  
  if (paramXML!=undefined) {
    this.parseBuffer=paramXML; 
    this.XML=this.parseXML (this.XML);
    this.XML=this.reduceXML (this.XML);
  }
  
}



idgXMLObject.prototype.setResponseReader = function (paramResponseReader) {
  this.responseReader=paramResponseReader;
}


idgXMLObject.prototype.loadXML = function (paramURL, paramQuestion) {

  this.commObject.URL=paramURL;
  this.commObject.getContent (paramQuestion==undefined?'':paramQuestion);
}



idgXMLObject.prototype.loadedXML = function () {

  this.parseBuffer=this.commObject.responseText;
  
  if (this.parseBuffer.indexOf ('<?xml')!=-1) {
    intTagStart=this.parseBuffer.indexOf ('>');
    this.parseBuffer=this.parseBuffer.substr (intTagStart+1).trim ();
  }
  
  this.XML=this.parseXML (this.XML);
  this.XML=this.reduceXML (this.XML);
  this.responseReader ();
}



idgXMLObject.prototype.setValue = function (paramCrombPath, paramValue, paramCromb) {

  if (paramCrombPath==null) return;

  if (paramCromb==undefined) paramCromb=this.XML;
  
  var nextCromb=paramCrombPath.explode ('.', 2);
  if (nextCromb[1]!='') {
    if (typeof (paramCromb[nextCromb[0]])=='undefined') paramCromb[nextCromb[0]]=[];
    this.setValue (nextCromb[1], paramValue, paramCromb[nextCromb[0]])
  } else {
    if (typeof (paramCromb[nextCromb[0]])=='undefined') paramCromb[nextCromb[0]]=[];
    paramCromb[nextCromb[0]]['value']=paramValue;
  }
  
}

idgXMLObject.prototype.getValue = function (paramCrombPath, paramCromb) {

  if (paramCromb==undefined) paramCromb=this.XML;

  var nextCromb=paramCrombPath.explode ('.', 2);
  if (nextCromb[1]!='') {
    if (typeof (paramCromb[nextCromb[0]])=='undefined') return ('');
    return (this.getValue (nextCromb[1], paramCromb[nextCromb[0]]));
  } else {
    if (typeof (paramCromb[nextCromb[0]])=='undefined') return ('');
    return (paramCromb[nextCromb[0]]['value']);
  }
  
}

idgXMLObject.prototype.deleteValue = function (paramCrombPath, paramCromb) {

  if (paramCromb==undefined) paramCromb=this.XML;

  var nextCromb=paramCrombPath.explode ('.', 2);
  if (nextCromb[1]!='') {
    if (typeof (paramCromb[nextCromb[0]])=='undefined') return;
    this.deleteValue (nextCromb[1], paramCromb[nextCromb[0]]);
  } else {
    if (typeof (paramCromb[nextCromb[0]])=='undefined') return;
    delete paramCromb[nextCromb[0]];
  }
  
}


idgXMLObject.prototype.parseXML = function (XML) {

  this.parseBuffer=this.parseBuffer.trim ();
  var intTagStart, intTagEnd, strTagValue, strTagNameAndAttributes, strAttributeName;
  var arrAttribs=[], arrTagAttributes=[], arrAttribValue=[];
  
  while (this.parseBuffer.indexOf ('<')!=-1) {

    intTagStart=this.parseBuffer.indexOf ('<')+1;
    intTagEnd=this.parseBuffer.indexOf ('>', intTagStart);
    
    if (this.parseBuffer.substr (intTagStart,1)=='/') {      
      strTagValue=this.parseBuffer.substr (0, intTagStart-1);
      this.parseBuffer=this.parseBuffer.substr (intTagEnd+1).trim ();
      if (strTagValue.length ==0) {
      } else {
        XML['value']=[];
        XML['value'][0]=strTagValue;
      }
      return;
    }

    strTagNameAndAttributes=this.parseBuffer.substr (intTagStart, intTagEnd-intTagStart);
    arrTagAttributes=strTagNameAndAttributes.explode (' ', 2);
    this.parseBuffer=this.parseBuffer.substr (intTagEnd+1).trim ();

   
    if (!XML[arrTagAttributes[0]]) XML[arrTagAttributes[0]]=[];
    XML[arrTagAttributes[0]][XML[arrTagAttributes[0]].length]=[];
    this.parseXML (XML[arrTagAttributes[0]][XML[arrTagAttributes[0]].length-1]);

    if (arrTagAttributes[1].length>0) {
      XML[arrTagAttributes[0]][XML[arrTagAttributes[0]].length-1]['attributes']=[];
      XML[arrTagAttributes[0]][XML[arrTagAttributes[0]].length-1]['attributes'][0]=[];
      arrAttribs=arrTagAttributes[1].split ('="');
      strAttributeName=arrAttribs[0];
      for (var dummy=1; dummy<(arrAttribs.length-1); dummy++) {
        arrAttribValues=arrAttribs[dummy].split ('" ');
        XML[arrTagAttributes[0]][XML[arrTagAttributes[0]].length-1]['attributes'][0][strAttributeName]=[];
        XML[arrTagAttributes[0]][XML[arrTagAttributes[0]].length-1]['attributes'][0][strAttributeName][0]=[];
        XML[arrTagAttributes[0]][XML[arrTagAttributes[0]].length-1]['attributes'][0][strAttributeName][0]['value']=[];
        XML[arrTagAttributes[0]][XML[arrTagAttributes[0]].length-1]['attributes'][0][strAttributeName][0]['value']=[arrAttribValues[0]];
        strAttributeName=arrAttribValues[1];
      }
      XML[arrTagAttributes[0]][XML[arrTagAttributes[0]].length-1]['attributes'][0][strAttributeName]=[];
      XML[arrTagAttributes[0]][XML[arrTagAttributes[0]].length-1]['attributes'][0][strAttributeName][0]=[];
      XML[arrTagAttributes[0]][XML[arrTagAttributes[0]].length-1]['attributes'][0][strAttributeName][0]['value']=[arrAttribs[arrAttribs.length-1].substr (0, arrAttribs[arrAttribs.length-1].length-1)];
    }
  }

  return (XML);
}



idgXMLObject.prototype.whiteSpace = function (Text) {
  var arrZeilen=Text.split ('\n');
  for (var dummy=0, retValue=''; dummy<arrZeilen.length; dummy++) retValue+=arrZeilen[dummy].trim ();
  return (retValue);  
}



idgXMLObject.prototype.reduceXML =  function  (XML) {
  var arrRetValue=[];
    
  var intArrayLength=0;
  for (var key in XML) intArrayLength++;

  switch (intArrayLength) {
    case  0: break;
    case  1: arrKey=array_keys (XML);
             if (is_array (XML[arrKey[0]])) {
               if (arrKey[0]!='0') {
                 arrRetValue[arrKey[0]]=this.reduceXML (XML[arrKey[0]]);
               } else {
                 arrRetValue=this.reduceXML (XML[arrKey[0]]);
               }
             } else {
               arrRetValue=XML[arrKey[0]];
             }
             break;
    default: for(var key in XML) arrRetValue[key]=this.reduceXML (XML[key]);
  }
  return (arrRetValue);
}



idgXMLObject.prototype.fnXML2String = function (paramXML, paramLevel) {

  var strRetValue='', strAttributes, attributeValue;

  if (paramLevel==undefined) paramLevel=-1;
  paramLevel++;

//  window.alert ('Start der Routine');

  for (var key in paramXML) {
    value=paramXML[key];
    if (key=='attributes') continue;
    strAttributes='';

    if (value.length) {
      for (var dummy=0; dummy<value.length; dummy++) {
        if (typeof (value[dummy])=='undefined') continue;
        strAttributes='';
        if (value[dummy]['attributes']) {
	  for (var attribute in value[dummy]['attributes']) {
	    strAttributes+=' '+attribute+'="'+value[dummy]['attributes'][attribute]['value']+'"';
	  }
        }
        strRetValue+=str_pad ('', 2*paramLevel, ' ')+'<'+key+strAttributes+'>';
        if (!value[dummy]['value']) {
          strRetValue+='\n';
          strRetValue+=this.fnXML2String (value[dummy], paramLevel);
          strRetValue+=str_pad ('', 2*paramLevel, ' ');
        } else {
          strRetValue+=value[dummy]['value'];
        }
        strRetValue+='</'+key+'>\n';
      }

    } else {
      if (value['attributes']) {
        for (var attribute in value['attributes']) {
          strAttributes+=' '+attribute+'="'+value['attributes'][attribute]['value']+'"';
        }
      }
      strRetValue+=str_pad ('', 2*paramLevel, ' ')+'<'+key+strAttributes+'>';
      if (!value['value']) {
        strRetValue+='\n';
        strRetValue+=this.fnXML2String (value, paramLevel);
        strRetValue+=str_pad ('', 2*paramLevel, ' ')+'</'+key+'>\n';
      } else {
        strRetValue+=value['value'];
        strRetValue+='</'+key+'>\n';
      }
    }
  }

//  window.alert (strRetValue);
  return (strRetValue);
}



















function fnDebugOut (paramText) {
  document.getElementById ('debug').innerHTML='<PRE>'+paramText+'</PRE>';
}
  
function fnDebugOutAppend (paramText) {
  document.getElementById ('debug').innerHTML=document.getElementById ('debug').innerHTML+paramText;
}

function is_array( mixed_var ) {
    var key = '';

    if (!mixed_var) {
        return false;
    }

    // BEGIN REDUNDANT
    if (!this.php_js) {
        this.php_js = {};
    }
    if (!this.php_js.ini) {
        this.php_js.ini = {};
    }
    // END REDUNDANT

    if (typeof mixed_var === 'object') {

        if (this.php_js.ini['phpjs.objectsAsArrays'] &&  // Strict checking for being a JavaScript array (only check this way if call ini_set('phpjs.objectsAsArrays', 0) to disallow objects as arrays)
            (
            (this.php_js.ini['phpjs.objectsAsArrays'].local_value.toLowerCase &&
                    this.php_js.ini['phpjs.objectsAsArrays'].local_value.toLowerCase() === 'off') ||
                parseInt(this.php_js.ini['phpjs.objectsAsArrays'].local_value, 10) === 0)
            ) {
            return mixed_var.hasOwnProperty('length') && // Not non-enumerable because of being on parent class
                            !mixed_var.propertyIsEnumerable('length') && // Since is own property, if not enumerable, it must be a built-in function
                                mixed_var.constructor.name !== 'String'; // exclude String()
        }

        if (mixed_var.hasOwnProperty) {
            for (key in mixed_var) {
                // Checks whether the object has the specified property
                // if not, we figure it's not an object in the sense of a php-associative-array.
                if (false === mixed_var.hasOwnProperty(key)) {
                    return false;
                }
            }
        }

        // Read discussion at: http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_is_array/
        return true;
    }

    return false;
}


function array_keys( input, search_value, argStrict ) {
    
    var tmp_arr = {}, strict = !!argStrict, include = true, cnt = 0;
    var key = '';
    
    for (key in input) {
        include = true;
        if (search_value != undefined) {
            if( strict && input[key] !== search_value ){
                include = false;
            } else if( input[key] != search_value ){
                include = false;
            }
        }
        
        if (include) {
            tmp_arr[cnt] = key;
            cnt++;
        }
    }
    
    return tmp_arr;
}


function array_intersect_key() {
    var arr1 = arguments[0], retArr = {};
    var k1 = '', arr = {}, i = 0, k = '';

    arr1keys:
    for (k1 in arr1) {
        arrs:
        for (i=1; i < arguments.length; i++) {
            arr = arguments[i];
            for (k in arr) {
                if (k === k1) {
                    if (i === arguments.length-1) {
                        retArr[k1] = arr1[k1];
                    }
                    // If the innermost loop always leads at least once to an equal value, continue the loop until done
                    continue arrs;
                }
            }
            // If it reaches here, it wasn't found in at least one array, so try next value
            continue arr1keys;
        }
    }

    return retArr;
}



function print_r( array, return_val ) {
    
    var output = "", pad_char = " ", pad_val = 4, d = document;

    var repeat_char = function (len, pad_char) {
        var str = "";
        for(var i=0; i < len; i++) {
            str += pad_char;
        }
        return str;
    };

    var formatArray = function (obj, cur_depth, pad_val, pad_char) {
        if (cur_depth > 0) {
            cur_depth++;
        }

        var base_pad = repeat_char(pad_val*cur_depth, pad_char);
        var thick_pad = repeat_char(pad_val*(cur_depth+1), pad_char);
        var str = "";

        if (typeof obj === 'object' && obj !== null && obj.constructor && obj.constructor.name !== 'PHPJS_Resource') {
            str += "Array\n" + base_pad + "(\n";
            for (var key in obj) {
                if (obj[key] instanceof Array) {
                    str += thick_pad + "["+key+"] => "+formatArray(obj[key], cur_depth+1, pad_val, pad_char);
                } else {
                    str += thick_pad + "["+key+"] => " + obj[key] + "\n";
                }
            }
            str += base_pad + ")\n";
        } else if(obj === null || obj === undefined) {
            str = '';
        } else { // for our "resource" class
            str = obj.toString();
        }

        return str;
    };

    output = formatArray(array, 0, pad_val, pad_char);

    if (return_val !== true) {
        if (d.body) {
            this.echo(output);
        }
        else {
            try {
                d = XULDocument; // We're in XUL, so appending as plain text won't work; trigger an error out of XUL
                this.echo('<pre xmlns="http://www.w3.org/1999/xhtml" style="white-space:pre;">'+output+'</pre>');
            }
            catch(e) {
                this.echo(output); // Outputting as plain text may work in some plain XML
            }
        }
        return true;
    } else {
        return output;
    }
}


function str_pad( input, pad_length, pad_string) {

  for (var dummy=input.length; dummy<pad_length; dummy+=pad_string.length) {
    input+=pad_string;
  }
  return (input);

}



Function.prototype.bind = function (obj) {
	var fn = this;
	return function () {
		var args = [this];
		for (var i = 0, ix = arguments.length; i < ix; i++) {
			args.push(arguments[i]);
		}
		return fn.apply(obj, args);
	};
};
