
// CMS-Suite v.1.0 (PHP) - js Utils Class
// last modify: 11/02/2005
// by Felice Ostuni



  //imported from dreamweaver

  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;
  }
 


 
 
 
// per illuminare i TR nei datagrid ... autore ignoto, scopiazzata da qualche parte...
 
function setPointer(theRow, theAction, theDefaultColor, thePointerColor, theMarkColor)
{
//alert('aweee');
    var theCells = null;

    // 1. Pointer and mark feature are disabled or the browser can't get the
    //    row -> exits
    if ((thePointerColor == '' && theMarkColor == '')
        || typeof(theRow.style) == 'undefined') {
        return false;
    }

    // 2. Gets the current row and exits if the browser can't get it
    if (typeof(document.getElementsByTagName) != 'undefined') {
        theCells = theRow.getElementsByTagName('TD');
        //alert('Trovata');
    }
    else if (typeof(theRow.cells) != 'undefined') {
        theCells = theRow.cells;
    }
    else {
        return false;
    }

    // 3. Gets the current color...
    var rowCellsCnt  = theCells.length;
    var domDetect    = null;
    var currentColor = null;
    var newColor     = null;
    // 3.1 ... with DOM compatible browsers except Opera that does not return
    //         valid values with "getAttribute"
    if (typeof(window.opera) == 'undefined'
        && typeof(theCells[0].getAttribute) != 'undefined') {
        currentColor = theCells[0].getAttribute('bgcolor');
        domDetect    = true;
    }
    // 3.2 ... with other browsers
    else {
        currentColor = theCells[0].style.backgroundColor;
        domDetect    = false;
    } // end 3

    // 4. Defines the new color
    // 4.1 Current color is the default one
    if (currentColor == ''
        || currentColor.toLowerCase() == theDefaultColor.toLowerCase()) {
        if (theAction == 'over' && thePointerColor != '') {
            newColor = thePointerColor;
        }
        else if (theAction == 'click' && theMarkColor != '') {
            newColor = theMarkColor;
        }
    }
    // 4.1.2 Current color is the pointer one
    else if (currentColor.toLowerCase() == thePointerColor.toLowerCase()) {
        if (theAction == 'out') {
            newColor = theDefaultColor;
        }
        else if (theAction == 'click' && theMarkColor != '') {
            newColor = theMarkColor;
        }
    }
    // 4.1.3 Current color is the marker one
    else if (currentColor.toLowerCase() == theMarkColor.toLowerCase()) {
        if (theAction == 'click') {
            newColor = (thePointerColor != '')
                     ? thePointerColor
                     : theDefaultColor;
        }
    } // end 4

    // 5. Sets the new color...
    if (newColor) {
        var c = null;
        // 5.1 ... with DOM compatible browsers except Opera
        if (domDetect) {
            for (c = 0; c < rowCellsCnt; c++) {
                theCells[c].setAttribute('bgcolor', newColor, 0);
            } // end for
        }
        // 5.2 ... with other browsers
        else {
            for (c = 0; c < rowCellsCnt; c++) {
                theCells[c].style.backgroundColor = newColor;
            }
        }
    } // end 5

    return true;
} 


 
  // cms suite custom functions
  
  var MainMenu_Timer = 0;

  function MainMenu_Show() {
    var i = 0, Item;
    if (MainMenu_Timer) clearTimeout(MainMenu_Timer);
    Item = document.all('MainMenu[' + i + ']');
    while (Item != null) {
      Item.style.visibility = 'hidden';
      Item = document.all('MainMenu[' + i++ + ']');
    }
    for (i=0; i<arguments.length; i++) {
      Item = document.all('MainMenu[' + arguments[i] + ']');
      Item.style.visibility = 'visible';
    }
  }

  function MainMenu_Hide() {
    MainMenu_Timer = setTimeout('MainMenu_Show()',200);
  }



  function LeftBar_Refresh() {
    if (GetCookie('LeftBar') != null) LeftBar_Expand();
  }
  function LeftBar_Expand() {
    document.all('LeftBar.Layer').style.display = '';
    document.all('LeftBar.Image').style.display = 'none';
    SetCookie('LeftBar','x',1);
  }
  function LeftBar_Collapse() {
    document.all('LeftBar.Layer').style.display = 'none';
    document.all('LeftBar.Image').style.display = '';
    DelCookie('LeftBar');
  }


  function OpenPage(url) { 
    document.location.href = url;
  } 

  function OpenDetailsPage(url,winname,params,width,height) { 
    if (params=='') params = 
      'toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes';
    desktop = window.open(url, winname, params 
      + ',width=' +  width + ',height=' + height);
  } 

  
  function RefreshCtrlItems(CtrlName,StartIndex) {
    var ItemIndex = 0, Item;
    Item = document.getElementById(CtrlName + '[' + StartIndex + ItemIndex + '].Layer');
    while (Item != null) {
      if (FindCookieItem(CtrlName,StartIndex + ItemIndex) == true) {
        ShowHideCtrlItem(0,CtrlName,StartIndex + ItemIndex);
        RefreshCtrlItems(CtrlName,StartIndex + ItemIndex + '.');
      }
      Item = document.getElementById(CtrlName + '[' + StartIndex + ItemIndex++ + '].Layer');
    }
  }  


  function ShowHideCtrlItem(UseCookie,CtrlName,ItemIndex) {
    var Layer,Collapse,Explode;

    Layer = document.getElementById(CtrlName + '[' + ItemIndex + '].Layer');
    Collapse = document.getElementById(CtrlName + '[' + ItemIndex + '].Collapse');    
    Explode = document.getElementById(CtrlName + '[' + ItemIndex + '].Explode');        
    //Immage = document.all(CtrlName + '[' + ItemIndex + '].Image');
    if (Layer != null)  {
      if (Layer.style.display == 'none') {
        Layer.style.display = '';    
        Collapse.style.display = '';
        Explode.style.display = 'none';        
        //Immage.src = imgOpen; 
        if (UseCookie) AddCookieItem(CtrlName,ItemIndex);
      }
      else {
        Layer.style.display = 'none';
        Collapse.style.display = 'none';
        Explode.style.display = '';                
        //Immage.src = imgClosed;
        if (UseCookie) DelCookieItem(CtrlName,ItemIndex);
      }
    }
  }


  function PushFieldToOpener(returnFiled,returnValue,returnDescripton) {
      var RF, RFD;
      RF = eval("window.opener.document.forms[0]."+returnFiled); 
      RF.value = returnValue; 
      if (returnDescripton != "") { 
        RFD = eval("window.opener.document.forms[0]."+returnFiled+"Description");             
        RFD.value = returnDescripton;         
      }        
      window.close();
  }

