/**
 * This array is used to remember mark status of rows in browse mode
 */
var marked_row = new Array;


/**
 * Sets/unsets the pointer and marker in browse mode
 *
 * @param   object    the table row
 * @param   interger  the row number
 * @param   string    the action calling this script (over, out or click)
 * @param   string    the default background color
 * @param   string    the color to use for mouseover
 * @param   string    the color to use for marking a row
 *
 * @return  boolean  whether pointer is set or not
 */
function setPointer(theRow, theRowNum, 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

    // 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;
            marked_row[theRowNum] = true;
        }
    }
    // 4.1.2 Current color is the pointer one
    else if (currentColor.toLowerCase() == thePointerColor.toLowerCase()
             && (typeof(marked_row[theRowNum]) == 'undefined' || !marked_row[theRowNum])) {
        if (theAction == 'out') {
            newColor              = theDefaultColor;
        }
        else if (theAction == 'click' && theMarkColor != '') {
            newColor              = theMarkColor;
            marked_row[theRowNum] = true;
        }
    }
    // 4.1.3 Current color is the marker one
    else if (currentColor.toLowerCase() == theMarkColor.toLowerCase()) {
        if (theAction == 'click') {
            newColor              = (thePointerColor != '')
                                  ? thePointerColor
                                  : theDefaultColor;
            marked_row[theRowNum] = (typeof(marked_row[theRowNum]) == 'undefined' || !marked_row[theRowNum])
                                  ? true
                                  : null;
        }
    } // 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

/** 
 * getElement
 */ 
function getElement(e,f){
    if(document.layers){
        f=(f)?f:self;
        if(f.document.layers[e]) {
            return f.document.layers[e];
        }
        for(W=0;i<f.document.layers.length;W++) {
            return(getElement(e,fdocument.layers[W]));
        }
    }
    if(document.all) {
        return document.all[e]; 
    }
    return document.getElementById(e);
}

function copyGRE(formname,checked,
        gre_date,verbal_score,quant_score,analyt_score,analyt_writ,
        verbal_pct,quant_pct,analyt_pct,source) {
//  var e = document.forms[formname].elements;
    var e = formname.elements;
    var msg = 'Copy these ' + source + ' test scores and mark as official?';
    if (e && checked) {
    if (confirm(msg)) {
        e['gre[gre_date]'].value =     gre_date;
        e['gre[verbal_score]'].value = verbal_score;
        e['gre[quant_score]'].value =  quant_score;
        e['gre[analyt_score]'].value = analyt_score;
        e['gre[analyt_writ]'].value =  analyt_writ;
        e['gre[verbal_pct]'].value =   verbal_pct;
        e['gre[quant_pct]'].value =    quant_pct;
        e['gre[analyt_pct]'].value =   analyt_pct;
        e['gre[gre_official]'].checked = true;
        return false;
    }
    }
    return false;
}

function copyTOEFL(formname,checked,
	toefl_date,toefl_type,toefl_score,toefl_listening,
	toefl_reading,toefl_writing,toefl_essay_speak,source) {
//  var e = document.forms[formname].elements;
    var e = formname.elements;
    var msg = 'Copy these ' + source + ' TOEFL scores and mark as official?';
    if (e && checked) {
    if (confirm(msg)) {
        e['toefl[toefl_date]'].value = toefl_date;
        e['toefl[toefl_type]'].selectedIndex = 
		arraySearch(e['toefl[toefl_type]'].options,toefl_type);
        e['toefl[toefl_score]'].value = toefl_score;
        e['toefl[toefl_listening]'].value = toefl_listening;
        e['toefl[toefl_reading]'].value = toefl_reading;
        e['toefl[toefl_writing]'].value = toefl_writing;
        e['toefl[toefl_essay_speak]'].value = toefl_essay_speak;
        e['toefl[toefl_official]'].checked = true;
        return false;
    }
    }
    return false;
}

function arraySearch(ary,val) {
    for (var i = 0; i < ary.length; i++) {
	if (ary[i].text == val) {
	    return i;
	}
    }
    return -1;
}

function copyIELTS(formname,checked,
	ielts_date,ielts_total,ielts_listening,ielts_reading,
	ielts_writing,ielts_speaking,source) {
//  var e = document.forms[formname].elements;
    var e = formname.elements;
    var msg = 'Copy these ' + source + ' IELTS scores and mark as official?';
    if (e && checked) {
    if (confirm(msg)) {
        e['gre[ielts_date]'].value = ielts_date;
        e['gre[ielts_total]'].value = ielts_total;
        e['gre[ielts_listening]'].value = ielts_listening;
        e['gre[ielts_reading]'].value = ielts_reading;
        e['gre[ielts_writing]'].value = ielts_writing;
        e['gre[ielts_speaking]'].value = ielts_speaking;
        e['gre[ielts_official]'].checked = true;
        return false;
    }
    }
    return false;
}

function copyTEST(formname,checked,what,date,score,source) {
//  var e = document.forms[formname].elements;
    var e = formname.elements;
    var msg = 'Copy these ' + source + ' test scores and mark as official?';
    if (e) {
    if (confirm(msg)) {
	var kdate  = what + '[' + what + '_date]';
	var kscore = what + '[' + what + '_score]';
	var koffl = what + '[' + what + '_official]';
	e[kdate].value   = date;
	e[kscore].value  = score;
	e[koffl].checked = true;
    }
    }
    return false;
}

function setSelectOptions(the_form, the_select, do_check)
{
    var selectObject = document.forms[the_form].elements[the_select];
    var selectCount  = selectObject.length;

    for (var i = 0; i < selectCount; i++) {
	selectObject.options[i].selected = do_check;
    } // end for

    return true;
}

function setSelectOptionsNonzero(the_form, the_select, do_check)
{
    var selectObject = document.forms[the_form].elements[the_select];
    var selectCount  = selectObject.length;

    for (var i = 0; i < selectCount; i++) {
	if (selectObject.options[i].value != 0)
	    selectObject.options[i].selected = do_check;
	else
	    selectObject.options[i].selected = !do_check;
    } // end for

    return true;
}
