/*-----------------------------------------------------------------------------------------------------*/
/* Help Search */
/*-----------------------------------------------------------------------------------------------------*/

function displayHint(hint) {
	ASpopup = window.open("/statics/png/pu_tu/pnghelp/hint_"+hint+".html","hint","width=400,height=200,resizable=yes,scrollbars=yes");
	ASpopup.focus();
	return;
}

function displayHintSpecific(hint) {
        ASpopup = window.open("/statics/png/pu_tu/pnghelp/hint_"+hint+".html","hint","width=400,height=200,resizable=yes,scrollbars=yes");
        ASpopup.focus();
        return;
}

function useAdvancedSearch(labelId, startText, fieldName, label) 
{
    var url;
    url = "/sf7/modules/common/search_advancedsearch.jsp?labelId=" + escape(labelId) + "&result=" +  removePlus(escape(startText)) + "&fieldName=" + escape(fieldName)+"&label="+label;
    ASpopup = window.open(url,"AdvancedSearch","width=300,height=340,resizable=yes,scrollbars=no,status=yes");
    ASpopup.focus();
    return;
}

function removePlus(s) {
	var i;
	var tmp="";
	for (i=0;i<s.length;i++) {
		if (s.charAt(i)!='+') {
			tmp=tmp+s.charAt(i);
		}
		else {
			tmp=tmp+'%2B';
		}
	}
	return tmp;
}

function addToResult(form) {
	var operator="EN";
	var type=form.type.value;

	//and
	var tmp = Trim(form.and.value);
	var atmp="";
	var res = form.result.value;
	var index = tmp.indexOf(" ");
	while (index!=-1) {
		if (Trim(atmp)!="")
			atmp = atmp + " EN " + tmp.substring(0,index) ;
		else
			atmp = atmp + tmp.substring(0,index) ;
		tmp = LTrim(tmp.substring(index,tmp.length));
		index = tmp.indexOf(" ");
	}

	if (Trim(tmp)!="") {//Il reste encore un mot
		if (Trim(atmp) != "") { //Il y avait d'autres mots
			atmp = atmp + " EN " + Trim(tmp);
		}
		else {
			atmp = Trim(tmp);
		}
	}

	if (atmp!="")
		if (Trim(res)!="")
			res = res +" "+ operator + " "+atmp;
		else
			if (Trim(form.phrase.value)=="" && Trim(form.or.value)=="" && Trim(form.not.value)=="") {
				res = atmp;
			}
			else {
				res = res + "(" + atmp + ") ";
			}
	
	//phrase
	tmp = Trim(form.phrase.value);
	var ptmp="";
	var indexBegin=0;
	var indexEnd=0;

	if (type=="phrase") { //Il ne peut normalement pas y avoir de phrase quand on est en type mot donc on ne devrait pas passer dans le if mais dans le else
		index = tmp.indexOf("\"");
		if (tmp!="" && index==-1) { //no '"' in the text
			alert("phrase field is incorrect");
			alert("tmp : "+tmp);
			return false;
		}

		while (index!=-1) {
			indexBegin=index;
			indexEnd=tmp.substring(1,tmp.length).indexOf("\"");
			if (indexEnd==-1) {
				alert("phrase field is incorrect");
				return false;
			}
			ptmp=ptmp + tmp.substring(indexBegin,indexEnd+2) + " OF ";
			tmp=LTrim(tmp.substring(indexEnd+2,tmp.length));
			index = tmp.indexOf("\"");
		}
		if (ptmp.substring(ptmp.length-4,ptmp.length)==" OF ")
			ptmp=ptmp.substring(0,ptmp.length-4);
		if (ptmp!="")
			if (Trim(res)!="")
				res = res + " "+operator+" (" + ptmp + ") ";
			else
				if (Trim(form.and.value)=="" && Trim(form.or.value)=="" && Trim(form.not.value)=="") {
					res = ptmp;
				}
				else {
					res = res + "(" + ptmp + ") ";
				}
	}
	else {
		if (tmp!="") {
			if (tmp.indexOf("\"")!=-1) {
				alert("Aanhalingstekens zijn niet toegestaan in het woordcombinatieveld.");
				return false;
			}
			if (Trim(res)!="") {
				res = res + " EN \"" + tmp +"\"" ;
			}
			else {
				res = "\""+tmp + "\"";
			}
		}
	}

	//or
	tmp = Trim(form.or.value);
	index = tmp.indexOf(" ");
	var otmp="";

	if (type=="phrase") {
		var indexBegin=0;
		var indexEnd=0;
		index = tmp.indexOf("\"");
		if (tmp!="" && index==-1) { //no '"' in the text
			alert("or field is incorrect");
			return;
		}

		while (index!=-1) {
			indexBegin=index;
			indexEnd=tmp.substring(1,tmp.length).indexOf("\"");
			if (indexEnd==-1) {
				alert("or field is incorrect");
				return;
			}
			ptmp=ptmp + tmp.substring(indexBegin,indexEnd+2) + " OF ";
			tmp=LTrim(tmp.substring(indexEnd+2,tmp.length));
			index = tmp.indexOf("\"");
		}
		if (ptmp.substring(ptmp.length-4,ptmp.length)==" OF ")
			ptmp=ptmp.substring(0,ptmp.length-4);
		if (ptmp!="")
			if (Trim(res)!="")
				res = res + " "+operator+" (" + ptmp + ") ";
			else
				if (Trim(form.and.value)=="" && Trim(form.phrase.value)=="" && Trim(form.not.value)=="") {
					res = ptmp;
				}
				else {
					res = res + "(" + ptmp + ") ";
				}
	}
	else {
		while (index!=-1) {
			if (Trim(otmp)!="")
				otmp = otmp + " OF " + tmp.substring(0,index);
			else
				otmp = otmp + tmp.substring(0,index);
			tmp = LTrim(tmp.substring(index,tmp.length));
			index = tmp.indexOf(" ");
		}
		if (tmp!="")
			if (Trim(otmp)!="")
				otmp = otmp + " OF " + tmp;
			else
				otmp = tmp; 
		if (otmp!="")
			if (Trim(res)!="")
				res = res + " "+operator+" (" + otmp + ") ";
			else
				//if there's only or expressions, there's no need to put ()
				/*if (Trim(form.and.value)=="" && Trim(form.phrase.value)=="" && Trim(form.not.value)=="") {
					res = otmp;
				}
				else {*/
					res = "( " + otmp + " ) ";
				//}
	}

	//not
	tmp = Trim(form.not.value);
	index = tmp.indexOf(" ");
	while (index!=-1) {
		if (Trim(res)!="")
			res = res + " EN NOT " + tmp.substring(0,index) ;
		else
			res = res + " NOT " + tmp.substring(0,index) ;
		tmp = LTrim(tmp.substring(index,tmp.length));
		index = tmp.indexOf(" ");
	}
	if (tmp!="")
		if (Trim(res)!="")
			res = res + " EN NOT " + tmp ;
		else
			res = "NOT " + tmp ;

	//result
	form.result.value=Trim(res);
	//Effacement des champs source
	form.and.value="";
	form.phrase.value="";
	form.or.value="";
	form.not.value="";

	return true;
}


function returnPicklistValue(field, word)
{
	 if (word != null && word != "") {
	 	field.value = word;
     }
     top.close();
     return;
}