//window.onload = function() {document.onkeydown = changeVal; document.onkeyup = changeVal;}
// Div Windows JavaScript Document
//recall function

var noToggleSelect=true;

var new_select = Array();

var new_selectSM = Array();

var new_select_x = Array();

var new_select_age = Array();

function opendiv(win) {
        if (winList[win]) {
                        winList[win].open();
        }
}
function closediv(win) {
        if (winList[win]) {
                        winList[win].close();
        }
}
//____________________________________________________________________________________________________________________________________________________
//=============================================================================
// Check Browser
//=============================================================================
// Determine browser and version.
//The script does some checking and stores the browser information in a global variable called browser.
//This is built as a user-defined object with properties to indicate the browser type and version number.
function Browser() {
  var ua, s, i;
  this.isIE    = false;  // Internet Explorer
  this.isNS    = false;  // Netscape
  this.version = null;
  ua = navigator.userAgent;
  s = "MSIE";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isIE = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }
  s = "Netscape6/";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }
  // Treat any other "Gecko" browser as NS 6.1.
  s = "Gecko";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = 6.1;
    return;
  }
}
var browser = new Browser();
//_____________________________________________________________________________________________________________________________________________________
//=============================================================================
// Window Object
//=============================================================================
//This function automatically scans the document for any DIV elements using the window class and creates a Window object for each one found
//Window() function takes one argument, the DOM node corresponding to outer, window-class DIV element of the window
function Window(el) {
  var i, mapList, mapName;
  // Get window components. (locate the sub elements of that DIV which make up the window components, like the title bar, client area, etc. and save them as properties of the object so that they can be easily referenced later):
  this.frame           = el;
  this.titleBar        = winFindByClassName(el, "titleBar");
  this.titleBarText    = winFindByClassName(el, "titleBarText");
  this.titleBarButtons = winFindByClassName(el, "titleBarButtons");
  this.clientArea      = winFindByClassName(el, "clientArea");
  // Find matching button image map. (find the image map assigned to the window's control buttons)
  mapName = this.titleBarButtons.useMap.substr(1);
  //document.getElementsByTagName() is used to find all the MAP elements on the page. It then checks them to find one whose name matches the USEMAP attribute value on the button image tag
  mapList = document.getElementsByTagName("MAP");
  for (i = 0; i < mapList.length; i++) {
    if (mapList[i].name == mapName) {
      this.titleBarMap = mapList[i];
        }
  }
  // Save colors.
  //(Window() function then saves the original element colors that will be altered whenever the window is inactivated so that they can be properly restored whenever it becomes active again)
  this.activeFrameBackgroundColor  = this.frame.style.backgroundColor;
  this.activeFrameBorderColor      = this.frame.style.borderColor;
  this.activeTitleBarColor         = this.titleBar.style.backgroundColor;
  this.activeTitleTextColor        = this.titleBar.style.color;
  this.activeClientAreaBorderColor = this.clientArea.style.borderColor;
  if (browser.isIE) {
    this.activeClientAreaScrollbarColor = this.clientArea.style.scrollbarBaseColor;
  }
  // Save images.
  //also saves the button image SRC and LONGDESC values so these can be easily referenced when switching between active to inactive states
  this.activeButtonsImage   = this.titleBarButtons.src;
  this.inactiveButtonsImage = this.titleBarButtons.longDesc;
  // Set flags. (sets up a couple of flags used by several functions to track the window's current state and assigns the window methods)
  this.isOpen      = false;
  this.isMinimized = false;
  // Set methods.
  this.open       = winOpen;
  this.close      = winClose;
  this.minimize   = winMinimize;
  this.restore    = winRestore;
  this.makeActive = winMakeActive;
  // Set up event handling.
  // the window frame (the outermost window DIV) is set to capture various mouse events so the window can be resized by dragging it
  this.frame.parentWindow = this;
  this.frame.onmousemove  = winResizeCursorSet;
  this.frame.onmouseout   = winResizeCursorRestore;
  this.frame.onmousedown  = winResizeDragStart;
  // the title bar element is set up for mouse events so the window can be moved by dragging it
  this.titleBar.parentWindow = this;
  this.titleBar.onmousedown  = winMoveDragStart;
  // client area is set up for the onclick event so that the user can make the window active by clicking on it
  this.clientArea.parentWindow = this;
  // a user-defined property called parentWindow is assigned to each of these elements
  // this is done so the event handler functions can easily reference the actual window object itself rather than just the object that fired the event
  this.clientArea.onclick      = winClientAreaClick;
  // this reference is actually set in the Window() function using the following code
  for (i = 0; i < this.titleBarMap.childNodes.length; i++) {
    if (this.titleBarMap.childNodes[i].tagName == "AREA") {
      this.titleBarMap.childNodes[i].parentWindow = this;
        }
  }
  //***************************************************************************************************************
  // Calculate the minimum width and height values for resizing
  // and fix any initial display problems.
  var initLt, initWd, w, dw;
  // Save the inital frame width and position, then reposition
  // the window.
  initLt = this.frame.style.left;
  initWd = parseInt(this.frame.style.width);
  this.frame.style.left = -this.titleBarText.offsetWidth + "px";
  // For IE, start calculating the value to use when setting
  // the client area width based on the frame width.
  if (browser.isIE) {
    this.titleBarText.style.display = "none";
    w = this.clientArea.offsetWidth;
    this.widthDiff = this.frame.offsetWidth - w;
    this.clientArea.style.width = w + "px";
    dw = this.clientArea.offsetWidth - w;
    w -= dw;
    this.widthDiff += dw;
    this.titleBarText.style.display = "";
  }
  // Find the difference between the frame's style and offset
  // widths. For IE, adjust the client area/frame width
  // difference accordingly.
  w = this.frame.offsetWidth;
  this.frame.style.width = w + "px";
  dw = this.frame.offsetWidth - w;
  w -= dw;
  this.frame.style.width = w + "px";
  if (browser.isIE) {
  //widthDiff property is updated to account for this difference
    this.widthDiff -= dw;
  }
  // Find the minimum width for resize.
  this.isOpen = true;  // Flag as open so minimize call will work.
  this.minimize();
  // Get the minimum width.
  if (browser.isNS && browser.version >= 1.2) {
    // For later versions of Gecko.
    this.minimumWidth = this.frame.offsetWidth;
  }else{
    // For all others.
    this.minimumWidth = this.frame.offsetWidth - dw;
  }
  // Find the frame width at which or below the title bar text will
  // need to be clipped.
  this.titleBarText.style.width = "";
  this.clipTextMinimumWidth = this.frame.offsetWidth - dw;
  // Set the minimum height.
  this.minimumHeight = 1;
  // Restore window. For IE, set client area width.
  // a call to the restore() method is used to undo the effects of the minimize() call
  this.restore();
  this.isOpen = false;  // Reset flag.
  initWd = Math.max(initWd, this.minimumWidth);
  this.frame.style.width = initWd + "px";
  if (browser.isIE) {
    this.clientArea.style.width = (initWd - this.widthDiff) + "px";
  }
  // Clip the title bar text if needed.
  if (this.clipTextMinimumWidth >= this.minimumWidth) {
    this.titleBarText.style.width = (winCtrl.minimizedTextWidth + initWd - this.minimumWidth) + "px";
  }
  // Restore the window to its original position.
  this.frame.style.left = initLt;
}
//_____________________________________________________________________________________________________________________________________________________________________
//=============================================================================
// Window Methods
//=============================================================================
// four of the window methods work in pairs; open/close and minimize/restore
// for the most part, these methods merely change a window's appearance by altering display style properties on individual window elements
//
// the window and its contents are fully visible whenever open() is called
function winOpen() {
  // if the window is already opened exit the function
  if (this.isOpen) {
    return false;
  }
  // restore the window and make it visible.
  this.makeActive();
  this.isOpen = true;
  if (this.isMinimized) {
    this.restore();
  }
  // a window is opened by setting the visibility style of its frame to visible
  this.frame.style.visibility = "visible";
  // by zioORazio
  // per mettere le select
  if (browser.isIE && noToggleSelect==true) ToggleSelectOff();

}
//
function winClose() {
  // Hide the window.
  this.frame.style.visibility = "hidden";
  this.isOpen = false;
  // by zioORazio
  // per togliere le select
  if (browser.isIE && noToggleSelect==true) ToggleSelectOn();

}

