var srcComp = 0;
function showContent(){
	document.getElementById("hidden_if_js_disabled").style.display = "block";
}
function ClearDoc()	{
	var doc = document.forms[0];
	doc.Open
}
			
function buildCBQuery(cbSelected, cbFldName) {
// Returns the selected checkbox values
	var i = 0;
	var nbrSel = 0;
	var cbString = "";
			
	if (cbSelected[i].checked) {
		// The All check box has been selected
		cbString = "";
		
	}
	else {
		for (i = 1; i < cbSelected.length; i++) {
			if (cbSelected[i].checked) {
				cbString = cbString + '(IN "' + cbFldName + '""' + cbSelected[i].value + '")';
				nbrSel = nbrSel + 1;
			}
		}
		if (nbrSel > 1) {
			cbString = "(OR " + cbString + ")";
		}
	};
	
	// If none of the "regions" have been selected, do not increment the global counter
	if (nbrSel > 0) {
		// update the global counter
		srcComp++;
	}
	
	return cbString;
}
function removeQuote(stringToClean) {
	var remQTmp = "";
	var remQPart = "";
	var remQString = "";
	var j = 0;
		
	// delimiter
	remQTmp = stringToClean + '"~~';
	remQPart = remQTmp.split('"');
	j = 0;
	while (remQPart[j] != "~~") {
		remQString = remQString + remQPart[j];
		j++;
	}
	return remQString;
}
function findSelectedRB(fldName) {
	var searchVal = "";
	for (i = 0; i < fldName.length; i++) {
		if (fldName[i].selected) {
			searchVal = fldName[i].value;
			return searchVal;
			break;
		}
	}
}
function buildStrQuery(stringValue) {
// Builds the string query
	var doc = document.forms[0];
	var strTmp = "";
	var strPart = "";
	var strOperator = "";
	var i = 0;
	var quoteRemoved = "";
	var strString = "";
			
	quoteRemoved = removeQuote(stringValue);
	// delimiter
	strTmp = quoteRemoved + " ~~";
			
	// Find which type of search is selected
	strOperator = findSelectedRB(doc.searchType);
	
	if (strOperator != "PHRASE") {
		strPart = strTmp.split(" ");
		i = 0;
		while (strPart[i] != "~~") {
			strString = strString + '"' + strPart[i] + '"';
			i++;
		};
		if (strString.indexOf('""') > 0) {
			strString = "(" + strOperator + " " + strString + ")";
		};
	}
	else {
		// If the search type is a phrase, than return the string without the string if any
		strString = '"' + quoteRemoved + '"';
	};
	if (strString == '""') {
		strString = "";
	}
	else {
		// additional search component
		srcComp++;
	};
	return strString;
}
		
function PubDate(fldName) {
// Adds the date restriction to the search variables
var iRange = 0;
	for(i=0; i < fldName.length; i++){
		if(fldName[i].checked){
			iRange = fldName[i].value;
			break;
		}
	}
			
	if(iRange > 0){
		var now = new Date();
		now.setTime(now.getTime() - (iRange * 24 * 60 * 60 * 1000));
		
		//Make sure the month is 2 digits
		var month = now.getMonth() + 1;
		
		if(month.toString().length == 1){month = "0" + month;}
		
		//Make sure the day is 2 digits
		var day = now.getDate();
		if(day.toString().length == 1){day = "0" + day;}
		
		//Set the constraint parameters for the "last modified" date
		doc.DESConstraintName1.value = "Modified";
		doc.DESConstraintOperator1.value = "GTE";
		doc.DESConstraintValue1.value = now.getYear().toString() + month.toString() + day.toString();
	}
}	
	
function checkQuery(lang) {
	srcComp = 0;
	var doc = document.forms[0];
	var srcPrefix = "";
	var srcSuffix = "";
	var tmpSrcString = "";
			
	doc.FormStatus.value = "Ok";
	tmpScrString = buildStrQuery(doc.freeTxtFld.value) + buildCBQuery(doc.instButton, "SiteEn");
			
	// If there are more than one search compononent, add the proper string prefix and suffix
	if (srcComp > 0) {
		if (srcComp > 1) {
			srcPrefix = "(AND ";
			srcSuffix = ")";
		};
		var avoidMenuSpacer = '(OR (IN "Type""1")(IN "Type""5"))';
		//DEBUG alert("srcPrefix ==> " + srcPrefix + " / tmpScrString ==> " + tmpScrString + " / srcSuffix ==> " + srcSuffix + " / avoidMenuSpacer ==> " +avoidMenuSpacer);
		doc.DESQueryString.value = "(AND " + srcPrefix + tmpScrString + srcSuffix + avoidMenuSpacer + ")";
	
		// Set the date restriction
		PubDate(doc.iDocDateRange)
	
		return true;
	}
	else {
		doc.FormStatus.value = "QueryBlank";
		doc.desTemplateFile.value = "CIDAWebAdv" + lang + ".htm";
		return false;
	}
}
var accent = "àÀâÂçÇéÉèÈêÊëËîÎïÏôÔùÙûÛüÜ";
var escape = new Array("&agrave","&Agrave","&acirc","&Acirc","&ccedil","&Ccedil","&eacute","&Eacute","&egrave","&Egrave","&ecirc","&Ecirc","&euml","&Euml","&icirc","&Icirc","&iuml","&Iuml","&ocirc","&Ocirc","&ugrave","&Ugrave","&ucirc","&Ucirc","&uuml","&Uuml");
var iItemNum = 0;
				
