function initForm(){
	alert("Hello");
	var dt=new Date();
	var cur_mon=dt.getMonth() + 1;
	var cur_day=dt.getDate();
	var cur_yr=dt.getFullYear();
	var cur_hr=dt.getHours();
	document.getElementById('dlstSMon').value = cur_mon;
	document.getElementById('dlstSYear').value = cur_yr;
	MonthChanged('dlstSMon','dlstSYear','dlstSDay');
	document.getElementById('dlstSDay').value = cur_day;
	document.getElementById('dlstEMon').value = cur_mon;
	document.getElementById('dlstEYear').value = cur_yr;
	MonthChanged('dlstEMon','dlstEYear','dlstEDay');
	document.getElementById('dlstEDay').value = cur_day;
	calculateEndDate();
}

function toggleTimeSelectors(){
	var chk = document.getElementById("adx_allday");
	/* Enable or disable time dropdowns according to the users selection */
	document.getElementById("dlstSHr").disabled = chk.checked;
	document.getElementById("dlstSMin").disabled = chk.checked;
	document.getElementById("dlstSAMPM").disabled = chk.checked;
	document.getElementById("dlstEHr").disabled = chk.checked;
	document.getElementById("dlstEMin").disabled = chk.checked;
	document.getElementById("dlstEAMPM").disabled = chk.checked;
	document.getElementById("adx_midnight").disabled = chk.checked;
	validShedule();
}

function toggleForMidnight(){
	var chk = document.getElementById("adx_midnight");
	/* Enable or disable time dropdowns according to the users selection */
	document.getElementById("dlstEHr").disabled = chk.checked;
	document.getElementById("dlstEMin").disabled = chk.checked;
	document.getElementById("dlstEAMPM").disabled = chk.checked;
	document.getElementById("adx_allday").disabled = chk.checked;
	validShedule();
}

function showErrorMessage(id, text){
	document.getElementById(id).style.visibility = "visible";
	document.getElementById(id).innerHTML = text;
}

function hideErrorMessage(id){
	document.getElementById(id).style.visibility = "hidden";
	document.getElementById(id).innerHTML = "";
}

function validOpenEntryForm(){
	var SubmitterIsValid = validSubmitter();
    var EventInformationIsValid = validEventInformation();
    var ScheduleIsValid = validShedule();
    var ImageIsValid = validImage();
    var AttachmentIsValid = validAttachment();
    var OtherInformationIsValid  = validOtherInformation();
    var VenueInformationIsValid = validVenueInformation();
	
    /* Return if the form was valid or not */
    return (SubmitterIsValid && EventInformationIsValid && ScheduleIsValid && ImageIsValid && AttachmentIsValid && OtherInformationIsValid && VenueInformationIsValid);  
}

