
// copyright - begin
//
//   Copyright (C) 2005 Espace Courbe inc.
//   You are free to modify and redistribute this code as long
//   as the following message is included:
//
//   Last modified:
//   $Date: 2006/06/05 19:34:53 $
//
//   Portions Copyright (C) 2006 Espace Courbe inc.
//   Home Page: http://www.espacecourbe.com/
//
//   Author: Christian Fecteau -- christian.fecteau@espacecourbe.com
//   Creation:  2005/12/06 17:05:02
//
// copyright - end

/*

$Id: impression.js,v 1.1 2006/06/05 19:34:53 patrick Exp $
$Revision: 1.1 $

*/

function ecritBoutonImpression()
{
	if (typeof vi_html == 'string') { document.writeln(vi_html); }
}

function getGET(s1, s2)
{
	// retrieve arguments from location
	if (location.search)
	{
		var query = unescape(location.search.substring(1));
		var pairs = query.split(s2);
		for (var i = 0; i < pairs.length; i++)
		{
			var pos = pairs[i].indexOf(s1);
			if (pos == -1) { continue; }
			var argname = pairs[i].substring(0,pos);
			var value = pairs[i].substring(pos+1);
			args[argname] = String(value);
		}
	}
}

function goImp(argn, se1, value, se2, targ)
{
	if (String(argn) == "undefined") { argn = arg; }
	if (String(se1) == "undefined") { se1 = sep1; }
	if (String(value) == "undefined") { value = val1; }
	if (String(se2) == "undefined") { se2 = sep2; }
	if (String(targ) == "undefined") { targ = tar; }
	var
	t = eval('window.' + targ),
	v = argn + se1 + value,
	c = argn + se1,
	w = eval("(t.location.href).replace(/" + se2 + "*$/,'')"),
	q = w.match(/\?[^#]*/),
	p = eval('/' + c + '[^' + se2 + ']*/g'),
	h = w.match(/#.*/);
	if (t.focus) t.focus();
	if (w.indexOf(c) != -1)
	{
		t.location.href = w.replace(p, v);
	}
	else if (q)
	{
		q = q[0] + se2 + v;
		t.location.href = w.replace(/\?[^#]*/, q);
	}
	else if (w.indexOf('#') != -1)
	{
		w = w.replace(/#.*/,'');
		t.location.href = w + '?' + v + h[0];
	}
	else
	{
		t.location.href = w + '?' + v;
	}
}

function ecritNav(imp, ret, onclk)
{
	if (typeof args[arg] == "string" && args[arg] == val1)
	{
		if (String(onclk) == "undefined") { onclk = 'retourEcran()'; }
		var n = '<div id="printbuttons"><form action="">';
		if (window.print)
		{
			n += '<input onclick="window.print()" ';
			n += 'value="'+imp+'" type="button">&nbsp;&nbsp;';
		}
		n += '<input onclick="'+onclk+'" value="'+ret+'" type="button" ';
		n += '/></form><hr /></div>';
		document.write(n);
	}
}

function retourEcran(targ)
{
	//history.go(-1);
	if (String(targ) == "undefined") { targ = tar; }
	var s = eval('/(\\?|\\' + sep2 + ')' + arg + sep1 + val1 + '/g');
	var t = eval('window.' + targ);
	var l = t.location.href;
	if (l.match(s))
	{
		eval("location.href = (t.location.href).replace(" + s + ",'')");
	}
	else
	{
		goImp(arg, sep1, val2, sep2);
	}
}

function checkVars()
{
	if (
		typeof href != 'undefined' &&
		typeof title != 'undefined' &&
		typeof Imp != 'undefined' &&
		typeof Ret != 'undefined'
	)
	{
		return true;
	}
	return false;
}


var args = {};

if (checkVars())
{
	if (typeof arg == 'undefined') arg = 'vi';
	if (typeof sep1 == 'undefined') sep1 = '=';
	if (typeof val1 == 'undefined') val1 = '1';
	if (typeof val2 == 'undefined') val2 = '0';
	if (typeof sep2 == 'undefined') sep2 = '&';
	if (typeof tar == 'undefined') tar = 'self';
	getGET(sep1, sep2, val1);
	if (typeof args[arg] == "string" && args[arg] == val1)
	{
		if (document.title) document.title += title;
		document.writeln('<link ');
		document.writeln('rel="stylesheet" ');
		document.writeln('type="text/css" ');
		document.writeln('charset="utf-8" ');
		document.writeln('href="' + href + '" ');
		document.writeln('/>');
		if (typeof window.onload == "function" && window.scroll)
		{
			var impressionEC_old_onload = window.onload;
			window.onload = function()
			{
				if (impressionEC_old_onload)
				{
					impressionEC_old_onload();
					impressionEC_old_onload = null;
				}
				setTimeout('window.scroll(0,0)',500);
			}
		}
	}
}

