﻿jQuery.ChiamaAjax = function(pagUrl, parametri, divErrore, divRisultato) {      
   //alert(parametri);
   $.ajax({
      //type: "Post",
      url: pagUrl,
      data: parametri,
      success: function(data) {
        $(divErrore).css("visibility", "hidden");        
        $(divRisultato).html(data);        
        //alert(data);
      },
      error: function(XMLHttpRequest, textStatus, errorThrown) {               
        ////$(divErrore).html(pagUrl+"<strong>Attenzione!</strong> si è verificato un errore. <br/>Potrebbe essere un errore dovuto alla connessione internet o ad un problema sul server. <br/>Se l'errore persiste contattami!");
        $(divErrore).html("<strong>Attenzione!</strong> si è verificato un errore. <br/>Potrebbe essere un errore dovuto alla connessione internet o ad un problema sul server. <br/>Se l'errore persiste contattami!<br/>"+XMLHttpRequest + "<br/>"+ textStatus + "<br/>" + errorThrown);
        $(divErrore).css("visibility", "visible");                
      }
   });
};

jQuery.InsericiMessaggio = function(testo, oggetto, dest, idMessRisp) {   
   //alert($("#ctl00_ContenutoPagina_Gruppo").attr("value"));
   //alert("ddd");
   $.ajax({
      //type: "Post",
      url: "backoffice/AjaxFile/InsTestoPagina.aspx",
      data: "oggetto=" + oggetto + "&testo=" + testo + "&dest=" + dest + "&idMessRisp=" + idMessRisp,
      success: function(data) {
        //alert(data);
        $('#ErroreAjax').css("visibility", "hidden");        
        if (data == "true")
        {                        
            //$.SvuotaEditor();
            //alert($("#ctl00_ContenutoPagina_TestoMess").val());
            $("#ctl00_ContenutoPagina_PanelGestUtenti").css("visibility", "hidden");
            //$("#ctl00_ContenutoPagina_TestoMess").val("Messaggio inviato!");
            alert("Messaggio inviato!");
            document.location.href = "PaginaMessaggi.aspx";
            //$("#ctl00_ContenutoPagina_PanelMessaggio").css("visibility", "visible");
            //$("#ctl00_ContenutoPagina_PanelMessaggio").css("display", "block");
            
        }        
      },
      error: function() {        
        //alert("ddd");
        //$('#ErroreAjax').html(pagUrl+"<strong>Attenzione!</strong> si è verificato un errore. <br/>Potrebbe essere un errore dovuto alla connessione internet o ad un problema sul server. <br/>Se l'errore persiste contattami!");
        $('#ErroreAjax').html("<strong>Attenzione!</strong> si è verificato un errore. <br/>Potrebbe essere un errore dovuto alla connessione internet o ad un problema sul server. <br/>Se l'errore persiste contattami!");
        $('#ErroreAjax').css("visibility", "visible");
      }
    });
};

jQuery.ReplaceHtml = function(val) {   
   var valore = val; 
   //alert(valore);
   valore = valore.replace(/\&/g, "&#38");
   valore = valore.replace("\"", "&#34");
   valore = valore.replace("/", "&#47");
   valore = valore.replace("\\", "&#92");
   valore = valore.replace("'", "&#39");
   valore = valore.replace(/\</g, "&#60");
   valore = valore.replace(/\>/g, "&#62");
   valore = valore.replace(/\?/g, "&#63");
   valore = valore.replace(/\;/g, "&#59");
   //alert(valore);
   return val;
};

//Carica la frase caricamento in corso
jQuery.Caricamento = function(campo) {   
    var strWait = '<div class="caricamento"><br><br><img src="../images/ajaxloading.gif" alt="Caricamento" /><br><br><h2>Caricamento dati in corso...</h2></div>';
    $(campo).html(strWait);
};

