// JavaScript Document

// ########## Settings for "checkField", "checkChecked", "checkEmail", "checkNumber", "checkPassword" functions ##########
var border_error = "#FF0000 2px outset";
var background_error = "#FF6600";
var font_error = "#FF0000";
// ########## End of settings ##########

function alertTaglie(){
	alert ('ATTENZIONE: Devi selezionare tutte le taglie per i tuoi articoli');
	return false;
}

function checkSpesaMinima(num, t, form){
	
	var res = true;
	
	if(checkForm2(t, form) == false){
		res = false;
		return res;	
	}
	if (parseInt(num) < 25){
		res = false;
		alert ('ATTENZIONE: Il minimo d\'ordine e\' di Euro 25,00 escluse le spese di spedizione.');
		
	}
	return res;	
}

function checkForm2(t,form) {
	
	var validation = true; // Required Fields Validator Variable
	var check_privacy = true; // Privacy Checkbox Variable
	var correct_checkbox = true; // CheckBox Fields Validator Variable
	var correct_email = true; // Email Fields Validator Variable
	var correct_number = true; // Numeric Fields Validator Variable
	
	var errors = ""; // Error Alert Variable
	var checkbox_richieste = ""; // Required Checkbox (NOT for Privacy) Variable
	var campo_num = ""; // Numeric Fields Alert Variable
	
	var privacy_validator = document.getElementById('check_privacy');
	var check_validator = document.getElementById('check_checkboxes');
	var email_validator = document.getElementById('check_emails');
	var number_validator = document.getElementById('check_numbers');
	
	switch (form) {
		
		case 0: case "0": // ########## Login ##########
		obbligatori = new Array ('user','pass');
		break;
		
		case 1: case "1": // ########## Richiesta info ##########
		obbligatori = new Array ('nome','cognome','email', 'messaggio');
		break;
		
		case 2: case "2": // ########## Crea cliente ##########
		obbligatori = new Array ('nick','pass','email','tel','nome','cognome','via','civico','cap','citta','regione','provincia','giorno_nascita','mese_nascita','anno_nascita');
		break;
		
		case 3: case "3": // ########## Edit cliente ##########
		obbligatori = new Array ('nick','email','tel','nome','cognome','via','civico','cap','citta','regione','provincia','giorno_nascita','mese_nascita','anno_nascita');
		break;
		
		case 4: case "4": // ########## Modalità di pagamento Carrello ##########
		obbligatori = new Array ('pagamento');
		break;
		
		case 5: case "5": // ########## Inserisci Annuncio ##########
		obbligatori = new Array ('titolo', 'messaggio');
		break;
						
		default:
		alert ('Errore durante la validazione del Form!');
		break;
	}
	
	// Check if Required Fields are empty
	for (c=0; c<obbligatori.length; c++) {

		if (obbligatori[c] != "") {
			
			if (checkField(obbligatori[c]) == false) {		
				validation = false;
				errors += "- Attenzione, verificare il campo obbligatorio '" + obbligatori[c] + "'. \n";
			}
			
		}
	
	}
	
	// Check Privacy Checkbox(es) (if any)
	if ( privacy_validator != undefined ) {
		
		var array_privacy = privacy_validator.value.split(",");
		
		for (c=0; c<array_privacy.length; c++) {
			//alert(array_privacy[c]);
			
			if (array_privacy[c] != "") {
				
				if (checkChecked(array_privacy[c]) == false) {
					validation = false;
					check_privacy = false;
				}
			
			}
		
		}
	
	}
	
	// Check Required Checkboxes (if any)
	if ( check_validator != undefined ) {
		
		var array_checkboxes = check_validator.value.split(",");
		
		for (c=0; c<array_checkboxes.length; c++) {
			//alert(array_checkboxes[c]);
			
			if (array_checkboxes[c] != "") {
				
				if (checkChecked(array_checkboxes[c]) == false) {
					validation = false;
					correct_checkbox = false;
					checkbox_richieste += "- Contrassegnare il checkbox '" + array_checkboxes[c] + "'. \n";
				}
			
			}
		
		}
	
	}
	
	// Check the accuracy of Email Address Fields (if any)
	if ( email_validator != undefined ) {
		
		var array_emails = email_validator.value.split(","); 
		
		for (c=0; c<array_emails.length; c++) {
			//alert(array_emails[c]);
			
			if ( (array_emails[c] != "") && (document.getElementById(array_emails[c]).value != "") ) {
				
				if (checkEmail(array_emails[c]) == false) {		
					validation = false;
					correct_email = false;
				}
				
			}
		
		}
	
	}
	
	// Check the accuracy of Numeric Fields (if any)
	if ( number_validator != undefined ) {
		
		var array_numbers = number_validator.value.split(",");
		
		for (c=0; c<array_numbers.length; c++) {
			//alert(array_numbers[c]);
			
			if ( (array_numbers[c] != "") && (document.getElementById(array_numbers[c]).value != "") ) {
				
				if (checkNumber(array_numbers[c]) == false) {
					validation = false;
					correct_number = false;
					campo_num += "- Controllare il campo numerico '" + array_numbers[c] + "'. \n";
				}
			
			}
		
		}
	
	}
	

	if (check_privacy == false) {
		errors += "- Consenso alla Privacy obbligatorio. \n";
	}
	if (correct_checkbox == false) {
		errors += checkbox_richieste;
	}
	if (correct_email == false) {
		errors += "- Indirizzo email non valido. \n";
	}
	if (correct_number == false) {
		errors += campo_num;
	}
	
	if (errors != "") {
		alert (errors);
	} 

	return validation;
}



function checkField(ID) {
	var field = true;
	var campo = document.getElementById(ID);
	
	if (campo.value == "") {
		field = false;
		campo.style.border = border_error;
	} else {
		campo.style.border = "";	
	}
	
	return field;
}


function checkChecked(ID) {
	var checkbox = true;
	var campo = document.getElementById(ID);
	
	if (campo.checked == false) {
		checkbox = false;
		campo.style.backgroundColor = background_error;
	} else {
		campo.style.border = "";	
	}
	
	return checkbox;
}


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;
		campo.style.border = border_error;
	} else {
		campo.style.border = "";	
	}
	
	return mail;
}


function checkNumber(ID) {
	var numb3r = true;
	var campo = document.getElementById(ID);

	if ( (campo.value == "") || (campo.value == 0) || (isNaN(campo.value) == true) ) {
		numb3r = false;
		campo.style.border = border_error;
	} else {
		campo.style.border = "";
	}
	
	return numb3r;
}


function checkPassword(ID){
	var pswd = true;
	var campo = document.getElementById(ID);
	
	if (campo.value.length <= 4) {
		pswd = false;
		campo.style.color = font_error;
	} else {
		campo.style.color = "";	
	}
	
	return pswd;
}
