
gJs.removeSessioneUtente_link = function(idU,idS){
	$('#sessUt_'+idU+'_'+idS).remove();
}

gJs.hideGrafici = function(){
	$('.graphStatistiche').hide();
	$('.graphNascondi').hide();
	$('.graphVisualizza').show();
}

gJs.showGrafici = function(){
	$('.graphStatistiche').show();
	$('.graphVisualizza').hide();
	$('.graphNascondi').show(); 
}

gJs.removeSessioneUtente = function(idU,idS,fDone,wantToRemoveLink){
	idU = idU || -1;
	idS = idS || -1;
	fDone = fDone || function(){ };
	wantToRemoveLink = wantToRemoveLink || false;
	var fD = function(){
		gJs.askServer('removeSessioneUtente','idU='+idU+'&idS='+idS);
		fDone();
		if (wantToRemoveLink) gJs.removeSessioneUtente_link(idU,idS);
	}
	msgBox('Sei sicuro di voler disiscrivere dalla sessione questo utente ?','Conferma disiscrizione','yesNo',fD);
}

gJs.removeCredit_link = function(idR){
	$('#creditLine_'+idR).remove();
}

gJs.removeCredit = function(idR,fDone,wantToRemoveLink){
	idR = idR || -1;
	fDone = fDone || function(){ };
	wantToRemoveLink = wantToRemoveLink || false;
	var fD = function(){
		gJs.askServer('removeCredit','idR='+idR);
		fDone();
		if (wantToRemoveLink) gJs.removeCredit_link(idR);
	}
	msgBox('Sei sicuro di voler rimuovere il credito assegnato ?','Conferma richiamo del credito','yesNo',fD);
}

gJs.removeBatteriaSessione_link = function(idB,idS){
	$('#sessBatt_'+idB+'_'+idS).remove();
}

gJs.removeBatteriaSessione = function(idB,idS,fDone,wantToRemoveLink){
	idB = idB || -1;
	idS = idS || -1;
	fDone = fDone || function(){ };
	wantToRemoveLink = wantToRemoveLink || false;
	var fD = function(){
		gJs.askServer('removeBatteriaSessione','idB='+idB+'&idS='+idS);
		fDone();
		if (wantToRemoveLink) gJs.removeBatteriaSessione_link(idB,idS);
	}
	msgBox('Sei sicuro di voler rimuovere questa batteria come fonte di domande per la sessione ?','Conferma rimozione associazione','yesNo',fD);
}

gJs.removeGruppoUtente_link = function(idU, idG){
	$('#gruppoUt_'+idG+'_'+idU).remove();
}

gJs.removeGruppoUtente = function(idU,idG,fDone,wantToRemoveLink){
	idU = idU || -1;
	idG = idG || -1;
	fDone = fDone || function(){ };
	wantToRemoveLink = wantToRemoveLink || false;
	var fD = function(){
		gJs.askServer('removeGruppoUtente','idU='+idU+'&idG='+idG);
		fDone();
		if (wantToRemoveLink) gJs.removeGruppoUtente_link(idU,idG);
	};
	msgBox('Sei sicuro di voler eliminare l\'assegnazione di questo utente con il gruppo ?','Conferma rimozione associazione','yesNo',fD);
};

gJs.removePianoDidattico = function(idU,idG,idP,fDone,selectorToRemove){
	idU = idU || 0;
	idG = idG || 0;
	idP = idP || 0;
	fDone = fDone || function(){ };
	selectorToRemove = selectorToRemove || '';
	var fD = function(){
		gJs.askServer('removePianoDidattico','idU='+idU+'&idG='+idG+'&idP='+idP);
		fDone();
		$(selectorToRemove).remove();
	};
	msgBox('ATTENZIONE: i piani didattici relativi verranno eliminati. Si vuole procedere ?','Conferma rimozione','yesNo',fD);
};

gJs.removeConcorsoSessione_link = function(idC,idS){
	$('#sessConc_'+idC+'_'+idS).remove();
};

gJs.removeConcorsoSessione = function(idC,idS,fDone,wantToRemoveLink){
	idC = idC || -1;
	idS = idS || -1;
	fDone = fDone || function(){ };
	wantToRemoveLink = wantToRemoveLink || false;
	var fD = function(){
		gJs.askServer('removeConcorsoSessione','idC='+idC+'&idS='+idS);
		fDone();
		if (wantToRemoveLink) gJs.removeConcorsoSessione_link(idC,idS);
	};
	msgBox('Sei sicuro di voler eliminare l\'assegnazione di questo concorso con la sessione ?','Conferma rimozione associazione','yesNo',fD);
};

