﻿function sendemail(encodedEmail)
{
  // do the mailto: link
  location.href = "mailto:" + decodeEmail(encodedEmail);
}

// return the decoded email address
function decodeEmail(encodedEmail)
{
  // holds the decoded email address
  var email = "";

  // go through and decode the email address
  for (i=0; i < encodedEmail.length;)
  {
    // holds each letter (2 digits)
    var letter = "";
    letter = encodedEmail.charAt(i) + encodedEmail.charAt(i+1)

    // build the real email address
    email += String.fromCharCode(parseInt(letter,16));
    i += 2;
  }
  
  return email;
}

function sendemail(encodedEmail)
{
  // do the mailto: link
  location.href = "mailto:" + decodeEmail(encodedEmail);
}

// return the decoded email address
function decodeEmail(encodedEmail)
{
  // holds the decoded email address
  var email = "";

  // go through and decode the email address
  for (i=0; i < encodedEmail.length;)
  {
    // holds each letter (2 digits)
    var letter = "";
    letter = encodedEmail.charAt(i) + encodedEmail.charAt(i+1)

    // build the real email address
    email += String.fromCharCode(parseInt(letter,16));
    i += 2;
  }
  
  return email;
}

function paging( sFormID, nPage )
{
    var oForm = document.getElementById(sFormID);
    var oPage = document.getElementById('PaginaCorrente');
    var oSubmitPaging = document.getElementById('SubmitPaging');
    if (oSubmitPaging) oSubmitPaging.value='1';
    if (oPage) oPage.value=nPage;
    if (oForm) oForm.submit();
}

function autoPostSelect( oSelect )
{
    if (oSelect)  {
        var oForm = oSelect.form;
        if (oForm) {
            var oHidden = document.getElementById(oSelect.id + '_autopost');
            if (oHidden) oHidden.value='1';
            oForm.submit();
                }
            }
}

function SelezionaMeseCalendario(id,value) {
    if (!document.getElementsByTagName) return;
    var tbf = document.getElementById(id);
    if (!tbf) return;
    for (var i=0; i<tbf.getElementsByTagName('span').length; i++) {
      var spa = tbf.getElementsByTagName('span')[i];
      if (spa.className=='mesecorrente')    {
          for (var k=0; k<spa.getElementsByTagName('input').length; k++) {
              var inp = spa.getElementsByTagName('input')[k];
              if (inp) inp.checked=value;
              }
       }   
    }
  }

function RedirectLoginSpWeb()
{
	window.open( 'ftp://' + document.getElementById('txtUsr').value + ':' + document.getElementById('txtPwd').value + '@ftp.scuolaer.it/' );
}


function submitForm( sFormName )
{
    var oForm = document.getElementById(sFormName);
	if (oForm) oForm.submit();
}

function submitFormNewWindow( sFormName, sWindowName, sHref )
{
    var oForm = document.getElementById(sFormName);
	if (oForm) {
		var oldTarget = oForm.target;
		var oldAction = oForm.action;
		oForm.target = sWindowName;
		if (sHref!='') oForm.action = sHref;
		oForm.submit();
		oForm.action = oldAction;
		oForm.target = oldTarget;
		}
}

function votaArticolo( shRef )
{
	jx.load(shRef, cbVotaArticolo);
}

function cbVotaArticolo( data )
{
	if(data.length > 0){
		var aData = data.split(',');
		var sSel = '';
		if (aData[1]=='-1') alert('E\' possibile votare solamente una volta al giorno.');
		var ulVoti = document.getElementById('votaarticoloul');
		if (ulVoti) ulVoti.className='sel' + data[0];
		for (i=1;i<=5;i++) {
			var liVoto = document.getElementById('votaarticolo' + i);
			if (liVoto) {
				sSel = '';
				if (data[0]==i) sSel = ' sel';
				liVoto.className='mediavoti' + i + sSel;
				}
			}
    }
}

function generateGuid()
{
	var result, i, j;
	result = '';
	for(j=0; j<32; j++)
			{
			i = Math.floor(Math.random()*16).toString(16).toUpperCase();
			result = result + i;
			}
	return result
}

function calScol_showModal( sPage ) { 
	jQuery('#returninfo').html('Sto salvando i dati. Attendere prego.')
	jQuery('#returninfo').dialog({
			modal: true,
			closeOnEscape: false,
			resizable: false,
			buttons: {
				Ok: function() {
					jQuery(this).dialog('destroy');
				}
			}
		});
	jQuery('.ui-dialog-buttonpane').hide();
	jQuery('.ui-dialog-titlebar-close').hide();
	}

function calScol_createUrl( sPage ) {
	return defaultPath + 'ext/CalendarioScolastico/' + sPage + '.' + jQuery('#txtCodMeccanografico').val() + '.aspx?' + generateGuid()
	}

