var im;
function openWin(url, w, h, moreParams) {
  if (navigator.userAgent.toLowerCase().indexOf("msie") != -1) {
    w += 20;
    h += 20;
  }
  var par = "width=" + w + ",height=" + h;
  if(moreParams) par += ',' + moreParams;
//  for (var i=3; i<arguments.length; i++)
//    par += ',' + arguments[i];
  im = window.open(url, "im", par, false);
}

function openWinImage(url, w, h, title, moreParams){
  if (navigator.userAgent.toLowerCase().indexOf("msie") != -1) {
    w += 20;
    h += 20;
  }
  var par = "width=" + w + ",height=" + h;
  if(moreParams) par += ',' + moreParams;
  im = window.open("", "im", par, false);

  im.document.write("<html>\n")
  im.document.write("<head>\n")
  im.document.write("<title>"+title+"</title>\n")
  im.document.write("<script type=\"text/javascript\">\n");
  im.document.write("<!--\n");
  im.document.write("var message=String.fromCharCode(169) + \" Sorrentonline scarl\";\n");
  im.document.write("document.oncontextmenu=new Function(\"alert(message);return false;\")\n");
  im.document.write("//-->\n");
  im.document.write("</script>\n");
  im.document.write("</head>\n");
  im.document.write("<body>\n");
  im.document.write("<img style=\"width: 100%;\" border==\"0\" src=\""+url+"\" alt=\"\" title=\"\">\n");
  im.document.write("</body>\n");
  im.document.write("</html>");
  im.document.close();
}

/* Versione abbreviata di document.getElementById. "id" puņ essere sia l'id
dell'oggetto che l'oggetto stesso (Pao) */
function o(id) {
    return getObject(id);
}

/* Restituisce l'oggetto document.getElementById(obj) se obj č una stringa,
altrimenti reistituisce obj. */
function getObject(obj) {
    return (typeof obj == "string") ? document.getElementById(obj) : obj;
}

/* Restituisce il value di un oggetto passandogli l'id dell'oggetto (Pippo)
Id: l'attributo id dell'oggetto.
Se l'oggetto non esiste ritorna una stringa vuota.
I tipi di oggetti supportati sono:
INPUT TEXT
INPUT CHECKBOX
INPUT RADIO
INPUT HIDDEN
SELECT
*/
function getValue(id) {
	var obj = o(id);
        var ret = '';
	if(obj) {
		var tag = obj.tagName.toUpperCase();
		if('INPUT' == tag) {
			var type = obj.type.toUpperCase();
			if('CHECKBOX' == type
                          || 'RADIO' == type) {
				if(obj.checked) {
					ret = obj.value;
				}
			} else { // TEXT || HIDDEN
				ret = obj.value;
			}
		} else if('SELECT' == tag || 'SELECT-ONE' == tag || 'SELECTONE' == tag) {
			ret = obj[obj.selectedIndex].value;
		} else {
			ret = obj.value;
		}
	}
        return ret;
}

function vai() {
  var winW = 1024;
  var winH = 768;
  if (parseInt(navigator.appVersion)>3) {
    if (navigator.appName.indexOf("Microsoft")!=-1) {
      winW = document.body.offsetWidth;
      winH = document.body.offsetHeight;
    } else {
      winW = window.innerWidth;
      winH = window.innerHeight;
    }
  }
  //LeftPosition=(screen.width)?(screen.width-w)/2:100;
  //TopPosition=(screen.height)?(screen.height-h)/2:100;
  var left = document.getElementById('ricerca').width || 300;
  document.getElementById('ricerca').style.left = (screen.width)?(screen.width-left)/2:100;
  document.getElementById('ricerca').style.top = '430px';
  document.getElementById('ricerca').style.display = 'block';
  document.formPrenota.submit();
}

function copyDate() {
  if (document.formMain.partenza.value == "" && document.formMain.arrivo.value != "") {
    var date = new Date();
    date.setFullYear(parseInt(document.formMain.arrivo.value.substring(6)));
    date.setMonth(parseInt(document.formMain.arrivo.value.substring(3, 5))-1);
    date.setDate(parseInt(document.formMain.arrivo.value.substring(0, 2))+1);
    var year = date.getFullYear()
    var month = padLeft(new String(date.getMonth()+1), '0', 2);
    var day = padLeft(new String(date.getDate()), '0', 2);
    document.formMain.partenza.value = day + '-' + month + '-' + year;
  }
}
function padLeft(str, ch, length) {
  while (str.length < length)
    str = ch + str;
  return str;
}

