var CourseEntryWin = null;
function OpenCourseEntryWin(courseID)
{
	CourseEntryWin = window.open('CourseEntry.aspx?courseid='+ courseID,'','status=1,toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,titlebar=0,width=800,height=600');
}

var CourseWin = null;
function OpenCourseWin(courseID)
{
	CourseWin = window.open('CourseWindow.aspx?courseid='+ courseID,'','status=1,toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,titlebar=0,width=700,height=550');
}

function HasAdminAccess(_role, _location)
{
	if(_role!='administrator')
	{
		alert('Access Denied to Administrator only functions.')
	}
	else
	{
		window.location.href=_location;
	}
}

function toggleLayer(whichLayer)
{
if (document.getElementById)
{
// this is the way the standards work
var style2 = document.getElementById(whichLayer).style;
}
else if (document.all)
{
// this is the way old msie versions work
var style2 = document.all[whichLayer].style;
}
else if (document.layers)
{
// this is the way nn4 works
var style2 = document.layers[whichLayer].style;
}
style2.display = style2.display? "":"block";
}

function showDiv(whichLayer)
{
	if (document.getElementById)
	{
	// this is the way the standards work
		var style2 = document.getElementById(whichLayer).style;
	}
	else if (document.all)
	{
		// this is the way old msie versions work
		var style2 = document.all[whichLayer].style;
	}
	else if (document.layers)
	{
	// this is the way nn4 works
	var style2 = document.layers[whichLayer].style;
	}
	style2.display = "block";
}

function hideDiv(whichLayer)
{
	if (document.getElementById)
	{
	// this is the way the standards work
		var style2 = document.getElementById(whichLayer).style;
	}
	else if (document.all)
	{
		// this is the way old msie versions work
		var style2 = document.all[whichLayer].style;
	}
	else if (document.layers)
	{
	// this is the way nn4 works
	var style2 = document.layers[whichLayer].style;
	}
	style2.display = "";
}

function MoveNextPage(page_name, id_value, id_role, b_hasAccess, error_msg)
{
	if(id_role=="") //No role, should never get here, but let's be explicit.
	{
		alert("Access Denied!");
	}
	else if(id_role.toLowerCase()=="program manager")
	{
		if(b_hasAccess) // determine if Program Manager has access to this page, returns true or false.
		{
			if(id_value!="")
			{
				location.href=page_name;
			}
			else
			{
				alert(error_msg);
			}
		}
		else
		{
			alert("Access Denied");
		}
	}
	else //Super User
	{
		if(id_value!="")
		{
			location.href=page_name;
		}
		else
		{
			alert(error_msg);
		}
	}


}

var cbs = null;

function FIX(btn)
{
    var frm = btn.form;

    // if we don't already have the array of checkboxes, build it
    if ( cbs == null )
    {
        cbs = new Array( );
        for ( var en = 0; en < frm.elements.length; ++en )
        {
            var fld = frm.elements[en];
            if ( fld.type == "checkbox" )
                cbs[cbs.length] = fld;
        }
    }
    // now look in the array of checkboxes for a pattern
    // of unchecked followed by checked...
    var sequential = true;
    for ( var cbn = 1; cbn < cbs.length; ++cbn )
    {
        if ( cbs[cbn].checked && ! cbs[cbn-1].checked )
            sequential = false;
    }
    if ( sequential ) return; // all is in order

    if ( confirm("Events MUST be completed in order.\n"
                 + "Please check the previous events.")
       )    btn.checked = ! btn.checked; return false;
		// flop it back the way it was
} 

function clickButton(e, buttonid){ 
      var bt = document.getElementById(buttonid); 
      if (typeof bt == 'object'){ 
            if(navigator.appName.indexOf("Netscape")>(-1)){ 
                  if (e.keyCode == 13){ 
                        bt.click(); 
                        return false; 
                  } 
            } 
            if (navigator.appName.indexOf("Microsoft Internet Explorer")>(-1)){ 
                  if (event.keyCode == 13){ 
                        bt.click(); 
                        return false; 
                  } 
            } 
      }
}

 function showstuff(boxid){
   document.getElementById(boxid).style.visibility="visible";
}