function validVenueInformation(){
	
	var valid_Venue	= true;
	
	if (document.getElementById('newVenueOption').value != "")//if new venue is checked
	{
		if(collate_adx_custom() == false && document.getElementById("adx_eventlocations").value == "0-0-0"){
			valid_Venue = false;
			showErrorMessage("adx_locationerror","<br>Please fill in new venue information.");
		}
		if(collate_adx_custom() == true && document.getElementById("adx_eventlocations").value != "0-0-0"){
			valid_Venue = false;
			showErrorMessage("adx_locationerror","<br>There are currently two venues selected.");
		}
		
	}else{
			if (document.getElementById("adx_eventlocations").value == "0-0-0")
			{
				clearNewVenueBoxes();
				valid_Venue = false;
				showErrorMessage("adx_locationerror","<br>Please select a location.");
			}
	}
	
	if (valid_Venue == true)
	{
		hideErrorMessage("adx_locationerror");	
	}

	return valid_Venue;
}
function collate_adx_custom(){
	var valid_newVenue = true;
	
	var venueName = document.getElementById("venueName").value;
	var venueAddress = document.getElementById("venueAddress").value;
	var venueCity = document.getElementById("venueCity").value;
	var venueZip = document.getElementById("venueZip").value;
	var text = venueName + "<br />" + venueAddress + "<br />" + venueCity + ", CA<br />" + venueZip;

	if (venueName == "" || venueAddress == "" || venueCity == "" || venueZip == "")
	{
		valid_newVenue = false;
	}else{
		document.getElementById("adx_custom1").innerHTML = text;
	}
	
	return valid_newVenue;
}
function changeVenue(){
	if(document.getElementById("adx_eventlocations").value != "0-0-0")
	{
		document.getElementById('newvenue').style.display = 'none';
		document.getElementById('newVenueOption').value = "";	
		clearNewVenueBoxes();		
	}
}
function clearNewVenueBoxes(){
		document.getElementById('venueName').value = "";
		document.getElementById('venueAddress').value = "";
		document.getElementById('venueCity').value = "";
		document.getElementById('venueZip').value = "";	
		document.getElementById("adx_custom1").innerHTML = "";
}
function validSubmitter(){
	/* The default is true */
	var valid_Submitter = true;
	
	/* Submitter First Name */
	if(document.getElementById("adx_fname") != null){
		var FName = document.getElementById("adx_fname").value; /* 50 */
		if(trimStr(FName) == ""){
			showErrorMessage("adx_fnameerror","<br>Please enter a First Name in the field provided.");
			valid_Submitter = false;
		}
		else{hideErrorMessage("adx_fnameerror");}
	}
	
	/* Submitter Last Name */
	if(document.getElementById("adx_lname") != null){
		var LName = document.getElementById("adx_lname").value; /* 50 */
		if(trimStr(LName) == ""){
			showErrorMessage("adx_lnameerror","<br>Please enter a Last Name in the field provided.");
			valid_Submitter = false;
		}
		else{hideErrorMessage("adx_lnameerror");}
	}
	
	/* Submitter Email */
	if(document.getElementById("adx_email") != null){
		var Email = document.getElementById("adx_email").value; /* 100 */
		if(trimStr(Email) == ""){
			showErrorMessage("adx_emailerror","<br>Please enter an Email Address in the field provided.");
			valid_Submitter = false;
		}
		else if(CheckEmail(Email)==false){
			showErrorMessage("adx_emailerror","<br>The Email Address entered is not valid.");
			valid_Submitter = false;
		}
		else{hideErrorMessage("adx_emailerror");}
	}
	
	/* Submitter Organization */
	if(document.getElementById("adx_organizationRequired") != null){
		/* Organization is required */
		var Organization = document.getElementById("adx_organization").value; /* 100 */
		if(trimStr(Organization) == ""){
			showErrorMessage("adx_organizationerror","<br>Please enter an Organization in the field provided.");
			valid_Submitter = false;
		}
		else{hideErrorMessage("adx_organizationerror");}
	}
	
	/* Submitter Phone */
	if(document.getElementById("adx_phoneRequired") != null){
		/* Phone is required */
		var Phone = document.getElementById("adx_phone").value; /* 100 */
		
		if(trimStr(Phone) == ""){
			showErrorMessage("adx_phoneerror","<br>Please enter a Phone # in the field provided.");
			valid_Submitter = false;
		}
		else{hideErrorMessage("adx_phoneerror");}
	}
	
	return valid_Submitter;
}

function validEventInformation(){
	/* The default is true */
	var valid_EventInformation = true;
	
	/* Event Name */
	if(document.getElementById("adx_eventname") != null){
		var EventName = document.getElementById("adx_eventname").value; /* 100 */
		if(trimStr(EventName) == ""){
			showErrorMessage("adx_eventnameerror","<br>Please enter an Artist Name in the field provided.");
			valid_EventInformation = false;
		}
		else{hideErrorMessage("adx_eventnameerror");}
	}
	
	/* Event Categorization */
	if(document.getElementById("adx_eventcategorizations") != null){
		var EventCategorization = document.getElementById("adx_eventcategorizations").value; /* 1000 */
		if(EventCategorization == "0-0"){
			showErrorMessage("adx_eventcategorizationserror","<br>Please select an Event Categorization from the field provided.");
			valid_EventInformation = false;
		}
		else{hideErrorMessage("adx_eventcategorizationserror");}
	}
	
	/* Event Description */
	if(document.getElementById("adx_eventdescription") != null){
		var EventDescription = document.getElementById("adx_eventdescription").value; /* 1000 */
		if(trimStr(EventDescription) == ""){
			showErrorMessage("adx_eventdescriptionerror","<br>Please enter an Event Description in the field provided.");
			valid_EventInformation = false;
		}
		else{hideErrorMessage("adx_eventdescriptionerror");}
	}
	
	return valid_EventInformation;
}

