var daysOfWeekArr = new Array('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat');
var daysOfMonthArr = new Array('1st', '2nd', '3rd', '4th', '5th', '6th', '7th', '8th', '9th', '10th', '11th', '12th', '13th', '14th', '15th', '16th', '17th', '18th', '19th', '20th', '21st', '22nd', '23rd', '24th', '25th', '26th', '27th', '28th', '29th', '30th', '31st');
var monthsOfYearArr = new Array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
var weeksOfMonthArr = new Array();
weeksOfMonthArr['01#02#03#04#05#06#07'] = '1st';
weeksOfMonthArr['08#09#10#11#12#13#14'] = '2nd';
weeksOfMonthArr['15#16#17#18#19#20#21'] = '3rd';
weeksOfMonthArr['22#23#24#25#26#27#28'] = '4th';
weeksOfMonthArr['29#30#31'] = '5th';

function checkForm(objForm){
	var strMessage = '';
	var objFocus;
	if(phoneError(objForm.contactPhone) && emailError(objForm.contactEmail)){
		strMessage = 'Please input either a phone number or email address for the contact person.\n' + strMessage;
		objFocus = objForm.contactPhone;
	}
	if(objForm.contactName.value == ''){
		strMessage = 'Please enter the contact name.\n' + strMessage;
		objFocus = objForm.contactName;
	}
	if(phoneError(objForm.moreInfoPhone) && emailError(objForm.moreInfoEmail)){
		strMessage = 'Please input either a phone number or email address for obtaining more event information.\n' + strMessage;
		objFocus = objForm.moreInfoPhone;
	}
	if(objForm.moreInfoName.value == ''){
		strMessage = 'Please enter the more information name.\n' + strMessage;
		objFocus = objForm.moreInfoName;
	}
	if(addressError(objForm)){
		strMessage = 'Please enter at least one address.\n' + strMessage;
		if(objForm.street[0]){
			objFocus = objForm.street[0];
		}
		else{
			objFocus = objForm.street;
		}
	}
	if(objForm.locCity.value == ''){
		strMessage = 'Please enter the event\'s city.\n' + strMessage;
		objFocus = objForm.eventName;
	}
	if(objForm.category[0].type == 'checkbox' && noneChecked(objForm.category)){
		strMessage = 'Your event must belong to at least one category.\n' + strMessage;
	}
	if(objForm.eventDatesDescription.value == ''){
		strMessage = 'Please enter a description for the event dates.\n' + strMessage;
		objFocus = objForm.eventDatesDescription;
	}
	if(objForm.hawaii.value == ''){
		strMessage = 'Please describe how the event shares Hawaii.\n' + strMessage;
		objFocus = objForm.hawaii;
	}
	else if(wordCountError(objForm.hawaii, objForm.hawaiiCount, 100)){
		strMessage = 'The Hawaii tie-in must contain 100 words or less.\n' + strMessage;
		objFocus = objForm.hawaii;
	}
	if(objForm.eventDescription.value == ''){
		strMessage = 'Please enter the event description.\n' + strMessage;
		objFocus = objForm.eventDescription;
	}
	else if(wordCountError(objForm.eventDescription, objForm.eventDescriptionCount, 100)){
		strMessage = 'The event description must contain 100 words or less.\n' + strMessage;
		objFocus = objForm.eventDescription;
	}
	if(objForm.eventName.value == ''){
		strMessage = 'Please enter the event name.\n' + strMessage;
		objFocus = objForm.eventName;
	}
	if(strMessage == ''){
		objForm.backButton.value = 'Yes';
		return true;
	}
	else{
		alert(strMessage);
		if(objFocus) objFocus.select();
		return false;	
	}
}

function addressError(objForm){
	if(objForm.street[0]){
		for(var i = 0; i < objForm.street.length; i++){
			if(objForm.street[i].value != '' && objForm.city[i].value != '' && objForm.state[i].value != '' && objForm.zip[i].value != '') return false;
		}
	}
	else if(objForm.street.value != '' && objForm.city.value != '' && objForm.state.value != '' && objForm.zip.value != ''){
		return false;
	}
	return true;
}

function insertAnd(listOfItems){
	if(listOfItems == '') return '';
	listOfItems = listOfItems.substring(0, listOfItems.length - 1);
	var tempIndex = listOfItems.lastIndexOf(' ');
	if(tempIndex == -1) return listOfItems;
	return listOfItems.substring(0, tempIndex) + ' &' + listOfItems.substring(tempIndex);
}