function hidestuff(boxid){
   document.getElementById(boxid).style.visibility="hidden";
}

function createWindow(url, width, height, winName) 
{
    var URL = url;

    var windowName = winName;

    var features =
        'width='        +	width +
        ',height='      + height +
        ',directories=0' +
        ',location=0'    +
        ',menubar=0'     +
        ',scrollbars=1'  +
        ',status=0'      +
        ',toolbar=0'     +
        ',resizable=1'

    window.open (URL, windowName, features);
}

function confirmDelete (frm)
{
    // loop through all elements
    for (i=0; i<frm.length; i++)
    {
        // Look for our checkboxes only
        if (frm.elements[i].name.indexOf("chkComplete") !=-1)
        {
            // If any are checked then confirm alert, otherwise nothing happens
            if(frm.elements[i].checked)
            {
                return confirm ('Are you sure you want to complete this event(s)?')
            }
        }
    }
}

// -------------------------------------------------------------------
// hasOptions(obj)
//  Utility function to determine if a select object has an options array
// -------------------------------------------------------------------
function hasOptions(obj) {
	if (obj!=null && obj.options!=null) { return true; }
	return false;
	}

// -------------------------------------------------------------------
// selectUnselectMatchingOptions(select_object,regex,select/unselect,true/false)
//  This is a general function used by the select functions below, to
//  avoid code duplication
// -------------------------------------------------------------------
function selectUnselectMatchingOptions(obj,regex,which,only) {
	if (window.RegExp) {
		if (which == "select") {
			var selected1=true;
			var selected2=false;
			}
		else if (which == "unselect") {
			var selected1=false;
			var selected2=true;
			}
		else {
			return;
			}
		var re = new RegExp(regex);
		if (!hasOptions(obj)) { return; }
		for (var i=0; i<obj.options.length; i++) {
			if (re.test(obj.options[i].text)) {
				obj.options[i].selected = selected1;
				}
			else {
				if (only == true) {
					obj.options[i].selected = selected2;
					}
				}
			}
		}
	}
		
// -------------------------------------------------------------------
// selectMatchingOptions(select_object,regex)
//  This function selects all options that match the regular expression
//  passed in. Currently-selected options will not be changed.
// -------------------------------------------------------------------
function selectMatchingOptions(obj,regex) {
	selectUnselectMatchingOptions(obj,regex,"select",false);
	}
// -------------------------------------------------------------------
// selectOnlyMatchingOptions(select_object,regex)
//  This function selects all options that match the regular expression
//  passed in. Selected options that don't match will be un-selected.
// -------------------------------------------------------------------
function selectOnlyMatchingOptions(obj,regex) {
	selectUnselectMatchingOptions(obj,regex,"select",true);
	}
// -------------------------------------------------------------------
// unSelectMatchingOptions(select_object,regex)
//  This function Unselects all options that match the regular expression
//  passed in. 
// -------------------------------------------------------------------
function unSelectMatchingOptions(obj,regex) {
	selectUnselectMatchingOptions(obj,regex,"unselect",false);
	}
	
// -------------------------------------------------------------------
// sortSelect(select_object)
//   Pass this function a SELECT object and the options will be sorted
//   by their text (display) values
// -------------------------------------------------------------------
function sortSelect(obj) {
	var o = new Array();
	if (!hasOptions(obj)) { return; }
	for (var i=0; i<obj.options.length; i++) {
		o[o.length] = new Option( obj.options[i].text, obj.options[i].value, obj.options[i].defaultSelected, obj.options[i].selected) ;
		}
	if (o.length==0) { return; }
	o = o.sort( 
		function(a,b) { 
			if ((a.text+"") < (b.text+"")) { return -1; }
			if ((a.text+"") > (b.text+"")) { return 1; }
			return 0;
			} 
		);

	for (var i=0; i<o.length; i++) {
		obj.options[i] = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);
		}
	}

