var caratteri = new Array();
caratteri[0] = "0123456789";
caratteri[1] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
caratteri[2] = "0123456789 \-";

var rulesGen = new Array();
rulesGen[0] = "piva|keypress|" + caratteri[0];
rulesGen[1] = "telefono|keypress|" + caratteri[2];
rulesGen[2] = "cap|keypress|" + caratteri[0];


function loginStaff(frmName) {
	var rules = new Array();
	rules[0] = "user_name|required|- Inserire lo username";
	rules[1] = "user_pwd|required|- Inserire la password";
	
	if (!performCheck(frmName, rules, 'classic')){			
		return false;
	}
	return true;	
}

function loginClienteFE(frmName) {

	var rules = new Array();
	rules[0] = "user_name|required|- Inserire lo username";
	rules[1] = "user_pwd|required|- Inserire la password";
	
	if (performCheck(frmName, rules, 'classic')){
	
		$('frmLogin').submit();
	}
	
}

function openSection(opt) {
	var actionVal = "";
	switch (opt) {
	
		case 1://Invio password
		
			actionVal = '/BE/utenti/manageAttesa.php';
		    break;
		case 2:
			actionVal = '/BE/utenti/manageUtenti.php';
			break;    
		case 3:
			actionVal = "/BE/clienti/mgrClienti.php";
		    break;
		case 4:
			actionVal = "/BE/clienti/mgrMaterialeAgenzie.php";
		    break;    
		case 5:
			actionVal = "/BE/clienti/mgrMaterialeCliAg.php";
		    break;  
		case 6:
			actionVal = "/BE/viaggi/mgrViaggi.php?new";
		    break;     
		case 7:
			actionVal = "/BE/racconti/mgrRacconti.php?new";
		    break;
		case 8:
			actionVal = "/BE/convenzioni/mgrConvenzioni.php?new";
		    break;                  
		
	}
	$('frmMenu').action = actionVal;
	$('frmMenu').submit();

}

function mgrConvenzioni(optName, opt, frmName) {

	switch(opt) {
		case 4:
			var msg = "Attenzione! Inserire i seguenti dati obbligatori: \r\n";
			
			var error = false;
			
			if ($F('convenzione') == "") {
				msg += "- Titolo della convenzione\r\n";
				error = true;
			}
			if ($F('codice') == "") {
				msg += "- Codice della convenzione (per la generazione di questo codice, utilizzare il bottone 'Genera Codice')\r\n";
				error = true;
			}
			if (error) {
				alert(msg);
				return;
			}
			break;
	
	    case '6':
	    	if (!confirm("Attenzione! L'operazione richiesta cancellera' in modo definitivo la convenzione e tutti i file allegati. Si intende procedere?")){
				return;
			}	
	        break;
		
	}
	$(optName).value = opt;
	$(frmName).submit();
}

function manageConvenzioni(opt, idFile) {
	switch(opt) {
	    
		case 3:
			if (!confirm("Attenzione! L'operazione richiesta cancellera' in modo definitivo il file selezionato. Si intende procedere?")){
				return;
			}	
			break;
	}
	$('idFileDett').value = idFile;
	$('opt').value = opt;
	$('frmConvenzione').submit();
}

function manageStaff(frmName, opt, idUtente) {

	switch(opt) {

		case 2:
			var rules = new Array();
   			rules[0]='nome|required|Inserire il nome';
			rules[1]='cognome|required|Inserire il cognome';
			rules[2]='userName|required|Inserire lo username';
			rules[3]='userPwd|required|Inserire la password';
			rules[4]='email|required|Inserire un indirizzo e-mail valido';
			
			if (!performCheck(frmName, rules, 'classic')){			
				return;
			}
			
			rules = new Array();
			rules[0]='userPwd|minlength|6|La password deve essere di almeno 8 caratteri.';
			
			if (!performCheck(frmName, rules, 'classic')){			
				return;
			}
			$('optDett').value = opt;
			break;
		
		case 3:
			if (!confirm("Attenzione! L'operazione richiesta cancellera' definitivamente l'utente Staff zainetto. Si indente procedere?")) {
				return;
			}
			$('optList'+idUtente).value = opt;
			break;
		
	}


	$(frmName).submit();
}

