
/**============================================================================
 * 
 * Functions used 4 strings manipulations. 
 * The functions 'R' :
 * - lTrim, rTrim, trim ( sMainString )
 * - str_replace ( sMainString , sSubString1 , sSubString2) 
 * - g_HTML_encode ( s )
 * - g_HTML_decode ( s )
 * 
 * JAVASCRIPT versions 1.1 and over
 *
 * LICENSE: This source file is subject to version 2.0 of the CeCILL license
 * that is available through the world-wide-web at the following URI:
 * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt.  
 * 
 * @category   -
 * @package    -
 * @author     Marc DIAZ <webmaster@cvstandard.net>
 * @copyright  2005-2008 cvstandard.net
 * @licence    CeCILL (GNU GPL compatible) , http://www.cecill.info/
 * @version    1.02
 * @link       -
 * @see        -
 * @since      File available since Release 0.01
 * @deprecated -
 *
 **============================================================================
 */


/**
 * Variable used for inserting break lines in HTML
 */
sBR = "<"+"br/"+">";



/**====================
 * ltrim : keeps off spaces from left
 * 
 * @var string sMS
 * @return string
 */
function ltrim(sMS){
	var sMR="";
	if (sMS!=""){
    	var i=0;
    	while (
    	    sMS.charAt(i)==" " 
    	    || sMS.charAt(i)=="\n" 
    	    || sMS.charAt(i)=="\t" 
    	    || sMS.charAt(i)=="\t" 
    	    || sMS.charAt(i)=="\r"
    	    || sMS.charAt(i)=="\0"
    	    ){i++}
	    sMR=sMS.substring(i-1,sMS.length);
    	return(sMR);
	}
	else{
	    return "";
    }
}



/**====================
 * rtrim : keeps off spaces from right
 * 
 * @var string sMS
 * @return string
 */
function rtrim(sMS){
    if (sMS!=""){
    	var i=sMS.length-1;
    	var sMR="";
    	while (
    	    sMS.charAt(i)==" " 
    	    || sMS.charAt(i)=="\n" 
    	    || sMS.charAt(i)=="\t" 
    	    || sMS.charAt(i)=="\t" 
    	    || sMS.charAt(i)=="\r"
    	    || sMS.charAt(i)=="\0"
    	    ){i--}
    	sMR=sMS.substring(0,i+1);
    	return(sMR);
    }
    else{
        return "";
    }
}



/**====================
 * trim : keeps off spaces from left & right
 * 
 * @var string sMS
 * @return string
 */
function trim(sMS){
	var sMR="";
	return(rtrim(ltrim(sMS)));
}



/**====================
 * Replace a substring1 by a substring2 in a mainstring
 * 
 * @var string sMS the mainstring
 * @var string sSS1 the substring1 to be replaced
 * @var string sSS2 the substring2 going to replace
 * @return string
 */
function str_replace(sMS,sSS1,sSS2) {
	if (sMS!="" && sSS1!=""){
		b=0;iP=0;iLP=0;sTmp="";
		while (!b){
			iP=sMS.indexOf(sSS1,iLP);
			if (iP==-1){b=1;sTmp+=sMS.substring(iLP,sMS.length);}
			else{sTmp+=sMS.substr(iLP,iP-iLP)+sSS2;iLP=iP+sSS1.length;}
		}
		return sTmp;
	}
	else{return sMS;}
}



/**====================
 * g_HTML_encode
 * 
 * <code>
 *
 * s = g_HTML_encode( "c'est déjà demain!" );
 *   -> C&#39;est d&#233;j&#224; demain!
 *
 * </code>
 *
 **
 */
function g_HTML_encode(s){
    sResult="";
    if (s!=""){
        s=str_replace(s,"&","&amp;");
        //s=str_replace(s,String.fromCharCode(34),"&#34;");//&quote;
        //s=str_replace(s,String.fromCharCode(39),"&#39;");//\'
        s=str_replace(s,String.fromCharCode(94),"&#94;");//&circ;
        s=str_replace(s,"<","&lt;");
        s=str_replace(s,">","&gt;");
        
        iHTML=0;
        iHTMLMax=s.length;
        while (iHTML<iHTMLMax){
            if (s.charCodeAt(iHTML)>127){
                sResult+="&#"+s.charCodeAt(iHTML)+";";    
            }
            else{
                sResult+=s.charAt(iHTML);
            }
            iHTML++;
        }
    }
    return(sResult);
}



/**====================
 * g_HTML_decode
 * 
 * <code>
 *
 * s = g_HTML_decode( "C&#39;est d&#233;j&#224; demain!" );
 *   -> c'est déjà demain!
 *
 * </code>
 *
 **
 */
function g_HTML_decode(s){
    //s=str_replace(s,"&","&amp;");
    if (s!=""){

        //s=str_replace(s,"&#34;",String.fromCharCode(34));
        //s=str_replace(s,"&#39;",String.fromCharCode(39));
        s=str_replace(s,"&#94;",String.fromCharCode(94));
        s=str_replace(s,"&lt;","<");
        s=str_replace(s,"&gt;",">");
    
        var regXML2Text=new RegExp("&#[0-9]{3,5};","g");
        //alert(s.match(regXML2Text));
        if (aMatch=s.match(regXML2Text)){
            iMatchMax=aMatch.length;
            iMatch=0;
            while (iMatch<iMatchMax){
                iCharMatch=aMatch[iMatch].substr(2,aMatch[iMatch].length-3);
                s=str_replace(s,aMatch[iMatch],String.fromCharCode(iCharMatch));
                iMatch++;
            }
        }
    }    
    
    return(s);
}


