var idAreaTest = ['mainForm:ragioneSocialeCheck', 
				'mainForm:testataVetrinaCheck',
				'mainForm:vetrinaCheck',
				'mainForm:parolaChiaveCheck',
				'mainForm:titoloOffertaCheck',
				'mainForm:offertaCheck'];
									
var idTabDisp = ['mainForm:dispPartnershipComm',
				'mainForm:dispSviluppoRete',
				'mainForm:dispFornituraBeni',
				'mainForm:dispPartnershipProd',
				'mainForm:dispContratto',
				'mainForm:dispFornituraServ'];
					
var idTabAtt = ['mainForm:attivitaProduzione',
				'mainForm:attivitaServizi',
				'mainForm:attivitaPrestazione',
				'mainForm:attivitaCommer',
				'mainForm:attivitaVenditaDettaglio'];
				
var idTabMerc = ['mainForm:atecoGroup',
				'mainForm:menuSecondoLivello'];
				
var labelsTab =['mainForm:tabDisponibile_lbl',
				'mainForm:tabAttivita_lbl',
				'mainForm:tabZone_lbl',
				'mainForm:tabDimensione_lbl'];
				
var idCheckUsaFiltro = ['mainForm:usaFiltriDispCheck',
						'mainForm:usaFiltriAttivitaCheck',
						'mainForm:usaFiltriMercCheck'];


function closePanel(panelId) {
	if (SimpleTogglePanelManager.panels.get(panelId).status == "true") {
    	SimpleTogglePanelManager.toggleOnClient(panelId);
    }
}

function openPanel(panelId) {
	if (SimpleTogglePanelManager.panels.get(panelId).status == "false") {
    	SimpleTogglePanelManager.toggleOnClient(panelId);
    }
}

function abilitaUsaFiltro(tabFiltro){
	
	checkBox = document.getElementById(idCheckUsaFiltro[tabFiltro-1]);
	
	if(tabFiltro == 1){
		if (checkBox != null && checkBox.checked == false){
			enableCheckboxs(false, idTabDisp);
		} else {
			enableCheckboxs(true, idTabDisp);
		}
	}
	if(tabFiltro == 2){
		if (checkBox != null && checkBox.checked == false){
			enableCheckboxs(false, idTabAtt);
		} else {
			enableCheckboxs(true, idTabAtt);
		}	
	}
	if(tabFiltro == 3){ 
		select1 = document.getElementById(idTabMerc[0]);
    	select2 = document.getElementById(idTabMerc[1]);
		if (checkBox != null && checkBox.checked == false){
			select1.disabled = true;
			select2.disabled = true;
		} else {
			select1.disabled = false;
			select2.disabled = false;
		}	
	}
	
}

function disabilitaUsaFiltroOnLoad(tabFiltro){
	
	checkBox = document.getElementById(idCheckUsaFiltro[tabFiltro-1]);
	
	if(tabFiltro == 1){
		if (checkBox != null && checkBox.checked==false ){
			disableCheckboxs(idTabDisp);
		}
	}
	if(tabFiltro == 2){
		if (checkBox != null  && checkBox.checked==false){
			disableCheckboxs(idTabAtt);
		}	
	}
	if(tabFiltro == 3){ 
		select1 = document.getElementById(idTabMerc[0]);
    	select2 = document.getElementById(idTabMerc[1]);
		if (checkBox != null && checkBox.checked == false){
			select1.disabled = true;
			select2.disabled = true;
		} else {
			select1.disabled = false;
			select2.disabled = false;
		}	
	}
	
}	

function enableCheckboxs(value, idList) {

	for(var i=0; i < cdoInputs.length; i++) {
		if(cdoInputs[i].getAttribute('type') == 'checkbox') {
			var myCheckbox = cdoInputs[i];
			currentId = myCheckbox.id;
			for(var m=0; m < idList.length; m++) {
				if (currentId == idList[m]) {
					//se non sto caricando la pagina cambio il valore selezionato dalle "sotto-checkbox"
					if (myCheckbox.checked != value){
						checkChangeSingle(i);
					}
					if (value == false) {
						myCheckbox.disabled = true;
						checkChangeSingle(i);
					} else {
					//cdoInputs[i].checked = '';
						if(myCheckbox.checked == true) {
							myCheckbox.disabled = false;
							myCheckbox.checked = 'checked';
							if(document.getElementById('checkImage'+i) != undefined)
								document.getElementById('checkImage'+i).src=imgTrue;
						} else {
							myCheckbox.disabled = false;
							myCheckbox.checked = '';
							if(document.getElementById('checkImage'+i) != undefined)
								document.getElementById('checkImage'+i).src=imgFalse;
						}
					}
				}
			}
		}
	}							
}	


