// funzioni per i controlli sull e-book

function ControlloCaratteri (originText, counterText, maxLen)
{
	if (!originText || !counterText) return;
	if (maxLen == null || maxLen < 0) maxLen = 255;

	if (originText.value.length > maxLen)
	{
		alert ("Il testo eccede " + maxLen + " caratteri.");
		originText.value = originText.value.substring(0, maxLen);
		//originText.select();
		originText.focus();
	}

    var numero = (maxLen - originText.value.length);
    if(numero<=500){
        counterText.innerHTML = "Restano " + numero + " caratteri disponibili.";
	}else{
        counterText.innerHTML = "";
    }
}

function aggiungiTagOLD(campo, codeo)
    {
      var code = "<b></b>";
      var myField = document.getElementById(campo);
      var myValue = code;

      if (document.selection)
      {  alert("primo if");
        // IE
        myField.focus();
          sel = document.selection.createRange();
          sel.text = "<b>"+sel.text+"</b>";
      }
      else if (myField.selectionStart || myField.selectionStart == '0') { alert("if");
        // MOZILLA/NETSCAPE
        var startPos = myField.selectionStart;
        var endPos = myField.selectionEnd;
        myField.value = myField.value.substring(0, startPos)
                + myValue
                + myField.value.substring(endPos, myField.value.length);
      }
      else
      {      alert("else");
        myField.value += myValue;
      }
    }


function textarea_wrap_text(obj, str_pre, str_post) {

  //Questa funzione mi aggiunge in una text area (obj) le stringhe pre e post nel range di testo selezionato dall utente, o dove c'è il cursore
  //Se pre è vuota vuol dire che metto un tag "unico", che non ha quinid apertura e chiusura
  
  //ATTENZIONE AL tag pre P. Puo essere p1, p2, p3 e p4 e intende <p align left, right, center o justify
  
  if(str_pre!=""){
     //str_pre="<"+str_pre+">";
     //Controllo se non è un paragrafo, altrimenti devo scrivere anche l'allineamento
     switch (str_pre) {
        case "p1":
           str_pre="<p align='left'>";
           break;
        case "p2":
           str_pre="<p align='right'>";
           break;
        case "p3":
           str_pre="<p align='center'>";
           break;
        case "p4":
           str_pre="<p align='justify'>";
           break;
        default:
           str_pre="<"+str_pre+">";
     }
  }
  if(str_post!=""){
     str_post="<"+str_post+">";
  }
  if (document.selection) { // Internet Explorer
    obj.focus();
    sel = document.selection.createRange();
    sel.text = str_pre + sel.text + str_post;
  } else if (obj.selectionStart || obj.selectionStart == '0') { // Others
    var pos_1 = obj.selectionStart;
    var pos_2 = obj.selectionEnd;
    obj.value = obj.value.substring(0, pos_1)
                + str_pre
                + obj.value.substring(pos_1, pos_2)
                + str_post
                + obj.value.substring(pos_2, obj.value.length);
  } else { // Fallback
    obj.value += str_pre + str_post;
  }
}

