//Coche ou decoche toutes les checkboxs de la page autre que celle d'ou est appele cette fonction
var codes;
function selectAll(){
	var checks = document.forms[0].checkboxes;
	var el = document.forms[0].elements;
	if(!this.checked){
		for(i=0;i< el.length;i++){
			if(el[i].type=='checkbox')
		
				el[i].checked = true;
		}
		this.checked = true;
	}
	else{
		for(i=0;i< el.length;i++){
			if((el[i].type=='checkbox'))
				el[i].checked = false;
		}
		this.checked = false;
	}
}

//Controle si aValue n'est pas vide et si aValue est differente de toutes les valeurs de codes
function checkCode(aValue, codes, codeExist,codeEmpty) {
	var trouve = false;
	if (aValue!=''){
		for(i=0;i<codes.length;i++){
			if(codes[i]==aValue) {
			
				trouve = true;
				message = codeExist;
			}
		}
	}
	else {
		trouve = true;
		message=codeEmpty;
	}
	
	if(trouve) {
		alert(message);
		return (false);
	}
	else
		return(true);
}

//Acces au layer de non name selon le navigateur
function getObj(name){
  if (document.getElementById){
        this.obj = document.getElementById(name);
        this.style = document.getElementById(name).style;
  }
  else if (document.all){
        this.obj = document.all[name];
        this.style = document.all[name].style;
  }
  else if (document.layers){
        this.obj = getObjNN4(document,name);
        this.style = this.obj;
  }
}

//Routine pour NN4
function getObjNN4(obj,name){
        var x = obj.layers;
        var foundLayer;
        for (var i=0;i<x.length;i++){
                if (x[i].id == name)
                        foundLayer = x[i];
                else if (x[i].layers.length)
                        var tmp = getObjNN4(x[i],name);
                if (tmp) foundLayer = tmp;
        }
        return foundLayer;
}

//Sert dans le cas de la definition d'une policy pour afficher une partie du formulaire selon le type choisi (liste ou autre)
function ddlChange(obj,layer){

	var l = new getObj(layer);
		var m = new getObj("facultatifslct");
		var m = new getObj("facultatifslct2");
	if(obj.options[obj.selectedIndex].value==5)
		l.style.visibility='inherit';
	else
		l.style.visibility='hidden';
		
	m.style.visibility=l.style.visibility;
	n.style.visibility=l.style.visibility;
}

//Pour les elements optionnels dans le cas du type liste
function chkclick(obj,layer){
	var l = new getObj(layer);
    if(obj.checked)
		l.style.visibility='hidden';
    else
        l.style.visibility='inherit';
}

//Rend visible la layer layer
function setVisible(layer){
	var l = new getObj(layer);
		
	l.style.visibility='inherit';
}

//Masque la layer layer
function setHidden(layer){
	var l = new getObj(layer);
		
	l.style.visibility='hidden';
}

function Ajouter2(N1,N2,combi){
	var val = N1.options[N1.selectedIndex].value;
	var arr = val.indexOf('/');
	
	var txt = val.substring(0,arr);
	var vl = val.substring(arr+1,val.length);
	var newItemT = vl + '/' + N2.options[N2.selectedIndex].value;
	var newItemV = txt + '/' + N2.options[N2.selectedIndex].value;
	
	var trouve = false;
	for(i=0;i<combi.options.length;i++){
		if(combi.options[i].value==newItemV)
			trouve = true;
	}
	if(trouve)
		alert('Cette valeur est deja presente');
	else{
		combi.length++;
		combi.options[combi.length-1].value = newItemV; 
		combi.options[combi.length-1].text = newItemT; 
	}	
	return false;
}

function AjouterSelection(src,dest){

	for(var i=0;i<src.options.length;i++){
		if(src.options[i].selected){
			var trouve = false;
			for(var j=0;j<dest.options.length;j++){
				if(dest.options[j].value==src.options[i].value)
					trouve = true;
			}
			if(!trouve){
				dest.length++;
				dest.options[dest.length-1].value = src.options[i].value; 
				dest.options[dest.length-1].text = src.options[i].text; 
			}
			src.options[i].selected = false;
			
		}
	}

}


//Supprime tous elements selctionnes de la listbox combi
function Supprimer(combi){

	var opt = combi.options;
	for(i=(opt.length-1);i>=0;i--){
		
		if(opt[i].selected)
			opt[i] = null;
	}
	return false;
}