function buildEventDateDescription(objForm){
	var eventDateDescriptionStr = '';
	var daysOfWeekStr = '';
	var weeksOfMonthStr = '';
	var daysOfMonthStr = '';
	var monthsOfYearStr = '';
	var startDateStr = objForm.eventStartMonth[objForm.eventStartMonth.selectedIndex].text.substring(0, 3) + ' ' + objForm.eventStartDay[objForm.eventStartDay.selectedIndex].text + ', ' + objForm.eventStartYear[objForm.eventStartYear.selectedIndex].text;
	var endDateStr = objForm.eventEndMonth[objForm.eventEndMonth.selectedIndex].text.substring(0, 3) + ' ' + objForm.eventEndDay[objForm.eventEndDay.selectedIndex].text + ', ' + objForm.eventEndYear[objForm.eventEndYear.selectedIndex].text;
	for(var i = 0; i < objForm.daysOfWeek.length; i++){
		if(objForm.daysOfWeek[i].checked == true) daysOfWeekStr += daysOfWeekArr[objForm.daysOfWeek[i].value] + ' ';
	}
	for(i = 0; i < objForm.weeksOfMonth.length; i++){
		if(objForm.weeksOfMonth[i].checked == true) weeksOfMonthStr += weeksOfMonthArr[objForm.weeksOfMonth[i].value] + ' ';
	}
	if(objForm.weeksOfMonthLast.checked == true) weeksOfMonthStr += 'Last ';
	for(i = 0; i < objForm.daysOfMonth.length; i++){
		if(objForm.daysOfMonth[i].checked == true) daysOfMonthStr += daysOfMonthArr[objForm.daysOfMonth[i].value - 1] + ' ';
	}
	for(i = 0; i < objForm.monthsOfYear.length; i++){
		if(objForm.monthsOfYear[i].checked == true) monthsOfYearStr += monthsOfYearArr[objForm.monthsOfYear[i].value - 1] + ' ';
	}
	daysOfWeekStr = insertAnd(daysOfWeekStr);
	weeksOfMonthStr = insertAnd(weeksOfMonthStr);
	daysOfMonthStr = insertAnd(daysOfMonthStr);
	monthsOfYearStr = insertAnd(monthsOfYearStr);
	if(daysOfWeekStr + weeksOfMonthStr + daysOfMonthStr + monthsOfYearStr != ''){
		if(weeksOfMonthStr != ''){
			eventDateDescriptionStr = weeksOfMonthStr + ' ' + daysOfWeekStr;
		}
		else if(daysOfWeekStr != ''){
			eventDateDescriptionStr = 'Every ' + daysOfWeekStr;
		}
		if(eventDateDescriptionStr != '' && daysOfWeekStr == '') eventDateDescriptionStr += 'Week';
		if(eventDateDescriptionStr == '' && daysOfMonthStr != '') eventDateDescriptionStr = daysOfMonthStr;
		if(eventDateDescriptionStr != ''){
			if(monthsOfYearStr != ''){
				eventDateDescriptionStr += ' of ' + monthsOfYearStr;
			}
			else if(weeksOfMonthStr != '' || daysOfMonthStr != ''){
				eventDateDescriptionStr += ' of Every Month';
			}
		}
		else{
			if(monthsOfYearStr != ''){
				eventDateDescriptionStr += 'Every Day of ' + monthsOfYearStr;
			}
		}
	}
	if(startDateStr != 'Mon Day, Year'){
		if(endDateStr != 'Mon Day, Year'){
			if(startDateStr == endDateStr){
				eventDateDescriptionStr = startDateStr;
			}
			else if(eventDateDescriptionStr == ''){
				eventDateDescriptionStr = startDateStr + ' - ' + endDateStr;
			}
			else{
				eventDateDescriptionStr = startDateStr + ' - ' + endDateStr + '\n' + eventDateDescriptionStr;
			}
		}
		else{
			if(eventDateDescriptionStr == ''){
				eventDateDescriptionStr = 'Starting ' + startDateStr;
			}
			else{
				eventDateDescriptionStr = eventDateDescriptionStr + '\nStarting ' + startDateStr;
			}
		}
	}
	else if(endDateStr != 'Mon Day, Year'){
		if(eventDateDescriptionStr == ''){
			eventDateDescriptionStr = 'Until ' + endDateStr;
		}
		else{
			eventDateDescriptionStr = eventDateDescriptionStr + '\nUntil ' + endDateStr;
		}
	}
	else if(eventDateDescriptionStr == ''){
		eventDateDescriptionStr = 'Daily';
	}
	objForm.eventDatesDescription.value = eventDateDescriptionStr;
}