function checkParamsClienti() {
	if (($F('cognome') == null || $F('cognome') == "") && 
		($F('localita') == null || $F('localita') == "") &&
		($F('mailing_s') == null || $F('mailing_s') == "") && 
		($F('mailing_n') == null || $F('mailing_n') == "") &&
		($F('ragSoc') == null || $F('ragSoc') == "") && 
		($F('flTipoUtente') == null || $F('flTipoUtente') == "") ) {
		
		alert("Attenzione! E' necessario specificare almeno un parametro di ricerca");
		return false;
	}
	
	return true;
}

function manageMaterialeAg(optName, optVal, frmName) {

	var error = false;
	var message = "Alcuni campi non sono stati riempiti correttamete.\r\n\r\n";

	switch(optVal) {
	
		case 1:
			//salvataggio (add/update(di un materiale
			if ($F('nome') == "") {
				message += "Inserire il nome del file.\r\n";
				error = true;
			}
			if ($F('fileAgOld') == "" && $F('fileAg') == "") {
				message += "Scegliere il file da caricare.\r\n";
				error = true;
			}
			
			if (error) {
				alert(message);
				return;
			}
			break;
			
		case 3:
			//delete	
			if (!confirm("Attenzione! L'operazione richiesta cancellera' il file selezionato. Si intende procedere?")) {
				return;
			}
			
		    break;
	}
	$(optName).value = optVal;
	$(frmName).submit();

}

function manageDivRadio(valSel) {
	if (valSel == "M") {
		$('radio1').style.display = "none";
		$('radio2').style.display = "none";
	} else {
		$('radio1').style.display = "inline";
		$('radio2').style.display = "inline";
		
	}
}

function manageAgenzie(idUtente, opt) {

	switch(opt) {
	
		case 0:
			if (!confirm("Attenzione! L'operazione richiesta attivera' l'agenzia assegnando "+
					"uno username e una password per accedere al servizio. I dati verranno inviati "+
					"tramite posta elettronica all'indirizzo indicato. "+
					"Si intende procedere?")) {
				return;
			}
			break;
		case 1: //ATTIVAZIONE AGENZIA
			if (!confirm("Attenzione! L'operazione richiesta cancellera' definitivamente dalla "+
						"banca dati la scheda dell'"+
						" agenzia selezionata. Si intende procedere?")) {
				return;
			}
			
			$('idUtente').value = idUtente;
			break;
		
	}

	$('opt').value = opt;
	$('frmAgenzia').submit();
	
}

function manageClienti(idUtente, opt) {
	if (opt == 1) {
	var win = window.open("/BE/clienti/mgrClienti.php?opt=" + opt + "&idUtente=" + idUtente, "popupWin", "width=700, height=700, toolbar=0"); 
	win.focus();
	} else {
		if (confirm("Attenzione! L'operazione richiesta cancellera' definitivamente dalla banca dati il cliente selezionato. Si intende procedere?")) {
			$('optDett').value = '2';
			$('idCliente').value = idUtente;
			$('frmClienti').submit();
		} else {
			return;
		}
	}
}

function manageUtenti(idUtente, opt) {

	if (opt == 1) {
		if (!confirm("Attenzione! L'operazione richiesta cancellera' definitivamente dalla banca dati il cliente selezionato. Si intende procedere?")) {
			return;
		}
		$('agenziaReqPwd').value = "";
	} else {
		$('agenziaReqPwd').value = "YES";
	}
	$('opt').value = opt;

	$('idUtente').value = idUtente;
	$('frmAgenzia').submit();
}

function loadLayerZone() {
	loadSelectValueZona('fkZona');
	ShowLayer();
}

function mgrFotoViaggio(opt, idFoto) {
	switch(opt) {
	
		case 11:
			
			var small = ($('fileSmall')) ? $F('fileSmall') : "";
			var big = ($('fileBig')) ? $F('fileBig') : "";
			
			var mess = "Attenzione. Alcuni dati non sono validi.\n\n";
			var e = false;
			
			if(small == "") {
				mess += "- Scegliere la foto piccola da associare\n";
				e = true;
			}
			if(big == "") {
				mess += "- Scegliere la foto grande da associare\n";
				e = true;
			}
			
			if(e) {
				mess += "\nCorreggere gli errori e riprovare";
				alert(mess);
				
				return;
			} 							
		   	
			if ($F('totFoto') >= 5) {
				alert("Attenzione! Si e' raggiunto il numero massimo di foto da associare ad un viaggio.");
				return;
			}			
			break;
		case 12:
			$('idFotoDett').value = idFoto;
			break;
		case 13:
			if (!confirm("Attenzione! L'operazione richiesta cancellera' in modo definitivo le foto del viaggio selezionate. Si intende procedere?")){
				return;
			}	
			break;
	}
	$('optVgg').value = opt;
	$('mgrViaggio').submit();
}