function escapeFrenchChars(itext){
	var xtmp = "";
				
	for(x=0;x < itext.length;x++){
		for(b=0;b<accent.length;b++){
			if(itext.charAt(x) == accent.charAt(b)){
				itext = itext.substring(0,x) + escape[b] + ";" + itext.substring(x+1);
				xtmp = escape[b];
				x = x + xtmp.length + 1;
			}
		}
	}
	return itext;
}
function gotoPage(num) {
	document.forms[0].DESGetPage.value = num;
	document.forms[0].submit();
}
			
function searchInfoDisplay(iPageSize, iTotalHits, iLang) {
	var iPageNum = document.forms[0].DESGetPage.value;
	var iPagesReturned = Math.ceil(iTotalHits / iPageSize);
	document.writeln("<div>");
					
	if(iPagesReturned > 0){
   		document.writeln("Page: ");
	}
	
	if(iPagesReturned > 1 && iPageNum > 1){
   		var iPreviousPage = Number(iPageNum) - 1;
   		if(iLang == "eng"){
   			document.writeln("<a href='javascript:gotoPage(" + iPreviousPage + ");'>Previous</a>&nbsp;&nbsp");
		} else {
			document.writeln("<a href='javascript:gotoPage(" + iPreviousPage + ");'>Pr&eacute;c&eacute;dente</a>&nbsp;&nbsp");
		}
	}
	for(i=1;i<=iPagesReturned;i++){
 		if(i == iPageNum){
      			document.writeln(i);
   		} else {
      			document.writeln("<a href='javascript:gotoPage(" + i + ");' onclick='javascript:gotoPage(" + i + ");'>" + i + "</a>");
   		}
   		if(i<iPagesReturned){document.writeln(",");}
	}
	if(iPagesReturned > 1 && iPageNum < iPagesReturned){
  		var iNextPage = Number(iPageNum) + 1;
   		if(iLang == "eng"){
	   		document.writeln("&nbsp;&nbsp<a href='javascript:gotoPage(" + iNextPage + ");'>Next</a><br /><br /></div>");
		} else {
			document.writeln("&nbsp;&nbsp<a href='javascript:gotoPage(" + iNextPage + ");'>Suivante</a><br /><br /></div>");
		}
	} else {
		document.writeln("<br /><br /></div>");
	}
}
function setItemNum(iPageSize) {
	var iPageNum = document.forms[0].DESGetPage.value;
	if (iPageNum == "") {
		iPageNum = 1;
	}
				
	if (iPageSize == "") {
		iPageSize = 100;
	}
	iItemNum = ((iPageNum - 1) * iPageSize) + 1;
}
function incItemNum(){
	iItemNum++;
}
function genNameValuePair(NameValuePair) {
	var i = NameValuePair.indexOf("=");
	var NameOnly = NameValuePair.substring(0,i);
	var ValueOnly = NameValuePair.substring(i+1,NameValuePair.length);
	if (NameOnly.indexOf("DESCategory") >= 0 || NameOnly.indexOf("DESConstraint") >= 0) {
		document.writeln('<input type="hidden" name="' + NameOnly + '" value="' + ValueOnly + '" />');
	}
}
function generateResults(iItemUrl, docTitle, iDocAbstract, docRank, vCreatedDate, vModifiedDate, iLang) {
	var vCreatedDate = "";
	var vModifiedDate = "";
	document.writeln("<strong>" + iItemNum + ". </strong>");
	document.writeln("<a href='" + iItemUrl + "' target='new'>");
	if (docTitle == "" && iLang == 'eng') {
		docTitle = "Document title missing! Please check the LES data source.";
	} else if(docTitle == "" && iLang == 'fra') {
		docTitle = "Le titre du document est manquant!  Veuillez s.v.p. vérifier la source de données « LES ».";
	}
	document.writeln("      " + docTitle + "</a><br />");
	if(iDocAbstract == "" && iLang == 'eng'){
		iDocAbstract = "-no description-";
	} else if(iDocAbstract == "" && iLang == 'fra') {
		iDocAbstract = "-aucune description-";
	}
	//Limit the size of the abstract to 55 chars
	if(iDocAbstract.length > 55){
		iDocAbstract = iDocAbstract.substring(0,55) + ' [...]';
	}
	document.writeln(iDocAbstract+"<br />");
	if(iLang == 'eng'){
		document.writeln("<strong><i>Relevance: </i></strong>" + docRank + "% ");
	} else {
		document.writeln("<strong><i>Pertinence : </i></strong>" + docRank + "% ");
	}
	if(vCreatedDate.length > 0 && vCreatedDate.indexOf(' ') > 0){
		vCreatedDate = vCreatedDate.substring(0,vCreatedDate.indexOf(' '));
	}
	else if(vCreatedDate == ''){
		vCreatedDate = "n/a";
	}
	if(iLang == 'eng'){
		document.writeln("<strong><i>Created: </i></strong>" + vCreatedDate + " ");
	} else {
		document.writeln("<strong><i>Créé le : </i></strong>" + vCreatedDate + " ");
	}
	if(vModifiedDate.length > 0 && vModifiedDate.indexOf(' ') > 0){
		vModifiedDate = vModifiedDate.substring(0,vModifiedDate.indexOf(' '));
	}
	else if(vModifiedDate == ''){
		vModifiedDate = "n/a";
	}
	
	if(iLang == 'eng'){				
		document.writeln("<strong><i>Last modified: </i></strong>" + vModifiedDate + " ");
	} else {
		document.writeln("<strong><i>Révisé le : </i></strong>" + vModifiedDate + " ");
	}
	document.write("<br /><br />");
	incItemNum();
}
