/*****************************************************************************
  STEFANO MENGARELLI - MICRORUN PHPXL JAVASCRIPT STANDARD LIBRARY
  Copyright (c) 2008 Stefano Mengarelli
  ALL RIGHT RESERVED
  File:         LIB.JS
  Versione:     1.0.0 Aprile 2008
  Autore:       Stefano Mengarelli
  E-mail:       info@liberalogica.com
 *****************************************************************************/

//<smWebCode>source</smWebCode> (C) YYYY by Stefano Mengarelli. All Rights Reserved.
 
/*****************************************************************************
  INIZIALIZZAZIONE VARIABILI E DOCUMENTO 
 *****************************************************************************/

var sm_xmlhttp = false;
var sm_ie4=document.all;
var sm_ns6=document.getElementById&&!document.all;
var sm_cronoidcounter=0;
var sm_cronolastmilliseconds="";
var sm_ajaxreturn="";

/*****************************************************************************
  STANDARD FUNCTIONS
 *****************************************************************************/

function sm_BootLib()
/* Esegue l'inizializzazione dell'ambiente. */
{
  if ((document.referrer != '')&&(document.MODULO.SM_REF<' ')) document.MODULO.SM_REF=document.referrer;
  if (document.MODULO.SM_LNG<' ') document.MODULO.SM_LNG=sm_BrowserLanguage();
  return void(0);
}

function sm_BrowserLanguage() 
/* Ritorna il codice della lingua del browser client. (p.es. "it" oppure "en") */
{
  var L=navigator.language? navigator.language : navigator.userLanguage;
  return L.toLowerCase().substr(0,2);
}

function sm_CallForm(sURL)
/* Invia il modulo corrente, richiamando la pagina con nome sURL */
{
  if (sURL.indexOf(".")>0) { document.MODULO.action=sURL; }
  else { document.MODULO.action=sURL+sm_DefaultPageExt(); }
  if (document.MODULO.SM_CRONO) { document.MODULO.SM_CRONO.value=sm_CronoID(); }
  document.MODULO.SM_BACK.value="";
  document.MODULO.submit();
  return void(0);
}

function sm_CallFormPage(nPAG)
/* Invia il modulo corrente, richiamando la pagina corrente e passandole il parametro di pagina nPAG */
{
  document.MODULO.action=document.MODULO.SM_SELF.value;
  document.MODULO.SM_PAG.value=nPAG;
  if (document.MODULO.SM_CRONO) { document.MODULO.SM_CRONO.value=sm_CronoID(); }
  document.MODULO.SM_BACK.value="";
  document.MODULO.submit();
  return void(0);
}

function sm_CallFormBack(sURL,sBACK)
/* Invia il modulo corrente, richiamando la pagina con nome sURL
   ed impostando come pagina di ritorno sBACK */
{
  if (sURL.indexOf(".")>0) { document.MODULO.action=sURL; }
  else { document.MODULO.action=sURL+sm_DefaultPageExt(); }
  if (sBACK>" ") { document.MODULO.SM_BACK.value=sBACK; }
  else { document.MODULO.SM_BACK.value=document.MODULO.SM_SELF.value; }
  if (document.MODULO.SM_CRONO) { document.MODULO.SM_CRONO.value=sm_CronoID(); }
  document.MODULO.submit();
  return void(0);
}

function sm_CheckEmailAddress(S)
/* Ritorna true se l'indirizzo e-mail contenuto in S è verosimile. */
{
	var b=false;
	if (!sm_StrEmpty(S)) {  		
		var l=S.length;
		if (l>4) 
		{
			var i=S.indexOf("@");
			if ((i>0)&&(i<l-3))
			{
				i=S.indexOf(".");
				if ((i>2)&&(i<l-1)) { b=true; }
			}
		}
	}
	return b;
}

function sm_CloseSession()
/* Azzera i parametri di sessione e ricarica la pagina. */
{
  document.MODULO.SM_UID.value="";
  document.MODULO.SM_SID.value="";
  document.MODULO.action="home.php";
  if (document.MODULO.SM_CRONO) { document.MODULO.SM_CRONO.value=sm_CronoID(); }
  document.MODULO.submit();
  return void(0);
}

function sm_ComboSelected(oSEL)
/* Ritorna il valore dell'elemento selezionato nell'oggetto di tipo combo oSEL */ 
{
  return oSEL.options[oSEL.selectedIndex].value;
}

function sm_ConfirmDlg(sMSG)
/* Visualizza la finestra di dialogo con la richiesta di conferma al messaggio sMSG e ritorna 
   True se l'utente risponde affermativamente oppure False in caso contrario. */ 
{
  return confirm(sMSG);
}

function sm_CookieErase(cookieName)
{
	sm_CookieWrite(cookieName,"",-1);
}

function sm_CookieRead(cookieName)
{
	var cookieID = cookieName + "=";
	var cookieArray = document.cookie.split(';');
	var i,c;
	for(i=0; i < cookieArray.length; i++) {
		c = cookieArray[i];
		while (c.charAt(0)==' ') { c = c.substring(1,c.length); }
		if (c.indexOf(cookieID) == 0) 
		{
			return c.substring(cookieID.length,c.length);
		}
	}
	return "";
}

