function addtoflat(value) {
	if (value != '') {

		var txtval = document.getElementById('frmfval');
		var lstval = document.getElementById('frmfvals');

		if (lstval.selectedIndex == -1) {
			if (checkdup('frmfvals', value) == 0) {
				lstval.options[lstval.length] = new Option(value, 'new');
			} else {
				alert('la valeur existe dèja !');
			}
		} else {
			if (checkdup('frmfvals', value) == 0) {
				var olval = lstval.options[lstval.selectedIndex].text;
				lstval.options[lstval.selectedIndex] = new Option(value,
						lstval[lstval.selectedIndex].value);
				updrelvals(txtval.value, olval);
			} else {
				if (lstval.options[lstval.selectedIndex].text != txtval.value) {
					alert('la valeur existe dèja !');
				}
			}
		}
		txtval.value = '';

	} else {
		alert('Veuillez entrer une valeur !');
	}
}
function updrelvals(value, olval) {
	var lstvalh2 = document.getElementById('frmchildvalsH2');
	var lstval = document.getElementById('frmchildvals');

	count = lstval.getElementsByTagName('option').length;
	for (o = 0; o < count; o++) {

		if (lstval.options[o].text == olval) {
			lstval.options[o].text = value;
			break;
		}
	}

	countd = lstvalh2.getElementsByTagName('option').length;
	for (o = 0; o < countd; o++) {
		if (lstvalh2.options[o].text == olval) {
			lstvalh2.options[o].text = value;
		}
	}
}

function delrelvals(olval) {
	var lstvalh2 = document.getElementById('frmchildvalsH2');
	var lstval = document.getElementById('frmchildvals');

	count = lstval.getElementsByTagName('option').length;
	for (o = 0; o < count; o++) {

		if (lstval.options[o].text == olval) {
			lstval.remove(o);
			break;
		}
	}

	countd = lstvalh2.getElementsByTagName('option').length;
	for (o = 0; o < countd; o++) {
		if (lstvalh2.options[o].text == olval) {
			lstvalh2.remove(o);
			o = -1;
			countd--;
		}
	}
}

function addtoflate(value, id) {
	var lstval = document.getElementById('frmfvals');
	lstval.options[lstval.length] = new Option(value, id);
}
function addpvalues(value, id) {
	var lstval = document.getElementById('frmparentvals');
	lstval.options[lstval.length] = new Option(value, id);
}
function addcvalues(value, id) {
	if (checkdup('frmchildvals', value) == 0) {
		var lstvalh2 = document.getElementById('frmchildvalsH2');
		var lstval = document.getElementById('frmchildvals');
		lstval.options[lstval.length] = new Option(value, id);
		lstvalh2.options[lstvalh2.length] = new Option(value, id);
	} else {
		alert('la valeur existe dèja !');

	}
}
function addcvaluese(value, id) {
	var lstvalh2 = document.getElementById('frmchildvalsH2');
	lstvalh2.options[lstvalh2.length] = new Option(value, id);
}
function addtorelflat() {
	var lstval = document.getElementById('frmchildvals');

	if (lstval.disabled == false) {
		var txtval = document.getElementById('frmfval');
		var plstval = document.getElementById('frmparentvals');
		var lstval = document.getElementById('frmfvals');

		if (lstval.selectedIndex != -1) {
			addcvalues(lstval.options[lstval.selectedIndex].text, plstval.value
					+ '_' + lstval.options[lstval.selectedIndex].value);
			txtval.value = '';
		}
	}
}

function remfromflat() {

	var txtval = document.getElementById('frmfval');
	var lstval = document.getElementById('frmfvals');
	var dels = document.getElementById('frmfalldels');
	if (lstval.selectedIndex != -1) {
		if (lstval[lstval.selectedIndex].value != 'new') {
			dels.value = dels.value + '|' + lstval[lstval.selectedIndex].value;
		}
		delrelvals(lstval.options[lstval.selectedIndex].text);
		lstval.options[lstval.selectedIndex] = null;
		txtval.value = '';
	}
}

function remfromrelflat() {

	var lstvalh2 = document.getElementById('frmchildvalsH2');
	var lstval = document.getElementById('frmchildvals');
	if (lstval.selectedIndex != -1) {
		countd = lstvalh2.getElementsByTagName('option').length;
		var pval = lstval.value;

		for (o = 0; o < countd; o++) {

			if (lstvalh2.options[o].value == pval) {
				lstvalh2.remove(o);
				break;
			}
		}
		lstval.options[lstval.selectedIndex] = null;
	}
}

