var lastClick;

function rowClickHandler (highlightColor, DefaultColor, event, last)
{
	if (document.all || document.getElementById)
	{
		// Le navigateur est IE
		if (event.type == 'mouseover')
		this.bgColor = highlightColor; // Colorie la ligne couramment sélectionnée
		if (event.type == 'mouseout')
		this.bgColor = DefaultColor; // Annule la coloration de la ligne couramment sélectionnée
		
		if(event.type == 'click') {
			if(last)
				last.bgColor = DefaultColor;
			this.bgColor = highlightColor;
			lastClick = this;
		}
	}
}

function initRowClickHandler(highlightColor)
{
	if (document.all || document.getElementById)
	{
		this.bgColor = highlightColor;
		lastClick = this;
	}
}

// desactive tous les input de validation de la page
function desactiver(obj, label) 
{ 
	for(i=0; i<document.forms.length; i++)
	{
		for(j=0; j<document.forms[i].elements.length; j++)
		{
			try
			{
				if((document.forms[i].elements[j].type == "submit") || (document.forms[i].elements[j].type == "image") || (document.forms[i].elements[j].type == "button"))
				{	
					document.forms[i].elements[j].disabled = "yes";
					//document.forms[i].elements[j].value=label;
				}
			}
			catch(e)
			{ /** Do nothing **/}
		}
	}

	//obj.value=label;
	try
	{
		obj.form.submit();
	}
	catch (iebug)
	{	
		// the bug is raised when an invalid filepath is given 
		// to occur this errors we set file value to ""
		for(i=0; i<document.forms.length; i++)
		{
			for(j=0; j<document.forms[i].elements.length; j++)
			{
				try
				{
					if((document.forms[i].elements[j].type == "file"))
					{	
						document.forms[i].elements[j].disabled = "yes";
					}
				}
				catch(e)
				{ /** Do nothing **/}
			}
		}
		obj.form.submit();
	}
	
	
	// display send form box
	try
	{
		document.getElementById("patientez").className = "alert-visible";
		
		// detect navigator ie and hide select manually if so
		var agt=navigator.userAgent.toLowerCase();
		var is_ie = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
		if(is_ie) fSwapSelect("patientez");
	}
	catch(e2)
	{}
}

// open popup window and make it focused
function openWindow(url, name, width, height)
{
  //window.alert(url + "\n" + name);
  // open popup window
  popup = window.open(url,name,'toolbar=0,location=0,directories=0,menuBar=0,scrollbars=1,resizable=1,width=' + width +',height=' + height + ',left=10,top=10');

  // Force focus
  if(popup.window.focus){popup.window.focus();}
}

// try to show or hide an element
// TODO : ajouter un agrument pour permettre de reinitiliaser le bon display {inline etc}
function showHide(elementID, boolhide)
{
	if(boolhide == undefined)
	{	if (document.getElementById(elementID))
		{
			
			if(document.getElementById(elementID).style.display == "block")
				document.getElementById(elementID).style.display = "none";
			else
				document.getElementById(elementID).style.display = "block";
		}
	}
	else
	{
	
		if (document.getElementById(elementID))
		{	
			if(boolhide == true)
				document.getElementById(elementID).style.display = "none";
			else
				document.getElementById(elementID).style.display = "block";
		}
	}
}

function trim(string)
{
return string.replace(/(^\s*)|(\s*$)/g,'');
} 

/****************************************
 *										*
 *     masquer les select sous ie		*
 *										*
 ****************************************/

var fDomOffset = function( oObj, sProp )
{ 
 // oObj : [Object] - Objet dont on désire le positionnement 
 // sProp : [String] - Propriété désirée : offsetLeft - offsetTop 
 var iVal = 0; 
 // On boucle tant que l'on n'est pas à la racine du document 
 while (oObj && oObj.tagName != 'BODY') { 
  eval('iVal += oObj.' + sProp + ';');
  oObj = oObj.offsetParent; 
 } 
 return iVal; 
}  

var fSwapSelect = function( sId ) 
{ 
 oObj = document.getElementById(sId); 
 Top_Element  = fDomOffset(oObj, 'offsetTop'); 
 Left_Element  = fDomOffset(oObj, 'offsetLeft'); 
 Largeur_Element  = oObj.offsetWidth; 
 Hauteur_Element  = oObj.offsetHeight; 
 oSelects = document.getElementsByTagName('SELECT'); 
 if (oSelects.length > 0) { 
  for (i = 0; i < oSelects.length; i++) { 
   oSlt = oSelects[i]; 
   Top_Select = fDomOffset(oSlt, 'offsetTop'); 
   Left_Select = fDomOffset(oSlt, 'offsetLeft'); 
   Largeur_Select = oSlt.offsetWidth; 
   Hauteur_Select = oSlt.offsetHeight; 
   isLeft = false; 
   if ((Left_Element > (Left_Select - Largeur_Element)) && (Left_Element < (Left_Select + Largeur_Select))) { 
    isLeft = true; 
   } 
   isTop = false; 
   if ((Top_Element > (Top_Select - Hauteur_Element)) && (Top_Element < (Top_Select + Hauteur_Select))) { 
    isTop = true; 
   } 
   if (isLeft && isTop) { 
    sVis = (oObj.style.visibility == 'hidden') ? 'visible' : 'hidden'; 
    if (oSlt.style.visibility != sVis) {oSlt.style.visibility = sVis;} 
   } else { 
    if (oSlt.style.visibility != 'visible') {oSlt.style.visibility = 'visible';} 
   } 
  } 
 } 
}

