function trim(str){
	 return str.replace(/^\s*|\s(?=\s)|\s*$/g, "");
}
function changemainstyle(main){
	switch(main){
		case 'visitors':
			$('body').removeClass('bodymeetings').removeClass('bodygrouptours').addClass('bodyvisitors');
			$('span#welcomemaintitle').text("Visitors");
			break;
		case 'meetings':
			$('body').removeClass('bodyvisitors').removeClass('bodygrouptours').addClass('bodymeetings');
			$('span#welcomemaintitle').text("Meeting Planners");
			break;
		case 'grouptours':
			$('body').removeClass('bodyvisitors').removeClass('bodymeetings').addClass('bodygrouptours');
			$('span#welcomemaintitle').text("Group Tour Planners");
			break;
	}
}

jQuery.fn.exists = function(){return jQuery(this).length>0;}

function hideSubnavs(){
	$('div.subnavgroup').hide();
	$('a#nav_visitors img').attr('src', $('a#nav_visitors img').attr('src').replace("1", "0"));
	$('a#nav_meetings img').attr('src', $('a#nav_meetings img').attr('src').replace("1", "0"));
	$('a#nav_grouptours img').attr('src', $('a#nav_grouptours img').attr('src').replace("1", "0"));
	$('a#nav_directory img').attr('src', $('a#nav_directory img').attr('src').replace("1", "0"));
	$('a#nav_specialoffers img').attr('src', $('a#nav_specialoffers img').attr('src').replace("1", "0"));
	clearTimeout(hidesubsubnavgrouptimer);
	hidesubsubnavgroup();
}

function hidesubsubnavgroup(){
	$('div#subnav div.subnavgroup').css('width', '150px');/**/
	$('div#subnav div#subnav_specialoffers').css('width', '165px');/**/
	$('div#subnav td.subnav_right').hide();
	$('div#subnav td.subnav_right div').hide();
	
	$('div#subnav_visitors td.subnav_left a').css({ fontWeight : 'normal', borderBottom : '1px solid #69c715'});
	$('div#subnav_meetings td.subnav_left a').css({ fontWeight : 'normal', borderBottom : '1px solid #33aef0'});
	$('div#subnav_grouptours td.subnav_left a').css({ fontWeight : 'normal', borderBottom : '1px solid #ff7b00'});
	$('div#subnav_directory td.subnav_left a').css({ fontWeight : 'normal', borderBottom : '1px solid #9b9b9b'});
	$('div#subnav_specialoffers td.subnav_left a').css({ fontWeight : 'normal', borderBottom : '1px solid #a628cb'});
}

var hideSubnavTimer = 0;
var hidesubsubnavgrouptimer = 0;
var navid = "";
var subnavrightpad = 0;

