
function submitForm(action){
     switch (action) {
        case 'EDIT':
            if (!checkMandatory())
               return false;
            obj = document.formEdit;
            break;
        case 'SEARCH':
            obj = document.formSearch;
            break;
        default:
            return false;
    }
    obj.submit();
    return true;
}
function selectAll(obj) {
  // per tutti gli elementi della select
  for (i=0; i < obj.length; i++) {
      // seleziona tutti gli altri elementi della select
      obj[i].selected = true;
  }
}

function submitSearch(ordine)
{
    var newOrder;
    oldOrderField =  document.formSearch.order.value;

    if (ordine == oldOrderField) {
        if (document.formSearch.orderAsc.value == '1')
            document.formSearch.orderAsc.value = '0';
        else
            document.formSearch.orderAsc.value = '1';
    }
    else
        document.formSearch.orderAsc.value = '1';
    document.formSearch.order.value = ordine;
    document.formSearch.submit();
    return true;
}

function closeWindow(){
  this.close();
  return true;
}
function confirmDelete() {
         return confirm('Vuoi veramente cancellare il record?');
}

function cancelItem(action, fieldName, defaultValue) {
     switch (action) {
        case 'EDIT':
            obj = document.formEdit;
            break;
        case 'SEARCH':
            obj = document.formSearch;
            break;
        default:
            return;
    }
    for (finito = false, i = 0; (!finito) && i < obj.length; i++ ) {
          if ( obj.elements[i].name == fieldName ) {
               obj = obj.elements[i];
               finito = true;
          }
     }
     if (finito) {
          x = new Option(defaultValue, "");
          obj.options[0] = x;
     }
}


function pushBack( fieldName, descrizione, id, bGroundAction) {
         window.opener.AddInSelect( fieldName, descrizione, id, bGroundAction);
         this.close();
         return true;
}

function openWindow(page4List, bGroundAction, tableName, bGroundKeyName, bGroundDescName) {
         urlPage = page4List+'?bGround=1&view='+tableName+'&bGroundAction='+bGroundAction+'&bGroundKeyName='+bGroundKeyName+'&bGroundDescName='+bGroundDescName;
         return window.open(urlPage, '', 'menubar=no,scrollbars=yes,toolbar=no,resizable=yes,width=700,height=500' );
}

function AddInSelect( fieldName, descrizione, id, bGroundAction) {
         x = new Option( descrizione, id);
         switch (bGroundAction) {
            case 'EDIT':
                obj = document.formEdit;
                break;
            case 'SEARCH':
                obj = document.formSearch;
                break;
            default:
             return;
         }
         for (finito = false, i = 0; (!finito) && i < obj.length; i++ ) {
              if ( obj.elements[i].name == fieldName ) {
                   obj = obj.elements[i];
                   finito = true;
              }
         }
         if (finito)
            obj.options[0] = x;

}

var cntMandatory=0;
var vMandatory = new Array();

function addMandatory(title, name) {
   vMandatory[cntMandatory] = new Array();
   vMandatory[cntMandatory][0] = title;
   vMandatory[cntMandatory][1] = name;
   cntMandatory++;

}

function checkMandatory() {
  objForm = document.formEdit;
  for (i=0; i<cntMandatory; i++) {
     obj = objForm.elements[vMandatory[i][1]]
     if (obj && !obj.value) {
       alert('Il campo '+vMandatory[i][0]+' non puņ essere vuoto');
       return false;
     }
  }

  return true;
}

function ShowImage(objImg, path) {
    objImg.src = path;
}

function setStyle(style) {
    var testo;
    var startTag, stopTag;

    switch(style) {

    case 1:
        startTag = "<b>";
        stopTag = "</b>";
        break;
    case 2:
        startTag = "<i>";
        stopTag = "</i>";
        break;
    case 3:
        startTag = "<u>";
        stopTag = "</u>";
        break;
    case 4:
        startTag = "<sup>";
        stopTag = "</sup>";
        break;
    case 5:
        startTag = "<sub>";
        stopTag = "</sub>";
        break;
    default:
        startTag ="";
        stopTag = "";
    }
    testo = document.selection.createRange();
    testo.text = startTag + testo.text + stopTag;
}

function Preview(control) {

    newwin = window.open('','','top=150,left=150,width=300,height=200,resizable=yes,scrollbars=yes');

    if (!newwin.opener)
        newwin.opener = self;

    with (newwin.document) {
        open();
        write('<html>');
        write('<body><br><table border="0" cellpadding="0" cellspacing="0"><tr><td>' + control.value + '</td></tr></table><br>');
        write('<p>');
        write('<center>');
        write('<input type=button value="Ok" OnClick="window.close()" id=button1 name=button1>');
        write('</center>');
        write('</body>');
        write('</html>');
    }
}

function setPointer(theRow, theAction, theDefaultColor, thePointerColor, theMarkColor)
{
    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');
    }
    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

    // 3.3 ... Opera changes colors set via HTML to rgb(r,g,b) format so fix it
    if (currentColor.indexOf("rgb") >= 0)
    {
        var rgbStr = currentColor.slice(currentColor.indexOf('(') + 1,
                                     currentColor.indexOf(')'));
        var rgbValues = rgbStr.split(",");
        currentColor = "#";
        var hexChars = "0123456789ABCDEF";
        for (var i = 0; i < 3; i++)
        {
            var v = rgbValues[i].valueOf();
            currentColor += hexChars.charAt(v/16) + hexChars.charAt(v%16);
        }
    }

    // 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 == 'unclick') {
            newColor              = 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;
} // end of the 'setPointer()' function
