//  DATA CREAZIONE: 02-08-2006;
//  DATA MODIFICA: 06-01-2007;
//  VERSIONE: 2006.1.0;
//  AUTORE: Dott. Cristian Lanza;
//          rezalan@tiscali.it
//  LICENSE:

var is;

function init() {
  is = new browserCheck();
  return true;
}

function browserCheck() {
  this.ns4 = (document.layers);
  this.ns6 = (!document.all && document.getElementById);
  this.ie4 = (document.all && !document.getElementById);
  this.ie5 = (document.all && document.getElementById);
  return true;
}

function fullShowObject(obj) {
  if (is === null) {
    return true;
  }
  if (is.ns4) {
    this.layer = window.document[obj];
    this.layer.visibility = "show";
    this.layer.display = "";
    this.layer.speak = "";
  }
  if (is.ie4) {
    this.element = window.document.all[obj];
    this.style = this.element.style;
    this.style.visibility = "visible";
    this.style.display = "";
    this.style.speak = "";
  }
  if (is.ns6 || is.ie5) {
    this.element = document.getElementById(obj);
    this.style = this.element.style;
    this.style.visibility = "visible";
    this.style.display = "";
    this.style.speak = "";
  }
  return true;
}

function fullHideObject(obj) {
  if (is === null) {
    return true;
  }
  if (is.ns4) {
    this.layer = window.document[obj];
    this.layer.visibility = "hide";
    this.layer.display = "none";
    this.layer.speak = "none";
  }
  if (is.ie4) {
    this.element = window.document.all[obj];
    this.style = this.element.style;
    this.style.visibility = "hidden";
    this.style.display = "none";
    this.style.speak = "none";
  }
  if (is.ns6 || is.ie5) {
    this.element = document.getElementById(obj);
    this.style = this.element.style;
    this.style.visibility = "hidden";
    this.style.display = "none";
    this.style.speak = "none";
  }
  return true;
}

function getObjectById(obj) {
  if (is === null) {
    return true;
  }
  if (is.ns4) {
    this.layer = window.document[obj];
    return this.layer;
  }
  if (is.ie4) {
    this.element = window.document.all[obj];
    return this.element;
  }
  if (is.ns6 || is.ie5) {
    this.element = document.getElementById(obj);
    return this.element;
  }
}

function changeImage(imgName,imgObj) {
  if (is === null) {
    return true;
  }
  if (is.ns4) {
    eval('this.document.' + imgName + '.src = "' + imgObj + '"');
  }
  if (is.ie4) {
    document.images[imgName].src = imgObj;
  }
  if (is.ie5 || is.ns6) {
    document.images[imgName].src = imgObj;
  }
  return true;
}

function setBody(obj,body) {
  if (is === null) {
    return true;
  }
  if (is.ns4) {
    this.layer = window.document[obj];
    for(var i=0;i<body.length;i++) {
      this.layer.document.writeln(body[i]);
    }
    this.layer.document.close();
  }
  if (is.ie4) {
    this.element = window.document.all[obj];
    this.element.innerHTML = body;
  }
  if (is.ie5 || is.ns6) {
    this.element = document.getElementById(obj);
    this.element.innerHTML = body;
  }
  return true;
}

// alias per setBody, ma con obj predefinito
function chgDescOpera(body) {
  var obj = "dscOpera";
  if (is === null) {
    return true;
  }
  // replace '' con " -> questo perche' per passare tag con attributi tramite JS serve ' e non si puo' usare "
  // quindi per convenzione adotto '' per riuscire a distinguerlo da un possibile e probabile ' vero
  body = body.replace(/''/g,'"');
  if (is.ns4) {
    this.layer = window.document[obj];
    for(var i=0;i<body.length;i++) {
      this.layer.document.writeln(body[i]);
    }
    this.layer.document.close();
  }
  if (is.ie4) {
    this.element = window.document.all[obj];
    this.element.innerHTML = body;
  }
  if (is.ie5 || is.ns6) {
    this.element = document.getElementById(obj);
    this.element.innerHTML = body;
  }
  return true;
}

// alias per changeImage, ma con imgName predefinito
function chgImgOpera(imgObj) {
  var imgName = "imgOpera";
  if (is === null) {
    return true;
  }
  if (is.ns4) {
    eval('this.document.' + imgName + '.src = "' + imgObj + '"');
  }
  if (is.ie4) {
    document.images[imgName].src = imgObj;
  }
  if (is.ie5 || is.ns6) {
    document.images[imgName].src = imgObj;
  }
  return true;
}

function openWinDonizDetOU(imgSrc,width,height) {
  if (is === null) {
    return true;
  }
  if (is.ns4) {
    // test
  }
  if (is.ns6) {
    width += 6;
    height += 54;
  }
  if (is.ie4) {
    // test
  }
  if (is.ie5) {
    width += 10;
    height += 10;
  }
  win = window.open("","","width=" + width + "px,height=" + height + "px,location=no,menubar=no,resizable=no,scrollbars=no,status=no,titlebar=no,toolbar=no");
  win.document.write("<html><head><title>Arsmedia - Arte Contemporanea Italiana - Mario Donizetti</title></head><body style=\"margin:0px;padding:0px;\"><img src=\"" + imgSrc + "\" width=\"" + width + "\" height=\"" + height + "\"></body></html>");
  win.focus();
  return true;
}

