// Ajouter par Arnaud  Impression et redirection
//RUBEN
function MyPrint()
{
	window.print();
	//window.location = "index.php?id=12&L=0";
}
function GoBack()
{
	window.history.back(1); //KCH utiliser cette technique avec prudence, si vient de google, renvoie sur google
	//window.location = "index.php?id=12";
}

// Ces deux fonctions sont ajoutées par l'équipe Inno le 19/10/2010
//Ces deux fonctions vont permettre d'affichier et de fermer l'image de l'article
//
function Afficheimage(refimage)
{
	//
	var elemsave = document.getElementById('savedivrefimage');
	if(elemsave.value!=0){
		Cacheimage(elemsave.value);
	}
	
	var elem = document.getElementById(refimage);
	elemsave.value = refimage;
	elem.style.left = 100;
} 
function Cacheimage(refimage)
{
	var elem = document.getElementById(refimage);
	elem.style.left = -1000;
}
// Fin ajout
//
////////////////////////////////////

function switchMenu()
{
	var el = document.getElementById('t3menu');
	
	if(el)
	{
		el.style.visibility = (el.style.visibility == 'hidden' ? 'visible' : 'hidden');
	}
}

function CForm(data)
{
	if(data['fields'] != null && data['fields'].length > 0)
	{
		this._formats = new Array();
		var id = new Array();
		for(var x = 0; x < data['fields'].length; x++)
		{
			id.push(data['fields'][x]['id']);
			this._formats[data['fields'][x]['id']] = {
				format : data['fields'][x]['id']['format']
			}

			this.setFormat(data['fields'][x]['id'], data['fields'][x]['format']);
		}
	}
}

CForm.prototype._formats;

CForm.prototype.setFormat = function(id, exemple)
{
	var el = null;

	if((typeof(id)).toLowerCase() == 'object')
	{
		el = id;
		id = el.id;
	}
	else
	{
		var el = document.getElementById(id);
	}

	if (el != null)
	{
		var inputExemple = el.cloneNode(false);
		inputExemple.name = inputExemple.name + '_exemple';
		inputExemple.id = inputExemple.id + '_exemple';
		inputExemple.className = (inputExemple.className != null && inputExemple.className != '' ? inputExemple.className + ' js_exemple' : 'js_exemple');
		inputExemple.value = exemple;

		inputExemple.onfocus = function()
		{
			inputExemple.style.display = 'none';
			el.style.display = 'inline';
			el.focus();
		}

		el.onblur = function()
		{
			if (el.value == null || el.value == '')
			{
				inputExemple.style.display = 'inline';
				el.style.display = 'none';
			}
		}

		el.parentNode.appendChild(inputExemple);

		if(el.value == null || el.value == '')
		{
			inputExemple.style.display = 'inline';
			el.style.display = 'none';
		}
		else
		{
			inputExemple.style.display = 'none';
			el.style.display = 'inline';
		}

		return true;
	}
}

function refreshCreateMvt()
{
	var synchro = document.getElementById('synchro_caisse');
	var synchroMontant = document.getElementById('synchro_montant');
	var synchroDeltaNeg = document.getElementById('synchro_delta_neg');
	var synchroDeltaPos = document.getElementById('synchro_delta_pos');
	var paymode = document.getElementById('paymode');
	
	if(synchro != null && synchroMontant != null && synchroDeltaNeg!= null && synchroDeltaPos != null && paymode != null)
	{
		var amount = document.getElementById('amount').value;
		var action = document.getElementById('action').value;	
		
		if(action == '+' || action == '-')
		{
			if(action == '+') 
			{
				paymode.value = document.getElementById('paymodePlus').value;
				synchroDeltaPos.checked = true;
			}
			else if(action == '-')
			{
				paymode.value = document.getElementById('paymodeMinus').value;
				synchroDeltaNeg.checked = true;
			}
			
			synchroMontant.value = amount;
			synchro.checked = true;
		}
	}
}

function formatMonetaire(value)
{
	return Math.round(value * 100) / 100;
}

function askPrint()
{
	var el = document.getElementById('askprint_string');
	if (el != null)
	{
		var string = el.value;
		if(confirm(string))
		{
			window.print();
		}
	}
}

