// =========================================================================
//                          String functions 
// =========================================================================
// Removes leading whitespaces
function LTrim( value ) {
	
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
	
}

// Removes ending whitespaces
function RTrim( value ) {
	
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
	
}

// Removes leading and ending whitespaces
function trim( value ) {
	
	return LTrim(RTrim(value));
	
}
// =========================================================================
//                          Urls functions 
// =========================================================================
function fileOnly(_Str) {
   var theSplit = _Str.lastIndexOf ('/');
   if (theSplit != -1)
      return _Str.substring(theSplit+1);
   else return "";
}

function pathOnly(_Str) {
   var isAt = _Str.lastIndexOf ("/")
   if (isAt != -1)
      return _Str.substring(0,isAt+1);
   else return "";
}
// =========================================================================
//                          Cookie functions 
// =========================================================================
/* This function is used to set cookies */
function setCookie(name,value,expires,path,domain,secure) {
  document.cookie = name + "=" + escape (value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") + ((secure) ? "; secure" : "");
}

/* This function is used to get cookies */
function getCookie(name) {
	var prefix = name + "=" 
	var start = document.cookie.indexOf(prefix) 

	if (start==-1) {
		return null;
	}
	
	var end = document.cookie.indexOf(";", start+prefix.length) 
	if (end==-1) {
		end=document.cookie.length;
	}

	var value=document.cookie.substring(start+prefix.length, end) 
	return unescape(value);
}

/* This function is used to delete cookies */
function deleteCookie(name,path,domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

// =========================================================================
//                          Open window functions 
// =========================================================================
/* open glossary */ 
function openGlossaire(myAnchor){  
	w = 700;
	h = 500;
	nameWindow = "glossaire";
	posX= (screen.availWidth/2)-w/2 ;
	posY= (screen.availHeight/2)-h/2 ;
	fen = window.open("glossaire.do#"+myAnchor, nameWindow, "width="+w+",height="+h+",left=" + posX + ",top=" + posY + ",scrollbars");
	fen.focus();
}

/* open window */ 
function openWindow(url, nameWindow, w, h){  
	posX= (screen.availWidth/2)-w/2 ;
	posY= (screen.availHeight/2)-h/2 ;
	fen = window.open(url, nameWindow,"width="+w+",height="+h+",left=" + posX + ",top=" + posY + ",scrollbars");
	fen.focus();
}
// =========================================================================
//                          CSS functions 
// =========================================================================
function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp('(^|\\s)'+searchClass+'(\\s|$)');
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}
// =========================================================================
//                          Rollover Image functions 
// =========================================================================
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_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_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 doSubmit() {
	location.replace('login');
}
function validateTextfield(aValue,aLabel){
		if(aValue == "") {
			alert("Veuillez renseigner le champ '" + aLabel + "'.");
			return false; //if so stop
		}else{
			return true;
		}
}
function validateLogin(frm){
	
	if(validateTextfield(frm.elements['login'].value, "Login")){
		if(validateTextfield(frm.elements['password'].value, "Mot de passe")){
			return true;
		}
	}
	return false;			
}

function writeTodayIfEmpty(inputText){
	if(inputText.value == "") {
		today = new Date();
		month = today.getMonth()+1;
		inputText.value = today.getDate() + "/" + month + "/" + today.getFullYear() ;
	} 
}
function changeInputText(src, dest) {
	if(src.value != ""){
		if(src.value == "Tous"){
			dest.value = "";
		} else {
			dest.value = src.value;
		}
	}
}
function showAndHideDateTransfert(dateTransfertParagraphe, myList){
	if (myList.value.indexOf("true") != -1) {
		dateTransfertParagraphe.style.display = "none";
	} else {
		dateTransfertParagraphe.style.display = "block";
	}
}
function showAndHideLists(listToHide, listToShow, dateTransfertParagraphe, listToShowParameter){
	listToShow.style.display = "block";
	listToHide.style.display = "none";
	showAndHideDateTransfert(dateTransfertParagraphe, listToShowParameter);
}
function showAndHideElementsByDirectionFlux(myForm, listRecepteur, listEmetteur, dateTransfertParagraphe){
	var reception = 1;
	var emission = 0;
	jeRechercheRecepteurParameter = myForm.elements['jeRechercheRecepteur']; 
	jeRechercheEmetteurParameter = myForm.elements['jeRechercheEmetteur'];
	directionFlux = myForm.elements['directionFlux']; // is a radio
	if (directionFlux[reception].checked==true){ // reception
		showAndHideLists(listEmetteur, listRecepteur, dateTransfertParagraphe, jeRechercheRecepteurParameter);
	}else if (directionFlux[emission].checked==true){ // emission
		showAndHideLists(listRecepteur, listEmetteur, dateTransfertParagraphe, jeRechercheEmetteurParameter);
	} else { // none
		listRecepteur.style.display = "none";
		listEmetteur.style.display = "none"; 
		//showAndHideDateTransfert(dateTransfertParagraphe, listRecepteur);
	}	
}

/* This function avoids the border transparency bug in current navigators */
function setouterColumnContainerBorderColor() {
	document.getElementById("outerColumnContainer").style.borderColor = "transparent";
}
function updateSubRadios (val) {
	if(val=="2") {
		for(i=0; i<document.forms[0].professionnel_sante.length ; i++){
			document.forms[0].professionnel_sante[i].disabled=false;
		}
		for(i=0; i<document.forms[0].amc.length ; i++){
			document.forms[0].amc[i].disabled=true;
			document.forms[0].amc[i].checked=false;
		}
		document.forms[0].numero_adeli.disabled=false;
		document.forms[0].societe_nom_amc.disabled=true;
	}
	if(val=="1") {
		for(i=0; i<document.forms[0].amc.length ; i++){
			document.forms[0].amc[i].disabled=false;
		}
		for(i=0; i<document.forms[0].professionnel_sante.length ; i++){
			document.forms[0].professionnel_sante[i].disabled=true;
			document.forms[0].professionnel_sante[i].checked=false;
		}
		document.forms[0].societe_nom_amc.disabled=false;
		document.forms[0].numero_adeli.disabled=true;
	}
}