function openPopWinFixedScrollable(src,name,width,height) {
  if (is === null) {
    return true;
  }
  if (is.ns4) {
    // test
  }
  if (is.ns6) {
    width += 6;
    height += 54;
  }
  if (is.ie4) {
    // test
  }
  if (is.ie5) {
    width += 10;
    height += 10;
  }
  win = window.open(src,name,"width=" + width + "px,height=" + height + "px,location=no,menubar=no,resizable=no,scrollbars=yes,status=no,titlebar=no,toolbar=no");
  win.focus();
  return true;
}

function openPopWinResizable(src,name,width,height) {
  width += 40;
  height += 80;
  win = window.open(src,name,"width=" + width + "px,height=" + height + "px,location=no,menubar=no,resizable=yes,scrollbars=no,status=no,titlebar=no,toolbar=no");
  win.resizeTo(width,height);
  win.focus();
  return true;
}

function checkForAlmostOneCheckbox(formIndex,lang) {
  var trovato = false;
  for (var i=0;i<document.forms[formIndex].elements.length;i++) {
    if (document.forms[formIndex].elements[i].type == 'checkbox') {
      if (document.forms[formIndex].elements[i].checked) {
        if (document.forms[formIndex].elements[i].name != 'anello') {
          trovato = true;
        }
      }
    }
  }
  if (!trovato) {
    if (lang == "it") {
      alert("E' necessario selezionare almeno un oggetto per poter procedere con l'acquisto.");
    } else if (lang == "en") {
      alert("You must select almost one object to continue.");
    }
    return false;
  }
  return true;
}

function valida(nmFrm,nmCmp,lblCmp,tpCmp,reqCmp,lang) {
  var i = 0;
  for (i=0;i<nmCmp.length;i++) {
    if (tpCmp[i] != "RP") {
      valore = trim(eval("document." + nmFrm + "." + nmCmp[i] + ".value"));
/*
      alert(i);
      alert(nmCmp[i]);
      alert(lblCmp[i]);
      alert(tpCmp[i]);
*/
      if (reqCmp[i] == true) {
        if (valore == "") {
          if (lang == "it") {
            alert("\" " + lblCmp[i] + " \" deve essere definito!");
          } else if (lang == "en") {
            alert("\" " + lblCmp[i] + " \" must be defined!");
          }
          eval("document." + nmFrm + "." + nmCmp[i] + ".focus();");
          return false;
        }
      }
    }
    if (tpCmp[i] == "RP") {
      var l = 0;
      rpl = eval("document." + nmFrm + "." + nmCmp[i] + ".length");
      for (l=0;l<rpl;l++) {
        if (eval("document." + nmFrm + "." + nmCmp[i] + "[l].checked")) {
          if (eval("document." + nmFrm + "." + nmCmp[i] + "[l].value") == 0) {
            if (lang == "it") {
              alert("Impossibile fornire il servizio in mancanza del consenso al trattamento dei dati.");
            } else if (lang == "en") {
              alert("Can't continue if you don't accept privacy condition.");
            }
            return false;
          }
        }
      }
    }
    if (tpCmp[i] == "T") {
    }
    if (tpCmp[i] == "N") {
      if (!isNumeric(valore)) {
        if (lang == "it") {
          alert("\" " + lblCmp[i] + " \" deve essere un numero!");
        } else if (lang == "en") {
          alert("\" " + lblCmp[i] + " \" must be a number!");
        }
        eval("document." + nmFrm + "." + nmCmp[i] + ".focus();");
        return false;
      }
    }
    if (tpCmp[i] == "M") {
      if (!isWellFormattedMail(valore)) {
        if (lang == "it") {
          alert("\" " + lblCmp[i] + " \" deve essere un indirizzo e-mail valido!");
        } else if (lang == "en") {
          alert("\" " + lblCmp[i] + " \" must be a valid e-mail address!");
        }
        eval("document." + nmFrm + "." + nmCmp[i] + ".focus();");
        return false;
      }
    }
  }
  return true;
}

function trim(stringa) {
  return stringa.replace(/(^\s*)|(\s*$)/g,"");
}

function isNumeric(val) {
  var alfabeto = "0123456789";
  var l = 0;
  for (l=0;l<val.length;l++) {
    if (alfabeto.indexOf(val.substring(l,l+1),0) == -1) {
      return false;
    }
  }
  return true;
}

function isWellFormattedMail(val) {
  var re = new RegExp("^[A-Za-z0-9._%-]+@(?:[A-Za-z0-9-]+\.)+[A-Za-z]{2,4}$");
  return re.test(val)
//  if (val.indexOf('@', 0) == -1 || val.indexOf('.', 0) == -1)  {
//    return false;
//  }
//  return true;
}