function manageViaggio(opt) {
	
	switch(opt) {
		case 0:
			var rules = new Array();	
		   	rules[0]="fkZona|required|- Scegliere la zona. Se non e' nella lista inserirla selezinando la voce '[Add+]'";
		   	rules[1]="titolo|required|- Inserire il titolo del viaggio";
		   	rules[2]="sottotitolo|required|- Inserire il sottotitolo del viaggio";
		   	rules[3]="descrizione|required|- Inserire la descrizione del viaggio";
		   	
		   	if (!performCheck('mgrViaggio', rules, 'classic')){			
				return;
			}
			
			if ($('flEvidenza').checked && (($F('immagineOld') == null || $F('immagineOld') == "") &&
				($F('immagine') == null || $F('immagine') == ""))) {
				alert("Attenzione! Per i viaggi in evidenza sul Front-End e' necessario associare un'immagine.");
				return;
			}
			
			if ($F('flEnoGastCk') == "S" || $F('flGruppoCk') == "S") {
				if ($F('dtStart') == "" || $F('dtStop') == "") {
					alert("Attenzione! Selezionare le date di inizio e fine del viaggio.");
					return;
				}
				var dtStart = new Date();
				var dtStartStr = $F('dtStart');
				var ddStart = dtStartStr.substring(0, 2);
				var mmStart = dtStartStr.substring(3, 5);
				var yyStart = dtStartStr.substring(6);
				dtStart.setFullYear(yyStart, mmStart-1, ddStart);
				
				
				var dtStop = new Date();
				var dtStopStr = $F('dtStop');
				var ddStop = dtStopStr.substring(0, 2);
				var mmStop = dtStopStr.substring(3, 5);
				var yyStop = dtStopStr.substring(6);
				dtStop.setFullYear(yyStop, mmStop-1, ddStop);
				
				
				if (dtStop < dtStart){		
					alert("Attenzione! La data di fine del viaggio non puo' essere antecedente alla data di inizio.");
					return;			
				}
			}
			
			break;
	
				
	}
	
	
	$('optVgg').value = opt;
	$('mgrViaggio').submit();
}

function manageRacconti(opt) {
	
	switch(opt) {
		case 0:
			var rules = new Array();	
		   	rules[0]="fkZona|required|- Scegliere la zona. Se non e' nella lista inserirla selezinando la voce '[Add+]'";
		   	rules[1]="titolo|required|- Inserire il titolo del viaggio";
		   	rules[2]="sottotitolo|required|- Inserire il sottotitolo del racconto";
		   	rules[3]="descrizione|required|- Inserire la descrizione del racconto";
		   	
		   	if (!performCheck('mgrViaggio', rules, 'classic')){			
				return;
			}
			
			break;
	
				
	}
	
	
	$('optVgg').value = opt;
	$('mgrViaggio').submit();
}

function openPopupSubmit(opt) {
	window.open("","popupWin","width=850, toolbar=0, scrollbars = yes, resizable=yes"); 
    var a = window.setTimeout("document.mgrViaggioRiepilogo.submit();", 500); 
}

function mailingNewsletter(typeOpt) {

	if (typeOpt == "all") {
		$('optDett').value = "7";
	} else if (typeOpt == "checked") {
		var strCheck = "";
		for (var i = 0; i < document.frmLista.sendEmailId.length; i++) {
			if (document.frmLista.sendEmailId[i].checked) {
				strCheck += document.frmLista.sendEmailId[i].value + "|";
			}
		}
		if (strCheck == "") {
			alert("Attenzione! Selezionare almeno un destinatario.");
			return;
		} 		
		$('optDett').value = "8";
		$('strCheck').value = strCheck;
	} else if (typeOpt == "send") {
	
		if (!confirm("Attenzione! L'operazione inviera' a tutti i destinatari del target la newsletter. Si intende procedere?")) {
			return;  
		}
		$('optDett').value = "9";
	} else if (typeOpt == "changeTarget") {
		if (!confirm("Attenzione! L'operazione cancella il target clienti. Pertanto sara' necessario eseguire una nuova ricerca clienti. Si intende procedere?")) {
			return;
		}
		openSection(3);
		return;
	}
	$('frmClienti').submit();
}