gJs.getFormStringValues = function(objForm,wich,dontEclude_ifNameEmpty){
	var data = ""; 
	wich = wich || '';
	dontEclude_ifNameEmpty = dontEclude_ifNameEmpty || false;
	if (objForm.substring){
		objForm = document.getElementById(objForm);
	}
	objForm = objForm || false;
	if (objForm && objForm.elements){
		for(i=0; i<objForm.elements.length; i++){
			if (!dontEclude_ifNameEmpty && (objForm.elements[i].name=='' || objForm.elements[i].name==undefined)){
				// excludo
			}else{
				if (!wich || (wich && wich==objForm.elements[i].name)){
					dataOb = gJs.getFormElementNameAndValue(objForm.elements[i]); // solo
																					// quello
																					// specificato
					if (data != "") data += "&";
					data += dataOb.name + "=" + dataOb.value;
				}
			}
		}
	}
	return data;
};

gJs.getFormJson = function(objForm,wich,dontEclude_ifNameEmpty){
	return JSON.stringify(gJs.getFormValues(objForm,wich,dontEclude_ifNameEmpty));
};

//restituisce un array di elementi array [nome, valore] di un oggetto form
//NB: impostare anche il nome, o passare TRUE come terzo parametro per non
//escludere i campi senza NAME impostato
gJs.getFormValues = function(objForm,wich,dontEclude_ifNameEmpty){
	var data = {};
	wich = wich || '';
	dontEclude_ifNameEmpty = dontEclude_ifNameEmpty || false;
	objForm = objForm || false;
	if (objForm){
		for(i=0; i<objForm.elements.length; i++){
			if (!dontEclude_ifNameEmpty && (objForm.elements[i].name=='')){
				// excludo
			}else{
				if (wich){
					if (wich==objForm.elements[i].name){
						data = gJs.getFormElementNameAndValue(objForm.elements[i]); // solo
																					// quello
																					// specificato
					}
				}else{
					data[i] = gJs.getFormElementNameAndValue(objForm.elements[i]); // accoda
				}
			}
		}
	}
	return data;
};

gJs.getFormElementNameAndValue = function(formElement){
 	var type = undefined;
	if(formElement.length != null) if(formElement[0] != null) type = formElement[0].type;
	if((typeof(type) == 'undefined') || (type == 0)) type = formElement.type;

	var data = new Object();
	data.value = null;
	data.name = formElement.name;
	switch(type)
	{
		case 'undefined':
			data.name = "";
			break;

		case 'radio':
			for(var x=0; x < formElement.length; x++) 
				if(formElement[x].checked == true)
					data.value = formElement[x].value;
			break;

		case 'select-multiple':
			var myArray = new Array();
			for(var x=0; x < formElement.length; x++) 
				if(formElement[x].selected == true)
					myArray[myArray.length] = formElement[x].value;
			data.value = myArray;
			break;
			

		case 'checkbox':
			data.value = formElement.checked;
			break;
	
		default:
			data.value = formElement.value;
			break;
	}
	return data;
};

gJs.getElementValue = function(formElement){
	if(formElement.length != null) var type = formElement[0].type;
	if((typeof(type) == 'undefined') || (type == 0)) var type = formElement.type;
	
	switch(type)
	{
		case 'undefined': return false;
	
		case 'radio':
			for(var x=0; x < formElement.length; x++) 
				if(formElement[x].checked == true)
			return formElement[x].value;
	
		case 'select-multiple':
			var myArray = new Array();
			for(var x=0; x < formElement.length; x++) 
				if(formElement[x].selected == true)
					myArray[myArray.length] = formElement[x].value;
			return myArray;
	
		case 'checkbox': return formElement.checked;
	
		default: return formElement.value;
	}
};

gJs.askServerFormAndMore = function(wG, obForm, wP, onDone, responseSelector, serverUrl, additionalData){
	var sDataForm = gJs.getFormStringValues(obForm);
	wP = ((wP!="")?wP+"&":"") + sDataForm + ((additionalData!="")?"&"+additionalData:"");
	
	return gJs.askServer(wG, wP, onDone, responseSelector, serverUrl);
};