function ToggleSelectOff() {
                      var j;
                      var k;
                      for(j=0;j<document.forms.length;j++) {
                          var ele_s=document.forms[j].elements;
                              for(k=0;k<ele_s.length;k++) {
                                  if (ele_s[k].type.search('select-')!=-1) {
                                      ele_s[k].style.visibility="hidden";
                                  }
                              }
                          }
                       return false;
}

function ToggleSelectOn() {
                  var j;
                  var k;
                  for(j=0;j<document.forms.length;j++) {
                      var ele_s=document.forms[j].elements;
                      for(k=0;k<ele_s.length;k++) { //alert(ele_s[k].type);
                          if (ele_s[k].type.search('select-')!=-1) {
                              ele_s[k].style.visibility="visible";
                          }
                      }
                  }
                  return false;
}
//_____________________________________________________________________________________________________________________________________________________________________
// when a window is minimized, only its title bar is displayed
function winMinimize() {
  // if not opened or if minimized exit the function
  if (!this.isOpen || this.isMinimized) {
    return;
  }
  // make active the current window
  this.makeActive();
  // Save current frame and title bar text widths.
  this.restoreFrameWidth = this.frame.style.width;
  this.restoreTextWidth = this.titleBarText.style.width;
  // Disable client area display.
  this.clientArea.style.display = "none";
  // Minimize frame and title bar text widths.
  if (this.minimumWidth) {
    this.frame.style.width = this.minimumWidth + "px";
  } else {
    this.frame.style.width = "";
  }
  this.titleBarText.style.width = winCtrl.minimizedTextWidth + "px";
  this.isMinimized = true;
}
//_____________________________________________________________________________________________________________________________________________________________________
// the restore() method simply resets the window's frame and title text widths to the values saved when it was minimized
function winRestore() {
  if (!this.isOpen || !this.isMinimized) {
    return;
  }
  this.makeActive();
  // Enable client area display.
  this.clientArea.style.display = "";
  // Restore frame and title bar text widths.
  this.frame.style.width = this.restoreFrameWidth;
  this.titleBarText.style.width = this.restoreTextWidth;
  this.isMinimized = false;
}
//_____________________________________________________________________________________________________________________________________________________________________
// makeActive() function uses the values defined in winCtrl to change the window's colors and possibly its button image, if an additional one was specified for it
function winMakeActive() {
  if (winCtrl.active == this) {
    return;
  }
  // Inactivate the currently active window.
  // the first step in making a window active is to inactive whichever window is currently the active one, that window is given by winCtrl.active
  if (winCtrl.active) {
    winCtrl.active.frame.style.backgroundColor    = winCtrl.inactiveFrameBackgroundColor;
    winCtrl.active.frame.style.borderColor        = winCtrl.inactiveFrameBorderColor;
    winCtrl.active.titleBar.style.backgroundColor = winCtrl.inactiveTitleBarColor;
    winCtrl.active.titleBar.style.color           = winCtrl.inactiveTitleTextColor;
    winCtrl.active.clientArea.style.borderColor   = winCtrl.inactiveClientAreaBorderColor;
    if (browser.isIE) {
      winCtrl.active.clientArea.style.scrollbarBaseColor = winCtrl.inactiveClientAreaScrollbarColor;
        }
    if (browser.isNS && browser.version < 6.1) {
      winCtrl.active.clientArea.style.overflow = "hidden";
        }
    if (winCtrl.active.inactiveButtonsImage) {
      winCtrl.active.titleBarButtons.src = winCtrl.active.inactiveButtonsImage;
        }
  }
  // Activate this window.
  this.frame.style.backgroundColor    = this.activeFrameBackgroundColor;
  this.frame.style.borderColor        = this.activeFrameBorderColor;
  this.titleBar.style.backgroundColor = this.activeTitleBarColor;
  this.titleBar.style.color           = this.activeTitleTextColor;
  this.clientArea.style.borderColor   = this.activeClientAreaBorderColor;
  if (browser.isIE) {
    this.clientArea.style.scrollbarBaseColor = this.activeClientAreaScrollbarColor;
  }
  if (browser.isNS && browser.version < 6.1) {
    this.clientArea.style.overflow = "auto";
  }
  if (this.inactiveButtonsImage) {
    this.titleBarButtons.src = this.activeButtonsImage;
  }
  // the windows is placed at the top of the stacking order so winCtrl.maxzIndex is incremented and the new value is assigned to the window's frame's z-index style
  this.frame.style.zIndex = ++winCtrl.maxzIndex;
  //winCtrl.active is updated to point to this window
  winCtrl.active = this;
}
//_____________________________________________________________________________________________________________________________________________________________________
//=============================================================================
// Event handlers.
//=============================================================================
//call makeActive() for the parent window (make it active by clicking just about anywhere on it):
function winClientAreaClick(event) {
  // Make this window the active one.
  this.parentWindow.makeActive();
}
//_____________________________________________________________________________________________________________________________________________________________________
//-----------------------------------------------------------------------------
// Window dragging.
//-----------------------------------------------------------------------------
function winMoveDragStart(event) {
  var target;
  var x, y;
  if (browser.isIE) {
    target = window.event.srcElement.tagName;
  }
  if (browser.isNS) {
    target = event.target.tagName;
  }
  // first checks to see if the cursor is on a button and, if so, exits (prevents it from interfering with the window button events). This way window drag does not work with the mouse on the close or resize buttons
  if (target == "AREA") {
    return;
  }
  // makeActive() is called to initialize the drag operation
  this.parentWindow.makeActive();
  // Get cursor offset from window frame.
  if (browser.isIE) {
    x = window.event.x;
    y = window.event.y;
  }
  if (browser.isNS) {
    x = event.pageX;
    y = event.pageY;
  }
  // calculates the offset of the cursor relative to the window frame, storing the values in the winCtrl object (offsets will be used to move the window as the cursor is moved around the page)
  winCtrl.xOffset = winCtrl.active.frame.offsetLeft - x;
  winCtrl.yOffset = winCtrl.active.frame.offsetTop  - y;
  // Set document to capture mousemove and mouseup events. Event handling is different for IE and Netscape:
  if (browser.isIE) {
    document.onmousemove = winMoveDragGo;
    document.onmouseup   = winMoveDragStop;
  }
  if (browser.isNS) {
    document.addEventListener("mousemove", winMoveDragGo,   true);
    document.addEventListener("mouseup",   winMoveDragStop, true);
    event.preventDefault();
  }
  // flag in winCtrl to indicate that a move drag has started
  winCtrl.inMoveDrag = true;
}
//_____________________________________________________________________________________________________________________________________________________________________
// winMoveDragGo() function will now execute as the mouse is dragged around the browser window
// it simply takes the current cursor position, adds the offset values saved earlier and uses those sums to change the left and top positions of the window frame
function winMoveDragGo(event) {
  var x, y;
  if (!winCtrl.inMoveDrag) {
    return;
  }
  // Get cursor position.
  if (browser.isIE) {
    x = window.event.x;
    y = window.event.y;
        //Event bubbling is canceled to prevent the browser's normal action of creating and highlighting a selection on the page
        //ONLY IE boolean cancelBubble: propagate up the element container hierarchy. The false setting equivalent for W3C DOM is stopPropagation(), otherwise, true
    window.event.cancelBubble = true;
        //ONLY IE: returnValue: set to false any event to be returned to the main window when an IE dialog window (created via the showModalDialog method) closes
    window.event.returnValue = false;
  }
  if (browser.isNS) {
    x = event.pageX;
    y = event.pageY;
    event.preventDefault();
  }
  // Move window frame based on offset from cursor.
  winCtrl.active.frame.style.left = (x + winCtrl.xOffset) + "px";
  winCtrl.active.frame.style.top  = (y + winCtrl.yOffset) + "px";
}
//_____________________________________________________________________________________________________________________________________________________________________
//When the user releases the mouse button, winMoveDragStop() is called.
// the function clears the move drag flag in winCtrl and removes the mousemove and mouseup event capturing for the document, ending the drag operation
function winMoveDragStop(event) {
  winCtrl.inMoveDrag = false;
  // Remove mousemove and mouseup event captures on document.
  if (browser.isIE) {
    document.onmousemove = null;
    document.onmouseup   = null;
  }
  if (browser.isNS) {
    document.removeEventListener("mousemove", winMoveDragGo,   true);
    document.removeEventListener("mouseup",   winMoveDragStop, true);
  }
}
//_____________________________________________________________________________________________________________________________________________________________________
//-----------------------------------------------------------------------------
// Window resizing.
//-----------------------------------------------------------------------------
// winResizeCursorSet() handles a mouseover on the window frame
function winResizeCursorSet(event) {
  var target;
  var xOff, yOff;
  // checks that the window is not currently minimized or already in the process of a resize drag
  if (this.parentWindow.isMinimized || winCtrl.inResizeDrag) {
    return;
  }
  // checks that the event originated from the window frame itself, not a sub element such as the title bar or client area, etc
  // If not on window frame, restore cursor and exit.
  if (browser.isIE) {
    target = window.event.srcElement;
  }
  if (browser.isNS) {
    target = event.target;
  }
  if (target != this.parentWindow.frame) {
    return;
  }
  // find the position of the cursor relative to the edges of the frame
  // Find resize direction.
  if (browser.isIE) {
    xOff = window.event.offsetX;
    yOff = window.event.offsetY;
  }
  if (browser.isNS) {
    xOff = event.layerX;
    yOff = event.layerY;
  }
  winCtrl.resizeDirection = "";
  // value set in winCtrl.resizeCornerSize is used for comparison (here is 16, so if the cursor is within 16 pixels of a given edge, it's considered a "hit."
  // this results in the variable winCtrl.resizeDirection being set to one of eight directions: "n", "ne", "e", "se", etc...
  if (yOff <= winCtrl.resizeCornerSize) {
    winCtrl.resizeDirection += "n";
  } else if (yOff >= this.parentWindow.frame.offsetHeight - winCtrl.resizeCornerSize) {
    winCtrl.resizeDirection += "s";
  }
  if (xOff <= winCtrl.resizeCornerSize) {
    winCtrl.resizeDirection += "w";
  }else if (xOff >= this.parentWindow.frame.offsetWidth - winCtrl.resizeCornerSize) {
    winCtrl.resizeDirection += "e";
  }
  // If not on window edge, restore cursor and exit.
  if (winCtrl.resizeDirection == "") {
    this.onmouseout(event);
    return;
  }
  // if it is close enough to the edge, the function changes the cursor to the appropriate resize style:
  // Change cursor.
  if (browser.isIE) {
    document.body.style.cursor = winCtrl.resizeDirection + "-resize";
  }
  if (browser.isNS) {
    this.parentWindow.frame.style.cursor = winCtrl.resizeDirection + "-resize";
  }
}
//_____________________________________________________________________________________________________________________________________________________________________
// winResizeCursorRestore() handles the mouseout event. It simply resets the cursor, provided that a resize drag is not currently being performed
function winResizeCursorRestore(event) {
  if (winCtrl.inResizeDrag) {
    return;
  }
  // Restore cursor.
  if (browser.isIE) {
    document.body.style.cursor = "";
  }
  if (browser.isNS) {
    this.parentWindow.frame.style.cursor = "";
  }
}
//_____________________________________________________________________________________________________________________________________________________________________
// When a mousedown occurs on the window frame, winResizeDragStart() is called
function winResizeDragStart(event) {
  var target;
  // Make sure the event is on the window frame.
  if (browser.isIE)
    target = window.event.srcElement;
  if (browser.isNS)
    target = event.target;
  if (target != this.parentWindow.frame)
    return;

  this.parentWindow.makeActive();
  //resize could not occour in a minimized window. If so it exits
  if (this.parentWindow.isMinimized)
    return;
  //To initialize the resize, the function finds the current cursor position relative to the page and the current position and size of the window, saving these values in the winCtrl variable
  // Save cursor position.
  if (browser.isIE) {
    winCtrl.xPosition = window.event.x;
    winCtrl.yPosition = window.event.y;
  }
  if (browser.isNS) {
    winCtrl.xPosition = event.pageX;
    winCtrl.yPosition = event.pageY;
  }

  // Save window frame position and current window size.

  winCtrl.oldLeft   = parseInt(this.parentWindow.frame.style.left,  10);
  winCtrl.oldTop    = parseInt(this.parentWindow.frame.style.top,   10);
  winCtrl.oldWidth  = parseInt(this.parentWindow.frame.style.width, 10);
  winCtrl.oldHeight = parseInt(this.parentWindow.clientArea.style.height, 10);

  // Set document to capture mousemove and mouseup events.

  if (browser.isIE) {
    document.onmousemove = winResizeDragGo;
    document.onmouseup   = winResizeDragStop;
  }
  if (browser.isNS) {
    document.addEventListener("mousemove", winResizeDragGo,   true);
    document.addEventListener("mouseup"  , winResizeDragStop, true);
    event.preventDefault();
  }
  //like the move drag, it sets up capturing for the mousemove and mouseup events at the document level and sets a flag in winCtrl to indicate that the resize drag has started
  winCtrl.inResizeDrag = true;
}
//_____________________________________________________________________________________________________________________________________________________________________
//
function winResizeDragGo(event) {
 var north, south, east, west;
 var dx, dy;
 var w, h;
  if (!winCtrl.inResizeDrag)
    return;
  // Set direction flags based on original resize direction.
  north = false;
  south = false;
  east  = false;
  west  = false;
  //uses charAt() String method to check the direction ex:n, ne, sw etc...
  if (winCtrl.resizeDirection.charAt(0) == "n")
    north = true;
  if (winCtrl.resizeDirection.charAt(0) == "s")
    south = true;
  if (winCtrl.resizeDirection.charAt(0) == "e" || winCtrl.resizeDirection.charAt(1) == "e")
    east = true;
  if (winCtrl.resizeDirection.charAt(0) == "w" || winCtrl.resizeDirection.charAt(1) == "w")
    west = true;
  // Find change in cursor position.
  //checks the current cursor position to calculate the distance it has moved in both the horizontal and vertical directions from its starting position
  if (browser.isIE) {
    dx = window.event.x - winCtrl.xPosition;
    dy = window.event.y - winCtrl.yPosition;
  }
  if (browser.isNS) {
    dx = event.pageX - winCtrl.xPosition;
    dy = event.pageY - winCtrl.yPosition;
  }
  // If resizing north or west, reverse corresponding amount.
  if (west)
    dx = -dx;
  if (north)
    dy = -dy;
  // Check new size.
  w = winCtrl.oldWidth  + dx;
  h = winCtrl.oldHeight + dy;
  //If the new width or height are below the allowed minimum, they are set to equal the minimum.
  if (w <= winCtrl.active.minimumWidth) {
    w = winCtrl.active.minimumWidth;
    dx = w - winCtrl.oldWidth;
  }
  if (h <= winCtrl.active.minimumHeight) {
    h = winCtrl.active.minimumHeight;
    dy = h - winCtrl.oldHeight;
  }
  //it performs the resize, changing the frame (and, for IE, the client area) width for east-west resizes, and the client area height for north-south resizes
  // Resize the window. For IE, keep client area and frame widths in synch.
  if (east || west) {
    winCtrl.active.frame.style.width = w + "px";
    if (browser.isIE)
      winCtrl.active.clientArea.style.width = (w - winCtrl.active.widthDiff) + "px";
  }
  if (north || south)
    winCtrl.active.clientArea.style.height = h + "px";
  // Clip the title bar text, if necessary.
  if (east || west) {
    if (w < winCtrl.active.clipTextMinimumWidth)
      winCtrl.active.titleBarText.style.width = (winCtrl.minimizedTextWidth + w - winCtrl.active.minimumWidth) + "px";
    else
      winCtrl.active.titleBarText.style.width = "";
  }
  // For a north or west resize, move the window.
  if (west)
    winCtrl.active.frame.style.left = (winCtrl.oldLeft - dx) + "px";
  if (north)
    winCtrl.active.frame.style.top  = (winCtrl.oldTop  - dy) + "px";

  if (browser.isIE) {
          //Event bubbling is canceled to prevent the browser's normal action of creating and highlighting a selection on the page
          //ONLY IE boolean cancelBubble: propagate up the element container hierarchy. The false setting equivalent for W3C DOM is stopPropagation(), otherwise, true
    window.event.cancelBubble = true;
        //ONLY IE: returnValue: set to false any event to be returned to the main window when an IE dialog window (created via the showModalDialog method) closes
    window.event.returnValue = false;
  }
  if (browser.isNS)
    event.preventDefault();
}
//_____________________________________________________________________________________________________________________________________________________________________
//ends the drag operation when the mouse button is released
function winResizeDragStop(event) {
  winCtrl.inResizeDrag = false;
  // Remove mousemove and mouseup event captures on document.
  if (browser.isIE) {
    document.onmousemove = null;
    document.onmouseup   = null;
  }
  if (browser.isNS) {
    document.removeEventListener("mousemove", winResizeDragGo,   true);
    document.removeEventListener("mouseup"  , winResizeDragStop, true);
  }
}
//_____________________________________________________________________________________________________________________________________________________________________

