/********************************************************************************************
 *
 * base.js
 *
 * funzioni javascript di base 
 *
 * version: 1.0 2006-09-25
 * author:  Massimo Viti <mviti@lamma.rete.toscana.it>, IBIMET Firenze Italy
 *
 ********************************************************************************************/

// rende il nome della directory in cui e' inserita la pagina html in corso
function getBaseDirname()
{
	var lpathname = window.location.pathname;
	var sz = lpathname.lastIndexOf('/');
	// controlla se l'ultimo carattere e' una barra '/'
	if (sz == (lpathname.length - 1))
	{
		lpathname = lpathname.substr(0, sz);
		sz = lpathname.lastIndexOf('/');
	}
	
	var dirname = lpathname.substr(0, sz); //window.location.pathname.substr(0, sz);
	return dirname;
}



// rende l'url di base: http://<nome host>/<baseDirname>
function getBaseURL()
{
	return window.location.protocol + "//" + window.location.hostname + getBaseDirname();
}

// rende il path dei file ajax secondo l'indirizzo http
// es: http://<nome host>/<baseDirname>/../ajax
function getAjaxURL()
{
 	return  getBaseURL() + "/../ajax";
 }


// abbandona la pagina corrente sostituendola con quella indicata
function abortPage(where)
{
	location.replace(where);
}

// rende ilnome del file senza il path
function basename (path) { return path.replace( /.*\//, "" ); }





/*************************************************************************
 *
 * funzioni di libreria generica
 *
 *************************************************************************/

// rende true se il parametro passato e' un array
function isArray(object)
{
	if (!window.Array || object == null)
		return false;
	return object.constructor == window.Array;
}




/*************************************************************************
 * rende un sottoinsieme dell'array d'origine
 *
 * subarray(srcArray, begin, [end])
 *
 * srcArray: array d'origine
 * begin: indice, base zero, dell'elemento, incluso, da cui iniziare l'estrazione
 * [end]: indice, base zero, dell'elemento, non incluso, a cui terminare l'estrazione
 * 
 * end non specificato: si estraggono tutti gli elementi dall'indice begin
 * begin maggiore del numero di elementi: otterremo un array con nessun elemento
 *
 *************************************************************************/
function subarray(srcArray, begin)
{
	var trgArray = new Array;
	
	var end = (arguments.length > 2) ? arguments[2] : srcArray.length + 1;
	for (var i = begin; i < srcArray.length && i < end; i++)
	{
		if (isArray(srcArray[i]))
			trgArray = trgArray.concat(srcArray[i])
		else
			trgArray.push(srcArray[i]);
	}
	
	return trgArray;
}


// apre una nuova finestra e ci scrive dentro 
//writeConsole('Hello from JavaScript!');
function writeConsole(content) {
 top.consoleRef=window.open('','myconsole',
  'width=350,height=250'
   +',menubar=0'
   +',toolbar=1'
   +',status=0'
   +',scrollbars=1'
   +',resizable=1')
 top.consoleRef.document.writeln(
  '<html><head><title>Console</title></head>'
   +'<body bgcolor=white onLoad="self.focus()">'
   +content
   +'</body></html>'
 )
 top.consoleRef.document.close()
}


/*******************************************************
ForEach: http://www.ejball.com/EdAtWork/CommentView,guid,c2d0f53f-afc7-4831-b60e-f4354ae3f7fa.aspx

Let me explain:

    * Calculating the array length outside the loop can be slightly faster.
    * I pass the item index as the second argument to the called function, since that can often be useful. (The called function doesn’t need to declare it as an explicit argument if it doesn’t need it.)
    * If the called function returns anything but undefined, the loop is terminated. This allows us to simulate what would be a break statement in a normal for loop. The ForEach method returns the value returned by the called function.
    * If the called function returns undefined, the loop continues. (A function returns undefined if it gets to the end without hitting a return statement, if it hits a return statement without an expression, or if it hits a return undefined (naturally).) An explicit return statement without an expression thus simulates what would be a continue statement in a normal for loop.
    * Using the call method allows us to specify this for the called function, which can be useful when calling ForEach from an object method.
    * The objThis argument is optional, but I make sure that it is never undefined, because under Internet Explorer, the call method is slightly faster when the first argument is a valid object (passing undefined forces it to determine the “global” object itself).

*********************************************************/
function ForEach(array, fn, objThis)
{
  objThis = objThis || this;
  var len = array.length;
  for (var n = 0; n < len; n++)
  {
    var r = fn.call(objThis, array[n], n);
    if (r !== undefined)
      return r;
  }
}