function disableCheckboxs(idList) {

	for(var i=0; i < cdoInputs.length; i++) {
		if(cdoInputs[i].getAttribute('type') == 'checkbox') {
			var myCheckbox = cdoInputs[i];
			currentId = myCheckbox.id;
			for(var m=0; m < idList.length; m++) {
				if (currentId == idList[m]) {
						myCheckbox.disabled = true;
						checkChangeSingle(i);
				}
			}
		}
	}							
}	

			
function selectAllCheckbox(value,idList){

	for(var i=0; i < cdoInputs.length; i++) {
		if(cdoInputs[i].getAttribute('type') == 'checkbox') {
			var myCheckbox = cdoInputs[i];
				
			currentId = myCheckbox.id;
			for(var m=0; m < idList.length; m++) {
				if (currentId == idList[m]) {
					if (myCheckbox.checked == !value)
						checkChangeSingle(i);
				}
			}
		}
	}			
}
function changeAreaTestuale(){
	if(document.getElementById("mainForm:tipoInformazioneCheck:0").checked == true){
		selectAreaTestualeAzienda(true);
		selectAreaTestualeOfferta(false);
		changeTabFilter(0);
	} else if(document.getElementById("mainForm:tipoInformazioneCheck:1").checked == true){
		selectAreaTestualeAzienda(false);
		selectAreaTestualeOfferta(true);
		changeTabFilter(1);	
	} 
}
function changeTabFilter(tipoSearch){
	for(var i=0; i < labelsTab.length; i++) {
		tab = document.getElementById(labelsTab[i]);
		if (tipoSearch == 0){
			tab.style.display="block";
			
		} else if (tipoSearch == 1){
			tab.style.display="none";
		}
	}
	if (tipoSearch == 0){
		RichFaces.switchTab('mainForm:tpSearch', 'mainForm:tabDisponibile', "");
	}
	if (tipoSearch == 1){
		RichFaces.switchTab('mainForm:tpSearch', 'mainForm:tabAreeMerc', "");
	}
}
function showHideRicercaAvanzata() {

	var checkBox = document.getElementById("mainForm:abilitaFiltriCheck");
	if(checkBox.checked == true){
		panelId='mainForm:stpSearch';
		openPanel(panelId);
		document.getElementById("boxSelezioneTipoInfo").style.display="block";
		document.getElementById("boxFiltriRicercaAvanzata").style.display="block";
		changeAreaTestuale();
	} else if(checkBox.checked == false){

		document.getElementById("boxFiltriRicercaAvanzata").style.display="none";
		document.getElementById("boxSelezioneTipoInfo").style.display="none";
		selectAreaTestualeAzienda(true);
		selectAreaTestualeOfferta(true);
	}
}

function switchCheckboxes(value, idList) {
	for(var i=0; i < cdoInputs.length; i++) {
		if(cdoInputs[i].getAttribute('type') == 'checkbox') {
			var myCheckbox = cdoInputs[i];
			
			currentId = myCheckbox.id;
			for(var m=0; m < idList.length; m++) {
				if (currentId == idList[m]) {
					if (myCheckbox.checked == !value)
						checkChangeSingle(i);
					if (value == false) {
						myCheckbox.disabled = true;
						checkChangeSingle(i);
					} else {
						myCheckbox.disabled = false;
						if (myCheckbox.checked == !value)
							checkChangeSingle(i);
					}
				}
			}
		}
	}					
}		
function selectAreaTestualeAzienda(value) {
	idAzienda = [idAreaTest[0],idAreaTest[1],idAreaTest[2],idAreaTest[3]];
	switchCheckboxes(value,idAzienda);			
}
		
function selectAreaTestualeOfferta(value) {
	idOfferta = [idAreaTest[4],idAreaTest[5]];
	switchCheckboxes(value,idOfferta);			
}
		
function controllaCerca(){
	checkBox = document.getElementById("mainForm:abilitaFiltriCheck");
	if(checkBox.checked == true){
		checkBoxDisp = document.getElementById(idCheckUsaFiltro[0]);
		checkBoxAtt = document.getElementById(idCheckUsaFiltro[1]);
		checkBoxMerc = document.getElementById(idCheckUsaFiltro[2]);	
		enableDisp = undefined;
		if (checkBoxDisp != null && checkBoxDisp.checked == true){
			 enableDisp = false;
			 for(var m=0; m < idTabDisp.length; m++) {
	   			checkbox = document.getElementById(idTabDisp[m]);
			 	if (checkbox.checked == true)
			 		enableDisp = true;
			}
		}
		enableAtt = undefined;
		if (checkBoxAtt != null && checkBoxAtt.checked == true){
			enableAtt = false;  
			for(var m=0; m < idTabAtt.length; m++) {
			 	checkbox = document.getElementById(idTabAtt[m]);
			 	if (checkbox.checked == true)
			 		enableAtt = true;
			} 
		} 
		enableMerc = undefined;
		if (checkBoxMerc != null && checkBoxMerc.checked == true){
			
			enableMerc = false;
			 select = document.getElementById(idTabMerc[0]);
			 for (i=1; i<select.options.length ; i++ ){
			 	
				if (select.options[i].selected == true) 
					enableMerc = true;
			} 	
		} 
		if (enableDisp == true && enableAtt == true && enableMerc == true) {
			return true;
		}else{
			nameTab ='';
			if (enableDisp == false) 
				nameTab += search_tab1_titolo+"\n ";
			if	(enableAtt == false) 
				nameTab += search_tab2_titolo+"\n ";
			if	(enableMerc == false) 
				nameTab += search_tab3_titolo+"\n " ;	
			if(nameTab != ''){	
				alert(search_alert_usaFiltro+"\n "+nameTab);		
				return false;
			}
		}
	} else if(checkBox.checked == false) {
		inputArea = document.getElementById("mainForm:searchQueryIt");
		if (inputArea.value !='' ){
			for(var i=0; i < idAreaTest.length; i++) {
				checkbox = document.getElementById(idAreaTest[i]);
				if (checkbox.checked == true )
				return true;
			}
			alert(search_alert_areaTestuale);
			return false;
		}
		return true;
	}
}		