function sm_CookieWrite(cookieName,cookieValue,expirationDays)
{
	if (expirationDays) {
		var todaysDate = new Date();
		todaysDate.setTime(todaysDate.getTime()+(expirationDays*24*60*60*1000));
		var expires = "; expires="+todaysDate.toGMTString();
	}
	else 
	{ 
		var expires = "";
    }
	document.cookie = cookieName+"="+cookieValue+expires+"; path=/";
}

function sm_Copy(S,I,L)
/* Ritorna la porzione di stringa che inizia dalla posizione I e lunga L. */
{
  return S.substr(I-1,L);
}

function sm_CronoID()
/* Ritorna una stringa lunga 20 caratteri nel formato YYYYMMDDHHNNSSZZZCCC
   rappresentante un codice identificativo cronologico. */
{
  var D = new Date(); 
  var S,R;
  S=""+D.getFullYear(); while (S.length<4) { S='0'+S; } R=S;
  S=""+D.getMonth(); while (S.length<2) { S='0'+S; } R=R+S;
  S=""+D.getDay(); while (S.length<2) { S='0'+S; } R=R+S;
  S=""+D.getHours(); while (S.length<2) { S='0'+S; } R=R+S;
  S=""+D.getMinutes(); while (S.length<2) { S='0'+S; } R=R+S;
  S=""+D.getSeconds(); while (S.length<2) { S='0'+S; } R=R+S;
  S=""+D.getMilliseconds(); while (S.length<3) { S='0'+S; } S=S.substr(0,3); R=R+S;
  if (S==sm_cronolastmilliseconds) { sm_cronoidcounter=sm_cronoidcounter+1; } 
  else { sm_cronoidcounter=0; sm_cronolastmilliseconds=S; }
  S=""+sm_cronoidcounter; while (S.length<3) { S='0'+S; } R=R+S.substr(0,3);
  return R;
}

function sm_DefaultPageExt() 
/* Ritorna una stringa comprendente l'estensione di default 
   delle pagine sviluppate, preceduta dal punto. */
{
  return ".php";
}

function sm_DetectVersion() 
/* Ritorna la versione del browser utilizzato. */
{ 
  return parseInt(navigator.appVersion); 
}  

function sm_DetectOS() 
/* Ritorna la stringa indicate se il sistema operativo su cui si 
   sta visualizzando le pagine è "Windows" oppure "Macintosh". */
{ 
  if (navigator.userAgent.indexOf('Win') == -1) { OS = 'Macintosh'; } 
  else { OS = 'Windows'; } 
  return OS; 
}

function sm_EmailPage() 
/* Invia una e-mail con la segnalazione della pagina corrente. */
{
  document.location="mailto:?BODY=Segnalazione url: "+document.location;
}

function sm_GoBack()
/* Torna alla pagina (MODULO) precedentemente visualizzata. */
{
  if (!document.MODULO.SM_BACK) { history.back(); }
  else if (sm_StrEmpty(document.MODULO.SM_BACK.value)) { history.back(); }
  else { sm_CallForm(document.MODULO.SM_BACK.value); }
}

function sm_HomeLanguage(mainURL)
/* Reindirizza la pagina alla home.php relativa alla lingua del browser. */
{
	var L=sm_BrowserLanguage();
	if ((L=='en')||(L=='us')||(L=='uk')) { L='_uk'; } else { L=''; }
	sm_RedirectPage(mainURL+'home'+L+sm_DefaultPageExt());
}

function sm_InputEmpty(obj)
/* Ritorna true se il campo modulo obj è nullo o contiene solo spazi. */
{
	if (obj) { 
	    if (obj.value) { return sm_StrEmpty(""+obj.value); }
		else { return true; }
    } else { return true; }
}

function sm_InputValue(obj)
/* Ritorna il valore sotto forma di stringa del campo modulo obj. */
{
	if (obj) {  
		if (obj.value) { return ""+obj.value; }
		else { return ""; }
    } else { return ""; }
}

function sm_IsLogged()
/* Ritorna True se l'utente web ha effettuato correttamente il Login. */
{
  if (document.MODULO.SM_UID.value>"0") { return true; }
  else { return false; }
}

function sm_Left(S,L)
/* Ritorna i L caratteri più a sinistra della stringa S. */
{
	return S.substr(0,L);
}

function sm_Length(S)
/* Ritorna la lunghezza della stringa S. */
{
  return S.length;
}

function sm_LowerCase(S)
/* Ritorna la stringa S convertita in minuscolo. */
{
  return S.toLowerCase();
}

function sm_Navigate(sURL) 
/* Richiama la pagina indicata dall'url sURL 
   aggiungendone l'estensione di default. */
{
  sm_RedirectPage(sURL+sm_DefaultPageExt());
}

function sm_OpenSession()
/* Esegue il login dalla home page. */
{
  document.MODULO.action="home.php";
  if (document.MODULO.SM_CRONO) { document.MODULO.SM_CRONO.value=sm_CronoID(); }
  document.MODULO.submit();
}