//=============================================================================
// Utility functions.
//=============================================================================
//winFindByClassName() is a utility function that searches the child nodes of a given element to find one that is assigned a given CSS class
//since all of the window components use a specific style class, this can be used to locate the various window elements
//it will not only search the element's child nodes, but any child nodes they have as well,as soon as it finds a match, it returns that element node
function winFindByClassName(el, className) {
  var i, tmp;
  if (el.className == className)
    return el;
  // Search for a descendant element assigned the given class.
  for (i = 0; i < el.childNodes.length; i++) {
    tmp = winFindByClassName(el.childNodes[i], className);
    if (tmp != null)
      return tmp;
  }
  return null;
}
//_____________________________________________________________________________________________________________________________________________________________________
//_____________________________________________________________________________________________________________________________________________________________________
//=============================================================================
// Initialization code.
//=============================================================================
//This function automatically scans the document for any DIV elements using the window class and creates a Window object for each one found
var winList = new Array();
var winCtrl = new Object();
//winList array, for the array of Window objects
//winCtrl array which is used to hold various data values used throughout the code


//winInit() functions first sets some values in the winCtrl variable including the colors to be used for inactive windows, constants and flags used during the move and resize drag operations
function winInit() {
  var elList;
  // Initialize window control object.
  winCtrl.maxzIndex                        =   0; //maxzIndex is used for the z-index property of the currently active window (incremented every time the active window changes so it can be placed at the top of the stacking order)
  winCtrl.resizeCornerSize                 =  16; //resizeCornerSize property determines how close the cursor must be to a corner of a window to set off a diagonal resize drag
  winCtrl.minimizedTextWidth               = 100; //minimizedTextWidth determines how wide minimized windows will be
  winCtrl.inactiveFrameBackgroundColor     = "#c0c0c0";
  winCtrl.inactiveFrameBorderColor         = "#f0f0f0 #505050 #404040 #e0e0e0";
  winCtrl.inactiveTitleBarColor            = "#808080";
  winCtrl.inactiveTitleTextColor           = "#c0c0c0";
  winCtrl.inactiveClientAreaBorderColor    = "#404040 #e0e0e0 #f0f0f0 #505050";
  winCtrl.inactiveClientAreaScrollbarColor = "";
  winCtrl.inMoveDrag                       = false;
  winCtrl.inResizeDrag                     = false;
  // Initialize windows and build list. (find all the windows defined on the page and create Window objects for them)
  elList = document.getElementsByTagName("DIV");
  for (var i = 0; i < elList.length; i++) {
    if (elList[i].className == "window") {
      winList[elList[i].id] = new Window(elList[i]);
        }
  }
  //document.getElementsByTagName("DIV") gives a list of all the DIV elements on the page. The script then loops through these and looks for any that use the window CSS class
}


