//tables 
//var juridiccionIds=new Array();
//var juridiccionLiterales=new Array();
//var juridiccionOperators=new Array();

var rangeIds=new Array();
var rangeLiterales=new Array();
var rangeOperators=new Array();

var speakerIds=new Array();
var speakerLiterales=new Array();
var speakerOperators=new Array();

var officialBulletinIds=new Array();
var officialBulletinLiterales=new Array();
var officialBulletinOperators=new Array();

var tribunalIds=new Array();
var tribunalLiterales=new Array();
var tribunalOperators=new Array();
var tribunalSearchIds=new Array();

var thesaurusIds=new Array();
var thesaurusLiterales=new Array();
var thesaurusOperators=new Array();
var thesaurusSearchIds=new Array();

var autorIds=new Array();
var autorLiterales=new Array();
var autorOperators=new Array();

var pubEditorialIds=new Array();
var pubEditorialLiterales=new Array();
var pubEditorialOperators=new Array();

var editorialIds=new Array();
var editorialLiterales=new Array();
var editorialOperators=new Array();

var disposicionesComentIds=new Array();
var disposicionesComentLiterales=new Array();
var disposicionesComentOperators=new Array();

function openAssistant(type,appliRoot) {
		var windowWidth = 640;
	
			if (type == "thesaurus") windowWidth=800;
		var gauche = Math.floor( (screen.width - windowWidth) / 2);
    	var haut =  Math.floor( (screen.height - 505) / 2);
    	var winParms = "scroll=no,top=" + haut + ",left=" + gauche + ",height=505,width="+windowWidth+",z-lock=no";

//		var astWin=window.open(appliRoot+'/modules/library/assistant.jsp?type='+type+'&product='+checks,'replace',winParms);    
		var product;
		if(TestCivi[0].checked){
	   product="civi";
	  }
	  if(TestPena[0].checked){
	  	product=product+"|Pena";
	  }
	  if(TestLabo[0].checked){
	  	product=product+"|Labo";
	  }
	  if(TestAdmo[0].checked){
	  	product=product+"|Admo";
	  }

	
		var astWin=window.open('/modules/library/assistant.jsp?type='+type+'&product='+product,'replace',winParms);    
		astWin.focus();		
}

function displaySelection(type,appliRoot) {
	
	//alert("type"+type);
	var tempIds = eval(type + "Ids");
	var tempLiterales = eval(type + "Literales");
	var tempOperators = eval(type + "Operators");
	//alert("tempIds="+tempIds.length);
	//alert("tempLiterales="+tempLiterales.length);
	//alert("tempOperators="+tempOperators.length);
	var td = document.getElementById("oTDf_"+type+"_vis");
	var text="";
	
	for (i=0 ; i<tempIds.length ; i++){
		//if (type=="tribunal")
		//	alert("Id="+tempIds[i]+"  Name="+tempLiterales[i]+" && SearchedId="+tribunalSearchIds[i]);
		text=text+"<img src='"+appliRoot+"/images/x.gif' border=0 onclick=\"javascript:undoCheck('"+tempIds[i]+"','"+type+"','"+appliRoot+"');\" alt='eliminar conceito'>"+"<font class='s11'>"+tempLiterales[i]+"</font>";
		//alert ("text="+text);
		if (i<tempIds.length-1){	
			var opImg="";
			
			if (tempOperators[i]=="o")
				opImg="ou.gif";				
			else if (tempOperators[i]=="y")
				opImg="e-and.gif";						
			else if (tempOperators[i]=="not")
				opImg="nao.gif";							
			
			text=text+"&nbsp;<img src='"+appliRoot+"/images/"+opImg+"' border=0 onclick=\"javascript:changeOp('"+i+"','"+type+"','"+appliRoot+"');\"  alt='modificar operador'>&nbsp;<br>";
		}
	}
	//for (i=0 ; i<tempOperators.length ; i++){
		//alert("Operator"+i+" = "+tempOperators[i]);
	//}
    td.innerHTML = text;    
    updateSearchMaskField(type);
}


function undoCheck(id,type,appliRoot){
	var tempIds = eval(type + "Ids");
	var tempLiterales = eval(type + "Literales");
	var tempOperators = eval(type + "Operators");
	 
	var undefined;
	var fEncontrado= false;
	for (i=0 ; i<tempIds.length ; i++)	{
		if (tempIds[i]==id)	{
			fEncontrado= true;	// The id is found
			break;
		}
	}
	if (fEncontrado){
		tempIds.splice(i,1);
		tempLiterales.splice(i,1);	
		tempOperators.splice(i,1);
		if ((type=="tribunal")||(type=="thesaurus")) {
		    var tempSearchIds = eval(type + "SearchIds");
		    tempSearchIds.splice(i,1);
		}
		displaySelection(type,appliRoot);
	}
}


