
var MAX_COMMENT_LENGTH = 650;

function isValidDate(dateStr) {
   // Checks for the following valid date formats:
   // DD/MM/YY   DD/MM/YYYY   DD-MM-YY   DD-MM-YYYY
   // Also separates date into month, day, and year variables

   //var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2}|\d{4})$/;
   // To require a 4 digit year entry, use this line instead:
   var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;

   var matchArray = dateStr.match(datePat); // is the format ok?
   if (matchArray == null) {
      alert("Date is not in a valid format.");
      return false;
   }
   month = matchArray[3]; // parse date into variables
   day = matchArray[1];
   year = matchArray[4];
   if (month < 1 || month > 12) { // check month range
      alert("Month must be between 1 and 12.");
      return false;
   }
   if (day < 1 || day > 31) {
      alert("Day must be between 1 and 31.");
      return false;
   }
   if ((month==4 || month==6 || month==9 || month==11) && day==31) {
      alert("Month "+month+" doesn't have 31 days!");
      return false
   }
   if (month == 2) { // check for february 29th
      var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
      if (day>29 || (day==29 && !isleap)) {
         alert("February " + year + " doesn't have " + day + " days!");
         return false;
      }
   }
   return true;  // date is valid
}

function GetDayOfWeek(TheDate) {

   objDate = new Date(TheDate.substr(6,4), TheDate.substr(3,2)-1, TheDate.substr(0,2), 0, 0, 0);
   return objDate.getDay();
}

function SetDayOfWeek(TheDate, sb) {
   var DoW

   DoW = GetDayOfWeek( TheDate );
   sb.selectedIndex = DoW
}

function GetSelectedItem(sb) {
   chosen = "0";

   len = sb.length;
   for (i = 0; i <len; i++) {
      if (sb[i].selected) {
         chosen = sb[i].value;
      }
   }
   return chosen;
}

function GetCheckedItem(rb) {
   chosen = "";

   len = rb.length;
   for (i = 0; i <len; i++) {
      if (rb[i].checked) {
         chosen = rb[i].value;
      }
   }
   return chosen;
}

//
// return which button in a group of radio buttons is selected
//
function GetRadioGroupValue(radioGroup)
{
	if (radioGroup != null)
	{
		for (var i = 0; i < radioGroup.length; i++)
		{
			if (radioGroup[i].checked == true)
			{
				return radioGroup[i].value;
			}
		}
	}
	return "";
}

function SetRadioGroupValue(radioGroup, strValue)
{
	if (radioGroup != null)
	{
		for (var i = 0; i < radioGroup.length; i++)
		{
			if (radioGroup[i].value == strValue)
			{
				radioGroup[i].checked = true;
				return;
			}
			else if (strValue == "")
			{
				radioGroup[i].checked = false;
			}
		}
	}
}

function testSelect (form) {
   Item = form.list.selectedIndex;
   Result = form.list.options[Item].text;
   alert (Result);
}

function testButton (form) {
   for (Count = 0; Count < 3; Count++) {
      if (form.rad[Count].checked)
         break;
      }
   alert ("Button " + Count + " is selected");
}

function validate(chk){
   if (chk.checked == 1)
      alert("Thank You");
   else
   alert("You didn't check it! Let me check it for you.");
   chk.checked = 1;
}

function clickIt() {
   txtOutput.value = window.event.srcElement.tagName;
   txtOutput1.value = window.event.srcElement.type;
   if ((window.event.srcElement.tagName) && ("A" + window.event.shiftKey)) window.event.returnValue = false;
}

function pop_up(url) {
	if (!newwindow.closed && newwindow.location) {
		newwindow.location.href = url;
	} else {
		newwindow=window.open(url,'name','height=320,width=660');
		if (!newwindow.opener) newwindow.opener = self;
	}
	if (window.focus) {newwindow.focus()}
	return false;
}

function confirmIt() {
   var x = confirm("Are you sure?")

   if (x)
      return true;
   else
      return false;
}

