// JavaScript Document
function execsubmit(nform)
{
	document.forms[nform].submit();
}
function cancella_avviso(id_div)
{
	document.getElementById(id_div).innerHTML = "";
}
function eliminare(tutto){
	if(confirm("Confermi l'eliminazione dei dati selezionati?")){
		return true;
	} else {
		if(tutto== true){
			checkedAll(false);
		}
		return false;
	}
}
function changeBackground(tr, caso, input){
	if(input != '')
	{
		var check = document.getElementById(input);
	}
	switch(caso){
		case 1:
			tr.style.backgroundColor = "#b2deff";
		break;
		case 2:
			if(check == null || !check.checked){
				tr.style.backgroundColor = "";
			}
		break;
	}
}
function selectCheck(input){
	var check = document.getElementById(input);
		unique(check);
		check.checked = "checked";
}
function unique(input){
	var arrayInput = document.getElementsByTagName('input');
	for(i=0; i<arrayInput.length; i++){
		tipoInput =arrayInput[i].type;
		if(tipoInput == "checkbox"){
			if(arrayInput[i].name != input.name && arrayInput[i].checked == true){
				arrayInput[i].checked = false;
				arrayInput[i].parentNode.parentNode.style.backgroundColor = "";
			}
		}
	}
}
function checkedAll(azione){
	var arrayInput = document.getElementsByTagName('input');
	for(i=0; i<arrayInput.length; i++){
		tipoInput =arrayInput[i].type;
		if(tipoInput == "checkbox"){
			arrayInput[i].checked = azione;
		}
	}
}
function verifica(nomeForm, id_div)
{
	var arrayInput = document[nomeForm].getElementsByTagName('input');
	var arraySelect = document[nomeForm].getElementsByTagName('select');
	var arrayTarea = document[nomeForm].getElementsByTagName('textarea');
	for(i=0; i<arrayTarea.length; i++){
		TareaName = arrayTarea[i].name;
		Empty = false;
		if("arrayExclude" in window){
			for(j=0; j<arrayExclude.length; j++){
				if(arrayExclude[j] == TareaName){
					Empty = true;
				}
			}
		}
		if(document[nomeForm][TareaName].value == '' && Empty == false){
			document.getElementById(id_div).innerHTML = "<p style=\"color:red; font-weight:bold;\">L'area di testo "+TareaName+" non può essere vuota!</p>";
			return false;
		}
	}
	var arrayCheck = Array();
	var arrayInputDepend = Array();
	var risposta = true;
	var Dipendenza = null;
	var pass = "";
	if("arrayDepended" in window){
		for(j=0; j<arrayDepended.length; j++){
			ThisValoreArr = arrayDepended[j].split("=>");
			arrayInputDepend[ThisValoreArr[0]] = ThisValoreArr[1] ;
		}
	}
	if(arraySelect.length > 0){
		var verificaSelect = false;
		for(i=0; i<arraySelect.length; i++){
			if(arraySelect[i].value != ""){
				verificaSelect = true;
			}
		}
		if(!verificaSelect){
			document.getElementById(id_div).innerHTML = "<p style=\"color:red; font-weight:bold;\">Devi selezionare almeno una voce dell'elenco!</p>";
			return false;
		}
	}
	for(i=0; i<arrayInput.length; i++){
		exclude = false;
		tipoInput =arrayInput[i].type;
		nome_input = arrayInput[i].name;
		if("arrayExclude" in window){
			for(j=0; j<arrayExclude.length; j++){
				if(arrayExclude[j]==nome_input){
					exclude = true;
				}
			}
		}
		if(tipoInput != "button" && tipoInput != "submit" && tipoInput != "reset" && exclude != true){			
			nome_input = nome_input.replace("_", " ");
			if(arrayInput[i].value == ""){
				document.getElementById(id_div).innerHTML = "<p style=\"color:red; font-weight:bold;\">Il campo "+nome_input+" non può essere vuoto!</p>";
				return false;
			}
			if(tipoInput == "checkbox"){
				arrayCheck.push(arrayInput[i]);
			}
		}
	}
	if(arrayCheck.length >0){
		var check = false;
		for(i=0; i<arrayCheck.length; i++){
			if(arrayCheck[i].checked == true){
				check = true;
			}
		}
		if(check == false){
			document.getElementById(id_div).innerHTML = "<p style=\"color:red; font-weight:bold;\">Devi selezionare almeno una voce!</p>";
			return false;
		}
	}
	for(i=0; i<arrayInput.length; i++){
		exclude = false;
		tipoInput =arrayInput[i].type;
		nome_input = arrayInput[i].name;
		if("arrayExclude" in window){
			for(j=0; j<arrayExclude.length; j++){
				if(arrayExclude[j]==nome_input){// && arrayInput[i].value == ""
					exclude = true;
				}
			}
		}
		if(nome_input == "ripeti_password" && pass != ""){
			exclude = false;
		}
		if((tipoInput != "button" || tipoInput != "submit" || tipoInput != "reset" || tipoInput != "password") && exclude != true){
			if(arrayInputDepend[nome_input] != null){
				Dipendenza = arrayInputDepend[nome_input];
			}
			nome_input = nome_input.replace("_", " ");
			if(Dipendenza != null){
				if(arraySelect[Dipendenza] != null){
					MatchingValue = arraySelect[Dipendenza].value;
				}
				if(arrayInput[Dipendenza] != null){
					MatchingValue = arrayInput[Dipendenza].value;
				}
				if(tipoInput == "text"){
					if(MatchingValue == "e_mail" || MatchingValue == "e-mail"){
						if(!validateInput(arrayInput[i].value, 5)){
							document.getElementById(id_div).innerHTML = "<p style=\"color:red; font-weight:bold;\">Il formato dell'indirizzo "+MatchingValue+" non è valido!</p>";
							return false;
						}
					} else {
						if(!validateInput(arrayInput[i].value, 1)){
							document.getElementById(id_div).innerHTML = "<p style=\"color:red; font-weight:bold;\">Il campo "+MatchingValue+" contiene caratteri non validi!</p>";
							return false;
						}
					}
				}
			} else {
				if(tipoInput == "text"){
					if(arrayInput[i].name == "e_mail"){
						if(!validateInput(arrayInput[i].value, 5)){
							document.getElementById(id_div).innerHTML = "<p style=\"color:red; font-weight:bold;\">Il formato dell'indirizzo "+nome_input+" non è valido!</p>";
							return false;
						}
					} else {
						if(!validateInput(arrayInput[i].value, 1)){
							document.getElementById(id_div).innerHTML = "<p style=\"color:red; font-weight:bold;\">Il campo "+nome_input+" contiene caratteri non validi!</p>";
							return false;
						}
					}
				} else if(tipoInput = "password"){
					if(nome_input == "password"){
						pass = arrayInput[i].value;
					} else if(nome_input == "ripeti password"){
						rep_pass = arrayInput[i].value;
						if(pass != rep_pass){
							document.getElementById(id_div).innerHTML = "<p style=\"color:red; font-weight:bold;\">Il campo "+nome_input+" non coincide con il capo password!</p>";
							pass = "";
							return false;
						}
					}
				}
			}
		}
	}
	return risposta;
}
function validateInput(sVal, str){
	switch (str){
		case 1:
		//validazione campo che ammette solo caratteri alfanumerici con possibilità di minuscole e maiuscole e spazi
		var regex= /^[(\\)(\/)-.:!'àèéùòì&\w\s\?]+$/;
	if(regex.test(sVal)){
		return true;
	} else {
		return false;
	}
	break;
	case 2:
	//validazione campo che ammette solo numeri
	var regex= /^[0-9]+$/;
	if(regex.test(sVal)){
		return true;
	} else {
		return false;
	}
	break;
	case 3:
	//validazione campo che ammette solo numeri con possibilità di decimali
	var regex= /^[0-9.]+$/;
	if(regex.test(sVal)){
		return true;
	} else {
		return false;
	}
	break;
	case 4:
		//validazione campo che ammette solo caratteri alfanumerici con possibilità di minuscole e maiuscole
		var regex= /^[\w]+$/;
	if(regex.test(sVal)){
		return true;
	} else {
		return false;
	}
	break;
	case 5:
		//Validazione e-mail
	var reg_mail = RegExp("^([a-zA-Z0-9\._-]+)(@)([a-zA-Z0-9\.-]+)([\.]{1})([a-z]{2,4})$");
 	if (reg_mail.test(sVal)){
		return true;
	} else{
        return false;
	  }
	break;
	}
}
/////////////////////////////////////////////////////////////////
//FUNZIONE PER LA GESTIONE DEI DIV CON PROPRIETA' DISPLAY: NONE//
/////////////////////////////////////////////////////////////////
function mostra_nascondiPrewiew(div)
{
	element = document.getElementById(div);
	element1 = document.getElementById('linkMN');
	if(element.style.display == "none"){
		element.style.display = "block";
		element1.innerHTML = "<a href=\"javascript:void(0);\" onclick=\"mostra_nascondiPrewiew('prova');\">Nascondi anteprima del testo</a>";
	} else{
		element.style.display = "none";
		element1.innerHTML = "<a href=\"javascript:void(0);\" onclick=\"mostra_nascondiPrewiew('prova');\">Mostra anteprima del testo</a>";
	}
}
function mostra_nascondi(div, div1)
{
	element = document.getElementById(div);
	element1 = document.getElementById(div1);
	if(element.style.display == "none"){
		element.style.display = "block";
		element1.style.display = "none";
	} else{
		element.style.display = "none";
		element1.style.display = "block";
	}
}
function ShowHideDiv(div){
	element = document.getElementById(div);
	if(element.style.display == "none"){
		element.style.display = "block";
	} else{
		element.style.display = "none";
	}
}
//////////////////
//FUNZIONI VARIE//
//////////////////
function jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}
function change_style(style, element){
	element.className = style;
}
function apri_anchor(ancora){
	window.open('#'+ancora);
}
function apri_popup(pagina, height, width ){
	posX = screen.width-300;
	pagopen = window.open(pagina,'_blank','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width='+width+',height='+height+',left='+posX+',top=20');
}
function simpleRollImg(imgName, url){
	name = imgName.replace(/_ov/i, '');
	document.images[name].src = url+'immagini/'+imgName+'.jpg';
}
function redimImgMapSite(){
	if("arrayMapSite" in window){
		counter = arrayMapSite.length;
		for(i=0; i<counter; i++){
			tdClass="dipendenza";
			class = 'spacer_full';
			class_1 = 'spacer_full';
			code = arrayMapSite[i];
			evalString = 0;
			arrayString = code.split('_');
			if(i==0){
				tdClass="tdHome";
			}
			if(i<counter-1){
				next = arrayMapSite[i+1];
				arrayString_1 = next.split('_');
				evalString = arrayString_1[1];
			}
			semiCode = arrayString[0]+'_'+arrayString[1];
			tdSel = document.getElementById('tdPage_'+semiCode);
			div_up = document.getElementById('spacer_up_'+semiCode);
			div_dw = document.getElementById('spacer_dw_'+semiCode);
			img = document.images['img_'+semiCode];
			val_a = document.getElementById('tdPage_'+semiCode).offsetHeight;
			alt_1 = img.offsetTop;
			alt_2 = alt_1+36;
			alt_2 = val_a-alt_2;
			alt_1 = alt_1+'px';
			alt_2 = alt_2+'px';
			larg_1 = arrayString[1]*15;
			larg_2 = larg_1;
			if(arrayString[2] != null){
				if(arrayString[2] == 'end'){
					larg_2 = larg_2-15;
				} 
			} else if(arrayString[1]<evalString){
				larg_2 = larg_2+15;
			}
			larg_1 = larg_1+'px';
			larg_2 = larg_2+'px';
			if(arrayString[2] == 01 || (arrayString[3] != null && arrayString[2] == 01)){
				class = 'spacer_empty';
			}
			tdSel.className = tdClass;
			div_up.className = class;
			div_dw.className = class_1;
			div_up.style.height = alt_1;
			div_up.style.width = larg_1;
			div_dw.style.height = alt_2;
			div_dw.style.width = larg_2;
			
		}
		
		
		
	}
}
