/*

 spellcheck-caller.js - This file needs to be included on the page that
 contains the fields that require spell checking.

 See spellcheck.license.txt.

 */

/*
 * 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.
 */
function startSpellCheck(baseUrl, /* Array of Form Elements */ elements)
{
    var params = '?op=1';

    for (var x = 0; x < elements.length; x++) {
        var form = elements[x].form;
        var formsNumber = getFormsNumber(form.id);
        params = params + '&element_' + x + '=forms[' + formsNumber + '].elements[\'' + elements[x].name + '\']';
    }

    openCenteredWindow(baseUrl + 'spellcheck-entry.jsp' + params, 300, 200);

}

/*
 * 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(formId) {
    var forms = document.forms;
    for (var x = 0; x < forms.length; x++) {
        if (forms[x].id == formId)
            return x;
    }

    return -1; // Form not found
}


/*
 * Function to open a window in the middle of the current window.
 *
 */
function openCenteredWindow(url, width, height)
{
    var left = 0;
    var top = 0;
    
    var myWidth;
    var myHeight;
    var x;
    var y;

    if( typeof( window.innerWidth ) == 'number' ) { 
    	//Non-IE 
    	myWidth = window.innerWidth;
    	myHeight = window.innerHeight;
    	x = window.top.screenX;
    	y = window.top.screenY;
    } else if( document.documentElement &&  ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) { 
    	//IE 6+ in 'standards compliant mode' 
    	myWidth = document.documentElement.clientWidth; 
    	myHeight = document.documentElement.clientHeight;
    	x = window.top.screenLeft;
    	y = window.top.screenTop;
    } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) { 
    	//IE 4 compatible 
    	myWidth = document.body.clientWidth; 
    	myHeight = document.body.clientHeight;
    	x=0;
    	y=0;
    }    
    top =  y+(myHeight/ 2) - (height / 2);
    left = x+(myWidth/2) - (width / 2);
    
    var newWin = window.open(url, "", "height=" + (height) + ",width=" + (width) + ",left=" + left + ",top=" + top + ",location=no,menubar=no,resizable=no,scrollbars=no,status=no,titlebar=yes,toolbar=no");
    return newWin;
}


