/**
$Id: formFunction.js,v 1.15 2007/01/30 15:16:06 ribolla Exp $
	File di script JS per la gestione delle azioni collegate a Form
	
	Author: Pietro Martinelli & Carlo Ray Rivadossi
	Version: $Id: formFunction.js,v 1.15 2007/01/30 15:16:06 ribolla Exp $
*/
/**
	Funzione JS che gestisce l'invio di un FORM modificando l'attributo ACTION del form stesso in base alla
	chiamata della funzione.
	
	@param Il valore dell'attributo ID del FORM da inviare
	@param actionURL L'URL da utilizzare come valore dell'attributo ACTION del FORM
	@param domanda Se tale parametro ï¿½impostato, chiede conferma presentando questa domanda all'utente
		prima di effettuare il submit. Se non ï¿½impostato, effettua il SUBMIT senza richiedere conferme.
	
	Esempio di utilizzo:
	<FORM ID="NomeDelForm" ACTION="">
		...
		<IMG SRC="immagine01.ico" onclick="submitGenericForm('NomeDelForm','azione01.jsp');"/>
		<IMG SRC="immagine02.ico" onclick="submitGenericForm('NomeDelForm','azione02.jsp','Sei sicuro ?');"/>
		...
	</FORM>
*/
function submitGenericForm(nomeFormulario, actionURL, domanda)
{
	var formulario = document.getElementById(nomeFormulario);
	formulario.action = actionURL;
	if (domanda == null || confirm(domanda))
	{
		formulario.submit();
	}
}


/**
	Funzione JS che richiede conferma per un'azione presentando una domanda all'utente.
	
	@param domanda La domanda da presentare all'utente
*/
function conferma(domanda)
{
	if (confirm(domanda))
	{
		return true;
	}
	else
	{
		return false;
	}
}
/**
	Funzione JS che controlla se i campi il cui nome sta in un dato elenco sono stati compilati.
	
	@param namesList L'elenco dei nomi dei campi obbligatori, separati da ";".
	@return TRUE se tutti i campi sono stati compilati, FALSE altrimenti.
*/
function checkFieldsNotEmpty(namesList)
{
	var names = namesList.split(';');
	for (i = 0; i < names.length; i++)
	{
		//alert('Nome = ' + names[i]);
		var tmpElement = document.getElementById(names[i]);
		//alert('Element = ' + tmpElement);
		//alert('Value = ' + tmpElement.value);
		if (tmpElement == null || tmpElement.value == '')
		{
			alert('Attenzione ! Il campo ' + names[i] + ' dev\'essere riempito !');
			return false;
		}
	}
	return true;
}


/**
	Funzione che controlla se e' stata effettuata una selezione su di un gruppo di radio - Button
	
	@param formElement L'elemento FORM contenente il gruppo di Radio - Button
	@param radioName Il NOME del gruppo di Radio - Button
	
	@return TRUE se uno dei Radio - Button e' selezionato, FALSE altrimenti
	
	Esempio di chiamata:
	<FORM ID="pippo" NAME="pippo" ACTION="paperino">
		<INPUT TYPE="RADIO" NAME="pluto" ID="pluto" VALUE="1">1</INPUT>
		<INPUT TYPE="RADIO" NAME="pluto" ID="pluto" VALUE="2">2</INPUT>
		<INPUT TYPE="RADIO" NAME="pluto" ID="pluto" VALUE="3">3</INPUT>
		<INPUT TYPE="RADIO" NAME="pluto" ID="pluto" VALUE="4">4</INPUT>
		<INPUT TYPE="BUTTON" VALUE="Clicca !" onclick="if(checkRadioButton(document.pippo,'pluto')){document.pippo.submit()}"/>
	</FORM>
*/
function checkRadioButton(formElement, radioName)
{
	var tmpElement = formElement.elements[radioName];
	var tot = tmpElement.length;
	//alert('length = ' + tot);
	if(tot != undefined)
	{
		for(i = 0; i < tot; i++)
		{
			//alert('[' + i + ']' + tmpElement[i].value);
			//alert('[' + i + ']' + tmpElement[i].checked);
			if(tmpElement[i].checked)
			{
				return true;
			}
		}
	}
	else if(tmpElement.checked)
	{
		return true;
	}
	
	alert('Attenzione !: Ã¨ obbligatorio selezionare un elemento del gruppo ' + radioName + ' di Radio - Button !');
	return false;
}
/**
	Funzione JS 
	che gestisce i bottoni di accettazione/ rifiuto di azioni specifiche (offerta, ordine...)
	@param nomeFormulario ï¿½il nome della form, 
	@ok  indica se ï¿½stato premuto il tasto accetta (e in tal caso viene passato 1) o rifiuta  (0)
	@nomeTextarea nome del capo textarea in cui vengon inserite le motivazioni 
	per la mancata accettazione
	OSS vengono ipostati i campi nascosti :
	- azione  :  campo usato per trattare l'azione relativa al cambio di stato
			e vale zero se ï¿½stato premutop il tasto accetta
	- xmlScelta :  indica se l'azione ï¿½stata Accettata o rifiutata.
			Tale campo viene mappato sul documento XML generato dalla azione.
*/

