
var gVerifBaseDir = "verifBaseDir";
var gVerifMod = "verifMod";
var gVerifExplMod = "verifExplMod";
var gFicBaseDir = "ficBaseDir";
var gJoDate = "joDate";
var gNotVerif = "notVerif";
var explicationActive = 1;

function URLDecode( variable )
{
   // Replace + with ' '
   // Replace %xx with equivalent character
   // Put [ERROR] in output if %xx is invalid.
   var HEXCHARS = "0123456789ABCDEFabcdef"; 
   var encoded = variable;
   var plaintext = "";
   var i = 0;
   while (i < encoded.length) {
       var ch = encoded.charAt(i);
	   if (ch == "+") {
	       plaintext += " ";
		   i++;
	   } else if (ch == "%") {
			if (i < (encoded.length-2) 
					&& HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 
					&& HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
				plaintext += unescape( encoded.substr(i,3) );
				i += 3;
			} else {
				alert( 'Bad escape combination near ...' + encoded.substr(i) );
				plaintext += "%[ERROR]";
				i++;
			}
		} else {
		   plaintext += ch;
		   i++;
		}
	} // while
   return plaintext;
};

function getQueryVariable ( variable )
{
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for( var i=0 ; i<vars.length ; i++ )
    {
        var pair = vars[i].split("=");
        if (pair[0] == variable)
        {
            return URLDecode(pair[1]);
        }
    }

    //alert('Query Variable ' + variable + ' not found');
    return "";
}

function loadPage(url)
{
    var basedir = getQueryVariable(gVerifModDir);
    var theUrl = basedir + "/" + url;

    top.load.location = theUrl;
    //window.open( theUrl );
}

function verifySignature( filename )
{
    var verifBaseDir = getQueryVariable(gVerifBaseDir);
    var fileBaseDir = getQueryVariable(gFicBaseDir);
    if (fileBaseDir == "")
	{
	    fileBaseDir = vFicBaseDir;
    }

    var nothing = top.data.document.formdata.nothing.value;
	//alert("nothing: " + nothing);	

	if (nothing > 0)
	{
        // Non-IE browser without Java support: open the PDF without verification. This method
		// must be called from the HTML onclick handler to by-pass pop-up blockers.
        window.open(verifBaseDir + "/getpdf.php?fic=" + fileBaseDir + "/" + filename);
        return false;
    }

    var verifModule     = getQueryVariable(gVerifMod);
    var verifModuleExpl = "";
    var filenameExpl = filename;
    var notVerif = getQueryVariable(gNotVerif);
    if (notVerif=="")
    {
	    notVerif=vNotVerif;
    }
    
    var showExplication = notVerif==0 && explicationActive==1 && top.data.document.formdata.explicationShown.value=='0';
    
    var vTryApplet = top.data.document.formdata.vTryApplet.value;
    var vTryActiveX = top.data.document.formdata.vTryActiveX.value;

    var os = top.data.document.formdata.osDectected.value;
    var jvm = top.data.document.formdata.jvmDectected.value;
    var nav = top.data.document.formdata.navDectected.value;

    if (showExplication)
    {
    	verifModuleExpl = getQueryVariable(gVerifExplMod);
    }

    var joDate			= getQueryVariable(gJoDate);
    if (joDate=="")
    {
	    joDate=vJoDate;
    }
    
	var parts = filename.split("_");
	var sommairePage="sommaire.php";
	var recherchePage="user.php";
	if (parts[0]=="dae")
	{
		sommairePage="sommaire_dae.php";
		recherchePage="dae_milieu.php";
		var mmjj = parts[1].substring(4);
		fileBaseDir = fileBaseDir+"/"+mmjj;
	}

	filename = fileBaseDir + "/" + filename;

//alert("filename:"+filename);
//alert("fileBaseDir:"+fileBaseDir);
//alert("verifBaseDir:"+verifBaseDir);

    var theUrl = verifBaseDir + "/" + verifModule 
    	+ "?" + "fic=" + filename 
    	+ "&basedir=" + fileBaseDir 
    	+ "&joDate=" + joDate 
    	+ "&vTryApplet=" + vTryApplet 
    	+ "&vTryActiveX=" + vTryActiveX 
    	+ "&nothing=" + nothing 
    	+ "&os=" + os 
    	+ "&jvm=" + jvm 
    	+ "&nav=" + nav ;
//alert(theUrl);

//    top.load.location.href = theUrl;
//    top.milieu.cont.location.href = theUrl;
	if (showExplication)
	{
	    var bandeauURL = verifBaseDir + "/bandeau_attente.php" 
    	+ "?" 
    	+ "&basedir=" + fileBaseDir 
    	+ "&joDate=" + joDate 
    	+ "&sommairePage=" + sommairePage
    	+ "&recherchePage=" + recherchePage
	    top.milieu.up.location.href = bandeauURL;
		var explURL = verifBaseDir + "/" + verifModuleExpl
		+ "?" + "fic=" + filenameExpl
		+ "&basedir=" + fileBaseDir
		+ "&joDate=" + joDate
    	+ "&vTryApplet=" + vTryApplet
    	+ "&vTryActiveX=" + vTryActiveX 
    	+ "&nothing=" + nothing 
    	+ "&os=" + os 
    	+ "&jvm=" + jvm 
    	+ "&nav=" + nav ;
		// changement de la page d'explication
	    top.milieu.cont.location.href = explURL;
	    
	    top.load.location.href = theUrl;
	}
	else
	{
		top.load.location.href = theUrl;
	}
    //window.open( theUrl );
}

