/*****************************************************************/
/*  This file contains functions related to the search form.     */
/*  Note that the actual search is not performed here. Although  */
/*  the function "search_form()", which is located here, does    */
/*  trigger the search routine, this routine is actually         */
/*  performed by the functions located in the "jse_search.js"    */
/*  file which is only loaded from the search result page to     */
/*  save bandwidth. This little trick allows us to include a     */
/*  search box on every page without the overhead of loading the */
/*  the actual search functions nor the associated site database */
/*  (i.e. the array of keywords for every page). Remember, this  */
/*  is a purely client-side solution due to hosting plan CGI     */
/*  scripting limitations...                                     */
/*****************************************************************/

// set the default search result page as the french one
var results_location = "/result.html";

// if the search was made from an english page, as determined
// by the "isEnglish()" function located in the "translate.js" 
// file, set the search result page as the english one
if(isEnglish()){ results_location = "/result_en.html"; }

/*****************************************************************/
/*  Function that clears the search box on it being clicked. We  */
/*  want the search box to automatically clear itself upon being */
/*  clicked in almost every case as a convenience to the user.   */
/*  The only exception is when the user is on the result page    */
/*  and has not had any success with his search. In this case,   */
/*  we leave the content of the search box "as is" so that the   */
/*  user may correct his entry. After all, the lack of results   */
/*  may be the consequence of a simple typo that could be        */
/*  corrected just by altering one letter (no need to have to    */
/*  type the whole search again).                                */
/*****************************************************************/
function clear_field(theField) {
	 // If we are on the search result page, clear the search field only if there was a positive match
	 // as a result of the searh that was just done	(i.e. make room for a clean new search)
	 if ((location.href.indexOf("result.html") > -1) || (location.href.indexOf("result_en.html") > -1)) {
	 	if (co > 0) {
		theField.value = "";
		}
		// clear the field also if nothing was actually searched...
		if ((theField.value == "Chercher...")||(theField.value == "Search...")) {theField.value = "";}
	 }
	 // on any other page clear the search field
	 else {theField.value = "";}
 }

/*****************************************************************/
/*  This function is the one that triggers the search. Actually, */
/*  it doesn't really trigger the search. Instead, what it does  */
/*  is it creates a cookie with the search terms and loads the   */
/*  result page on submition of the search form. It is from the  */
/*  result page that the search routine will be called based on  */
/*  the value of the cookie created with this function.          */
/*****************************************************************/
function search_form(jse_Form) {
	if ( (jse_Form.d.value.length > 0) && (jse_Form.d.value != "Chercher...") && (jse_Form.d.value != "Search...") ) {
		document.cookie = "mampurdc=" + escape(jse_Form.d.value) + "; path=/";
		window.location = results_location;
	}
}

/*****************************************************************/
/*  This function deals with accents (basically removing them    */
/*  before going through a comparison routine). This is          */
/*  necessary in order to account for users with both AZERTY     */
/*  keyboards that have accents and QWERTY keyboards that don't. */
/*  Although not explicitly used by the search form itself, this */
/*  function is used by the search routine (located in the       */
/*  "jse_search.js" file) and the highlighting routine (located  */
/*  in the "highlight.js" file). Placing this function in this   */
/*  file makes it available to both routines without having to   */
/*  download it twice.                                           */
/*****************************************************************/
function stripVowelAccent(str) {
	var s=str;
	
	var rExps=[ /[\xC0-\xC5]/g, /[\xE0-\xE5]/g,
	/[\xC8-\xCB]/g, /[\xE8-\xEB]/g,
	/[\xCC-\xCF]/g, /[\xEC-\xEF]/g,
	/[\xD2-\xD8]/g, /[\xF2-\xF8]/g,
	/[\xD9-\xDC]/g, /[\xF9-\xFC]/g,
	/[\xBC]/g, /[\xBD]/g,
	/[\xBE]/g, /[\xFF]/g,
	/[\xDD]/g, /[\xFD]/g,
	/[\xC6]/g, /[\xE6]/g,
	/[\xC7]/g, /[\xE7]/g,
	/[\xD1]/g, /[\xF1]/g,
	/[\xDF]/g ];
	
	var repChar=['A','a','E','e','I','i','O','o','U','u','OE','oe','Y','y','Y','y','AE','ae','C','c','N','n','ss'];
	
	for(var i=0; i<rExps.length; i++)
	s=s.replace(rExps[i],repChar[i]);
	
	return s;
}

/*****************************************************************/
/*  This function returns the value of a cookie that was created */
/*  in the wake of a search. If there is no cookie (i.e. no      */
/*  search has been made), then it returns a blank.              */
/*  Although not explicitly used by the search form itself, this */
/*  function is used by the search routine (located in the       */
/*  "jse_search.js" file) and the highlighting routine (located  */
/*  in the "highlight.js" file). Placing this function in this   */
/*  file makes it available to both routines without having to   */
/*  download it twice.                                           */
/*****************************************************************/
function read_cookie(cookiename) {
	var cookie_value = "";
	var name = cookiename + "=";
	var cookies = document.cookie;
	var p = cookies.indexOf(name);
		if (p != -1) {
			var st = p + name.length;
			var en = cookies.indexOf(";", st);
			if (en == -1) {	en = cookies.length; }
		cookie_value = cookies.substring(st, en);
		cookie_value = unescape(cookie_value);
		}
	return cookie_value;	
}

