// Javascript from Moodle modules
// The maxattachment limit should be enforced both in javascript
// and in php.
// Taken from email sendmail.php js code with a few modifications

var upload_number = 1;
function addFileInput(txt,max) {
	
	if (upload_number != max ) {
    	var d = document.createElement("div");
    	d.setAttribute("id", "id_FILE_"+upload_number);
    	var file = document.createElement("input");
    	file.setAttribute("type", "file");
    	file.setAttribute("name", "FILE_"+upload_number);
    	file.setAttribute("id", "FILE_"+upload_number);
		//    file.setAttribute("onchange", "addFileInput('"+txt+"')");
    	d.appendChild(file);
    	var a = document.createElement("a");
    	a.setAttribute("href", "javascript:removeFileInput('id_FILE_"+upload_number+"');");
    	a.appendChild(document.createTextNode(txt));
    	d.appendChild(a);
    	document.getElementById("id_FILE_"+(upload_number-1)).parentNode.appendChild(d);
    	upload_number++;
	} else {
		alert("You are at your max attachment size of " + max);	
	}
}

function removeFileInput(i) {
    var elm = document.getElementById(i);
    document.getElementById(i).parentNode.removeChild(elm);
    upload_number--;
}

/**
 * JavaScript for checking or unchecking 
 * all the students or all students in a group.
 *
 * @param toggle Check All/None
 * @param start the first checkbox to be changed
 * @param end the last checkbox to be changed
 * return boolean
 **/
function block_quickmail_toggle(toggle, start, end) {
    // Element ID
    var id = 'mailto'+start;

    // iterate through all of the appropriate checkboxes and change their state
    while(document.getElementById(id) && start != end) {
        document.getElementById(id).checked = toggle;
        start++;
        id = 'mailto'+start;
    }

    return false;
}
