/*
fuction: printit
	funzione crossbrowser per la stampa della pagina
*/
function printit(){  
	if (window.print) {
	    window.print() ;  
	} else {
	    var WebBrowser = '<OBJECT ID="WebBrowser1" CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>';
	document.body.insertAdjacentHTML('beforeEnd', WebBrowser);
	    WebBrowser1.ExecWB(6, 2);//Use a 1 vs. a 2 for a prompting dialog box    WebBrowser1.outerHTML = "";  
	}
}

/*
fuction: alltrim
	toglie tutti gli spazi e i ritorni a capo prima e dopo un testo

Arguments:
	str - stringa da "trimmare"
*/
function alltrim(str) {
	return str.replace(/^\s+|\s+$/g, '');
}

/*
fuction: getApplicationURL
	ritorna la directory di installazione dell'applicazione
	es: www.hyperborea.com:9003/dbgaw/main.htm ritorna "dbgaw"
*/
function getApplicationURL() {
	var sitename=location.pathname;
	var words=sitename.split("/");
	//prendo words[1] perch� words[0] � vuoto essendo che il primo carattere � /
	return words[1];
}

/*
fuction: fillObject
	riempie un oggetto con il contenuto di una pagina web (richiede mootools)

Arguments:
	id - id dell'oggetto da riempire
	url - url della pagina da chiamare
*/
function fillObject(id, url) {
	new Ajax(url, {
		method: 'get',
		update: $(id)
	}).request();
}


/*
fuction: setHiddenPar
	setta nome e valore di un input hidden  
	
Arguments:
	id - id dell'input hidden 
	name - nome del parametro 
	value - valore del parametro 
*/
function setHiddenPar(id, name, value){
	$(id).setProperty('name', name);
	$(id).setProperty('value', value);
}

/*
fuction: toggleCheckbox
	fa toggle della seleziona  
	
Arguments:
	className - classe dei checkbox 
	checkValue - valore a cui settare il checkbox (opzionale)
*/
function toggleCheckbox(className, checkValue){ 
	var cbList = $$('.'+className);
	var chkd = checkValue || true;
	if (cbList[0].checked) chkd = false;
	for (var i = 0; i < cbList.length; i++) {
		cbList[i].checked = chkd;
	}
}

function getContextPath(protocol){

 context = window.location.pathname.substring(1,window.location.pathname.length);
 context = context.substring(0,context.indexOf('/'));
 contextpath=protocol+"://"+window.location.hostname+":"+window.location.port+"/"+context;
 
 return    contextpath;
 }




/*
function: createWindow
	crea una finestra modale 800x600 e la riempie con la risposta dell'url

Arguments:
	url - pagina da chiamare via ajax
	id - id dell'elemento HTML da riempire con la risposta ajax
	popupId - id della finestra che viene creata
	urlBack - url da chiamare dopo la submit
	w - larghezza della finestra (default: 800px)
	h - altezza della finestra (default: 600px)
*/
function createWindow(url, id, popupId, urlBack, w, h){
	w = w || 800;
	h = h || 600;
	
	
	
	// creo il div popup
	win = new Windoo({
			width: w,
			height: h,
			id: popupId,
			title: '',
			theme: Windoo.Themes.alphacube,
			shadow: false,
			destroyOnClose: true,
			container: false,
			buttons: {close: true, minimize: false, maximize: false},
			resizable: false,
			draggable: true,
			modal: true,
			ghost: {resize: false, move: false}
		})
	
	//creo il div (non visibile) che riempio con la chiamata ajax e lo inserisco nel dom appendendolo ad un div esistente
	//questo passaggio potrebbe essere più elegante, ma è importante che il mio div sia nel dom	
	var myDiv = '<div id="'+id+'" style="display:none;"></div>';
	$('divBuffer').setHTML(myDiv);
			 
	$(id).setHTML('');
 	
 	// Get our window size - subtract some pixels to make sure the browser never
    // adds scrollbars
    var winWidth = $(popupId).offsetWidth - 5;
    var winHeight = $(popupId).offsetHeight - 5;

    // Create our loading animated icon
    var loading = new Element('img',
    {
        'styles': {
          'position': 'absolute',	
       	  'left': (winWidth/2) - 16 + 'px',
       	  'top':  (winHeight/2) - 16 + 'px'
    	},
    	'id': 'loading',
    	'src': getContextPath('http')+'/resources/loading.gif'
    });
    loading.injectInside( $(id));		 
			 
	// chiamata ajax		 
	new Ajax(url, {
		data: {'urlBack': urlBack},
		method: 'get',
		evalScripts: true,
		update: id
	}).request();	
	
	//mostro il tutto
	$(id).setStyle('display', 'block');
	win.adopt($(id)).show();
	
	
}



/**
 * updateWindow
	* aggiorna una finestra esistente con un nuovo contenuto, 
	* si basa sul'id del div creato al momento della creazione della finestra
 * @param {string} url 
 * @param {string} id
 * @param {string} popupId - id della finestra che viene aggiornata
 */
 function updateWindow(url, id, popupId) {
 	$(id).setHTML('');
 	
 	// Get our window size - subtract some pixels to make sure the browser never
    // adds scrollbars
    var winWidth = $(popupId).offsetWidth - 5;
    var winHeight = $(popupId).offsetHeight - 5;

    // Create our loading animated icon
    var loading = new Element('img',
    {
        'styles': {
          'position': 'absolute',	
       	  'left': (winWidth/2) - 16 + 'px',
       	  'top':  (winHeight/2) - 16 + 'px'
    	},
    	'id': 'loading',
    	'src': getContextPath('http')+'/resources/loading.gif'
    });
    loading.injectInside( $(id));
    	
 	// chiamata ajax		  
	new Ajax(url, {
		method: 'get',
		evalScripts: true,
		update: id
	}).request();	
 }

/**
 * windooMostraPopup
	* mostra un popup adottando dell'html già presente nel dom 
 * @param {string} id - id del div da adottare
 * @param {string} w - larghezza del popup
 * @param {string} h - altezza del popup
 * @param {bool} mdl - true (default) per avere la finesta modale, false altrimenti
 * @param {string} ttl - titolo della finestra
 */
 function windooMostraPopup(id, w, h, mdl, ttl) {
	var w = w || 500;
	var h = h || 200;
	if (mdl==null) mdl = true;

	// creo il div popup
	win = new Windoo({
			width: w,
			height: h,
			id: 'win_'+id,
			title: ttl,
			theme: Windoo.Themes.alphacube,
			shadow: false,
			destroyOnClose: false,
			container: false,
			buttons: {close: true, minimize: false, maximize: false},
			resizable: false,
			draggable: true,
			modal: mdl,
			ghost: {resize: false, move: false}
		});
			
	//mostro il tutto
	$(id).setStyle('display', 'block');
	win.adopt($(id)).show();
 }