function addProgramma() {
	var rules = new Array();	
   	rules[0]='titolo|required|- Inserire il titolo del programma';
   	rules[1]='sottotitolo|required|- Inserire il sottotitolo del programma';
   	rules[2]='descrizione|required|- Inserire la descrizione del programma';
   	
   	if (!performCheck('mgrViaggio', rules, 'classic')){			
		return;
	}
	
	$('mgrViaggio').submit();
}

function lstViaggio(idViaggio, opt) {

	if (opt == 2) {
		if (!confirm("Attenzione! L'operazione richiesta cancellera' il viaggio selezionato. Si intende procedere?")) {
			return;
		}
	}

	$('idViaggioSearch').value = idViaggio;
	$('optLst').value = opt;
	$('frmDettViaggio').submit();
}

function loadLayerQuote(ordineQuota, giorni, notti, prezzo) {
	$('idQuota').value = "";
	$('giorni').value = "";
	$('notti').value = "";
	$('prezzo').value = "";
	if (ordineQuota != undefined) {
		$('idQuota').value = ordineQuota;
		$('deleteBtn').disabled = false;
	}
	if (giorni != undefined) {
		$('giorni').value = giorni;
	}
	if (notti != undefined) {	
		$('notti').value = notti;
	}
	if (notti != undefined) {	
		$('prezzo').value = prezzo;
	}
	ShowLayer();
}

function managePrgViaggio(frmName, opt, idProgramma) {
	
	switch(opt) {

		case 5:
			if (!confirm("Attenzione! L'operazione richiesta cancellera' definitivamente il singolo programma. Si indente procedere?")) {
				return;
			}
			
			break;
		case 6:
			$('idPrgViaggio').value = "";
			$('nrOrdine').value = "";	
			$('titolo').value = "";
			$('sottotitolo').value = "";
			$('descrizione').value = "";
			$('buttonAdd').value = "Aggiungi";
			$('idTotali').style.display='inline';
			
			return;
		
	}

	$('optList'+idProgramma).value = opt;
	$(frmName).submit();
}

function saveZona(frmName, opt) {
	var error = false;
	if (opt == 0) {
		var msgError = "Attenzione!\r\n";		
		if (!$('zonaTipo_E').checked && !$('zonaTipo_I').checked) {
			msgError += "- Scegliere 'Italia' o 'Estero'\r\n";
			error = true;
		};
		if ($('zonaDescr').value == "" || $('zonaDescr').value == null) {
			msgError += "- Inserire il nome della zona";
			error = true;
		};
		
		if (error) {
			alert(msgError);
			return;
		}	   
	} else if (opt == 1) {
			
		$('delete').value = "yes";
	} else if (opt == 2) {
		$('zonaId').value = "";
		$('zonaDescr').value = "";
		$('zonaTipo').value = "";
		$('deleteBtn').disabled = true;
		return;
	}
	$(frmName).submit();
}

function saveQuota(frmName, opt) {
	switch(opt) {
		case 9://insert
			var rules = new Array();	
		   	rules[0]='giorni|required|- Inserire il numero di giorni a cui si riferisce la quota';
		   	rules[1]='notti|required|- Inserire il numero di notti a cui si riferisce la quota';
		   	rules[2]='prezzo|required|- Inserire la quota (inserire il seguente formato x.xxx,xx)';
		   	
		   	if (!performCheck(frmName, rules, 'classic')){			
				return;
			}
			break;
		
		case 10://delete
			if (!confirm("Attenzione! L'operazione richiesta cancellera' la quota selezionata. Si intende procedere?")) {
				return;
			}
			$('optQuota').value = 10;
			break;
			
		default://reset
			break;
	}
	
	$(frmName).submit();
}

function clearDate(flagData) {
	if ($F('flEnoGastCk') == "N") {
		$('dtStart').value = "";
		$('dtStop').value = "";
	} 
}