function isChecked(chk, msg){
   if (!chk.checked == 1) {
      window.alert ( msg );
      chk.select();
      chk.focus();
      return false;
   }
   return true;
}

function isDigit(str, msg){
   if (str.value.length == 0){
      window.alert ( msg );
      str.select();
      str.focus();
      return false;
   }
   for (var i=0;i < str.value.length;i++){
      if ((str.value.substring(i,i+1)<'0')||(str.value.substring(i,i+1)>'9')){
         window.alert ( msg );
         str.select();
         str.focus();
         return false;
      }
   }
   return true;
}

function ShowAlert(msg) {
   window.alert ( msg );
   return false;
}

function isOneSelected(sb, msg) {
   var len = sb.length;
   var cnt = 0;

   for (i = 0; i <len; i++) {
      if (sb[i].selected) {
         cnt = ++cnt;
      }
   }
   if (!cnt) {
      window.alert ( msg );
      sb.focus();
      return false;
   }
   return true;
}

function isEntered(TheField, msg) {
   if (TheField.value == '') {
      window.alert ( msg );
      TheField.focus();
      return false;
   }
   return true;
}

// Returns the number of days between the first date and the second date
function DaysDiff(objDate1, objDate2) {
   var sStartDate, sEndDate, sDate1, sDate2;

   // dd/mm/yyyy

   sDate1 = objDate1.options[objDate1.selectedIndex].value;
   sDate2 = objDate2.options[objDate2.selectedIndex].value;

   sStartDate = new Date(sDate1.substr(6,4), sDate1.substr(3,2)-1, sDate1.substr(0,2), 0, 0, 0);
   sEndDate   = new Date(sDate2.substr(6,4), sDate2.substr(3,2)-1, sDate2.substr(0,2), 0, 0, 0);

   return (sEndDate - sStartDate)/(1000*60*60*24);
}

// Returns the number of minutes between the finish time and the start time
function TimeDiff(objDate, objStartTime, objFinishTime) {
   var sStartTime, sEndTime, sDate, sStart, sEnd;

   // dd/mm/yyyy hh:mm AM/PM

   sDate  = objDate.options[objDate.selectedIndex].value;
   sStart = objStartTime.options[objStartTime.selectedIndex].value;
   sEnd   = objFinishTime.options[objFinishTime.selectedIndex].value;

   sStartTime = new Date(sDate.substr(6,4), sDate.substr(3,2)-1, sDate.substr(0,2), ((sStart.substr(6,2) == "PM") ? Number(sStart.substr(0,2))+12 : sStart.substr(0,2)), sStart.substr(3,2), 0);
   sEndTime   = new Date(sDate.substr(6,4), sDate.substr(3,2)-1, sDate.substr(0,2), ((sEnd.substr(6,2) == "PM") ? Number(sEnd.substr(0,2))+12 : sEnd.substr(0,2)), sEnd.substr(3,2), 0);

   return (sEndTime - sStartTime)/(1000*60);
}

/**
 * This array is used to remember mark status of rows in browse mode
 */
var marked_row = new Array;

/**
 * enables highlight and marking of rows in data tables
 *
 */