//_____________________________________________________________________________________________________
//SEE MAIN JS -> kcasa.js
//_____________________________________________________________________________________________________

//window.onload = winInit;  // run initialization code after page loads.
//sets the page onload event to run an initialization function as soon as the browser finishes loading the page

//*******************JAVASCRIPT INITIALIZING FUNCTIONS********************************************************
//functions initialize functions

window.onload=callfunctions;
function callfunctions() {
        winInit();
        formValidationOverRules();
        //document.onkeydown = changeVal;
        document.onkeyup = changeVal;
}

//
//*******************FORM VALIDATION FUNCTIONS****************************************************************************
//form onFocus and onBlur functions:
function formValidationOverRules() {
        if (!document.getElementsByTagName) {
                return;
        }
        var allInputs = document.getElementsByTagName("input");
        for (var i = 0; i<allInputs.length; i++) {
                //input type password fields
                //alert(allInputs[i].type);
                if (allInputs[i].type == 'password') {
                        allInputs[i].onfocus = function() {
                                //this.className = 'boxFocus';
                                this.style.backgroundColor = "#eee";
                                this.style.color = "#333";
                                if (this.value=='password') this.value='';
                        };
                        allInputs[i].onblur = function() {
                                //this.className = 'boxBlur';
                                this.style.backgroundColor = "#fff";
                if (this.value=='' && this.getAttribute("name")=="log_password") this.value='password';
                        };
                }
                if (allInputs[i].type != 'text')
                        continue;
                        //var allInput = allInputs[i];
                        allInputs[i].onfocus = function() {
                                //this.className = 'boxFocus';
                                this.style.backgroundColor = "#eee";
                                this.style.color = "#333";
                                if (this.value=='username') this.value='';
                        };
                        allInputs[i].onblur = function() {
                                //this.className = 'boxBlur';
                                this.style.backgroundColor = "#fff";
                                if (this.value=='' && this.getAttribute("name")=="log_username") this.value='username';
                        };
        }
        //textareas
        var allTextAreas = document.getElementsByTagName("textarea");
        for (var i = 0; i<allTextAreas.length; i++) {
                var allTextArea = allTextAreas[i];
                allTextAreas[i].onfocus = function() {
                        //this.className = 'boxFocus';
                        this.style.backgroundColor = "#eee";
                        this.style.color = "#333";
                };
                allTextAreas[i].onblur = function() {
                        //this.className = 'boxBlur';
                        this.style.backgroundColor = "#fff";
                };
        }
        //rel external --- overrides the target="_blank" tag:
        //Before:<a href="document.html" target="_blank">external link</a>
        //After:<a href="document.html" rel="external">external link</a>
        var anchors = document.getElementsByTagName("a");
        for (var i = 0; i<anchors.length; i++) {
                var anchor = anchors[i];
                if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") {
                        anchor.target = "_blank";
                }
        }
}