/*METODI PER GESTIRE LE zone*/
function loadSelectValueZona(from) {

	var valueSel;
	var valueIndex;
	var zonaId;
	var zonaTipoId;
	
	$('zonaId').value = "";
	$('zonaDescr').value = "";
	
	
	for (i = 0; i < $(from).options.length; i++) {
	
		if ($(from).options[i].selected) {
			
			valueSel = $(from).options[i].value;
			if (valueSel != "") {
				valueIndex = valueSel.indexOf("#");
				
				//sto trattando una sottozona
				zonaId = valueSel.substring(0, valueIndex);
				for (j = 0; j < $(from).options.length; j++) {
					
					if ($(from).options[j].value == valueSel) {
						$('zonaDescr').value = ltrim($(from).options[j].text);
						break;
					}
				}
				
				$('zonaId').value = zonaId;
				
				zonaTipoId = valueSel.substring(valueIndex + 1);
				
				if (zonaTipoId == "Italia") {
					$('zonaTipo_I').checked = true;
				} else if (zonaTipoId == "Estero") {
					$('zonaTipo_E').checked = true;
				}
						
				
				$('deleteBtn').disabled = false;	
			} else {
				$('deleteBtn').disabled = true;
			}
			break;
		}
	}
}


function inviaPreventivo() {

	var msg = "Attenzione! I seguenti campi sono obbligatori:\r\n";
	var errore = false;
	
	if ($F('nome')  == "") {
		msg += "- Nome\r\n";
		errore = true;
	}
	if ($F('cognome')  == "") {
		msg += "- Cognome\r\n";
		errore = true;
	}
	/*if ($F('indirizzo')  == "") {
		msg += "- Indirizzo\r\n";
		errore = true;
	}
	if ($F('localita')  == "") {
		msg += "- Citta'\r\n";
		errore = true;
	}
	if ($F('cap')  == "") {
		msg += "- CAP\r\n";
		errore = true;
	}
	if ($F('prov')  == "") {
		msg += "- Provincia\r\n";
		errore = true;
	}
	if ($F('telefono')  == "") {
		msg += "- Telefono o Cellulare\r\n";
		errore = true;
	}*/
	if ($F('email')  == "") {
		msg += "- E-mail\r\n";
		errore = true;
	}
	/*if ($F('destinazione')  == "") {
		msg += "- Destinazione preferita\r\n";
		errore = true;
	}
	if ($F('nrAdulti')  == "") {
		msg += "- Numero di Adulti\r\n";
		errore = true;
	}
	if ($F('nrBambini')  == "") {
		msg += "- Numero di Bambini\r\n";
		errore = true;
	}*/
	if (!$('consenso').checked) {
		msg += "- Dare ilconsenso al trattamento dei dati personali\r\n";
		errore = true;
	}

	if (errore) {
		alert(msg);
		return;
	} else {
		if (!checkEmail($F('email'))) {
			alert("Attenzione! L'indirizzo email non e' valido.");
			return;
		}
	
		if ($('flMailingCk').checked) {
			$('flMailing').value = "Y";
		} else {
			$('flMailing').value = "N";
		}
		$('frmPreventivo').submit();
	}
}

function inviaMailing() {

	var msg = "Attenzione! I seguenti campi sono obbligatori:\r\n";
	var errore = false;
	
	/*if ($F('nome')  == "") {
		msg += "- Nome\r\n";
		errore = true;
	}
	if ($F('cognome')  == "") {
		msg += "- Cognome\r\n";
		errore = true;
	}
	if ($F('indirizzo')  == "") {
		msg += "- Indirizzo\r\n";
		errore = true;
	}
	if ($F('localita')  == "") {
		msg += "- Citta'\r\n";
		errore = true;
	}
	if ($F('cap')  == "") {
		msg += "- CAP\r\n";
		errore = true;
	}
	if ($F('prov')  == "") {
		msg += "- Provincia\r\n";
		errore = true;
	}
	if ($F('nazione')  == "") {
		msg += "- Nazione\r\n";
		errore = true;
	}
	if ($F('telefono')  == "") {
		msg += "- Telefono o Cellulare\r\n";
		errore = true;
	}*/
	if ($F('email')  == "") {
		msg += "- E-mail\r\n";
		errore = true;
	}
	
	if (!$('consenso').checked) {
		msg += "- Dare il consenso al trattamento dei dati personali\r\n";
		errore = true;
	}

	if (errore) {
		alert(msg);
		return;
	} else {
		
		if (!checkEmail($F('email'))) {
			alert("Attenzione! L'indirizzo email non e' valido.");
			return;
		}
		$('frmPreventivo').submit();
	}
}

