function trimEmail(value) {
   var temp = value;
   var obj = /^(\s*)([\W\w]*)(\b\s*$)/;
   if (obj.test(temp)) { temp = temp.replace(obj, '$2'); }
   var obj = / +/g;
   temp = temp.replace(obj, " ");
   if (temp == " ") { temp = ""; }
   document.form1.email.value = temp;
   return emailCheck(temp)
}

function ctRadio(checkThisName){
	for(k =0; k < checkThisName.length; k++){
		if(checkThisName[k].checked == true){
		return true;
		}
	}
	return false;
}

var checkboxOkay = "";

/* using the ctRadio function loop through each AV checkbox to see if one of the is checked or if they have entered text into the textarea
	if neiter is true then set the checkboxOkay var to 1 else 0 (used below in the else if)

*/
function checkbox(){
	if(!ctRadio(document.form1.other_equipment) && document.form1.otherAV.value.length == 0){
		alert("Please specify the AV equipment or services needed for this event.");
		checkboxOkay = 1;
	}else{
	checkboxOkay = 0;
	}
}

function isBlank(checkThis){
	//this line will get rid of all white space
	checkThis = checkThis.replace(/\s+/g ,"");		
	//alert("checking if this is blank: " + checkThis);
	retval = (checkThis == "" || checkThis == null || checkThis == " ")?true: false;		
	return retval;
}

