/*
*	Funzioni di supporto per la gestione
*	dell'integrazione con il server EldasoftWSDM.
*
*/

var tempo = 400;
var _tipoWSERP;

/*
 * Funzione di attesa
 */
function _wait() {
	document.getElementById('bloccaScreen').style.visibility='visible';
	$('#bloccaScreen').css("width",$(document).width());
	$('#bloccaScreen').css("height",$(document).height());
	document.getElementById('wait').style.visibility='visible';
	$("#wait").offset({ top: $(window).height() / 2, left: ($(window).width() / 2) - 200});
}


/*
 * Nasconde l'immagine di attesa
 */
function _nowait() {
	document.getElementById('bloccaScreen').style.visibility='hidden';
	document.getElementById('wait').style.visibility='hidden';
}

/*
 * Lettura del sistema remoto di protocollazione
 */
function _getWSTipoSistemaRemoto() {
	var servizio = $("#servizio").val();
	
	$.ajax({
		type: "POST",
		dataType: "json",
		async: false,
		beforeSend: function(x) {
			if(x && x.overrideMimeType) {
				x.overrideMimeType("application/json;charset=UTF-8");
			}
		},
		url: "pg/GetWSERPRemote.do",
		data: "servizio=" + servizio,
		success: function(data){
			if (data) {
				$.map( data, function( item ) {
					$("#tiposistemaremoto").val(item[0]);
				});
        	}
		},
		error: function(e){
			alert("Errore durante la lettura della tipologia di sistema remoto");
		}
	});
}



function _getTipoWSERP(){
	var servizio = $("#servizio").val();
	
	$.ajax({
		type: "POST",
		dataType: "json",
		async: false,
		beforeSend: function(x) {
			if(x && x.overrideMimeType) {
				x.overrideMimeType("application/json;charset=UTF-8");
			}
		},
		url: "pg/GetWSERPRemote.do",
		data: "servizio=" + servizio,
		success: function(data){
			if (data) {
				$.map( data, function( item ) {
					_tipoWSERP = item[0];
				});
        	}
		}
	});
}

/*
 * Lettura dell'utente e degli attributi per la connessione al servizio remoto.
 */
function _getWSERP_Login() {
	var syscon = $("#syscon").val();
	var servizio = "WSERP";
	
	/*
	 * Lettura delle informazioni di login memorizzati nella 
	 * tabella WSLogin.
	 */
	$.ajax({
		type: "POST",
		dataType: "json",
		async: false,
		beforeSend: function(x) {
			if(x && x.overrideMimeType) {
				x.overrideMimeType("application/json;charset=UTF-8");
			}
		},
		url: "pg/GetWSLogin.do",
		data: "syscon=" + syscon + "&servizio=" + servizio,
		success: function(data){
			if (data) {
				$.map( data, function( item ) {
					$("#ga_username").html(item[0]);
					$("#ga_username").val(item[0]);
					$("#ga_password").html("................");
					$("#ga_password").val("................");
				});
        	}
		},
		error: function(e){
			alert("Errore durante la lettura dell'utente e dei suoi attributi");
		}
	});
	
}

function _getWSERP_L190_Login() {
	var syscon = $("#syscon").val();
	var servizio = "WSERP_L190";
	
	/*
	 * Lettura delle informazioni di login memorizzati nella 
	 * tabella WSLogin.
	 */
	$.ajax({
		type: "POST",
		dataType: "json",
		async: false,
		beforeSend: function(x) {
			if(x && x.overrideMimeType) {
				x.overrideMimeType("application/json;charset=UTF-8");
			}
		},
		url: "pg/GetWSLogin.do",
		data: "syscon=" + syscon + "&servizio=" + servizio,
		success: function(data){
			if (data) {
				$.map( data, function( item ) {
					$("#L190_username").html(item[0]);
					$("#L190_username").val(item[0]);
					$("#L190_password").html("................");
					$("#L190_password").val("................");
				});
        	}
		},
		error: function(e){
			alert("Errore durante la lettura dell'utente e dei suoi attributi");
		}
	});
	
}

/*
 * Salva l'utente e gli attributi attributi per la connessione ai servizi remoti
 */
function _setWSERP_Login() {
	var syscon = $("#syscon").val();
	var servizio = "WSERP";
	var username = $("#ga_username").val();
	var password = $("#ga_password").val();
	
	$.ajax({
		type: "GET",
		async: false,
		url: "pg/SetWSLogin.do",
		data: "syscon=" + syscon + "&servizio=" + servizio + "&username=" + username + "&password=" + password
	});
}

function _setWSERP_L190_Login() {
	var syscon = $("#syscon").val();
	var servizio = "WSERP_L190";
	var username = $("#L190_username").val();
	var password = $("#L190_password").val();
	
	$.ajax({
		type: "GET",
		async: false,
		url: "pg/SetWSLogin.do",
		data: "syscon=" + syscon + "&servizio=" + servizio + "&username=" + username + "&password=" + password
	});
}

function _testURL(url, tns) {
	var _URLvalido = false;
	$.ajax({
		type: "POST",
		dataType: "json",
		async: false,
		timeout: 3000,
		beforeSend: function(x) {
			if(x && x.overrideMimeType) {
				x.overrideMimeType("application/json;charset=UTF-8");
			}
		},
		url: "pg/GetURL.do",
		data: "url=" + url + "?wsdl&tns=" + tns, 
		success: function(data){
			if (data == true) {
				_URLvalido = true;
			} 
		}
	});
	return _URLvalido;
}



