function isValidDate(dateStr) {
var datePat = /^(\d{4})(-)(\d{1,2})\2(\d{1,2})$/;

var matchArray = dateStr.match(datePat); // is the format ok?
if (matchArray == null) {
alert("Date is not in a valid format.")
return false;
}
month = matchArray[3]; // parse date into variables
day = matchArray[4];
year = matchArray[1];
if (month < 1 || month > 12) { // check month range
alert("Month must be between 1 and 12.");
return false;
}
if (day < 1 || day > 31) {
alert("Day must be between 1 and 31.");
return false;
}
if ((month==4 || month==6 || month==9 || month==11) && day==31) {
alert("Month "+month+" doesn't have 31 days!")
return false
}
if (month == 2) { // check for february 29th
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day>29 || (day==29 && !isleap)) {
alert("February " + year + " doesn't have " + day + " days!");
return false;
   }
}
return true;  // date is valid
}

function fixElement(element, message) {
	alert(message);
	element.focus();
}

function checkEmail(str){
	var filter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	var passed = false;
	
	if (filter.test(str)) {
		passed = true;
	}
	
	return passed;
}

function isFormReady(theform) {
	var passed = false;

	switch (theform.name) {
	
	// basic request information form
	case "basic_info":
		if (theform.name_first.value == "") {
			fixElement(theform.name_first, "Please include your first name.");
			break;
		}
		if (theform.name_last.value == "") {
			fixElement(theform.name_last, "Please include your last name.");
			break;
		}
		if (theform.street_addr.value == "") {
			fixElement(theform.street_addr, "Please include your street address.");
			break;
		}
		if (theform.zip.value == "") {
			fixElement(theform.zip, "Please include your zip code.");
			break;
		}
		if ((theform.email.value == "") || (checkEmail(theform.email.value) === false)) {
			fixElement(theform.email, "Please include a valid email address.");
			break;
		}
		else {
			passed = true;
		}
	break;

	// rental book it form
	case "book_it":
	case "request_info":
		if (theform.name_first.value == "") {
			fixElement(theform.name_first, "Please include your first name.");
			break;
		}
		if (theform.name_last.value == "") {
			fixElement(theform.name_last, "Please include your last name.");
			break;
		}
		if (theform.street_addr.value == "") {
			fixElement(theform.street_addr, "Please include your street address.");
			break;
		}
		if (theform.zip.value == "") {
			fixElement(theform.zip, "Please include your zip code.");
			break;
		}
		if (theform.check_in.value == "") {
			fixElement(theform.check_in, "Please include your requested check in date.");
			break;
		}
		if (theform.check_out.value == "") {
			fixElement(theform.check_out, "Please include your requested check out date.");
			break;
		}
		else {
			passed = true;
		}
	break;

	// email a friend form
	case "email_friend":
		if ((theform.email_from.value == "") || (checkEmail(theform.email_from.value) === false)) {
			fixElement(theform.email_from, "Please include a valid email address.");
			break;
		}
		if ((theform.email_to.value == "") || (checkEmail(theform.email_to.value) === false)) {
			fixElement(theform.email_to, "Please enter a valid address for the email to be sent to.");
			break;
		}
		else {
			passed = true;
		}
	break;

	// event admin form
	case "admin_events":
		if (theform.event_title.value == "") {
			fixElement(theform.event_title, "You must provide a title for this event.");
			break;
		}
		if (!isValidDate(theform.event_date_start.value)) {
			break;
		}
		if (theform.event_desc.value == "") {
			fixElement(theform.event_desc, "You must provide a description for this event.");
			break;
		}
		else {
			passed = true;
		}
	break;

	// news admin form
	case "admin_news":
		if (theform.news_title.value == "") {
			fixElement(theform.news_title, "You must provide a title for this news.");
			break;
		}
		if (theform.news_body.value == "") {
			fixElement(theform.news_body, "You must provide some news for this news.");
			break;
		}
		else {
			passed = true;
		}
	break;

	}
	return passed;
}


// Image Preloading

if (document.images) {
        properties_off = new Image
        properties_on = new Image
        buying_blank = new Image
        buying_off = new Image
        buying_on = new Image
        renting_blank = new Image
        renting_off = new Image
        renting_on = new Image
        selling_blank = new Image
        selling_off = new Image
        selling_on = new Image
        agency_off = new Image
        agency_on = new Image
        agents_blank = new Image
        agents_off = new Image
        agents_on = new Image
        story_blank = new Image
        story_off = new Image
        story_on = new Image
        community_off = new Image
        community_on = new Image
        events_blank = new Image
        events_off = new Image
        events_on = new Image
        resources_blank = new Image
        resources_off = new Image
        resources_on = new Image
        emergency_blank = new Image
        emergency_off = new Image
        emergency_on = new Image
        contact_off = new Image
        contact_on = new Image
        mininav_off = new Image
        mininav_on = new Image

        properties_off.src = "/menu_images/properties_off.gif";
        properties_on.src = "/menu_images/properties_on.gif";
        buying_blank.src = "/menu_images/buying.gif";
        buying_off.src = "/menu_images/buying_off.gif";
        buying_on.src = "/menu_images/buying_on.gif";
        renting_blank.src = "/menu_images/renting.gif";
        renting_off.src = "/menu_images/renting_off.gif";
        renting_on.src = "/menu_images/renting_on.gif";
        selling_blank.src = "/menu_images/selling.gif";
        selling_off.src = "/menu_images/selling_off.gif";
        selling_on.src = "/menu_images/selling_on.gif";
        agency_off.src = "/menu_images/agency_off.gif";
        agency_on.src = "/menu_images/agency_on.gif";
        agents_blank.src = "/menu_images/agents.gif";
        agents_off.src = "/menu_images/agents_off.gif";
        agents_on.src = "/menu_images/agents_on.gif";
        story_blank.src = "/menu_images/story.gif";
        story_off.src = "/menu_images/story_off.gif";
        story_on.src = "/menu_images/story_on.gif";
        community_off.src = "/menu_images/community_off.gif";
        community_on.src = "/menu_images/community_on.gif";
        events_blank.src = "/menu_images/events.gif";
        events_off.src = "/menu_images/events_off.gif";
        events_on.src = "/menu_images/events_on.gif";
        resources_blank.src = "/menu_images/resources.gif";
        resources_off.src = "/menu_images/resources_off.gif";
        resources_on.src = "/menu_images/resources_on.gif";
        emergency_blank.src = "/menu_images/emergency.gif";
        emergency_off.src = "/menu_images/emergency_off.gif";
        emergency_on.src = "/menu_images/emergency_on.gif";
        contact_off.src = "/menu_images/contact_off.gif";
        contact_on.src = "/menu_images/contact_on.gif";
        mininav_off.src = "/images_2/menu/mininav_bottom_off.gif";
        mininav_on.src = "/images_2/menu/mininav_bottom_on.gif";
}

function newWindow(page,width,height) {
    var pageName = Math.round(Math.random()*100);
    previewWindow = window.open(page,pageName,'width='+width+',height='+height+',scrollbars=yes,resizable=yes,location=no,directories=no,menubar=no,status=no,toolbar=no,titlebar=no,left = 10,top = 10');
}