function changeOp(id,type,appliRoot){
	var tempOperators = eval(type + "Operators");
	if (tempOperators[id]=="o")
		tempOperators[id]="y";	
	else if (tempOperators[id]=="y")
		tempOperators[id]="not";				
	else if (tempOperators[id]=="not")
		tempOperators[id]="o";	
	displaySelection(type,appliRoot);
}

function updateSearchMaskField(type){    
	// Get the values for the EIPQL
	var tempOperators = eval(type + "Operators");
	var tempValues;
	var fieldName = "";
	var fieldPrefix = "viewSearchMaskForm_ic";
	var fieldSuffix = "_inputValue";
  var category="";
	if (type == "juridiccion") {
		//not in use, to be cleaned
	    tempValues = eval(type + "Ids");     	
    	fieldName = fieldPrefix + "Juridiccion" + fieldSuffix;  
    	//document.formulario.juridiccionIds.value = juridiccionIds.join(';');   
    	//document.formulario.juridiccionLiterales.value = juridiccionLiterales.join(';');     
    	//document.formulario.juridiccionOperators.value = juridiccionOperators.join(';');  
	}
	else if (type == "range") {
	    category="WKPT-JN-CAT-05#"
	    tempValues = eval(type + "Ids");     	
	    fieldName = fieldPrefix + "range" + fieldSuffix; 
    	document.formulario.rangeIds.value = rangeIds.join(';');   
    	document.formulario.rangeLiterales.value = rangeLiterales.join(';');     
    	document.formulario.rangeOperators.value = rangeOperators.join(';');     
 	}
 	else if (type == "speaker") {
  	  category="WKPT-JN-CAT-06#"    	    	  
      tempValues = eval(type + "Ids");
      fieldName = fieldPrefix + "speaker" + fieldSuffix; 
    	document.formulario.speakerIds.value = speakerIds.join(';');    
    	document.formulario.speakerLiterales.value = speakerLiterales.join(';');  
    	document.formulario.speakerOperators.value = speakerOperators.join(';');     
 	}
 	else if (type =="tribunal") {
      tempValues = eval(type + "SearchIds");
      fieldName = fieldPrefix + "tribunal" + fieldSuffix; 
    	document.formulario.tribunalIds.value = tribunalIds.join(';');   
    	document.formulario.tribunalLiterales.value = tribunalLiterales.join(';');    
    	document.formulario.tribunalOperators.value = tribunalOperators.join(';');  
    	document.formulario.tribunalSearchIds.value = tribunalSearchIds.join(';');  
  }
  else  if(type == "officialBulletin") {
      category="WKPT-JN-CAT-07#"
      tempValues = eval(type + "Ids");
      fieldName = fieldPrefix + "officialBulletin" + fieldSuffix; 
    	document.formulario.officialBulletinIds.value = officialBulletinIds.join(';');    
    	document.formulario.officialBulletinLiterales.value = officialBulletinLiterales.join(';');  
    	document.formulario.officialBulletinOperators.value = officialBulletinOperators.join(';');     
  }
  else  if(type == "thesaurus") {
      tempValues = eval(type + "SearchIds");
      fieldName = fieldPrefix + "thesaurus" + fieldSuffix; 
    	document.formulario.thesaurusIds.value = thesaurusIds.join(';');   
    	document.formulario.thesaurusLiterales.value = thesaurusLiterales.join(';');    
    	document.formulario.thesaurusOperators.value = thesaurusOperators.join(';');  
    	document.formulario.thesaurusSearchIds.value = thesaurusSearchIds.join(';'); 
  }
 	else if (type == "autor") {
  	  category="WKPT-JN-CAT-08#"    	    	  
      tempValues = eval(type + "Ids");
      fieldName = fieldPrefix + "autor" + fieldSuffix; 
    	document.formulario.autorIds.value = autorIds.join(';');    
    	document.formulario.autorLiterales.value = autorLiterales.join(';');  
    	document.formulario.autorOperators.value = autorOperators.join(';');     
 	}
 	else if (type == "pubEditorial") {
  	  category="WKPT-JN-CAT-09#"    	    	  
      tempValues = eval(type + "Ids");
      fieldName = fieldPrefix + "pubEditorial" + fieldSuffix; 
    	document.formulario.pubEditorialIds.value = pubEditorialIds.join(';');    
    	document.formulario.pubEditorialLiterales.value = pubEditorialLiterales.join(';');  
    	document.formulario.pubEditorialOperators.value = pubEditorialOperators.join(';');     
 	}
 	else if (type == "editorial") {
  	  category="WKPT-JN-CAT-10#"    	    	  
      tempValues = eval(type + "Ids");
      fieldName = fieldPrefix + "editorial" + fieldSuffix; 
    	document.formulario.editorialIds.value = editorialIds.join(';');    
    	document.formulario.editorialLiterales.value = editorialLiterales.join(';');  
    	document.formulario.editorialOperators.value = editorialOperators.join(';');     
 	}
 	else if (type == "disposicionesComent") {
  	  category="WKPT-JN-CAT-11#"    	    	  
      tempValues = eval(type + "Ids");
      fieldName = fieldPrefix + "disposicionesComentAst" + fieldSuffix; 
    	document.formulario.disposicionesComentIds.value = disposicionesComentIds.join(';');    
    	document.formulario.disposicionesComentLiterales.value = disposicionesComentLiterales.join(';');  
    	document.formulario.disposicionesComentOperators.value = disposicionesComentOperators.join(';');     
 	}
 	
	var td = document.getElementById("oTDf_"+type+"_vis");
	var text="";

		
	for (i=0 ; i<tempValues.length ; i++){
	    text=text+category+tempValues[i];
		if (i<tempValues.length-1){	
			var opValue="";
			
			if (tempOperators[i]=="o")
				opValue=" OR ";			//was <o>	
			else if (tempOperators[i]=="y")
				opValue=" AND ";		// was <y>				
			else if (tempOperators[i]=="not")
				opValue=" AND NOT ";// was <y> <no>							
			
			text=text+opValue;
		}
	}	
  //alert("fieldName="+fieldName); 
  //alert("text="+text);
	document.getElementById(fieldName).value = text;  
 }
 
