// JavaScript Document
function toggle_display(id) {
	var disp = document.getElementById(id).style.display;
	if (disp == 'none') {
		disp = 'block';	
	} else {
		disp = 'none';
	}
}
function validate_required(field)
{
	with (field)
	{
		if (value==null||value=="")
		{
			return false;
		} else {
			field.focus();
			return true;
		}
	}
}
function validate_expression(field, expr)
{
	var ex = '';
	switch (expr) {
		case 'alpha':
			ex = /^[a-zA-Z ]+$/;
			break;
		case 'number':
			ex  = /^[0-9]+$/;
			break;
		case 'alphanum':		
			ex = /^[0-9a-zA-Z ]+$/;
			break;
		case 'email':
			ex = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-Z0-9]{2,4}$/;	
			break;
		default: // alpha
			ex = /^[a-zA-Z ]+$/;
			break;
	}

	var val = field.value.replace(/ /,'');
	if (val.match(ex)) {
		return true;	
	} else {
		field.focus();
		return false;
	}
}

function compare_values(field_a, field_b) {
	if (field_a.value == field_b.value) {
		return true;
	} else {
		field_a.focus();
		return false;
	}
}

function confirm_delete(obj_name, id, page) {
	if (confirm("Are you sure you want to delete " + obj_name + "?")) {
		window.location.href = page + "?id=" + id + "&action=remove";
	}
}

function checkSiteSearch(searchForm) {
	if(validate_required(searchForm.search))
		return true;
	else
		return false;
}
function checkContact(cForm) {
	var msg = '';
	var retval = true;
	if(!validate_required(cForm.zip)) {
		msg = 'Your Zip Code\n' + msg;
		cForm.zip.focus();
	}
	if(!validate_required(cForm.phone)) {
		msg = 'Your Phone Number\n' + msg;
		cForm.phone.focus();
	}
	if(!validate_required(cForm.email) || !validate_expression(cForm.email, 'email')) {
		msg = 'Your Email Address\n' + msg;
		cForm.email.focus();
	}
	if(!validate_required(cForm.contactName)) {
		msg = 'Your Name\n' + msg;
		cForm.contactName.focus();
	}
	if(msg.length > 0) {
		alert('Please complete or correct the following:\n' + msg);
		retval = false;
	}
	return retval;
}

/* BEGIN DROP-DOWN MENU */
function loadNav(list) {
	var ids = new Array(list);
	for(var i = 0; i < ids.length; i++)
	{
		var sfEls = document.getElementById(ids[i]).getElementsByTagName("LI");
		for (var i = 0; i < sfEls.length; i++) {
			sfEls[i].onmouseover = function() {
				if(!this.oldClassName)
					this.oldClassName = this.className.replace(new RegExp("\\bsfhover\\b"), "");
				this.className += " sfhover";
			}
			sfEls[i].onmouseout = function() {
				this.className = this.oldClassName;
			}

			var sfEls2 = sfEls[i].getElementsByTagName("A");
			for (var j = 0; j < sfEls2.length; j++)
			{
				sfEls2[j].onclick = function() {
					if(this.href.substr(this.href.length-1) == "#")
						return false;
					else
						this.parentNode.parentNode.parentNode.className = this.parentNode.parentNode.parentNode.oldClassName;
					if(this.className == "subnewwindow")
					{
						window.open(this.href);
						return false;
					}
				}
			}
		}
	}
}
/* END DROP-DOWN MENU */

/* BEGIN PRELOAD IMAGES */

/* END PRELOAD IMAGES */

/* BEGIN GALLERY FUNCTIONS */
function showPhoto(img, altText) {
	var tmp = document.createElement("img");
	tmp.id = 'galleryPhoto';
	tmp.src = 'assets/photos/std/' + img;
	tmp.alt = altText;
	document.getElementById('photoWindow').appendChild(tmp);
	document.getElementById('photo-wrapper').className = 'photoVis';
	document.getElementById('photo-container').className = 'containerVis';
}
function hidePhoto() {
	oldImg = document.getElementById('galleryPhoto');
	document.getElementById('photoWindow').removeChild(oldImg);
	document.getElementById('photo-wrapper').className = 'photoNoVis';
	document.getElementById('photo-container').className = 'containerNoVis';
}
/* END GALLERY FUNCTIONS */

/* BEGIN HERO IMAGEMAP FUNCTIONS */
function showDiv(elem) {
	document.getElementById(elem).className = 'pestFlyout-on';
}
function hideDiv(elem) {	
	document.getElementById(elem).className = 'pestFlyout';
}
/* END HERO IMAGEMAP FUNCTIONS */

window.onload = function () {
	loadNav('nav');
	if(socmed)
		loadNav('greenbox-nav-list');
}