// @(#) $Id: ams.js 28149 2006-04-05 13:28:21Z peter $
function setDay( change_what, month, year ) {
     if (parseInt(month.value) == 2 && parseInt(change_what.value) >28){
          if  (IsLeapYear(parseInt(year.value))) change_what.value=29
          else change_what.value=28
     }
     if (( parseInt(month.value) == 4 ||
           parseInt(month.value) == 6 ||
           parseInt(month.value, 10) == 9 ||
           parseInt(month.value) == 11) &&
           parseInt(change_what.value) >30)
         change_what.value=30

}

function IsLeapYear(y) {
    if (((y%4)==0) && (((y%100)>0) || ((y%400)==0)))
	return(true);
    else
	return(false);
}

/*
 * Used to toggle the display of sections of forms based
 * on the state of a checkbox. This is introduced for the
 * CUP version of the admin system.
 */
function toggle_block(id,state) {
    if(document.getElementById) {
        var element_style = document.getElementById(id).style;
        if(state) {
            element_style.display = "block";
        } else {
            element_style.display = "none";
        }
        return false;
    }
    return true;
}

function getkey(e)
{
    if (window.event)
        return window.event.keyCode;
    else if (e)
        return e.which;
    else
        return null;
}

function goodchars(e, goods)
{
    var key, keychar;
    key = getkey(e);
    if (key == null) return true;

    // get character
    keychar = String.fromCharCode(key);
    keychar = keychar.toLowerCase();
    goods = goods.toLowerCase();

    // check goodkeys
    if (goods.indexOf(keychar) != -1)
        return true;

    // control keys
    if ( key==null || key==0 || key==8 || key==9 || key==13 || key==27 )
       return true;

    // else return false
    return false;
}

/*
 * Turn other check boxes on or off depending on the value of this box.
 *
 * This code assumes that the input="checkbox" elements are all within
 * a parent element and that they have the ids "administrator",
 * "ipeditor" and "sslimited".
 */

function enforce_groups(myCheckbox) {
    var parentNode = myCheckbox.parentNode;
    if(myCheckbox.id == "administrator" && !myCheckbox.checked){
      // If the administrator group is unchecked then the
      // ipeditor and sslimited groups must be turned off

      var checkNodes = parentNode.getElementsByTagName("input");
      for(a=0; a<checkNodes.length; a++){
        if( checkNodes[a].id == "ipeditor" )
          checkNodes[a].checked = false;
        if( checkNodes[a].id == "sslimited" )
          checkNodes[a].checked = false;
      }
    }
    else if((myCheckbox.id == "ipeditor" || myCheckbox.id == "sslimited") && myCheckbox.checked){
        // The administrator group needs to be turned on if
        // the ipeditor or sslimited groups are checked

        var checkNodes = parentNode.getElementsByTagName("input");
        for(a=0; a<checkNodes.length; a++){
            if( checkNodes[a].id == "administrator" )
                checkNodes[a].checked = true;
        }
    }
}

/*
 * This function checks that the password matches the 
 * value in confirm password
 */

function verify_password(passwordID1, passwordID2) {
    if (document.getElementById(passwordID1).value == document.getElementById(passwordID2).value) {
	if(document.getElementById(passwordID1).value) {
	    return true;
	}
	else {
	    alert( "The password cannot be blank, please enter a password" );
	    return false;
	}
    }
    else {
	alert( "The passwords you typed do not match. Please retype the new password in both boxes.");
	return false;
    }
}