function displayHtml(div) {
        if (!document.getElementById) { return; }
        var o = document.getElementById(div);
        if (typeof(o) == "undefined" || o==null) { return; }
        var s=o.innerHTML;
        if (s==null || s.length==0) { return; }
        document.write('<a style="font-family:Verdana; font-size:9px; text-decoration:none; font-weight:normal; height:18px; background-image:url(./images/menusub.gif); background-repeat:no-repeat; padding-left:18px;" href="#" onClick="var d=document.getElementById(\'jssource'+div+'\').style; if(d.display==\'block\'){d.display=\'none\';this.innerHTML=\'[+ Mostra dettagli ]\';}else{d.display=\'block\';this.innerHTML=\'[- Nascondi dettagli ]\';} return false;">[+ Mostra dettagli ]</a><br />');
        document.write('<span id="jssource'+div+'" style="display:none;"><tt>'+s+'</tt></span>');
}

/**/
//functions for vertical hide menu:
function show(id) {
        if (document.getElementById) {
                document.getElementById(id).style.display = "block";
        } else if (document.all) {
                document.all[id].style.display = "block";
        } else if (document.layers) {
                document.layers[id].display = "block";
        }
}
function hide(id) {
        if (document.getElementById) {
                document.getElementById(id).style.display = "none";
        } else if (document.all) {
                document.all[id].style.display = "none";
        } else if (document.layers) {
                document.layers[id].display = "none";
        }
}

