// Set used from/to dates to max range with one click.  Probably
//  on test sites only.

function URSetMaxUsedDates() {
    var f = document.forms.updateresources;
    for (var i = 0; i < f.length; i++) {
	var e = f.elements[i];
	if (e.name == 'action.useddates') {
	    e.checked = true;
	}
	else if (e.type == 'select-one') {
	    //	    alert(e.name);
	    if (e.name == 'value.uf.day') {
		e.selectedIndex = 1;
	    }
	    else if (e.name == 'value.uf.month') {
		e.selectedIndex = 1;
	    }
	    else if (e.name == 'value.uf.year') {
		e.selectedIndex = 1;
	    }
	    else if (e.name == 'value.ut.day') {
		e.selectedIndex = e.options.length - 1;
	    }
	    else if (e.name == 'value.ut.month') {
		e.selectedIndex = e.options.length - 1;;
	    }
	    else if (e.name == 'value.ut.year') {
		e.selectedIndex = e.options.length - 1;;
	    }
	}
    }
}


// Mark or clear all item checkboxes on update resources form

function URMarkAll(b) {
    f = document.forms.updateresources;
    for (var i = 0; i < f.length; i++) {
	var e = f.elements[i];
	if (e.type == 'checkbox') {
	    if (e.name.substr(0,5) == 'item.') {
		e.checked = b;
	    }
	}
    }
}

// Method called from BODY onLoad handler to try and give
// focus to the first text input field on the page.
// Called RSLFocus to prevent any possible clashes and confusion
// with other methods called focus().

function RSLFocus() {
  for (i = 0; i < document.forms.length; i++) {
    f = document.forms[i];
    for (j = 0; j < f.length; j++) {
      if (f.elements[j].type == 'text') {
        f.elements[j].focus();
        return;
      }
    }
  }
}



// Check a string for blankness

function isBlank(s) {
    for (var i = 0; i < s.length; i++) {
        var c = s.charAt(i);
        if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
    }
    return true;
}


// Check that the create/update list form has been filled in properly

function validateModListForm(f) {
    if (isBlank(f.name.value)) {
	alert('Please enter the module name');
	f.name.focus();
	return false;
    }
    if (f.campus.selectedIndex == 0 && isBlank(f.newcampus.value)) {
	alert('Please select a campus or enter a new one.');
	return false;
    }
    if (f.campus.selectedIndex > 0 && !isBlank(f.newcampus.value)) {
	alert('Please select a campus OR enter a new one. Not both.');
	return false;
    }
    if (isBlank(f.code.value)) {
	return confirm("A temporary module code will be generated.");
    }
    if (isBlank(f.studentnos.value)) {
	alert('Please tell us roughly how many students will be taking this course');
	return false;
    }
    var v = parseFloat(f.studentnos.value);
    if (isNaN(v)) {
	alert(f.studentnos.value+' is not a valid number');
	f.studentnos.focus();
	return false;
    }
    if (v <= 0) {
	alert('Please enter a positive number');
	f.studentnos.focus();
	return false;
    }
    return true;
}



// Check that the user registration/update form has been filled in properly

function validateRegisterForm(f, mod) {
    if (f.faculty.selectedIndex == 0 && isBlank(f.newfaculty.value)) {
	alert('Please select a faculty or enter a new one.');
	return false;
    }
    if (f.faculty.selectedIndex > 0 && !isBlank(f.newfaculty.value)) {
	alert('Please select a faculty OR enter a new one. Not both.');
	return false;
    }
    if (isBlank(f.useremail.value)) {
	alert('Please enter an email address');
	f.useremail.focus();
	return false;
    }
    if (isBlank(f.usertitle.value)) {
	alert('Please enter your title');
	f.usertitle.focus();
	return false;
    }
    if (isBlank(f.userfullname.value)) {
	alert('Please enter your name');
	f.userfullname.focus();
	return false;
    }
    if (isBlank(f.userphone.value)) {
	alert('Please enter a phone number');
	f.userphone.focus();
	return false;
    }
    if ("false" == mod) {
	if (isBlank(f.userpass1.value)) {
	    alert('Please enter a password');
	    f.userpass1.focus();
	    return false;
	}
	if (isBlank(f.userpass2.value)) {
	    alert('Please verify the password');
	    f.userpass2.focus();
	    return false;
	}
    }
    return true;
}


// Open popup window to get/edit a subheading.
//
// Parameters are   currval    current value
//                  formname   value of name="" attribute of the HTML
//                             form wrapping a call to this function
//                  elemname   value of name="" attribute of the HTML
//                             form element whose name we want to set
//
// There are 3 ways out of this popup
//      1.  External close of the window.  Nothing will be updated.
//      2.  User clicks cancel.  Nothing will be updated.
//      3.  User clicks save.  The element in the calling form will
//              be updated.
//
// One feels it should be possible to pass in the element we want
//  to update, rather than the form name and element name, but I
//  couldn't get it to work.  javascript is a bit of a pain.
//
// Note 1: the window is NOT modal.  That is difficult cross platform.
// Note 2: the window has a hard-coded name so there will only ever
//           be one open at a time.
//

function getHeading(currval, formname, elemname) {
    var w = window.open("",
			"AddHeading",
			"resizable,width=600,height=300");
    w.focus();
    var target = "opener.document."+formname+"."+elemname;
    var caller = "opener.document."+formname;
    var d = w.document;
    d.writeln('<html><head></head><body bgcolor="#ffffff" onLoad="document.headingform.heading.focus()">');
    d.writeln('<h2>Add a subheading</h2>');
    d.writeln('<p>Please enter the text for the subheading and press <em>',
	      'Save</em>.  Or press <em>Cancel</em> to close this window ',
	      'without adding a heading</p>');
    d.writeln('<form name="headingform">');
    d.writeln('<textarea rows="3" cols="80" wrap="physical" name="heading">',
	      currval,'</textarea>');
    d.writeln('<p>');
    d.writeln('<input type="submit" value="Save"');
    d.writeln(' onClick="',
	      target,
	      '.value=document.headingform.heading.value; ',
	      caller,
	      '.submit(); self.close();"');
    d.writeln('&nbsp; <input type="reset" value="reset"> &nbsp;');
    d.writeln('<input type="button"');
    d.writeln(' value="clear"',
	      ' onClick="document.headingform.heading.value=\'\';">'); 
    d.writeln('<input type="submit"');
    d.writeln('  value="Cancel" onClick="self.close();">');
    d.writeln('</form></body></html>');
    d.close();
}

