function vbescape(chaine) {
    chaine=escape(chaine);
    chaine=monreplace(chaine, "+", "%2B");
    return chaine;
}

function monreplace(chaine, chercher, remplacer) {
    var inter="";
    var c;
    for(var i = 0, j=chaine.length; i<j;i++) {
    	c=chaine.charAt(i);
        if (c==chercher)
	       	inter=inter+remplacer;
        else
        	inter=inter+c;
    }
    return inter;
}

//affiche le calendrier dans une popup
//nomform est le nom du formulaire qui contient le champs où la date sera modifiée
//nomchamp est le nom du champ à modifier

function calendrier(theURL, nomform, nomchamp, winName, features) {
    var ladate = document.forms[nomform][nomchamp].value;
    theURL = theURL + "?nom_formulaire=" + nomform + "&nom_controle=" + nomchamp + "&asubmiter=false&modesmart=Y"
    theURL = theURL + "&date=" + vbescape(ladate);
    if (winName == '') winName = 'calendrier_' + nomform;
    window.open(theURL, winName, features);
}

/*
	Various JS string utilities.
	Usefull in everyday coding...
*/


// Trim function
// Conservee pour raisons historiques : elle assure de ne pas avoir de nullpointer
// ARNAUD : repasser sur la dizaine d'appels pour mettre au propre
function trimString(inputString) {
   var ret=""+inputString;
   return ret.trim();
}

// Trim function
String.prototype.trim = function () {
   var i=0;
   var j=this.length;
   var k=this.length-1;
   while ((i<j) && (this.charAt(i) == ' ')) { i=i+1; }
   while ((k>i) && (this.charAt(k) == ' ')) { k=k-1; }
   return (this.substring(i, k+1));
}

// ReplaceAll function
// Remplace tous les occurences trouvées
String.prototype.replaceAll = function (what, by) {
	what=""+what;
	by=""+by;
	var start=0, oldLen=what.length, newLen=by.length, baseLen=this.length, base=""+this;
	while ((found=base.indexOf(what, start))!=-1 && found>=start && start<base.length) {
		base=base.substring(0, found)+by+base.substring(found+oldLen, base.length);
		start=found+newLen;
	}
	return base;
}

// Endswith function
String.prototype.endsWith = function(search) {
	if (search==null) return false;
	if (search=="") return true;
	if (this.length<search.length) return false;
	if (this.substring(this.length-search.length, this.length)==search) return true;
	return false;
}

// Startswith function
String.prototype.startsWith = function(search) {
	if (search==null) return false;
	if (search=="") return true;
	if (this.length<search.length) return false;
	if (this.substring(0, search.length)==search) return true;
	return false;
}

// Countchars function
String.prototype.countChars = function(str) {
	if (!isDefined(str)) return 0;
	str=""+str;
	var found, start=0, count=0, len=str.length;
	if (len<1) return 0;
	while ((found=this.indexOf(str, start))!=-1 && found>=start) {
		count++;
		start=found+len;
	}
	return count;
}

function poster(url, target) {
	var doc=document;
	url=unescape(url);
	var frm=document.createElement("FORM");
	frm.target=target;
	frm.method="POST";
	var spl=url.split('&');
	frm.action=spl[0];
	var i, j, ctrl, sp;
	
	document.body.appendChild(frm);
	for (i=1, j=spl.length; i<j; i++) {
		ctrl=document.createElement("INPUT");
		ctrl.type="HIDDEN"
		sp=spl[i].split('=');
		ctrl.name=sp[0];
		ctrl.value=sp[1];
		frm.appendChild(ctrl);
	}
	
	frm.submit();
}

function addEvent(obj, evType, fn) {
  if (obj.addEventListener) {
    obj.addEventListener(evType, fn, false);
    return true;
  } 
  else if (obj.attachEvent) {
    var r = obj.attachEvent("on"+evType, fn);
    return r;
  } 
  else {
    alert("Handler could not be attached");
  }
} 