function sm_PageName()
/* Ritorna il nome del file relativo alla pagina corrente, completo di estensione. */
{
	var sPath = window.location.pathname;
	var i=sPath.lastIndexOf('\\'); if (i<0) { i=sPath.lastIndexOf('/'); }
	return sPath.substring(i+1);
}

function sm_PopUp(URL,TGT,W,H,X,Y) 
/* Apre una finestra di popup con il documento URL, di larghezza W, altezza H e posizione X,Y. */
{
  var w=window.open(URL,TGT,"resizable=1,scrollbars=1,toolbar=0,menubar=0,status=0,height="+H+",width="+W+",left="+X+",top="+Y);
  w.focus();
  return w;
}

function sm_Pos(SubStr,S)
/* Ritorna la posizione della sottostringa SubStr all'interno di S. */
{
  return S.indexOf(SubStr)+1;
}

function sm_PrintPage() 
/* Stampa la pagina corrente. */
{
  window.print();
}

function sm_RedirectPage(sURL)
/* Redireziona la pagina corrente all'url: sURL. */
{
  if (window.location.replace) { window.location.replace(sURL); }
  else { window.location=sURL; }
}

function sm_Right(S,L)
/* Ritorna i L caratteri più a destra della stringa S. */
{		 				           
	return S.substr(S.length-L,L); 
}

function sm_RollOver(obj) 
/* Esegue il rollover sull'immagine obj cambiandola da xxxxx_normal.gif (o da xxxxx_selected.gif) a xxxxx_hover.gif  */
{
  obj.src=sm_StrTran(sm_StrTran(obj.src,"_normal.gif","_hover.gif"),"_selected.gif","_hover.gif");
} 

function sm_RollOut(obj) 
/* Esegue il rollout sull'immagine obj cambiandola da xxxxx_hover.gif a xxxxx_normal.gif  */
{
  obj.src=sm_StrTran(obj.src,"_hover.gif","_normal.gif");
} 

function sm_RollOutSelected(obj) 
/* Esegue il rollout sull'immagine obj cambiandola da xxxxx_hover.gif a xxxxx_selected.gif  */
{
  obj.src=sm_StrTran(obj.src,"_hover.gif","_selected.gif");
}

function sm_StrBtw(S,S1,S2)
/* Ritorna la porzione di stringa di $S compresa tra le sottostringhe $S1 e $S2. */
{
	var R="";
	var I=sm_Pos(S1,S);
	if (I>0) {
		var SB=sm_Copy(S,I+S1.length,S.length);
		I=sm_Pos(S2,SB); 
		if (I>0) { R=sm_Copy(SB,1,I-1); }
	}
	return R;
}
			   
function sm_StrEmpty(S)
/* Ritorna True se la stringa S è vuota oppure se costituita da soli spazi. */
{
  if (sm_Length(sm_Trim(S))<1) { return true; } else { return false; }
}

function sm_StrTran(S,oldS,newS)
/* Ritorna la stringa ottenuta sostituendo le sottostringhe oldS con le sottostringhe newS. */
{
  return S.replace(oldS,newS);
}

function sm_Trim(S)
/* Ritorna una stringa senza eventuali spazi in testa o in coda. */
{
	var R=S;
	while ((R.length>0)&&(R.charAt(0)==" ")) { R=R.substr(1); }
	while ((R.length>0)&&(R.charAt(R.length-1)==" ")) { R=R.substr(0,R.length-1); }
	return R;
}

function sm_UpperCase(S)
/* Ritorna la stringa S convertita in maiuscolo. */
{
	return S.toUpperCase();
}

function sm_Wait(mSec) 
/* Produce una pausa di elaborazione (attesa) di mSec millisecondi. */
{
	var D = new Date();
	var cD = null;
	do { cD = new Date(); } 
	while(cD-D < mSec);
} 

/*****************************************************************************
  AJAX FUNCTIONS
 *****************************************************************************/

function sm_AjaxGetURL(URL,ID) 
/* Esegue una chiamata AJAX alla URL specificata e reindirizzando il contenuto html
   della risposta nell'innerHTML dell'oggetto ID (se specificato) . */
{
  sm_ajaxreturn="";
  try 
  {
    sm_xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  } 
  catch(e) 
  {
    try 
	{
      sm_xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
	catch(e)
	{
      sm_xmlhttp = false;
    }
  }
  if ((!sm_xmlhttp) && (typeof XMLHttpRequest != 'undefined')) 
  {
    sm_xmlhttp = new XMLHttpRequest();
  }
  if (sm_xmlhttp) 
  {
    sm_xmlhttp.open('GET', ''+URL); 
    sm_xmlhttp.onreadystatechange = function () {
      if (sm_xmlhttp.readyState == 4) { 
	    if (ID>'') 
		{
	      document.getElementById(ID).innerHTML = sm_xmlhttp.responseText; 
		} 
		else
		{
		  sm_ajaxreturn=sm_xmlhttp.responseText;
		}
	  }
    }
    sm_xmlhttp.send(null);
    return true;
  } 
  else
  { 
    return false; 
  }
}
 