function setAction(nomeFormulario, ok, nomeTextarea)
{
	var formulario = document.getElementById(nomeFormulario);
	var txtArea=document.getElementById(nomeTextarea);
	if (ok==1)
	{
		formulario.azione.value=0;
		// alert(formulario.azione.value);
		formulario.xmlScelta.value="Accettata";
		txtArea.value='';
	}
	else 
	{
		formulario.xmlScelta.value="Rifiutata";
		formulario.azione.value=formulario.preAction.value;
	}
}
/**
	Metodo che imposta action e target per un dato form
*/
function setSubmitProperties(idForm, myAction, myTarget)
{
	var formulario = document.getElementById(idForm);
	formulario.action = myAction;
	formulario.target = myTarget;
	formulario.submit();
}

/**
	Funzione che effettua il submit di un form se e' stato premuto ENTER su un campo di input dato
	@param myfield Il campo di INPUT da monitorare
	@param L'evento occorso
	
	ESEMPIO:
	<INPUT TYPE="text" NAME="j_username" onKeyPress="return submitenter(this,event);" >
*/
function submitenter(myfield, e, valAction)
{
	var keycode;

	if (window.event)
	{
		keycode = window.event.keyCode;
	}
	else if (e)
	{
		keycode = e.which;
	}
	else
	{
		return true;
	}
	//alert("keycode = " + keycode);
	if (keycode == 13)
	{
		if (valAction != '')
		{
			
			myfield.form.action=valAction;
		}
		myfield.form.submit();
		return false;
	}
	else
	{
		return true;
	}
}

function checkIfEnter(myfield, e)
{
	var keycode;

	if (window.event)
	{
		keycode = window.event.keyCode;
	}
	else if (e)
	{
		keycode = e.which;
	}
	else
	{
		return false;
	}
	//alert("keycode = " + keycode);
	if (keycode == 13)
	{
		/*
		if (valAction != '')
		{
			
			myfield.form.action=valAction;
		}
		myfield.form.submit();
		*/
		return true;
	}
	else
	{
		return false;
	}
}


function isnum(obj)
{
	if (isNaN(obj.value) || parseInt(obj.value)<0 || parseInt(obj.value) > 9999999999)
	{
		alert('Nel campo è possibile immettere solo numeri!');
		obj.value="";
		obj.focus();
	}
}

function checkAll(elm,stringa)
{
  for (var i = 0; i < elm.form.elements.length-1; i++)
  {
	  //alert(elm.form.elements[i].name + ' - '  + name);
	  if (elm.form.elements[i].id == stringa)
	  	if (!elm.form.elements[i].checked)
	    	elm.form.elements[i].checked = elm.checked;
  }
}

function check_ricambi(elm,stringa,stringa_quantita)
{
  var checked="false";
  var quantita_ok="true";
  var msg = "";
  for (var i = 0; i < elm.form.elements.length-1; i++)
  {
	  //alert(elm.form.elements[i].id + ' - '  + stringa);
	  if (elm.form.elements[i].id == stringa)
	  {
		if (elm.form.elements[i].checked)
	  	{
		    checked="true";
		    //if (elm.form.elements[i+1].value <= 0 || elm.form.elements[i+1].value > elm.form.elements[i+2].value)
			//alert(elm.form.elements[i+3].value+ " - " + elm.form.elements[i+4].value);
			if (parseInt(elm.form.elements[i+3].value) > parseInt(elm.form.elements[i+4].value))
	  		{
				msg = "Attenzione! Le quantita\' selezionate devono essere minori di quelle presenti in magazzino";
				quantita_ok="false";
		    	break;
			}
			if(elm.form.elements[i+3].value <= 0)
			{
				msg = "Attenzione! Le quantita\' selezionate devono essere maggiori di 0";
				quantita_ok="false";
				break;
			}
		}
	  }
   }
   
   	if (checked=="true")
   	{
   		if(quantita_ok=="true")
   		{
		   return true;
		}
		else
		{
		 	alert(msg);
			return false;
		}
	}
	else
	{
		alert('Attenzione! Deve essere selezionato almeno un componente');
		return false;
	}
}

