//CONTROLLI E CALCOLI SUI VALORI

function ControllaNumero(valore)
   {
	   Filtro = /^([1-9]+)([0-9]*)$/;
	   if (Filtro.test(valore))
			{return 'OK';}
	   else
			{return 'KO';}	
   }
   
function ControllaMinimo(valore,minimo)
   {
	   if (valore >= minimo)
			{return 'OK';}
	   else
			{return 'KO';}	
   }
 
function setControl(valore,field,form,minimo,fasce) {
	if (ControllaMinimo(valore,minimo) == 'KO'){
		window.alert('Per questo tipo di licenza, il numero minimo\nacquistabile è ' + minimo + '.')
		resetField(form)
	} else {
	// avvia il fillaggio degli altri campi
	fillaField(valore,field,form,minimo,fasce)}
}

function controlli(field,form,minimo,fasce){
	//window.alert(field + ' - ' + form + ' - ' + minimo)
	VALORE = document.getElementById(field+form).value
	if (VALORE != ''){
		if (ControllaNumero(VALORE) == 'KO'){
			window.alert('Valore non corretto.')
			resetField(form)
		} else {
			eval("setTimeout(\"setControl(" + VALORE + ",'" + field + "','" + form + "'," + minimo + "," + fasce + ")\",0);")
		}
	} else {
		resetField(form)
	}
 }
 
 function startControl(field,form,minimo,fasce){
	eval("setTimeout(\"controlli('" + field + "','" + form + "'," + minimo + "," + fasce + ")\",1000);")
 }
 
 function fillaField(valore,field,form,minimo,fasce){
	// individuazione della fascia di prezzo
	var prezzoUnitario = 0
	var prezzoTot = 0
	var prezzoTotIva = 0
	var prezzoTotGen = 0
	var spedizione = 0
	var prezzoStringa
	var lunghezza = 0
	//distinzione tra multilicenze e enterprise da monoutente
	
	//window.alert(prezziA2[1][1])
	
	if (form == 'A1' || form == 'R1'){
		if (form == 'A1') {
			prezzoUnitario = prezziA1[1][2]}
		if (form == 'R1') {
			prezzoUnitario = prezziR1[1][2]}
		document.getElementById('BU'+form).disabled = false
	} else {	
		if (form == 'A2'){
			for(i=1;i<=fasce;i++){
				if (prezziA2[i][1] == 0){
					if (valore >= prezziA2[i][0]){
						prezzoUnitario = prezziA2[i][2]
					}
				} else {
					if ((valore >= prezziA2[i][0]) && (valore <= prezziA2[i][1])){
						prezzoUnitario = prezziA2[i][2]
					}
				}
				document.getElementById('BU'+form).disabled = false
			}
		}
		
		if (form == 'A3'){
			for(i=1;i<=fasce;i++){
				if (prezziA3[i][1] == 0){
					if (valore >= prezziA3[i][0]){
						prezzoUnitario = prezziA3[i][2]
					}
				} else {
					if ((valore >= prezziA3[i][0]) && (valore <= prezziA3[i][1])){
						prezzoUnitario = prezziA3[i][2]
					}
				}
				document.getElementById('BU'+form).disabled = false
			}
		}
		
		if (form == 'A4'){
			for(i=1;i<=fasce;i++){
				if (prezziA4[i][1] == 0){
					if (valore >= prezziA4[i][0]){
						prezzoUnitario = prezziA4[i][2]
					}
				} else {
					if ((valore >= prezziA4[i][0]) && (valore <= prezziA4[i][1])){
						prezzoUnitario = prezziA4[i][2]
					}
				}
				document.getElementById('BU'+form).disabled = false
			}
		}
		
		if (form == 'R2'){
			for(i=1;i<=fasce;i++){
				if (prezziR2[i][1] == 0){
					if (valore >= prezziR2[i][0]){
						prezzoUnitario = prezziR2[i][2]
					}
				} else {
					if ((valore >= prezziR2[i][0]) && (valore <= prezziR2[i][1])){
						prezzoUnitario = prezziR2[i][2]
					}
				}
				document.getElementById('BU'+form).disabled = false
			}
		}
		
		if (form == 'R3'){
			for(i=1;i<=fasce;i++){
				if (prezziR3[i][1] == 0){
					if (valore >= prezziR3[i][0]){
						prezzoUnitario = prezziR3[i][2]
					}
				} else {
					if ((valore >= prezziR3[i][0]) && (valore <= prezziR3[i][1])){
						prezzoUnitario = prezziR3[i][2]
					}
				}
				document.getElementById('BU'+form).disabled = false
			}
		}
		
		if (form == 'R4'){
			for(i=1;i<=fasce;i++){
				if (prezziR4[i][1] == 0){
					if (valore >= prezziR4[i][0]){
						prezzoUnitario = prezziR4[i][2]
					}
				} else {
					if ((valore >= prezziR4[i][0]) && (valore <= prezziR4[i][1])){
						prezzoUnitario = prezziR4[i][2]
					}
				}
				document.getElementById('BU'+form).disabled = false
			}
		}
	}
	
	
	
	//impostazion prezzo unitario
	document.getElementById('PU'+form).value = eurizzatore(prezzoUnitario)
	
	//impostazione prezzototale senza iva
	prezzoTot = prezzoUnitario * valore
	document.getElementById('TO'+form).value = eurizzatore(prezzoTot)
	
	//impostazione prezzototale senza iva
	prezzoTotIva = prezzoUnitario * valore * IVA
	document.getElementById('TI'+form).value = eurizzatore(prezzoTotIva)
	
	//spese di spedizione
	if (form == 'A1' || form == 'R1'){
		if (valore <= 2){
			spedizione = SPED[0]
		} else {
			spedizione = SPED[1]
		}			
	} else {
		spedizione = SPED[0]
	}
	document.getElementById('SP'+form).value = eurizzatore(spedizione)
	
	//impostazione totale generale
	prezzoTotGen = (prezzoUnitario * valore * IVA) + spedizione
	document.getElementById('TG'+form).value = eurizzatore(prezzoTotGen)
 }
 
 function eurizzatore(valore){
	var valoreStringa
	var lunghezza = 0
	var intero
	var decimale
	valoreStringa = String(valore)
	lunghezza = valoreStringa.length
	intero = valoreStringa.substr(0,lunghezza-2)
	decimale = valoreStringa.substr(lunghezza-2,2)
	return intero + ',' + decimale
 }
 