function confirmDelete(url)
{
	var el = document.getElementById('askdelete_string');
	if (el != null)
	{
		var string = el.value;
		if(confirm(string))
		{
			window.location = url;
		}
		else
		{
			return false;
		}
	}
}

function switchAdvantage(check)
{
	switchDisplay('advantagenum_container',check.checked);
}

function goTo(url)
{
	if(window && window.location)
	{
		window.location = url;
	}
	
	return false;
}

function refreshFacture()
{
	var check = document.getElementById('fact');
	switchDisplay('hideFact', check.checked);
}

function refreshConstructList()
{
	var list = document.getElementById('list_body');
	var list_rows = list.getElementsByTagName('tr');
	var inputs;
	var check;

	var x;
	var id;
	for (x = 0; x < list_rows.length; x++)
	{
		id = x + 1;
		check = document.getElementById('line' + id);
		inputs = list_rows[x].getElementsByTagName('input');
		var y;
		for (y = 0; y < inputs.length; y++)
		{
			if (inputs[y].id == check.id || check.checked)
			{
				inputs[y].style.visibility = 'visible';
				//inputs[y].disabled = false;
			}
			else
			{
				inputs[y].style.visibility = 'hidden';
				//inputs[y].disabled = true;
			}
		}

	}
}

function refreshRechercheTransaction()
{
	var chkDonation = document.getElementById('donation');
	if (chkDonation != null)
	{
		switchDisplay('tr_donation_nom', chkDonation.checked);
		switchDisplay('tr_donation_prenom', chkDonation.checked);
	}
}

function switchAll(bool)
{
	var list = document.getElementById('list_body');
	inputs = list.getElementsByTagName('input');
	var y;
	var id = 1;
	for (y = 0; y < inputs.length; y++)
	{
		if (inputs[y].id == 'line' + id)
		{
			id++;
			inputs[y].checked = bool;
		}
	}

	refreshConstructList();
	return false;
}

function copyP1ToP2()
{
	document.getElementById("p2Phone").value = document.getElementById("p1Phone").firstChild.nodeValue;
	if (document.getElementById("p1Num"))
	{
		document.getElementById("p2Num").value = document.getElementById("p1Num").firstChild.nodeValue;
	}
	document.getElementById("p2Mail").value = document.getElementById("p1Mail").firstChild.nodeValue;
	if (document.getElementById("p1Box"))
	{
		document.getElementById("p2Box").value = document.getElementById("p1Box").firstChild.nodeValue;
	}
	var tmp = document.getElementById("p1Country").firstChild.nodeValue;
	var listTmp = document.getElementById("p2Country");
	var i;
    for (i=0; i < listTmp.length; i++)
    {
       if (listTmp[i].value == tmp)
       {
       		listTmp[i].selected = 'selected';
       }
    }
	document.getElementById("p2Town").value = document.getElementById("p1Town").firstChild.nodeValue;
	document.getElementById("p2Zipcode").value = document.getElementById("p1Zipcode").firstChild.nodeValue;
	document.getElementById("p2Street").value = document.getElementById("p1Street").firstChild.nodeValue;
	return true;
}

function copyP1ToF()
{
	document.getElementById("fPhone").value = document.getElementById("p1Phone").firstChild.nodeValue;
	if (document.getElementById("p1Num"))
	{
		document.getElementById("fNum").value = document.getElementById("p1Num").firstChild.nodeValue;
	}
	if (document.getElementById("p1Box"))
	{
		document.getElementById("fBox").value = document.getElementById("p1Box").firstChild.nodeValue;
	}
	var tmp = document.getElementById("p1Country").firstChild.nodeValue;
	var listTmp = document.getElementById("fCountry");
	var i;
    for (i=0; i < listTmp.length; i++)
    {
       if (listTmp[i].value == tmp)
       {
       		listTmp[i].selected = 'selected';
       }
    }
	document.getElementById("fTown").value = document.getElementById("p1Town").firstChild.nodeValue;
	document.getElementById("fZipcode").value = document.getElementById("p1Zipcode").firstChild.nodeValue;
	document.getElementById("fStreet").value = document.getElementById("p1Street").firstChild.nodeValue;
	return true;
}

