
//var debug_enabled = true;
var debug_object_props = true;
var debug_events = new Array();
function aop(AObject, Comments)
{
	var res = '';
	for (anItem in AObject)	{
		var avalue = AObject[anItem];
//		res = res + 'Prop=' + anItem + ', Value=' + avalue + '\n';
		var t = typeof(avalue);
		res = res + anItem + '(' + t + ') = ' + avalue + '\n';
	}
	res = 'Object props\n' + Comments + '\n' + res;
	if (debug_object_props) {
		da(res);
	}
	return res;
}

var offsetChar = '	';
if (navigator.userAgent.indexOf('MSIE') < 0) {
	offsetChar = '. . ';
}

function vardumpInt(obj, offset)
{
    var res = '';
	for (var aitem in obj)	{
//		da(aitem);
		var avalue = '';
		try {
			avalue = obj[aitem];
		} catch(x) {
			avalue = 'error "' + x + '"';
		}
		var t = typeof(avalue);
		if (t != 'function') {
			if (t == 'object') {
				if ((offset.length / offsetChar.length) < 10) {
					var dodump = false;
					try { 
						if (!(avalue.tagName)) {
							dodump = true;
						}
					} catch (x) {
					}
					if (dodump) {
						avalue = '{\n' + vardumpInt(avalue, offset + offsetChar) + '\n' + offset + '}';
					} else {
//						avalue = 'DHTML object';
					}
				} else {
					avalue = 'NOT SHOWN';
				}
			} else {
			}
			res += offset + aitem + ' (' + t + ') = ' + avalue + '\n'
		}
	}
	return res;
}

function vardump(AObject, Comments)
{
	var res = '';
	if (Comments) {
		res += 'Properties of "' + Comments + '"\n';
	
	}
	res += vardumpInt(AObject, '');
	if (debug_object_props) {
		da(res);
	}
	dl(res)
	return res;
}

function vd(AObject)
{
	var res = '';
	res += vardumpInt(AObject, '');
	return res;
}



//	sraka[piska][]=1&sraka[piska][]=2&sraka[piska2][r][]=3&sraka[piska2][r][]=4


function my_hash2query (content, varName) {
			if (!varName) { varName = ''; }
			var query = [];
			if (content instanceof Object) {
				// TODO: build more nested objects in PHP-style.
				for (var k in content) {
					if (content[k] instanceof Object) {
						query[query.length] = my_hash2query(content[k], varName + '[' + k + ']');
					} else {
						query[query.length] = varName + '[' + escape(k) + ']=' + escape(content[k]);
					}
				}
			} else {
				query = [content];
			}
			return query.join('&');
		}



function arrayToQuery(obj, varName)
{

	function arrayToQueryInt(obj, varName)
	{
		var res = '';
		for (var aitem in obj)	{
			var avalue = '';
			try { avalue = obj[aitem]; } catch(x) {	avalue = 'error "' + x + '"'; }
			var t = typeof(avalue);
			if (t != 'function') {
				var dodump = false;
				if (t == 'object') {
					try { if (!(avalue.tagName)) { dodump = true; }	} catch (x) {}
					if (dodump) {
						avalue = arrayToQueryInt(avalue, varName + '[' + aitem + ']');
					} else {
	//					avalue = 'DHTML object';
					}
				}
				if (res != '') {
					res += '&';
				}
				if (dodump) {
					res += avalue;
				} else {
					res += varName + '[' + aitem + ']=' + avalue;
				}
			}
		}
		return res;
	}

	function arrayToQueryInt2(obj, varName)
	{
		var res = '';
		for (var aitem in obj)	{
			var avalue = '';
			try { avalue = obj[aitem]; } catch(x) {	avalue = 'error "' + x + '"'; }
			var t = typeof(avalue);
			if (t != 'function') {
				var dodump = false;
				if (t == 'object') {
					try { if (!(avalue.tagName)) { dodump = true; }	} catch (x) {}
					if (dodump) {
						avalue = arrayToQueryInt(avalue, varName + '[' + aitem + ']');
					} else {
	//					avalue = 'DHTML object';
					}
				}
				if (res != '') {
					res += '&';
				}
				if (dodump) {
					res += avalue;
				} else {
					res += varName + '[' + aitem + ']=' + avalue;
				}
			}
		}
		return res;
	}

	var res = '';
	if (!varName || (varName == '')) {
		varName = 'data'
	}
	res += arrayToQueryInt(obj, varName);
	return res;
}


function dl(msg)
{
	if (debug_events.length > 1000) {
		debug_events.splice(0, 100);
	}
	debug_events.push(msg);
}

function da(msg)
{
	dl(msg);
	if (js_debug_enabled) {
		alert(msg);
	}
}

function getDebugEvents()
{
	return vd(debug_events, 'log');
}

function log(msg)
{
	dl(msg);
	if (!js_debug_enabled) { return; }
	var logElement = document.getElementById('id_log');
	if (!logElement) {
        logElement = document.body.appendChild(document.createElement("DIV"));
        logElement.id = 'id_log';
        logElement.style.backgroundColor = '#BBBBCC';
        logElement.appendChild(document.createTextNode('LOG'));
	}
	var ne = document.createElement("DIV"); // document.createTextNode(msg);
	ne.innerHTML = '' + msg;
//	ne.style.display = 'block';
	logElement.insertBefore(ne, logElement.childNodes[0]);
//	logElement.innerHTML += msg + "<br>";
}