function inviaAgenzia() {

	var msg = "Attenzione! I seguenti campi sono obbligatori:\r\n";
	var errore = false;
	
	if ($F('nome')  == "") {
		msg += "- Nome\r\n";
		errore = true;
	}
	
	if ($F('cognome')  == "") {
		msg += "- Cognome\r\n";
		errore = true;
	}
	
	if ($F('ragSoc')  == "") {
		msg += "- Ragione Sociale\r\n";
		errore = true;
	}
	
	if ($F('piva')  == "") {
		msg += "- Partita Iva\r\n";
		errore = true;
	}
	if ($F('indirizzo')  == "") {
		msg += "- Indirizzo\r\n";
		errore = true;
	}

	if ($F('citta')  == "") {
		msg += "- Citta'\r\n";
		errore = true;
	}
	if ($F('cap')  == "") {
		msg += "- CAP\r\n";
		errore = true;
	}
	if ($F('prov')  == "") {
		msg += "- Provincia\r\n";
		errore = true;
	}
	if ($F('telefono')  == "") {
		msg += "- Telefono o Cellulare\r\n";
		errore = true;
	}
	
	if ($F('email')  == "") {
		msg += "- E-mail\r\n";
		errore = true;
	}
	
	if (!$('consenso').checked) {
		msg += "- Dare il consenso al trattamento dei dati personali\r\n";
		errore = true;
	}

	if (errore) {
		alert(msg);
		return;
	} else {
	
		if (!checkEmail($F('email'))) {
			alert("Attenzione! L'indirizzo email non e' valido.");
			return;
		}
		
		if ($('flMailingCk').checked) {
			$('flMailing').value = "S";
		} else {
			$('flMailing').value = "N";
		}
		$('frmAg').submit();
	}
}



function ShowLayer() {
	var d= $('ModalBGConfirm');
	var p= $('DialogWindowConfirm');
  	
  	if (!(d)) { 
   		ShowError();
   		return;
  	}    	
         	
    offset = Position.realOffset(p);
    pos = p.getDimensions();    
   
    if (pos.height > document.body.clientHeight){
    	altez = offset[1] + 20;
    }else{
    	altez = offset[1] + (document.body.clientHeight - pos.height) / 2;
    }
    if (pos.width > document.body.clientWidth){
    	larg = offset[0] + 20;
    }else{
    	larg = offset[0] + (document.body.clientWidth - pos.width) / 2;
    }
  	
    p.style.left = larg + "px";
    p.style.top = altez + "px";
    
    new Draggable('DialogWindowConfirm',{ scroll:window,
                                    	  handle: 'dw_header',
                                    	  starteffect:function(){},
                                    	  endeffect:function(){}
                                  		});
                                  		
    d.style.display='inline';
    p.style.display='inline';    
}

function CloseDialogBoxConfirm() {
	
	var mbgc = $('ModalBGConfirm');
	var dwc = $('DialogWindowConfirm');
	if (mbgc) {
		mbgc.style.display='none';
		dwc.style.display='none';
	} else 
		reloadSession();
}


function formatNumber(num) {

var isNeg=0;

    if(num < 0) {
       num=num*-1;
       isNeg=1;
    }

    
	onum=Math.round(num*100)/100;
		
	integer=Math.floor(onum);

	if (Math.ceil(onum) == integer) {
		decimal="00";
	} else{
		decimal=Math.round((onum-integer)*100)
	}
	decimal=decimal.toString();
	if (decimal.length<2) decimal="0"+decimal;

	integer=integer.toString();
	var tmpnum="";
	var tmpinteger="";
	var y=0;

	for (x=integer.length;x>0;x--) {
		tmpnum=tmpnum+integer.charAt(x-1);
		y=y+1;
		if (y==3 & x>1) {
			tmpnum=tmpnum+",";
			y=0;
		}
	}

	for (x=tmpnum.length;x>0;x--) {
		tmpinteger=tmpinteger+tmpnum.charAt(x-1);
	}
		
	finNum=tmpinteger+"."+decimal;

    if(isNeg == 1) {
       finNum = "-" + finNum;
    }

	return finNum;
}