function checkFieldsAck()
{
	document.getElementById("fNum").value = document.getElementById("p1Num").firstChild.nodeValue;
	return true;
}



function Present(isTitre, code, intitule, canEditIntitule, description, remarque, goal, offert)
{
	this.isTitre = isTitre;
	this.canEditIntitule = (canEditIntitule ? true : false);
	this.data = new Array();
	this.data['code'] = code;
	this.data['intitule'] = (intitule ? intitule : '');
	this.data['description'] = (description ? description : '');
	this.data['remarque'] = (remarque ? remarque : '');
	this.data['goal'] = (goal ? goal : 0);
	this.data['offert'] = (offert ? offert : 0);
}

Present.prototype.type;
Present.prototype.canEditIntitule;
Present.prototype.data;

Present.prototype.createRow = function(x)
{
	if (this.isTitre)
	{
		return createTitleLineTable(x, this);
	}
	else
	{
		return createLineTable(x, this);
	}
}

function createLineTable(x, present)
{
	var code = present.data['code'];
	var intitule = present.data['intitule'];
	var desc = present.data['description'];
	var rmq = present.data['remarque'];
	var goal = present.data['goal'];
	var offert = present.data['offert'];

	var row = document.createElement('tr');
	row.className = 'ctable_row';

	var cell = document.createElement('td');

	if (present.canEditIntitule)
	{
		var input = createInput('text', 'wording' + x, intitule, null);
		input.className = 'input_inno';
		input.setAttribute('onchange', function() { eval('presents.SetData(' + x + ',\'intitule\', this.value)') });
		input.setAttribute('onChange', 'presents.SetData(' + x + ',\'intitule\', this.value)');
		cell.appendChild(input);
	}
	else
	{
		var label = document.createElement('label');
		label.appendChild(document.createTextNode(intitule));
		cell.appendChild(label);
		var input = createInput('hidden', 'wording'+x, intitule, null);
		cell.appendChild(input);
	}

	row.appendChild(cell);

	row.id = 'custom' + x;

	cell = document.createElement('td');
	var input = createInput('text', 'description'+x, desc, null);
	input.className = 'input_inno';
	input.setAttribute('onchange', function() { eval('presents.SetData(' + x + ',\'description\', this.value)') });
	input.setAttribute('onChange', 'presents.SetData(' + x + ',\'description\', this.value)');
	cell.appendChild(input);
	row.appendChild(cell);

	cell = document.createElement('td');
	cell.className = 'center';
	input = createInput('text', 'remark'+x, rmq, null);
	input.setAttribute('onchange', function() { eval('presents.SetData(' + x + ',\'remarque\', this.value)') });
	input.setAttribute('onChange', 'presents.SetData(' + x + ',\'remarque\', this.value)');
	input.className = 'input_inno';
	cell.appendChild(input);
	row.appendChild(cell);

	cell = document.createElement('td');
	cell.className = 'center';
	input = createInput('text', 'goalamount'+x, goal, null);
	input.className = 'input_inno';
	input.setAttribute('onchange', function() { eval('presents.SetData(' + x + ',\'goal\', this.value)') });
	input.setAttribute('onChange', 'presents.SetData(' + x + ',\'goal\', this.value)');
	cell.appendChild(input);
	row.appendChild(cell);

	cell = document.createElement('td');
	cell.className = 'center';
	label = document.createElement('label');
	label.appendChild(document.createTextNode(offert + ' \u20ac'));
	cell.appendChild(label);
	row.appendChild(cell);

	cell = document.createElement('td');
	cell.className = 'center';
	var image = document.createElement('img');
	image.src = 'fileadmin/template/images/common/arrow_down.gif';
	if (x == presents.elements.length-1)
	{
		image.style.display = 'none';
	}
	image.setAttribute('onclick', function() { eval('presents.MoveDown(' + x + ')') });
	image.setAttribute('onClick', 'presents.MoveDown(' + x + ')');

	cell.appendChild(image);
	row.appendChild(cell);

	cell = document.createElement('td');
	cell.className = 'center';
	image = document.createElement('img');
	image.src = 'fileadmin/template/images/common/arrow_up.gif';
	if (x == 0)
	{
		image.style.display = 'none';
	}
	image.setAttribute('onclick', function() { eval('presents.MoveUp(' + x + ')') });
	image.setAttribute('onClick', 'presents.MoveUp(' + x + ')');

	cell.appendChild(image);
	row.appendChild(cell);

	cell = document.createElement('td');
	cell.className = 'center';
	input = createInput('button', 'add', '+', null);
	input.setAttribute('onclick', function() { eval('presents.Insert(' + (x+1) + ', new Present(null,\'_'+ x +'\',null,true))') });
	input.setAttribute('onClick', 'presents.Insert(' + (x+1) + ', new Present(null,\'_'+ x +'\',null,true))');
	cell.appendChild(input);
	row.appendChild(cell);

	cell = document.createElement('td');
	cell.className = 'center';
	if (offert == 0) //on ne peut pas supprimer la ligne si une donation a été faite
	{
		input = createInput('button', 'remove', '-', null);
		if (code && code[0] != '-') //on replace cette ligne dans le tableau standard
		{
			input.setAttribute('onclick', function() { eval('presents.Remove(' + x + '); addLineStandard(\''+code+'\', \''+intitule+'\', \''+desc+'\', \''+rmq+'\', '+goal+');'); });
			input.setAttribute('onClick', 'presents.Remove(' + x + '); addLineStandard(\''+code+'\', \''+intitule+'\', \''+desc+'\', \''+rmq+'\', '+goal+');');
		}
		else
		{
			input.setAttribute('onclick', function() { eval('presents.Remove(' + x + ')') });
			input.setAttribute('onClick', 'presents.Remove(' + x + ')');
		}
		cell.appendChild(input);
	}
	input = createInput('hidden', 'code'+x, code, null);
	cell.appendChild(input);
	row.appendChild(cell);

	return row;
}