// -------------------------------------------------------------------
// selectAllOptions(select_object)
//  This function takes a select box and selects all options (in a 
//  multiple select object). This is used when passing values between
//  two select boxes. Select all options in the right box before 
//  submitting the form so the values will be sent to the server.
// -------------------------------------------------------------------
function selectAllOptions(obj) {
	if (!hasOptions(obj)) { return; }
	for (var i=0; i<obj.options.length; i++) {
		obj.options[i].selected = true;
		}
	}
	
// -------------------------------------------------------------------
// moveSelectedOptions(select_object,select_object[,autosort(true/false)[,regex]])
//  This function moves options between select boxes. Works best with
//  multi-select boxes to create the common Windows control effect.
//  Passes all selected values from the first object to the second
//  object and re-sorts each box.
//  If a third argument of 'false' is passed, then the lists are not
//  sorted after the move.
//  If a fourth string argument is passed, this will function as a
//  Regular Expression to match against the TEXT or the options. If 
//  the text of an option matches the pattern, it will NOT be moved.
//  It will be treated as an unmoveable option.
//  You can also put this into the <SELECT> object as follows:
//    onDblClick="moveSelectedOptions(this,this.form.target)
//  This way, when the user double-clicks on a value in one box, it
//  will be transferred to the other (in browsers that support the 
//  onDblClick() event handler).
// -------------------------------------------------------------------
function moveSelectedOptions(from,to) {
	// Unselect matching options, if required
	if (arguments.length>3) {
		var regex = arguments[3];
		if (regex != "") {
			unSelectMatchingOptions(from,regex);
			}
		}
	// Move them over
	if (!hasOptions(from)) { return; }
	for (var i=0; i<from.options.length; i++) {
		var o = from.options[i];
		if (o.selected) {
			if (!hasOptions(to)) { var index = 0; } else { var index=to.options.length; }
			to.options[index] = new Option( o.text, o.value, false, false);
			}
		}
	// Delete them from original
	for (var i=(from.options.length-1); i>=0; i--) {
		var o = from.options[i];
		if (o.selected) {
			from.options[i] = null;
			}
		}
	if ((arguments.length<3) || (arguments[2]==true)) {
		sortSelect(from);
		sortSelect(to);
		}
	from.selectedIndex = -1;
	to.selectedIndex = -1;
	}

// -------------------------------------------------------------------
// copySelectedOptions(select_object,select_object[,autosort(true/false)])
//  This function copies options between select boxes instead of 
//  moving items. Duplicates in the target list are not allowed.
// -------------------------------------------------------------------
function copySelectedOptions(from,to) {
	var options = new Object();
	if (hasOptions(to)) {
		for (var i=0; i<to.options.length; i++) {
			options[to.options[i].value] = to.options[i].text;
			}
		}
	if (!hasOptions(from)) { return; }
	for (var i=0; i<from.options.length; i++) {
		var o = from.options[i];
		if (o.selected) {
			if (options[o.value] == null || options[o.value] == "undefined" || options[o.value]!=o.text) {
				if (!hasOptions(to)) { var index = 0; } else { var index=to.options.length; }
				to.options[index] = new Option( o.text, o.value, false, false);
				}
			}
		}
	if ((arguments.length<3) || (arguments[2]==true)) {
		sortSelect(to);
		}
	from.selectedIndex = -1;
	to.selectedIndex = -1;
	}