// valitazione campi //

var digits = "0123456789";
var lowercaseLetters = "abcdefghijklmnopqrstuvwxyz"
var uppercaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
var whitespace = " \t\n\r";
var decimalPointDelimiter = "."

var defaultEmptyOK = true;

function isLetter (c)
{   return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) )
}

function isDigit (c)
{   return ((c >= "0") && (c <= "9"))
}

function isLetterOrDigit (c)
{   return (isLetter(c) || isDigit(c))
}


function isEmpty(s) {
  return ((s == null) || (s.length == 0))
}

function isWhitespace (s) {
  var i;
  if (isEmpty(s)) return true;

  for (i = 0; i < s.length; i++) {   
    var c = s.charAt(i);
    if (whitespace.indexOf(c) == -1) return false;
  }
  return true;
}

function isInteger (s) {
  var i;
  if (isEmpty(s)) 
    if (isInteger.arguments.length == 1) return defaultEmptyOK;
      else return (isInteger.arguments[1] == true);

  for (i = 0; i < s.length; i++) {   
    var c = s.charAt(i);
    if (!isDigit(c)) return false;
  }
  return true;
}

function isIntegerInRange (s, a, b) {
  if (isEmpty(s)) 
    if (isIntegerInRange.arguments.length == 1) return defaultEmptyOK;
      else return (isIntegerInRange.arguments[1] == true);

  if (!isInteger(s, false)) return false;
  var num = parseInt (s);
  return ((num >= a) && (num <= b));
}