$(document).ready(function(){
	
	/*$('div#nav img').mouseover(function(){
		$(this).attr('src', $(this).attr('src').replace("0", "1"));
		//alert(this.src);
	});
	$('div#nav img').mouseout(function(){
		$(this).attr('src', $(this).attr('src').replace("1", "0"));
	});
	/**/
		
	$('div#nav a').mouseover(function(){
		navid = $(this).attr('id').replace(/nav_/i, "");
		clearTimeout(hideSubnavTimer);
		hideSubnavs();
		if($('div#subnav_'+navid).exists()){
			$('div#subnav_'+navid).show();
		}
		$('a#nav_'+navid+' img').attr('src', $('a#nav_'+navid+' img').attr('src').replace("0", "1"));
	});
	$('div.subnavgroup').mouseover(function(){
		clearTimeout(hideSubnavTimer);
		//clearTimeout(hidesubsubnavgrouptimer);

		navid = $(this).attr('id').replace(/subnav_/i, "");
		$('a#nav_'+navid+' img').attr('src', $('a#nav_'+navid+' img').attr('src').replace("0", "1"));
	});
	
	$('div#nav a').mouseout(function(){
		clearTimeout(hideSubnavTimer);
		hideSubnavTimer = setTimeout("hideSubnavs()", 500);
		clearTimeout(hidesubsubnavgrouptimer);
		hidesubsubnavgrouptimer = setTimeout("hidesubsubnavgroup()", 500);
	});
	$('div.subnavgroup').mouseout(function(){
		clearTimeout(hideSubnavTimer);
		hideSubnavTimer = setTimeout("hideSubnavs()", 500);
		clearTimeout(hidesubsubnavgrouptimer);
		hidesubsubnavgrouptimer = setTimeout("hidesubsubnavgroup()", 500);
	});
	
	$('div#subnav td.subnav_left a').mouseover(function(){		
		clearTimeout(hidesubsubnavgrouptimer);
		hidesubsubnavgroup();
		
		navid = $(this).attr('id');
		$(this).css({ fontWeight : 'bold', borderBottom : '1px solid #d8d8d8'}); 
		if($('div#sub'+navid).exists()){
			/*switch($(this).attr('class')){
				case "vatop": $('div#subnav td.subnav_right').css('vertical-align', 'top'); break;
				case "vamid": $('div#subnav td.subnav_right').css('vertical-align', 'middle'); break;
				case "vabot": $('div#subnav td.subnav_right').css('vertical-align', 'bottom'); break;
			}/**/
			//subnavrightpad = $(this).attr('class').replace("subpad", "");
			//$('div#subnav td.subnav_right').css('padding-top', (subnavrightpad*25)+"px");
			$('div#subnav div.subnavgroup').css('width', '330px');/**/
			$('div#subnav td.subnav_right').show();
			$('div#sub'+navid).show();
		}
	});
	
	$('div#subnav td.subnav_left a').mouseout(function(){
		clearTimeout(hidesubsubnavgrouptimer);
		hidesubsubnavgrouptimer = setTimeout("hidesubsubnavgroup()", 500);
	});
	$('div#subnav td.subnav_right').mouseover(function(){
		clearTimeout(hidesubsubnavgrouptimer);
	});
	$('div#subnav td.subnav_right').mouseout(function(){
		clearTimeout(hidesubsubnavgrouptimer);
		hidesubsubnavgrouptimer = setTimeout("hidesubsubnavgroup()", 500);
	});
	
	$('form#frm_search input').val("search valleyforge.org");

	$('form#frm_search input').focus(function(){
		if(trim($(this).val()) == "search valleyforge.org"){
			$(this).val("");
			$(this).css('fontStyle', 'normal');
		}
	});
	$('form#frm_search input').blur(function(){
		if(trim($(this).val()) == ""){
			$(this).css('fontStyle', 'italic');
			$(this).val("search valleyforge.org");
		}
	});
	var searchstr = "";
	$('form#frm_search').submit(function(){
		if(trim($('form#frm_search input').val()) == "search valleyforge.org" || trim($('form#frm_search input').val()) == ""){
			alert("Please enter a vaild search term!");
			return false;
		} else {
			searchstr = trim($('form#frm_search input').val()).replace(/\s+/g, "+").replace(/\"/g, "").replace(/\'/g, "").replace(/&/g, "and");
			window.open('/search/'+searchstr, '_self');
			//$(this).submit();
			return false;			
		}
	});
	$('form#frm_search img').click(function(){
		if(trim($('form#frm_search input').val()) == "search valleyforge.org" || trim($('form#frm_search input').val()) == ""){
			alert("Please enter a vaild search term!");
			return false;
		} else {
			searchstr = trim($('form#frm_search input').val()).replace(/\s+/g, "+").replace(/\"/g, "").replace(/\'/g, "").replace(/&/g, "and");
			window.open('/search/'+searchstr, '_self');
			//$('form#frm_search').submit();
		}
	});
	
	var reg_email = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	
	$('form#frm_enews input').focus(function(){
		if(trim($(this).val()) == "enter your email here to get the latest news"){
			$(this).val("");
			$(this).css('fontStyle', 'normal');
		}
	});
	$('form#frm_enews input').blur(function(){
		if(trim($(this).val()) == ""){
			$(this).val("enter your email here to get the latest news");
			$(this).css('fontStyle', 'italic');
		}
	});

	$('form#frm_enews').submit(function(){
		if(reg_email.test(trim($('form#frm_enews input').val())) == false){
			alert('You must enter a valid email.\nPlease try again.');
			return false;
		} else {
			$(this).submit();
		}
	});
	$('form#frm_enews img').click(function(){
		if(reg_email.test(trim($('form#frm_enews input').val())) == false){
			alert('You must enter a valid email.\nPlease try again.');
			return false;
		} else {
			document.frm_enews.submit();
		}
	});
});

function validateForm(theform, reqArray){
	var theval="";
	var validform = true;
	for(var i in reqArray){
		theval = eval("theform."+reqArray[i]+".value");
		if(trim(theval) == ""){
			validform = false;
		}
	}
	if(validform){
		theform.submit();
	} else {
		alert("You must enter all the required fields!");
	}
}


