var dtCh= "/";
var minYear=2009;
var maxYear=2015;

function package_submit(thisform) {
	myOption = -1;
	for (i=thisform.package.length-1; i > -1; i--) {
		if (thisform.package[i].checked) {
			return true;
		}
	}

	alert("You must select a wedding package before proceeding.");
	return false;
}

// check for email address: look for [@] and [.]
function isEmail(elm) {
	if (elm.value.indexOf("@") != "-1" &&
		elm.value.indexOf(".") != "-1" &&
		elm.value != "")
		return true;
	else
		return false;
}

// Check for null and for empty
function isFilled(elm) {
	if (elm.value == "" || elm.value == null)
		return false;
	else
		return true;
}

function isValidEmail(addr) {
	var re = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	return re.test(addr);
}

function validPhoneNumber(s) {
	// Check for correct phone number
	var re = /^\d\d\d\-\d\d\d-\d\d\d\d$/;
	return re.test(s);
}

function validateForm(form) {
	//Bride info
	if(isFilled(form.bride_first_name) == false) {
		alert("Please enter the bride's first name.");
		form.bride_first_name.focus();
		return false;
	}

	if(isFilled(form.bride_last_name) == false) {
		alert("Please enter the bride's last name.");
		form.bride_last_name.focus();
		return false;
	}

	if(isFilled(form.bride_phone) == false || form.bride_phone.value == '727-555-1234') {
		alert("Please enter the bride's phone number.");
		form.bride_phone.focus();
		return false;
	}
	if(!validPhoneNumber(form.bride_phone.value)) {
		alert("Please enter the phone number formatted like this:\n\n 727-555-1234.");
		form.bride_phone.focus();
		return false;
	}

	if(isFilled(form.bride_email) == false) {
		alert("Please enter the bride's email address.");
		form.bride_email.focus();
		return false;
	}
	if(!isValidEmail(form.bride_email.value)) {
		alert("Please enter a valid email address.");
		form.bride_email.focus();
		return false;
	}

	//Groom info
	if(isFilled(form.groom_first_name) == false) {
		alert("Please enter the groom's first name.");
		form.groom_first_name.focus();
		return false;
	}

	if(isFilled(form.groom_last_name) == false) {
		alert("Please enter the groom's last name.");
		form.groom_last_name.focus();
		return false;
	}

	if(isFilled(form.groom_phone) == false || form.groom_phone.value == '727-555-1234') {
		alert("Please enter the groom's phone number.");
		form.groom_phone.focus();
		return false;
	}
	if(!validPhoneNumber(form.groom_phone.value)) {
		alert("Please enter the phone number formatted like this:\n\n 727-555-1234.");
		form.groom_phone.focus();
		return false;
	}

	if(isFilled(form.groom_email) == false) {
		alert("Please enter the groom's email address.");
		form.groom_email.focus();
		return false;
	}
	if(!isValidEmail(form.groom_email.value)) {
		alert("Please enter a valid email address.");
		form.groom_email.focus();
		return false;
	}

	if (isDate(form.wedding_date.value)==false){
		form.wedding_date.focus();
		return false;
	}

	if(isFilled(form.wedding_city) == false) {
		alert("Please enter the city where the wedding will occur.");
		form.wedding_city.focus();
		return false;
	}

	if(form.wedding_state.selectedIndex==0) {
		alert("Please select the state where the wedding will occur.");
		form.wedding_state.focus();
		return false;
	}

	if(isFilled(form.announcement_text) == false) {
		alert("Please enter the announcement text.");
		form.announcement_text.focus();
		return false;
	}

	if(isFilled(form.wedding_venue) == false) {
		alert("Please enter the venue where the wedding will occur.");
		form.wedding_venue.focus();
		return false;
	}

	var weddingDate=new Date(form.wedding_date.value);
	var today=new Date();

	//Preferred pub date
	if (isDate(form.pub_date.value)==false) {
		form.pub_date.focus();
		return false;
	}
	var pubDate=new Date(form.pub_date.value);
	if (pubDate.getDay() != 1) {
		alert("Please select a Monday for the preferred publish date.");
		form.pub_date.focus();
		return false;
	}

	if (today >= pubDate) {
		alert("The preferred publish date must be after today.");
		form.pub_date.focus();
		return false;
	}

	//Alternate pub date
	if (isDate(form.pub_date_alternate.value)==false) {
		form.pub_date_alternate.focus();
		return false;
	}
	var pubDateAlternate=new Date(form.pub_date_alternate.value);
	if (pubDateAlternate.getDay() != 1) {
		alert("Please select a Monday for the alternate publish date.");
		form.pub_date_alternate.focus();
		return false;
	}

	if (pubDate >= pubDateAlternate) {
		alert("The preferred publish date must be before the alternate publish date.");
		form.pub_date.focus();
		return false;
	}

	/*if (pubDate > weddingDate) {
		alert("The preferred publish date cannot be after the wedding date.");
		form.pub_date.focus();
		return false;
	}*/
	
	if (!isThisMondayValid(pubDate)) {
		alert("The coming Monday is too soon.  Please select the next Monday for the preferred publication date.");
		form.pub_date.focus();
		return false;
	}

	if(isFilled(form.contact_first_name) == false) {
		alert("Please enter the contact's first name.");
		form.contact_first_name.focus();
		return false;
	}

	if(isFilled(form.contact_last_name) == false) {
		alert("Please enter the contact's last name.");
		form.contact_last_name.focus();
		return false;
	}

	if(isFilled(form.contact_phone_day) == false || form.contact_phone_day.value == '727-555-1234') {
		alert("Please enter the contacts's phone number.");
		form.contact_phone_day.focus();
		return false;
	}
	if(!validPhoneNumber(form.contact_phone_day.value)) {
		alert("Please enter the phone number formatted like this:\n\n 727-555-1234.");
		form.contact_phone_day.focus();
		return false;
	}

	if(isFilled(form.contact_phone_evening) == false || form.contact_phone_evening.value == '727-555-1234') {
		alert("Please enter the contact's evening phone number.");
		form.contact_phone_evening.focus();
		return false;
	}
	if(!validPhoneNumber(form.contact_phone_evening.value)) {
		alert("Please enter the phone number formatted like this:\n\n 727-555-1234.");
		form.contact_phone_evening.focus();
		return false;
	}

	if(isFilled(form.contact_email) == false) {
		alert("Please enter the contact's email address.");
		form.contact_email.focus();
		return false;
	}
	if(!isValidEmail(form.contact_email.value)) {
		alert("Please enter a valid email address.");
		form.contact_email.focus();
		return false;
	}

	if(isFilled(form.contact_street) == false) {
		alert("Please enter the contact's street address.");
		form.contact_street.focus();
		return false;
	}

	if(isFilled(form.contact_city) == false) {
		alert("Please enter the contact's city.");
		form.groom_last_name.focus();
		return false;
	}

	if(form.contact_state.selectedIndex==0) {
		alert("Please select the contact's state.");
		form.contact_state.focus();
		return false;
	}

	if(isFilled(form.contact_zip) == false) {
		alert("Please enter the contact's zip.");
		form.contact_zip.focus();
		return false;
	}

	return true;
}

