// pop-up function

var newWin = null;

function closeWin()
{
	if (newWin != null)
	{
		if(!newWin.closed)
			newWin.close();
	}
}


function popUp(strURL,strType,strWidth,strHeight)
{
	closeWin();
	
	var strOptions="";
	if (strType=="console") 
		strOptions="resizable,height="+strHeight+",width="+strWidth;

	if (strType=="fixed") 
		strOptions="status,height="+strHeight+",width="+strWidth;

	if (strType=="elastic") 
		strOptions="toolbar,menubar,scrollbars,resizable,location,height="+strHeight+",width="+strWidth;

	if (strType=="scroll") 
		strOptions="scrollbars,resizable,height="+strHeight+",width="+strWidth;

	newWin = window.open(strURL, 'newWin', strOptions);

	newWin.focus();
}

function buildMail(strAddress, strUrl)
{
	document.write('<a hre' + 'f="mai' + 'lto:' + strAddress +'@' + strUrl + '">' + strAddress +'@' + strUrl +'</a>') ;
}

/*Fonction qui renvoie le HTML du contenu pour impression et PDF
	IL faut bien indiquer dans le main le "<!-- STARTPAGEPRINT -->" et le "<!-- ENDPAGEPRINT -->" autour du contenu 
*/
function getPrintableHTML()
{
	//Constante de commentaire
	var startPrintMark = "<!-- STARTPAGEPRINT -->" ;
	var endPrintMark = "<!-- ENDPAGEPRINT -->" ;
	var startPrintMark_icone = "<!-- STARTPAGEPRINT_ICONE -->" ;
	var endPrintMark_icone = "<!-- ENDPAGEPRINT_ICONE -->" ;
	
	var source = document.body.innerHTML ;
	var sourceSuite = document.body.innerHTML ;
	source = source.substr(source.indexOf(startPrintMark) + startPrintMark.length) ;
	source = source.substr(0,source.indexOf(startPrintMark_icone)) ;
	sourceSuite = sourceSuite.substr(sourceSuite.indexOf(endPrintMark_icone) + endPrintMark_icone.length) ;
	sourceSuite = sourceSuite.substr(0, sourceSuite.indexOf(endPrintMark)) ;
	source = source + sourceSuite ;
	source = source.replace(/’/gi,"'"); //Remplacement des apostrophes pas standard
	
	return source;
}

/*Fonction qui permet d'imprimer une page*/
function doPrint(theme)
{
	//Recupere le html à imprimer
	var source = getPrintableHTML();

	//Creer la popup
	var win = window.open("",'',"height=500,width=650,status=0, scrollbars=1,toolbar=0,menubar=0,location=0, resizable=1;");
	win.document.write('<html><head><title>Print</title><LINK href="/template/themes/');
	win.document.write(theme);
	win.document.write('/style/styles.css" rel="stylesheet"></head>');
	win.document.write('<body>') ;
    win.document.write('<table width=630 cellpadding=5 cellspacing=0 border=0>') ;
    win.document.write('<tr><td height=77 background="/template/themes/');
    win.document.write(theme);
    win.document.write('/image/logoprint.gif" align=right valign=bottom><img src="/template/image/print.gif" onclick="window.print();" style="cursor:hand">&nbsp;&nbsp;&nbsp;&nbsp;</td></tr>') ;
	win.document.write(source) ;
    win.document.write('</table>') ;
	win.document.write('</body></html>') ;
	win.document.close();
}


/*Fonction qui permet de lancer la génération d'un pdf
   Attention: il faut mettre un <div style="display:none" id="divPDF"></div> au fond du tpl Main
   Il faut déclarer les noms de domaine dans le web.config de l'applic .NET
*/
function doPDF(theme)
{
	//Constante
	var urlPDF = "http://pdf.conchita-plus.com/topdf.aspx";

	//Recupere le html à imprimer
	var source = getPrintableHTML();
	source = source.replace(/<form/gi, "<div");
	source = source.replace(/<\/form/gi, "</div");
	
	
	//Recuperation du host
    var host = document.location.href;
    var idx = host.indexOf("/",8); //index du premier slash après le "https://"
    host = host.substr(0, idx+1); //je prends le dernier slash

    //Recuperation du nom de la page
    var pdfName = document.location.href;
    var idxStart = pdfName.lastIndexOf("/")+1;
    var idxEnd = pdfName.lastIndexOf(".");
    pdfName = pdfName.substr(idxStart, idxEnd-idxStart); //je prends le dernier slash
    if(pdfName=="")
    	pdfName = "index";

   	//Création du formulaire
	var html = '<form id="frmPDF" method="POST" action="' + urlPDF + '"><textarea name="htmlContent">';
    html += '<html><head><BASE href="' + host + '"><META http-equiv="Content-type" content="text/html; charset=ISO-8859-1">';
    html += '<LINK href="/template/themes/' + theme + '/style/styles.css" rel="stylesheet"></head>';
	html += '<body>';
    html += '<table width="100%" cellpadding=0 cellspacing=0 border=0>';
    html += '<tr><td height="77"><img src="/template/themes/' + theme + '/image/logoprint.gif"></td></tr>';
	html += source;
    html += '</table>';
    html += '</body></html>' ;
    html += '</textarea><input type="hidden" name="htmlHost" value="' + host + '"><input type="hidden" name="pdfName" value="' + pdfName + '"></form>';
    
    //Ecrire le form dans le div du Main
    var myDiv = document.getElementById("divPDF");
    myDiv.innerHTML = html;
    
    
    //Poste le formulaire
    document.getElementById("frmPDF").submit();
}