function RemoveSelection(combi, message){

	var opt = combi.options;

var interdits = ''
	for(i=(opt.length-1);i>=0;i--){
		
		if(opt[i].selected){
			var trouve = false;
			if(obli!=null){
			for(var j=0;j<obli.length;j++)
				if(obli[j]==opt[i].value){
					trouve =true;
					j=obli.length;
				}
			}
			if(trouve){
				interdits = ', ' + opt[i].text + interdits
				opt[i].selected = false;
			}
			else
				opt[i] = null;
		}
	}
	if(interdits!=''){
		interdits = interdits.substring(1);
		alert(message.replace('#1',interdits));
		
	}
	//return false;
}

//Workaround pour le probleme avec les listbox .Net
//On selectionne tous les elements
function selectListBox(combi){
	combi.disabled = false;
	var opt = combi.options;
	for(i=(opt.length-1);i>=0;i--){
		
		opt[i].selected = true;

	}
	return true;
}

function CheckBoxAllNodes(all,oCheckBox){
	for(var i=0;i<oCheckBox.length;i++){
		if(document.forms[0][oCheckBox[i]]!=null)
			document.forms[0][oCheckBox[i]].checked = all.checked;
		if(all.checked)
			document.forms[0][rach+oCheckBox[i]+'_hi'].value='True';
		else
			document.forms[0][rach+oCheckBox[i]+'_hi'].value='False';
	}
	if(all.checked)
		document.forms[0][rach+all.name+'_hi'].value='True';
	else
		document.forms[0][rach+all.name+'_hi'].value='False';
}

		

function CommuteAll(all,oCheckBox){
	var coche = true
	for(var i=0;i<oCheckBox.length;i++){
		if(document.forms[0][oCheckBox[i]]!=null){
			if(document.forms[0][oCheckBox[i]].checked)
				document.forms[0][rach+oCheckBox[i]+'_hi'].value='True';
			else
				document.forms[0][rach+oCheckBox[i]+'_hi'].value='False';
				
			coche = coche && document.forms[0][oCheckBox[i]].checked;
		}
	}
	document.forms[0][all].checked = coche;
	
	if(coche)
		document.forms[0][rach+all+'_hi'].value='True';
	else
		document.forms[0][rach+all+'_hi'].value='False';
}
		
				
function PostForm(Page,QueryString) {
	document.forms[0].action = Page+QueryString;
	document.forms[0].submit();
}
		
function ChangeImages(node, imgpath, imgMain, imgPlusMinusPrefix, imgPlusMinus){

	bSetMainImage = (imgMain != ""); //don't set main image in text modes
	if (node.childNodes != null && node.childNodes.length > 0){
		if (node.childNodes.item(0).tagName == "IMG"){
			if ((imgPlusMinus != "No") && (imgPlusMinus != "own")) //"No" indicates that it is not needed Plus or Minus image
				node.childNodes.item(0).src = imgpath + imgPlusMinusPrefix + imgPlusMinus + ".gif";
			else {//if is not needed Plus/Minus image
				if (bSetMainImage) {//if node has image that must be changed
					node.childNodes.item(0).src = imgpath + imgMain;
					bSetMainImage = false;
				}
			}									
		}			
	}
	
	// process situation <A..><Checkbox><needed image></A>
	if (node.childNodes != null && bSetMainImage && node.childNodes.length > 1){
		if (node.childNodes.item(0).tagName == "INPUT" && node.childNodes.item(1).tagName == "IMG"){
			node.childNodes.item(1).src = imgpath + imgMain;
			bSetMainImage = false;
		}
	}
	
	//process next tag
	if (bSetMainImage){
	
		nextNode = node.nextSibling;
		if(nextNode!=null){
		if (nextNode.tagName == "INPUT") //skip checkbox
			nextNode = nextNode.nextSibling;

		if (nextNode.tagName == "IMG") //next tag is needed image
			nextNode.src = imgpath + imgMain;
		else //next tag is <A...>
			if (nextNode.childNodes.length > 0){
				pos = 0;
				if (nextNode.childNodes.item(0).tagName == "INPUT" && nextNode.childNodes.length > 1)	
					pos = 1; // <A..> <Checkbox> <needed image>

				if (nextNode.childNodes.item(pos).tagName == "IMG")							
					nextNode.childNodes.item(pos).src = imgpath + imgMain;
			}
		}
		}		
}
		
