var gGoThrough = 0;
var foundStopword = 0;

function validate_search_generic(argSearch) {
	var pos;
	var pair = true;
	var SpaceError = false;
	var ParError = false;
	var forbchar = "!|\/#<>[]{}:;=+?&*\\"
	var searchStr = argSearch.value;
	var ParCpt = 0;

	// look for forbidden characters
	for(pos=0;pos<searchStr.length+1;pos++){
		if(forbchar.indexOf(searchStr.charAt(pos)) != -1 && searchStr.charAt(pos) != ""){
			error(argSearch, "Le caractère (" + searchStr.charAt(pos) + ") n'est pas repérable. Veuillez le retirer.", 1);
			return "_CANCEL_SEARCH_";
		}
	}
	
	// find start and end points for quotes
	for(pos=0;pos<searchStr.length+1;pos++) {
		if( searchStr.charAt(pos) == '"' ) {
			if( pair == true ) pair = false;
			else pair = true;
			if( searchStr.charAt(pos+1) == ' ' && !pair ) SpaceError = true;
			if( searchStr.charAt(pos-1) == ' ' && pair ) SpaceError = true;
			if( searchStr.charAt(pos+1) == '"') SpaceError = true;			
		}
	}
	
	if( !pair || SpaceError ) {
		error(argSearch, "Vérifier l'utilisation des guillemets.", 1);
		return "_CANCEL_SEARCH_";
	}

	// find start and end points for parenthesis
	for(pos=0;pos<searchStr.length+1;pos++) {
		if( searchStr.charAt(pos) == '(' ) {
			ParCpt++;
			if( searchStr.charAt(pos+1) == ')') ParError = true;
		}
		if( searchStr.charAt(pos) == ')' ) ParCpt--;
	}

	if( ParCpt != 0  || ParError ) {
		error(argSearch, "Vérifier l'utilisation des parenthèse.", 1);
		return "_CANCEL_SEARCH_";
	}	  	 
		
	return "_CONTINUE_SEARCH_";
}


function fix_search_generic(searchStr) {
  var retStr;
  var pos;
  
  // replace all ' in string by a space
	while( (pos=searchStr.indexOf('\'')) != -1 ) { 
		retStr =  searchStr.substring(0,pos)+ " " + searchStr.substring(pos+1,searchStr.length);
		searchStr = retStr;  
	}  
      
  // remove all carriage return and line feeds (\n \r) and replace with a space
	while( (pos=searchStr.indexOf('\n')) != -1 ) { 
		retStr =  searchStr.substring(0,pos) + " " + searchStr.substring(pos+1,searchStr.length);
		searchStr = retStr;  
	}	
	while( (pos=searchStr.indexOf('\r')) != -1 ) { 
		retStr =  searchStr.substring(0,pos) + searchStr.substring(pos+1,searchStr.length);
		searchStr = retStr;  
	}	   
		
   // replace french quotes with rien
	pos=searchStr.indexOf('«');
	while(  (pos=searchStr.indexOf('«')) != -1 ) { 
		retStr =  searchStr.substring(0,pos) + searchStr.substring(pos+1,searchStr.length);
		searchStr = retStr;  
	}	   
	while( (   pos=searchStr.indexOf('»')) != -1 ) { 
		retStr =  searchStr.substring(0,pos) + searchStr.substring(pos+1,searchStr.length);
		searchStr = retStr;  
	}	   

  // strip leading blanks	
  pos=0;		
  while( searchStr.charAt(pos) == ' ' ) pos++;
  retStr = searchStr.substring(pos,searchStr.length);
  searchStr = retStr;
  
  // strip trailing blanks
  while( searchStr.charAt(searchStr.length-1) == ' ' ) {
		retStr = searchStr.substring(0,searchStr.length-1);
		searchStr = retStr;
  }	

  // because of NS4.x, replace spaces by +
	while( (pos=searchStr.indexOf(' ')) != -1 ) { 
		retStr =  searchStr.substring(0,pos) + "+" + searchStr.substring(pos+1,searchStr.length);
		searchStr = retStr;  
	}	
   
	return searchStr;
}


