var ajax = new Array(11);
var iAjax;
var mRiga = "";

function GetMyHeight() {
  if (self.innerHeight){
    return self.innerHeight;
  }else if (document.documentElement && document.documentElement.clientHeight){
    return document.documentElement.clientHeight;
  }else if (document.body){
    return document.body.clientHeight;
  }
}

function GetMyWidth() {
  if (self.innerHeight){
    return self.innerWidth;
  }else if (document.documentElement && document.documentElement.clientHeight){
    return document.documentElement.clientWidth;
  }else if (document.body){
    return document.body.clientWidth;
  }
}

function prendiElementoDaId(id_elemento) {
	var elemento;
	if(document.getElementById){
	  elemento = document.getElementById(id_elemento);
	}else{
	  elemento = document.all[id_elemento];
	}
	return elemento;
}

function assegnaXMLHttpRequest() {
  var XHR = null;
  var browserUtente = navigator.userAgent.toUpperCase();
  if(typeof(XMLHttpRequest) === "function" || typeof(XMLHttpRequest) === "object"){
    XHR = new XMLHttpRequest();
  }else if(window.ActiveXObject && (browserUtente.indexOf("MSIE 4") < 0)) {
    if(browserUtente.indexOf("MSIE 5") < 0){
      XHR = new ActiveXObject("Msxml2.XMLHTTP");
    }else{
      XHR = new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
  return XHR;
}

function diagnostica(mOgg) {
  var myWin = window.open();
  myWin.document.open();
  for (var mVal in mOgg) {
    myWin.document.write(mVal + " = " + mOgg[mVal] + "<br />");
  }
  myWin.document.close();
}

function vai(url, azione, parametro) {
	var mForm = document.getElementById('m_Form');
	var mAzione = document.getElementById('m_Azione');
	var mParametro = document.getElementById('m_Parametro');
	
	mAzione.value = azione;
	mParametro.value = parametro;
	if ((url != "") && (mForm.action != url)) mForm.action = url;
	mForm.target = "_self";
	mForm.submit();
}

function vaiLink(mLink) {
	var mStatoNodi = document.getElementById('m_statoNodi');
	var destLink;
	
	if (mStatoNodi.value != "") {
  	if (mLink.indexOf("?") == -1) {
  	  destLink = mLink + "?m_statoNodi=" + mStatoNodi.value;
    } else {
      destLink = mLink + "&m_statoNodi=" + mStatoNodi.value;
    }
  } else {
    destLink = mLink
  }
	location.href = destLink;
}

function ExpanderClicked(m_id) {
	var strCSS;
	var ctlExpander = document.getElementById("LINK_" + m_id);
	var ctlSelectedEntry = document.getElementById("DIV_" + m_id);

	if(ctlSelectedEntry.childNodes.length > 0)
	{
		if(ctlExpander.className == "mMenuO") {
			ctlExpander.className = "mMenuC"
			strCSS = "IsVisible";
			//adNodo(m_id);
		} else {
			ctlExpander.className = "mMenuO"
			strCSS = "NotVisible";
		}

		for(var intCounter = 0; intCounter < ctlSelectedEntry.childNodes.length; intCounter++) {
			if(ctlSelectedEntry.childNodes[intCounter].nodeName == "DIV") {
				ctlSelectedEntry.childNodes[intCounter].className = strCSS;
				var m_nodoID = ctlSelectedEntry.childNodes[intCounter].id.replace("DIV_","");

				if (strCSS == "IsVisible") {
					adNodo(m_nodoID);
				} else {
					rmNodo(m_nodoID);
				}

			}
		}
	}
}


function adNodo(idNodo) {
	var m_Nodi = document.getElementById("m_statoNodi");

	if ((m_Nodi.value == "") || (m_Nodi.value == null)) {
		m_Nodi.value = idNodo;
	} else if (m_Nodi.value.search(", ") == -1) {
		if (m_Nodi.value != idNodo) {
			m_Nodi.value = m_Nodi.value + ", " + idNodo;
		}
	} else {
		var v_Nodi = m_Nodi.value.split(", ");
		var c_Nodi = 0;
		m_Nodi.value = "";
		while (c_Nodi < v_Nodi.length) {
			if (v_Nodi[c_Nodi] != idNodo) {
				m_Nodi.value = m_Nodi.value + v_Nodi[c_Nodi] + ", ";
			}
			c_Nodi+=1;
		}
		m_Nodi.value = m_Nodi.value + idNodo;
	}
}


function rmNodo(idNodo) {
	var m_Nodi = document.getElementById("m_statoNodi");

	if ((m_Nodi.value == "") || (m_Nodi.value == null)) {
		m_Nodi.value == "";
	} else if (m_Nodi.value.search(", ") == -1) {
		if (m_Nodi.value == idNodo) {
			m_Nodi.value = "";
		}
	} else {
		var v_Nodi = m_Nodi.value.split(", ");
		var c_Nodi = 0;
		m_Nodi.value = "";
		while (c_Nodi < v_Nodi.length) {
			if (v_Nodi[c_Nodi] != idNodo) {
				m_Nodi.value = m_Nodi.value + v_Nodi[c_Nodi] + ", ";
			}
			c_Nodi+=1;
		}
		m_Nodi.value = m_Nodi.value.substr(0, m_Nodi.value.length - 2);
	}
}

function apri(pagina_da_aprire, wid, hei) {
	popupImage = window.open(pagina_da_aprire,'immage','toolbar=0,status=0,menubar=0,scrollbars=0,resizable=0,width=' + wid + ',height=' + hei);
	popupImage.focus();
}

function SettaLink(mID, mSquadra, mUrl, mMail) {
	var inputID = document.getElementById("m_LinkID");
	var inputSquadra = document.getElementById("m_LinkSquadra");
	var inputUrl = document.getElementById("m_LinkUrl");
	var inputMail = document.getElementById("m_LinkMail");
	
	inputID.value = mID;
	inputSquadra.value = mSquadra;
	inputUrl.value = mUrl;
	inputMail.value = mMail;

}

function ShowHide(m_id) {
	var mSubMenu = document.getElementById("FOTOMENU_" + m_id);
	var mLink = document.getElementById("FOTOLINK_" + m_id);

	if(mSubMenu.className == "FotoNotVisible") {
		mSubMenu.className = "FotoIsVisible";
		mLink.className = "mLinkC";
	} else {
		mSubMenu.className = "FotoNotVisible";
		mLink.className = "mLinkO";
	}
}

function ChiudiScegliFoto(){
    window.opener.aggiornaListaFoto();
    window.close();
}

function ScegliFoto() {
    var mNazione = prendiElementoDaId('nazione').value;
    if (mNazione == "") {
        ScriviMessaggio("msgGen", "Indicare la Nazione" , '#FF0000');
        return;
    }
    
    var mCitta = prendiElementoDaId('citta').value;
    if (mCitta == "") {
        ScriviMessaggio("msgGen", "Indicare la Cittą" , '#FF0000');
        return;
    }

    var mAnno = prendiElementoDaId('anno').value;    
    if (mAnno == "") {
        ScriviMessaggio("msgGen", "Indicare l'anno" , '#FF0000');
        return;
    }
    
    prendiElementoDaId('mAzione').value = "InsFoto";
		var mForm = prendiElementoDaId('m_Form');
		mForm.action = "AdminFoto3.asp";
		mForm.target = "_self";
		mForm.submit();
    
    
/*
		var mURL = "AppletPopUp.asp?Nazione=" + mNazione + "&Citta=" + mCitta + "&Anno=" + mAnno;
		window.open(mURL, '', 'scrollbars=no, resizable=no, width=550, height=350, status=no, location=no, toolbar=no');

    var mPopUp = window.open('', '', 'scrollbars=no, resizable=no, width=550, height=350, status=no, location=no, toolbar=no');
    mPopUp.document.open();
    mPopUp.document.writeln('<html>');
    mPopUp.document.writeln('<head>');
    mPopUp.document.writeln('<script type="text/javascript" src="img/utilita.js"></script>');
    mPopUp.document.writeln('</head>');
    mPopUp.document.writeln('<body>');
    mPopUp.document.writeln('<applet name="uploadApplet" code="javaatwork.myuploader.UploadApplet.class" archive="myuploader-free-signed-1.11.jar, labels.jar" width="100%"  height="100%">');
    mPopUp.document.writeln('<param name="uploadURL" value="upload.aspx">');
    mPopUp.document.writeln('<param name="maxImageSize" value="500">');
    mPopUp.document.writeln('<param name="successURL" value="javascript:ChiudiScegliFoto();">');
    mPopUp.document.writeln('<param name="charsetForURLEncoding" value="ISO-8859-1">');
    mPopUp.document.writeln('<param name="showThumbNailsInFileDialog" value="true">');
    mPopUp.document.writeln('<param name="showThumbNailsInApplet" value="true">');
    mPopUp.document.writeln('<param name="postParameters" value="Nazione, Citta, Anno">');
    mPopUp.document.writeln('<param name="Nazione" value="' + mNazione + '">');
    mPopUp.document.writeln('<param name="Citta" value="' + mCitta + '">');
    mPopUp.document.writeln('<param name="Anno" value="' + mAnno + '">');
    mPopUp.document.writeln('</applet>');
    mPopUp.document.writeln('</body>');
    mPopUp.document.writeln('</html>');
    mPopUp.document.close();
*/
}

function InsFoto(){
    var mFile = prendiElementoDaId('mioFile').value;
    if (mFile == "") {
        ScriviMessaggio("msgGen", "Selezionare una foto" , '#FF0000');
        return;
    }

    var mEstensione = mFile.slice(mFile.lastIndexOf(".")).toLowerCase();
    switch (mEstensione) {
      case ".jpg":
        break;
      case ".jpeg":
        break;
      case ".png":
        break;
      case ".gif":
        break;
      case ".bmp":
        break;
      default: 
        ScriviMessaggio("msgGen", "Selezionare una foto valida" , '#FF0000');
        return;
    }

    var mNazione = prendiElementoDaId('nazione').value;
    if (mNazione == "") {
        ScriviMessaggio("msgGen", "Indicare la Nazione" , '#FF0000');
        return;
    }
    
    var mCitta = prendiElementoDaId('citta').value;
    if (mCitta == "") {
        ScriviMessaggio("msgGen", "Indicare la Cittą" , '#FF0000');
        return;
    }

    var mAnno = prendiElementoDaId('anno').value;    
    if (mAnno == "") {
        ScriviMessaggio("msgGen", "Indicare l'anno" , '#FF0000');
        return;
    }

    var mDesc = prendiElementoDaId('desc').value;
    if (mDesc == "") {
        ScriviMessaggio("msgGen", "Indicare la descrizione" , '#FF0000');
        return;
    }

		var mURL = "EstraiDaDb.asp?AZIONE=InsFotoDesc&Nazione=" + mNazione + "&Citta=" + mCitta + "&Anno=" + mAnno + "&Desc=" + mDesc
    ajax[0] = assegnaXMLHttpRequest();
    ajax[0].onreadystatechange = SendInsFoto;
    ajax[0].open("GET", mURL, true);
    ajax[0].send(null);
}

function SendInsFoto(){
    if (ajax[0].readyState == 4) {
        if(ajax[0].responseText == 'OK') {
            prendiElementoDaId('mAzione').value = "InsFoto";
            prendiElementoDaId('m_Form').submit();
        } else {
            ScriviMessaggio("msgGen", "Descrizione duplicata" , '#FF0000');
        }
    }
}

function ModFoto2(){
    var mIdFoto = prendiElementoDaId('FotoId').value;
    if (mIdFoto == "") {
        ScriviMessaggio("msgGen", "Selezionare una foto" , '#FF0000');
        return;
    }

    var mIdPadre = prendiElementoDaId('FotoPadre').value;    
    if (mIdPadre == "") {
        ScriviMessaggio("msgGen", "Selezionare una foto" , '#FF0000');
        return;
    }

    var mDesc = prendiElementoDaId('desc').value;
    if (mDesc == "") {
        ScriviMessaggio("msgGen", "Indicare la descrizione" , '#FF0000');
        return;
    }
	prendiElementoDaId('mAzione').value = "ModFoto";
	var mForm = prendiElementoDaId('m_Form');
	mForm.action = "UpploadFoto.aspx";
	mForm.target = "destinazione";
	mForm.submit();
}

function ModFoto(){
    var mIdFoto = prendiElementoDaId('FotoId').value;
    if (mIdFoto == "") {
        ScriviMessaggio("msgGen", "Selezionare una foto" , '#FF0000');
        return;
    }

    var mIdPadre = prendiElementoDaId('FotoPadre').value;    
    if (mIdPadre == "") {
        ScriviMessaggio("msgGen", "Selezionare una foto" , '#FF0000');
        return;
    }

    var mDesc = prendiElementoDaId('desc').value;
    if (mDesc == "") {
        ScriviMessaggio("msgGen", "Indicare la descrizione" , '#FF0000');
        return;
    }

	var mURL = "EstraiDaDb.asp?AZIONE=ModFotoDesc&IdPadre=" + mIdPadre + "&Desc=" + mDesc
    ajax[1] = assegnaXMLHttpRequest();
    ajax[1].onreadystatechange = SendModFoto;
    ajax[1].open("GET", mURL, true);
    ajax[1].send(null);
}

function SendModFoto(){
    if (ajax[1].readyState == 4) {
        if(ajax[1].responseText == 'OK') {
            prendiElementoDaId('mAzione').value = "ModFoto";
            prendiElementoDaId('m_Form').submit();
        } else {
            ScriviMessaggio("msgGen", "Descrizione duplicata" , '#FF0000');
        }
    }
}

function DelFoto2(){
    var mIdFoto = prendiElementoDaId('FotoId').value;
    if (mIdFoto == "") {
        ScriviMessaggio("msgGen", "Selezionare una foto" , '#FF0000');
        return;
    }

    var mIdPadre = prendiElementoDaId('FotoPadre').value;    
    if (mIdPadre == "") {
        ScriviMessaggio("msgGen", "Selezionare una foto" , '#FF0000');
        return;
    }

	var answer = confirm ("Sei sicuro di voler cancellare la foto selezionata?");
	if (answer) {
        prendiElementoDaId('mAzione').value = "DelFoto";
				var mForm = prendiElementoDaId('m_Form');
				mForm.action = "UpploadFoto.aspx";
				mForm.target = "destinazione";
				mForm.submit();
    }
}

function DelFoto(){
    var mIdFoto = prendiElementoDaId('FotoId').value;
    if (mIdFoto == "") {
        ScriviMessaggio("msgGen", "Selezionare una foto" , '#FF0000');
        return;
    }

    var mIdPadre = prendiElementoDaId('FotoPadre').value;    
    if (mIdPadre == "") {
        ScriviMessaggio("msgGen", "Selezionare una foto" , '#FF0000');
        return;
    }

	var answer = confirm ("Sei sicuro di voler cancellare la foto selezionata?");
	if (answer) {
        prendiElementoDaId('mAzione').value = "DelFoto";
        prendiElementoDaId('m_Form').submit();
    }
}

function DoPostBack(){
  prendiElementoDaId('mAzione').value = "Prova";
  prendiElementoDaId('m_Form').submit();
}

function Test(){
//	var messaggio = "Nazione: " + prendiElementoDaId('nazione').value + "\nCittą: " + prendiElementoDaId('citta').value + "\nAnno: " + prendiElementoDaId('anno').value + "\nDescrizione: " + prendiElementoDaId('desc').value + "\nId Foto: " + prendiElementoDaId('FotoId').value + "\nIdPadre: " + prendiElementoDaId('FotoPadre').value + "\nPercorso Foto: " + prendiElementoDaId('mioFile').value;
	//alert(messaggio);
	var mBottone = prendiElementoDaId('mDel');
	diagnostica(mBottone);
}	    

function SettaValoriFoto(mvalore){
	prendiElementoDaId('nazione').value = obj.getCellText(0, mvalore);
	prendiElementoDaId('citta').value = obj.getCellText(1, mvalore);
	prendiElementoDaId('anno').value = obj.getCellText(2, mvalore);
	prendiElementoDaId('desc').value = obj.getCellText(3, mvalore);
	prendiElementoDaId('OldFotoPath').value = obj.getCellText(4, mvalore);
	prendiElementoDaId('FotoId').value = obj.getCellText(5, mvalore);
	prendiElementoDaId('FotoPadre').value = obj.getCellText(6, mvalore);
	prendiElementoDaId('mFoto').src = obj.getCellText(4, mvalore);
}

function mostraEsito(){
  var mMdbFrame = document.getElementById("mdbText");
  var mEsito = mMdbFrame.contentWindow.document.getElementById('mEsito').value;
  var mMessaggio = mMdbFrame.contentWindow.document.getElementById('mMessaggio').value;
  mRiga = mMdbFrame.contentWindow.document.getElementById('mRiga').value;
  
  if (mEsito == "OK") {
  	  table.request();
  	  ScriviMessaggio("msgGen", mMessaggio);
  } else {
  	ScriviMessaggio("msgGen", mMessaggio , '#FF0000');
	}
}

function aggiornaListaFoto(){
    mRiga = "";
    table.request();
    ScriviMessaggio("msgGen", "Upload eseguito correttamente");
}

function ScriviMessaggio(mLabel, mTesto){
	var mLabel = prendiElementoDaId(mLabel);
	var mTesto = document.createTextNode(mTesto);
	if (mLabel.hasChildNodes()) {
		mLabel.removeChild(mLabel.firstChild);
	}
	mLabel.appendChild(mTesto);
}

function ScriviMessaggio(mLabel, mTesto, mColore){
	var mLabel = prendiElementoDaId(mLabel);
	var mTesto = document.createTextNode(mTesto);
	if (mLabel.hasChildNodes()) {
		mLabel.removeChild(mLabel.firstChild);
	}
	mLabel.appendChild(mTesto);
	mLabel.setAttribute("style", "color:" + mColore + ";");
}

function TabellaFotoReady(){
	if (mRiga != "") {
		if (mRiga == 0) {
			obj.setSelectedRows([mRiga+1]);
			obj.setCurrentRow(mRiga+1);
		}else {
			obj.setSelectedRows([mRiga-1]);
			obj.setCurrentRow(mRiga-1);
		}
		obj.setSelectedRows([mRiga]);
		obj.setCurrentRow(mRiga);
	}
}

function TornaGestioneFoto(){
	vaiLink("AdminFoto3.asp");
}