function PMA_markRowsInit() {
    // for every table row ...
    var rows = document.getElementsByTagName('tr');
    for ( var i = 0; i < rows.length; i++ ) {
        // ... with the class 'odd' or 'even' ...
        if ( 'odd' != rows[i].className.substr(0,3) && 'even' != rows[i].className.substr(0,4) ) {
            continue;
        }
        // ... add event listeners ...
        // ... to highlight the row on mouseover ...
        if ( navigator.appName == 'Microsoft Internet Explorer' ) {
            // but only for IE, other browsers are handled by :hover in css
            rows[i].onmouseover = function() {
                this.className += ' hover';
            }
            rows[i].onmouseout = function() {
                this.className = this.className.replace( ' hover', '' );
            }
        }
        // Do not set click events if not wanted
        if (rows[i].className.search(/noclick/) != -1) {
            continue;
        }
        // ... and to mark the row on click ...
        rows[i].onmousedown = function() {
            var unique_id;
            var checkbox;

            checkbox = this.getElementsByTagName( 'input' )[0];
            if ( checkbox && checkbox.type == 'checkbox' ) {
                unique_id = checkbox.name + checkbox.value;
            } else if ( this.id.length > 0 ) {
                unique_id = this.id;
            } else {
                return;
            }

            if ( typeof(marked_row[unique_id]) == 'undefined' || !marked_row[unique_id] ) {
                marked_row[unique_id] = true;
            } else {
                marked_row[unique_id] = false;
            }

            if ( marked_row[unique_id] ) {
                this.className += ' marked';
            } else {
                this.className = this.className.replace(' marked', '');
            }

            if ( checkbox && checkbox.disabled == false ) {
                checkbox.checked = marked_row[unique_id];
            }
        }

        // ... and disable label ...
        var labeltag = rows[i].getElementsByTagName('label')[0];
        if ( labeltag ) {
            labeltag.onclick = function() {
                return false;
            }
        }
        // .. and checkbox clicks
        var checkbox = rows[i].getElementsByTagName('input')[0];
        if ( checkbox ) {
            checkbox.onclick = function() {
                // opera does not recognize return false;
                this.checked = ! this.checked;
            }
        }
    }
}
window.onload=PMA_markRowsInit;

/**
 * marks all rows and selects its first checkbox inside the given element
 * the given element is usaly a table or a div containing the table or tables
 *
 * @param    container    DOM element
 */
function markAllRows( container_id ) {
    var rows = document.getElementById(container_id).getElementsByTagName('tr');
    var unique_id;
    var checkbox;

    for ( var i = 0; i < rows.length; i++ ) {

        checkbox = rows[i].getElementsByTagName( 'input' )[0];

        if ( checkbox && checkbox.type == 'checkbox' ) {
            unique_id = checkbox.name + checkbox.value;
            if ( checkbox.disabled == false ) {
                checkbox.checked = true;
                if ( typeof(marked_row[unique_id]) == 'undefined' || !marked_row[unique_id] ) {
                    rows[i].className += ' marked';
                    marked_row[unique_id] = true;
                }
            }
        }
    }

    return true;
}

/**
 * marks all rows and selects its first checkbox inside the given element
 * the given element is usaly a table or a div containing the table or tables
 *
 * @param    container    DOM element
 */
function unMarkAllRows( container_id ) {
    var rows = document.getElementById(container_id).getElementsByTagName('tr');
    var unique_id;
    var checkbox;

    for ( var i = 0; i < rows.length; i++ ) {

        checkbox = rows[i].getElementsByTagName( 'input' )[0];

        if ( checkbox && checkbox.type == 'checkbox' ) {
            unique_id = checkbox.name + checkbox.value;
            checkbox.checked = false;
            rows[i].className = rows[i].className.replace(' marked', '');
            marked_row[unique_id] = false;
        }
    }

    return true;
}

/**
 * Checks/unchecks all checkbox in given conainer (f.e. a form, fieldset or div)
 *
 * @param   string   container_id  the container id
 * @param   boolean  state         new value for checkbox (true or false)
 * @return  boolean  always true
 */
function setCheckboxes( container_id, state ) {
    var checkboxes = document.getElementById(container_id).getElementsByTagName('input');

    for ( var i = 0; i < checkboxes.length; i++ ) {
        if ( checkboxes[i].type == 'checkbox' ) {
            checkboxes[i].checked = state;
        }
    }

    return true;
} // end of the 'setCheckboxes()' function

/**
  * Checks/unchecks all options of a <select> element
  *
  * @param   string   the form name
  * @param   string   the element name
  * @param   boolean  whether to check or to uncheck the element
  *
  * @return  boolean  always true
  */
function setSelectOptions(the_form, the_select, do_check)
{
    var selectObject = document.forms[the_form].elements[the_select];
    var selectCount  = selectObject.length;

    for (var i = 0; i < selectCount; i++) {
        selectObject.options[i].selected = do_check;
    } // end for

    return true;
} // end of the 'setSelectOptions()' function

