// JavaScript Document for index.php page
/**************************************************
Execute on page load
***************************************************/
$(document).ready(function() {
//bind mouseover actions to main menu navbar

$(".nav_item_link").mouseover(function(e){
	var el=	$(this).attr("id");
	if(!($("#"+el).hasClass("this_menu"))){
	$(".nav_item_link").removeClass("nav_item_selected");
	$(".nav_item_link:not(.this_menu)").addClass("this_bg");

	$("#"+el).addClass("nav_item_selected");
		$("#"+el).removeClass("this_bg");
		 }
 }).mouseout(function(){ 
 		var el=	$(this).attr("id");
		$("#"+el).removeClass("nav_item_selected");
		$("#"+el).addClass("this_bg");
		});

}); //end doc ready
//sets the current pages menu nav bar to selected
/* deprecated
function showCurMenu(tab){
	$("#nav_menu li a").removeClass("this_menu");
	$("#nav_item_"+tab).addClass("this_menu");
	$("#nav_item_"+tab).removeClass("this_bg");
	
}
*/

//used to toggle questions divs on questions.php pages
function toggleFAQs(){
	//$("#printer-text").html("<a id='printer_link' href='pages/print_questions.php' target='_blank' onclick='tb_remove();'>Printer-friendly page</a>");
	$(".expander").html("+");
	$("div.answer").css("display","none");
	$("h4.question").click(function(e){
		$("*").removeClass("selected_question");							
		$(this).addClass("selected_question");						
		var this_div = $(this).attr("id");
		$("div.answer:not(#answer_"+this_div+")").hide(400);

		$("#answer_"+this_div).toggle(400,function(){
			if($("#answer_"+this_div).attr("style")=="display: none;"){
				$("#expand_"+this_div).html("+");
				$("*").removeClass("selected_question");							

			}else{
				$(".expander").html("+");
				$("#expand_"+this_div).html("-");
						}
			}) ;
		
									});
}

//sends email from thickbox mailer contact form

function validateContactForm(){	
var email=$("#Email").val();
var subject=$("#subject").val();
var message=$("#message").val();
var to=$("#to").val()
	
	
var val=1;
//now let's run some validation checks to prevent bad data
var alertMsg='';
if(!subject){
		val=0;
		alertMsg=alertMsg+="<li>Subject</li>";
		$("#sub_label").addClass("validate");
	}
if(!message){
		val=0;
		alertMsg=alertMsg+="<li>Message</li>";
		$("#msg_label").addClass("validate");
	}
	var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
     if (document.mailer.Email.value.search(emailRegEx) == -1) {
          val=0;
		alertMsg=alertMsg+="<li>Email Address</li>";
		$("#email_label").addClass("validate");
	}
	
if(val==0){$.prompt("Your message cannot be sent without the following items:<ul class='alert_list'>"+alertMsg+"</ul>",{prefix:'impromptu' }); }



if(!val==0){//validation checks all are fine - proceed



//now this needs to be modified to pass form info to ajax page for db insertion	
var data = {
	email: email,
	subject: subject, 
	message: message,
	to:to
	};
	content="e="+data.email+"&t="+data.to+"&s="+data.subject+"&m="+data.message;
$.ajax({ //pass the data to the ajax function for saving to the db
			url: "http://www.heritage-umc.org/includes/sendmail.php",
			type: "POST",
			data: content,
			dataType: "html",
			success: function(html){
				
				if(html=="success"){
				$.prompt("Your mail has been sent.",{ callback: mycallbackfunc, prefix:'impromptu' });
				}
				if(html=="fail"){
				$.prompt("There was a problem sending your email. Please try again or contact the webmaster.",{prefix:'impromptu'});
				}
				parent.tb_remove();
				},
			error: function(request) {
				$.prompt("There was a problem with this data." + request+"<br />Please try again.",{prefix:'impromptu'});
				//	el_form.children("div").html(data.original_html);
				}
			});
//

}
}
function mycallbackfunc(){
	parent.tb_remove();
}