function GetQueryAndDisplayField(type,appliRoot) {
	var tempIds = eval(type + "Ids");
	var tempLiterales = eval(type + "Literales");
	var tempOperators = eval(type + "Operators");
	//alert("tempIds="+tempIds.length);
	//alert("tempLiterales="+tempLiterales.length);
	//alert("tempOperators="+tempOperators.length);
	var td = document.getElementById("oTDf_"+type+"_vis");
	var text="";
	
	for (i=0 ; i<tempIds.length ; i++){
		//if (type=="tribunal")
			//alert("Id="+tempIds[i]+"  Name="+tempLiterales[i]+" && SearchedId="+tribunalSearchIds[i]);
		text=text+"<img src='"+appliRoot+"/images/x.gif' border=0 onclick=\"javascript:undoCheck('"+tempIds[i]+"','"+type+"','"+appliRoot+"');\" alt='eliminar conceito'>"+"<font class='s11'>"+tempLiterales[i]+"</font>";
		if (i<tempIds.length-1){	
			var opImg="";
			
			if (tempOperators[i]=="o")
				opImg="ou.gif";				
			else if (tempOperators[i]=="y")
				opImg="e-and.gif";						
			else if (tempOperators[i]=="not")
				opImg="nao.gif";							
			
			text=text+"&nbsp;<img src='"+appliRoot+"/images/"+opImg+"' border=0 onclick=\"javascript:changeOp('"+i+"','"+type+"','"+appliRoot+"');\"  alt='modificar operador'>&nbsp;<br>";
		}
	}
	//for (i=0 ; i<tempOperators.length ; i++){
		//alert("Operator"+i+" = "+tempOperators[i]);
	//}
    td.innerHTML = text;    
}
 
function getArrayFromString(str) {
    result = new String(eval("document.formulario."+str+".value"));
    if (result != "")
        return result.split(";");
    return new Array();
}
 
