﻿// JScript File
//////////////////////////////////////////
//Variabili generiche
var http = getHTTPObject(); 
var httpWorking;
var targetDiv = "";
var targetFunction = "";
var historyFlag;
var historyPageLoaded = false;
var emb;


/////////////////////////////////////////
//Funzioni pubbliche


// Mostra e gestisci calendario nei campi per selezionare una data
function show_calendario(str_target,  str_divId, str_datetime) {
    var vWin = document.getElementById(str_divId);
	if (vWin.style.visibility=='visible')
	    vWin.style.visibility='hidden';
	else 
	   show_calendar(str_target, str_datetime, str_divId)
}


function loadPage(url, divID, uniqueCall, history, historyData)
{
    if (httpWorking) 
    {
        alert('E\' in corso un\' altra operazione. Attendere.');
        return;
    }
    if (!document.getElementById(divID))
    {
        alert('Impossibile effettuare l\'operazione richiesta');
        return;
    }
    targetDiv = divID;
    
    if (uniqueCall)
    {
        if (url.indexOf('?')>0) url += '&randomID=' + Math.random()*10000;
        else url += '?randomID=' + Math.random()*10000;
    }
   
    if (http) {
    httpWorking = true;
    if (history)
    {
        addHistoryData(historyData);
    }
    http.open("GET", url, true);
    http.onreadystatechange = handleHttpResponse;
    http.send(null);
    }
    else
    {
        alert('Impossibile effettuare l\'operazione richiesta');
    }
}

function loadPageNoControllo(url, divID, uniqueCall, history, historyData)
{
    if (!document.getElementById(divID))
    {
        alert('Impossibile effettuare l\'operazione richiesta');
        return;
    }
    targetDiv = divID;
    
    if (uniqueCall)
    {
        if (url.indexOf('?')>0) url += '&randomID=' + Math.random()*10000;
        else url += '?randomID=' + Math.random()*10000;
    }
   
    if (http) {
    httpWorking = true;
    if (history)
    {
        addHistoryData(historyData);
    }
    http.open("GET", url, true);
    http.onreadystatechange = handleHttpResponse;
    http.send(null);
    }
    else
    {
        alert('Impossibile effettuare l\'operazione richiesta');
    }

}

function loadDoc(url, uniqueCall, history, historyData)
{
    if (httpWorking) 
    {
        alert('E\' in corso un\' altra operazione. Attendere.');
        return;
    }
    
    if (uniqueCall)
    {
        if (url.indexOf('?')>0) url += '&randomID=' + Math.random()*10000;
        else url += '?randomID=' + Math.random()*10000;
    }
   
    if (http) {
    httpWorking = true;
    if (history)
    {
        addHistoryData(historyData);
    }
    http.open("GET", url, true);
    http.onreadystatechange = handleHttpResponseDoc;
    http.send(null);
    }
    else
    {
        alert('Impossibile effettuare l\'operazione richiesta');
    }
}


function loadPageRisRicerca(url, data)
{
    if (httpWorking) 
    {
        alert('E\' in corso un\' altra operazione. Attendere.');
        return;
    }

    if (http) {
        httpWorking = true;
        http.open('POST', url, true);
        http.onreadystatechange = handleHttpResponseRic;
        http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        http.setRequestHeader("Content-length", data.length);
        http.setRequestHeader("Connection", "close");
        http.send(data);
    }
    else
    {
        alert('Impossibile effettuare l\'operazione richiesta');
    }
}