// apri finestra in popup
function openPopUp(nome_file,l,h,scroll) {
        //var l = screen.width-(screen.width/3);
        //var h = screen.height-(screen.height/3);

        var checked_stampa_scheda=false;
        var checked_stampa_img=false;
        if (document.getElementById('stampa_scheda_chk')) checked_stampa_scheda=document.getElementById('stampa_scheda_chk').checked;
        if (document.getElementById('stampa_img_chk')) checked_stampa_img=document.getElementById('stampa_img_chk').checked;

        var winleft;
        var winUp;
        if((!l && !h) || (l=='' && h=='')) {
                l=screen.width-12;
                h=screen.height-2;
                winleft = 0;
                winUp = 0;
        }else {
                winleft = (screen.width - l) / 2;
                winUp = (screen.height - h) / 2;
        }
        var dimensioni = 'width='+l+',height='+h+',left='+winleft+',top='+winUp+',scrollbars='+scroll+',resizable=yes';
        //var dimensioni= "width="+l+"px,height="+h+"px";

        if (nome_file.search(".jpg")>0) {
                var popup = window.open(nome_file,"",dimensioni);
        } else {
                var popup = window.open(nome_file+"&stampa_scheda_chk="+checked_stampa_scheda+"&stampa_img_chk="+checked_stampa_img,"",dimensioni);
        }

        popup.focus();
}
/*  // OLD codes
function ordinaBy(cerca,elenco,ordina,start,step) {
        document.location.href='index.php?cerca='+cerca+'&elenco='+elenco+'&ordineby='+ordina+'&start='+start+'&step='+step;
}

function ordinaBy_Ric(cerca,ids,count,ordina,start,step,id_imm,tipo_cli) {
        if (cerca=='') {
            document.location.href='xsearch.php?id_imm='+id_imm+'&tipo_cli='+tipo_cli+'&action=xsearchresult&cerca='+cerca+'&ids='+ids+'&count='+count+'&ordineby='+ordina+'&start='+start+'&step='+step+'#table_start';
        } else {
            document.location.href='index.php?cerca='+cerca+'&ids='+ids+'&count='+count+'&ordineby='+ordina+'&start='+start+'&step='+step+'#table_start';
        }
}
// per ordinamento dei clienti nella selezione, quando si inserisce un nuovo immobile
// per ora è in stand-by
function ordinaBy_InsCli(cerca,ids,count,ordina,start,step) {
        document.location.href='index.php?inserisci=cliente&ids='+ids+'&count='+count+'&ordineby='+ordina+'&start='+start+'&step='+step;
}
*/
function ordinaBy(cerca,elenco,ordina,start,step) {
        document.location.href='index.php?cerca='+cerca+'&elenco='+elenco+'&ordineby='+ordina+'&start='+start+'&step='+step;
}