function fix_search_motscle(searchStr) {
	var retStr = "";
	var pos;
	var x;
	var QuoteArray = new Array();
	var QuoteIndex = 0;
	var inQuotes;

	// replace quotes with rien
	pos=searchStr.indexOf('"');
	while(  (pos=searchStr.indexOf('"')) != -1 ) { 
		retStr =  searchStr.substring(0,pos) + searchStr.substring(pos+1,searchStr.length);
		searchStr = retStr;  
	}	   

	//alert("searchStr=" + searchStr);

	// find start and end points for quotes
	for(pos=0;pos<searchStr.length+1;pos++)  if( searchStr.charAt(pos) == '"' ) QuoteArray[QuoteIndex++] = pos;

	//alert("QuoteIndex=" + QuoteIndex);
	
	// find all words ending with a . and truncate if not in quotes
	retStr = "";
	for(pos=0;pos<searchStr.length+1;pos++)  {
		if( searchStr.charAt(pos) == '.' ) {
			if( pos == searchStr.length-1 || searchStr.charAt(pos+1) == ' ' || searchStr.charAt(pos+1) == ')' || searchStr.charAt(pos+1) == '"' ) continue;
				retStr += searchStr.charAt(pos);
		}
		else retStr += searchStr.charAt(pos);
	}	
	searchStr = retStr;
   
	// at this point all . in a word are not at the end, so place word in quotes, unless it already is.
	var tempStr;
	var tempStr;
	for(pos=0;pos<searchStr.length+1;pos++)  {
	
		if( searchStr.charAt(pos) == '.' ) {
	
			inQuotes=false;
			for(x=0;x<QuoteIndex-1;x=x+2) if( (pos > QuoteArray[x]) && (pos < QuoteArray[x+1]) ) inQuotes = true;
	
			if( inQuotes == false ) {
				// get the index of the last space, '(' , or beginging of line.
				start=pos;
				while( searchStr.charAt(start) != '+' && searchStr.charAt(start) != '(' && (start > 0) ) start--;
	
				// get the index of the next space, ')', or end of line.
				end=pos;
				while( searchStr.charAt(end) != '+' && searchStr.charAt(end) != ')'  && (end < searchStr.length+1) ) end++;
	
				// If start == 0, then the offset of 1 that I add in the normal case does not apply
				// since there is no space to compensate for, so I substract 1 to make it work. 
				if( start == 0 && searchStr.charAt(start) != '(' ) start = -1;			
				tempStr = searchStr.substring(0,start+1) + '"' + searchStr.substring(start+1,end) + '"' + searchStr.substring(end,searchStr.length);
	
				searchStr = tempStr;
	
				// reset start and end points for quotes
				QuoteIndex=0;
				for(x=0;x<searchStr.length+1;x++)  if( searchStr.charAt(x) == '"' ) QuoteArray[QuoteIndex++] = x;   
	
				pos=0;
			} 
		}
	}	   

	return searchStr;
}

function fix_search_stopword(searchStr) {
	var temp;
  var x=0;
  var stopword_mode1=new Array();
      stopword_mode1[x++]="le";
      stopword_mode1[x++]="la";
      stopword_mode1[x++]="les";
      stopword_mode1[x++]="du";
      stopword_mode1[x++]="de";
      stopword_mode1[x++]="des";
      stopword_mode1[x++]="un";
      stopword_mode1[x++]="une";
      stopword_mode1[x++]="ce";
      stopword_mode1[x++]="ces";
      stopword_mode1[x++]="cette";
      stopword_mode1[x++]="ne";
      stopword_mode1[x++]="se";
      stopword_mode1[x++]="sa";
      stopword_mode1[x++]="ses";
      stopword_mode1[x++]="il";
      stopword_mode1[x++]="ils";
      stopword_mode1[x++]="elle";
      stopword_mode1[x++]="elles";
      stopword_mode1[x++]="que";
  
  var y=0;
  var stopword_mode2=new Array();
      stopword_mode2[y++]="l'";
      stopword_mode2[y++]="d'";
      stopword_mode2[y++]="c'";
      stopword_mode2[y++]="n'";
      stopword_mode2[y++]="s'";
      stopword_mode2[y++]="qu'";

  for(var i=0; i < x; i++) FindStopword(searchStr, stopword_mode1[i], 0);
	for(var i=0; i < y; i++) FindStopword(searchStr, stopword_mode2[i], 1);
	
  // If we want to fix the search string, use SearchReplace() function instead of FindStopword() and return result 
  // return searchStr;
}

