function CheckCalendar(form) {
	if (form.c_firstname.value == "") {
		alert("Please provide us with your first name");
		return false;
	}
	if (form.c_lastname.value == "") {
		alert("Please provide us with your last name");
		return false;
	}
	if (form.c_email.value == "") {
		alert("Please provide us with an e-mail address");
		return false;
	}
	if (form.c_cell_phone.value == "" && form.c_room_phone.value == "") {
		alert("Please provide us with a cell phone or room phone");
		return false;
	}
	if (form.d_startdate.value == "") {
		alert("A start date is required");
		return false;
	}
	if (form.d_startdate.value == form.d_enddate.value) {
		alert("The start and end date are the same. You do not need to provide an end date in this case.");
		return false;
	}
	if (form.t_title.value == "") {
		alert("A title is required for this event.");
		return false;
	}
	if (form.c_hall.value == "") {
		if (confirm("Are you sure you do not live in a residence hall?")==true)
			return true;
		else
			return false;
	}
	return true;
}

function CheckRental(form) {
	if (form.c_firstname.value == "") {
		alert("Please provide us with your first name");
		return false;
	}
	if (form.c_lastname.value == "") {
		alert("Please provide us with your last name");
		return false;
	}
	if (form.c_email.value == "") {
		alert("Please provide us with an e-mail address");
		return false;
	}
	if (form.c_cell_phone.value == "" && form.c_room_phone.value == "") {
		alert("Please provide us with a cell phone or room phone");
		return false;
	}
	if (form.date.value == "") {
		alert("Date missing");
		return false;
	}
	if (form.c_hall.value == "") {
		if (confirm("Are you sure you do not live in a residence hall?")==true)
			return true;
		else
			return false;
	}
	return true;
}

function valDate(M, D, Y){
  Months= new Array(31,28,31,30,31,30,31,31,30,31,30,31);
  Leap  = false;

  if((Y % 4 == 0) && ((Y % 100 != 0) || (Y %400 == 0)))
    Leap = true;
  if (D != 99) {
    if((D < 1) || (D > 31) || (M < 1) || (M > 12) || (Y < 0))
      return(false);
    if((D > Months[M-1]) && !((M == 2) && (D > 28)))
      return(false);
    if(!(Leap) && (M == 2) && (D > 28))
      return(false);    
    if((Leap)  && (M == 2) && (D > 29))
      return(false);    
  }
};

function CheckDate(dateForm){
   cDate = dateForm.value;
   dSize = cDate.length;
   sCount= 0;
 
   if(cDate=='') return true;

   //Check out the slashs
   for(var i=0; i < dSize; i++)
      (cDate.substr(i,1) == "/") ? sCount++ : sCount;
   if (sCount != 2){
      alert("Date must be in the format ''mm/dd/yyyy''");
      dateForm.focus();
      return(false);
   };

   //Check if the year is a 2 or 4 digits string
   ySize = cDate.substring(cDate.lastIndexOf("/")+1,dSize).length;
   if(ySize<2 || ySize>4 || ySize == 3){
     alert('Invalid Date !');
     dateForm.focus();
     return false;
   };
   //Cut the date string into three parts (Month, Day & Year)
   idxBarI = cDate.indexOf("/");
   idxBarII= cDate.lastIndexOf("/");
   strM    = cDate.substring(0,idxBarI);
   strD    = cDate.substring(idxBarI+1,idxBarII);
   strY    = cDate.substring(idxBarII+1,dSize);

   strM = (strM.length < 2 ? '0'+strM : strM);   
   strD = (strD.length < 2 ? '0'+strD : strD);
   if(strY.length == 2)
      strY = (strY > 90  ? '19'+strY : '20'+strY);
   dateForm.value = strM+'/'+strD+'/'+strY;
 
   ok = valDate(strM, strD, strY);
    if(ok==false){ 
      alert("Invalid Date!");
      dateForm.focus();
      return false;
   };
   return true;
};

function CheckTime(timeForm) {
	// Checks if time is in HH:MM:SS AM/PM format.
	// The seconds are optional.
	timeStr = timeForm.value
	
	if(timeStr=='') return true;
	
	var timePat = /^(\d{1,2}):(\d{2})\s*(AM|am|PM|pm)$/;
	
	var matchArray = timeStr.match(timePat);
	if (matchArray == null) {
	alert("Time is not in a valid format. Use HH:MM AM/PM");
	timeForm.focus();
	return false;
	}
	hour = matchArray[1];
	minute = matchArray[2];
	ampm = matchArray[3];
	
	if (ampm=="") {
	alert("You must specify AM or PM.");
	timeForm.focus();
	return false;
	}
	if (hour < 0 || hour > 12) {
	alert("Hour must be between 1 and 12.");
	timeForm.focus();
	return false;
	}
	if (minute<0 || minute > 59) {
	alert ("Minute must be between 0 and 59.");
	timeForm.focus();
	return false;
	}
	
	return true;
}