function ordinaBy_Ric(cerca,count,ordina,start,step,id_imm,tipo_cli,elenco,id_search,prezzo,tipo_elenco_ric) {
        document.location.href='index.php?cerca='+cerca+'&count='+count+'&ordineby='+ordina+'&elenco='+elenco+'&start='+start+'&step='+step+'&id_search='+id_search+'&tipo_elenco_ric='+tipo_elenco_ric;
}


function setValues(index,id)
{
 if (index=='undefined' || index=='') {
    index='';
   if (new_select.length>0) new_select[index]="<option value=''>- Seleziona -</option>";
   if (new_select_x.length>0) new_select_x[index]="<option value=''>- Seleziona -</option>";
   if (new_select_age.length>0) new_select_age[index]="<option value=''>- Seleziona -</option>";
   if (new_selectSM.length>0) new_selectSM[index]="<option value=''>- Seleziona -</option>";
 }

 // tiopologia e sottotipologia

 if (id=='id_tim~textnum') {
     if (navigator.appName=='Netscape') { id_sim_textnum=document.getElementById('id_sim_textnum'); }
     id_sim_textnum.innerHTML ='<span class="listbox"><select name="id_sim~textnum" id="id_sim~textnum" class="selectbox" size="1">'+new_selectSM[index]+'</select></span>';
     }
 if (id=='rif_tipologia_imm~textnum') {
     if (navigator.appName=='Netscape') rif_sub_tipologia_imm_textnum=document.getElementById('rif_sub_tipologia_imm_textnum');
     rif_sub_tipologia_imm_textnum.innerHTML ='<span class="listbox"><select name="rif_sub_tipologia_imm~textnum" id="rif_sub_tipologia_imm~textnum" class="selectbox" size="1">'+new_select_x[index]+'</select></span>';
     }
 if (id=='id_tim') {
     if (navigator.appName=='Netscape') id_sim=document.getElementById('id_sim');
     id_sim.innerHTML='<span class="listbox"><select name="id_sim" id="id_sim" class="selectbox" size="1">'+new_select[index]+'</select></span>';
     }
 if (id=='rif_tipologia_imm') {
     if (navigator.appName=='Netscape') rif_sub_tipologia_imm=document.getElementById('rif_sub_tipologia_imm');
     rif_sub_tipologia_imm.innerHTML ='<span class="listbox"><select name="rif_sub_tipologia_imm" id="rif_sub_tipologia_imm" class="selectbox" size="1">'+new_select_x[index]+'</select></span>';
     }
 // tiopologia e sottotipologia  MULTIPLO
 if (id=='id_tim~textnumSM') {
     if (navigator.appName=='Netscape')  id_sim_textnumSM=document.getElementById('id_sim~textnumSM[]');
     id_sim_textnumSM.innerHTML ='<span class="listbox"><select multiple name="id_sim~textnumSM[]" id="id_sim~textnumSM[]" class="Mselectbox" size="6">'+new_selectSM[index]+'</select></span>';
     }
 if (id=='id_timSM') {
     if (navigator.appName=='Netscape')  id_simSM=document.getElementById('id_simSM');
     id_simSM.innerHTML ='<span class="listbox"><select multiple name="id_sim[]" id="id_sim[]" class="Mselectbox" size="6">'+new_selectSM[index]+'</select></span>';
     }
/*
 if (id=='rif_tipologia_imm~textnum') {
     if (navigator.appName=='Netscape') rif_sub_tipologia_imm_textnum=document.getElementById('rif_sub_tipologia_imm_textnum');
     rif_sub_tipologia_imm_textnum.innerHTML ='<span class="listbox"><select name="rif_sub_tipologia_imm~textnum" id="rif_sub_tipologia_imm~textnum" class="selectbox" size="1">'+new_select_x[index]+'</select></span>';
     }
 if (id=='id_tim') {
     if (navigator.appName=='Netscape') id_sim=document.getElementById('id_sim');
     id_sim.innerHTML='<span class="listbox"><select name="id_sim" id="id_sim" class="selectbox" size="1">'+new_select[index]+'</select></span>';
     }
 if (id=='rif_tipologia_imm') {
     if (navigator.appName=='Netscape') rif_sub_tipologia_imm=document.getElementById('rif_sub_tipologia_imm');
     rif_sub_tipologia_imm.innerHTML ='<span class="listbox"><select name="rif_sub_tipologia_imm" id="rif_sub_tipologia_imm" class="selectbox" size="1">'+new_select_x[index]+'</select></span>';
     }
  */
 // agenzie ed agenti
 if (id=='id_agz~textnum') {
     if (navigator.appName=='Netscape') id_agente_textnum=document.getElementById('id_agente_textnum');
     id_agente_textnum.innerHTML ='<span class="listbox"><select name="id_age~textnum" id="id_age~textnum" class="selectbox" size="1">'+new_select_age[index]+'</select></span>';
     }
/*
 if (id=='rif_tipologia_imm~textnum') {
     if (navigator.appName=='Netscape') rif_sub_tipologia_imm_textnum=document.getElementById('rif_sub_tipologia_imm_textnum');
     rif_sub_tipologia_imm_textnum.innerHTML ='<span class="listbox"><select name="rif_sub_tipologia_imm~textnum" id="rif_sub_tipologia_imm~textnum" class="selectbox" size="1">'+new_select_x[index]+'</select></span>';
     }
*/
 if (id=='id_agz') {
     if (navigator.appName=='Netscape') id_agente=document.getElementById('id_agente');
     id_agente.innerHTML='<span class="listbox"><select name="id_age" id="id_age" class="selectbox" size="1">'+new_select_age[index]+'</select></span>';
     }
/*
 if (id=='rif_tipologia_imm') {
     if (navigator.appName=='Netscape') rif_sub_tipologia_imm=document.getElementById('rif_sub_tipologia_imm');
     rif_sub_tipologia_imm.innerHTML ='<span class="listbox"><select name="rif_sub_tipologia_imm" id="rif_sub_tipologia_imm" class="selectbox" size="1">'+new_select_x[index]+'</select></span>';
     }
*/

}


