//* Fonctions MM de getobject

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

//* Fonctions des trims

function strim(val, crt)
{
  var txt = val;
  while (txt.substr(0, 1) == crt) {txt = txt.substr(1, txt.length);}
  while (txt.substr(txt.length-1, 1) == crt) {txt = txt.substr(0, txt.length-1);}

  return txt;
}

function trim(val)
{
  return  strim(val, " ");
}

//* Fonctions de création de l'URL GET des tris

function jstri(tabsel)
{
	var idoc = document.forms[0];
	var research = trim(idoc.research.value);
	var tsel = tabsel.split(":");
	
	turlsel = ""; turlval = "";
	for (i=0; i<=tsel.length-1; i++)
	{
		var ps = idoc[tsel[i]].selectedIndex;
		var tps = idoc[tsel[i]].options[ps].value;
		turlsel += tsel[i] + ":";
		turlval += tps + ":";
	}
	
	turlsel = strim(turlsel, ":");
	turlval = strim(turlval, ":");
	
	turl = JSqs;
	turl = turl + "&sel=" + turlsel + "&val=" + turlval;
	if (research != '') {turl = turl + "&research=" + research;}
	
	turl = strim(turl, "&");
	
	self.document.location.href = "index.asp?" + turl;
}

//* Fonctions de changement de pages 1/n

function topageget(num)
{
	var idoc = document.forms[0];
	var research = trim(idoc.research.value);
	
	turl = JSpage + "&page=" + num;
	if (research != '') {turl = turl + "&research=" + research;}
	self.document.location.href = "index.asp?" + turl;
}

//* Fonctions de classement

function orderby(orderby, sens)
{
	var idoc = document.forms[0];
	var research = trim(idoc.research.value);
	
	turl = JSorder + "&orderby=" + orderby + ":" + sens;
	if (research != '') {turl = turl + "&research=" + research;}
	self.document.location.href = "index.asp?" + turl;
}

//* Fonctions de pop up

function pop(url, param)
{
	if (url)
	{
		var jour = new Date();
		var name = 'pop_'+jour.getHours()+''+jour.getMinutes()+''+jour.getSeconds();
	
		eval(name+" = window.open('"+url+"','"+name+"','"+param+"');");

  		eval("setTimeout('"+name+".focus();',250);");
	}
}

//* Fonctions de suppression

function supp(url, cpt)
{
	if (cpt == 0)
	{
		if (confirm(tx_js_supp))
		{
			self.document.location.href = url;
		}
	}
	else
	{
		alert("Vous ne pouvez pas supprimer cette ligne, "+cpt+" élément(s) lui sont associées !");
	}
}

//* Fonctions de cochage

function coche(url)
{
	self.document.location.href = url;
}

//* Fonctions de contrôle de saisie

function isMailChar(num) 
{
	var string="abcdefghijklmnopqrstuvwxyz@._-1234567890 ";
	if (string.indexOf(num.toLowerCase()) != -1) {return true;}
	return false;
}

function isValidEmail(val)
{
	if (val.length == 0) {return true;}
	else
	{
		for (var i=0; i < val.length; i++) 
	  		{if (!isMailChar(val.charAt(i))) {return false;}}
		
		if (val.indexOf('@') < 0) {return false;}
		if (val.indexOf('.') < 0) {return false;}
		
		return true;
	}
}

function isNumber(num)
{
	if (num.length >1 )	{
		for(i=0; i<num.length; i++){
			if (!isNumber(num.substr(i,1))){return false;}
			
		}
		return true;
	}
	else{
		var string="1234567890";
		if (string.indexOf(num) == -1) {return false;}
		else return true;
	}
}

function isPhone(num){
	for (i=0; i<=num.length-1; i++){
		if(!isNumber(num.substring(i,(i+1)))){
			if(num.substring(i,(i+1)) !=' ' && num.substring(i,(i+1)) != '.')return false;
		}
	}
return true;
}

function isDigitDate(num) 
{
	var string="1234567890/";
	if (string.indexOf(num) != -1) {return true;}
	return false;
}

function isIntegerDate(val) 
{
	for (var i=0; i < val.length; i++) 
	  {if (!isDigitDate(val.charAt(i))) {return false;}}
	return true;
}