function validate(theForm){
firstname = "" + document.form1.firstname.value;
lastname = "" + document.form1.lastname.value;
recurring = "" + document.form1.recurring.value;
area_code = "" + document.form1.ph_area_code.value;
prefix = "" + document.form1.ph_prefix.value;
suffix = "" + document.form1.ph_suffix.value;
email = "" + document.form1.email.value;
dept = "" + document.form1.dept.value;
title = "" + document.form1.title.value;
//sponsor = "" + document.form1.sponsor.value;



start_date = "" + document.form1.start_date.value;
end_date = "" + document.form1.end_date.value;
/*startdate_day = "" + document.form1.startdate_day.value;
startdate_year = "" + document.form1.startdate_year.value;*/

starttime_hour = "" + document.form1.starttime_hour.value;
starttime_minute = "" + document.form1.starttime_minute.value;
starttime_ampm = "" + document.form1.starttime_ampm.value;

endtime_hour = "" + document.form1.endtime_hour.value;
endtime_minute = "" + document.form1.endtime_minute.value;
endtime_ampm = "" + document.form1.endtime_ampm.value;

numattending = "" + document.form1.number_attending.value;
//Check for occurance. If a week is selected check to see that others are selected
//recurring = "" + document.form1.recurring.checked;
firstchoiceroom = "" + document.form1.firstchoiceroom.value;
indexnum = "" + document.form1.indexnum.value;

index1Val = document.form1.showHideIndex_1_text.value;
index2Val = document.form1.showHideIndex_2_text.value;

dayBoxes = document.form1.days.length;		
dayValue = "";								
weekBoxes = document.form1.weeks.length;	//check the weeks checkboxes
weekValue = "";								//checked box value
monthBoxes = document.form1.months.length;
monthValue = "";

YesNeedsAV = "" + document.form1.needAV[0].checked;
NoNeedsAV = "" + document.form1.needAV[1].checked;

// check to see if they checked the 'yes' need av equip or services
if (YesNeedsAV == 'true'){
	// call the checkbox function to see if they have checked any checkboxes
	checkbox();
}
	
if (firstname == "" || firstname == 'undefined'){
	alert('Firstname is required');
     document.form1.firstname.focus();
     return false; // cancel submission
	}
else if (document.form1.recurring[0].checked == true){
		//----------------------------------------------------------------
		//----------- DAYS -----------------------------------------------
		//----------------------------------------------------------------
		
		for (i = 0; i < dayBoxes; i++) {
			if (document.form1.days[i].checked) {
				dayValue = dayValue + document.form1.days[i].value + " ";
			}
		}
		if (dayValue == "") {
			alert("You have not chosen a DAY for your recurring meeting.");
			return false;
		}
		else {}			
		
		for (i = 0; i < weekBoxes; i++) {
			if (document.form1.weeks[i].checked) {
				weekValue = weekValue + document.form1.weeks[i].value + " ";
			}
		}
		if (weekValue == "") {
			alert("You have not chosen a WEEK for your recurring meeting.");
			return false;
		}
		else {}
		
		//----------------------------------------------------------------
		//----------- MONTHS ---------------------------------------------
		//----------------------------------------------------------------
		
		for (i = 0; i < monthBoxes; i++) {
			if (document.form1.months[i].checked) {
				monthValue = monthValue + document.form1.months[i].value + " ";
			}
		}
		if (monthValue == "") {
			alert("You have not chosen a MONTH for your recurring meeting.");
			return false;
		}
		else {}				
	 }	
	 
else if (lastname == "" || lastname == 'undefined'){
	alert('Lastname is required.');
     document.form1.lastname.focus();
     return false; // cancel submission
	 }		 
else if ( isBlank(email) || email == 'undefined'){
	alert('An Email Address is required.');
     document.form1.email.focus();
     return false; // cancel submission
	 }	 
else if( !isBlank(email) ){
	//let's check the email here.
		retval = emailCheck(email);
		if(!retval){
			//alert('The email address does not seem to be in a valid format.');
			 document.form1.email.focus();
			 return false; // cancel submission
		}
	}	
else{} 


if (area_code == "" || area_code == 'undefined'){
	alert('An telephone area code is required.');
     document.form1.ph_area_code.focus();
     return false;
	 }
else if (prefix == "" || prefix == 'undefined'){
	alert('A telephone prefix is required.');
     document.form1.ph_prefix.focus();
     return false;
	 }
else if (suffix == "" || suffix == 'undefined'){
	alert('A telephone suffix is required.');
     document.form1.ph_suffix.focus();
     return false;
	 }
else if (dept == "" || dept == 'undefined'){
	alert('A Department is required.');
     document.form1.dept.focus();
     return false; // cancel submission
	 }	 
else if (title == "" || title == 'undefined'){
	alert('An Event Title is required.');
     document.form1.title.focus();
     return false; // cancel submission
	 }
else if (start_date == "No" || start_date == ""){
	alert('Please select a Start Date');
     document.form1.start_date.focus();
     return false; // cancel submission
	 }	 
else if (end_date == "No" || end_date == ""){
	alert('Please select a End Date');
     document.form1.end_date.focus();
     return false; // cancel submission
	 }	
else if (starttime_hour == "No" || starttime_hour == "" || starttime_hour == "Hour" || starttime_hour == null){
	alert('Please select a Start Time (Hour)');
	document.form1.starttime_hour.focus();
	return false;
	}
else if (starttime_minute == "No" || starttime_minute == "" ){
	alert('Please select a Start Time (Minute)');
	document.form1.starttime_minute.focus();
	return false;
	}
else if (starttime_ampm == "No" || starttime_ampm == "am/pm" || starttime_ampm == " "){
	alert('Please select a Start Time (AM/PM)');
	document.form1.starttime_ampm.focus();
	return false;
	}
else if (endtime_hour == "No" || endtime_hour == "" || endtime_hour == "Hour"){
	alert('Please select an End Time (Hour)');
	document.form1.endtime_hour.focus();
	return false;
	}
else if (endtime_minute == "No" || endtime_minute == ""){
	alert('Please select an End Time (Minute)');
	document.form1.endtime_minute.focus();
	return false;
	}
else if (endtime_ampm == "No" || endtime_ampm == "am/pm" || endtime_ampm == ""){
	alert('Please select an End Time (AM/PM)');
	document.form1.endtime_ampm.focus();
	return false;
	}
else if (numattending == "" || numattending == "undefined"){
	alert('Please enter a Numeric value for the Approximate Attendance');
	document.form1.number_attending.focus();
	return false;
	}
else if (isNaN(numattending)){
	alert('Please enter a numeric value for people attending.');
	document.form1.number_attending.focus();
	return false;
}
else if (firstchoiceroom == "" || firstchoiceroom == "undefined" || firstchoiceroom == "--No Room Selected--"){	
	alert('Please select a room');
	document.form1.firstchoiceroom.focus();
	return false;
	}
else if(document.getElementById('showHideIndex_1').style.display == 'inline' && index1Val == ''){
	alert("Please input an index number for first room choice.");
	document.form1.showHideIndex_1_text.focus();
	return false;
}

else if(document.getElementById('showHideIndex_2').style.display == 'inline' && index2Val == ''){
	alert("Please input an index number for second room choice.");
	document.form1.showHideIndex_2_text.focus();
	return false;
}
// make sure they answered the needs AV question
else if (YesNeedsAV == 'false' && NoNeedsAV == 'false'){
	alert('Please let us know if you need AV equipment or services for this event');
	document.form1.needAV[0].focus();
	return false;
	}
// if they need AV but havent checked any check boxes or entered anything in the textarea
else if(checkboxOkay == 1){
	document.form1.other_equipment[0].focus();
	return false
	}
else{
	
	var itemchecked = false;
	 oe_check = document.form1.other_equipment;
	 //loop thorught the checkboxes for the audio equipment
	 for(var m = 0 ; m < oe_check.length ; m++){
		if(oe_check[m].checked){
			itemchecked = true;
		}
	  }
	  
	  tex_item = document.form1.otherAV.value;
	  
	  //check to see if the additional audio equipment box was filled out
	  if( !(  isBlank(tex_item) ) ){
	  	itemchecked = true;		
	  }	
	
		// check to make sure that the av services index box was filled
	  if (itemchecked){
		
		if(isBlank( document.form1.indexnum.value ) ){
			alert("An Index Number is Required for AV Services");
			document.form1.indexnum.focus();
			return false;
		}
	}
	
	//okay ..... everything is good....but check the dates to make sure they are okay before we can proceed
	//validate dates	
	goodStarDate = dateValidator(start_date, 0);
	goodEndDate  = dateValidator(end_date, 1);
		
	goodDates = (goodStarDate && goodEndDate)? true: false;
	
	if(!goodDates){
		return false;
	}
	//okay our dates are valid...so let's compare them.
	good_dates = checkDates(start_date, end_date); 
	if(!good_dates){	
		return false;
	}
	
	return true; 	
	}
}	



