// JavaScript Document

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 = "#322c21";
	campo.style.color = "white";
	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 = "#7b764e 2px inset";
	} 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 = "#FF0000";
	} else {
		campo.style.color = "";	
	}
	
	return pswd;
}


function checkForm(t,form) {
	var validation = true;
	var correct_email = true;
	var check_privacy = true;
	
	switch (form) {
		case 0: // ########## Newsletter ##########
		obbligatori = new Array ('Nome','Email');
		break;
		
		case 1: // ########## Login ##########
		obbligatori = new Array ('user','pass');
		break;
		
		case 2: // ########## Crea + Edit Categoria ##########
		obbligatori = new Array ('categoria');
		break;
		
		case 3: // ########## Crea + Edit Brand ##########
		obbligatori = new Array ('nome');
		break;
		
		case 4: // ########## Crea ##########
		obbligatori = new Array ('categoria','codice','brand','euro','centesimi','foto');
		break;
		
		case 5: // ########## Edit Prodotto ##########
		obbligatori = new Array ('categoria','codice','brand','euro','centesimi');
		break;
		
		case 6: // ########## Dillo ad un amico ##########
		obbligatori = new Array ('Nome','Cognome','Nome_amico','Email_amico');
		break;
		
		case 7: // ########## Crea + Edit Contatto Mailing List ##########
		obbligatori = new Array ('nome','email');
		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() {
	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;
}