function validShedule(){
	var valid_Schedule = true;/* The default is true */
	
	/* Get the date information from the form */
	var StartDate = getDateInput("S");
	/* This sets the start date with no time specified */
	var StartDateNoTime = new Date();
	StartDateNoTime.setFullYear(StartDate.getFullYear(), StartDate.getMonth(), StartDate.getDate());
	StartDateNoTime.setHours(0,0,0,0);
	var EndDate = getDateInput("E");
	
	/* These are the time elements the user chose */
	var allday = document.getElementById("adx_allday").checked;
	var midnight = document.getElementById("adx_midnight").checked;
	var sHour;
	var eHour;
	if(allday==true){
		/* All Day Is Checked */
		sHour = "00";
		eHour = "00";
	}
	else if(midnight==true){
	    /* Midnight Is Checked */
	    eHour = "00";
	}
	else{
		/* All Day Is Not Checked */
		sHour = document.getElementById("dlstSHr").value;
		eHour = document.getElementById("dlstEHr").value;
	}
	
	if(midnight==true){
	    if(sHour == "00"){
		    /* Midnight without a start time is invalid */
		    showErrorMessage("adx_dateerror","A Start Time must be specified when an event ends at Midnight.");
		    return(false);
	    }
	}
	
	/* Always make sure error message are hidden */
	hideErrorMessage("adx_dateerror")
	if(sHour == "00" && eHour != "00"){
		/* An end time without a start time is invalid */
		showErrorMessage("adx_dateerror","An end time cannot be specified without a start time.");
		valid_Schedule = false;
	}
	else{
		if(sHour != "00" && eHour != "00"){
			/* Start & End Times */
			if(StartDate >= EndDate){
				/* The starting date must be less than the ending date. */
				showErrorMessage("adx_dateerror","The start date selected cannot be after the end date. Please change your selections.");
				valid_Schedule = false;
			}
		}
		else if(sHour != "00" && eHour == "00"){
			/* Start Time & No End Time */
			if(StartDateNoTime > EndDate){
				showErrorMessage("adx_dateerror","The start date selected cannot be after the end date. Please change your selections.");
				valid_Schedule = false;
			}
		}
		else{
			/* No Time */
			if(StartDate > EndDate){
				showErrorMessage("adx_dateerror","The start date selected cannot be after the end date. Please change your selections.");
				valid_Schedule = false;
			}
		}
	}
	
	if(document.getElementById("adx_recurtype").value == "One Time" && valid_Schedule == true){
		/* This should only be done when the schedule is valid. */
		document.getElementById("adx_occurcount").value = "1";
	}
		
	/* Return if this was a valid schedule */
	return valid_Schedule;
}

function validOtherInformation(){
	/* The default is true */
	var valid_OtherInformation = true;
	
	/* This validates all the custom imput fields. */
	var AddmissionInformation = validOtherInformationHelper("adx_admission"); /* 255 */
	var OtherDetails = validOtherInformationHelper("adx_otherdetails"); /* 500 */
	var Custom1 = validOtherInformationHelper("adx_custom1"); /* 255 */
	var Custom2 = validOtherInformationHelper("adx_custom2"); /* 255 */
	
	var InternalNotes; /* 1000 */
	if(document.getElementById("adx_internalnotes") != null){
		InternalNotes = document.getElementById("adx_internalnotes").value;
	}
	
	var facilityRoomSelection = true;
	hideErrorMessage("adx_eventlocationserror"); /*Hide error messages by default*/
	if(document.getElementById("adx_facilitycalendar")!=null){
	    /*There is a facility identifier*/
	    if(document.getElementById("adx_facilitycalendar").value=="Y"){
	        /*This is a facility calendar*/
	        var fac = document.getElementById("adx_eventlocations").value.split("-");
	        if(fac[0]=="0"){
	            /*No Facility Was Selected*/
	        }
	        else{
	            /*There is a facility selected, make sure a room is selected*/
	            if(fac[2]=="0"){
	                facilityRoomSelection=false;
	                showErrorMessage("adx_eventlocationserror", "<br />Please select a Room from the dropdown providfed.");
	            }
	        }
	    }
	}
	return AddmissionInformation && OtherDetails && Custom1 && Custom2 && facilityRoomSelection;
}

function validOtherInformationHelper(customControlName){
    var validInput=true;
    if(document.getElementById(customControlName) != null){
        var customInput="";/*This is the custom selection-input*/
        if(document.getElementById(customControlName).value == null){
            /* Checkboxes */
            var index=0;
            while(document.getElementById(customControlName+"_"+index)!=null){
                if(document.getElementById(customControlName+"_"+index).checked == true){
                    /* This checkbox was checked */
                    customInput="valid selection";
                }
                index++;
            }
        }
        else{
            /* Dropdown or Textbox */
            customInput = trimStr(document.getElementById(customControlName).value);
        }
        
        if(document.getElementById(customControlName+"Required") != null){
		    if(trimStr(customInput) == ""){
		        /* Tell the user to select or enter something.*/
			    showErrorMessage(customControlName+"error","<br>This is a required event field.");
			    validInput=false;
		    }
		    else{
		        hideErrorMessage(customControlName+"error");
		    }
	    }
    }  
    return validInput;
}

