/* This is the Javascript to be included when rendering the gradebook overview. */
var defaulttext = 'Changes are not saved as they are typed. Click "Save" to save them.';
var numstudents = <tal:block replace="python: len(view.students)"/> 
var students = new Array(numstudents);
<tal:loop repeat="student view/students">
     students[<tal:block replace="repeat/student/index"/>] = '<tal:block replace="python: view.breakJSString(student.username)"/>';
     </tal:loop>

var numscores = <tal:block replace="python: len(view.scores)"/>;
var scores = new Object();
<tal:loop repeat="activity view/scores">
    scores['<tal:block replace="activity" />'] = new Array(<tal:block replace="python: view.scores[activity]" />);
</tal:loop>

var edited = false;

/*function unloadWarn()
{
    /// XXX Not compatible with all browsers XXX
    return false;
}

window.onbeforeunload = unloadWarn;
*/
function setWarning(text, bg){
    document.getElementById('warningtext').innerHTML = text;
    changeBackgroundColor('warning', bg);
}

function checkValid(e,name){
    var activity = name.split('_')[0];
    if(activity == "fd")
        activity = name.split('_')[1];
    edited = true;
    if(e!=null)  // Make sure the element is selected after movement
	{
	    var keynum;
	    if(window.event) // IE
		{ 
		    keynum = e.keyCode;
		}
	    else if(e.which) // Netscape/Firefox/Opera
		{
		    keynum = e.which;
		}
	    if ((keynum==13)||(keynum==37)||(keynum==38)||(keynum==39)||(keynum==40) )
		{
		    document.getElementById(name).select();
		    return true;
		}
	}
    var element = document.getElementById(name);
    var elementCell = document.getElementById(name+'_cell');
    var value = element.value;
    setWarning(defaulttext, 'default_bg');
    changeBackgroundColor(name+'_cell', 'default_bg');

    if (value == '')
        return true;

    // handle validation of discrete score system
    var actScores = scores[activity];
    if (actScores[0] == 'd')
    {
        for(var index in actScores)
        {
               if (index > 0 && value == actScores[index])
                   return true;
        }  
        changeBackgroundColor(name+'_cell', 'error_bg');
        return false;   
    }

    // handle validation of ranged score system
    else
    {
        var min = parseInt(actScores[1]);
        var max = parseInt(actScores[2]);
        var intValue = parseInt(value);
        var regex = /[0-9]+$/;
        if (!value.match(regex) || intValue < min)
        {
            changeBackgroundColor(name+'_cell', 'error_bg');
            return false;   
        }
        if (intValue > max)
        {
            changeBackgroundColor(name+'_cell', 'warning_bg');
            return true;
        }
    }

    changeBackgroundColor(name+'_cell', 'changed_bg');
    return true;    
} 



function performFillDown(activity) {
    var fd = document.getElementById('fd_'+activity);
    if (fd.value=='') return false;
    changeBackgroundColor('fd_'+activity+'_cell', 'default_bg');
    if (checkValid(null, 'fd_'+activity) == false)  {
    	changeBackgroundColor('fd_'+activity+'_cell', 'warning_bg');
	    setWarning('You may not apply the fill-down to an invalid grade', 'error_bg');
	    return false;
	}
    for(j=0;j!=numstudents;j++) {
    	document.getElementById(activity+'_'+students[j]).value=fd.value;
	    checkValid(null,activity+'_'+students[j]);
    }
    document.getElementById('fd_'+activity).value = '';
    document.getElementById('fdbtn_'+activity).style.display = 'none';
    changeBackgroundColor('fd_'+activity+'_cell', 'default_bg');
    setWarning(defaulttext, 'default_bg');
}

function checkFillDown(activity)
{
    var fd = document.getElementById('fd_'+activity);
    if (checkValid(null, 'fd_'+activity))
	{
	    var el = document.getElementById('fdbtn_'+activity);
	    el.style.display='block';
	    if (document.getElementById('fd_'+activity).value=='')  {
	    	changeBackgroundColor('fd_'+activity+'_cell', 'default_bg');
	    	el.style.display='none';
	    }
	}
    else
	{
	    document.getElementById('fdbtn_'+activity).style.display='none';
	    if (document.getElementById('fd_'+activity).value=='')  {
    		changeBackgroundColor('fd_'+activity+'_cell', 'default_bg');
		}
	}
}

function changeBackgroundColor(id, class)    {
    obj = document.getElementById(id);
    $(obj).removeClass('default_bg');
    $(obj).removeClass('changed_bg');
    $(obj).removeClass('warning_bg');
    $(obj).removeClass('error_bg');
    $(obj).addClass(class);
}