function ExpCol(node, imgpath, imgPlusMinus, imgCollapsed, imgExpanded, nodekey){
	if(node != null){
		nextNode = node.parentNode.parentNode.parentNode.parentNode;
		if (nextNode != null){
			nextNode = nextNode.nextSibling;
			bFinded = false;
		
			while (nextNode != null && bFinded == false){					
				if (nextNode.tagName == "DIV")
					bFinded = true;	
				else			
					nextNode = nextNode.nextSibling;	
			}
		
			if (nextNode != null && bFinded){						
		
				if (nextNode.style.display == 'none'){
					nextNode.style.display = '';
					ChangeImages(node, imgpath, imgExpanded, 'tv_minus', imgPlusMinus);
				}
				else{
					nextNode.style.display = 'none';
					ChangeImages(node, imgpath, imgCollapsed, 'tv_plus', imgPlusMinus);
				}
			}				
		}
	}
}



function ExpandAll(nodes){

	for(var i=0;i<nodes.length;i++){
		var node = document.getElementById(nodes[i]);
		
		
		if(node != null){
		
			var nextNode = node.parentNode.parentNode.parentNode.parentNode;
			if (nextNode != null){
				nextNode = nextNode.nextSibling;
				bFinded = false;
			
				while (nextNode != null && bFinded == false){					
					if (nextNode.tagName == "DIV")
						bFinded = true;	
					else			
						nextNode = nextNode.nextSibling;	
				}
			
				if (nextNode != null && bFinded){						
			
						nextNode.style.display = 'none';
				}
				if(node.click==null)
					eval(node.getAttribute("onclick").replace('this','document.getElementById(\''+nodes[i]+'\')'));
				else
					node.click();
			}
		}
	}
}

function doPost(aParamList){
					document.forms[0].action = 'javascript:openDialog(' + aParamList + ',400,600)';
					document.forms[0].submit();
				}
				
function readonly(obj){
	obj.checked = true;
	obj.blur();
	return false;
}

function selectOnly(obj){
var el = document.forms[0].elements;
	for(var i=0;i<el.length;i++){
	
		if((el[i].type=="checkbox") && (el[i]!=obj)){
			el[i].checked = false;
		}
	}
	
}

function verifDefaut(name){
	if(document.forms[0][name].checked){
	
		alert("Vous ne pouvez pas supprimer le langage par defaut");
		return(false);
	}
	else
		return(true);
}

function setPage(page, valeur){
if(this.disabled==null){
if(document.forms[0][page]!=null){
document.forms[0][page].value = valeur;
document.forms[0].InputPageBas.value='';
document.forms[0].InputPageHaut.value='';
}
}
}


function copie(src,obj){

	document.forms[0][obj].value = src.value;
}

function noenter() {

  return !(window.event && window.event.keyCode == 13); 
}


function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

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;
}

function MM_swapImage() { //v3.0

  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function checkSubmit(e){
if(document.disabl == 'true'){
	document.disabl = null;
	return false;
}
else
	return true;
}

function FocusTxt(){
	var trouve = false;
	var i=0;
	while((i<document.forms[0].elements.length)&&!trouve){
		if(document.forms[0].elements[i].type=='text'){
			document.forms[0].elements[i].focus();
			trouve = true;
		}
		else
			i = i+1;
	}
}

function calendar(field,fieldTicks,value) {
	openDialog(['calendrier.aspx','field='+field,'fieldTicks='+fieldTicks,'LaDate='+value],256,205);
	//window.open('calendrier.aspx?field='+field+'&fieldTicks='+fieldTicks+'&LaDate='+value,'CALENDRIER','resizable=no,width=256,height=205,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,');
}
		
function effacer(field, fieldTicks) {
	field.value='';
	fieldTicks.value = '';
}

function disableChk(chk){
	chk.checked = !chk.checked;
	return false;
}

function setIdem(obj,src,dest){
	if(obj.checked){
		
		for(var i=0;i<dest.options.length;i++){
			dest.options[i].selected = false;
		}
		for(var i=0;i<src.options.length;i++){
			src.options[i].selected = true;
		}
		AjouterSelection(src,dest);
		src.disabled = true;
		dest.disabled = true;
	}
	else{
		src.disabled = false;
		dest.disabled = false;
	}
	
}

function unselect(lst){
	for(var i=0;i<lst.options.length;i++){
			lst.options[i].selected = false;
		}
}

var obli;