function enableInputText(inputTextId) {
	//alert(inputTextId);
	var inputText = document.getElementById(inputTextId);
	//alert(inputText);
	inputText.value="";
	if (inputText.disabled) {
		inputText.disabled=false;
		inputText.style.backgroundColor="#ffffff";
	} else {
		inputText.disabled=true;
		inputText.style.backgroundColor="#878787";
	}		
}

function enableTipoUtente() {
	var select = document.getElementById("tipo_utente");
	//alert(select.value);
	//il privato ha solo il codice fiscale
	if (select.value=="privato") {
		hideDiv('iva_row');
		hideDiv('ragione_row');
	//il rivenditore ha il codice fiscale, la partita iva e la ragione sociale
	} else {
		showDiv('iva_row');
		showDiv('ragione_row');
	}
}
		
function hideDiv(fieldName) {
	document.getElementById(fieldName).style.display = 'none';
}

function showDiv(fieldName) {
	document.getElementById(fieldName).style.display = 'block';
}

/** Mostra un div se è nascosto, lo nasconde se è visibile */
function showHideDiv(divId) {
	// alert(divId);
	// alert(document.getElementById(divId).style.display);
	if (document.getElementById(divId).style.display == 'block')
		document.getElementById(divId).style.display = 'none';
	else
		document.getElementById(divId).style.display = 'block';
}

// array of images
var imgs = new Array(94);
imgs[0] = "images/menu/first/edifici_medievali_out.jpg";
imgs[1] = "images/menu/first/edifici_medievali_over.jpg";

imgs[2] = "images/menu/first/edifici_fantasy_out.jpg";
imgs[3] = "images/menu/first/edifici_fantasy_over.jpg";

imgs[4] = "images/menu/first/edifici_medievali_out.jpg";
imgs[5] = "images/menu/first/edifici_medievali_over.jpg";

imgs[6] = "images/menu/first/giungla_out.jpg";
imgs[7] = "images/menu/first/giungla_over.jpg";

imgs[8] = "images/menu/first/deserto_out.jpg";
imgs[9] = "images/menu/first/deserto_over.jpg";

imgs[10] = "images/menu/first/landa_out.jpg";
imgs[11] = "images/menu/first/landa_over.jpg";

imgs[12] = "images/menu/first/terre_elfiche_out.jpg";
imgs[13] = "images/menu/first/terre_elfiche_over.jpg";

imgs[14] = "images/menu/first/pianeta_roccioso_out.jpg";
imgs[15] = "images/menu/first/pianeta_roccioso_over.jpg";

imgs[16] = "images/menu/first/basette_new_out.jpg";
imgs[17] = "images/menu/first/basette_new_over.jpg";

imgs[18] = "images/menu/first/particolari_out.jpg";
imgs[19] = "images/menu/first/particolari_over.jpg";

imgs[20] = "images/menu/first/accessori_out.jpg";
imgs[21] = "images/menu/first/accessori_over.jpg";

imgs[52] = "images/menu/first/BasetteQOFF.jpg";
imgs[53] = "images/menu/first/BasetteQON.jpg";

imgs[54] = "images/menu/first/BasetteTOFF.jpg";
imgs[55] = "images/menu/first/BasetteTON.jpg";

imgs[56] = "images/menu/first/guerramondiale_out.jpg";
imgs[57] = "images/menu/first/guerramondiale_over.jpg";

imgs[58] = "images/menu/first/panni_out.jpg";
imgs[59] = "images/menu/first/panni_over.jpg";

imgs[76] = "images/menu/first/BasetteSOFF.jpg";
imgs[77] = "images/menu/first/BasetteSON.jpg";

imgs[78] = "images/menu/first/ac_out.jpg";
imgs[79] = "images/menu/first/ac_over.jpg";

imgs[80] = "images/menu/first/am_out.jpg";
imgs[81] = "images/menu/first/am_over.jpg";

imgs[82] = "images/menu/first/bs_out.jpg";
imgs[83] = "images/menu/first/bs_over.jpg";

