// global flag
var isIE = false;

// global request and XML document objects
var req;

/*
function print_r(theObj){
  var output = '';
  if(theObj.constructor == Array ||
     theObj.constructor == Object){
    //output += '<ul>';
    //document.write("<ul>")
    for(var p in theObj){
      if(theObj[p].constructor == Array||
         theObj[p].constructor == Object){
         //document.write("<li>["+p+"] => "+typeof(theObj)+"</li>");
	output = '['+p+'] => '+typeof(theObj)+'\n';
        //document.write("<ul>")
	output = output + '\n';
        print_r(theObj[p]);
        //document.write("</ul>")
	//output += '</ul>';
      } else {
//document.write("<li>["+p+"] => "+theObj[p]+"</li>");
	output = '['+p+'] => '+theObj[p]+'\n';
      }
    }
    //document.write("</ul>")
	//output += '</ul>';
	return(output);
  }
}


function str_repeat(str, repeat) {
  var output = '';
  for (var i = 0; i < repeat; i++) {
    output += str;
  }
  return output;
}


var MAX_DEPTH = 10;
function print_r(obj, indent, depth) {
  var ws = '    '; //four whitespaces
  var output = '';
  indent = (!indent) ? 0 : indent;
  depth = (!depth) ? 0 : depth;
  if (depth > MAX_DEPTH) {
    return str_repeat(ws, indent) + '*Maximum Depth Reached*\n';
  }
  if (typeof(obj) == 'object') {
    output += (indent == 0) ? typeof(obj) + '\n(\n' : '';
    indent++;
    var child = '';
    for (var key in obj) {
      try {
        child = obj[key];
      }
      catch (e) {
        child = '*Unable To Evaluate*';
      }
      output += str_repeat(ws, indent) + '['+key+'] => ';
      if (typeof(child) == 'object') {
        indent++;
        output += typeof(child) + '\n';
        output += str_repeat(ws, indent) + '(\n';
        output += print_r(child, indent, depth+1);
        output += str_repeat(ws, indent) + ')\n';
        indent--;
      }
      else {
        output += child + '\n';
      }
    }
    indent.;
    output += (indent == 0) ? ')\n' : '';
    return output;
  }
  else {
    return str_repeat(ws, indent) + obj + '\n';
  }
} 
*/

function print_r(theObj,indent){
      var output="";
      if (indent == undefined) { indent = "  "; } else { indent += "  "; }
      if(theObj.constructor == Array || theObj.constructor == Object) {
        for(var p in theObj){
          if(theObj[p].constructor == Array|| theObj[p].constructor == Object){
              var type = (theObj[p].constructor == Array) ? "Array" : "Object";
              output += indent+"["+p+"]("+type+")=>\n";
              output += print_r(theObj[p],indent);
          } else { output += indent+"["+p+"]:"+theObj[p]+"\n"; }
        }
      }
      return output;
  } 


// retrieve XML document (reusable generic function);
// parameter is URL string (relative or complete) to
// an .xml file whose Content-Type is a valid XML
// type, such as text/xml; XML source must be from
// same domain as HTML file
function loadXMLDoc(url) {
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChange;
        req.open("GET", url, true);
        req.send(null);
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        isIE = true;
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChange;
            req.open("GET", url, true);
            req.send();
        }
    }
}

function getFormDataByTag(theform, tag)
{
	var thechilds = theform.getElementsByTagName(tag);
	var postit = '';
	for (var k = 0; k < thechilds.length; k++) {
		name = thechilds[k].getAttribute('name');
		type = thechilds[k].getAttribute('type');
		if (type == 'checkbox')
			if (thechilds[k].checked)
				checked = true;
			else
				checked = false;
		else
			checked = true;
		if (checked) {
			value = encodeURIComponent(thechilds[k].value);
			postit += name + '=' + value + '&';
		}
	}
	return postit;
}

function getFormData(id)
{
	var theform = document.getElementById(id);
	if (theform == null)
		return '';
	var postit1 = getFormDataByTag(theform, 'input');
	var postit2 = getFormDataByTag(theform, 'textarea');
	var postit3 = getFormDataByTag(theform, 'select');
	return postit1 + postit2 + postit3;
}

function loadXMLDocForm(url, id) {
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChange;
        req.open("POST", url, true);
	postit = getFormData(id);
	req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=utf-8');
	req.setRequestHeader('Referer', window.location);
        req.send(postit);
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        isIE = true;
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChange;
            req.open("POST", url, true);
	postit = getFormData(id);
	req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=utf-8');
	req.setRequestHeader('Referer', window.location);
        req.send(postit);
        }
    }
}