gJs.askServer = function(wG, wP, onDone, responseSelector, serverUrl, selectorToDisable){
	wG = wG || "";
	wP = wP || "";
	serverUrl = serverUrl || "server.php";
	responseSelector = responseSelector || "#server";
	onDone = onDone || function(){ };
	selectorToDisable = selectorToDisable || "";
	
	if (selectorToDisable != "")
		$(selectorToDisable).attr('disabled', 'disabled');
	
	$.ajax({
	  	type: 'POST',
	  	url: serverUrl+'?what='+wG,
	  	data: wP,
	  	success: function(data){
			$(responseSelector).html(data);
			
			if (selectorToDisable != "")
				$(selectorToDisable).attr('disabled', '');
			
			onDone();
		}
	});
};

gJs.askEdit = function(wG, wP, onDone, responseSelector){
	return gJs.askServer(wG, wP, onDone, responseSelector, 'editjqgriddata.php');
};

gJs.jqGHtml_infinite = '&#8734;';
gJs.jqGHtml_infiniteBold = '<b>&#8734;</b>';
gJs.jqGHtml_infiniteTextual = 'illimitato';
gJs.jqGArrayParse = { '<b>∞</b>':'-1', '<b>&#8734;</b>':'-1' };
gJs.jqGArrayParseInsideInput = { '<b>∞</b>':'illimitato', '<b>&#8734;</b>':'illimitato' };

/*
 * 3.4
 * 
 * function jqGFormatterCredito(el, cellval, opts){
	if (cellval=='-1'){
		$(el).html('<b>&#8734;</b>');
	}else{
		$(el).html(cellval);
	}
}*/

// 3.5
function jqGShowInEdit(el){
	$(el).load(function(){
		$(this).show().css('display','inline-block');
	});
	$(el).show();
}

function jqGFormatterCredito(cellval, el, opts){
	var ret = cellval;
	if (cellval=='-1'){
		ret = gJs.jqGHtml_infiniteBold;
	}
	return ret;
}

function jqGFormatterCredito_setHtmlCredito(idC){
	$('#'+idC).val(gJs.jqGHtml_infiniteTextual);
}

function jqGFormatterCredito_edit(el){
	$(el).load(function(){
		var id = $(this).attr('id');
		var value = $(this).val();
		if (gJs.jqGArrayParseInsideInput[value]!=undefined){
			$(this).val(gJs.jqGArrayParseInsideInput[value]);
		}
		var sHtm = '<input type="button" value="' + gJs.jqGHtml_infinite + '" onclick="jqGFormatterCredito_setHtmlCredito(\'' + id + '\');" />';
		$(this).after(sHtm);
	});
}

function jqGFormatterValidita(cellval, el, opts){
	var ret = cellval;
	if (cellval=='1'){
		img = 'delete.png';
	} else {
		img = 'accept.png';
	}
	
	ret = '<img src=\'main/imgs/icons/' + img +  '\' />';
	
	return ret;
}


function jqGFormatterPeriodo(cellval, el, opts){
	var ret = cellval;
	if (cellval=='-1'){
		ret = 'una tantum';
	}
	return ret;
}

function jqGFormatterPeriodo_setHtmlPeriodo(idC){
	$('#'+idC).val('una tantum');
}

function jqGFormatterPeriodo_edit(el){
	$(el).load(function(){
		var id = $(this).attr('id');
		var value = $(this).val();
		if (gJs.jqGArrayParseInsideInput[value]!=undefined){
			$(this).val(gJs.jqGArrayParseInsideInput[value]);
		}
		var sHtm = '<input type="button" value="una tantum" onclick="jqGFormatterPeriodo_setHtmlPeriodo(\'' + id + '\');" />';
		$(this).after(sHtm);
	});
}


function jqGFormatterDatetime_setTime(idC){
	var obN = $('#'+idC).next('input');
	var val = $('#'+idC+'_hid').val();
	if (val!=''){
		$('#'+idC).val(val + ' ' + $(obN).val());
	}
}

function jqGFormatterDatetime_reset(idC){
	var obN = $('#'+idC).next('input');
	var val = $('#'+idC+'_hid').val();
	if (val!=''){
		$('#'+idC).val(val);
	}
}

function jqGFormatterDatetime_edit(el){
	$(el).load(function(){
		var id = $(this).attr('id');
		var value = $(this).val();
		var time = '00:00';
		
		if (value.length>11){
			time = value.substr(11,value.length);
			value = value.substr(0,11);
		}
		
		var sHtm = '<input type="text" size="10" value="'+time+'" onchange="jqGFormatterDatetime_setTime(\'' + id + '\');" onkeyup="jqGFormatterDatetime_setTime(\'' + id + '\');" />';
		var sHHtm = '<input type="hidden" value="" id="' + id + '_hid" />';
		$(this).after(sHtm + sHHtm);
		
		$('#'+id+'_hid').val(value);
		
		$(this).keydown(function(){
			jqGFormatterDatetime_reset(id);
		});
		$(this).change(function(){
			$('#'+id+'_hid').val($(this).val());
			if ($(this).val()!=''){
				//$(this).next('input').removeAttr('disabled');
				jqGFormatterDatetime_setTime(id);
			}else{
				//$(this).next('input').attr({disabled: 'disabled'});
			}
		});
	});
}

