var InternetExplorer = navigator.appName.indexOf("Microsoft") != -1;
// Handle all the the FSCommand messages in a Flash movie
function FlashQuest_DoFSCommand(command, args) {
	if (command == "setRegione") {
		document.Ricerca.Regioni.selectedIndex = args;
	}
}

// Hook for Internet Explorer 
if (navigator.appName && navigator.appName.indexOf("Microsoft") != -1 && 
	navigator.userAgent.indexOf("Windows") != -1 && navigator.userAgent.indexOf("Windows 3.1") == -1) {
	document.write('<SCRIPT LANGUAGE=VBScript\> \n');
	document.write('on error resume next \n');
	document.write('Sub FlashQuest_FSCommand(ByVal command, ByVal args)\n');
	document.write('  call FlashQuest_DoFSCommand(command, args)\n');
	document.write('end sub\n');
	document.write('</SCRIPT\> \n');
}


function multiRisp(command, field, value) {

	if (command == "Aggiungi"){
	  if (document.all[field].value ==""){
		document.all[field].value= value;
	  } else {
		var app = document.all[field].value;
		var risposta = app.split(",");
  		risposta.push(value);
		document.all[field].value= risposta.valueOf();
	  }
	} else {
	  var app = document.all[field].value;
	  var risposta = app.split(",");
	  test = 0;
	  app="";
	  for (i = 0; i <= risposta.length; i++)
	  {
	  	if (risposta[i] == value){
		  test=1;
		} else {
		  if (app == ""){
		    app = new Array(risposta[i]);
		  } else {
		    app.push(risposta[i]);
		  }
		}
	  }

	  if (test == 0){
  		risposta.push(value);
		document.all[field].value= risposta.valueOf();
	  } else {
		document.all[field].value= app.valueOf();
		app = new String(document.all[field].value);
		n = app.length;
		if (app.charAt(n-1) == ","){
		  app=app.substr(0,n-1);
		}
		document.all[field].value= app;
	  }
	}
}

function CreateFlash(DivID, ObjectID, Whidth, Height)
{
	var myObject = document.createElement('object');
	document.all[DivID].appendChild(myObject);
	myObject.width = Whidth;
	myObject.height = Height;
	myObject.classid= "clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"; 
	myObject.codebase= "http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,24,0"; 
	myObject.movie = ObjectID;
	myObject.menu = "false";
}

function CreateFlashFSC(DivID, Name, ObjectID, Whidth, Height)
{
	var myObject = document.createElement('object');
	document.all[DivID].appendChild(myObject);
	myObject.width = Whidth;
	myObject.height = Height;
	myObject.classid= "clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"; 
	myObject.codebase= "http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,24,0"; 
	myObject.movie = ObjectID;
	myObject.menu = "false";
	myObject.wmode = "transparent";
	myObject.id = Name;
}

function CreateStream(DivID, ObjectID, Whidth, Height)
{
	var myObject = document.createElement('object');
	document.all[DivID].appendChild(myObject);
	
	myObject.classid= "CLSID:22D6f312-B0F6-11D0-94AB-0080C74C7E95"; 
	myObject.codebase= "http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,4,5,715"; 
	myObject.width = Whidth;
	myObject.height = Height;
	myObject.FileName = ObjectID;

	myObject.AutoRewind = "True";
	myObject.ShowStatusBar = "True";
	myObject.ShowControls = "True";
	myObject.Autostart = "False";
	myObject.ShowDisplay = "False";
	myObject.ControlType = "1";
/*	
	myObject.type = "application/x-oleobject";
*/
}

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.id; 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 e-mail.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- '+nm+' must contain a number.\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 essere un numero compreso tra '+min+' e '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' è obbligatorio.\n'; }
  } if (errors) alert('Si è verificato il seguente errore:\n'+errors);
  document.MM_returnValue = (errors == '');
}
function MM_validateQuantity() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateQuantity.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.id; 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 e-mail.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- '+nm+' must contain a number.\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+='- Disponibilità insufficiente per soddisfare la richiesta.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' è obbligatorio.\n'; }
  } if (errors) alert('Si è verificato il seguente errore:\n'+errors);
  document.MM_returnValue = (errors == '');
}

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_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_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_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 ControlForm() {
  if (document.form_search.SearchText.value=='') {
        alert('Il campo di ricerca non può essere vuoto');
        return false;
  }
}

function loadImage() { 
  larghezza_finestra = myImage.width + 20;
  altezza_finestra = myImage.height + 30;
  proprieta_finestra = ("'toolbar=no,scrollbars=no,statusbar=no,width=" + larghezza_finestra + ",height=" + altezza_finestra + "'");
  window.open(URL,'',proprieta_finestra);
  return true
}

function openWindow(theURL,imageWidth,imageHeight) {
  //alert(theURL);
  newWindow = window.open("","newWindow","width="+imageWidth+",height="+imageHeight);
  newWindow.document.open();
  newWindow.document.write('<html><title>' + document.title + '</title><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"></head><body bgcolor="#FFFFFF" leftmargin="0" topmargin="0" marginheight="0">'); 
  newWindow.document.write('<img src="'+theURL+'">'); 
  newWindow.document.write('</body></html>');
  newWindow.document.close();
  newWindow.focus();
}

