// SEZIONE jQuery

$(document).ready(function() {
	$('#conto_richiesta').keyup(function() {
		$('#ajax_deposit').load('http://www.operazionenergia.com/gestionale/content/calcola.php?conto=' + $(this).val(),function() {
			
			var valori_associati = new Array();
			valori_associati = $('#ajax_deposit').html().split("*-*");
			
			$('#bonusid_richiesta').val(valori_associati[0]);
			$('#cdg_richiesta').val(valori_associati[1]);
			
			copiabonus();
		});
	});

	$('#conto_fam01_richiesta').keyup(function() {
		$('#ajax_deposit1').load('http://www.operazionenergia.com/gestionale/content/calcola.php?conto=' + $(this).val(),function() {
			
			var valori_associati = new Array();
			valori_associati = $('#ajax_deposit1').html().split("*-*");
			
			$('#conto_fam01_bonus_richiesta').val(valori_associati[0]);
			$('#cdg_fam01_bonus_richiesta').val(valori_associati[1]);
			
			copiabonus();
		});
	});

	$('#conto_fam02_richiesta').keyup(function() {
		$('#ajax_deposit2').load('http://www.operazionenergia.com/gestionale/content/calcola.php?conto=' + $(this).val(),function() {

			var valori_associati = new Array();
			valori_associati = $('#ajax_deposit2').html().split("*-*");
			
			$('#conto_fam02_bonus_richiesta').val(valori_associati[0]);
			$('#cdg_fam02_bonus_richiesta').val(valori_associati[1]);
			
			copiabonus();
		});
	});

	$('#conto_fam03_richiesta').keyup(function() {
		$('#ajax_deposit3').load('http://www.operazionenergia.com/gestionale/content/calcola.php?conto=' + $(this).val(),function() {
			var valori_associati = new Array();
			valori_associati = $('#ajax_deposit3').html().split("*-*");
			
			$('#conto_fam03_bonus_richiesta').val(valori_associati[0]);
			$('#cdg_fam03_bonus_richiesta').val(valori_associati[1]);
			
			copiabonus();
		});
	});

	$('#conto_fam04_richiesta').keyup(function() {
		$('#ajax_deposit4').load('http://www.operazionenergia.com/gestionale/content/calcola.php?conto=' + $(this).val(),function() {
			var valori_associati = new Array();
			valori_associati = $('#ajax_deposit4').html().split("*-*");
			
			$('#conto_fam04_bonus_richiesta').val(valori_associati[0]);
			$('#cdg_fam04_bonus_richiesta').val(valori_associati[1]);
			
			copiabonus();
		});
	});

});

// CONTROLLI  FORM  inserisci_richiesta.php








function filtrarichieste() {
document.location.href='gestisci_richieste.php?id=' + document.getElementById('filtrarichieste').value;
}


function erase() {
	document.getElementById('action').value = 'delete';
}

function logout() {
		document.getElementById('formlogout').submit();
}

function toggledettagli(layer) {

	bid = 'b' + layer;	
	cid = 'c' + layer;

	if (document.getElementById(bid).className == 'visibile') {
		document.getElementById(bid).className = 'nascosta';
		document.getElementById(cid).className = 'nascosta';
	} else {
		document.getElementById(bid).className = 'visibile';
		document.getElementById(cid).className = 'visibile';
		}
}

function modificabanca() {
}

function cancellabanca() {
}

function modificaoperatore() {
}