jQuery.popUp = function(pag, param, titolo, w, h) {     
    $('#contenutoPopUp').css("visibility", "hidden");
    $('#newPopUp').fadeIn(1500);
    
//    $("#newPopUp").animate({
//    width: "100%",
//    //opacity: 0.4,
//    marginLeft: "0.1",
//    //fontSize: "3em",
//    //borderWidth: "10px"
//    }, 3000 );

    $('#newPopUp').css("visibility", "visible");
    $('#newPopUp').css("display", "block");       
    $.Caricamento('#contenutoPopUp');
    $(document).scrollTop(0);
    
    $.ajax({
      //type: "Post",
      url: pag,
      data: param,
      success: function(data) {
        //alert("ciao3");
        $('#ErroreAjax').css("visibility", "hidden");
        $('#contenutoPopUp').html(data);
        //alert('Load was performed.');
        $('#newPopUp').height($(document).height());       
        //alert(h); 
        $('#contenutoPopUp').css("width", w + "px");
//        if (h != undefined)
//        {
//            $('#contenutoPopUp').css("height", h + "px");
//        }
        $('#contenutoPopUp').css("margin-left", "-" + parseInt(w)/2 + "px");        
        $('#ChiudiPopUp').css("margin-left", "-" + parseInt(w)/2 + "px");
        $('#contenutoPopUp').css("visibility", "visible");     
      },
      error: function(XMLHttpRequest, textStatus, errorThrown) {        
        $('#ErroreAjax').html("<strong>Attenzione!</strong> si è verificato un errore. <br/>Potrebbe essere un errore dovuto alla connessione internet o ad un problema sul server. <br/>Se l'errore persiste contattami!");
        $('#ErroreAjax').css("visibility", "visible");
        $('#newPopUp').css("visibility", "hidden");
        $('#newPopUp').css("display", "none");
      }
    });
};

jQuery.Nascondi = function(campo, val) {   
  if (val == "true")
  {
    $(campo).css("visibility", "hidden");
  }
  else
  {
    $(campo).css("visibility", "visible");
  }  
};


function vaiAConAnimazione(id){
   $('html,body').animate({scrollTop: $(id).offset().top},'slow');
}

/********************* Editor **************************/

//jQuery.CaricaEditor = function() {               
//    $("#txtCustomHtmlArea").cleditor({
//          width:        "100%", // width not including margins, borders or padding
//          height:       "100%", // height not including margins, borders or padding
//          controls:     // controls to add to the toolbar
//                        "bold italic underline strikethrough subscript superscript | font size " +
//                        "style | color highlight removeformat | bullets numbering | outdent " +
//                        "indent | alignleft center alignright justify | table | undo redo | " +
//                        "rule | youtube image link unlink | pastetext | print | rep | source",
//          colors:       // colors in the color popup
//                        "FFF FCC FC9 FF9 FFC 9F9 9FF CFF CCF FCF " +
//                        "CCC F66 F96 FF6 FF3 6F9 3FF 6FF 99F F9F " +
//                        "BBB F00 F90 FC6 FF0 3F3 6CC 3CF 66C C6C " +
//                        "999 C00 F60 FC3 FC0 3C0 0CC 36F 63F C3C " +
//                        "666 900 C60 C93 990 090 399 33F 60C 939 " +
//                        "333 600 930 963 660 060 366 009 339 636 " +
//                        "000 300 630 633 330 030 033 006 309 303",    
//          fonts:        // font names in the font popup
//                        "Arial,Arial Black,Comic Sans MS,Courier New,Narrow,Garamond," +
//                        "Georgia,Impact,Sans Serif,Serif,Tahoma,Trebuchet MS,Verdana",
//          sizes:        // sizes in the font size popup
//                        "1,2,3,4,5,6,7",
//          styles:       // styles in the style popup
//                        [["Paragraph", "<p>"], ["Header 1", "<h1>"], ["Header 2", "<h2>"],
//                        ["Header 3", "<h3>"],  ["Header 4","<h4>"],  ["Header 5","<h5>"],
//                        ["Header 6","<h6>"]],
//          useCSS:       false, // use CSS to style HTML when possible (not supported in ie)
//          docType:      // Document type contained within the editor
//                        '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">',
//          docCSSFile:   // CSS file used to style the document contained within the editor
//                        "~/css/gpc.css", 
//          bodyStyle:    // style to assign to document body contained within the editor
//                        "margin:4px; font:10pt Arial,Verdana; cursor:text"
//        });
//};