function go_site(lingua) {
   app = document.location.pathname;
   location.href = lingua + app;
}

function openLink(theURL) {
  //alert(theURL);
  newWindow = window.open(theURL,"gallery","width=400,height=300,resizable=no,scrollbars=no");
  newWindow.focus();
}

function backToBook(pagina) {
	if (window.opener == undefined){
	  document.location=pagina;
	}else{
	  if (window.opener.closed){
		document.location=pagina;
	  }else{
		window.close();
	  }
	}
}

function selectDestination(valore) {
	if (valore!="-"){
		document.location = "/content/view/full/" + valore ;
	}
}

function filterZone(valore) {
	if (valore!="-"){
		document.location = "/content/view/full/422/(zone)/" + valore ;
	}
}

QTObject = function(mov, id, w, h) {
	this.mov = mov;
	this.id = id;
	this.width = w;
	this.height = h;
	this.redirect = "";
	this.sq = document.location.search.split("?")[1] || "";
	this.altTxt = "This content requires the QuickTime Plugin. <a href='http://www.apple.com/quicktime/download/'>Download QuickTime Player</a>.";
	this.bypassTxt = "<p>Already have QuickTime Player? <a href='?detectqt=false&"+ this.sq +"'>Click here.</a></p>";
	this.params = new Object();
	this.doDetect = getQueryParamValue('detectqt');
}

QTObject.prototype.addParam = function(name, value) {
	this.params[name] = value;
}

QTObject.prototype.getParams = function() {
    return this.params;
}

QTObject.prototype.getParam = function(name) {
    return this.params[name];
}

QTObject.prototype.getParamTags = function() {
    var paramTags = "";
    for (var param in this.getParams()) {
        paramTags += '<param name="' + param + '" value="' + this.getParam(param) + '" />';
    }
    if (paramTags == "") {
        paramTags = null;
    }
    return paramTags;
}

QTObject.prototype.getHTML = function() {
    var qtHTML = "";
	if (navigator.plugins && navigator.plugins.length) { // not ie
        qtHTML += '<embed type="video/quicktime" src="' + this.mov + '" width="' + this.width + '" height="' + this.height + '" id="' + this.id + '"';
        for (var param in this.getParams()) {
            qtHTML += ' ' + param + '="' + this.getParam(param) + '"';
        }
        qtHTML += '></embed>';
    }
    else { // pc ie
        qtHTML += '<object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" width="' + this.width + '" height="' + this.height + '" id="' + this.id + '">';
        this.addParam("src", this.mov);
        if (this.getParamTags() != null) {
            qtHTML += this.getParamTags();
        }
        qtHTML += '</object>';
    }
    return qtHTML;
}


QTObject.prototype.getVariablePairs = function() {
    var variablePairs = new Array();
    for (var name in this.getVariables()) {
        variablePairs.push(name + "=" + escape(this.getVariable(name)));
    }
    if (variablePairs.length > 0) {
        return variablePairs.join("&");
    }
    else {
        return null;
    }
}

QTObject.prototype.write = function(elementId) {
	if(isQTInstalled() || this.doDetect=='false') {
		if (elementId) {
			document.getElementById(elementId).innerHTML = this.getHTML();
		} else {
			document.write(this.getHTML());
		}
	} else {
		if (this.redirect != "") {
			document.location.replace(this.redirect);
		} else {
			if (elementId) {
				document.getElementById(elementId).innerHTML = this.altTxt +""+ this.bypassTxt;
			} else {
				document.write(this.altTxt +""+ this.bypassTxt);
			}
		}
	}		
}

function isQTInstalled() {
	var qtInstalled = false;
	qtObj = false;
	if (navigator.plugins && navigator.plugins.length) {
		for (var i=0; i < navigator.plugins.length; i++ ) {
         var plugin = navigator.plugins[i];
         if (plugin.name.indexOf("QuickTime") > -1) {
			qtInstalled = true;
         }
      }
	} else {
		execScript('on error resume next: qtObj = IsObject(CreateObject("QuickTimeCheckObject.QuickTimeCheck.1"))','VBScript');
		qtInstalled = qtObj;
	}
	return qtInstalled;
}

/* get value of querystring param */
function getQueryParamValue(param) {
	var q = document.location.search;
	var detectIndex = q.indexOf(param);
	var endIndex = (q.indexOf("&", detectIndex) != -1) ? q.indexOf("&", detectIndex) : q.length;
	if(q.length > 1 && detectIndex != -1) {
		return q.substring(q.indexOf("=", detectIndex)+1, endIndex);
	} else {
		return "";
	}
}


function zoomIn() {
	livello = document.all.cartina.src;
	livello = livello.charAt(livello.length - 5);
	livello = parseInt(livello) + 1;
	if (livello < 6) {
		document.all.cartina.src = 'images/cartina' + livello + '.gif';
		document.all.zoom.src = 'images/zoom' + livello + '.gif';
	}
}

function zoomOut() {
	livello = document.all.cartina.src;
	livello = livello.charAt(livello.length - 5);
	livello = parseInt(livello) - 1;
	if (livello > 0) {
		document.all.cartina.src = 'images/cartina' + livello + '.gif';
		document.all.zoom.src = 'images/zoom' + livello + '.gif';
	}
}
