// JavaScript Document
// ########## Settings for "checkField", "checkPassword", "checkNumber" functions ##########
var background_error = "#FFFFFF";
var border_error = "#feb660 2px inset";
var font_error = "#FF0000";
// ########## End of settings ##########

function daiFocus(field) {
	document.getElementById(field).focus();
}


function submitForm(form) {
	document.getElementById(form).submit();
}


function avviaForm(checkBox, formName) {
	if (document.getElementById(checkBox).checked == false) {
		document.getElementById('ValidationReplyMessage').innerHTML = "Consenso alla Privacy obbligatorio";
	} else {
		SendForm(formName);
	}			
}


function fieldManagement (field, title, value) {
	var campo = document.getElementById(field);
	
	campo.style.backgroundColor = "#F3E7EF";
	campo.style.color = "black";
	if (campo.value == title) {
		campo.value = "";
	}
}


function fieldAway (field, title) {
	var campo = document.getElementById(field);
	
	campo.style.backgroundColor = "";
	campo.style.color = "";
	if (campo.value == "") {
		campo.value = title;
	}
}


function visualizzaInfo(idDiv,status1,status2) {
	if (document.getElementById(idDiv).className == status1) {
		document.getElementById(idDiv).className = "visibile";
	} else {
		document.getElementById(idDiv).className = "nascosto";	
	}
}


function checkEmail(email){
	var mail = true;
	var campo = document.getElementById(email);
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;

	if(reg.test(campo.value) == false) {
		mail = false;
	}
	
	return mail;
}


function checkField(x) {
	var field = true;
	var campo = document.getElementById(x);
	
	if (campo.value == "") {
		field = false;
		campo.style.border = border_error;
	} else {
		campo.style.border = "";	
	}
	
	return field;
}


function checkPassword(x){
	var pswd = true;
	var campo = document.getElementById(x);
	
	if (campo.value.length <= 4) {
		pswd = false;
		campo.style.color = font_error;
	} else {
		campo.style.color = "";	
	}
	
	return pswd;
}


function checkNumber(x) {
	var numb3r = true;
	var campo = document.getElementById(x);

	if ( (campo.value == "") || (campo.value == 0) || (isNaN(campo.value) == true) ) {
		numb3r = false;
		campo.style.backgroundColor = background_error;
	} else {
		campo.style.backgroundColor = "";
	}
	
	return numb3r;
}


function checkForm(t,form) {
	var validation = true;
	var correct_email = true;
	var check_privacy = true;
	
	switch (form) {
		case 1: case "1": // ########## Login ##########
		obbligatori = new Array ('user','pass');
		break;
		
		case 2: case "2": // ########## Crea + Edit Materiale ##########
		obbligatori = new Array ('nome_ita','nome_eng');
		break;
		
		case 3: case "3": // ########## Crea Espositore ##########
		obbligatori = new Array ('materiale','nome_ita','nome_eng','foto');
		break;
		
		case 4: case "4": // ########## Edit Espositore ##########
		obbligatori = new Array ('materiale','nome_ita','nome_eng');
		break;
		
		case 5: case "5": // ########## Edit News ##########
		obbligatori = new Array ('titolo','data');
		break;
		
		case 6: case "6": // ########## Crea News ITA ##########
		obbligatori = new Array ('titolo_ita','data','foto');
		break;
		
		case 7: case "7": // ########## Crea News ENG ##########
		obbligatori = new Array ('titolo_eng','data','foto');
		break;
		
		case 8: case "8": // ########## Crea News MULTILINGUA ##########
		obbligatori = new Array ('titolo_ita','titolo_eng','data','foto');
		break;
		
		case 9: case "9": // ########## Crea + Edit Rivenditore ##########
		obbligatori = new Array ('zona','nome','indirizzo1','indirizzo2','cap','citta');
		break;
		
		default:
		alert ('Errore durante la validazione del Form!');
		break;
	}
	
	for (c=0; c<obbligatori.length; c++) {
		//alert(obbligatori[c]);
		
		if (checkField(obbligatori[c]) == false) {		
			validation = false;
		}
		
		if (obbligatori[c] == "Check_privacy" || obbligatori[c] == "check_privacy") {
			if (document.getElementById(obbligatori[c]).checked == false) {
				validation = false;
				check_privacy = false;
			}
		}
		
		if (obbligatori[c] == "Email" || obbligatori[c] == "email" || obbligatori[c] == "Mail" || obbligatori[c] == "mail" || obbligatori[c] == "Email_amico" || obbligatori[c] == "email_amico") {
			if (checkEmail(obbligatori[c]) == false) {
				validation = false;
				correct_email = false;
			}
		}
	
	}
	
	if (check_privacy == false) {
		alert ('Attenzione, consenso alla Privacy obbligatorio!');
	} else if (correct_email == false) {
		alert ('Attenzione, indirizzo email non valido!');
	} else if (validation == false) {
		alert ('Attenzione, controllare i campi obbligatori!');
	}

	return validation;
}