function isFloat (s) {
  var i;
  var seenDecimalPoint = false;
  if (isEmpty(s)) 
     if (isFloat.arguments.length == 1) return defaultEmptyOK;
       else return (isFloat.arguments[1] == true);
  if (s == decimalPointDelimiter) return false;
  for (i = 0; i < s.length; i++) {   
    var c = s.charAt(i);
    if ((c == decimalPointDelimiter) && !seenDecimalPoint) seenDecimalPoint = true;
      else if (!isDigit(c) && !((i==0) && (s.charAt(0)=="-"))) return false;
  }
  return true;
}

function isEmail (s) {
  if (isEmpty(s)) 
    if (isEmail.arguments.length == 1) return defaultEmptyOK;
      else return (isEmail.arguments[1] == true);
  if (isWhitespace(s)) return false;
  var i = 1;
  var sLength = s.length;
  while ((i < sLength) && (s.charAt(i) != "@")) {
    i++
  }
  if ((i >= sLength) || (s.charAt(i) != "@")) return false;
    else i += 2;

  while ((i < sLength) && (s.charAt(i) != "."))
    { i++
    }

  if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
    else return true;
}


function checkDate (pDate) {
  
  if (isEmpty(pDate)) {
    return defaultEmptyOK;
  } else {
  
    pDate = pDate.substr(0,10);
    var arrDate = (''+pDate).split('/');
    if (arrDate.length != 3) {
      return (false);
    } else {
      var anno = arrDate[2];
      var mese = arrDate[1] - 1;
      var giorno = arrDate[0];
      var test = new Date(anno,mese,giorno);
      if ( (anno == test.getFullYear()) && (mese == test.getMonth()) && (giorno == test.getDate()) ) {
        return true;
      } else {
        return false;
      }
    }
  }
}

function checkLength (val,minLen,maxLen) {
  var i;
//  if (isNaN(val) || isNaN(minLen) || isNaN(maxLen)) return false;
  if ((val.length <= maxLen) && (val.length >= minLen)) return true;
    else return false;
}


// gestione Cookie //

  function AddCookieItem(CookieName,ItemName) {
    var str;
    str = GetCookie(CookieName);
    if (str == null) {
      SetCookie(CookieName,"|" + ItemName + "|",1);
    }
    else if (str.indexOf(ItemName + "|") == -1) {
      str += ItemName + "|";
      SetCookie(CookieName,str,1);
    }
  }

  function DelCookieItem(CookieName,ItemName) {
    var str, i,j;
    str = GetCookie(CookieName);
    if (str != null) {
      i = str.indexOf("|" + ItemName + "|");
      if (i != -1) {
        j = str.indexOf("|",i + 1);
        str = str.substring(0,i + 1) + str.substring(j + 1);
        if (str == "|") DelCookie(CookieName)
	    else SetCookie(CookieName,str,1);
      }
    }
  }

  function FindCookieItem(CookieName,ItemName) {
    var str, i,j;
    str = GetCookie(CookieName);
    if (str == null) return false
    else if (str.indexOf("|" + ItemName + "|") == -1) return false
    else return true;
  }

  function GetCookie(CookieName) {
    var i,j;
    if (document.cookie.length = 0) 
      return null
    else {  
      i = document.cookie.indexOf(CookieName + "="); 
      if (i == -1) 
        return null
      else {
        i += CookieName.length + 1; 
        j = document.cookie.indexOf(";",i);
        if (j == -1) j = document.cookie.length;
        return unescape(document.cookie.substring(i,j));
      }
    }
  }  

  function SetCookie(CookieName, Value, ExpireDays) {
    var ExpireDate = new Date();
    ExpireDate.setTime(ExpireDate.getTime() + (ExpireDays * 24 * 3600 * 1000));
    document.cookie = CookieName + "=" + escape(Value) + 
      ((ExpireDays == null) ? "" : "; expires=" + ExpireDate.toGMTString());
  }

  function DelCookie(CookieName) {
    if (GetCookie(CookieName)) {
      document.cookie = CookieName + "=; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
  }