jQuery.CaricaEditor = function(larg, alt, diritti, source) {               
    var controlli = "";
    var dimTesto = "";
     
    switch(diritti)
    {
        case "0":
            //Completo
            controlli = "bold italic underline strikethrough subscript superscript | font size " +
                        "style | color highlight removeformat | bullets numbering | outdent " +
                        "indent | alignleft center alignright justify | undo redo | table | " +
                        "rule | youtube image emailLink link unlink | pastetext | print | rep linkPag templatePag";
            dimTesto = "1,2,3,4,5,6,7";
            break;
        case "1":
            //Pagina
            controlli = "bold italic underline strikethrough subscript superscript | " +
                        "style | bullets numbering | outdent " +
                        "indent | alignleft center alignright justify | undo redo | table | " +
                        "rule | youtube image emailLink link unlink | pastetext | rep linkPag templatePag";
            dimTesto = "1,2,3";
            break;
        case "2":
            //News
            controlli = "bold italic underline strikethrough | " +
                        "bullets numbering | outdent " +
                        "indent | alignleft center alignright justify | undo redo | " +
                        "rule | emailLink link unlink | pastetext | repSoloAll linkPag";
            dimTesto = "1,2,3";
            break;        
        case "3":
            //Newsletter e rassegna stampa
            controlli = "bold italic underline strikethrough subscript superscript | " +
                        "style | bullets numbering | outdent " +
                        "indent | alignleft center alignright justify | undo redo | table | " +
                        "rule | youtube image emailLink link unlink | pastetext | rep linkPag";
            dimTesto = "1,2,3";
            break;
        case "4":
            //Pagina Exhibit
            controlli = "bold italic underline strikethrough subscript superscript | " +
                        "style | bullets numbering | outdent " +
                        "indent | alignleft center alignright justify | undo redo | table | " +
                        "rule | youtube image emailLink link unlink | pastetext | rep templatePag";
            dimTesto = "1,2,3";
            break;
    }
    
    if (source == "1")
    {
        controlli += " | source";
    }
                        
    $("#txtCustomHtmlArea").cleditor({
          width:        larg, // width not including margins, borders or padding
          height:       alt, // height not including margins, borders or padding
          controls:     // controls to add to the toolbar
                        controlli,
          colors:       // colors in the color popup
                        "FFF FCC FC9 FF9 FFC 9F9 9FF CFF CCF FCF " +
                        "CCC F66 F96 FF6 FF3 6F9 3FF 6FF 99F F9F " +
                        "BBB F00 F90 FC6 FF0 3F3 6CC 3CF 66C C6C " +
                        "999 C00 F60 FC3 FC0 3C0 0CC 36F 63F C3C " +
                        "666 900 C60 C93 990 090 399 33F 60C 939 " +
                        "333 600 930 963 660 060 366 009 339 636 " +
                        "000 300 630 633 330 030 033 006 309 303",    
          fonts:        // font names in the font popup
                        "Arial,Arial Black,Comic Sans MS,Courier New,Narrow,Garamond," +
                        "Georgia,Impact,Sans Serif,Serif,Tahoma,Trebuchet MS,Verdana",
          sizes:        // sizes in the font size popup
                        dimTesto,
          styles:       // styles in the style popup
                        [["Paragraph", "<p>"], ["Header 1", "<h1>"], ["Header 2", "<h2>"],
                        ["Header 3", "<h3>"],  ["Header 4","<h4>"],  ["Header 5","<h5>"],
                        ["Header 6","<h6>"]],
          useCSS:       false, // use CSS to style HTML when possible (not supported in ie)
          docType:      // Document type contained within the editor
                        '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">',
          docCSSFile:   // CSS file used to style the document contained within the editor
                        "~/css/gpc.css", 
          bodyStyle:    // style to assign to document body contained within the editor
                        "margin:4px; font:10pt Arial,Verdana; cursor:text"
        });
};