function createTitleLineTable(x, present)
{
	var code = present.data['code'];
	var intitule = present.data['intitule'];

	var row = document.createElement('tr');
	row.className = 'ctable_subhead';
	var cell = document.createElement('td');
	cell.colSpan = 5;

	if (present.canEditIntitule)
	{
		var input = createInput('text', 'wording' + x, intitule, null);
		input.className = 'input_inno input_inno_title';
		input.setAttribute('onchange', function() { eval('presents.SetData(' + x + ',\'intitule\', this.value)') });
		input.setAttribute('onChange', 'presents.SetData(' + x + ',\'intitule\', this.value)');
		cell.appendChild(input);
	}
	else
	{
		var label = document.createElement('label');
		label.appendChild(document.createTextNode(intitule));
		cell.appendChild(label);
		var input = createInput('hidden', 'wording'+x, intitule, null);
		cell.appendChild(input);
	}
	row.appendChild(cell);
	row.id = 'custom' + x;

	cell = document.createElement('td');
	cell.className = 'center';
	var image = document.createElement('img');
	image.src = 'fileadmin/template/images/common/arrow_down.gif';
	if (x == presents.elements.length-1)
	{
		image.style.display = 'none';
	}
	image.setAttribute('onclick', function() { eval('presents.MoveDown(' + x + ')') });
	image.setAttribute('onClick', 'presents.MoveDown(' + x + ')');
	cell.appendChild(image);
	row.appendChild(cell);

	cell = document.createElement('td');
	cell.className = 'center';
	image = document.createElement('img');
	image.src = 'fileadmin/template/images/common/arrow_up.gif';
	if (x == 0)
	{
		image.style.display = 'none';
	}
	image.setAttribute('onclick', function() { eval('presents.MoveUp(' + x + ')') });
	image.setAttribute('onClick', 'presents.MoveUp(' + x + ')');
	cell.appendChild(image);
	row.appendChild(cell);

	cell = document.createElement('td');
	cell.className = 'center';
	input = createInput('button', 'add', '+', null);
	input.setAttribute('onclick', function() { eval('presents.Insert(' + (x+1) + ', new Present(true,\'_'+ x +'\',null,true))') });
	input.setAttribute('onClick', 'presents.Insert(' + (x+1) + ', new Present(true,\'_'+ x +'\',null,true))');
	cell.appendChild(input);
	row.appendChild(cell);

	cell = document.createElement('td');
	cell.className = 'center';
	input = createInput('button', 'remove', '-', null);
	if (code && code[0] != '-' && !present.canEditIntitule) //on replace cette ligne dans le tableau standard
	{
		input.setAttribute('onclick', function() { eval('presents.Remove(' + x + '); addLineTitleStandard(\''+code+'\', \''+intitule+'\');'); });
		input.setAttribute('onClick', 'presents.Remove(' + x + '); addLineTitleStandard(\''+code+'\', \''+intitule+'\');');
	}
	else
	{
		input.setAttribute('onclick', function() { eval('presents.Remove(' + x + ')') });
		input.setAttribute('onClick', 'presents.Remove(' + x + ')');
	}
	cell.appendChild(input);
	input = createInput('hidden', 'code'+x, code, null);
	cell.appendChild(input);
	row.appendChild(cell);

	return row;
}