var controllo = false;
function controlSwitch (switcher) {
	if	(switcher == 0) {
		controllo = false;
		return controllo;
	} else {
		controllo = true;
		return controllo;	
	}
}


function deleteConfirmation(t,x) {
	var permission = true;
	
	if (controllo == true) {
	
		var notifica = "Attenzione, cancellando il presente oggetto, saranno cancellati anche tutti i suoi rispettivi prodotti. \r\nVuoi continuare?";
		
		if (!confirm(notifica)) {
			permission = false;
		};
		
	}
		
	return permission;
}


function modificaCancella (t,form) {
	var modifica = deleteConfirmation(this, controllo);
	var cancella = checkForm(this,form);
	
	if (modifica == true && cancella == true) {
		return true;
	} else {
		return false;	
	}
}


function checkCartInsert(t, field) {
	var validation = true;
	
	if (checkNumber(field) == false) {		
		validation = false;
	}
	
	if (validation == false) {
		alert ('Attenzione, selezionare la quantita\' di articoli da inserire nel carrello.');
	} 
	return validation;
}

function modificaCarrello(field, form) {
	var validation = true;
	
	if (checkNumber(field.id) == false) {		
		validation = false;
	}
	
	if (validation == false) {
		alert ('Attenzione, inserire un numero intero.');
	} else {
		submitForm(form.id);
	}
	
}


function activateDeactivate (t, field) {
	var checkbox = document.getElementById(t.id);
	var target = document.getElementById(field);
	
	if (checkbox.checked == true) {
		target.disabled = false;
	} else {
		target.disabled = true;	
	}
}


function showHide(t, l1, l2, l3) {
	var option = document.getElementById(t.id);
	var controllo = document.getElementById('controllo');
	var blocco1a = document.getElementById('italiano1');
	var blocco2a = document.getElementById('italiano2');
	var blocco3a = document.getElementById('italiano3');
	var blocco1b = document.getElementById('english1');
	var blocco2b = document.getElementById('english2');
	var blocco3b = document.getElementById('english3');
	
	if (option.value == l1) {
		
		controllo.value = 6;
		blocco1a.style.display = "block";
		blocco2a.style.display = "block";
		blocco3a.style.display = "block";
		blocco1b.style.display = "none";
		blocco2b.style.display = "none";
		blocco3b.style.display = "none";
		
	} else if (option.value == l2) {
		
		controllo.value = 7;
		blocco1a.style.display = "none";
		blocco2a.style.display = "none";
		blocco3a.style.display = "none";
		blocco1b.style.display = "block";
		blocco2b.style.display = "block";
		blocco3b.style.display = "block";
		
	} else if (option.value == l3) {
		
		controllo.value = 8;
		blocco1a.style.display = "block";
		blocco2a.style.display = "block";
		blocco3a.style.display = "block";
		blocco1b.style.display = "block";
		blocco2b.style.display = "block";
		blocco3b.style.display = "block";
		
	}
	
}