imgs[84] = "images/menu/first/ed_fantasy_out.jpg";
imgs[85] = "images/menu/first/ed_fantasy_over.jpg";

imgs[86] = "images/menu/first/ed_futuro_out.jpg";
imgs[87] = "images/menu/first/ed_futuro_over.jpg";

imgs[88] = "images/menu/first/egitto_out.jpg";
imgs[89] = "images/menu/first/egitto_over.jpg";

imgs[90] = "images/menu/first/es_out.jpg";
imgs[91] = "images/menu/first/es_over.jpg";

imgs[92] = "images/menu/first/slot_out.jpg";
imgs[93] = "images/menu/first/slot_over.jpg";

imgs[94] = "images/menu/second/pannelli_out.jpg";
imgs[95] = "images/menu/second/pannelli_over.jpg";

/* secondo menu */
imgs[22] = "images/menu/second/tavoli_out.jpg";
imgs[23] = "images/menu/second/tavoli_over.jpg";

imgs[24] = "images/menu/second/pannelli_out.jpg";
imgs[25] = "images/menu/second/pannelli_over.jpg";

imgs[26] = "images/menu/second/diorami_out.jpg";
imgs[27] = "images/menu/second/diorami_over.jpg";

imgs[28] = "images/menu/second/imbasettamento_out.jpg";
imgs[29] = "images/menu/second/imbasettamento_over.jpg";

/* terzo menu */
imgs[30] = "images/menu/third/home_out.jpg";
imgs[31] = "images/menu/third/home_over.jpg";

imgs[32] = "images/menu/third/punti_vendita_out.jpg";
imgs[33] = "images/menu/third/punti_vendita_over.jpg";

imgs[34] = "images/menu/third/download_out.jpg";
imgs[35] = "images/menu/third/download_over.jpg";

imgs[36] = "images/menu/third/link_out.jpg";
imgs[37] = "images/menu/third/link_over.jpg";

imgs[38] = "images/menu/third/come_ordinare_out.jpg";
imgs[39] = "images/menu/third/come_ordinare_over.jpg";

imgs[40] = "images/menu/third/condizioni_out.jpg";
imgs[41] = "images/menu/third/condizioni_over.jpg";

imgs[42] = "images/menu/third/spese_spedizione_out.jpg";
imgs[43] = "images/menu/third/spese_spedizione_over.jpg";

imgs[44] = "images/menu/third/prezzi_out.jpg";
imgs[45] = "images/menu/third/prezzi_over.jpg";

imgs[46] = "images/menu/third/contatti_out.jpg";
imgs[47] = "images/menu/third/contatti_over.jpg";

imgs[48] = "images/menu/third/credits_out.jpg";
imgs[49] = "images/menu/third/credits_over.jpg";

imgs[50] = "images/menu/third/privacy_out.jpg";
imgs[51] = "images/menu/third/privacy_over.jpg";

imgs[60] = "images/menu/third/novita_out.jpg";
imgs[61] = "images/menu/third/novita_over.jpg";

/******************** carrello ***************************/

imgs[62] = "images/catalogo/articolo/carrello_out.jpg";
imgs[63] = "images/catalogo/articolo/carrello_over.jpg";

imgs[64] = "images/catalogo/articolo/dettagli_out.jpg";
imgs[65] = "images/catalogo/articolo/dettagli_over.jpg";

/******************** novita ***************************/

imgs[66] = "images/ordina_novita_out.jpg";
imgs[67] = "images/ordina_novita_over.jpg";

/******************** modulo d'ordine ***************************/

imgs[68] = "images/aggiorna_out.jpg";
imgs[69] = "images/aggiorna_over.jpg";

imgs[70] = "images/svuota_carrello_out.jpg";
imgs[71] = "images/svuota_carrello_over.jpg";

/****************************************************************/

imgs[72] = "images/paga_out.jpg";
imgs[73] = "images/paga_over.jpg";

/****************************************************************/

imgs[74] = "images/catalogo/articolo/video_out.jpg";
imgs[75] = "images/catalogo/articolo/video_over.jpg";

