// Detection du browser
var is_ns = false;
var is_ie = false;
var browser = navigator.userAgent.toLowerCase();
if (browser.indexOf('netscape') >= 0) is_ns = true;
if (browser.indexOf('mozilla') >= 0) is_ns = true;
if (browser.indexOf('msie') >= 0) { is_ie = true; is_ns = false }

function windowOpen(url, name, x, y) {
	var param = 'width=' + x + ',height=' + y + ',scrollbars=yes';
	param = param + ',left=' + (screen.width - x) / 2;
	param = param + ',top=' + (screen.height - (y + 30)) / 2;

	var a = window.open(url,name,param);
}

function windowOpen2(url, name, x, y, param) {
	var pos = 'width=' + x + ',height=' + y ;
	pos = pos + ',left=' + (screen.width - x) / 2;
	pos = pos + ',top=' + (screen.height - (y + 30)) / 2;

	var a = window.open(url,name, pos + ',' + param);
}

function getObject(objName) {
  if (document.layers) {
    return document.layers[objName].document;
  }
  else if (document.getElementById) {
    return document.getElementById(objName);
  }
  else if (document.all) {
    return window[objName];
  } 
}

function dynamic(objName, html) {
	var obj = getObject(objName);
	
	if (document.layers) {
    obj.open("text/html");
    obj.write(html);
    obj.close();
	}
  else if (document.all) {
    if (obj.innerHTML != html) obj.innerHTML = html;
  } 
  else if (document.getElementById) {
    if (obj.innerHTML != html) obj.innerHTML = html;
  }

/*	
  if (document.layers) {
    var doc = document.layers[objName].document;
    doc.open("text/html");
    doc.write(html);
    doc.close();
  }
  else if (document.all) {
    if (window[objName].innerHTML != html) window[objName].innerHTML = html;
  } 
  else if (document.getElementById) {
    if (document.getElementById(objName).innerHTML != html) document.getElementById(objName).innerHTML = html;
  } */
}

function showHide(objName, state) {
  if (document.layers) {
	document.layers[objName].visibility = state;
  }
  else if (document.getElementById) {
	document.getElementById(objName).style.visibility = state;
  }
}

function getShowHide(objName) {
  if (document.layers) {
	result = document.layers[objName].visibility;
	if (result == 'hide') result = 'hidden';
	return result;
  }
  else if (document.getElementById) {
  	result = document.getElementById(objName).style.visibility;
  	if (result == 'hide') result = 'hidden';
	return result;
  }
}

function validEmail(email) {
	var goodChar = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789@.-_";
	
	var valid = true;
	
	if (email.indexOf("@") < 1) valid = false;
	if (email.indexOf(".") < 1) valid = false;
	if (email.lastIndexOf(".") + 1 == email.length) valid = false;
	if (email.indexOf("@") != email.lastIndexOf("@")) valid = false;
	if (email.lastIndexOf(".") < email.indexOf("@")) valid = false;
	if (email.indexOf(".@") >= 0) valid = false;
	if (email.indexOf("@.") >= 0) valid = false;
	
	var temp;
	while (email.length > 0) {
		temp = email.substring(0,1);
		email = email.substring(1, email.length);
		
		if (goodChar.indexOf(temp) == -1) {
			valid = false
			email = "";
		}
	}

	return valid;
}

/******************  drop down *********************/
var tampon = "";
var keyPressOldObj;

function keyPress(obj) {
	var x;
	
	// Si l'objet change depuis la derniere fois, remise du tampon à 0
	if (keyPressOldObj != obj) {
		keyPressOldObj = obj;
		tampon = "";
	}

	// Si touche = ESC, vider le tampon
	if (event.keyCode == 27) {
		tampon = ""
		event.returnValue = false;
		return;
	}

	// Cumule les touches dans le tampon
	tampon += String.fromCharCode(event.keyCode).toLowerCase();
	
	// Compare le début du tampon avec chaque valeur de la liste
	for (x=0; x < obj.length; x++) {
		if (obj[x].text.substring(0, tampon.length).toLowerCase() == tampon) {
			// s'arrete sur le 1er qui concorde et le sélectionne
			obj[x].selected = true;
			x = obj.length;
		}
	}

	event.returnValue = false;
}
/********************** fin drop down *************************/

function make_new(id, text) {
	var retval = document.createElement("OPTION");
	retval.text = text;
	retval.value = id;
	//retval.setAttribute("text", text);
	//retval.setAttribute("value", id);
	return retval;
}

// vérifie si le nombre est valide
function isNumeric(sText) {
  var ValidChars = "0123456789.,";
  var Char;
 
  for (i = 0; i < sText.length; i++) { 
    Char = sText.charAt(i); 
    if (ValidChars.indexOf(Char) == -1) {
      return false;
    }
  }
  
  return true;
}

