//Valorizza il contenuto dell'elemento 'col_main' della pagina con il codice html indicato
var utilDebug = false;

function htmlScript(htmlCode, divID) {	
	if (utilDebug) log('[' + divID + '] incoming HTML = \n' + htmlCode);
	
	if (!htmlCode) {
		if (utilDebug) log('incoming HTML for div [' + divID + '] is null', ERROR);
		return;
	}

	var layer = document.getElementById(divID);
	
	if (!layer) {
		if (utilDebug) {
			log("layer " + divID + " non trovato", WARNING);
		}
		return;
	}
	
	var extractor 	= /(<script[^>]*>)([\s\S]+?)(<\/script>)/g;
	var cleaner		= /<script[^>]*>|<\/script>/g;

	var HTML = htmlCode.replace(extractor, '');
	
	if (utilDebug) log('extracted HTML = \n' + HTML);
	
	layer.innerHTML = HTML;
	
	var scripts = htmlCode.match(extractor);
	
	if (scripts) {
		for (i = 0; i < scripts.length; i++) {
			var script = scripts[i].replace(cleaner, '');
		
			if (utilDebug) log('extracted script[' + i + '] = \n' + script);
			
			eval(script);
		}
	}
}

function cleanID(id) {
	return id.replace(/_(.*)/, '');
}

var timeouts = new Object();
function setRefresh(box, delay) {
	if (utilDebug) log('setRefresh(' + box + ', ' + delay + ')');
	
	var dwrID = cleanID(box);
	
	if (utilDebug) log('setting timeout on ' + dwrID + ' to layer ' + box);
	
	if (timeouts[box]) {
		clearTimeout(timeouts[box]);
		timeouts[box] = undefined;
	}
	
	timeouts[box] = setTimeout(dwrID + ".refresh('" + box + "', function(htmlCode) { htmlScript(htmlCode, '" + box + "'); } )", delay * 1000);
}

function parseNumber(bcsID) {
	return parseInt(bcsID.match( /\d+/ )[0].replace(/^0+/, ''));
}

function apriPopup(url, popupName, w, h) {
	var newPopup = window.open(url, popupName, config='height=' + h + ',width=' + w + ', toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, directories=no, status=no');
    if (newPopup) {
    	newPopup.opener = this;
    	newPopup.focus();
    }
}

function apriPopupScrollbars(url, popupName, w, h) {
	var newPopup = window.open(url, popupName, config='height=' + h + ',width=' + w + ', toolbar=no, menubar=no, scrollbars=yes, resizable=no,location=no, directories=no, status=no');
    if (newPopup) {
    	newPopup.opener = this;
    	newPopup.focus();
    }
}

function addOnLoadEvent(theEvent) {
	if (window.attachEvent) {
		window.attachEvent(
			'onload',
			theEvent
		);
	} else if (window.addEventListener) {
		window.addEventListener(
			'load', 
			theEvent,
			false
		);
	}
}

function addOnMouseDownEvent(obj, theEvent) {
	if (!obj) {
		return;
	}
	
	if (obj.attachEvent) {
		obj.attachEvent(
			'onmousedown',
			theEvent
		);
	} else if (obj.addEventListener) {
		obj.addEventListener(
			'mousedown', 
			theEvent,
			false
		);
	}
}

function addOnFocusEvent(obj, theEvent) {
	if (!obj) {
		return;
	}

	if (obj.attachEvent) {
		obj.attachEvent(
			'onfocus',
			theEvent
		);
	} else if (obj.addEventListener) {
		obj.addEventListener(
			'focus', 
			theEvent,
			false
		);
	}
}