function FindStopword(strSrc, strSearch, mode) 
{
  var retStr = "" + strSrc.toLowerCase();
  var regEx;
  
  if( mode == 0 ) {
    regEx = new RegExp("\\s" + strSearch + "\\s");
    if( retStr.match(regEx) ) foundStopword=1;
    regEx = new RegExp("^" + strSearch + "\\s");
    if( retStr.match(regEx) ) foundStopword=1;
    regEx = new RegExp("\\s" + strSearch + "$");
    if( retStr.match(regEx) ) foundStopword=1;
    regEx = new RegExp("^" + strSearch + "$");
    if( retStr.match(regEx) ) foundStopword=1;
  }
  if( mode == 1 ) {
    regEx = new RegExp("\\s" + strSearch);
    if( retStr.match(regEx) ) foundStopword=1;
    regEx = new RegExp("^" + strSearch);
    if( retStr.match(regEx) ) foundStopword=1;
  }
}


// Not used since we only detect stopwords, but if we want to fix them instead use this function.
function SearchReplace(strSrc, strSearch, mode) 
{
  var retStr = "" + strSrc.toLowerCase();
  var regEx;
  
  if( mode == 0 ) {
    regEx = new RegExp("\\s" + strSearch + "\\s");
    while( retStr.match(regEx) ) retStr=retStr.replace(regEx," ");
    regEx = new RegExp("^" + strSearch + "\\s");
    while( retStr.match(regEx) ) retStr=retStr.replace(regEx," ");
    regEx = new RegExp("\\s" + strSearch + "$");
    while( retStr.match(regEx) ) retStr=retStr.replace(regEx," ");
    regEx = new RegExp("^" + strSearch + "$");
    while( retStr.match(regEx) ) retStr=retStr.replace(regEx," ");
  }
  if( mode == 1 ) {
    regEx = new RegExp("\\s" + strSearch);
    while( retStr.match(regEx) ) retStr=retStr.replace(regEx," ");
    regEx = new RegExp("^" + strSearch);
    while( retStr.match(regEx) ) retStr=retStr.replace(regEx," ");
  }
  
  return retStr;
}

function paddate(number) { return (number < 10) ? '0' + number : number; }

function fix_search_date(strdate) {
   if (strdate.length == 0) return "";
	
	str=strdate.replace(/\//ig,"");strdate=str;
	str=strdate.replace(/-/ig,"");strdate=str;

	if (isDate(strdate)) return strdate;
	else {
		error(strdate, "Date invalide", 1);
		return "_CANCEL_SEARCH_";
	}
}

function isDate (strdate) {

   if (strdate.length != 8) return false;

   var stryear = strdate.substring (0,4);
   var strmonth = strdate.substring (4,6);
   var strday = strdate.substring (6,8);

   var dateref1 = strday + '/' + strmonth + '/' + stryear;
   var date2 = new Date (stryear-0, strmonth-1, strday-0);
   var dateref2 = paddate(date2.getDate()) + '/' + paddate(date2.getMonth()+1) + '/' + (date2.getFullYear())

   if (dateref1 != dateref2) return false;

   if (stryear < 1800) return false;

   return true;
}