function jqGAfterEditCell(idList, rowid, cellname, value, iRow, iCol){
	idList = idList || '';
	if (idList!=''){
		if ($('#'+idList)[0]) if ($('#'+idList)[0].p && $(this)[0]){
			var colM = $('#'+idList)[0].p.colModel;
			value = $('#'+iRow+'_'+colM[iCol].name).val();
			if (gJs.jqGArrayParse[value]!=undefined){
				$('#'+iRow+'_'+colM[iCol].name).val(gJs.jqGArrayParse[value]);
			}
		}
	}else{
		return false;
	}
}

function jqGEditRow(idList, rowid, keys, oneditfunc, succesfunc, url, extraparam, aftersavefunc, onerrorfunc){
	idList = idList || '';
	if (idList!=''){
		$('#'+idList).editRow(selX, rowid, keys, oneditfunc, succesfunc, url, extraparam, aftersavefunc, onerrorfunc);
		
		if ($('#'+idList)[0]) if ($('#'+idList)[0].p && $(this)[0]){
			var value = '';
			var colM = $('#'+idList)[0].p.colModel;
			for(var ix=0;ix<colM.length;ix++){
				value = $('#'+rowid+'_'+colM[ix].name).val();
				if (gJs.jqGArrayParse[value]!=undefined){
					$('#'+rowid+'_'+colM[ix].name).val(gJs.jqGArrayParse[value]);
				}
			}
		}
	}else{
		return false;
	}
}

gJs.setCapofila = function (cid) {
	$.ajax({
			  	type: "POST",
			  	url: "server.php?what=setCapofila",
			  	data: "cid="+ cid,
			  	success: function(data){
					window.location.reload();
			  	}
			});
}

gJs.setTestcenter = function (cid) {
	$.ajax({
			  	type: "POST",
			  	url: "server.php?what=setCentrotest",
			  	data: "cid="+ cid,
			  	success: function(data){
					window.location.reload();
			  	}
			});
}

gJs.setUtente = function (cid) {
	$.ajax({
			  	type: "POST",
			  	url: "server.php?what=setUtente",
			  	data: "cid="+ cid,
			  	success: function(data){
					window.location.reload();
			  	}
			});
}

gJs.richiediCrediti = function (serverDiv) {
	$.ajax({
		  	type: "POST",
		  	url: "server.php?what=richiediCrediti",
		  	data: "div="+serverDiv,
		  	success: function(data){
				$('#server').html (data);
		  	}
		});
}

gJs.getSelectCapofila = function (divId) {
	$("#" + divId).load("server.php?what=getSelectCapofila&div="+divId);
}

gJs.getSelectTestcenter = function (divId) {
	$("#" + divId).load("server.php?what=getSelectCentrotest&div="+divId);
}

gJs.getSelectUtente = function (divId) {
	$("#" + divId).load("server.php?what=getSelectUtente&div="+divId);
}

gJs.removeSelectCapofila = function (divId) {
	$("#" + divId).load("server.php?what=removeSelectCapofila&div="+divId);
}

gJs.removeSelectCentrotest = function (divId) {
	$("#" + divId).load("server.php?what=removeSelectCentrotest&div="+divId);
}

gJs.removeSelectUtente = function (divId) {
	$("#" + divId).load("server.php?what=removeSelectUtente&div="+divId);
}

gJs.btnCambiaPassword = 
	function (pw1, pw2, operation, frmName, checkDiv) {
		frmName = frmName || 'frmNewPassword';
		operation = operation || 'passwordLost';
		checkDiv = checkDiv || 'server_check';
		if (pw1 == pw2) {
			var vars = $('#frmNewPassword').formSerialize();
			$.ajax({
			  	type: 		'POST',
			  	url: 		'server.php?what=' + operation,
			  	data: 		vars,
			  	success: 	function (data)	{ $('#' + checkDiv).html(data); },
		   		error: 		function () 	{ $('#' + checkDiv).html('<span class=\"msgError\">Errore del server, eseguire nuovamente la procedura.</span>'); }
			});
		}
		else {
			msgBox ("Le password inserite non coincidono verificarne l\'esattezza.");
		}
	}
