// Check Browser Version Andreas Bauer
function browserVersion(path) {
	this.path = (path) ? path : '../code/style/';
	this.isDynamic = (!window.navigator.appVersion.indexOf('4.') || !window.navigator.appVersion.indexOf('5.'));
	var version = navigator.appVersion
	var v = parseInt(version)
	var agent = navigator.userAgent.toLowerCase();
	this.isMac = (agent.indexOf('mac') >= 0);
	this.isLinux = (agent.indexOf('inux') >= 0);
	this.isOpera = (agent.indexOf('opera') >= 0);
	this.name = window.navigator.appName;
	this.isIE = (this.name == "Microsoft Internet Explorer");	
	this.isNS = (this.name =="Netscape");	
	this.isNS5 = ((this.name=="Netscape" || this.name=="Mozilla" ) && v==5);
	this.css = '<LINK REL="stylesheet" HREF="' + this.path;
	if(this.isMac)	this.css += 'Mac';
	if(this.isLinux) this.css += 'Linux';
	if(this.isIE)	this.css += 'IEStyle';
	else if (this.isNS) this.css += 'NSStyle';
	else this.css += 'IEStyle';
	this.css += (this.css == '' ? '' : '.css" TYPE="text/css">');
}

//b = new browserVersion();

function screenInfo() {
	if(b.isDynamic)	{	
		if(b.isIE) {
			this.width = document.body.clientWidth;
			this.height = document.body.clientHeight;
		}
		else if (b.isNS)  {
			this.width = window.innerWidth;	
			this.height = window.innerHeight; 		
		}
		else if (b.isOpera)  {
			this.width = window.innerWidth;	
			this.height = window.innerHeight; 		
		}
	}	
}

function frameLoader(frameset) {
	if(top==self) { 
		this.frameset = (frameset) ? frameset : "../index.htm";
		this.currURL = unescape(window.location.pathname);
		this.newURL = this.frameset + "?" + this.currURL;
		if (b.isDynamic)
			this.redirect = 'location.replace('+this.newURL +')';
		else
			this.redirect = 'location.href ='+ this.newURL;
	}
	else this.redirect = "";
}


function getLayerHandle(id, nestref) {
	var hObj;
	// = (parent.frames.length > 0) frames
	if(b.isIE) hObj = document.all[id].style;
	if(b.isOpera) hObj = document.getElementById(id).style;
	if(b.isNS && !b.isOpera) {
		if(b.isNS5) hObj = document.getElementById(id).style;
		else hObj = (nestref)? eval(nestref+'.document.'+id) : document.layers[id];		
	}		
	return hObj;
}
// function which position and show or hide layers - Andreas Bauer
function handleLayer(layername, pixelx, pixely, view, nestref) {
	if(b.isDynamic)	{
		var obj = getLayerHandle(layername, nestref);
		if(b.isIE || b.isOpera) { 
			obj.pixelLeft = pixelx;
			obj.pixelTop = pixely;
		}
		else {
			obj.left = pixelx;
			obj.top = pixely;
		}
		showLayer(layername,view, nestref);
	}	
}

function moveLayerBy(layername, pixelx, pixely, nestref) {
	if(b.isDynamic){
		var obj = getLayerHandle(layername, nestref);	
		if(b.isNS5)	{
			obj.left = parseInt(obj.left) + pixelx;
			obj.top = parseInt(obj.top) + pixely;
		}
		if (b.isNS && !b.isNS5) {
			obj.moveBy(pixelx,pixely);						
		}
		if(b.isIE) {
			obj.pixelLeft += pixelx;
			obj.pixelTop += pixely;				
		}
	}
}

function isLayerVisible(layername, nestref) {
	var bVisible = false;
	if(b.isDynamic)	{
		var obj = getLayerHandle(layername, nestref);
		 bVisible = (b.isNS) ? ( obj.visibility == "show") : (obj.visibility == "visible");		
	}
	return bVisible;
}

function showLayer(layername,view, nestref) {
	if(b.isDynamic)	{
		var obj = getLayerHandle(layername, nestref);
		if(view) obj.visibility = (b.isNS && !b.isNS5 && !b.isOpera) ? "show" : "visible";
		else obj.visibility = (b.isNS && !b.isNS5 && !b.isOpera) ? "hide" : "hidden";
	}			
}

