// JavaScript Document

function verif_str(str){
	
	var keywords = str;
	var space = 0;
	
	for(var i=0; i<keywords.length;i++){
		if(keywords[i] == ' '){
			space++; 
		}else{
			return(true);
		}
	}
	
	if(space == str.length){
		return(false);
	}
	
	return(true);
}

//Cookies functions
function ecrire_cookie(nom, valeur, expires) {
		
	document.cookie=nom+"="+escape(valeur)+
	((expires==null) ? "" : ("; expires="+expires.toGMTString()));
	
}

function arguments_cookie(offset){
	
	var endstr=document.cookie.indexOf (";", offset);
	if (endstr==-1) endstr=document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr)); 
	
}

function lire_cookie(nom){
	
	var arg=nom+"=";
	var alen=arg.length;
	var clen=document.cookie.length;
	var i=0;
	while (i<clen){
		var j=i+alen;
		if (document.cookie.substring(i, j)==arg)
			return arguments_cookies(j);
		i=document.cookie.indexOf(" ",i)+1;
		if (i==0) break;
	}
	
	return null;
	
}


/*
*
* Word Purifier
*
*/


var Word_Entity_Scrubber = function()
{
	/**
	 * The DOM element to get the input from
	 *
	 * @access protected
	 */
	this.elem_input = null;
	
	/**
	 * The DOM element to get the output from
	 *
	 * @access protected
	 */
	this.elem_output = null;
	
	/**
	 * The array of MS Word characters to find for replacement
	 *
	 * @access protected
	 */
	this.arr_find = [
		String.fromCharCode(8220), //"
		String.fromCharCode(8221), //"
		String.fromCharCode(8216), //'
		String.fromCharCode(8217), //'
		String.fromCharCode(8211), //–
		String.fromCharCode(8212), //—
		String.fromCharCode(189), //½
		String.fromCharCode(188), //¼
		String.fromCharCode(190), //¾
		String.fromCharCode(169), //©
		String.fromCharCode(174), //®
		String.fromCharCode(8230) //…
	];
	
	/**
	 * The array of standard string to replace MS Word characters in this.arr_find with
	 *
	 * @access protected
	 */
	this.arr_replace = [
		'"',
		'"',
		"'",
		"'",
		"-",
		"--",
		"1/2",
		"1/4",
		"3/4",
		"(C)",
		"(R)",
		"..."
	];
	this.bindTo = function(elem_input, elem_output, elem_activate, elem_activate_action)
	{
		//Store the input element
		this.elem_input = $div(elem_input);
		
		//Store the output element
		this.elem_output = $div(elem_output);
		
		//Just to be sure we've got the DOM element
		elem_activate = $div(elem_activate);
		
		//Assign the appropriate action to the activate element
		elem_activate[elem_activate_action] = this.bindScrub.bind(this);
		
	}
	
	this.bindScrub = function()
	{
		this.elem_output.value = this.scrub(this.elem_input.value);
		
	}

	this.scrub = function(input_string)
	{		
		//Make sure find and replace have equal lengths
		if ( !(this.arr_find.length == this.arr_replace.length) ) {
			throw new Error("The MS Word entities find values do not match the replacement values");
			
		}
		
		//Still here, then replacement rules are ok - loop across and do the replacement
		for ( var i=0; i<this.arr_find.length; i++ ) {
			var regex = new RegExp(this.arr_find[i], "gi");
			input_string = input_string.replace(regex, this.arr_replace[i]);
			
		}
		
		return input_string
		
	}
	
}

/**
 * Just a little bit of a hack to get Word_Entity_Scrubber.scrub() to work as a 
 * "static" method as well as a method on an instance object...
 */
Word_Entity_Scrubber.scrub = function(input_string)
{
	var obj = new Word_Entity_Scrubber();
	return obj.scrub(input_string);
	
}//end Word_Entity_Scrubber.scrub