function checkinserimentooperatore() {


if (document.sceltapremio.conto_richiesta.value == document.sceltapremio.conto_fam01_richiesta.value || 
	document.sceltapremio.conto_richiesta.value == document.sceltapremio.conto_fam02_richiesta.value ||
	document.sceltapremio.conto_richiesta.value == document.sceltapremio.conto_fam03_richiesta.value ||
	document.sceltapremio.conto_richiesta.value == document.sceltapremio.conto_fam04_richiesta.value)
{alert("Il campo NUMERO CONTO non può essere utilizzato anche nei BONUS FAMILIARI!\nControllare i Numeri di Conto dei Bonus Familiari."); document.sceltapremio.conto_richiesta.focus();return false;};


if (document.sceltapremio.nome_richiesta.value == "")
{alert("Il campo NOME è obbligatorio!"); document.sceltapremio.nome_richiesta.focus(); return false;};

if (document.sceltapremio.cognome_richiesta.value == "")
{alert("Il campo COGNONOME è obbligatorio!"); document.sceltapremio.cognome_richiesta.focus();return false;};

if (document.sceltapremio.conto_richiesta.value == "")
{alert("Il campo NUMERO CONTO è obbligatorio!"); document.sceltapremio.conto_richiesta.focus();return false;};

if (document.sceltapremio.bonus_richiesta.value == "")
{alert("Il campo BONUS TOTALI deve essere calcolato!"); document.sceltapremio.bonus_richiesta.focus();return false;};

if (isNaN(document.sceltapremio.bonus_richiesta.value))
{alert("Selezionare un CONTO corrente diverso!"); document.sceltapremio.bonus_richiesta.focus();return false;};

if (document.sceltapremio.bonusresidui_richiesta.value < 0)
{alert("Il campo BONUS RESIDUI non può essere negativo! Deseleziona il premio!"); document.sceltapremio.bonusresidui_richiesta.focus();return false;};

if (
(!document.sceltapremio.premio1.checked) && 
(!document.sceltapremio.premio2.checked) && 
(!document.sceltapremio.premio3.checked) && 
(!document.sceltapremio.premio4.checked) && 
(!document.sceltapremio.premio5.checked) && 
(!document.sceltapremio.premio6.checked) && 
(!document.sceltapremio.premio7.checked) && 
(!document.sceltapremio.premio8.checked) && 
(!document.sceltapremio.premio9.checked) && 
(!document.sceltapremio.premio10.checked) && 
(!document.sceltapremio.premio11.checked) && 
(!document.sceltapremio.premio12.checked) && 
(!document.sceltapremio.premio13.checked) && 
(!document.sceltapremio.premio14.checked) && 
(!document.sceltapremio.premio15.checked) && 
(!document.sceltapremio.premio16.checked) && 
(!document.sceltapremio.premio17.checked) && 
(!document.sceltapremio.premio18.checked) && 
(!document.sceltapremio.premio19.checked) && 
(!document.sceltapremio.premio20.checked) && 
(!document.sceltapremio.premio21.checked) && 
(!document.sceltapremio.premio22.checked) && 
(!document.sceltapremio.premio23.checked) && 
(!document.sceltapremio.premio24.checked) && 
(!document.sceltapremio.premio25.checked) && 
(!document.sceltapremio.premio26.checked) && 
(!document.sceltapremio.premio27.checked) && 
(!document.sceltapremio.premio28.checked) && 
(!document.sceltapremio.premio29.checked) && 
(!document.sceltapremio.premio30.checked) && 
(!document.sceltapremio.premio31.checked) && 
(!document.sceltapremio.premio32.checked) && 
(!document.sceltapremio.premio33.checked) && 
(!document.sceltapremio.premio34.checked) && 
(!document.sceltapremio.premio35.checked) && 
(!document.sceltapremio.premio36.checked) && 
(!document.sceltapremio.premio37.checked) && 
(!document.sceltapremio.premio38.checked) && 
(!document.sceltapremio.premio39.checked) && 
(!document.sceltapremio.premio40.checked) && 
(!document.sceltapremio.premio41.checked) && 
(!document.sceltapremio.premio42.checked) && 
(!document.sceltapremio.premio43.checked) && 
(!document.sceltapremio.premio44.checked) && 
(!document.sceltapremio.premio45.checked) && 
(!document.sceltapremio.premio46.checked) && 
(!document.sceltapremio.premio47.checked) && 
(!document.sceltapremio.premio48.checked) && 
(!document.sceltapremio.premio49.checked) && 
(!document.sceltapremio.premio50.checked) && 
(!document.sceltapremio.premio51.checked) && 
(!document.sceltapremio.premio52.checked) && 
(!document.sceltapremio.premio53.checked) && 
(!document.sceltapremio.premio54.checked) && 
(!document.sceltapremio.premio55.checked) && 
(!document.sceltapremio.premio56.checked) && 
(!document.sceltapremio.premio57.checked) && 
(!document.sceltapremio.premio58.checked) && 
(!document.sceltapremio.premio59.checked) && 
(!document.sceltapremio.premio60.checked) && 
(!document.sceltapremio.premio61.checked) && 
(!document.sceltapremio.premio62.checked) && 
(!document.sceltapremio.premio63.checked) && 
(!document.sceltapremio.premio64.checked) && 
(!document.sceltapremio.premio65.checked) && 
(!document.sceltapremio.premio66.checked) && 
(!document.sceltapremio.premio67.checked) && 
(!document.sceltapremio.premio68.checked) && 
(!document.sceltapremio.premio69.checked) && 
(!document.sceltapremio.premio70.checked) && 
(!document.sceltapremio.premio71.checked) && 
(!document.sceltapremio.premio72.checked) && 
(!document.sceltapremio.premio73.checked) && 
(!document.sceltapremio.premio74.checked) && 
(!document.sceltapremio.premio75.checked) && 
(!document.sceltapremio.premio76.checked) && 
(!document.sceltapremio.premio77.checked) && 
(!document.sceltapremio.premio78.checked) && 
(!document.sceltapremio.premio79.checked) && 
(!document.sceltapremio.premio80.checked) && 
(!document.sceltapremio.premio81.checked) && 
(!document.sceltapremio.premio82.checked) && 
(!document.sceltapremio.premio83.checked) && 
(!document.sceltapremio.premio84.checked) && 
(!document.sceltapremio.premio85.checked) && 
(!document.sceltapremio.premio86.checked) && 
(!document.sceltapremio.premio87.checked))
{alert("E' necessario selezionare almeno un prodotto!");document.sceltapremio.premio1.focus();return false;}




return true;


}

