
/*
 * Function to be called by the form that requires spell checking.
 *
 * The caller should pass in an array of form elements that require spell checking.
 */
var params = '';
var index = 0;
var retValues = new Array(2);
var pos = 0;

function pippo() {
  try{
      if(newWin)  newWin.focus();
    }catch(e){ }
    }

function startSpellCheck( baseUrl,objHtml,idlang )  {
  pos = 0;
  params ='?idlang='+idlang+'&op=1';
  var contentWindow = document.getElementById('wysiwygareaText').contentWindow;
  var getDocument = contentWindow.document;
  retValues[0] = params;
  retValues[1] = index;
  retValues = domInspection(getDocument.body, 'getDocument.body', retValues);
  openCenteredWindow( baseUrl + 'spellcheck-entry.jsp' + retValues[0], 400, 300 );
  window.onfocus = pippo;
}

function domInspection(node, domPath, retValues) {
  if (node!=null && node.childNodes!=null) {
    for (var i = 0; i < node.childNodes.length; i++) {
      var child = node.childNodes[i];
      var myDomPath = domPath + '.childNodes['+i+']';
      if (child.nodeType == 3) {
        var params = retValues[0];
        var index = retValues[1];
        params = params + '&element_' + index + '=' + myDomPath + '&abs_pos_'+index+'=' + pos;
        index++;
        retValues[0] = params;
        retValues[1] = index;
        pos += child.nodeValue.toString().length;
      } else {
        retValues = domInspection(child, myDomPath, retValues);
      }
    }
  }
  return retValues;
}



/*
 * Finds the index of the form in the document.forms collection.
 *
 * This is necessary because you can't get the form name from form.name if
 *  there is a field in that form called name.
 */
function getFormsNumber( form ) {
    var contentWindow = document.getElementById('wysiwygareaText').contentWindow;
    var getDocument = contentWindow.document;
    var forms = getDocument.forms;
    for( var x = 0; x < forms.length; x++ ) {
        if( forms[x] == form )
            return x;
    }
    return -1; // Form not found
}


/*
 * Function to open a window in the middle of the current window.
 *
 */
var newWin;
function openCenteredWindow( url, width, height ) {

    var top = (getWindowDimensionH()/2)-(height/2);
    var left = (getWindowDimensionW()/2)-(width/2);

    newWin = window.open( url,"spellcheck","height=" + (height) + ",width=" + (width) +",left=" +left+",top="+top+",location=no,menubar=no,resizable=no,scrollbars=no,status=no,titlebar=yes,toolbar=no");
    return newWin;
}