function showhidevals(value) {

	var divval = document.getElementById('divvals');
	var divrel = document.getElementById('divrels');
	var divmsk = document.getElementById('divmsk');
	var maxs = document.getElementById('frmfmaxl');
	var mins = document.getElementById('frmfminl');
	var msk = document.getElementById('frmfmask');

	if (value == 2 || value == 3 || value == 4) {
		divval.style.display = 'block';
		divmsk.style.display = 'none';
		if (value == 2) {
			divrel.style.display = 'block';
		} else {
			divrel.style.display = 'none';
		}
		maxs.value = '';
		mins.value = '';
		msk.selectedIndex = 0;
		msk.disabled = true;
		maxs.disabled = true;
		mins.disabled = true;

	} else {
		divval.style.display = 'none';
		divrel.style.display = 'none';
		divmsk.style.display = 'block';
		msk.disabled = false;
		maxs.disabled = false;
		mins.disabled = false;
	}

}
function getrelflat() {

	var lstval = document.getElementById('frmchildvalsH2');
	var allvals = document.getElementById('frmfallvalsp');
	var buff = '';

	countd = lstval.getElementsByTagName('option').length;

	for (o = 0; o < countd; o++) {

		buff = buff + '|' + lstval[o].text;
	}

	allvals.value = buff;

}
function getrelids() {

	var lstval = document.getElementById('frmchildvalsH2');
	var allvals = document.getElementById('frmfallidsp');
	var buff = '';

	countd = lstval.getElementsByTagName('option').length;

	for (o = 0; o < countd; o++) {
		buff = buff + '|' + lstval[o].value;
	}

	allvals.value = buff;

}
function getflat() {

	var lstval = document.getElementById('frmfvals');
	var allvals = document.getElementById('frmfallvals');
	var buff = '';

	countd = lstval.getElementsByTagName('option').length;

	for (o = 0; o < countd; o++) {
		buff = buff + '|' + lstval[o].text;
	}

	allvals.value = buff;

}

function getids() {

	var lstval = document.getElementById('frmfvals');
	var allvals = document.getElementById('frmfallids');
	var buff = '';

	countd = lstval.getElementsByTagName('option').length;

	for (o = 0; o < countd; o++) {
		buff = buff + '|' + lstval[o].value;
	}

	allvals.value = buff;

}

function cloneflat() {
	var lstvals = document.getElementById('frmfvals').cloneNode(true);
	var relvals = document.getElementById('frmchildvals');
	countd = lstvals.getElementsByTagName('option').length;
	for (o = 0; o < countd; o++) {
		if (lstvals[o].text) {
			relvals.options[o] = new Option(lstvals[o].text, lstvals[o].value);
		}
	}
}

function setCheckAll(frmEle) {
	frmObj = frmEle.form;
	if (frmEle.checked == false) {
		for ( var i = 0; i < frmObj.no.value; i++) {
			var tempObj = eval("frmObj.ch" + i);
			tempObj.checked = false;
		}
		frmEle.checked = false;
	} else {
		for ( var i = 0; i < frmObj.no.value; i++) {
			var tempObj = eval("frmObj.ch" + i);
			tempObj.checked = true;
		}
		frmObj.checkAll.checked = true;
	}
}

function validateCheckbox(frmName, status) {
	frmObj = eval(frmName);
	var flagChk = false;

	for ( var i = 0; i < frmObj.no.value; i++) {
		var tempObj = eval("frmObj.ch" + i);
		if (tempObj.checked == true) {
			flagChk = true;
		}
	}

	if (flagChk) {

		if (status == 'Delete')
			var msg = "Supprimer les formulaires séléctionés ?";

		if (confirm(msg)) {
			frmObj.action.value = status;
			frmObj.submit();
		}
	} else {
		alert("Veuillez sélectionner au moins un formulaire");
		return;
	}
}

function ajreq(url) {

	var xmlhttp = false;
	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			xmlhttp = false;
		}
	}

	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		try {
			xmlhttp = new XMLHttpRequest();
		} catch (e) {
			xmlhttp = false;
		}
	}
	if (!xmlhttp && window.createRequest) {
		try {
			xmlhttp = window.createRequest();
		} catch (e) {
			xmlhttp = false;
		}
	}

	xmlhttp.open("GET", url, true);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4) {
			var lstval = document.getElementById('frmparentvals');
			lstval.options.length = 0;
			psid = xmlhttp.responseText.split('|');
			for (i = 0; i < psid.length - 1; i++) {
				vals = psid[i].split('$');
				addpvalues(vals[1], vals[0]);
			}
		}
		filterrelvals(document.getElementById('frmparentvals').value);
	}
	xmlhttp.send(null)

}
function clearcvals() {
	var clstval = document.getElementById('frmchildvals');
	var clstvalh2 = document.getElementById('frmchildvalsH2');
	clstval.options.length = 0;
	clstvalh2.options.length = 0;
}

