$(document).ready(function(){
  
  /*if(CommPerArticolo!=""){
    $.post("/ajaxCalls/commentiArticolo.asp", {IdArt: CommPerArticolo}, function(data){
      $("#contenutoCommenti").hide()
      $("#contenutoCommenti").html(data)
      $("#contenutoCommenti").fadeIn("fast")
    })
  }*/
  
  $("ul.srcBtn a.srcBtn").click(function(){
    $("#searchForm").submit()
    return false;
  })
  
  $("#searchInput").focus(function(){
    $("#searchDiv").addClass("overStatus")
    $(this).val("")
  })
  
  $("#searchInput").blur(function(){
    $("#searchDiv").removeClass("overStatus")
  })
  
  $("a[rel='imgAutori']").lightBox()
  
  $("a.toTop").click(function(){
    $(document).scrollTo( 0, 800)
    return false;
  })
  
  $("a[rel='risp_comm']").click(function(){
    var nome = $(this).attr("href")
    oEditor.SetHTML("<b>@ "+nome+"</b>:")
    oEditor.Focus()
    $(document).scrollTo( "#guestbookform", 500)
    return false;
  })
  
  $("a[rel='quota_comm']").click(function(){
    var nome = $(this).attr("href")
    var messaggio = $("#commento_" + String($(this).attr("class")).replace("quota_", "")).html()
    oEditor.SetHTML("<b>"+nome+"</b> ha scritto:<blockquote>"+messaggio+"</blockquote>")
    oEditor.Focus()
    $(document).scrollTo( "#guestbookform", 500)
    return false;
  })
  
})

/*script per i commenti*/
  function Guest_isValidURL(url){ 
      var RegExp = /^(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?$/; 
      if(RegExp.test(url)){ 
          return true; 
      }else{ 
          return false; 
      } 
  }
  $(function(){
    /*FCKEditor*/
    
    if($("#messaggioGuestbook").length>0){
      var oFCKeditor = new FCKeditor( 'messaggioGuestbook' ) ;
      oFCKeditor.ToolbarSet = 'pubblicazioni_toolbar' ;
      oFCKeditor.BasePath = "/gsoft/fckeditor/" ;
      oFCKeditor.Height = 200 ;
      oFCKeditor.Width = 505 ;
      oFCKeditor.ReplaceTextarea() ;
    }
    
    $("#guestbookform").submit(function(){
      var nome = Trim(String($("#nomeGuestbook").val()))
      var email = Trim(String($("#emailGuestbook").val()))
      var url = Trim(String($("#urlGuestbook").val()))
      
      if(nome.length<=0){
        $("#erroreGuestbook").html("Il nome è obbligatorio")
        $("#nomeGuestbook").focus();return false;
      }
      
      if(email.length<=0){
        $("#erroreGuestbook").html("L'email è obbligatoria")
        $("#emailGuestbook").focus();return false;
      }
      
      var espr = /^[_a-z0-9+-]+(\.[_a-z0-9+-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)+$/;
      if (!espr.test( email )){
        $("#erroreGuestbook").html("L'email inserita non è valida")
        $("#emailGuestbook").focus();return false;
      }
      
      if(url.length>0 && Guest_isValidURL(url)==false){
        $("#erroreGuestbook").html("L'url inserito non è valido")
        $("#urlGuestbook").focus();return false;
      }
      
      if(url.indexOf("//") != -1)
        url = url.split("//")[1]
      
      var messaggio = oEditor.GetXHTML()
      if(Trim(String(messaggio)).length<=0){
        $("#erroreGuestbook").html("Il messaggio è obbligatorio")
        $("#messaggioGuestbook").focus();return false;
      }
      
      
      var reHTML = new RegExp('<(?!\/?(u(?=>|\s.*>)|strong(?=>|\s.*>)|em(?=>|\s.*>)|strike(?=>|\s.*>)))\/?.*?>', "gi");
      var anteprimaCommento = messaggio.replace(reHTML, "")
      var inviaEmail = "No"
      var IdArt_p = String($("#IdArticoloPerCommento").val())
      var IdRisposta_p = ""
      
      $("#loaderGuestbook").show()
      $.post("/addComment.asp", 
        {nick_p: nome, email_p: email, url_p: url, anteprima_p: anteprimaCommento, inviEmail_p: inviaEmail, messaggio_p: messaggio, IdArt: IdArt_p, IdRisposta: IdRisposta_p}, 
        function(data){
        $("#loaderGuestbook").hide()
        if(data=="ok"){
          $("#erroreGuestbook").css("color", "green")
          $("#erroreGuestbook").html("Commento inviato con successo.")
          window.location.reload();
        }else{
          $("#erroreGuestbook").html("C'è stato un errore durante l'invio. Riprovare più tardi.")
        }
        
      })
      
      return false;
    })
    
    $("#contattiForm").submit(function(){
      var nome = Trim(String($("#nomeContatti").val()))
      var email = Trim(String($("#emailContatti").val()))
      var ogg = Trim(String($("#oggettoContatti").val()))
      
      if(nome.length<=0){
        $("#erroreGuestbook").html("Il nome è obbligatorio")
        $("#nomeContatti").focus();return false;
      }
      
      if(email.length<=0){
        $("#erroreGuestbook").html("L'email è obbligatoria")
        $("#emailContatti").focus();return false;
      }
      
      var espr = /^[_a-z0-9+-]+(\.[_a-z0-9+-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)+$/;
      if (!espr.test( email )){
        $("#erroreGuestbook").html("L'email inserita non è valida")
        $("#emailContatti").focus();return false;
      }
      
      
      var messaggio = $("#messaggioContatti").val()
      if(Trim(String(messaggio)).length<=0){
        $("#erroreGuestbook").html("Il messaggio è obbligatorio")
        $("#messaggioContatti").focus();return false;
      }
      
      $("#loaderContatti").show()
      $.post("/contatti/senda.asp", 
        {nome_p: nome, email_p: email, oggetto_p: ogg, messaggio_p: messaggio}, 
        function(data){
        $("#loaderContatti").hide()
        if(data=="ok"){
          $("#erroreGuestbook").css("color", "green")
          $("#erroreGuestbook").html("Messaggio inviato con successo.")
        }else{
          $("#erroreGuestbook").html("C'è stato un errore durante l'invio. Riprovare più tardi.")
        }
        
      })
      
      return false;
    })
    
  })
  
  function FCKeditor_OnComplete( editorInstance ){
    oEditor=FCKeditorAPI.GetInstance('messaggioGuestbook');
  }

/*script per i commenti FINE*/

/*funzioni di utilità*/

function Trim(strq)
{
	while(strq.indexOf(" ")==0)
	{
		if(strq.length > 0)
			strq = strq.replace(" ", "");
		else
			break;
	}
	
	while(strq.lastIndexOf(" ")==strq.length-1)
	{
		if(strq.length > 0)
			strq = strq.substring(0, strq.length-1);
		else
			break;
	}
		
	return strq;
}

$.ajaxSetup({"error":function(XMLHttpRequest,textStatus, errorThrown) {   
    //alert(textStatus);
    //alert(errorThrown);
    alert("errore: "+XMLHttpRequest.responseText);
  }
});
