/**************************************************
Execute on page load
***************************************************/
$(document).ready(function() {
//override window alert with jqalert
try {
	window.alert = window.jqalert;
} catch (err) {
	window.alert('Your browser does not support overloading window.alert. ' + err);
}


$("#comment").keypress(function(e) { 
  var max_char = 49;


	  var char_left = max_char-$(this).val().length;;
   	 if($(this).val().length > max_char)  {
     	 return false; 
	 }
  	
	 $("#comm_characters").html(char_left+" characters remaining");   
}); 
	 });


//to validate sign up form on members.php page

function validateForm(){
var firstname=$("#firstname").val();	
var lastname=$("#lastname").val();
var email=$("#email").val();
var city=$("#city").val();
var state=$("#state").val();
var comment=$("#comment").val();
if($("#OptOut").is(":checked")){
	var optout=1;
}else {
	var optout=0;
}

	
	
var val=1;
//now let's run some validation checks to prevent bad data
var alertMsg='';
if(!firstname){
		val=0;
		alertMsg=alertMsg+="<li>First Name</li>";
		$("#firstname").addClass("validate");
	}
if(!lastname){
		val=0;
		alertMsg=alertMsg+="<li>Last Name</li>";
		$("#lastname").addClass("validate");
	}
if(!city){
		val=0;
		alertMsg=alertMsg+="<li>City</li>";
		$("#city").addClass("validate");
}
	var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
     if (document.signup_form.email.value.search(emailRegEx) == -1) {
          val=0;
		alertMsg=alertMsg+="<li>Email Address</li>";
		$("#email").addClass("validate");
	}

	
if(val==0){	jqalert("The following items are missing and must be provided in order to sign up as a member:<ul class='alert_list'"+alertMsg+"</ul>","Missing Items"); }
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 = {
	type:"signup",
	firstname: firstname,
	lastname: lastname,
	city: city, 
	state: state,
	email:email,
	optout:optout,
	comment:comment
	};
	content="action=" + data.type + "&firstname="+data.firstname+"&lastname="+data.lastname+"&city=" + data.city + "&state=" + data.state +"&email="+data.email+"&optout="+data.optout+"&comment="+data.comment;
$.ajax({ //pass the data to the ajax function for saving to the db
			url: "includes/mail_script.php",
			type: "POST",
			data: content,
			dataType: "html",
			success: function(html){
				
				if(html=="prev"){
				$.prompt("This email has already been registered at InTheFight.com.",{prefix:'cleanblue'});
				}
				if(html=="success"){
				$.prompt("Thank you for registering as a member of the InTheFight Fellowship.",{ callback: mycallbackfunc, prefix:'cleanblue' });
				}
				if(html=="fail"){
				$.prompt("There was a problem with your registration. Please try again or contact the webmaster.",{prefix:'cleanblue'});
				}
				
				},
			error: function(request) {
					jqalert("There was a problem with this data." + request+"<br />Please try again.","Program Error",{icon: "/admintools/images/alert_icon_warning.png"});
				//	el_form.children("div").html(data.original_html);
				}
			});
//

}
}
function mycallbackfunc(){
	window.location="members.php";
}




//*************************************************************************

function validateContactForm(){	
var email=$("#email").val();
var subject=$("#subject").val();
var message=$("#message").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){	jqalert("Your message cannot be sent without the following items:<ul class='alert_list'"+alertMsg+"</ul>","Missing Items"); }
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
	};
	content="email=" + data.email+ "&subject="+data.subject+"&message="+data.message;
$.ajax({ //pass the data to the ajax function for saving to the db
			url: "includes/sendmail.php",
			type: "POST",
			data: content,
			dataType: "html",
			success: function(html){
				
				if(html=="success"){
				$.prompt("Your mail has been sent.",{ callback: mycallbackfunc, prefix:'cleanblue' });
				}
				if(html=="fail"){
				$.prompt("There was a problem sending your email. Please try again or contact the webmaster.",{prefix:'cleanblue'});
				}
				parent.tb_remove();
				},
			error: function(request) {
					jqalert("There was a problem with this data." + request+"<br />Please try again.","Program Error",{icon: "/admintools/images/alert_icon_warning.png"});
				//	el_form.children("div").html(data.original_html);
				}
			});
//

}
}
function mycallbackfunc(){
	window.location="contact.php";
}