// -------------------------------------------------------------------
// moveAllOptions(select_object,select_object[,autosort(true/false)[,regex]])
//  Move all options from one select box to another.
// -------------------------------------------------------------------
function moveAllOptions(from,to) {
	selectAllOptions(from);
	if (arguments.length==2) {
		moveSelectedOptions(from,to);
		}
	else if (arguments.length==3) {
		moveSelectedOptions(from,to,arguments[2]);
		}
	else if (arguments.length==4) {
		moveSelectedOptions(from,to,arguments[2],arguments[3]);
		}
	}

// -------------------------------------------------------------------
// copyAllOptions(select_object,select_object[,autosort(true/false)])
//  Copy all options from one select box to another, instead of
//  removing items. Duplicates in the target list are not allowed.
// -------------------------------------------------------------------
function copyAllOptions(from,to) {
	selectAllOptions(from);
	if (arguments.length==2) {
		copySelectedOptions(from,to);
		}
	else if (arguments.length==3) {
		copySelectedOptions(from,to,arguments[2]);
		}
	}

// -------------------------------------------------------------------
// swapOptions(select_object,option1,option2)
//  Swap positions of two options in a select list
// -------------------------------------------------------------------
function swapOptions(obj,i,j) {
	var o = obj.options;
	var i_selected = o[i].selected;
	var j_selected = o[j].selected;
	var temp = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);
	var temp2= new Option(o[j].text, o[j].value, o[j].defaultSelected, o[j].selected);
	o[i] = temp2;
	o[j] = temp;
	o[i].selected = j_selected;
	o[j].selected = i_selected;
	}
	
// -------------------------------------------------------------------
// moveOptionUp(select_object)
//  Move selected option in a select list up one
// -------------------------------------------------------------------
function moveOptionUp(obj) {
	if (!hasOptions(obj)) { return; }
	for (i=0; i<obj.options.length; i++) {
		if (obj.options[i].selected) {
			if (i != 0 && !obj.options[i-1].selected) {
				swapOptions(obj,i,i-1);
				obj.options[i-1].selected = true;
				}
			}
		}
	}

// -------------------------------------------------------------------
// moveOptionDown(select_object)
//  Move selected option in a select list down one
// -------------------------------------------------------------------
function moveOptionDown(obj) {
	if (!hasOptions(obj)) { return; }
	for (i=obj.options.length-1; i>=0; i--) {
		if (obj.options[i].selected) {
			if (i != (obj.options.length-1) && ! obj.options[i+1].selected) {
				swapOptions(obj,i,i+1);
				obj.options[i+1].selected = true;
				}
			}
		}
	}

// -------------------------------------------------------------------
// removeSelectedOptions(select_object)
//  Remove all selected options from a list
//  (Thanks to Gene Ninestein)
// -------------------------------------------------------------------
function removeSelectedOptions(from) { 
	if (!hasOptions(from)) { return; }
	if (from.type=="select-one") {
		from.options[from.selectedIndex] = null;
		}
	else {
		for (var i=(from.options.length-1); i>=0; i--) { 
			var o=from.options[i]; 
			if (o.selected) { 
				from.options[i] = null; 
				} 
			}
		}
	from.selectedIndex = -1; 
	} 

// -------------------------------------------------------------------
// removeAllOptions(select_object)
//  Remove all options from a list
// -------------------------------------------------------------------
function removeAllOptions(from) { 
	if (!hasOptions(from)) { return; }
	for (var i=(from.options.length-1); i>=0; i--) { 
		from.options[i] = null; 
		} 
	from.selectedIndex = -1; 
	} 

// -------------------------------------------------------------------
// addOption(select_object,display_text,value,selected)
//  Add an option to a list
// -------------------------------------------------------------------
function addOption(obj,text,value,selected) {
	if (obj!=null && obj.options!=null) {
		obj.options[obj.options.length] = new Option(text, value, false, selected);
		}
	}
// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}