// preload the images
function preload() {
	var tmp = null;
	for ( var j = 0; j < imgs.length; j++) {
		tmp = imgs[j];
		imgs[j] = new Image();
		imgs[j].src = tmp;
	}
}

void (preload());

function changeImage(img, imgArrayId) {
	//alert(img);
	//alert(imgArrayId);
	img.src = imgs[imgArrayId].src;
}

function submitForm(formId) {
	// alert("form: "+document.getElementById(formId));
	// alert("action: "+document.getElementById(formId).action.value);
	document.getElementById(formId).submit();
}

function submitFormSearch() {
	var isOk = false;
	var nome_prodotto = document.getElementById("nome_prodotto");
	//alert("'"+nome_prodotto.value+"'");
	if (nome_prodotto.value == "")
		alert("Inserire una parola o un codice nel capo di ricerca");
	else 
		isOk = true;
	//alert(isOk);
	return isOk;
}

function submitFormModuloOrdine(formId) {
	var isOk = checkQuantita(formId);
	if (isOk)
		document.getElementById(formId).submit();
}

function submitLogin() {
	document.getElementById("loginForm").submit();
}

function showLoginAlert() {
	alert("Per poter effettuare ordini registrati ed esegui il LOGIN ");
}

// svuota il carrello
function clearCart() {
	if (confirm("Con questa operazione il carrello sarà svuotato.\nConfermi l'operazione?")) {
		document.location = "cart_clear.php?page=mo"
	}
}

// ritorna true se l'utente immette un numero, false altrimenti
function checkQuantita(formId) {
	var quantita = document.getElementById(formId).quantita.value;
	// alert("quantita num? "+isNaN(quantita)+" quantita: "+quantita);
	// si accettano solo numeri
	if (isNaN(quantita)) {
		alert("Inserire un valore numerico nel campo quantità.");
		return false;
	} else {
		document.getElementById(formId).n_articoli.value = quantita;
		// alert("quantita: "+quantita);
		// alert("document.getElementById(formId).n_articoli.value:
		// "+document.getElementById(formId).n_articoli.value);
		return true;
	}
}

// ritorna true se è stato selezionato almeno un prodotto da pagare, false
// altrimenti
function checkProdottiSelezionati() {
	if (document.getElementById("prodotto_selezionato"))
		return true
	else {
		alert("Per procedere, selezionare almeno un prodotto da pagare");
		return false;
	}
}

function controllaFormRegistrazione(form) {
	var isOk = false;
	
	var select = document.getElementById("tipo_utente");

	//alert("tipo utente: '"+select.value+"'");
	//alert("tipo utente2: '"+form.tipo_utente.value+"'");
	//alert("pwd value3: '"+form.password.value+"'");
	//var password = document.getElementById("password");
	//var conferma_password = document.getElementById("conferma_password");
	
	/*if (form.username.value == "") {
		alert("Inserire un valore nel campo \"Username\"");
	} else */
	if (form.password && form.password.value == "") {
		alert("Inserire un valore nel campo \"Password\"");

	} else if (form.conferma_password && form.conferma_password.value == "") {
		alert("Inserire un valore nel campo \"Conferma Password\"");

	} else if (form.password && form.conferma_password && form.password.value != form.conferma_password.value) {
		alert("La password non coincide con quella scritta nel campo \"Conferma Password\"");

	} else if (form.cognome.value == "") {
		alert("Inserire un valore nel campo \"Cognome\"");

	} else if (form.nome.value == "") {
		alert("Inserire un valore nel campo \"Nome\"");

	} else if ((select.value=="privato") && !isCodiceFiscale(form.cf)) {
		alert("Codice Fiscale non valido");

	} else if (form.cf.value == "") {
		alert("Inserire un valore nel campo \"Codice Fiscale\"");

	} else if ((select.value=="rivenditore") && form.ragione_sociale.value == "") {
		alert("Inserire un valore nel campo \"Ragione sociale\"");

	} else if ((select.value=="rivenditore") && form.iva.value == "") {
		alert("Inserire un valore nel campo \"Partita iva\"");

	} else if (form.indirizzo.value == "") {
		alert("Inserire un valore nel campo \"Indirizzo\"");

	} else if (form.cap.value == "") {
		alert("Inserire un valore nel campo \"Cap\"");

	} else if (form.citta.value == "") {
		alert("Inserire un valore nel campo \"Citta\"");

	} else if (form.provincia.value == "") {
		alert("Inserire un valore nel campo \"Provincia\"");

	} else if (form.telefono.value == "") {
		alert("Inserire un numero di telefono indispensabile al corriere che se non vi trova in casa vi contatta");
		
	} else if (form.paese.value == "") {
		alert("Inserire un valore nel campo \"Paese\"");

	} /*else if (form.email.value == "") {
		alert("Inserire un valore nel campo \"Posta elettronica\"");

	} */else if (form.privacy.checked == false) {
		alert("Acconsentire al trattamento dei dati personali");
		return (false);
	} else
		isOk = true;
	// if (form.iva.value.length > 0 && !isPartitaIVA(form.iva.value)) {
	// alert("Partita IVA non valido");
	// return false;
	// }

	return isOk;
}

