// 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/08/16 17:05:54 $
//
//   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.8 2006/08/16 17:05:54 christian Exp $
$Revision: 1.8 $

*/

function ECI_vi(
	instanceName,
	href,
	html,
	title,
	arg,
	sep1,
	val1,
	val2,
	sep2,
	tar,
	imp,
	ret,
	logo,
	logoSrc,
	logoWidth,
	logoHeight
)
{
	this.instanceName = instanceName;
	this.href = (String(href) != 'undefined') ? href : '';
	this.html = (String(html) != 'undefined') ? html : '<a href="javascript:'+instanceName+'.goImp()">Version imprimable</a>';
	this.title = (String(title) != 'undefined') ? title : ' - Version imprimable';
	this.arg = (String(arg) != 'undefined') ? arg : 'vi';
	this.sep1 = (String(sep1) != 'undefined') ? sep1 : '=';
	this.val1 = (String(val1) != 'undefined') ? val1 : '1';
	this.val2 = (String(val2) != 'undefined') ? val2 : '0';
	this.sep2 = (String(sep2) != 'undefined') ? sep2 : '&';
	this.tar = (String(tar) != 'undefined') ? tar : 'self';
	this.imp = (String(imp) != 'undefined') ? imp : 'Impression';
	this.ret = (String(ret) != 'undefined') ? ret : 'Retour &agrave; la version &eacute;cran';
	this.logo = (String(logo) != 'undefined') ? logo : null;
	this.logoSrc = (String(logo) != 'undefined') ? logoSrc : null;
	this.logoWidth = (String(logoWidth) != 'undefined') ? logoWidth : null;
	this.logoHeight = (String(logoHeight) != 'undefined') ? logoHeight : null;

	this.args = this.getGET(this.sep1, this.sep2, this.val1);
	if (typeof this.args[this.arg] == "string" && this.args[this.arg] == this.val1)
	{
		if (document.title) { document.title += this.title; }
		document.writeln('<link ');
		document.write('rel="stylesheet" ');
		document.write('type="text/css" ');
		document.write('href="' + this.href + '" ');
		document.write('/>');
		if (window.scroll)
		{
			this.old_onload = null;
			if (typeof window.onload == "function")
			{
				this.old_onload = window.onload;
			}
			window.ECI_vi_instanceName = this.instanceName;
			window.onload = function()
			{
				var instance = eval(this.ECI_vi_instanceName);
				if (instance.old_onload)
				{
					instance.old_onload();
					instance.old_onload = null;
				}
				instance.linksNoClick("A");
				instance.bandeau(
					eval(instance.logo),
					instance.logoSrc,
					instance.logoWidth,
					instance.logoHeight
				);
				setTimeout('window.scroll(0,0)',500);
			}
		}
	}
}
ECI_vi.prototype.bandeau = function(img, src, width, height)
{
	if (img && src)
	{
		img.src = src;
		if (width) img.style.width = width + 'px';
		if (height) img.style.height = height + 'px';
	}
}
ECI_vi.prototype.ecritBoutonImpression = function() { document.writeln(this.html); }
ECI_vi.prototype.getGET = function(s1, s2)
{
	var args = {};
	// 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);
		}
	}
	return args;
}
ECI_vi.prototype.goImp = function(argn, se1, value, se2, targ)
{
	if (String(argn) == "undefined") { argn = this.arg; }
	if (String(se1) == "undefined") { se1 = this.sep1; }
	if (String(value) == "undefined") { value = this.val1; }
	if (String(se2) == "undefined") { se2 = this.sep2; }
	if (String(targ) == "undefined") { targ = this.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;
	}
}
ECI_vi.prototype.ecritNav = function(imp, ret, onclk)
{
	if (String(imp) == 'undefined') imp = this.imp;
	if (String(ret) == 'undefined') ret = this.ret;
	if (typeof this.args[this.arg] == "string" && this.args[this.arg] == this.val1)
	{
		if (String(onclk) == "undefined") { onclk = this.instanceName + '.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.writeln(n);
	}
}
ECI_vi.prototype.retourEcran = function(targ)
{
	//history.go(-1);
	if (String(targ) == "undefined") { targ = this.tar; }
	var s = eval('/(\\?|\\' + this.sep2 + ')' + this.arg + this.sep1 + this.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
	{
		this.goImp(this.arg, this.sep1, this.val2, this.sep2);
	}
}
ECI_vi.prototype.linksNoClick = function(el)
{
	var a = null;
	var isIE5Mac = (!window.showModelessDialog && window.ActiveXObject && document.getElementById);
	if (document.getElementsByTagName) { a = document.getElementsByTagName(el); }
	else if (document.all) { a = document.all.tags(el); }
	else if (document.links) { a = document.links; }
	if (!a) return;
	for (var i = 0; i < a.length; i++)
	{
		a[i].onclick = function()
		{
			this.blur();
			window.status = '';
			return false;
		}
		a[i].onmouseover = function() { window.status = ''; return true; }
		if (!isIE5Mac && a[i].style) { a[i].style.cursor = 'default'; }
	}
}
