var is_ie/*@cc_on = {
  version : parseFloat(navigator.appVersion.match(/MSIE (.+?);/)[1])
}@*/;

var box = null; // object
var initialized = false;

if (document.implementation && document.implementation.createDocument) 
 var isMozilla=true;
 else var isMozilla=false;

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

var showBox = false;

function hideProgressive() 
{
  reachHeight = (clipHeight >= 0);
  reachWidth = (clipWidth <= finalWidth);
  
  if (reachHeight || reachWidth)
  {
    if (reachHeight)
		clipHeight -= 10;

    if (reachWidth)
		clipWidth += Math.round(10 * ratio);
	
	box.style.clip = "rect(0px," + finalWidth + "px," + clipHeight + "px," + clipWidth + "px)";
    setTimeout(hideProgressive,1); 
  }
 
}

function showProgressive() {

	box.style.visibility = "visible";

	reachHeight = (clipHeight < finalHeight);
	reachWidth = (clipWidth > 0);
  
  if (reachHeight || reachWidth)
  {
    if (reachHeight)
		clipHeight += 10;

    if (reachWidth)
		clipWidth -= Math.round(10 * ratio);
	
	box.style.clip = "rect(0px," + finalWidth + "px," + clipHeight + "px," + clipWidth + "px)";
    setTimeout(showProgressive,1); 
  }
  
}

function init() 
{
  
	box = document.getElementById('requestBox'); 
	generaPDF = document.getElementById('GeneraPDF');
	XY = findPos(generaPDF);
	
	finalWidth = box.offsetWidth;
	finalHeight = box.offsetHeight;
	
	ratio = finalWidth / finalHeight;
	
	X1 = XY[0] - finalWidth + generaPDF.offsetWidth;
	Y1 = XY[1] + 47;
	box.style.left = X1;
	box.style.top = Y1;
	
	//box.style.clip = "rect(0px," + finalWidth + "px,0px,0px)";
	clipHeight = 0;
	clipWidth = finalWidth;	
	initialized = true;
}

function toggleClip()
{
	if (!initialized) {
		init();
	}
	
	if (showBox) {
		hideProgressive(); 
		showBox = false;
	} else {	

		showProgressive(); 
		showBox = true;
		window.scrollTo(XY[0],XY[1]-47);
		}
}



sPDFUrl="/pdf/pdf2.php";
sPDFUrl2="/pdf/pdf3.php";
PDFDiv =  "requestBoxInside";
origHTML = "";


function PDFadjustMessage() {
	var MessageObj=document.getElementById(PDFDiv);
	origHTML = MessageObj.innerHTML;
	MessageObj.innerHTML = '<br><div align=\"center\">Stiamo processando la Vostra richiesta... <br><br><br><img src="/chart/ajax-loader.gif"/><br><br><br></div>';	
}

function restoreClip() {
	var MessageObj=document.getElementById(PDFDiv);
	MessageObj.innerHTML = origHTML;
	toggleClip();
}


function PDFShowMessage(json) {
	var MessageObj=document.getElementById(PDFDiv);
	var respondStructure = eval( '(' + json + ')' ); 
	
	popupBlocked = "<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\" width=\"100%\">";
	popupBlocked += "<tr><td align=\"right\"><a href=\"#\" onclick=\"restoreClip(); return false;\">";
	popupBlocked += "<img src=\"http://www.soprarno.it/picture/chiudi.gif\" border=\"0\" alt=\"Chiudi\"/></a></td>";
	popupBlocked += "</tr></table><br>";
	popupBlocked += "<div align=\"center\">Se il download non si avvia automaticamente,<br>potreste avere un <strong>blocco popup</strong> attivo.<br>";
	popupBlocked += "<br>Potete comunque <strong>scaricare il file</strong><br>cliccando sull'<strong>icona sottostante</strong><br><br><br>";
	popupBlocked += "<a href=\""+respondStructure.url+"\" onclick=\"setTimeout('restoreClip()',5000);\" >";
	popupBlocked += "<img src=\"/img/icons/pdf.jpg\" border=\"0\" alt=\"Scarica il file PDF\"/></a><br><br><br></div>";
	
	if (respondStructure.ErrorMessage != "") {
		MessageObj.innerHTML = origHTML;
		alert(respondStructure.ErrorMessage);	
	} else if (respondStructure.url != "") {
		MessageObj.innerHTML = popupBlocked;
		var pdfWin = window.open(respondStructure.url,'pdfWin','width=800,height=600,menubar=0,toolbar=1,status=0,scrollbars=1,resizable=1');
		if (!(is_ie && (is_ie.version >= 7)) && pdfWin) { 
			restoreClip();
		}
	}
	return false;
}

var handleSuccessPDF = function(o){
	if(o.responseText !== undefined){
		PDFShowMessage(o.responseText);
	}
};

var handleFailurePDF = function(o){
	if(o.responseText !== undefined){
		PDFShowMessage("{'url':'', 'ErrorMessage' : 'Errore di connessione'}",PDFDiv);
	}
};

var callbackPDF =
{
  success:handleSuccessPDF,
  failure:handleFailurePDF,
  argument:['foo','bar']
};


function getPDF2(mese,intestazione,email,consenso){
		requestBody = "mese=" + encodeURIComponent(mese); 
		requestBody += "&Intestazione=" + encodeURIComponent(intestazione); 
		requestBody += "&Email="+ encodeURIComponent(email); 
		requestBody += "&consenso="+ encodeURIComponent(consenso); 
		
		var request = YAHOO.util.Connect.asyncRequest('POST', sPDFUrl2, callbackPDF, requestBody);
		
		PDFadjustMessage();
}




function getPDF(fondo,tipo,intestazione,email,consenso){
/*	try
	{
*/
		requestBody = "fondo=" + encodeURIComponent(fondo); 
		requestBody += "&tipo=" + encodeURIComponent(tipo); 
		requestBody += "&Intestazione=" + encodeURIComponent(intestazione); 
		requestBody += "&Email="+ encodeURIComponent(email); 
		requestBody += "&consenso="+ encodeURIComponent(consenso); 
		
		var request = YAHOO.util.Connect.asyncRequest('POST', sPDFUrl, callbackPDF, requestBody);
		
		PDFadjustMessage();

/*		
	}
		catch(e)
	{
		return;
	}
*/
}