function cancellaoperatore(richiesta) {

if (confirm("Vuoi davvero cancellare il cliente " + richiesta + "?")) {return true;} else {return false;}

}

function checkinserimentobanche() {
}


String.prototype.trim = function() {
return this.replace(/^\s*/,"").replace(/\s*$/,"");
}

function copiabonus() {


if (document.getElementById('conto_fam01_bonus_richiesta').value == "") {document.getElementById('conto_fam01_bonus_richiesta').value = 0;}
if (document.getElementById('conto_fam02_bonus_richiesta').value == "") {document.getElementById('conto_fam02_bonus_richiesta').value = 0;}
if (document.getElementById('conto_fam03_bonus_richiesta').value == "") {document.getElementById('conto_fam03_bonus_richiesta').value = 0;}
if (document.getElementById('conto_fam04_bonus_richiesta').value == "") {document.getElementById('conto_fam04_bonus_richiesta').value = 0;}


	document.getElementById('bonus_richiesta').value = eval(document.getElementById('bonusid_richiesta').value.trim()) + eval(document.getElementById('conto_fam01_bonus_richiesta').value.trim()) + eval(document.getElementById('conto_fam02_bonus_richiesta').value.trim()) + eval(document.getElementById('conto_fam03_bonus_richiesta').value.trim()) + eval(document.getElementById('conto_fam04_bonus_richiesta').value.trim());
	document.getElementById('bonusresidui_richiesta').value = document.getElementById('bonus_richiesta').value;
	calcolabonus();
}

function calcolabonus() {

var parziale = 0;
var parzialepremi = "";

	for (var i=1; i<88; i++) {
	
		if (document.getElementById('premio' + i).checked == true) {
			document.getElementById('quantita_richiesta' + i).disabled = false;
			if (document.getElementById('quantita_richiesta' + i).value == "") {
				document.getElementById('quantita_richiesta' + i).value = 1;
			}
			parziale += eval(document.getElementById('premio' + i).value) * eval(document.getElementById('quantita_richiesta' + i).value);
			parzialepremi += document.getElementById('quantita_richiesta' + i).value;
			if (i < 87) {parzialepremi += ",";}
		} else {
			document.getElementById('quantita_richiesta' + i).disabled = true;
			if (document.getElementById('quantita_richiesta' + i).value != "") {
				document.getElementById('quantita_richiesta' + i).value = "";
			}
			parzialepremi += "0";
			if (i < 87) {parzialepremi += ",";}
		}
		

	document.getElementById('bonusresidui_richiesta').value = document.getElementById('bonus_richiesta').value - parziale;
	document.getElementById('scelta_richiesta').value = parzialepremi;

	if (document.getElementById('bonusresidui_richiesta').value<"0"){
	alert("Premio scelto superiore al valore dei bonus!");
	
	/* PROBLEMA
	document.getElementById('quantita_richiesta' + i).disabled = true;
	document.getElementById('quantita_richiesta' + i).value = "";

	document.getElementById('bonusresidui_richiesta').value = document.getElementById('bonus_richiesta').value;
	document.getElementById('premio' + i).checked = false

	*/
	return;
	
	}

	}

}

function bonusfam (){

		if (document.getElementById('checkbonusfam_richiesta').checked == true)	{
		
		document.getElementById('datifamview').style.display='block';
		
		} else {
		
		document.getElementById('datifamview').style.display='none';
		document.getElementById('conto_fam01_richiesta').value ='';
		document.getElementById('conto_fam01_bonus_richiesta').value ='';
		document.getElementById('cdg_fam01_bonus_richiesta').value ='';
		
		document.getElementById('conto_fam02_richiesta').value ='';
		document.getElementById('conto_fam02_bonus_richiesta').value ='';
		document.getElementById('cdg_fam02_bonus_richiesta').value ='';
		
		document.getElementById('conto_fam03_richiesta').value ='';
		document.getElementById('conto_fam03_bonus_richiesta').value ='';
		document.getElementById('cdg_fam03_bonus_richiesta').value ='';
		
		document.getElementById('conto_fam04_richiesta').value ='';
		document.getElementById('conto_fam04_bonus_richiesta').value ='';
		document.getElementById('cdg_fam04_bonus_richiesta').value ='';
		

		copiabonus();
		
		}

}


function isnum(obj) {
if (isNaN(obj.value) || parseInt(obj.value)<0 || parseInt(obj.value) > 999999999999)
{
alert('Nel campo è possibile immettere solo numeri!');
obj.value="";
obj.focus();
}
}