function setCookie(c_name,value,expiredays){
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function getCookie(c_name){
	if (document.cookie.length>0)
	  {
	  c_start=document.cookie.indexOf(c_name + "=");
	  if (c_start!=-1)
	    {
	    c_start=c_start + c_name.length+1;
	    c_end=document.cookie.indexOf(";",c_start);
	    if (c_end==-1) c_end=document.cookie.length;
	    return unescape(document.cookie.substring(c_start,c_end));
	    }
	  }
	return "";
}

function updatePageSizeFromCookie() {
	num=getCookie('NumeroRisXPagina');
	pageSelects = document.getElementById("mainForm:selectRisPagina");
	if (num!=null && num!="") {
		for (i=0; i<pageSelects.options.length ; i++ ){
			if (pageSelects.options[i].value == num) {
				pageSelects.selectedIndex = i;
				break;
			}
		}
	} else {
		num=pageSelects.options[pageSelects.selectedIndex].value;
		setCookie('NumeroRisXPagina',num,365);
	}
}

function updateCookieFromPageSize() {
	num=getCookie('NumeroRisXPagina');
	pageSelects = document.getElementById("mainForm:selectRisPagina");
	num=pageSelects.options[pageSelects.selectedIndex].value;
	setCookie('NumeroRisXPagina',num,365);
}

function viewBoxNazioneItaliana(elem){
	index = elem.selectedIndex;
	box1 = document.getElementById("trZone");
	box2 = document.getElementById("trRegioni");
	box3 = document.getElementById("trProvince");
	if(elem.options[index].value == 'IT'){
		box1.style.display="";
		box2.style.display="";
		box3.style.display="";
	}else{
		box1.style.display="none";
		box2.style.display="none";
		box3.style.display="none";
	}
}

function hideIfNotEuropa(elem){
	index = elem.selectedIndex;
	box1 = document.getElementById("trZone");
	box2 = document.getElementById("trRegioni");
	box3 = document.getElementById("trProvince");
	if(elem.options[index].value != 'EU'){
		box1.style.display="none";
		box2.style.display="none";
		box3.style.display="none";
	}
}

function checkNazioneITPreSelected(){
	if (document.getElementById("mainForm:nazioneIT") != undefined){
		viewBoxNazioneItaliana(document.getElementById("mainForm:nazioneIT"));
	}else if (document.getElementById("mainForm:nazioneES") != undefined){
		viewBoxNazioneItaliana(document.getElementById("mainForm:nazioneES"));
	
	}else if (document.getElementById("mainForm:nazioneEN") != undefined){
		viewBoxNazioneItaliana(document.getElementById("mainForm:nazioneEN"));
	}
}

function showHideRicercaAvanzataOnLoad() {

	var checkBox = document.getElementById("mainForm:abilitaFiltriCheck");
	if(checkBox.checked == true){
		panelId='mainForm:stpSearch';
		openPanel(panelId);
		document.getElementById("boxSelezioneTipoInfo").style.display="block";
		document.getElementById("boxFiltriRicercaAvanzata").style.display="block";
	} else if(checkBox.checked == false){

		document.getElementById("boxFiltriRicercaAvanzata").style.display="none";
		document.getElementById("boxSelezioneTipoInfo").style.display="none";

	}
}

function updateSearchChecks() {
	showHideRicercaAvanzataOnLoad();
	panelId='mainForm:stpSearch';
	closePanel(panelId);
	disabilitaUsaFiltroOnLoad(1);
	disabilitaUsaFiltroOnLoad(2);
	disabilitaUsaFiltroOnLoad(3);
	updatePageSizeFromCookie();
	checkNazioneITPreSelected();
	
		
}

addEvent(window,'load',function(e) {
if(document.getElementsByTagName('body')) updateSearchChecks();});