function displayLayer(layername,view) {
  if(b.isIE) {
			document.all[layername].style.display = (view) ? '' : 'none';	
	}
	else if(b.isNS5) {
			document.getElementById(layername).style.display = (view) ? 'block' : 'none';
	}
}

function writeToLayer(layername,txt, nestref) {
	var hObj;
	if(b.isDynamic) {
		if(b.isIE) document.all[layername].innerHTML = txt;
		else if(b.isOpera) document.getElementById(layername).innerHTML = txt;//alert("write don't work with Opera");
		else if(b.isNS) {
			if(b.isNS5) document.getElementById(layername).innerHTML = txt;
			else {
				hObj = (nestref)? eval("document."+nestref+".document."+layername+".document") : document.layers[layername].document;  // nestref todo
				with(hObj) {open();	write(txt);	close();}
			}
		}				
	}	
}

function getLayerId(index,nestref) {
	if(b.isIE) returnHtml = document.all.tags("DIV").item(index).id;
	if(b.isNS5 || b.isOpera) returnHtml = document.getElementsByTagName("DIV").item(index).id;
	if(b.isNS && !b.isNS5) returnHtml = (nestref) ? eval(nestref+'.document.layers["'+index+'"].id') : document.layers[index].id;
	return returnHtml;
}

function getLayerCount(nestref) {
	var nLayers = 0; 
	if(b.isIE) nLayers = document.all.tags("DIV").length; 
	if(b.isNS5) nLayers = document.getElementsByTagName("DIV").length;
	//if(b.isOpera) alert(document.getElementsByTagName());
	if(b.isNS && !b.isNS5) nLayers = (nestref) ? eval(nestref+".document.layers.length") : document.layers.length; 	
	return nLayers;
}

function resizeIt() {
	if(b.isIE) start();
	else document.location = document.location;
}

function panelHandler(nr,path) {
	if(b.isIE) {
		if(document.all['contentDiv'+nr].style.display =='none') {
			document.all['contentDiv'+nr].style.display = '';
			document.all['headerImg'+nr].src = path +'img/jslib/expandPanel/gotop.gif';	
		}
		else {
			document.all['contentDiv'+nr].style.display = 'none';
			document.all['headerImg'+nr].src = path +'img/jslib/expandPanel/expand.gif';	
		}
	}
	else if(b.isNS5) {
		if(document.getElementById('contentDiv'+nr).style.display =='none') {
			document.getElementById('contentDiv'+nr).style.display = 'block';
			document.getElementById('headerImg'+nr).setAttribute("src",path +'img/jslib/expandPanel/gotop.gif');	
		}
		else {
			document.getElementById('contentDiv'+nr).style.display = 'none';
			document.getElementById('headerImg'+nr).setAttribute("src",path +'img/jslib/expandPanel/expand.gif');	
		}
	}
}

var marked_row = new Array;
var marked_row_index = 0;
/**
 * Sets/unsets the pointer and marker in browse mode
 *
 * @param   object    the table row
 * @param   interger  the row number
 * @param   string    the action calling this script (over, out or click)
 * @param   string    the default background color
 * @param   string    the color to use for mouseover
 * @param   string    the color to use for marking a row
 *
 * @return  boolean  whether pointer is set or not
 */