// AZZERAMENTO E SETTAGGIO CAMPI E SESSIONI
function resetField(form){
	document.getElementById('QU'+form).value = ''
	document.getElementById('PU'+form).value = ''
	document.getElementById('TO'+form).value = ''
	document.getElementById('TI'+form).value = ''
	document.getElementById('SP'+form).value = ''
	document.getElementById('TG'+form).value = ''
	document.getElementById('BU'+form).disabled = true
 }

function resetSession(form){
	resetField(form)
	window.open('_reset_session.asp?form=' + form,'hidden_frame')
}

function resetModuleField() {
	document.order_form.cli_pagamento[0].checked = false
	document.order_form.cli_pagamento[1].checked = false
	document.order_form.cli_pagamento[2].checked = false
	document.order_form.cli_ragione_sociale.value = ''
	document.order_form.cli_cognome.value = ''
	document.order_form.cli_nome.value = ''
	document.order_form.cli_indirizzo.value = ''
	document.order_form.cli_cap.value = ''
	document.order_form.cli_citta.value = ''
	document.order_form.cli_provincia.value = ''
	document.order_form.cli_indirizzo2.value = ''
	document.order_form.cli_cap2.value = ''
	document.order_form.cli_citta2.value = ''
	document.order_form.cli_provincia2.value = ''	
	document.order_form.cli_codice_fiscale.value = 
	document.order_form.cli_partita_iva.value = ''
	document.order_form.cli_email.value = ''
	document.order_form.cli_email2.value = ''
	document.order_form.cli_telefono.value = ''
	document.order_form.cli_fax.value = ''
	document.order_form.cli_privacy[0].checked = false
	document.order_form.cli_privacy[1].checked = true
	document.order_form.cli_privacy2[0].checked = false
	document.order_form.cli_privacy2[1].checked = true
}

function resetModule(){
	resetModuleField()
	window.open('_reset_modulo.asp','hidden_frame')
}

function writeSession(form){
	var QU = document.getElementById('QU'+form).value
	var PU = document.getElementById('PU'+form).value
	var TO = document.getElementById('TO'+form).value
	var TI = document.getElementById('TI'+form).value
	var SP = document.getElementById('SP'+form).value
	var TG = document.getElementById('TG'+form).value
	window.open('_write_session.asp?form=' + form + '&QU=' + QU + '&PU=' + PU + '&TO=' + TO + '&TI=' + TI + '&SP=' + SP + '&TG=' + TG,'hidden_frame')
}

function resetAll(){
	var formName = new Array('A','R')
	for(j=0;j<=1;j++){
		for(i=1;i<=4;i++){
			resetSession(formName[j]+i)
			}
		}
	resetModule()
	}

function loadOrderData(){
	window.open('_order_data.asp','order_data')	
}

function loadOrder(){
	window.open('_riepilogo.asp','order_summary')	
}

function sendOrder(){
	window.open('_invio.asp','order_send')
}

// VISUALIZZAZIONE LAYER

function closeHelpMod(){
	document.getElementById("DIV_HELP_MODULE").style.display = "none"
	}

function openHelpMod(){
	document.getElementById("DIV_HELP_MODULE").style.display = "block"
	}