var imageWindow;
var messageWindow;

function popImage(folder, filename, imageWidth, imageHeight, popTitle){
	var tweak = 0;
	content = '<HTML><HEAD><TITLE>';
	content += unescape(popTitle);
	content += '</TITLE></HEAD>';
	if(navigator.appName == 'Netscape'){
		tweak = 2;
		content += '<frameset rows="100%,*" border="0" frameborder="0" framespacing="0">';
		content += '<frame src="http://';
		content += top.location.host;
		content += '/images/';
		content += filename;
		content += '" name="imageFrame" scrolling="no" marginwidth="0" marginheight="0"><frame src="http://' + top.location.host + '/blank.html" name="empty" scrolling="no" marginwidth="0" marginheight="0"></frameset>';
	}
	else{
		content += '<BODY TOPMARGIN="0" LEFTMARGIN="0" BGCOLOR="#FFFFFF">';
		content += '<CENTER><IMG SRC="http://';
		content += top.location.host;
		content += '/images/';
		content += folder;
		content += '/';
		content += filename;
		content += '" WIDTH="' + imageWidth + '" HEIGHT="' + imageHeight + '">';
		content += '</CENTER></BODY>';
	}
	content += '</HTML>';
	imageWindow = window.open('', filename.substring(0, filename.indexOf('.')), 'scrollbars=no,width=' + (imageWidth + tweak) + ',height=' + (imageHeight + tweak));
	drawWindow(imageWindow);
}

function checkImageSize(objOpener, objForm, w, h){
	objForm.imageWidth.value = w;
	objForm.imageHeight.value = h;
	objOpener.messageWindow.close();
}

function checkImageSizeSave(objOpener, objForm, w, h){
	if(w > 300 || h > 600){
		alert('Your image is too big (' + w + ' pixels x ' + h + ' pixels).\nImages must be 300 pixels x 600 pixels or smaller.');
	}
	else{
		objForm.imageWidth.value = w;
		objForm.imageHeight.value = h;
	}
	objOpener.messageWindow.close();
}

function popMessage(uploadValue, objForm){
	if(uploadValue.charAt(uploadValue.length - 1) != '.' && uploadValue.indexOf('.') != -1){
		objForm.uploadExtension.value = uploadValue.substring(uploadValue.lastIndexOf('.') + 1);
		messageWindow = window.open('', 'Message', 'scrollbars=no,width=250,height=150');
		content = '<HTML><HEAD><TITLE>Message</TITLE><BASE HREF="http://' + location.host + '/"><LINK REL="stylesheet" TYPE="text/css" HREF="http://' + location.host + '/style/main.css"></HEAD>';
		content += '<BODY CLASS="lighterText" LEFTMARGIN="0" TOPMARGIN="0" MARGINWIDTH="0" MARGINHEIGHT="0">';
		content += '<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0" WIDTH="100%" HEIGHT="100%">';
		content += '<TR><TD CLASS="lighterText" ALIGN="center">Getting Image Dimensions...</TD></TR>';
		content += '</TABLE><BR><BR><BR><BR>';
		if(uploadValue.charAt(0) == '/'){
			content += '<IMG SRC="file://' + uploadValue + '" NAME="uploadImage" ONLOAD="opener.checkImageSize(opener, opener.document.' + objForm.name + ', this.width, this.height);"><BR>';
			content += '<IMG SRC="file://localhost' + uploadValue.substring(uploadValue.indexOf('/', 1)) + '" NAME="uploadImageOSX" ONLOAD="opener.checkImageSize(opener, opener.document.' + objForm.name + ', this.width, this.height);">';
		}
		else{
			content += '<IMG SRC="' + uploadValue + '" NAME="uploadImage" ONLOAD="opener.checkImageSize(opener, opener.document.' + objForm.name + ', this.width, this.height);">';
		}
		content += '</BODY></HTML>';
		drawWindow(messageWindow);
	}
	else{
		objForm.uploadExtension.value = '';
		objForm.imageWidth.value = '';
		objForm.imageHeight.value = '';
	}
}

