// 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, strClass,strNom)
{
	
	if(strNom == undefined)
	document.write('<a class="' + strClass + '" hre' + 'f="mai' + 'lto:' + strAddress +'@' + strUrl + '">' + strAddress +'@' + strUrl +'</a>') ;
	else
	document.write('<a class="' + strClass + '" hre' + 'f="mai' + 'lto:' + strAddress +'@' + strUrl + '">' + strNom +'</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=500,status=0, scrollbars=1,toolbar=0,menubar=0,location=0, resizable=1;");
	win.document.open();
	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=470 cellpadding=5 cellspacing=5 border=0>') ;
	win.document.write('<tr><td height=71><img src="/template/themes/');
	win.document.write(theme);
	win.document.write('/image/logoPrint.gif">&nbsp;&nbsp;&nbsp;&nbsp;<img src="/template/image/print.gif" onclick="window.print();" style="cursor:hand"><hr color=#000000 width=450></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();

	//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="70"><img src="/template/themes/' + theme + '/image/logoPrint.gif"><hr color="#000000" width="100%"></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();
}

/******** Ajoute la fonction trim comme méthode de l'objet String. ***************/
String.prototype.trim = function()
{ return this.replace(/(^\s*)|(\s*$)/g, ""); }


/*Fct qui check les champs obligatoires d'un formulaire rempli pas l'utilisateur*/
function check_custom_form(form, msgError)
{
 var isOk = true;
 var ctrl;
 for(i=0;i<form.length;i++)
 {
    ctrl = form[i];
    if(ctrl.name.indexOf("ok_") == 0)
    {
        if(ctrl.type == "checkbox" || ctrl.type == "radio")
        {
           if(!ctrl.checked)
           {
              isOk = false;
      		  break;	        
           }
        }
        else if(ctrl.value.trim() == "")
        {
          isOk = false;
          break;
        }
    }
 }
  
 if(!isOk)
     alert(msgError.replace("%s",ctrl.name.substr(3)));
  
  return isOk;
}


 	//Fonction pour tester l'email et champs obligatoires
	function checkMail(email)
	{
		return email.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.biz)|(\.info)|(\.gov)|(\.org)|(\..{2,2}))$)\b/gi);
	}