function loadDocPost(url, uniqueCall, history, historyData, postData)
{
    if (httpWorking) 
    {
        alert('E\' in corso un\' altra operazione. Attendere.');
        return;
    }
    
    if (uniqueCall)
    {
        if (url.indexOf('?')>0) url += '&randomID=' + Math.random()*10000;
        else url += '?randomID=' + Math.random()*10000;
    }
   
    if (http) {
    httpWorking = true;
    if (history)
    {
        addHistoryData(historyData);
    }
    http.open('POST', url, true);
    http.onreadystatechange = handleHttpResponseDoc;
    http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    http.setRequestHeader("Content-length", postData.length);
    http.setRequestHeader("Connection", "close");
    http.send(postData);
        
    }
    else
    {
        alert('Impossibile effettuare l\'operazione richiesta');
    }
}
function loadPagePost(url, divID, uniqueCall, postData)
{
    if (httpWorking) 
    {
        return;
    }
    if (!document.getElementById(divID))
    {
        return;
    }
    targetDiv = divID;
    
    if (uniqueCall)
    {
        if (url.indexOf('?')>0) url += '&randomID=' + Math.random()*10000;
        else url += '?randomID=' + Math.random()*10000;
    }
   
    if (http) {
    httpWorking = true;

    http.open('POST', url, true);
    http.onreadystatechange = handleHttpResponse;
    http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    http.setRequestHeader("Content-length", postData.length);
    http.setRequestHeader("Connection", "close");
    http.send(postData);
    
    }
}



function addHistoryData(historyData)
{
    historyPageLoaded = true;
    historyFlag = true;
    frm = document.getElementById('historyFrame');
    frm.src = 'history.aspx?data=' + historyData;
}

function inizializzahistory()
{
    historyFlag = true;
}

function history(data)
{
    if(!historyPageLoaded && data!='parent.location.reload()') 
    {
        historyPageLoaded = true;
        historyFlag = false;
        httpWorking = false;
    }
    if (historyFlag)
    {
        historyFlag = false;
    }
    else
    {
        funzione = data.replace(/&amp;/g,'&');
        funzione = funzione.replace(/%20/g,' ');
        eval(funzione);
    }
}

function updatePage(url, data, endFunction)
{
    if (httpWorking) 
    {
        alert('E\' in corso un\' altra operazione. Attendere.');
        return;
    }
    targetFunction = endFunction;
    
    if (url.indexOf('?')>0) url += '&randomID=' + Math.random()*10000;
    else url += '?randomID=' + Math.random()*10000;
    
    if (http) {
    httpWorking = true;
    http.open('POST', url, true);
    http.onreadystatechange = handleHttpResponse2;
    http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    http.setRequestHeader("Content-length", data.length);
    http.setRequestHeader("Connection", "close");
    http.send(data);
    }
    else
    {
        alert('Impossibile effettuare l\'operazione richiesta');
    }

}



//////////////////
//Funzioni usate dalle altre precedenti

function handleHttpResponse() {
  if (http.readyState == 4) {
    document.getElementById(targetDiv).innerHTML=http.responseText;
    httpWorking = false;
    if (targetDiv == 'framebasso') 
        resizeWin();
    
  }
}

function handleHttpResponseRic() {
  if (http.readyState == 4) {
    document.getElementById('framebasso').innerHTML=http.responseText;
    httpWorking = false;
  }
}

function handleHttpResponseDoc() {
  if (http.readyState == 4) {
    var mydoc = http.responseText;
    pos1 = mydoc.indexOf('@@@@--div--@@@@');
    pos2 = mydoc.indexOf('@@@@--div--@@@@',pos1+1);
    testo1 = mydoc.substring(pos2+15);
    toolbar1 = mydoc.substring(pos1+15,pos2);
    titolo1 = mydoc.substring(0,pos1);

    posemb = testo1.indexOf('<EMBED');
    if (posemb>0)
    {
        emb = true;
    }
    else
    {
        emb=false;
    }
    
    content_parte = document.getElementById("contentparte");
    content_parte.innerHTML=testo1;
    content_parte.scrollTop = 0;
    
    document.getElementById("toolbar").innerHTML=toolbar1;
    document.getElementById("titoloProvvedimento").innerHTML=titolo1;

    // Gestione ancore (per esempio a un comma)
    pos3 = testo1.indexOf('<!--navigaAncora:');
    if (pos3>0)
    {
        pos4 = testo1.indexOf('-->',pos3);
        ancora = testo1.substring(pos3+17,pos4);
        comma = document.getElementById(ancora);
        if (comma) window.location.href='#'+ancora; //document.getElementById("contentparte").scrollTop = comma.scrollPos;
    }
    
    httpWorking = false;
    documentiCaricati++;
    
    // Gestione chiamata a funzioni javascript
    pos3 = testo1.indexOf('<!--@@@[EVAL]@@@');
    if (pos3>0)
    {
        pos4 = testo1.indexOf('-->',pos3);
        funzione = testo1.substring(pos3+16,pos4);
        eval(funzione);
    }
    ripristinaSommario();
  }
}

