// inizio preload immagini layout
var myimages=new Array()
function preload(){
	$$('html')[0].style.visibility='hidden';
  var tot = preload.arguments.length;
  for (i=0;i<tot;i++){
    myimages[i] = new Image();
		if (i == tot-1) myimages[i].onload = $$('html')[0].style.visibility='visible';
		myimages[i].src = preload.arguments[i];
	}
}
//preload("images/slide_example/1.jpg",
//        "images/slide_example/1.jpg");
// fine preload immagini layout





// effetto animazione
function effect(obj,speed) {
    if (speed==null) speed = 500;
    if (typeof(obj)=='object') {
        return new Fx.Tween(obj, { duration:speed, transition: Fx.Transitions.Sine.easeInOut });
    }
}




// stessa funzione del PHP
function str_replace (search, replace, subject, count) {
  f = [].concat(search),
  r = [].concat(replace),
  s = subject,
  ra = r instanceof Array, sa = s instanceof Array;    s = [].concat(s);
  if (count) {
    this.window[count] = 0;
  }
  for (i=0, sl=s.length; i < sl; i++) {
    if (s[i] === '') {
      continue;
    }
    for (j=0, fl=f.length; j < fl; j++) {
      temp = s[i]+'';
      repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0];
      s[i] = (temp).split(f[j]).join(repl);
      if (count && s[i] !== temp) {
        this.window[count] += (temp.length-s[i].length)/f[j].length;
      }
    }
  }
  return sa ? s : s[0];
}




// document.getElementsByName --> funziona solo con i form!!!
function getByName(name,numero) {
  n = (numero) ? numero : 0;
  return document.getElementsByName(name)[n];
}




// controllo form : elementi ("id" oppure "name") separati da "|"
function controlla_form(elementi) {
  var msg = "";
  var count = 0;
  var split = elementi.split("|");
  for (var key = 0; key < split.length; key++) {
    var name = split[key];
    var getElement = $(name);
    var obj = (getElement) ? $(name) : getByName(name);
    if (obj.value=='') {
      count++;
      msg += "- " + name + "\n";
      obj.setStyle("border-color",'#FF3300');
    } else {
      obj.setStyle("border-color",'');
    }
  }
  if(count>0) {
    msg = str_replace("_"," ",msg);
    alert("Attenzione, devi compilare " + count + " campi:\n" + msg);
    return( false );
  }
}




// controllo password registrazione
function comparaPswd(id1,id2) {
  var pasw1 = $(id1).value;
  var pasw2 = $(id2).value;
  var paswl = parseInt(pasw1.length);
  if (paswl < 6) {
    $(id1).setStyle("border-color",'#FF3300');
    alert( "Errore: la password deve contenere almeno 6 caratteri!" );
    return false;
  }
  if ( pasw1 != pasw2 ) {
    $(id2).setStyle("border-color",'#FF3300');
    alert( "Errore: la password non è stata confermata correttamente!" );
    return true;
  }
}




// imposta i cookie utilizzando mootools e se viene definito fa il redirect
function mooCookie(nome,valore,giorni,redirect) {
  if ( Cookie.write(nome,valore, {duration:giorni}) ) {
    if (redirect) top.location.href = redirect;
    return true;
  }
}




// redirect
function goto(pag,t) {
  if (!t) t = this;
  t.location.href=pag;
}




// funzione per stampare
function printIt(printThis)
{
  var win = window.open();
  self.focus();
  win.document.open();
  win.document.write('<'+'html'+'><'+'head'+'><'+'style'+'>');
  win.document.write('body, td { font-family: Verdana; font-size: 11px;}');
  win.document.write('<'+'/'+'style'+'><'+'/'+'head'+'><'+'body'+'>');
  win.document.write(printThis);
  win.document.write('<'+'/'+'body'+'><'+'/'+'html'+'>');
  win.document.close();
  win.print();
  win.close();
}