function ltrim(stringToTrim) {
	var charCode = stringToTrim.substring(0,1);
	
	while (stringToTrim.charCodeAt(0) == 160) {
		stringToTrim = stringToTrim.substring(1, stringToTrim.length);
	}
	return stringToTrim;
}

function selectComboVal(tbox, value) {

	for (i = 0; i < tbox.options.length; i++) {
		if (tbox.options[i].value == value) {
			tbox.options[i].selected = true;
			break;
		}
	}
}

function checkFlag(fieldCheck, filedApp, eleObj) {

	if(fieldCheck == "flEnoGast") {
		$("flGruppo").checked = false;
		$("flGruppoCk").value = "";
	} else if(fieldCheck == "flGruppo") {
		$("flEnoGast").checked = false;
		$("flEnoGastCk").value = "";
	}
	
	var read = true;
	var display = "inline";
	
	if ($(fieldCheck).checked) {
		$(filedApp).value = "S";
		display	= "inline";
	} else {
		$(filedApp).value = "N";
		display	= "none";
	}
	
	for (var i = 0; i < eleObj.length; i++) {
		$(eleObj[i]).style.display = display;
	}
}

function checkEmail(email){

	var filter = /^[a-zA-Z0-9,!#\$%&'\*\+/=\?\^_`\{\|}~-]+(\.[a-zA-Z0-9,!#\$%&'\*\+/=\?\^_`\{\|}~-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.([a-zA-Z]{2,})$/i	
	
	if (!filter.test(email)){		
			return false;									
	}else{
		return true;
	}
}



var oldLink = null;
function setActiveStyleSheet(link, title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
  if (oldLink) oldLink.style.fontWeight = 'normal';
  oldLink = link;
  link.style.fontWeight = 'bold';
  return false;
}

// This function gets called when the end-user clicks on some date.
function selected(cal, date) {
  cal.sel.value = date; // just update the date in the input field.
  if (cal.dateClicked && (cal.sel.id == "sel1" || cal.sel.id == "sel3"))
    cal.callCloseHandler();
}

function closeHandler(cal) {
  cal.hide();                        // hide the calendar
//  cal.destroy();
  _dynarch_popupCalendar = null;
}

function showCalendar(id, format, showsTime, showsOtherMonths) {
  var el = document.getElementById(id);
  alert("1");
  if (_dynarch_popupCalendar != null) {
    // we already have some calendar created
    _dynarch_popupCalendar.hide();                 // so we hide it first.
  } else {
    // first-time call, create the calendar.
    var cal = new Calendar(1, null, selected, closeHandler);
    // uncomment the following line to hide the week numbers
    // cal.weekNumbers = false;
    if (typeof showsTime == "string") {
      cal.showsTime = true;
      cal.time24 = (showsTime == "24");
    }
    if (showsOtherMonths) {
      cal.showsOtherMonths = true;
    }
    _dynarch_popupCalendar = cal;                  // remember it in the global var
    cal.setRange(1900, 2070);        // min/max year allowed.
    cal.create();
  }
  alert("2");
  alert("2A: " + format);
  alert("2B: " + el.nextSibling);
  
  _dynarch_popupCalendar.setDateFormat(format);    // set the specified date format
  _dynarch_popupCalendar.parseDate(el.value);      // try to parse the text in field
  _dynarch_popupCalendar.sel = el;                 // inform it what input field we use
alert("PRIMA 3");
  _dynarch_popupCalendar.showAtElement(el.nextSibling, "Br");        // show the calendar
alert("3");
  return false;
}

var MINUTE = 60 * 1000;
var HOUR = 60 * MINUTE;
var DAY = 24 * HOUR;
var WEEK = 7 * DAY;

function isDisabled(date) {
  var today = new Date();
  return (Math.abs(date.getTime() - today.getTime()) / DAY) > 10;
}

function flatSelected(cal, date) {
  var el = document.getElementById("preview");
  el.innerHTML = date;
}

function showFlatCalendar() {
  var parent = document.getElementById("display");

  var cal = new Calendar(0, null, flatSelected);

  cal.weekNumbers = false;

  cal.setDisabledHandler(isDisabled);
  cal.setDateFormat("%A, %B %e");

  cal.create(parent);

  cal.show();
}