function displayAssistants(appliRoot)
{ 
    //Not in use on CJ	
    //juridiccionIds=getArrayFromString("juridiccionIds");
    //juridiccionLiterales=getArrayFromString("juridiccionLiterales");
    //juridiccionOperators=getArrayFromString("juridiccionOperators");
    
    rangeIds=getArrayFromString("rangeIds");
    rangeLiterales=getArrayFromString("rangeLiterales");
    rangeOperators=getArrayFromString("rangeOperators");
    
    speakerIds=getArrayFromString("speakerIds");
    speakerLiterales=getArrayFromString("speakerLiterales");
    speakerOperators=getArrayFromString("speakerOperators");
    
    officialBulletinIds=getArrayFromString("officialBulletinIds");
    officialBulletinLiterales=getArrayFromString("officialBulletinLiterales");
    officialBulletinOperators=getArrayFromString("officialBulletinOperators");
    
    tribunalIds=getArrayFromString("tribunalIds");
    tribunalLiterales=getArrayFromString("tribunalLiterales");
    tribunalOperators=getArrayFromString("tribunalOperators");
    tribunalSearchIds=getArrayFromString("tribunalSearchIds");

    thesaurusIds=getArrayFromString("thesaurusIds");
    thesaurusLiterales=getArrayFromString("thesaurusLiterales");
    thesaurusOperators=getArrayFromString("thesaurusOperators");
    thesaurusSearchIds=getArrayFromString("thesaurusSearchIds");

    autorIds=getArrayFromString("autorIds");
    autorLiterales=getArrayFromString("autorLiterales");
    autorOperators=getArrayFromString("autorOperators");

    pubEditorialIds=getArrayFromString("pubEditorialIds");
    pubEditorialLiterales=getArrayFromString("pubEditorialLiterales");
    pubEditorialOperators=getArrayFromString("pubEditorialOperators");

    editorialIds=getArrayFromString("editorialIds");
    editorialLiterales=getArrayFromString("editorialLiterales");
    editorialOperators=getArrayFromString("editorialOperators");

    disposicionesComentIds=getArrayFromString("disposicionesComentIds");
    disposicionesComentLiterales=getArrayFromString("disposicionesComentLiterales");
    disposicionesComentOperators=getArrayFromString("disposicionesComentOperators");
    
    //GetQueryAndDisplayField("juridiccion",appliRoot);
    if(document.getElementById("oTDf_speaker_vis") != null){
    	GetQueryAndDisplayField("speaker",appliRoot);
    }
    if(document.getElementById("oTDf_thesaurus_vis") != null){
    	GetQueryAndDisplayField("thesaurus",appliRoot);
    }
    if(document.getElementById("oTDf_tribunal_vis") != null){
    	GetQueryAndDisplayField("tribunal",appliRoot);
    }
    if(document.getElementById("oTDf_officialBulletin_vis") != null){
    	GetQueryAndDisplayField("officialBulletin",appliRoot);
    }
    if(document.getElementById("oTDf_range_vis") != null){
     	GetQueryAndDisplayField("range",appliRoot);
    }
    if(document.getElementById("oTDf_autor_vis") != null){
    	GetQueryAndDisplayField("autor",appliRoot);
    }
    if(document.getElementById("oTDf_pubEditorial_vis") != null){
    	GetQueryAndDisplayField("pubEditorial",appliRoot);
    }
    if(document.getElementById("oTDf_editorial_vis") != null){
    	GetQueryAndDisplayField("editorial",appliRoot);
    }
    if(document.getElementById("oTDf_disposicionesComent_vis") != null){
    	GetQueryAndDisplayField("disposicionesComent",appliRoot);
    }
}


function emptyArrays(){
	//juridiccionIds.splice(0,juridiccionIds.length);
	//juridiccionLiterales.splice(0,juridiccionLiterales.length);
	//juridiccionOperators.splice(0,juridiccionOperators.length);
	
	rangeIds.splice(0,rangeIds.length);
	rangeLiterales.splice(0,rangeLiterales.length);
	rangeOperators.splice(0,rangeOperators.length);
	
	speakerIds.splice(0,speakerIds.length);
	speakerLiterales.splice(0,speakerLiterales.length);
	speakerOperators.splice(0,speakerOperators.length);
	
	officialBulletinIds.splice(0,officialBulletinIds.length);
	officialBulletinLiterales.splice(0,officialBulletinLiterales.length);
	officialBulletinOperators.splice(0,officialBulletinOperators.length);
	
	tribunalIds.splice(0,tribunalIds.length);
	tribunalLiterales.splice(0,tribunalLiterales.length);
	tribunalOperators.splice(0,tribunalOperators.length);
	tribunalSearchIds.splice(0,tribunalSearchIds.length);
	
	thesaurusIds.splice(0,thesaurusIds.length);
	thesaurusLiterales.splice(0,thesaurusLiterales.length);
	thesaurusOperators.splice(0,thesaurusOperators.length);
	thesaurusSearchIds.splice(0,thesaurusSearchIds.length);

	autorIds.splice(0,autorIds.length);
	autorLiterales.splice(0,autorLiterales.length);
	autorOperators.splice(0,autorOperators.length);

	pubEditorialIds.splice(0,pubEditorialIds.length);
	pubEditorialLiterales.splice(0,pubEditorialLiterales.length);
	pubEditorialOperators.splice(0,pubEditorialOperators.length);

	editorialIds.splice(0,editorialIds.length);
	editorialLiterales.splice(0,editorialLiterales.length);
	editorialOperators.splice(0,editorialOperators.length);

	disposicionesComentIds.splice(0,disposicionesComentIds.length);
	disposicionesComentLiterales.splice(0,disposicionesComentLiterales.length);
	disposicionesComentOperators.splice(0,disposicionesComentOperators.length);
}