function calScol_aggData() {
	jQuery("#datepickeraggdata").datepicker(
		{ 
			onSelect: function(dateText, inst) { 
					jQuery("#datepickeraggdata").datepicker( "destroy" )
					calScol_showModal();
					jQuery.getJSON( calScol_createUrl('salvadata'), { data: dateText }, calScol_setCampi );
					return false; 
						} 
		});
	return false;
}

function calScol_aggData_OLD() {
	calScol_showModal();
	var dData = jQuery('#txtAggiungiData').val()
	jQuery.getJSON( calScol_createUrl('salvadata'), { data: dData }, calScol_setCampi );
	return false;
}

function calScol_delData( dData ) {
	if (confirm('Sei sicuro di voler cancellare la data: ' + dData)) {
	    calScol_showModal();
		jQuery.getJSON( calScol_createUrl('eliminadata'), { data: dData }, calScol_setCampi );
		}
	return false;
}

function calScol_salvaOperatore() {
	calScol_showModal();
	jQuery.getJSON( calScol_createUrl('salvadatioperatore'), { nome: jQuery("#txtNomeCompilatore").val(), cognome: jQuery("#txtCognomeCompilatore").val(), datadelibera: jQuery("#txtDataDelibera").val(), numdelibera: jQuery("#txtNumDelibera").val() }, calScol_setCampi );
	return false;
}

function calScol_aggDataPatrono() {
	jQuery("#datepickerdatapatrono").datepicker(
		{ 
			onSelect: function(dateText, inst) { 
					jQuery("#datepickerdatapatrono").datepicker( "destroy" )
					calScol_showModal();
					jQuery.getJSON( calScol_createUrl('salvadatapatrono'), { data: dateText }, calScol_setCampi );
					return false; 
						} 
		});
	return false;
}

function calScol_aggDataPatrono_OLD() {
	calScol_showModal();
	var dData = jQuery('#txtFestaPatrono').val();
	jQuery.getJSON( calScol_createUrl('salvadatapatrono'), { data: dData }, calScol_setCampi );
	return false;
}

function calScol_salvaGiorniSettimanali() {
	if (confirm("Cambiando i giorni di orario scolastico settimanali andranno persi i dati inseriti. Continuare?")) {
		calScol_showModal();
		jQuery.getJSON( calScol_createUrl('salvagiornisettimanali'), { giornisettimanali: jQuery('input[name="rbGiorni"]:checked').val() }, calScol_setCampi );
			}
	return false;		
}

function calScol_load() {
	jQuery.getJSON( calScol_createUrl('load'), calScol_setCampi );
	return false;
	}

function calScol_setCampi( json ) {
			
			if (json.daticalendario.errore>'')
				jQuery('#returninfo').html('<span>' + json.daticalendario.errore + '</span>');
			else if (json.daticalendario.info>'')
				jQuery('#returninfo').html('<span>' + json.daticalendario.info + '</span>');
			else
				jQuery('#returninfo').html('<span>Operazione eseguita con successo</span>');
			jQuery('input[name="rbGiorni"]').attr('checked', false);
			jQuery('input[name="rbGiorni"][value="' + json.daticalendario.giornisettimanali + '"]').attr('checked', true);
			jQuery('#giornilezioni').html(json.daticalendario.numgiorni);
			if (json.daticalendario.giornisettimanali==5)
				jQuery('#aggiunginuovadata').hide();
			else	
				jQuery('#aggiunginuovadata').show();
			if (json.daticalendario.cognomecompilatore!=''&&json.daticalendario.nomecompilatore!='')
					{
				jQuery('.msgcampiobbligatori').hide();
				jQuery('.compilazionecampi').show();
					}	
			else
					{
				jQuery('.msgcampiobbligatori').show();
				jQuery('.compilazionecampi').hide();
					}	
			jQuery('#txtFestaPatrono').datepicker({ onSelect: function(dateText, inst) { return calScol_aggDataPatrono(); } });
			if (json.daticalendario.patrono=='')
				jQuery('#festapatrono').html('data non inserita');
			else	
				jQuery('#festapatrono').html(json.daticalendario.patrono);
			jQuery("#txtDataDelibera").val(json.daticalendario.datadelibera)
			jQuery("#txtNumDelibera").val(json.daticalendario.numdelibera)
			jQuery('#txtNomeCompilatore').val(json.daticalendario.nomecompilatore);
			jQuery('#txtCognomeCompilatore').val(json.daticalendario.cognomecompilatore);
			jQuery('#ulaggiungidata li').remove();
			jQuery.each(json.daticalendario.elencodate, function(index, value) { 
					jQuery('<li><label>' + Date.parse(value).toString('dddd dd MMMM yyyy') + '</label>- <a href="#ulaggiungidata" title="elimina data" onclick="calScol_delData(\'' + value + '\');">elimina</a></li>').appendTo('#ulaggiungidata');
					});
			if (jQuery('#returninfo').dialog( "isOpen" )) {
				jQuery('.ui-dialog-buttonpane').show();
				};
			return false;		
	}
  
jQuery(function($){
  Shadowbox.init();
});