function changeVal(e) {
     if (!e) e = window.event;
     var keyInfo = String.fromCharCode(e.keyCode);
     var ereg_exp = /\d/g;

     // calcola i punti solo se il carattere immesso è un numero o se si preme canc o backspace
     if (keyInfo.search(ereg_exp)>=0 || e.keyCode==8 || e.keyCode==46) {
     //********************
     var j;
     var k;
     var campi = new Array("rendita_dc2","canone_dei","prezzo_dei","rata_mutuo_dei","importo_restante_mutuo_dei","rendita_catastale_mutuo_dei","deposito_cauzionale_mutuo_dei","fidejussione_mutuo_dei","spese_condom_annuali_dei","spese_condom_mensili_dei","prezzo_amministratore_dei","prezzo_dei~textnum","canone_dei~textnum","prezzo_mq_dei","prezzo_mq_dei~textnum","rata_mutuo_dei~textnum","importo_restante_mutuo_dei~textnum","rendita_catastale_mutuo_dei~textnum","deposito_cauzionale_mutuo_dei~textnum","fidejussione_mutuo_dei~textnum","spese_condom_mensili_dei~textnum","spese_condom_annuali_dei~textnum","prezzo_amministratore_dei~textnum");
     for(j=0;j<document.forms.length;j++) {
          var ele_s=document.forms[j].elements;
          for(k=0;k<ele_s.length;k++) {
            if (ele_s[k].type=='text') {
                         var y;
                         for(y=0;y<campi.length;y++) {
                             if (campi[y]==ele_s[k].name) {
                                                  // sniffig browser
                                                  // ie 5.5+
                                                  if (browser.isIE) {
                                                     var s1 = document.getElementById(ele_s[k].name).value;
                                                  } else {
                                                  // netscape/firefox...
                                                     var s1 = document.all(ele_s[k].name).value;
                                                  }
                                                  var ereg_exp = /\D/g;
                                                  var s2 = s1.replace(ereg_exp,'');
                                                  //var s2 = s1.replace(".","");
                                                  var s1l = s2.length;
                                                  // se è divisibile per 3...
                                                  if (s1l>2) {
                                                       var i;
                                                       var newstring = new Array();
                                                       var conta=0;
                                                       var conta_x3=0;
                                                       for(i=(s1l-1);i>=0;i--) {
                                                          // controllare quando mettere il punto
                                                          if (conta_x3==3) {
                                                               newstring[conta]='.';
                                                               conta_x3=0;
                                                               conta++;
                                                          }
                                                          newstring[conta]=s2.substr(i,'1');
                                                          conta++;
                                                          conta_x3++;
                                                       }
                                                       newstring.reverse();
                                                       var s4 = newstring.join('');
                                                       s1 = s4.replace(",","");
                                                  }
                                                  // sniffig browser
                                                  // ie 5.5+
                                                  if (browser.isIE) {
                                                     document.getElementById(ele_s[k].name).value = s1;
                                                  } else {
                                                  // netscape/firefox...
                                                     document.all(ele_s[k].name).value = s1;
                                                  }

                                }
                         }
               }
            }
          }
          //********************
          }
}
