// AJAX controllers
function createRequest() {
	try {
		request = new XMLHttpRequest();
	} catch ( tryMS ) {
		try {
			request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch ( otherMS ) {
			try {
				request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch ( failed ) {
				request = null;
			}
		}
	}

	return request;
}

function getActivatedObject(e) {
	var obj;

	if ( !e ) {
		// early version of IE
		obj = window.event.srcElement;
	} else if ( e.srcElement ) {
		// IE 7 or later
		obj = e.srcElement;
	} else {
		// DOM Level 2 browser
		obj = e.target;
	}

	return obj;
}

function addEventHandler( obj, eventName, handler ) {
	if ( document.attachEvent ) {
		obj.attachEvent( "on" + eventName, handler );
	} else if ( document.addEventListener ) {
		obj.addEventListener( eventName, handler, false );
	}
}

// Debug
function inspect( obj, maxLevels, level ) {
	var str = '', type, msg;

	// Start Input Validations
	// Don't touch, we start iterating at level zero
	if( level == null ) level = 0;

	// At least you want to show the first level
	if( maxLevels == null ) maxLevels = 1;

	if( maxLevels < 1 )
		return '<font color="red">Error: Levels number must be > 0</font>';

	// We start with a non null object
	if( obj == null )
		return '<font color="red">Error: Object <b>NULL</b></font>';
	// End Input Validations

	// Each Iteration must be indented
	str += '<ul>';

	// Start iterations for all objects in obj
	for( property in obj ) {
		try {
			// Show "property" and "type property"
			type =  typeof( obj[property] );

			str += '<li>(' + type + ') ' + property + ( ( obj[property]==null )?(': <b>null</b>'):('')) + '</li>';

			// We keep iterating if this property is an Object, non null
			// and we are inside the required number of levels
			if( ( type == 'object' ) && ( obj[property] != null ) && ( level+1 < maxLevels ) )
				str += inspect( obj[property], maxLevels, level+1 );
		} catch( err ) {
			// Is there some properties in obj we can't access? Print it red.
			if( typeof( err ) == 'string' ) msg = err;
			else if( err.message )          msg = err.message;
			else if( err.description )      msg = err.description;
			else                            msg = 'Unknown';

			str += '<li><font color="red">(Error) ' + property + ': ' + msg +'</font></li>';
		}
	}

	// Close indent
	str += '</ul>';

	return str;
}

function encode_entities( s ) {
	var result = '';

	for ( var i = 0; i < s.length; i++ ) {
		var c = s.charAt( i );

		result += {'<':'&lt;', '>':'&gt;', '&':'&amp;', '"':'&quot;'}[c] || c;
	}

	return result;
}

/*
function replaces extended characters of 'text' with its character entities.
call the function by default with output = false. if you switch it to true
it will format the source code for output in a textarea.
*/
function replaceExtChars( text, output ) {
	text = text.replace(eval('/&/g'), '&amp;');

	fromTo = new Array(
		'&Ecirc;',		'Ê', 
		'&AElig;',		'Æ', 
		'&Aacute;',		'Á', 
		'&Acirc;',		'Â', 
		'&Agrave;',		'À', 
		'&Aring;',		'Å', 
		'&Atilde;',		'Ã', 
		'&Auml; ',		'Ä', 
		'&Ccedil;',		'Ç', 
		'&ETH;',		'Ð', 
		'&Eacute;',		'É', 
		'&Egrave;',		'È', 
		'&Euml;',		'Ë', 
		'&Iacute;',		'Í', 
		'&Icirc;',		'Î', 
		'&Igrave;',		'Ì', 
		'&Iuml;',		'Ï', 
		'&Ntilde;',		'Ñ', 
		'&Oacute;',		'Ó', 
		'&Ocirc;',		'Ô', 
		'&Ograve; ',	'Ò', 
		'&Oslash;',		'Ø', 
		'&Otilde;',		'Õ', 
		'&Ouml;',		'Ö', 
		'&THORN;',		'Þ', 
		'&Uacute;',		'Ú', 
		'&Ucirc;',		'Û', 
		'&Ugrave ;',	'Ù', 
		'&Uuml;',		'Ü', 
		'&Yacute;',		'Ý', 
		'&aacute;',		'á', 
		'&acirc;',		'â', 
		'&aelig;',		'æ', 
		'&agrave;',		'à', 
		'&aring ;',		'å', 
		'&atilde;',		'ã', 
		'&auml;',		'ä', 
		'&brvbar;',		'¦', 
		'&ccedil;',		'ç', 
		'&cent;',		'¢', 
		'&copy;',		'©', 
		'&deg;',		'°', 
		'&e th;',		'ð', 
		'&eacute;',		'é', 
		'&ecirc;',		'ê', 
		'&egrave;',		'è', 
		'&euml;',		'ë', 
		'&frac12;',		'½', 
		'&frac14;',		'¼ ', 
		'&frac34;',		'¾', 
		'&gt;',			'>', 
		'&iacute;',		' í', 
		'&icirc;',		'î', 
		'&iexcl;',		'¡', 
		'&igrave;',		'ì', 
		'&iq uest;',	'¿', 
		'&iuml;',		'ï', 
		'&laquo;',		'«', 
		'&lt;',			'<', 
		'&mdash;',		'—', 
		'&micro;',		'µ', 
		'&middot;',		'· ', 
		'&ndash;',		'–', 
		'&not;',		'¬', 
		'&ntilde;',		'ñ', 
		'&oacut e;',	'ó', 
		'&ocirc;',		'ô', 
		'&ograve;',		'ò', 
		'&oslash;',		'ø', 
		'&otilde;',		'õ', 
		'&ouml;',		'ö', 
		'&para;',		'¶', 
		'&plusmn;',		'±', 
		'&pound;',		'£', 
		'&quot;',		'\"', 
		'&r aquo;',		'»', 
		'&reg;',		'®', 
		'&sect;',		'§', 
		'&sup1 ;',		'¹', 
		'&sup2;',		'²', 
		'&sup3;',		'³', 
		'&szlig;',		'ß', 
		'&t horn;',		'þ', 
		'&tilde;',		'˜', 
		'&trade;',		'™', 
		'&uacute;',		'ú', 
		'&ucirc;',		'û', 
		'&ugrave;',		'ù', 
		'&uuml;',		'ü', 
		'&ya cute;',	'ý', 
		'&yen;',		'¥', 
		'&yuml;',		'ÿ');

	if (output) {
		fromTo[fromTo.length] = '&amp;';
		fromTo[fromTo.length] = '&';
	}

	for (i=0; i < fromTo.length; i=i+2)
		text = text.replace(eval('/'+fromTo[i+1]+'/g'), fromTo[i]);

	return (text);
}

function ObjectPosition( obj ) {
	var curleft = 0;
	var curtop = obj.offsetHeight;

	if ( obj.offsetParent ) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while ( obj = obj.offsetParent );
	}

	return [curleft,curtop];
}