// Retourne la hauteur utilisable du browser;
function getClientHeight() {
	if (window.innerHeight) {
		return window.innerHeight;
	} else if (document.body.clientHeight) {
	  return document.body.clientHeight;
	} else if (document.documentElement.clientHeight) {
	  return document.documentElement.clientHeight;
	}
}

// Retourne la largeur utilisable du browser;
function getClientWidth() {
	if (window.innerWidth) {
		return window.innerWidth;
	} else if (document.body.clientWidth) {
	  return document.body.clientWidth;
	} else if (document.documentElement.clientWidth) {
	  return document.documentElement.clientWidth;
	}
}

function fct_format_number(v_number, v_decimals)
/* ============================================================
	Cette fonction javascript formatte un nombre
============================================================ */
{
	var v_formatted_number;
	var v_index;
	var v_round = 10;
	var i;
	
	for(i = 0 ; i < parseInt(v_decimals); i++)
		v_round *= 10

	v_formatted_number = String(v_number + (5 / v_round));

	i = 0

	while(i < v_formatted_number.length && v_formatted_number.charAt(i) != '.')
		i++;

	v_index = (i >= v_formatted_number.length) ? -1 : i;

	if(v_index >= 0)
	{
		if(v_index + parseInt(v_decimals) > v_formatted_number.length - 1)
		{
			i = v_formatted_number.length;

			while(v_index < i)
			{
				v_formatted_number = v_formatted_number + '0';
				v_index++;
			}
		}
		else
			v_formatted_number = v_formatted_number.slice(0,v_index + parseInt(v_decimals) + 1)
	}
	else
	{
		v_formatted_number = v_formatted_number + '.';

		for(i = 0 ; i < v_decimals ; i++)
			v_formatted_number = v_formatted_number + '0';
	}

	return v_formatted_number;
}

// Positionne le pop-up javascript au milieu de l'écran, selon les dimensions X et Y
function popupPos(x, y) {
  var str;
  str = 'width=' + x + ',height=' + y;
  str = str + ',left=' + (screen.width - x) / 2;
  str = str + ',top=' + (screen.height - x) / 2;
  
  return str;
}

// Ouvre le pop-up calendrier, avec comme parametre le nom du formulaire et du champs de la page Parent
// Ajoutez ces 3 evennements au champ :
// ondblclick="javascript:openCalendrier('frm_add_new_entry','TB_DATE');"
// onfocus="javascript:window.status='< %=replace(v_text_calendar_status, "'", "\'")% >';"
// onblur="window.status='TymeWeb';"
function openCalendrier(frm, field) {
  var a=window.open('calendrier.asp?frm=' + frm + '&field=' + field, 'calendrier',popupPos(200,200));	
}


function setMouseListen() {
  if (document.layers) { // Netscape
    document.captureEvents(Event.MOUSEMOVE);
    document.onmousemove = captureMousePosition;
  } else if (document.all) { // Internet Explorer
    document.onmousemove = captureMousePosition;
  } else if (document.getElementById) { // Netcsape 6
    document.onmousemove = captureMousePosition;
  }
}

xMousePos = 0; // Horizontal position of the mouse on the screen
yMousePos = 0; // Vertical position of the mouse on the screen
xMousePosMax = 0; // Width of the page
yMousePosMax = 0; // Height of the page

function captureMousePosition(e) {
    if (document.layers) {
        // When the page scrolls in Netscape, the event's mouse position
        // reflects the absolute position on the screen. innerHight/Width
        // is the position from the top/left of the screen that the user is
        // looking at. pageX/YOffset is the amount that the user has
        // scrolled into the page. So the values will be in relation to
        // each other as the total offsets into the page, no matter if
        // the user has scrolled or not.
        xMousePos = e.pageX;
        yMousePos = e.pageY;
        xMousePosMax = window.innerWidth+window.pageXOffset;
        yMousePosMax = window.innerHeight+window.pageYOffset;
    } else if (document.all) {
        // When the page scrolls in IE, the event's mouse position
        // reflects the position from the top/left of the screen the
        // user is looking at. scrollLeft/Top is the amount the user
        // has scrolled into the page. clientWidth/Height is the height/
        // width of the current page the user is looking at. So, to be
        // consistent with Netscape (above), add the scroll offsets to
        // both so we end up with an absolute value on the page, no
        // matter if the user has scrolled or not.
        xMousePos = window.event.x+document.body.scrollLeft;
        yMousePos = window.event.y+document.body.scrollTop;
        xMousePosMax = document.body.clientWidth+document.body.scrollLeft;
        yMousePosMax = document.body.clientHeight+document.body.scrollTop;
    } else if (document.getElementById) {
        // Netscape 6 behaves the same as Netscape 4 in this regard
        xMousePos = e.pageX;
        yMousePos = e.pageY;
        xMousePosMax = window.innerWidth+window.pageXOffset;
        yMousePosMax = window.innerHeight+window.pageYOffset;
    }
}

