// global functions

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  popup = window.open(theURL,winName,features);
}

function MM_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+' deve contenere un indirizzo email.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- '+nm+' deve contenere un numero.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+nm+' deve contenere un numero da '+min+' a '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' č richiesta.\n'; }
  } if (errors) alert('I seguenti errori sono occorsi:\n'+errors);
  document.MM_returnValue = (errors == '');
}

/************************ Code by Felixone.it ***********************/

function stringToUpper(element) {
  element.value = element.value.toUpperCase();
}

function stringToLower(element) {
  element.value = element.value.toLowerCase();
}

function wordUpperc(element) {
  var word, mychar, str="";
  var elemval = element.value;
  var words = elemval.split(" ");
  for (var i=0; i<words.length; i++) {
    word = words[i].replace(/ /g, "");
    if (word != "") {
      mychar = word.charAt(0);
      word = word.replace(mychar, mychar.toUpperCase());
      if (str != "") word = " " + word;
      str += word;
    }
  }
  element.value = str;
}

function firstUpperc(element) {
  var sentence, mychar, str="";
  var elemval = element.value;
  var string = elemval.split(". ");
  for (var i=0; i<string.length; i++) {
    sentence = string[i].replace(/^\s*/,"");
    if (sentence != "") {
      mychar = sentence.charAt(0);
      sentence = sentence.replace(mychar, mychar.toUpperCase());
      if (str != "") sentence = ". " + sentence;
      str += sentence;
    }
  }
  element.value = str;
}

function resetAll(theform) {
  var myform = eval("document."+theform);
  var len = myform.length;
  for (var i=0; i<len; i++) {
    if (myform.elements[i].type == "text" || myform.elements[i].type == "password" || myform.elements[i].type == "textarea" || myform.elements[i].type == "file") {
      myform.elements[i].value = "";
    } else if (myform.elements[i].type == "select-one") {
      myform.elements[i].selectedIndex = 0;
    } else if (myform.elements[i].type == "checkbox") {
      myform.elements[i].checked = false;
    }
  }
}

function deleteRow(theform) {
  if (confirm('     Sei sicuro?')) eval("document."+theform+".submit()");
}

function setParam() {
  if (document.searchform.prezzo.selectedIndex == 0) {
     document.searchform.Da.value = 0.00;
     document.searchform.A.value = 1000000.00;
  }
  if (document.searchform.prezzo.selectedIndex == 1) {
     document.searchform.Da.value = 0.00;
     document.searchform.A.value = 500.00;
  }
  if (document.searchform.prezzo.selectedIndex == 2) {
     document.searchform.Da.value = 0.00;
     document.searchform.A.value = 1000.00;
  }
  if (document.searchform.prezzo.selectedIndex == 3) {
     document.searchform.Da.value = 0.00;
     document.searchform.A.value = 2500.00;
  }
  if (document.searchform.prezzo.selectedIndex == 4) {
     document.searchform.Da.value = 2500.00;
     document.searchform.A.value = 5000.00;
  }
  if (document.searchform.prezzo.selectedIndex == 5) {
     document.searchform.Da.value = 5000.00;
     document.searchform.A.value = 12500.00;
  }
  if (document.searchform.prezzo.selectedIndex == 6) {
     document.searchform.Da.value = 12500.00;
     document.searchform.A.value = 25000.00;
  }
  if (document.searchform.prezzo.selectedIndex == 7) {
     document.searchform.Da.value = 25000.00;
     document.searchform.A.value = 1000000.00;
  }
  if (document.searchform.annoim.selectedIndex == 0) {
     document.searchform.dal.value = 0;
     document.searchform.fino.value = 1000;
  }
  if (document.searchform.annoim.selectedIndex == 1) {
     document.searchform.dal.value = 0;
     document.searchform.fino.value = 1;
  }
  if (document.searchform.annoim.selectedIndex == 2) {
     document.searchform.dal.value = 1;
     document.searchform.fino.value = 3;
  }
  if (document.searchform.annoim.selectedIndex == 3) {
     document.searchform.dal.value = 3;
     document.searchform.fino.value = 6;
  }
  if (document.searchform.annoim.selectedIndex == 4) {
     document.searchform.dal.value = 6;
     document.searchform.fino.value = 1000;
  }
}

function validatebox(form,element) {
  if (eval('document.'+form+'.'+element+'.value != ""')) {
    eval('document.'+form+'.r_'+element+'.checked = false');
  }
}

function alertbox(form,element) {
  if (eval('document.'+form+'.'+element+'.value != ""')) {
    alert("Please blank the file field if you want to check this box.");
  }
}

function moveTo() {
  var page = document.getElementById("area").value;
  window.location = page;
}

function selectType() {
  var type = document.getElementById("tipo").value;
  window.location = type;
}

function wildcard() {
  if (document.form1.f.value == "") document.form1.f.value = "%";
  if (document.form1.min.value > 0 && document.form1.max.value == "") document.form1.max.value = 1000000;
  if (document.form1.min.value == "" && document.form1.max.value > 0) document.form1.min.value = 0;
}

function validaRange() {
  var msg = "";
  var valid = true;
  if (document.form1.min) {
    if (isNaN(document.form1.min.value) || document.form1.min.value.indexOf(".") != -1) {
      valid = false;
    } else if (isNaN(document.form1.max.value) || document.form1.max.value.indexOf(".") != -1) {
      valid = false;
    }
  }
  if (!valid) {
    msg = "I valori dei prezzi devono contenere solo numeri e senza separatori.";
  } else if (parseInt(document.form1.max.value) < parseInt(document.form1.min.value)) {
    msg = "Il valore massimo non puņ essere minore del valore minimo.";
  }
  if (msg != "") {
    alert(msg);
    return false;
  } else return true;
}

var popup;
function closepop() {
  if (popup) popup.close();
}


