function textCounter(field, countfield, maxlimit) {

   if (field.value.length > maxlimit) {
      field.value = field.value.substring(0, maxlimit);
   } else {
      countfield.value = maxlimit - field.value.length;
   }
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function replaceString(oldS,newS,fullS) { // Replaces oldS with newS in the string fullS

   for (var i=0; i<fullS.length; i++) {
      if (fullS.substring(i,i+oldS.length) == oldS) {
         fullS = fullS.substring(0,i)+newS+fullS.substring(i+oldS.length,fullS.length);
      }
   }

   return fullS;
}

function editWindowOpen(name) {

  width=650;
  height=450;

  if (screen.width) {
     width=screen.width * 0.7 ; // 70% ;
     height=screen.height * 0.7 ; // 70% ;
  }

  /*
  window_location = window.location.toString();
  // mi assicuro che il protocollo non sia https
  window_location = replaceString(window.location.protocol, 'http:', window_location);
  // elimino l'eventuale carattere '?' e tutto ciņ che segue
  window_location = replaceString(window.location.search, '', window_location);
  // elimino l'ultimo carattere '/'  e tutto ciņ che segue (ad esempio: /index.php)
  window_location = window_location.substring(0, window_location.lastIndexOf('/'))
  url= window_location+"/editor/editor.php?field="+name;

  nulla di fatto perchč mozilla non permette ad una pagina http
  di accedere al dom di una pagina https

  */

  url= "./scripts/fckeditor/editor.html?"+name;

  //if(typeof(editWindow)=='undefined') {
  editWindow=window.open(url,"edit","scrollbars=yes,toolbar=no,location=no,status=yes,menubar=no,resizable=yes,width=" + width + ",height=" + height);
  //}
  editWindow.focus();
}

function previewWindowOpen(value) {

  width=650;
  height=450;

  var ns4=(document.layers)? true:false;
  var ie4=(document.all)? true:false;

  if (ie4) width=((window.document.body.clientWidth)/3)*2;
  if (ns4) width=((window.innerWidth)/3)*2;

  if (ie4) height=((window.document.body.clientHeight)/3)*2;
  if (ns4) height=((window.innerHeight)/3)*2;

  previewWindow=window.open("","preview","scrollbars=yes,toolbar=no,location=no,status=no,menubar=no,resizable=yes,width=" + width + ",height=" + height);

  previewWindow.document.write ('<HTML>');
  previewWindow.document.write ('<HEAD>');
  previewWindow.document.write ('<TITLE>preview</TITLE>');
  previewWindow.document.write ('<link rel="stylesheet" href="./scripts/style.css" type="text/css">');
  previewWindow.document.write ('</HEAD>');
  previewWindow.document.write ('<BODY onBlur="window.close()" style="margin: 20px;" BGCOLOR="#FFFFFF">');
  previewWindow.document.write (value);
  previewWindow.document.write ('</BODY>');
  previewWindow.document.write ('</HTML>');
  previewWindow.document.close();
}

function setVariables(){

   if (navigator.appName == "Netscape") {
      if (parseInt(navigator.appVersion) >= 5) {
         v=".top=";
         h=".left=";
         dS="document.getElementById(\"";
         sD="\").style";
         y="window.pageYOffset";
         x="window.pageXOffset";
      } else {
         v=".top=";
         h=".left=";
         dS="document.";
         sD="";
         y="window.pageYOffset";
         x="window.pageXOffset";
      }
   } else {
      v=".pixelTop=";
      h=".pixelLeft=";
      dS="";
      sD=".style";
      y="document.body.scrollTop";
      x="document.body.scrollLeft";
   }

   ystart=eval(y);
   xstart=eval(x);
}

function checkLocation() {

   var ytop=0;
   var xtop=0;
   object="module_menu_layer";
   yy=eval(y);
   xx=eval(x);
   ydiff=ystart-yy;
   xdiff=xstart-xx;

   if ((ydiff<(-1))||(ydiff>(1))) {
      movey=Math.round(ydiff/10);
      ystart-=movey;
   }

   if ((xdiff<(-1))||(xdiff>(1))) {
      movex=Math.round(xdiff/10);
      xstart-=movex;
   }

   if (ystart>=ytop) {
       eval(dS+object+sD+v+(ystart+0));
   } else {
       eval(dS+object+sD+v+ytop);
   }

   if (xstart>=xtop) {
       eval(dS+object+sD+h+xstart);
   } else {
       eval(dS+object+sD+h+xtop);
   }

   setTimeout("checkLocation()",10);
}

function Base64Decode(str) {
/* Copyright (c) 2000 by Robert Graham (RobertGraham.com) */

 var result = "";
 var i = 0;
 var x;
 var shiftreg = 0;
 var count = -1;

 for (i=0; i < str.length; i++) {
  c = str.charAt(i);
  if ('A' <= c && c <= 'Z')
   x = str.charCodeAt(i) - 65;
  else if ('a' <= c && c <= 'z')
   x = str.charCodeAt(i) - 97 + 26;
  else if ('0' <= c && c <= '9')
   x = str.charCodeAt(i) - 48 + 52;
  else if (c == '+')
   x = 62;
  else if (c == '/')
   x = 63;
  else
   continue;

  count++;

  switch (count % 4) {
   case 0:
    shiftreg = x;
    continue;
   case 1:
    v = (shiftreg<<2) | (x >> 4);
    shiftreg = x & 0x0F;
    break;
   case 2:
    v = (shiftreg<<4) | (x >> 2);
    shiftreg = x & 0x03;
    break;
   case 3:
    v = (shiftreg<<6) | (x >> 0);
    shiftreg = x & 0x00;
    break;
  }

  result = result + String.fromCharCode(v);

 }
 return result.toString();
}