function validImage(){
	if(document.getElementById("adx_image") != null){
		/* Validate the Extention */
		var filepath = document.getElementById("adx_image").value;
			
		if(trimStr(filepath) != ""){
			/* The user selected a file to upload, validate the extention and alt text */
			var filename = filepath.substring(filepath.lastIndexOf("\\") + 1);
			var extention = filename.substring(filename.lastIndexOf(".") + 1);
			
			switch(extention){
				case "gif":/* Graphical Interchange Format */
				case "jpg":/* Joint Photographic Experts Group */
				case "jpeg":/* Joint Photographic Experts Group */
					/* These are the only valid image extentions */
					hideErrorMessage("adx_imageerror");
					break;
				default:
					/* The image uploaded is not valid */
					showErrorMessage("adx_imageerror","<br>The image being uploaded is not a valid type.");
					return(false);
					break;
			}
				
			/* Get the alt text entered by the user */
			var ImageAlt = document.getElementById("adx_imagealt").value;
			if(trimStr(ImageAlt) == ""){
				/* Image Alt Text is REQUIRED */
				showErrorMessage("adx_imagealterror","<br>Please enter Image Alt Text in the field provided.");
				return(false);
			}
			else{
				/* Hide any error message */
				hideErrorMessage("adx_imagealterror");
			}	
		}
	}
	return(true);	
}

function validAttachment(){
	if(document.getElementById("adx_attachment") != null){
		/* Validate the Extention */
		var filepath = document.getElementById("adx_attachment").value;
		
		if(trimStr(filepath) != ""){
			/* The user selected a file to upload, validate the extention and alt text */
			var filename = filepath.substring(filepath.lastIndexOf("\\") + 1);
			var extention = filename.substring(filename.lastIndexOf(".") + 1);
			
			switch(extention){
				case "gif":/* Graphical Interchange Format */
				case "jpg":/* Joint Photographic Experts Group */
				case "jpeg":/* Joint Photographic Experts Group */
				case "png":/* Portable Network Graphics */
				case "mov":/* Movie */
				case "qt":/* Quick Time */
				case "avi":/* Audio Video Interleave */
				case "wmv":/* Windows Media Video */
				case "wma":/* Windows Media Audio */
				case "wmf":/* Windows Meta File */
				case "wav":/* Wave Type */
				case "mp3":/* Mpeg-3 Type */
				case "m4a":/* Mpeg-4 Audio File Type (iTunes) */
				case "ram":/* Real Audio Media */
				case "doc":/* MS Word */
				case "xls":/* MS Excel */
				case "ppt":/* MS Power Point */
				case "vsd":/* MS Visio */
				case "pdf":/* Personal Document File */
				case "txt":/* Text */
				case "swf":/* Flash */
					/* These are the valid attachment extentions */
					hideErrorMessage("adx_attachmenterror");
					break;
				default:
					/* The attachment uploaded is not valid */
					showErrorMessage("adx_attachmenterror","<br>The attachment being uploaded is not a valid type.");
					return(false);
					break;
			}
			
			/* Get the attachmentB text entered by the user */
			var AttachmentText = document.getElementById("adx_attachmenttext").value;
			if(trimStr(AttachmentText) == ""){
				/* Image Alt Text is REQUIRED */
				showErrorMessage("adx_attachmenttexterror","<br>Please enter Attachment Link Text in the field provided.");
				return(false);
			}
			else{
				/* Hide any error message */
				hideErrorMessage("adx_attachmenttexterror");
			}	
		}
	}
	return(true);
}

function getDateInput(start_end){
	/* Check the dates and times entered */
	var theYear = document.getElementById("dlst" + start_end + "Year").value;
	var theMonth = document.getElementById("dlst" + start_end + "Mon").value;
	var theDay = document.getElementById("dlst" + start_end + "Day").value;
	var theHour = document.getElementById("dlst" + start_end + "Hr").value;
	var theMinute = document.getElementById("dlst" + start_end + "Min").value;
	var theAMPM = document.getElementById("dlst" + start_end + "AMPM").value;
	
	var allday = document.getElementById("adx_allday").checked;
	if(allday){
		theHour = "";
		theMinute = "";
	}
	
	var theDate = new Date();
	theDate.setFullYear(theYear, theMonth - 1, theDay);
		
	if(theHour != "" && theMinute != ""){
		if(theAMPM == "PM"){
			if(theHour == "12"){
				theDate.setHours(theHour,theMinute,0,0);
			}
			else{
				theDate.setHours(parseInt(theHour) + 12,theMinute,0,0);
			}
		}
		else{
			if(theHour == "12"){
				theDate.setHours(0,theMinute,0,0);
			}
			else{
				theDate.setHours(theHour,theMinute,0,0);
			}
		}
	}
	else{
		theDate.setHours(null,null,null,null);
	}
	
	/* This is the date scraped from the form */
	return (theDate);
}