function updateNbCustom()
{
	var el = document.getElementById('nbCustom');
	if (el != null)
	{
		el.value = presents.elements.length;
	}
}

function addLineTitleStandard(code, intitule)
{
	var row = document.createElement('tr');
	row.className = 'ctable_subhead';
	var cell = document.createElement('td');
	cell.colSpan = 4;
	var label = document.createElement('label');
	label.appendChild(document.createTextNode(intitule));
	cell.appendChild(label);
	row.appendChild(cell);
	cell = document.createElement('td');
	cell.className = 'center';
	input = createInput('button', 'add', '+', null);
	input.onclick = function() { removeRow(this); presents.Add(new Present(true, code, intitule, false, '', '', 0)); };
	cell.appendChild(input);
	row.appendChild(cell);
	var body = document.getElementById('standard_body');
	body.appendChild(row);
}

function addLineStandard(code, intitule, desc, remark, goal)
{
	var row = document.createElement('tr');
	row.className = 'ctable_row';

	var cell = document.createElement('td');
	var label = document.createElement('label');
	label.appendChild(document.createTextNode(intitule));

	cell.className = 'center';
	cell.appendChild(label);
	row.appendChild(cell);

	cell = document.createElement('td');
	row.appendChild(cell);

	cell = document.createElement('td');
	row.appendChild(cell);

	cell = document.createElement('td');
	cell.className = 'center';
	label = document.createElement('label');
	label.appendChild(document.createTextNode(goal + ' \u20ac'));
	cell.appendChild(label);
	row.appendChild(cell);

	cell = document.createElement('td');
	cell.className = 'center';
	input = createInput('button', 'add', '+', null);
	input.onclick = function() { removeRow(this); presents.Add(new Present(false, code, intitule, false, desc, remark, goal)); };
	cell.appendChild(input);
	row.appendChild(cell);
	var body = document.getElementById('standard_body');
	body.appendChild(row);
}

function refreshMontantGift(nbLigne, canBeNeg)
{
	var x;
	var total = 0;
	for (x = 1; x <= nbLigne; x++)
	{
		if (document.getElementById('parti' + x) != null)
		{

			var val = document.getElementById('parti' + x).value;
			val = val.replace(',', '.');
			if (val > 0 || canBeNeg == 1)
			{
				total += parseFloat(val);
				document.getElementById('parti' + x).value = val;
			}
			else
			{
				document.getElementById('parti' + x).value = '';
			}
		}
	}
	document.getElementById("montant").innerHTML = String(formatMonetaire(total));
}

function resetMontantGift(nbLigne)
{
	document.getElementById("montant").innerHTML = "0";
	var x;
	for (x = 1; x <= nbLigne; x++)
	{
		if (document.getElementById('parti' + x) != null)
		{
			document.getElementById('parti' + x).value = '';
		}
	}
}

function refreshNombreCaractere(txtArea)
{
	var longueurTxt = txtArea.value.length;
	var cptChar = document.getElementById("cptChar");
	if (longueurTxt > 500)
	{
		cptChar.className = "error";
		longueurTxt = 500;
	}
	else
	{
		cptChar.className = "";
	}
	cptChar.innerHTML = 500 - longueurTxt;
}