function validDate(formField,fieldLabel,required) {
var result = true;


if (required && !validRequired(formField,fieldLabel))
  result = false;

if (result) {
  var elems = eval(formField.value.split('/'));

  result = (elems.length == 3); // should be three components

  if (result) {
    var month = parseInt(elems[0],10);
    var day = parseInt(elems[1],10);
    var year = parseInt(elems[2],10);
    result = allDigits(elems[0]) && (month > 0) && (month < 13) &&
    allDigits(elems[1]) && (day > 0) && (day < 32) &&
    allDigits(elems[2]) && ((elems[2].length == 2) || (elems[2].length == 4));
  }

  if (!result) {
    alert('Please enter a date in the format MM/DD/YYYY for the "' + fieldLabel +'" field.');
    formField.focus();
  }

}

return result;
} 

function LeapYear(intYear) {
if (intYear % 100 == 0) {
if (intYear % 400 == 0) { return true; }
}
else {
if ((intYear % 4) == 0) { return true; }
}
return false;
}

function isEmailAddr(email)
{
  var result = false;
  var theStr = new String(email);
  var index = theStr.indexOf("@");
  if (index > 0)
  {
    var pindex = theStr.indexOf(".",index);
    if ((pindex > index+1) && (theStr.length > pindex+1))
  result = true;
  }

  return result;
}

function validRequired(formField,fieldLabel)
{
  var result = true;
  
  if (formField.value == "")
  {
    alert('Please enter a value for the "' + fieldLabel +'" field.');
    formField.focus();
    result = false;
  }
  
  return result;
}

function validEmail(formField,fieldLabel,required)
{
  var result = true;
  
  if (required && !validRequired(formField,fieldLabel))
    result = false;

  if (result && ((formField.value.length < 3) || !isEmailAddr(formField.value)) )
  {
    alert("Please enter a complete email address in the form: yourname@yourdomain.com");
    formField.focus();
    result = false;
  }
   
  return result;

}


var digits = "0123456789";

//checks for blank string
function isEmpty(s)
{
	return ((s == null) || (s.length == 0))
}