function listRinAcqOff(){
	for (i=1;i<=4;i++){
		document.getElementById("DIV_LISTINO_A" + i).style.display = "none"
		document.getElementById("DIV_LISTINO_R" + i).style.display = "none"
		}
	}

function homeOn(){
	document.getElementById("DIV_PRESENTAZIONE").style.display = "block"
	document.getElementById("DIV_SS_RIV").style.display = "block"
	document.getElementById("DIV_SS").style.display = "block"
	document.getElementById("DIV_INFOS").style.display = "block"
	}

function homeOff(){
	document.getElementById("DIV_PRESENTAZIONE").style.display = "none"
	document.getElementById("DIV_SS_RIV").style.display = "none"
	document.getElementById("DIV_SS").style.display = "none"
	document.getElementById("DIV_INFOS").style.display = "none"
}

function help(){
	document.getElementById("DIV_HELP").style.display = "none"
	document.getElementById("DIV_HELP_CLOSE").style.display = "block"
	document.getElementById("DIV_HELP_TEXT").style.display = "block"
}

function helpOff(){
	document.getElementById("DIV_HELP").style.display = "none"
	document.getElementById("DIV_HELP_CLOSE").style.display = "none"
	document.getElementById("DIV_HELP_TEXT").style.display = "none"
}

function helpClose(){
	document.getElementById("DIV_HELP").style.display = "block"
	document.getElementById("DIV_HELP_CLOSE").style.display = "none"
	document.getElementById("DIV_HELP_TEXT").style.display = "none"
}

function rinAcqOff(type){
	document.getElementById("DIV_RINNOVO").style.display = "none"
	document.getElementById("DIV_ACQUISTO").style.display = "none"
}

function listOn(form,type){
	document.getElementById("DIV_LISTINO_" + form + type).style.display = "block"
	}

function acqOn(){
	document.getElementById("DIV_ACQUISTO").style.display = "block"
	}

function rinOn(){
	document.getElementById("DIV_RINNOVO").style.display = "block"
	}

function modOn(){
	document.getElementById("DIV_MODULO").style.display = "block"
}

function modOff(){
	document.getElementById("DIV_MODULO").style.display = "none"
}

function ordOn(){
	document.getElementById("DIV_ORDINE").style.display = "block"
}

function ordOff(){
	document.getElementById("DIV_ORDINE").style.display = "none"
}

function sndOn(){
	document.getElementById("DIV_SEND").style.display = "block"
}

function sndOff(){
	document.getElementById("DIV_SEND").style.display = "none"
}

function hbOn(){
	document.getElementById("helpButton").style.display = "block"
}

function hbOff(){
	document.getElementById("helpButton").style.display = "none"
}

function acq(type){
	listOn('A',type)
	homeOff()
	hbOff()
	rinAcqOff()
	helpOff()
	modOff()
	ordOff()
	sndOff()
	resetAll()
}

function rin(type){
	listOn('R',type)
	homeOff()
	hbOff()
	rinAcqOff()
	helpOff()
	modOff()
	ordOff()
	sndOff()
	resetAll()
}

function homepage(){
	homeOn()
	hbOff()
	rinAcqOff()
	helpOff()
	listRinAcqOff()
	modOff()
	ordOff()
	sndOff()
	resetAll()
}

function acquisto(){
	rinAcqOff()
	acqOn()
	hbOff()
	homeOff()
	helpOff()
	listRinAcqOff()
	modOff()
	ordOff()
	sndOff()
	resetAll()
}

function rinnovo(){
	rinAcqOff()
	rinOn()
	hbOff()
	homeOff()
	helpClose()
	listRinAcqOff()
	modOff()
	ordOff()
	sndOff()
	resetAll()
}

function openModule(form){
	writeSession(form)
	setTimeout('loadOrderData()',1000)
	if (form == 'R2' || form == 'R3' || form == 'R4'){
		hbOn()
	}
	modOn()
	listRinAcqOff()
	homeOff()
	rinAcqOff()
	helpOff()
	sndOff()
	ordOff()
}

function backModule(){
	setTimeout('loadOrderData()',1000)
	modOn()
	listRinAcqOff()
	homeOff()
	rinAcqOff()
	helpOff()
	sndOff()
	ordOff()
}

function openSummary(form){
	setTimeout('loadOrder()',1000)
	if (form == 'R2' || form == 'R3' || form == 'R4'){
		hbOn()
	}
	modOff()
	listRinAcqOff()
	homeOff()
	rinAcqOff()
	helpOff()
	sndOff()
	ordOn()
}

function openSend(){
	setTimeout('sendOrder()',1000)
	sndOn()
	modOff()
	hbOff()
	listRinAcqOff()
	homeOff()
	rinAcqOff()
	helpOff()
	ordOff()
}

function deleteOrder(){
	resetAll()
	homepage()
}

function closeV4(){
	document.getElementById('DIV_V4').style.display = 'none'
	document.getElementById('DIV_V4_sh').style.display = 'none'
}