function isInteger(s) {
	var i;
	for (i = 0; i < s.length; i++) {   
		// Check that current character is number.
		var c = s.charAt(i);
		if (((c < "0") || (c > "9")))
			return false;
	}
	// All characters are numbers.
	return true;
}

function stripCharsInBag(s, bag) {
	var i;
	var returnString = "";
	// Search through string's characters one by one.
	// If character is not in bag, append to returnString.
	for (i = 0; i < s.length; i++) {   
		var c = s.charAt(i);
		if (bag.indexOf(c) == -1) returnString += c;
	}
	return returnString;
}

function daysInFebruary (year) {
	// February has 29 days in any year evenly divisible by four,
	// EXCEPT for centurial years which are not also divisible by 400.
	return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
	} 
	return this
}

function isDate(dtStr) {
	var daysInMonth = DaysArray(12);
	var pos1=dtStr.indexOf(dtCh);
	var pos2=dtStr.indexOf(dtCh,pos1+1);
	var strMonth=dtStr.substring(0,pos1);
	var strDay=dtStr.substring(pos1+1,pos2);
	var strYear=dtStr.substring(pos2+1);
	strYr=strYear;
	if (strDay.charAt(0)=="0" && strDay.length>1)
		strDay=strDay.substring(1);
	if (strMonth.charAt(0)=="0" && strMonth.length>1)
		strMonth=strMonth.substring(1);
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1)
			strYr=strYr.substring(1);
	}
	month=parseInt(strMonth);
	day=parseInt(strDay);
	year=parseInt(strYr);
	if (pos1==-1 || pos2==-1) {
		alert("The date format should be : mm/dd/yyyy");
		return false;
	}
	if (strMonth.length<1 || month<1 || month>12) {
		alert("Please enter a valid month");
		return false;
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]) {
		alert("Please enter a valid day");
		return false;
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear) {
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear);
		return false;
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false) {
		alert("Please enter a valid date");
		return false;
	}

	return true;
}

function isDateMonday(dateStr) {
	var theDate=new Date(dateStr);
	if (theDate.getDay() != 1)
		return false;

	return true;
}

// Make sure that the date given is not too soon.  It can't be the
//  next Monday after noon on Tuesday
function isThisMondayValid(dateValue) {

	// Get the date in seconds
	var dateInSecs = dateValue.getTime() / 1000;
		

	// Afternoon on Tuesday can't be the next Monday, subtract the seconds
	//  between Tuesday afternoon and Monday to see if the time is far enough out
	var dueDate = dateInSecs - 561600;
		
	// Get the system time in seconds
	var currentTimeInSecs = (new Date()).getTime() / 1000;

		
	return currentTimeInSecs < dueDate;
}
