// JavaScript Document
//IMPOSTAZIONE VARIABILI GLOBALI//
var protocol = document.location.protocol

var root = document.location.host;
if(root=="localhost:8080"){
	root = root+"/associazionemorga";
}
var rootImg = protocol+"//"+root+"/immagini/";

function cancella_avviso(id_div)
{
	document.getElementById(id_div).innerHTML = "";
}
function verifica(nomeForm, id_div)
{
	var arrayInput = document.getElementsByTagName('input');
	var risposta = true;
	if(nomeForm != "pagina_personale"){
		for(i=0; i<arrayInput.length; i++){
			tipoInput =arrayInput[i].type;
			if(tipoInput != "button" || tipoInput != "submit" || tipoInput != "reset"){
				nome_input = arrayInput[i].name.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;
				}
			}
		}
	}
	for(i=0; i<arrayInput.length; i++){
		nome_input = arrayInput[i].name.replace("_", " ");
		tipoInput =arrayInput[i].type;
		if(tipoInput != "button" || tipoInput != "submit" || tipoInput != "reset" || tipoInput != "password"){
			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>";
						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_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 roll_img(urlImg, imgName){
	document.images[imgName].src = rootImg+urlImg;
}
function roll_img_btn(urlImg, btn){
	btn.style.backgroundImage = "url("+rootImg+urlImg+");";
}
function change_style(style, element){
	element.className = style;
}
function apri_popup(pagina, height, width ){
	window.open(pagina,'_blank','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width='+width+',height='+height+',left=100,top=20'); 
}