function ordina(val, dir) {
  document.formMain.ord.value = val;
  document.formMain.dir.value = dir;
  document.formMain.elements["do"].value = "aggiorna";
  document.formMain.submit();
}
function scegli(id) {
  document.formMain.bo11_id.value = id;
  document.formMain.elements["do"].value = "scegliTipo";
  document.formMain.submit();
}
function findLi02(bo11_id, ri01_id) {
  if (document.formMain.elements["li02_codice_" + bo11_id + "_" + ri01_id].length) {
    var value = "";
    for (var i=0; i<document.formMain.elements["li02_codice_" + bo11_id + "_" + ri01_id].length; i++) {
      if (document.formMain.elements["li02_codice_" + bo11_id + "_" + ri01_id][i].checked)
        value += document.formMain.elements["li02_codice_" + bo11_id + "_" + ri01_id][i].value + "|";
    }
    if (value != "")
      return value;
    else
      return null;
  } else {
    if (document.formMain.elements["li02_codice_" + bo11_id + "_" + ri01_id].checked)
      return document.formMain.elements["li02_codice_" + bo11_id + "_" + ri01_id].value;
    else
      return null;
  }
}
function findLi05(bo11_id, ri01_id) {
  if (document.formMain.elements["li05_id_" + bo11_id + "_" + ri01_id].length) {
    var value = "";
    for (var i=0; i<document.formMain.elements["li05_id_" + bo11_id + "_" + ri01_id].length; i++) {
      if (document.formMain.elements["li02_codice_" + bo11_id + "_" + ri01_id][i].checked)
        value += document.formMain.elements["li05_id_" + bo11_id + "_" + ri01_id][i].value + "|";
    }
    if (value != "")
      return value;
    else
      return null;
  } else {
    if (document.formMain.elements["li02_codice_" + bo11_id + "_" + ri01_id].checked)
      return document.formMain.elements["li05_id_" + bo11_id + "_" + ri01_id].value;
    else
      return null;
  }
}

function backResult() {
  document.formMain.elements["do"].value = "backResult";
  document.formMain.action = "booking";
  document.formMain.submit();
}
function resetForm() {
  for (var i=0; i<document.formMain.elements.length; i++) {
    if (document.formMain.elements[i].type == "radio") {
      document.formMain.elements[i].checked = false;
    }
  }
}
function backChoose() {
  document.form1.elements["do"].value = "backChoose";
  document.form1.submit();
}

function controllaArrivo(giorno, mese, anno) {
  var select = new Date(anno.options[anno.selectedIndex].value,
                        parseInt(mese.options[mese.selectedIndex].value)-1,
                        giorno.options[giorno.selectedIndex].value);
  var start = new Date();
  start.setUTCHours(0, 0, 0, 0);
  if (select.getTime() < start.getTime()) {
    for (var i=0; i<anno.options.length; i++) {
      if (anno.options[i].value == new String(start.getFullYear())) {
        anno.selectedIndex = i;
        break;
      }
    }
    for (var i=0; i<mese.options.length; i++) {
      if (mese.options[i].value == new String(start.getMonth()+1)) {
        mese.selectedIndex = i;
        break;
      }
    }
    for (var i=0; i<giorno.options.length; i++) {
      if (giorno.options[i].value == new String(start.getDate())) {
        giorno.selectedIndex = i;
        break;
      }
    }
  }
}
function controllaPartenza(giorno, mese, anno) {
  var select = new Date(anno.options[anno.selectedIndex].value,
                        parseInt(mese.options[mese.selectedIndex].value)-1,
                        giorno.options[giorno.selectedIndex].value);
  var f = document.formPrenota;
  var start = new Date(f.arrivalYear.options[f.arrivalYear.selectedIndex].value,
                       parseInt(f.arrivalMonth.options[f.arrivalMonth.selectedIndex].value)-1,
                       f.arrivalDay.options[f.arrivalDay.selectedIndex].value);
  start.setTime(start.getTime() + 1000 * 60 * 60 * 24);
  if (select.getTime() < start.getTime()) {
    for (var i=0; i<anno.options.length; i++) {
      if (anno.options[i].value == new String(start.getFullYear())) {
        anno.selectedIndex = i;
        break;
      }
    }
    for (var i=0; i<mese.options.length; i++) {
      if (mese.options[i].value == new String(start.getMonth()+1)) {
        mese.selectedIndex = i;
        break;
      }
    }
    for (var i=0; i<giorno.options.length; i++) {
      if (giorno.options[i].value == new String(start.getDate())) {
        giorno.selectedIndex = i;
        break;
      }
    }
  }
}
