/**
 * @author Bonuccelli Daniele
 */

function removeImage(imagePath)
{
	var xmlHttp
	
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
		{
		alert ("Your browser does not support AJAX!");
		return;
		} 
	var url="strutture/removeImage.asp";
	url=url+"?imagePath="+imagePath;
	url=url+"&sid="+Math.random();
	xmlHttp.onreadystatechange=function() {stateChanged("elencoFotografie",xmlHttp)};
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function getImagesList(idStruttura, field, directoryPath)
{
	var xmlHttp
	
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
		{
		alert ("Your browser does not support AJAX!");
		return;
		} 
	var url="strutture/imageslist.asp";
	url=url+"?idStruttura="+idStruttura+"&field="+field+"&directoryPath="+directoryPath;
	url=url+"&sid="+Math.random();
	
	//alert(url);
	
	xmlHttp.onreadystatechange=function() {stateChanged("elencoFotografie",xmlHttp)};
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function caricapagina()
{
	var xmlHttp
	
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
		{
		alert ("Your browser does not support AJAX!");
		return;
		} 
//	var url="elencofotografie.asp";
	var url="paginatextarea.asp";
	url=url+"?sid="+Math.random();
	xmlHttp.onreadystatechange=function() {stateChanged("layer_editor",xmlHttp)};
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function hideLayerFoto()
{
	document.getElementById("layer_foto").style.display = "none"
}

function getPhoto(pathfoto, luogo, data)
{
	var xmlHttp
	
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
		{
		alert ("Your browser does not support AJAX!");
		return;
		} 
	var url="fotografia.asp";
	url=url+"?pathfoto="+pathfoto;
	url=url+"&sid="+Math.random();
	xmlHttp.onreadystatechange=function() {stateChanged("layer_foto_tdFoto",xmlHttp)};
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	
	document.getElementById("layer_foto_tdTesto").innerHTML = luogo + " - " + data

	if (document.getElementById("layer_foto").style.display == "inline")
	{
		document.getElementById("layer_foto").style.display = "none"
//		document.getElementById("layer_foto").innerHTML = "| "+ pathfoto +" |"
	}
	else
	{
		document.getElementById("layer_foto").style.display = "inline"
	}


	//Sposto il layer alle coordinate giuste: centro dello schermo
	var newXCoordinate = (document.body.offsetWidth - getObject("layer_foto").offsetWidth) / 2 - 20;
	var newYCoordinate = (document.body.offsetHeight - getObject("layer_foto").offsetHeight) / 2 - 180;
//	var newXCoordinate = (getObject("body").offsetWidth - getObject("layer_foto").offsetWidth) / 2;
//	var newYCoordinate = (getObject("body").offsetHeight - getObject("layer_foto").offsetHeight) / 2;
	
	moveObject("layer_foto", newXCoordinate, newYCoordinate);

}

function getCalendar(mese,anno)
{
	var xmlHttp
	
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
		{
		alert ("Your browser does not support AJAX!");
		return;
		} 
	var url="calendario/calendario.asp";
	url=url+"?mese="+mese+"&anno="+anno;
	url=url+"&sid="+Math.random();
	xmlHttp.onreadystatechange=function() {stateChanged("calendarCell",xmlHttp)};
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}


function savehomepage()
{

//document.getElementById("iframenascosto").contentDocument.body.innerHTML = document.getElementById("homepagefakeform").innerHTML;
//document.getElementById("iframenascosto").contentDocument.getElementById("homepageform").innerHTML = document.getElementById("homepagefakeform").innerHTML;
document.getElementById("iframenascosto").contentWindow.document.body.innerHTML = document.getElementById("homepagefakeform").innerHTML;
//alert(document.getElementById("iframenascosto"));
//alert(document.getElementById("iframenascosto").contentWindow.document.body);

alert(document.getElementById("iframenascosto").contentWindow.document.getElementById("homepageform").submit);
alert(document.getElementById("immaginehomepage").value);
document.getElementById("iframenascosto").contentWindow.document.getElementById("immaginehomepage").value = 'fedf'; 
alert(document.getElementById("iframenascosto").contentWindow.document.getElementById("immaginehomepage").value);

//document.getElementById("iframenascosto").contentWindow.document.getElementById("homepageform").submit();

/*
	var xmlHttp
	
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
		{
		alert ("Your browser does not support AJAX!");
		return;
		} 
	var url="homepage/provaupload.asp";
	url=url+"&sid="+Math.random();
	xmlHttp.onreadystatechange=function() {stateChanged("okCell",xmlHttp)};
	xmlHttp.open("POST",url,true);
	xmlHttp.send(null);
*/
}

//----------------------------------------------------


   var http_request = false;
   function makePOSTRequest(url, parameters) {
      http_request = false;
      if (window.XMLHttpRequest) { // Mozilla, Safari,...
         http_request = new XMLHttpRequest();
         if (http_request.overrideMimeType) {
         	// set type accordingly to anticipated content type
            //http_request.overrideMimeType('text/xml');
            http_request.overrideMimeType('text/html');
         }
      } else if (window.ActiveXObject) { // IE
         try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
      if (!http_request) {
         alert('Cannot create XMLHTTP instance');
         return false;
      }
      
      http_request.onreadystatechange = alertContents;
      http_request.open('POST', url, true);
      http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      http_request.setRequestHeader("Content-length", parameters.length);
      http_request.setRequestHeader("Connection", "close");
      http_request.send(parameters);
   }

   function alertContents() {
      if (http_request.readyState == 4) {
         if (http_request.status == 200) {
            //alert(http_request.responseText);
            result = http_request.responseText;
            document.getElementById('okCell').innerHTML = result;            
         } else {
            alert('There was a problem with the request.');
         }
      }
   }
   
   function get(obj) {
      var poststr = "immaginehomepage=" + encodeURI( document.getElementById("immaginehomepage").value );
                    // +
                    //"&mytextarea2=" + encodeURI( document.getElementById("mytextarea2").value );
      makePOSTRequest('homepage/provaupload.asp', poststr);
   }




//----------------------------------------------------

function stateChanged(targetElementId,xmlHttp) 
{ 
	if (xmlHttp.readyState==1)
	{ 
		//document.getElementById(targetElementId).innerHTML="<img src='images/ajax-loader.gif'>";
	}
	if (xmlHttp.readyState==4)
	{ 
		// if "OK"
		if (xmlHttp.status==200)
		{
			document.getElementById(targetElementId).innerHTML=xmlHttp.responseText;
		}
//		else
//		{
//			document.getElementById(targetElementId).innerHTML="HTTP Error " + xmlHttp.status;
//		}
	}
}

function GetXmlHttpObject()
{
	var xmlHttp=null;
	try
	{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{
		// Internet Explorer
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}