function isLetter(c)
{
	if(((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")))
	{
		return true;
	}
	else
	{
		return false;
	}
}

function isDigit(c)
{
	return ((c >= "0") && (c <= "9"));
}

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function isAlphanumeric(s)
{
	var i;
	var bNumeric = false;
	var bAlphabetic = false;

	for(i=0; i < s.length; i ++)
	{
		var c = s.charAt(i);
		if(isLetter(c))
		{
			bAlphabetic = true;
		}		
		
		if(isDigit(c))
		{
			bNumeric = true;
		}
	}

	if((bNumeric == true) && (bAlphabetic == true))
	{
		return true;
	}
	else
	{
		return false;
	}
}

function isDecimal(field)
{
	var validTxt = /^[0-9.]+$/
	if (! validTxt.test(field.value))
	{
		return false;
	}
	else
	{
		return true;
	}
}

function isNumeric(field)
{
	var validTxt = /^[0-9]+$/
	if (! validTxt.test(field.value))
	{
		field.focus();
		return false;
	}
	else
	{
		return true;
	}
}


function isValidInteger(s)
{
	var i;
	for(i=0; i < s.length; i++)
	{
		var c = s.charAt(i);

		if(i>=1)
		{	// There shouldnt be a negative sign unless it is
			// in front of the number
			if(c=="-") return false;
		}

		if(!isDigit(c)) return false;	
	}
	return true; 	// All characters are valid
}

function isIntDec(s)
{
	var i;
	for(i=0; i < s.length; i++)
	{
		var c = s.charAt(i);
		if(!isDigit(c) && c!="." && c!="-") return false;	
	}
	return true; 	// All characters are numbers
}

function allDigits(str)
{
	return inValidCharSet(str,"0123456789");
}

function inValidCharSet(str,charset)
{
	var result = true;

	// Note: doesn't use regular expressions to avoid early Mac browser bugs	
	for (var i=0;i<str.length;i++)
		if (charset.indexOf(str.substr(i,1))<0)
		{
			result = false;
			break;
		}
	
	return result;
}

function fixMoney(fld,sep)
{ // monetary field check
  if(!fld.value.length||fld.disabled) return true; // blank fields are the domain of requireValue 
  var val= fld.value;
  if(typeof(sep)!='undefined') val= val.replace(new RegExp(sep,'g'),'');
  if(val.indexOf('$') == 0)
    val= parseFloat(val.substring(1,40));
  else
    val= parseFloat(val);
  if(isNaN(val))
  { // parse error 
    status= 'The '+fieldname(fld)+' field must contain a dollar amount.';
    return false;
  }
  var sign= ( val < 0 ? '-': '' );
  val= Number(Math.round(Math.abs(val)*100)).toString();
  while(val.length < 2) val= '0'+val;
  var len= val.length;
  val= sign + ( len == 2 ? '0' : val.substring(0,len-2) ) + '.' + val.substring(len-2,len+1);
  fld.value= val;
  return true;
}

function validPrice(formField,fieldLabel,required)
{
	var result = true;

	if (required && !validRequired(formField,fieldLabel))
		result = false;
  
 	if (result)
 	{
 		if (!allDigits(formField.value))
 		{
 			alert('The "Price" field can not include any decimals or letters.');
			formField.focus();		
			result = false;
		}
	} 
	
	return result;
}


function FormatPhone(s)
{
	var Area = "";
	var phon3 = "";
	var phon4 = "";
	var digitCount = 0;
	var phoneNum = "";
	
	if(isEmpty(s))
		return s;

	for (i=0; i<s.length;i++)
	{
		p = s.substring(i,i+1);
		for (j=0;j<digits.length;j++)
		{
   		   	n=digits.substring(j,j+1)                                   
			if (p == n)
			{
				if(digitCount <= 2)
				{
					Area = Area + p;
				}
				else if(digitCount <= 5)
				{
					phon3 = phon3 + p;
				}
				else if(digitCount <= 9)
				{
					phon4 = phon4 + p;
				}
				else if(digitCount <= 10)
				{
					Area = Area.substring(1,3) + phon3.substring(0,1);
					phon3 = phon3.substring(1,3) + phon4.substring(0,1);
					phon4 = phon4.substring(1,4) + p; 
				}

				digitCount ++;			
				break;
		}	}                                        
	}
	
	if(Area.length == 0 && phon3.length == 0 && phon4.length == 0)
	{
		Area = "   ";
		phon3 = "   ";
		phon4 = "    ";
	}

	phoneNum = "(" + Area + ") " + phon3 + "-" + phon4;
	return phoneNum;
}

function validatePhone(textfield) {
     phoneOK = true; // phoneOK is GLOBAL
     var digits = 0;

     // Check that the phone number only contains
     // the characters "() -0-9" and contains the right
     // number of digits total

     for (var i=0; i < textfield.value.length; i++) {
        var theChar = textfield.value.charAt(i);
        if ((theChar >= "0") && (theChar <= "9")) {
           digits++;
           continue;
        }
        if (theChar == " ") continue;
        if (theChar == "-") continue;
        if (theChar == "(") continue;
        if (theChar == ")") continue;
        phoneOK = false;
     }

     // The string is OK if it contains only the allowed
     // characters and the number of digits in the
     // phone number is 10 total (area code + number)

     phoneOK = phoneOK && (digits == 10);
     if (!phoneOK) {
        alert("Please enter a phone number in the format (nnn) nnn-nnnn");
        textfield.focus();
        textfield.select();
     }

     return phoneOK;
  }
	
function formatCurrency(num) {
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num))
num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
cents = num%100;
num = Math.floor(num/100).toString();
if(cents<10)
cents = "0" + cents;
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3))+','+
num.substring(num.length-(4*i+3));
return (((sign)?'':'-') + num + '.' + cents);
}

//  End -->