function setPointer(theRow, theRowNum, theAction, theDefaultColor, thePointerColor, theMarkColor)
{
    var theCells = null;
    
    // 1. Pointer and mark feature are disabled or the browser can't get the
    //    row -> exits
    if ((thePointerColor == '' && theMarkColor == '')
        || typeof(theRow.style) == 'undefined') {
        return false;
    }

    // 2. Gets the current row and exits if the browser can't get it
    if (typeof(document.getElementsByTagName) != 'undefined') {
        theCells = theRow.getElementsByTagName('td');
    }
    else if (typeof(theRow.cells) != 'undefined') {
        theCells = theRow.cells;
    }
    else {
        return false;
    }

    /****************************************/
    /***   traitement des dossiers ***/
    if(theAction=='click') {
      var currentId = theRow.getAttribute('id');
      var oldId = document.frmDossier.selectedId.value;
      document.frmDossier.selectedId.value = currentId;
      if(oldId>0) {
        var oldRow = document.getElementById(oldId);
        currentCells = oldRow.getElementsByTagName('td');
        for(j=0;j<currentCells.length;j++) {
           currentCells[j].style.backgroundColor = theDefaultColor;
        }
      }
      for(j=0;j<theCells.length;j++) {
         theCells[j].style.backgroundColor = theMarkColor;
      }

      /*for(i=0;i<theRow.parentNode.childNodes.length;i++) {
        currentRow = theRow.parentNode.childNodes[i];
        currentCells = currentRow.getElementsByTagName('td');
        if(currentRow.id == currentId) {
          for(j=0;j<currentCells.length;j++) {
            currentCells[j].style.backgroundColor = theMarkColor;
          }          
        }
        else {
          for(j=0;j<currentCells.length;j++) {
            currentCells[j].style.backgroundColor = theDefaultColor;
          }
        }
      }*/
    }

    /* set color on mouse over */
    if(theAction=='over') {
        currentCells = theRow.getElementsByTagName('td');
        for(j=0;j<currentCells.length;j++) {
            currentCells[j].style.backgroundColor = thePointerColor;
        }
    }

    /* reset color on mouse out */
    if(theAction=='out') {
        if(theRow.getAttribute('id')!=document.frmDossier.selectedId.value) {
          currentCells = theRow.getElementsByTagName('td');
          for(j=0;j<currentCells.length;j++) {
            currentCells[j].style.backgroundColor = theDefaultColor;
          }
        }
    }

   
    return true;
} // end of the 'setPointer()' function

function hideModifyMenu() {
  showLayer('modifymenuDiv',false);
}

function hideCopieMenu() {
  showLayer('copiemenuDiv',false);
}

function hideAdminMenu() {
  showLayer('adminmenuDiv',false);
}

function hideMenu() {
  showLayer('modifymenuDiv',false);
  showLayer('copiemenuDiv',false);
  showLayer('adminmenuDiv',false);
}

function switchColor(item) {
  item.style.backgroundColor = '#6F79A9';
  item.style.color = '#ffffff';
}

function resetColor(item) {
  item.style.backgroundColor = '#CDDAF0';
  item.style.color = '#000000';
}

// Window functions
function openPopup(url,name,width,height) {
	var hWindow;
	hWindow = window.open(url,name, "dependent=yes,toolbar=0,location=0,status=0,menubar=0,scrollbars=1,resizable=1,width="+width +",height="+height+"\"");
	hWindow.focus();
}

function openToolWnd(url,name,width,height) {
	var hWindow;
	/*var oPopup = window.createPopup();
	if(b.isIE) {
		var oPopBody = oPopup.document.body;
 	    oPopBody.style.backgroundColor = "lightyellow";
    	oPopBody.style.border = "solid black 1px";
    	oPopBody.innerHTML = "Click outside <B>popup</B> to close.";
    	oPopup.show(100, 100, 180, 25, document.body);
	}*/	
	hWindow = window.open(url,name, "dependent=yes,toolbar=0,location=0,status=0,menubar=0,scrollbars=0,resizable=0,width="+width +",height="+height+"\"");
	hWindow.focus();
	
}

function openPrintWnd(url,wndName,width,height) {
	var hWindow;
	hWindow = window.open(url,wndName, 'dependent=yes,toolbar=1,location=0,status=0,menubar=1,scrollbars=1,resizable=1,width='+width +',height='+height);
	hWindow.focus();
}

function openReport() {
	var querystring = '';
	var doc = '';
  var type = '';
  var dateDebut = '';
  var dateFin = '';
  
	if(document.frmStats.statsCombo.value!='-1') {
		doc = document.frmStats.statsCombo.value;
    type = document.frmStats.typeImpressionCombo.value;
    dateDebut = document.frmStats.txtDateDebut.value;
    dateFin = document.frmStats.txtDateFin.value;
		doc += '?type='+type+'&debut='+dateDebut+'&fin='+dateFin;
		openPrintWnd(doc,'report'+document.frmStats.statsCombo.selectedIndex+document.frmStats.typeImpressionCombo.value,750,350);			
	}
	else {
		alert('Please select a report type!');
		document.frmStats.statsCombo.focus();
	}	
}


/* Rollover functions */
function 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 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=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 findObj(n, d) { //v3.0
  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=findObj(n,d.layers[i].document); return x;
}

function swapImage() { //v3.0
  var i,j=0,x,a=swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
/* End Rollover functions */

function setCookie (name, value, expires) {
	document.cookie = name + "=" + escape(value) + ((expires == null) ? "" : ("; expires=" + expires.toGMTString()));
}