function clear_form(MyForm) {

   for (var i=0; i < MyForm.elements.length; i++) {
      var element = MyForm.elements[i];
      if (element.type == 'submit') { continue; }
      if (element.type == 'reset') { continue; }
      if (element.type == 'button') { continue; }
      if (element.type == 'hidden') { continue; }
      if (element.type == 'text') { element.value = ''; }
      if (element.type == 'textarea') { element.value = ''; }
      if (element.type == 'checkbox') {
         if (element.name.match(/search_/)) {
            if (element.name.match(/search_comments/)) {
               element.checked = false;
            } else {
               element.checked = true;
            }
         } else {
            element.checked = false;
         }
      }
      if (element.type == 'radio') { element.checked = false; }
      if (element.type == 'select-multiple') { element.selectedIndex = -1; }
      if (element.type == 'select-one') { element.selectedIndex = -1; }
   }
   return false;
}

function clear_form_old(form) {
	var newLoc = 'index.php?action=' + form.action.value + '&module=' + form.module.value;
	if(typeof(form.advanced) != 'undefined'){
		newLoc += '&advanced=' + form.advanced.value;
	}
	document.location.href= newLoc;
}

function GetPageElementTraining(strElementID)
{
	var obj;
	if (document.all)
	{
		return document.all[strElementID];
	}
	else if (document.getElementById)
	{
		return document.getElementById(strElementID);
	}
	else if (document.layers)
	{

		if (strElementID == "AwsCourseID")
		{
			obj = document.MainForm.AwsCourseID;
		}
		if (strElementID == "AwsPageID")
		{
			obj = document.MainForm.AwsPageID;
		}
		else if (strElementID == "LessonBits")
		{
			obj = document.MainForm.LessonBits;
		}

		return obj;
	}
	return null;
}

function GetFeedbackErrorString(iTotal, iLimit)
{
	var strPasteError = g_strFeedbackOverLimit;
	strPasteError = strPasteError.replace("{0}", iTotal);
	strPasteError = strPasteError.replace("{1}", iLimit);
	return strPasteError;
}

function DisplayCommentCharsRemaining()
{
	var objCommentBox = document.getElementById('maintext');
	if (objCommentBox)
		{
		var strComments = objCommentBox.value;
		var iCharsRemaining = MAX_COMMENT_LENGTH - strComments.length;
		var objCommentCounter = document.getElementById('CommentCounter');
		if (objCommentCounter)
			{
			var strCharsRemaining = strGoDisplayCountOK;
//			strCharsRemaining.replace("{0}", "<B>" + iCharsRemaining + "</B>");
			strCharsRemaining = strCharsRemaining + "<B>" + iCharsRemaining + "</B>";
			if (iCharsRemaining < 0)
				{
				var strPasteError = GetFeedbackErrorString(strComments.length, MAX_COMMENT_LENGTH);
				objCommentCounter.innerHTML = '<SPAN CLASS="FeedbackWizCounterStar">' + g_strFeedbackOverLimitStar +
					'</SPAN><SPAN CLASS="FeedbackWizCounterOverText">' + strPasteError + '</SPAN>'
				}
			else
				{
				objCommentCounter.innerHTML = strCharsRemaining;
				}
			}
		}
}

function CommentChange(objCommentBox)
{
	DisplayCommentCharsRemaining();
	return true;
}

function CommentKeyPress(objCommentBox, evt)
{
	DisplayCommentCharsRemaining();
	return true;
}

function CommentKeyUp(objCommentBox)
{
	DisplayCommentCharsRemaining();
	return true;
}

function CommentPaste(objCommentBox)
{
	DisplayCommentCharsRemaining();
	return true;
}

function CommentFocus(objCommentBox)
{

}

function CommentBlur(objCommentBox)
{

}

function CommentKeyDown(objCommentBox)
{

	return true;
}