//Scrive su #txtCustomHtmlArea 
jQuery.ScriviSuEditor = function(valore) {
    var editor = $("#txtCustomHtmlArea").cleditor()[0];
    if (editor)
    {
        editor.focus();
        editor.execCommand("inserthtml", valore, false, null);
        editor.updateTextAreaDec();
        editor.updateFrameDec();
        editor.focus();
    }
    //$("#txtCustomHtmlArea").cleditor()[0].updateFrame();
};

//legge #txtCustomHtmlArea 
jQuery.LeggiEditor = function() {    
    var testo = $("#txtCustomHtmlArea").val();
    if (testo.length > 0)
    {
        testo = testo.replace(/\&nbsp;/g, "§§");
        testo = testo.replace(/\&/g, "££");
        testo = testo.replace(/\#/g, "^^^");
    }
    
    return testo;
};

jQuery.SvuotaEditor = function() {
    $("#txtCustomHtmlArea").cleditor()[0].clear();    
    //$.CaricaEditor();
};

jQuery.CaricaEmoticons = function(idEmoticons) {   
   //alert($("#ctl00_ContenutoPagina_Gruppo").attr("value"));
   $.ajax({
      //type: "Post",
      url: "Ajax/VisEmoticons.aspx",
      data: "id="+idEmoticons.toString()+"&soloNomeImg=false",
      success: function(data) {
        $('#ErroreAjax').css("visibility", "hidden");        
        //$("#txtCustomHtmlArea").htmlarea("image", data);               
        //$("#txtCustomHtmlArea").cleditor()[0].execCommand("inserthtml", data, null , null);        
        $.ScriviSuEditor(data);
      },
      error: function() {
        //$('#ErroreAjax').html("<strong>Attenzione!</strong> si è verificato un errore. <br/>Potrebbe essere un errore dovuto alla connessione internet o ad un problema sul server. <br/>Se l'errore persiste contattami!");
        //$('#ErroreAjax').css("visibility", "visible");        
      }
    });
};

/********************* Fine Editor **************************/

/*
function toggle(div_id) {
	var el = document.getElementById(div_id);
	if ( el.style.display == 'none' ) {	el.style.display = 'block';}
	else {el.style.display = 'none';}
}
function blanket_size(popUpDivVar) {
	if (typeof window.innerWidth != 'undefined') {
		viewportheight = window.innerHeight;
	} else {
		viewportheight = document.documentElement.clientHeight;
	}
	if ((viewportheight > document.body.parentNode.scrollHeight) && (viewportheight > document.body.parentNode.clientHeight)) {
		blanket_height = viewportheight;
	} else {
		if (document.body.parentNode.clientHeight > document.body.parentNode.scrollHeight) {
			blanket_height = document.body.parentNode.clientHeight;
		} else {
			blanket_height = document.body.parentNode.scrollHeight;
		}
	}
	var blanket = document.getElementById('blanket');
	blanket.style.height = blanket_height + 'px';
	var popUpDiv = document.getElementById(popUpDivVar);
	popUpDiv_height=blanket_height/2-150;//150 is half popup's height
	popUpDiv.style.top = popUpDiv_height + 'px';
}
function window_pos(popUpDivVar) {
	if (typeof window.innerWidth != 'undefined') {
		viewportwidth = window.innerHeight;
	} else {
		viewportwidth = document.documentElement.clientHeight;
	}
	if ((viewportwidth > document.body.parentNode.scrollWidth) && (viewportwidth > document.body.parentNode.clientWidth)) {
		window_width = viewportwidth;
	} else {
		if (document.body.parentNode.clientWidth > document.body.parentNode.scrollWidth) {
			window_width = document.body.parentNode.clientWidth;
		} else {
			window_width = document.body.parentNode.scrollWidth;
		}
	}
	var popUpDiv = document.getElementById(popUpDivVar);
	window_width=window_width/2-150;//150 is half popup's width
	popUpDiv.style.left = window_width + 'px';
}

function popup(windowname) {
	blanket_size(windowname);
	window_pos(windowname);
	toggle('blanket');
	toggle(windowname);		
}
*/