function check_passwords(namesList)
{
	var names = namesList.split(';');
	var pwd1 = document.getElementById(names[0]);
	var pwd2 = document.getElementById(names[1]);
	if(pwd1.value != pwd2.value)
	{
		alert('Le password inserite non corrispondono');
		return false;
	}
	else
		return true;
}

function invia_a_dpe(form, nuovaURL)
{
	window.print();
	form.submit();
	//self.opener.location.reload();
	self.opener.location= nuovaURL;
}

function chiusura_scheda(form_invio, form_popup)
{
	form_invio.submit();
	if (form_popup != undefined)
		form_popup.submit();
}

function controlla_check(elm,stringa)
{
  var checked="false";
  for (var i = 0; i < elm.form.elements.length-1; i++)
  {
	  //alert(elm.form.elements[i].name + ' - '  + stringa);
	  if (elm.form.elements[i].name == stringa)
	  {
		if (elm.form.elements[i].checked)
	  	{
		    checked="true";
		    break;
		}
	  }
   }
	if (checked=="true")
   	{
   	 	return true;
   	}
   	else
   	{
   		alert("Deve essere selezionato almeno un componente");
		return false;
   	}
   		
}

function disabilita_campi(namesList)
{
	var campi = namesList.split(';');
	if (document.getElementById('garanzia').value == 'si')
	{
		for(i=0; i<campi.length ; i++)
		{
			document.getElementById(campi[i]).disabled=false;
		}
	}
	else
	{
		for(i=0; i<campi.length ; i++)
		{
			document.getElementById(campi[i]).disabled=true;
			document.getElementById(campi[i]).value = '';
		}
	}
}

function check_data(data_form)
{
	var data = document.getElementById(data_form).value;
 	var giorno = data.substring(0,2);
 	var mese = data.substring(3,5);
 	var anno = data.substring(6,10);
	if (data.substring(2,3) != "/" ||
   		data.substring(5,6) != "/" ||
   		isNaN(giorno) ||
   		isNaN(mese) ||
   		isNaN(anno))
	{
      alert("Inserire data nel formato gg/mm/aaaa");
      data = "";
      document.data_form.focus();
      return false;
	}
	else if (giorno > 31) //Giorno
	{
   		alert("Errore inserimento data: valore del giorno non corretto");
   		document.modulo.nascita.select();
   		return false;
	}
	else if (mese > 12) //MEse
	{
   		alert("Errore inserimento data: valore del mese non corretto");
   		data = "";
   		document.modulo.nascita.focus();
   		return false;
	}
	else if (anno < 1900) //Anno
	{
   		alert("Errore inserimento data: valore dell'anno inferiore a 1900");
   		data = "";
   		document.getElementById(data_form).focus();
   		return false;
	}
	else
	{
		//alert (giorno + "-"+ mese);
		if((mese == 11 || mese == 04 || mese == 05 || mese == 09) && giorno > 30) //Mesi con 30 giorni
		{ 
		   alert("Errore inserimento data: valore del giorno non corretto");
		   return false;
		}
		else if(mese == 02 && giorno > 29)
		{
			alert("Errore inserimento data: valore del giorno non corretto");
   			return false;
		}
		else
		{
			var ora = new Date();
			var data_inserita=new Date(data.substring(6,10),data.substring(3,5)-1,data.substring(0,2))
			ora.getTime()
			var diff = ora-data_inserita
			diff = Math.round(diff/1000/60/60/24) //differenza in giorni
			if (diff > 365*2)
			{
				document.getElementById("garanzia").value = "no";
				alert("Data superiore a 24 mesi, prodotto fuori garanzia");
			}
			return true;
		}
	}
}

function check_si_no(campo_form)
{
	var campo = document.getElementById(campo_form).value;
	if(campo == "si")
	{
		return true;
	}
	else
	{
		alert("E' necessario accettare la privacy");
		return false;
	}
}

function check_campo_si_no(campo_form)
{
	var campo = document.getElementById(campo_form).value;
	if(campo == "si")
	{
		return true;
	}
	else
	{
		return false;
	}
}

function submit_multiplo(form1, form2)
{
	form1.submit();
	form2.submit();
}

function chiudi_invia_a_dpe()
{
	self.close();
	self.opener.location.reload();
}

function check_campo_settato(campo_form, id_scheda)
{
	var campo = document.getElementById(campo_form).value;
	if(campo != "")
	{
		return true;
	}
	else
	{
		alert("Il campo " +campo_form+ " risulta vuoto, è necessario compilarlo prima di chiudere la scheda");
		location.replace("gestione_magazzino.php?id=14&scheda=" + id_scheda + "");
		return false;
	}
}
