//the following function limits the number of characters that can be entered in the text field 
function limitTxtArea(object){
	var charNum = document.getElementById(object).value.length;
	if (charNum > 500){
		alert("You have reached the maximum length allowed for this field");
		document.getElementById(object).value = document.getElementById(object).value.substring(0, 500);
		document.getElementById(object).focus();
	}
	charNum = document.getElementById(object).value.length;
	document.getElementById("txtCharCount").value = 500 - charNum;
}

//the following function populates the days select box in the update form
function populateDaysBox(monthObj, yearObj, outObj){
	outObj.options.length = 0;
	
	for (i = 0; i < getDaysInMonth(monthObj, yearObj); i++){
		outObj.options[i] = new Option(i + 1, i + 1);
	}
}

//the following function returns the number of days in the month passed
//months are integers and based on 1 (January is 1, February is 2, and so on)
function getDaysInMonth(aMonth, aYear){
		var daysNum;
		
		//convert the passed values to integers
		aMonth = parseInt(aMonth);
		aYear = parseInt(aYear);
		
		//assign number of days according to the month
		switch(aMonth){
			case 1:
			case 3:
			case 5:
			case 7:
			case 8:
			case 10:
			case 12: daysNum = 31; break;
			case 4:
			case 6:
			case 9:
			case 11: daysNum = 30; break;
			case 2:
				if ((aYear % 4 == 0 && aYear % 100 != 0) || (aYear % 400 == 0)){
					daysNum = 29;
				}else{
					daysNum = 28;
				}
		}
		return daysNum;
}
function formValidate(){
	//check if all required fields are not empty
	if (document.getElementById('cboType').value.length == 0){
		alert("Please select an Event Category.");
		return false;
	}
	if (document.getElementById('txtSubject').value.length == 0){
		alert("Event Subject is required.");
		return false;
	}
/*	if (document.getElementById('txtLocation').value.length == 0){
		alert("Event Location is required.");
		return false;
	}*/
	
	//check if the date for the event is not in the past and display a prompt if it is
	var eDate = new Date();
	var curDate = new Date();
	var cont;
	eDate.setMonth(document.getElementById('cboDateMonth').value - 1);
	eDate.setDate(document.getElementById('cboDateDay').value);
	eDate.setFullYear(document.getElementById('cboDateYear').value);

	if (eDate < curDate){
		cont = confirm("The date entered for the event is in the past. Are you sure that you want to record this event with this date?");
		if (!cont){
			return false;
		}
	}
	
	//check if time is all day (in which case no time inputs are required)
	return true;
}
function validateTimeFormat(element){
	var passed = true;

	//check if the correct length is passed as a string
	if (document.getElementById(element).value.length > 8){
		passed = false;
	}
	
	//separate the hour
	var separator, hour, minutes, hourSet;
	if (document.getElementById(element).value.indexOf(".") > -1 && document.getElementById(element).value.indexOf(":") > -1){
		passed = false;
	}
	else if (document.getElementById(element).value.indexOf(".") > -1){
		separator = document.getElementById(element).value.indexOf(".")
	}else if(document.getElementById(element).value.indexOf(":") > -1){
		separator = document.getElementById(element).value.indexOf(":")
	}else{
		passed = false;
	}
	hour = document.getElementById(element).value.substr(0, separator);

	//hour cannot be longer than 2 symbols and cannot be larger than 23
	if (hour.length > 2){
		passed = false;
	}
	if (hour > 23){
		passed = false;
	}
	
	//get the minutes
	minutes = document.getElementById(element).value.substr(separator + 1, 2);
	//eliminate space characters in the minutes
	if (minutes == "  ") passed = false;
	if (minutes.indexOf(" ") > - 1){
		minutes = minutes.substr(0, minutes.indexOf(" ")) + minutes.substr(minutes.indexOf(" "), 1)
	}
	//minues cannot be longer than 2 symbols and cannot be larger than 59
	if (minutes.length > 2){
		passed = false;
	}
	if (!IsNumeric(minutes.substr(0, 1))){
		passed = false;
	}else if (!IsNumeric(minutes.substr(1, 1))){
		minutes = "0" + minutes.substr(0, 1);
	}
	if (minutes > 59){
		passed = false;
	}
	if (minutes.length == 1){
		minutes = "0" + minutes;
	}
	
	//get AM or PM
	if (document.getElementById(element).value.toUpperCase().indexOf("A") > -1){
		hourSet = "AM";
	}else if (document.getElementById(element).value.toUpperCase().indexOf("P") > -1){
		hourSet = "PM";
	}else{
		if (hour > 12){
			hour -= 12;
			hourSet = "PM";
		}else{
			hourSet = "AM";
		}
	}
	
	if (document.getElementById(element).value.toUpperCase() == "TBD" || document.getElementById(element).value.toUpperCase() == "TBA"){
		passed = true;
		return "TBD";
	}
	if (!passed){
		if (document.getElementById(element).value != ""){
			alert ("The time you entered is in invalid format.  Please enter a time in the format 'HH:MM AM/PM' or in military time format.");
			return false;
		}
	}else{
		document.getElementById(element).value = hour + ":" + minutes + " " + hourSet;
		return true;
	}
}

//function that checks if a value is numeric
function IsNumeric(sText){
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;
 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;   
}
function deletePrompt(destURL){
	var cont = confirm("You are about to delete this event.  Are you sure that you want to continue?");
	if (cont){
		window.location = destURL;
	}
}
function reDate (dest){
	var nMonth = document.getElementById("cboMonth").value;
	var nYear = document.getElementById("cboYear").value;
	var destURL = dest + '?month=' + nMonth + '&year=' + nYear;
	window.location = destURL;
}
function toggleGeneralDivs(checkControl, targetDiv, checkToOpen){
	if (checkToOpen){
		if(document.getElementById(checkControl).checked){
			document.getElementById(targetDiv).style.display = "inline";
		}else{
			document.getElementById(targetDiv).style.display = "none";
		}
	}else{
		if(document.getElementById(checkControl).checked){
			document.getElementById(targetDiv).style.display = "none";
		}else{
			document.getElementById(targetDiv).style.display = "inline";
		}
	}
}