function isValidDateFull(val)
{
  if (val.length == 0) {return true;}
  if (val.length > 0)
   {t_tl = val.split("/");}

  return ( (isIntegerDate(val) ) && (val.length==10) && (t_tl.length ==3) )
}

function inList(l, val)
{
  for (var i=0; i < l.length; i++)
   {if (l[i].toUpperCase()==val.toUpperCase()) {return true; break;}
   }
  return false;
}

function isExist(id, li, nval, oval)
{
	inl = inList(li, nval);

	if ((inl) && (id == 0)) {return true;}
	if ((inl) && (id > 0) && ((nval).toUpperCase() != (oval).toUpperCase())) {return true;}
	return false;
}

function isSelected(l)
{
	var i = l.selectedIndex;
	var il = l.options[i].value;
	
	return il;
}

function getLcheck(ifra){
	var sep = "";
	tth = "";
	for (var i = 0; i < ifra.elements.length; i++) 
    {
	  if ( (ifra.elements[i].type == 'checkbox') && (ifra.elements[i].name == 'ccase') )  {
		if (ifra.elements[i].checked) {
			tth += sep + ifra.elements[i].value;
			sep = ":";
		}
	}
	}
	
return tth;
}


function in_array(val, tab){
	for(var i=0; i<tab.length; i++){
		if(tab[i] == val){
			return i;
		}
	}
return false;
}

function is_array_key(val, tab){
	if(tab[val] != undefined) {
		return true;
	}
	else{
		return false;
	}
}


function buildcoche(valcoche){

	idoc = document.forms[0];
	tcoche = getLcheck(idoc);

	if (tcoche.length == 0){
		tcoche = (valcoche).toString();
	}
	else{
		var tlcoche = tcoche.split(":");
		if (valcoche != '')	{
			if (!inList(tlcoche, (valcoche).toString())){
				tcoche += ":"+ valcoche;
			
			}
		}
	}
return tcoche;
}

function supp_lot(val){
    	tcoche = buildcoche(val);
    //	alert(tcoche);
    	var ttlcoche = tcoche.split(":");
		
		if (ttlcoche.length <= 1) {tph = tx_js_supp;}
		else {tph = tx_js_supp_lot.replace("_#_", ttlcoche.length);}
		
		if (confirm(tph)) {

		var	url = document.location.href+'';
			url = url.replace('index.asp', 'supp.asp');
			url = BuildUrl(url, 'ccoche', tcoche);

		self.document.location.href = url;

		}
}

function mGroup(){
	tcoche = buildcoche('');
	if (tcoche != ''){
		url = "../groupe_edit/edit.asp?rub=CTC&ccoche=" + tcoche;
		pop(url, 'width=600,height=300');
	}
	else{
		alert("Effectuez au moins une sélection ! ");
	}
}



//Retourne l'url [Qstr] en attribuant [val] à [champ]
function BuildUrl(Qstr, champ, val){
	if(param = GetVal(champ, Qstr)){
		return Qstr.replace(champ+'='+param, champ+'='+val);
	}
	else{
		var sep = '?';
		if(Qstr.indexOf(sep)>-1){
			sep = '&';
		}
	return Qstr+=sep+champ+'='+val;
	}
}


function checkall(ifra, val){
	for(var i = 0; i < ifra.elements.length; i++){
		if ((ifra.elements[i].type == 'checkbox') && (ifra.elements[i].name == 'ccase')){
			ifra.elements[i].checked = val;
		}
	}
}

/*
Retourne la valeur de [val] passé en GET
False si la variable n'existe pas
*/
function GetVal(val){
	args = GetVal.arguments;
	if(args.length > 1){
		var Turl = args[1];
	}
	else{
		var Turl = window.location+'';
	}
	var deb = Turl.indexOf('&'+val+'=');
	if(deb == -1){
		deb = Turl.indexOf('?'+val+'=');
	}
	var fin = 0;
		if(deb > -1)	{
			var sub1 = Turl.substring(deb+1,Turl.length+1)
			fin	 = sub1.indexOf('&');
			if (fin == -1)	fin = sub1.length+1;
			sub2 = sub1.substring(0,fin)
			return sub2.replace(val+'=','');
			}
		else	return false;
}