function desact(obj, label) { 
obj.value=label; 
}


//---------------------------- MWK SESSION TIMER --------------------------



// general function
function co_pad ( num ) { return ( ( num > 9 ) ? num : "0" + num ); }

var debugCounter = 0;
function co_debugMsg(msg) {
  // un rem the next line when you go live to prevent debug messages from showing
  return;
  debugCounter++;
  var t = document.createTextNode("\n" + co_pad(debugCounter) + ": " + msg);
  var br = document.createElement("br");
  var debug = document.getElementById("debug");
  debug.appendChild(t);
  debug.appendChild(br);
}

function co_redirect_refresh_page() {
  window.location=redirect;
}

function co_refresh_alert() {
  //co_debugMsg("Timeout !!");
  //alert("La c'est fini !!");
}

function co_prealert() {
  //co_debugMsg("Timeout !!");
  document.getElementById("timeout").className = "alert-timeout-visible";
  boxalertdisplayed=true;
}

function co_seconds_elapsed () {
	counttime=counttime-1;
	return counttime;
}

function co_close() {
	document.getElementById('timeout').className = 'alert-invisible';
	boxalertdisplayed=false;
	clearTimeout(jstimer);
	co_start_countdown();
}
// general function

function co_dmsg(ehour,emin,esec) {
  var msg1 = co_pad(ehour) + ":" + co_pad(emin) + ":" + co_pad(esec);  
  if(ehour<=0 && emin<5 && !boxalertdisplayed){
	co_prealert();
  }
  if(ehour<=0 && emin<=0 && esec<1){ 
    //co_redirect_refresh_page();
    co_refresh_alert();
  }
  if (document.layers){
    document.countdownnsmain.document.countdownnssub.document.write(msg1);
    document.countdownnsmain.document.countdownnssub.document.close();
  }
  else if (document.all||document.getElementById){
    crosscount.innerHTML = msg1;
  }
}

function co_countdown(){
  if(counttime>1) jstimer=setTimeout("co_countdown()",onesec);
  var secs = co_seconds_elapsed ();   
  var mins = Math.floor ( secs / 60 );   
  secs = secs - (mins * 60);
  var hour = Math.floor ( mins / 60 ); 
  mins = mins - (hour * 60);
  co_dmsg(hour,mins,secs);
}

function co_start_countdown(){
  if (jstimer != null) clearTimeout(jstimer);
  boxalertdisplayed=false;

  counttime=initialcounttime;
  if (document.layers) document.countdownnsmain.visibility="show";
  else if (document.all||document.getElementById)
    crosscount = document.getElementById&&!document.all ? document.getElementById("countdownie") : countdownie;
  
  co_debugMsg("Initialisation avec COUNTTIME = " + counttime );
  co_countdown();
}

//---------------------------- END : MWK SESSION TIMER --------------------------

/**
 * Center element (horizontally and vertically in a window)
 * BV : TODO Fix height and width value of element (return 0,0) default value set (460,400)
 * default value @see css box-visible
 */
function centerElement(element){
  
	// window inner size
	var pageWidth,pageHeight;
	if (self.innerHeight) // all except Explorer
	{
		pageWidth = self.innerWidth;
		pageHeight = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
		// Explorer 6 Strict Mode
	{
		pageWidth = document.documentElement.clientWidth;
		pageHeight = document.documentElement.clientHeight;
	}
	else if (document.body) // other Explorers
	{
		pageWidth = document.body.clientWidth;
		pageHeight = document.body.clientHeight;
	}
    //window.alert("page width height : " +pageWidth  + " " + pageHeight);
	
	// scroll position
	var sTop,sLeft;
	if (self.pageYOffset) // all except Explorer
	{
		sLeft = self.pageXOffset;
		sTop = self.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop)
		// Explorer 6 Strict
	{
		sLeft = document.documentElement.scrollLeft;
		sTop = document.documentElement.scrollTop;
	}
	else if (document.body) // all other Explorers
	{
		sLeft = document.body.scrollLeft;
		sTop = document.body.scrollTop;
	}  
  //window.alert("scroll top left  : " + sTop + " " +sLeft); 
	
  // dimension div à positionner
  var elementHeight=(document.getElementById(element).offsetHeight == 0)?460:document.getElementById(element).offsetHeight;//hauteur de l'élément à positionner
  var elementWidth=(document.getElementById(element).offsetWidth == 0)?400:document.getElementById(element).offsetWidth;//largeur de l'élément à positionner
  //window.alert("div width height  : " + elementWidth + " " +elementHeight); 
 
  // calcul des positions
  var posY=(pageHeight/2)-(elementHeight/2)+sTop;//Calcul de la position en Y
  var posX=(pageWidth/2)-(elementWidth/2)+sLeft;//Calcul de la position en X
  //window.alert("div posX posY  : " + posX + " " +posY); 
 
  document.getElementById(element).style.top=posY+"px";
  document.getElementById(element).style.left=posX+"px";  
}

//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

function showSelectBoxes(){
	selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "visible";
	}
}

// ---------------------------------------------------

function hideSelectBoxes(){
	selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "hidden";
	}
}