// handle onreadystatechange event of req object
function processReqChange() {
    // only if req shows "loaded"
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
            loadPage();
	    document.getElementById("loading").style.display = 'none';
	    document.getElementById("semitransparent").style.display = 'none';
         } else {
            alert("There was a problem connecting to the server:\n" +
                req.statusText);
         }
    } else if (req.readyState == 1) {
	document.getElementById("loading").style.display = 'block';
	document.getElementById("semitransparent").style.display = 'block';
    }
}

function loadPage() {
        response = req.responseText;
        if (req.responseXML == null) {
                alert('There was an error in the server response. Try again later or contact us.');
                document.getElementById("loading").style.display = 'none';
                document.getElementById("semitransparent").style.display = 'none';
                alert(response);
        }
        var items = req.responseXML.getElementsByTagName("module");
        for (var i = 0; i < items.length; i++) {
                div = document.getElementById("am_" + items[i].getAttribute('id'));
                if (div == null) {
                        continue;
                }
                var ajaxdivs = items[i].getElementsByTagName("div");
		var ajaxids = items[i].getElementsByTagName("element");
                var content = items[i].getElementsByTagName("content");
                var lala = content[0].childNodes;
                if (ajaxdivs.length == 0 && ajaxids.length == 0) {
                	div.innerHTML = "";
                	for (var k = 0; k < lala.length; k++) {
                        if (lala[k].nodeType == 4) {
                        	div.innerHTML = lala[k].nodeValue;
                        	var js = div.getElementsByTagName("script");
                        	for(var h = 0; h < js.length; h++) 
                        		if ( js[h].className == 'execonAjax' && js[h] != null )
                        			eval(js[h].innerHTML);
                        }
                	}
                } else {
			for (var k = 0; k < lala.length; k++) {
				if (lala[k].nodeType == 4) {
					var resContent = lala[k].nodeValue;
					var myElement = document.createElement('div');
					myElement.innerHTML = resContent;
				}
			}
			if(ajaxdivs.length != 0) {
                	for (var j = 0; j < ajaxdivs.length; j++) {
                		var ajaxdiv = ajaxdivs[j].getAttribute('class');
                		var innerdivs = div.getElementsByTagName("div");
                		for(var d = 0; d < innerdivs.length; d++) {
                			if(innerdivs[d].className == ajaxdiv) {
                				innerdivs[d].innerHTML = "";
                				var innerdiv = innerdivs[d];
                			} else
                				continue;	
             				var alldivs = myElement.getElementsByTagName('div');
                			for(var g = 0; g < alldivs.length; g++)  {
                				if(alldivs[g].className == ajaxdiv) {
                					innerdiv.innerHTML = alldivs[g].innerHTML;	
                					var js = innerdiv.getElementsByTagName("script");
                                        		for(var h = 0; h < js.length; h++) 
                                        			if (js[h].className != 'notAjax' && js[h] != null ) 
                                        				eval(js[h].innerHTML);
                				}
                			}
                		}
                	}
			}
			if(ajaxids.length != 0)	{
			for (var j = 0; j < ajaxids.length; j++) {
				var ajaxid = ajaxids[j].getAttribute('id');
				var ajaxtype = ajaxids[j].getAttribute('type');
				if(ajaxtype == 'tinymce') {
					var newIds = myElement.getElementsByTagName('textarea');
					for(x = 0; x < newIds.length; x++)
						if(newIds[x].id == ajaxid)
							tinyMCE.get(ajaxid).setContent(Encoder.htmlDecode(newIds[x].innerHTML));
				} else {
					var innerid = document.getElementById(ajaxid);
					var newIds = myElement.getElementsByTagName(ajaxtype);
					for(x = 0; x < newIds.length; x++) 
						if(newIds[x].id == ajaxid)
							innerid.innerHTML = newIds[x].innerHTML;
				}
			}
			}             	
		}
        }
        
        
    	
    	
	//var js = document.getElementsByTagName("script");
	//for(var i = 0; i < js.length; i++) 
        var error = req.responseXML.getElementsByTagName("error");
        for (var l = 0; l < error.length; l++) {
        if (error[l].hasChildNodes() && error[l].firstChild.nodeValue.length > 1)
                alert (error[l].firstChild.nodeValue);
        }
        var message = req.responseXML.getElementsByTagName("message");
        var thismessage = '';
        for (var l = 0; l < message.length; l++)
                if (message[l].hasChildNodes() && message[l].firstChild.nodeValue.length > 1)
                        thismessage += message[l].firstChild.nodeValue;
        div = document.getElementById("statusid");
        if (thismessage.length > 1) {
                if (div != null) {
                        div.innerHTML = "";
                        div.innerHTML = thismessage;
                        div.style.display = 'inline';
                        window.setTimeout('resetMsg()', 15000);
                } else
                        alert(thismessage);
        }
}

function resetMsg()
{
	div = document.getElementById("statusid");
	div.innerHTML = "";
	div.style.display = 'none';
}