/**************************************
 *  This function will take in a start and end days in the following format:
 *   mm/dd/yyyy
 * 
 *************************************/

function checkDates(day_begin, day_end){
	bad_date= false;
	
	//get different dates
	day_end_mn = day_end.substring(0, 2);
	day_end_dy = day_end.substring(3, 5);
	day_end_yr = day_end.substring(6, day_end.length);
	
	day_begin_mn = day_begin.substring(0, 2);
	day_begin_dy = day_begin.substring(3, 5);
	day_begin_yr = day_begin.substring(6, day_begin.length);
	
	//check to make sure end is not before start..
	if(day_begin_yr == day_end_yr){
		if(day_begin_mn == day_end_mn){
			if(day_begin_dy > day_end_dy){
				bad_date= true;
			}
		}		
		else if(day_begin_mn > day_end_mn){
			bad_date = true;
		}
		//input is good
		else{}
	}
	//case for bad input	
	else if(day_begin_yr > day_end_yr){
		bad_date = true;
	}
	//input date is good
	else{}
	
	if(bad_date){
		alert("The date that you entered for the end date comes before the start date.  Please enter a start date that precedes the end date.");		
		return false;
	}
	return true;
}

function isBlank(checkThis){
	retval_0 = (checkThis == "" || checkThis == " " || checkThis == null)?true: false;
	return retval_0;
}