function _verificaPresenzaRda(numeroRda) {
	var _pres = false;
	$.ajax({
		type: "POST",
		dataType: "json",
		async: false,
		timeout: 3000,
		beforeSend: function(x) {
			if(x && x.overrideMimeType) {
				x.overrideMimeType("application/json;charset=UTF-8");
			}
		},
		url: "pg/GetWSERPPresenzaRda.do",
		data: "numeroRda=" + numeroRda, 
		success: function(data){
			if (data == true) {
				_pres = true;
			} 
		}
	});
	return _pres;
}


/*
 * Lettura dei lotti disponibili 
 * perche' senza carrello collegato (SmeUp) 
 */
function _popolaLotti(codiceGara,id) {
	
	$.ajax({
		type: "POST",
		dataType: "json",
		async: false,
		beforeSend: function(x) {
			if(x && x.overrideMimeType) {
				x.overrideMimeType("application/json;charset=UTF-8");
			}
		},
		url: "pg/GetWSERPLottiSenzaCarrello.do",
		data: "codiceGara=" + codiceGara,
		success: function(data){
			if (data) {
				$("#" + id).html("");
				$("#" + id).append($("<option/>", {value: "" ,text: "" }));
				$.map( data, function( item ) {
					$("#" + id).append($("<option/>", {value: item[0], text: item[1] }));
				});
				
				//Se risulta presente un solo valore nel tabellato allora lo si seleziona
				if($("#" + id + " option").length == 2 ){
					$("#" + id + " option").eq(1).prop('selected', true);
				}
			}
		},
		error: function(e){
			alert("Errore durante la lettura dei lotti disponibili per la gara " + codiceGara);
		}
	});
}

/*
 * Lettura del tabellato identificato da "nome".
 * Richiede che nel DOM sia gia' presente un oggetto
 * di tipo "select" con "id" e "name" uguale a "id" 
 */
function _popolaWSERPTabellato(codice,id) {
	
	$.ajax({
		type: "POST",
		dataType: "json",
		async: false,
		beforeSend: function(x) {
			if(x && x.overrideMimeType) {
				x.overrideMimeType("application/json;charset=UTF-8");
			}
		},
		url: "pg/GetWSERPTabellato.do",
		data: "codice=" + codice,
		success: function(data){
			if (data) {
				$("#" + id).append($("<option/>", {value: "" ,text: "" }));
				$.map( data, function( item ) {
					$("#" + id).append($("<option/>", {value: item[0], text: item[1] }));
				});
				
				//Se è presente un solo valore nel tabellato allora lo si seleziona
				if($("#" + id + " option").length == 2 ){
					$("#" + id + " option").eq(1).prop('selected', true);
				}
			}
		},
		error: function(e){
			alert("Errore durante la lettura del tabellato " + codice);
		}
	});
}

function _popolaWSERPListaCondPag(id) {
	
	$.ajax({
		type: "POST",
		dataType: "json",
		async: false,
		beforeSend: function(x) {
			if(x && x.overrideMimeType) {
				x.overrideMimeType("application/json;charset=UTF-8");
			}
		},
		url: "pg/GetWSERPCondizioniPagamento.do",
		success: function(data){
			if (data) {
				$("#" + id).append($("<option/>", {value: "" ,text: "" }));
				$.map( data, function( item ) {
					$("#" + id).append($("<option/>", {value: item[0], text: item[1] }));
				});
				
				//Se è presente un solo valore nel tabellato allora lo si seleziona
				if($("#" + id + " option").length == 2 ){
					$("#" + id + " option").eq(1).prop('selected', true);
				}
			}
		},
		error: function(e){
			alert("Errore durante la lettura delle condizioni di pagamento");
		}
	});
}

/*
 * Lettura del dato fisso da "nome".
 */
function _popolaWSERPDatoFisso(codice,id) {
	$.ajax({
		type: "POST",
		dataType: "json",
		async: false,
		beforeSend: function(x) {
			if(x && x.overrideMimeType) {
				x.overrideMimeType("application/json;charset=UTF-8");
			}
		},
		url: "pg/GetWSERPTabellato.do",
		data: "codice=" + codice,
		success: function(data){
			if (data) {
				$.map( data, function( item ) {
					$("#"+id).val(item[0]);
				});
        	}
		},
		error: function(e){
			alert("Errore durante la lettura del dato fisso " + codice);
		}
	});
}

/*
 * Legge i dati di una rda.
 */
function _getWSERPRda(numeroRda,esercizio) {

	_wait();
	
	$.ajax({
		type: "POST",
		dataType: "json",
		async: true,
		beforeSend: function(x) {
			if(x && x.overrideMimeType) {
				x.overrideMimeType("application/json;charset=UTF-8");
			}
		},
		url: "pg/GetWSERPListaRda.do",
		"data" : { 
				codicerda: numeroRda,
				divisione: esercizio
		},
		success: function(json){
			if (json.esito == true) {
				_popolaTabellaDatiPersonalizzati(json.iTotalRecordsDATIPERSONALIZZATI, json.dataDATIPERSONALIZZATI);
				
			} else {
				var messaggio = "Errore durante la lettura delle rda";
				$('#rdamessaggio').text(messaggio);
				$('#rdamessaggio').show(300);
			}
		},
		error: function(e){
			var messaggio = "Errore durante la lettura delle rda";
			$('#rdamessaggio').text(messaggio);
			$('#rdamessaggio').show(300);
		},
		complete: function() {
			_nowait();
        }
		
		
		
	});
}