function calculateEndDate(){
	var rectype = document.getElementById("adx_recurtype").value;
	var count = document.getElementById("adx_occurcount").value;
	var dt = new Date();	
	
	if(count == ""){
		/* There must be at least 1 occurrence */
		document.getElementById("adx_occurcount").value = 1;
		count=1;
	}
	
	/* Make sure the occurrence input field is enabled */
	/* document.getElementById("adx_occurcount").disabled = false; */
	
	/* This ensures the count variable will be treated as an integer */
	count = parseInt(count);
	count--; /* Always decrement the count value by 1 before starting */
	
	/* Get the start date of the event input by the user, without time. */
	dt = getDateInput("S");
	dt.setHours(0,0,0,0);
	
	/* Hide any date references */
	hideErrorMessage("adx_occurdate");
	if (rectype == "One Time"){
		/* Make the occurrence counter 1 */
		document.getElementById("adx_occurcount").value = "1";
		/* document.getElementById("adx_occurcount").disabled = true; */
	}
	else if(rectype == "Interval1"){
		while(count > 0)
		{
			/* These are all occurrences of the event */
			dt.setFullYear(dt.getFullYear(),dt.getMonth(),dt.getDate()+1);
			count--;
		}
		showErrorMessage("adx_occurdate","Last Occurrence Date: " + (dt.getMonth() + parseInt(1)) + "/" + dt.getDate() + "/" + dt.getFullYear());
	}
	else if(rectype == "Weekly1"){
		while(count > 0)
		{
			/* These are all occurrences of the event */
			dt.setFullYear(dt.getFullYear(),dt.getMonth(),dt.getDate()+7);
			count--;
		}
		showErrorMessage("adx_occurdate","Last Occurrence Date: " + (dt.getMonth() + parseInt(1)) + "/" + dt.getDate() + "/" + dt.getFullYear());
	}
	else if(rectype == "Monthly by Date1"){
		var tempDate = new Date();
		tempDate.setHours(0,0,0,0); /* There should be no time */
		var dayVal = dt.getDate(); /* Day we are looking for */
		var numDays; /* Num days in a month */
		while(count > 0)
		{
			tempDate.setFullYear(dt.getFullYear(),dt.getMonth()+1,1); /* How many days are in the next month */
			numDays = DaysInMonth(tempDate.getFullYear(),tempDate.getMonth());
			while(numDays < dayVal){
				/* Look at the next month, until it has the right # of days */
				tempDate.setFullYear(tempDate.getFullYear(),tempDate.getMonth()+1,1);
				numDays = DaysInMonth(tempDate.getFullYear(),tempDate.getMonth());
				/* This block correct cases where the month does not have 31 or 30 days */
				/* This generally affects the month of Feb. or when the event occurres  */
				/* on a monthly basisi on day 31 */
			}
			/* This is the next occurrence */	
			dt.setFullYear(tempDate.getFullYear(),tempDate.getMonth(),dayVal);
			count--;
		}
		showErrorMessage("adx_occurdate","Last Occurrence Date: " + (dt.getMonth() + parseInt(1)) + "/" + dt.getDate() + "/" + dt.getFullYear());
	}
	else if(rectype == "Yearly by Date"){
		while(count > 0)
		{
			/* These are all occurrences of an event */
			dt.setFullYear(dt.getFullYear()+1,dt.getMonth(),dt.getDate());
			count--;
		}
		showErrorMessage("adx_occurdate","Last Occurrence Date: " + (dt.getMonth() + parseInt(1)) + "/" + dt.getDate() + "/" + dt.getFullYear());
	}
}

function openGrid(url, month, day, year){
	var qs = "month=" + month + "&day=" + day + "&year=" + year;
	window.open(url + "openentryjscalendar.aspx?" + qs,'','toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=190,height=200,left=212,top=50');
}

function syncDateSelectors(){
	//Set the dates with no time
	var sDt = new Date();
	sDt = getDateInput("S");
	sDt.setHours(0,0,0,0);
	
	//Set the end date selectors
	document.getElementById("dlstEMon").value = sDt.getMonth() + 1;
	document.getElementById("dlstEDay").value = sDt.getDate();
	document.getElementById("dlstEYear").value = sDt.getFullYear();
	MonthChanged('dlstEMon','dlstEYear','dlstEDay');
}