function createXmlHttp() {
	var xmlHttp = null;

	try {
		xmlHttp = new XMLHttpRequest();
	} catch (trymicrosoft) {
		try {
			xmlHttp = new ActiveXObject('Msxml2.XMLHTTP');
		} catch (othermicrosoft) {
			try {
				xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
			} catch (failed) {
				xmlHttp = null;
			}
		}
	}

	return xmlHttp;
}

function makeAsyncRequest(url, onready, arg, method)
{
	var xmlHttp = createXmlHttp();

	if (xmlHttp != null)
	{
		if (method == null) method = 'GET';
		if (onready == null) onready_func = function() { alert(xmlHttp.readyState + ' : ' + xmlHttp.responseText); }
		else onready_func = function()
		{
			var isSuccess = (xmlHttp.readyState == 4 && xmlHttp.status == 200);
			onready(isSuccess, arg, (isSuccess ? xmlHttp.responseText : ''), xmlHttp.readyState);
		}

		xmlHttp.onreadystatechange = onready_func;
		xmlHttp.open(method, url, true);
		xmlHttp.send(null);
	}
	else
	{
		alert('Sorry, your browser doesn\'t support XmlHttpRequests.');
	}
}

function showDetailsTransaction(isSuccess, arg, response, readystate)
{
	if(isSuccess)
	{
		var div = document.getElementById('content_detail');
		var divR = document.getElementById('content_recherche');
		var divS = document.getElementById('content_switcher');
		if (div != null)
		{
			//alert(response);
			div.innerHTML = response;
			div.style.display = 'block';
			divR.style.display = 'none';
			divS.style.display = 'block';
		}
	}
}

function switchContainer(idCurrent, idAwaited)
{
	var div = document.getElementById('content_' + idCurrent);
	var divR = document.getElementById('content_' + idAwaited);
	var divS = document.getElementById('content_switcher');

	if (div != null && divR != null)
	{
		div.innerHTML = '';
		div.style.display = 'none';
		divR.style.display = 'block';
	}
	if (divS != null) divS.style.display = 'none';
}


function moveUp(elementId)
{
	var element = document.getElementById(elementId);
	var container = getParent(element);
	if(container != null && element != null)
	{
		var tag = element.tagName;
		var prev = null;
		for(var x = 0; x < container.childNodes.length && container.childNodes[x] != element; x++)
		{
			if (container.childNodes[x].tagName == tag) prev = container.childNodes[x];
		}

		if(prev != null)
		{
			if (container.insertBefore(element, prev));
			var order = document.getElementById(prev.id + '_order');
			order.value = Number(order.value) + 1;
			order = document.getElementById(element.id + '_order');
			order.value = Number(order.value) - 1;
		}
	}
}

function moveDown(elementId)
{
	var element = document.getElementById(elementId);
	var container = getParent(element);
	if(container != null && element != null)
	{
		var tag = element.tagName;
		var prev = null;
		for(var x = container.childNodes.length - 1; x > 0 && container.childNodes[x] != element; x--)
		{
			if (container.childNodes[x].tagName == tag) prev = container.childNodes[x];
		}

		if(prev != null)
		{
			if (container.insertBefore(prev, element));
			var order = document.getElementById(prev.id + '_order');
			order.value = Number(order.value) - 1;
			order = document.getElementById(element.id + '_order');
			order.value = Number(order.value) + 1;
		}
	}
}

function switchBlogDataSection(id)
{
	var shown = switchDisplay('section_'+id+'_content');

	var el = document.getElementById('section_'+id+'_visible');

	if(el != null)
	{
		el.value = (shown ? '1' : '0');
	}
}

/* onLoad hack*/
var onloadFunctions = new Array();

function customOnload(e)
{
	for(var x = 0; x < onloadFunctions.length; x++)
	{
		onloadFunctions[x](e);
	}
}

function useCustomOnload()
{
	addCustomOnloadFunction(T3_onloadWrapper);
	window.onload = customOnload;
}

function addCustomOnloadFunction(func)
{
	onloadFunctions.push(func);
}

function affBoutonRetour()
{
	document.getElementById('retour').style.display = 'inline';
}



function unformatter(v)
{
	v = v + '';
	v = v.replace(/,/g,'.');
	return v;
}