function filterrelvals(pval) {
	var plstval = document.getElementById('frmparentvals');
	var lstval = document.getElementById('frmchildvals');
	var lstvalh2 = document.getElementById('frmchildvalsH2');
	lstval.options.length = 0;
	countd = lstvalh2.getElementsByTagName('option').length;

	for (o = 0; o < countd; o++) {
		psid = lstvalh2[o].value.split('_');

		if (psid[0] == plstval.value) {
			lstval.options[lstval.length] = new Option(lstvalh2[o].text,
					lstvalh2[o].value);
		}
	}

}
function checkdup(list, value) {
	var ret = 0;
	var lstval = document.getElementById(list);
	countd = lstval.getElementsByTagName('option').length;
	for (o = 0; o < countd; o++) {
		if (lstval[o].text == value) {
			ret = 1;
			break;
		}

	}
	return ret;
}
function jGet(x) {
	if (location.href.match(x)) {
		return location.href.split(x + '=')[1].split('&')[0];
	}
}

function jgetpvals() {
	var pval = document.getElementById('frmfrels');
	var lstp = document.getElementById('frmparent');
	var lstpval = document.getElementById('frmparentvals');
	var lstcval = document.getElementById('frmchildvals');
	if (lstp.options.length != 0) {
		if (pval.checked) {
			lstpval.disabled = false;
			lstp.disabled = false;
			lstcval.disabled = false;
			ajreq('includes/ajreq.php?a=prvals&pid=' + document
					.getElementById('frmparent').value);

		} else {
			lstpval.disabled = true;
			lstp.disabled = true;
			lstcval.disabled = true;
		}
	} else {
		pval.disabled = true;
		lstpval.disabled = true;
		lstp.disabled = true;
		lstcval.disabled = true;
	}
}
// //////////////////PROD
function addhtmltoprodflat() {
	var htmlval = document.getElementById('prodaddhtml');
	var lstprod = document.getElementById('prodprv');
	value = htmlval.value;
	if (value != '') {
		if (lstprod.selectedIndex == -1) {
			lstprod.options[lstprod.length] = new Option('[HTML]>' + value,
					'html_new');
		} else {
			try {
				lstprod.add(new Option('[HTML]>' + value, 'html_new'),
						lstprod.options[lstprod.selectedIndex]);
			} catch (ex) {
				lstprod.add(new Option('[HTML]>' + value, 'html_new'),
						lstprod.selectedIndex);
			}
		}
	}
	htmlval.value = '';
}
function addcontoprodflat(oc) {

	var lstprod = document.getElementById('prodprv');

	if (oc == 1) {
		var conval = document.getElementById('prodaddcop');
		value = conval.value;
		if (lstprod.selectedIndex == -1) {
			lstprod.options[lstprod.length] = new Option('[CONT]>' + value,
					'conto_new');
		} else {
			try {
				lstprod.add(new Option('[CONT]>' + value, 'conto_new'),
						lstprod.options[lstprod.selectedIndex]);
			} catch (ex) {
				lstprod.add(new Option('[CONT]>' + value, 'conto_new'),
						lstprod.selectedIndex);
			}
		}
		conval.value = '';
	} else {
		if (lstprod.selectedIndex == -1) {
			lstprod.options[lstprod.length] = new Option('[CONT]>FERMER',
					'contc_new');
		} else {
			try {
				lstprod.add(new Option('[CONT]>FERMER', 'contc_new'),
						lstprod.options[lstprod.selectedIndex]);
			} catch (ex) {
				lstprod.add(new Option('[CONT]>FERMER', 'contc_new'),
						lstprod.selectedIndex);
			}
		}
	}

}
function addfrmtoprodflat() {
	var lstprod = document.getElementById('prodprv');
	var frmval = document.getElementById('prodfrms');
	var obc = document.getElementById('prodobl');
	value = frmval.value;
	txt = frmval.options[frmval.selectedIndex].text;

	if (obc.checked == 1) {
		if (lstprod.selectedIndex == -1) {
			lstprod.options[lstprod.length] = new Option('[FRM*]>' + txt,
					'frm_' + value + '_new_o');
		} else {
			try {
				lstprod.add(new Option('[FRM*]>' + txt,
						'frm_' + value + '_new_o'),
						lstprod.options[lstprod.selectedIndex]);
			} catch (ex) {
				lstprod.add(new Option('[FRM*]>' + txt,
						'frm_' + value + '_new_o'), lstprod.selectedIndex);
			}
		}
	} else {
		if (lstprod.selectedIndex == -1) {
			lstprod.options[lstprod.length] = new Option('[FRM]>' + txt,
					'frm_' + value + '_new_n');
		} else {
			try {
				lstprod.add(new Option('[FRM]>' + txt,
						'frm_' + value + '_new_n'),
						lstprod.options[lstprod.selectedIndex]);
			} catch (ex) {
				lstprod.add(new Option('[FRM]>' + txt,
						'frm_' + value + '_new_n'), lstprod.selectedIndex);
			}
		}
	}
}
function filterfrms(url) {

	var xmlhttp = false;
	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			xmlhttp = false;
		}
	}

	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		try {
			xmlhttp = new XMLHttpRequest();
		} catch (e) {
			xmlhttp = false;
		}
	}
	if (!xmlhttp && window.createRequest) {
		try {
			xmlhttp = window.createRequest();
		} catch (e) {
			xmlhttp = false;
		}
	}

	xmlhttp.open("GET", url, true);

	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4) {

			var lstval = document.getElementById('prodfrms');
			lstval.options.length = 0;
			psid = xmlhttp.responseText.split('|');
			for (i = 0; i < psid.length - 1; i++) {
				vals = psid[i].split('$');
				lstval.options[lstval.length] = new Option(vals[1], vals[0]);
			}
		}

	}
	xmlhttp.send(null)

}
function remfromprodflat() {

	var lstval = document.getElementById('prodprv');

	if (lstval.selectedIndex != -1) {
		lstval.options[lstval.selectedIndex] = null;
	}
}
function prodflatmvup() {
	var lstval = document.getElementById('prodprv');

	if (lstval.selectedIndex != -1 && lstval.selectedIndex != 0) {
		i = lstval.selectedIndex;
		tmpt = lstval.options[lstval.selectedIndex].text;
		tmpv = lstval.options[lstval.selectedIndex].value;
		lstval.options[lstval.selectedIndex] = new Option(
				lstval.options[lstval.selectedIndex - 1].text,
				lstval.options[lstval.selectedIndex - 1].value);
		lstval.options[i - 1] = new Option(tmpt, tmpv);
		lstval.selectedIndex = i - 1;
	}
}
function prodflatmvdown() {
	var lstval = document.getElementById('prodprv');

	if (lstval.selectedIndex != -1
			&& lstval.selectedIndex != lstval.options.length - 1) {
		i = lstval.selectedIndex;
		tmpt = lstval.options[lstval.selectedIndex].text;
		tmpv = lstval.options[lstval.selectedIndex].value;
		lstval.options[lstval.selectedIndex] = new Option(
				lstval.options[lstval.selectedIndex + 1].text,
				lstval.options[lstval.selectedIndex + 1].value);
		lstval.options[i + 1] = new Option(tmpt, tmpv);
		lstval.selectedIndex = i + 1;
	}
}
function editprodflat() {
	var eds = document.getElementById('editstate');
	var edtc = document.getElementById('edtcol');
	var lstval = document.getElementById('prodprv');
	if (lstval.selectedIndex != -1) {
		ptp = lstval.value.split('_');
		oval = ptp[2];
		switch (ptp[0]) {
		case 'frm':
			var htmlval = document.getElementById('prodfrms');
			var obc = document.getElementById('prodobl');
			var prodftid = document.getElementById('prodftid');
			if (eds.value == '0') {
				htmlval.value = ptp[1];
				if (ptp[3] == 'o') {
					obc.checked = '1';
				} else {
					obc.checked = false;
				}
				edtc.border = 2;
				lstval.disabled = true;
				eds.value = '1';
			} else {
				value = htmlval.options[htmlval.selectedIndex].text;
				idd = htmlval.value;
				lstval.disabled = false;
				eds.value = '0';
				edtc.border = 0;
				if (obc.checked == '1') {
					lstval.options[lstval.selectedIndex] = new Option(
							'[FRM*]>' + value, 'frm_' + idd + '_' + oval + '_'
									+ 'o');
				} else {
					lstval.options[lstval.selectedIndex] = new Option(
							'[FRM]>' + value, 'frm_' + idd + '_' + oval + '_'
									+ 'n');
				}
				prodftid.selectedIndex = 0;
				htmlval.selectedIndex = 0;
				obc.checked = false;
			}

			break;
		case 'conto':
			var htmlval = document.getElementById('prodaddcop');
			oval = lstval.options[lstval.selectedIndex].value;
			if (eds.value == '0') {
				htmlval.value = lstval.options[lstval.selectedIndex].text
						.substr(lstval.options[lstval.selectedIndex].text
								.indexOf('>') + 1);
				edtc.border = 2;
				lstval.disabled = true;
				eds.value = '1';
			} else {
				value = htmlval.value;
				lstval.disabled = false;
				eds.value = '0';
				edtc.border = 0;
				lstval.options[lstval.selectedIndex] = new Option(
						'[CONT]>' + value, oval);
				htmlval.value = '';
			}

			break;
		case 'html':
			var htmlval = document.getElementById('prodaddhtml');
			oval = lstval.options[lstval.selectedIndex].value;
			if (eds.value == '0') {
				htmlval.value = lstval.options[lstval.selectedIndex].text
						.substr(lstval.options[lstval.selectedIndex].text
								.indexOf('>') + 1);
				edtc.border = 2;
				lstval.disabled = true;
				eds.value = '1';
			} else {
				value = htmlval.value;
				lstval.disabled = false;
				eds.value = '0';
				edtc.border = 0;
				lstval.options[lstval.selectedIndex] = new Option(
						'[HTML]>' + value, oval);
				htmlval.value = '';
			}

			break;
		}
	}
}
function getprodprvflat() {

	var lstval = document.getElementById('prodprv');
	var allvals = document.getElementById('prodallvals');
	var buff = '';

	countd = lstval.getElementsByTagName('option').length;

	for (o = 0; o < countd; o++) {
		buff = buff + '|' + lstval[o].value + '~' + lstval[o].text;
	}

	allvals.value = buff;

}
function addtoprodprv(value, id) {
	var lstval = document.getElementById('prodprv');
	lstval.options[lstval.length] = new Option(value, id);
}
function checkNaN() {
	var lstval = document.getElementById('frmfmaxl');
	if (isNaN(lstval.value) && lstval.value != '') {
		alert('la longueur maximale n\'est pas un nombre');
		return false;
	} else {
		return true;
	}
}
function jpop(url, titre) {
	window
			.open(url, '',
					'height=600px,width=620px,menubar=no,toolbar=no,scrollbars=yes,location=no');
}
function filterchild(fid, pfid, value, sel) {
	if (sel == '')
		sel = 0;

	var xmlhttp = false;
	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			xmlhttp = false;
		}
	}

	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		try {
			xmlhttp = new XMLHttpRequest();
		} catch (e) {
			xmlhttp = false;
		}
	}
	if (!xmlhttp && window.createRequest) {
		try {
			xmlhttp = window.createRequest();
		} catch (e) {
			xmlhttp = false;
		}
	}

	xmlhttp.open("GET", 'includes/ajreq.php?a=childfilter&fid=' + fid + '&pfid=' + pfid + '&pval=' + value, true);

	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4) {
			var valin = 0;
			var lstval = document.getElementById(fid);
			lstval.options.length = 0;
			psid = xmlhttp.responseText.split('|');
			for (i = 0; i < psid.length - 1; i++) {
				vals = psid[i].split('$');
				lstval.options[lstval.length] = new Option(vals[1], vals[0]);
				if (vals[0] == sel)
					valin = 1;
			}
			if (valin == 1) {
				lstval.value = sel;
			} else {
				lstval.value = 0;
			}

			lstval.onchange();
			if (lstval.options.length > 1) {
				document.getElementById(fid).disabled = false;
			} else {
				document.getElementById(fid).disabled = true;
			}

		}

	}
	xmlhttp.send(null)

}
function showhidecps(){
	var cps = document.getElementById('cps');
	
	switch(cps.style.display){
	case 'block':
		cps.style.display='none';
	break;
	case 'none':
		cps.style.display='block';
	break;	
	}
}
function ajcp() {
	var valz = document.getElementById('frmfval').value;
	if(valz.length>2){
	var xmlhttp = false;
	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			xmlhttp = false;
		}
	}

	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		try {
			xmlhttp = new XMLHttpRequest();
		} catch (e) {
			xmlhttp = false;
		}
	}
	if (!xmlhttp && window.createRequest) {
		try {
			xmlhttp = window.createRequest();
		} catch (e) {
			xmlhttp = false;
		}
	}

	xmlhttp.open("GET", "includes/ajreq.php?a=cpvals&val="+valz, true);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4) {
			var lstval = document.getElementById('frmfvals');
			lstval.options.length = 0;
			psid = xmlhttp.responseText.split('|');
			for (i = 0; i < psid.length - 1; i++) {
				vals = psid[i].split('$');
				lstval.options[lstval.length] = new Option(vals[0]+' - '+vals[1], vals[0]);
			}
		}
		
	}
	xmlhttp.send(null)
	}
}