function handleHttpResponse2() {
  if (http.readyState == 4) {
    //document.getElementById(targetDiv).innerHTML= document.getElementById(targetDiv).innerHTML + ;
    httpWorking = false;
    //alert(targetFunction+'(\''+http.responseText+'\')');
    eval(targetFunction+'(\''+http.responseText+'\')');
  }
}

function getHTTPObject() {
  var xmlhttp;
  /*@cc_on
  @if (@_jscript_version >= 5)
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
        xmlhttp = false;
      }
    }
  @else
  xmlhttp = false;
  @end @*/
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
	  xmlhttp.overrideMimeType("text/xml"); 
	  //xmlhttp.overrideMimeType("text/html"); 
    } catch (e) {
      xmlhttp = false;
    }
  }
  return xmlhttp;
}




// visualizza calendario
function show_calendar(str_target, str_datetime, str_divId) {

    var vWin = document.getElementById(str_divId);
    
	var arr_months = ["Gennaio","Febbraio","Marzo","Aprile","Maggio","Giugno","Luglio","Agosto","Settembre","Ottobre","Novembre","Dicembre"];
	var week_days = ["Dom","Lun","mar","Mer","Gio","Ven","Sab"];
	var n_weekstart = 1; // day week starts from (normally 0 or 1)

	var dt_datetime = (str_datetime == null || str_datetime =="" ?  new Date() : str2dt(str_datetime));
	var dt_prev_month = new Date(dt_datetime);
	dt_prev_month.setMonth(dt_datetime.getMonth()-1);
	var dt_next_month = new Date(dt_datetime);
	dt_next_month.setMonth(dt_datetime.getMonth()+1);
	var dt_firstday = new Date(dt_datetime);
	dt_firstday.setDate(1);
	dt_firstday.setDate(1-(7+dt_firstday.getDay()-n_weekstart)%7);
	var dt_lastday = new Date(dt_next_month);
	dt_lastday.setDate(0);
	
	// html generation (feel free to tune it for your particular application)
	// print calendar header
	var str_buffer = new String (
		"<table cellspacing=\"0\" border=\"0\" width=\"100%\">\n"+
		"<tr><td bgcolor=\"#4682B4\">\n"+
		"<table cellspacing=\"1\" cellpadding=\"3\" border=\"0\" width=\"100%\">\n"+
		"<tr>\n	<td bgcolor=\"#4682B4\"><a href=\"javascript:show_calendar('"+
		str_target+"', '"+ dt2dtstr(dt_prev_month)+"','"+str_divId+"');\">"+
		"<img src=\"images/prev.gif\" width=\"16\" height=\"16\" border=\"0\""+
		" alt=\"previous month\"></a></td>\n"+
		"	<td bgcolor=\"#4682B4\" align=\"center\" colspan=\"5\">"+
		"<font color=\"white\" face=\"tahoma, verdana\" size=\"2\">"
		+arr_months[dt_datetime.getMonth()]+" "+dt_datetime.getFullYear()+"</font></td>\n"+
		"	<td bgcolor=\"#4682B4\" align=\"right\"><a href=\"javascript:show_calendar('"
		+str_target+"', '"+dt2dtstr(dt_next_month)+"','"+str_divId+"');\">"+
		"<img src=\"images/next.gif\" width=\"16\" height=\"16\" border=\"0\""+
		" alt=\"next month\"></a></td>\n</tr>\n"
	);

	var dt_current_day = new Date(dt_firstday);
	var oggi = new Date();
	// print weekdays titles
	str_buffer += "<tr>\n";
	for (var n=0; n<7; n++)
		str_buffer += "	<td bgcolor=\"#87CEFA\">"+
		"<font color=\"white\" face=\"tahoma, verdana\" size=\"2\">"+
		week_days[(n_weekstart+n)%7]+"</font></td>\n";
	// print calendar table
	str_buffer += "</tr>\n";
	while (dt_current_day.getMonth() == dt_datetime.getMonth() ||
		dt_current_day.getMonth() == dt_firstday.getMonth()) {
		// print row heder
		str_buffer += "<tr>\n";
		for (var n_current_wday=0; n_current_wday<7; n_current_wday++) {
				if (dt_current_day.getDate() == oggi.getDate() &&
					dt_current_day.getMonth() == oggi.getMonth())
					// print current date
					str_buffer += "	<td bgcolor=\"#FFB6C1\" align=\"right\">";
				else if (dt_current_day.getDay() == 0 || dt_current_day.getDay() == 6)
					// weekend days
					str_buffer += "	<td bgcolor=\"#DBEAF5\" align=\"right\">";
				else
					// print working days of current month
					str_buffer += "	<td bgcolor=\"white\" align=\"right\">";

				if (dt_current_day.getMonth() == dt_datetime.getMonth())
					// print days of current month
					str_buffer += "<a href=\"javascript:"+str_target+
					".value='"+dt2dtstr(dt_current_day)+"'; closeDivDate('"+str_divId+"');\">"+
					"<font color=\"black\" face=\"tahoma, verdana\" size=\"2\">";
				else 
					// print days of other months
					str_buffer += "<a href=\"javascript:"+str_target+
					".value='"+dt2dtstr(dt_current_day)+"'; closeDivDate('"+str_divId+"');\">"+
					"<font color=\"gray\" face=\"tahoma, verdana\" size=\"2\">";
				str_buffer += dt_current_day.getDate()+"</font></a></td>\n";
				dt_current_day.setDate(dt_current_day.getDate()+1);
		}
		// print row footer
		str_buffer += "</tr>\n";
	}
	// print calendar footer
	str_buffer +=
		"</table>\n" +
		"</tr>\n</td>\n</table>\n";

	//var vWinCal = window.open("", "Calendar", 
	//	"width=250,height=250,status=no,resizable=yes,top=200,left=200");
	//vWinCal.opener = self;
	//var calc_doc = vWinCal.document;
	//calc_doc.write (str_buffer);
	//calc_doc.close();
	
	// Show Calendar
	
	vWin.style.width = 210;
	//vWin.style.top = 10;
	vWin.innerHTML=str_buffer;
	
	   vWin.style.visibility='visible';
}




function closeDivDate(str_id)
{
   document.getElementById(str_id).style.visibility='hidden';
}

// datetime parsing and formatting routimes. modify them if you wish other datetime format
function str2dt (str_datetime) {
    vals = str_datetime.split("/");
	return (new Date (vals[2], vals[1]-1, vals[0], 0, 0, 0));
}
function dt2dtstr (dt_datetime) {
	return (new String (
			dt_datetime.getDate()+"/"+(dt_datetime.getMonth()+1)+"/"+dt_datetime.getFullYear()));
}
function dt2tmstr (dt_datetime) {
	return (new String (
			dt_datetime.getHours()+":"+dt_datetime.getMinutes()+":"+dt_datetime.getSeconds()));
}

/////////////////////////////////////////////////////////////////////////////////


function aggiornaQuesitiDisponibili(disp,cons,tot)
{
    document.getElementById('numeroquesitidiposnibili').innerHTML = '<strong>' +disp + '</strong>';
    document.getElementById('numeroquesitiutilizzati').innerHTML = '<strong>' +cons + '</strong>';
    document.getElementById('numeroquesitotale').innerHTML = '<strong>' +tot + '</strong>';
}