function isCodiceFiscale(obj) {
	var nTempNum;
	var nAppoNum;
	var i;
	var szTempCodFisc;
	var szLastChar;
	var szValued;
	var szInValued;

	szCodFisc = obj.value;

	if (szCodFisc.length < 16)
		return false
	if (szCodFisc == "")
		return false;
	else {
		szCodFisc = szCodFisc.toUpperCase();
		szLastChar = szCodFisc.substr(szCodFisc.length - 1, 1);
		szTempCodFisc = szCodFisc.substr(0, szCodFisc.length - 1);
		nTempNum = 0;
		i = 0;

		while (true) {
			szValued = "B1A0KKPPLLC2QQD3RRE4VVOOSSF5TTG6UUH7MMI8NNJ9WWZZYYXX";
			// I DISPARI
			szInValued = szTempCodFisc.substr(i, 1);
			nAppoNum = szValued.search(szInValued);
			nTempNum = nTempNum + (nAppoNum & 0x7FFE) / 2;
			i = i + 1;
			if (i > 15)
				break;
			szValued = "A0B1C2D3E4F5G6H7I8J9KKLLMMNNOOPPQQRRSSTTUUVVWWXXYYZZ";
			// I PARI
			szInValued = szTempCodFisc.substr(i, 1);
			nAppoNum = szValued.search(szInValued);
			nTempNum = nTempNum + (nAppoNum & 0x7FFE) / 2;
			i = i + 1;
		}
		nTempNum = nTempNum % 26; // mod
		szValued = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		szValued = szValued.substr(nTempNum, 1);
		if (szValued == szLastChar)
			return true;
		else
			return false;
	}
}

function isPartitaIVA(sz_Codice) {
	var n_Val, n_Som1 = 0, n_Som2 = 0, lcv;
	if (sz_Codice.length != 11 || isNaN(parseFloat(sz_Codice))
			|| parseFloat(sz_Codice) < parseFloat(0))
		return false;

	for (lcv = 0; lcv < 9; lcv += 2) {
		n_Val = parseInt(sz_Codice.charAt(lcv));
		n_Som1 += n_Val;
		n_Val = parseInt(sz_Codice.charAt(lcv + 1));
		n_Som1 += Math.floor(n_Val / 5) + (n_Val << 1) % 10;
	}
	n_Som2 = 10 - (n_Som1 % 10);
	n_Val = parseInt(sz_Codice.charAt(10));
	if (n_Som2 == n_Val)
		return true;
	return false;
}

function checkProdottoSelezionato() {
	var checkProdottoSelezionato = false;
    
    for (counter = 0; counter < document.storicoForm.riga_prodotto_selezionato.length; counter++) {
        if (document.storicoForm.riga_prodotto_selezionato[counter].checked) 
        	checkProdottoSelezionato = true;
    }
    
    if (checkProdottoSelezionato) 
		checkProdottoSelezionato = true;
	else
		alert("Per procedere, selezionare un ordine");
	
    return checkProdottoSelezionato;
}

