// JavaScript Document
$(document).ready(function(){
    $("#sjcDialog").dialog({draggable: true, resizable: false, autoOpen: false, modal: false, title: 'no title'});
	//$("#sjcDialog").bind('dialogopen',function(event, ui) {
	//												 darkenPage();
	//												 });
	//$("#sjcDialog").bind('dialogclose',function(event, ui) {
	//												 field.focus();
	//												 });
$('a[title]').qtip({ style: { 'font-size': 14, name: 'dark', border: {width: 2, radius: 8}, tip: true }, position: { target: 'mouse' } })

// Notice the use of the each method to gain access to each element individually
   $('#links_content a, .thumbnail').each(function()
   {
      // Create image content using websnapr thumbnail service
      var content = '<img src="http://images.websnapr.com/?key=8Q3kEkB077rw&url=';
      content += $(this).attr('href');
      content += '" alt="Loading thumbnail..." height="152" width="202" />';
      
      // Setup the tooltip with the content
      $(this).qtip(
      {
         content: content,
         position: {
            corner: {
               tooltip: 'bottomMiddle',
               target: 'topMiddle'
            }
         },
         style: {
            tip: true, // Give it a speech bubble tip with automatic corner detection
            name: 'cream'
         }
      });
   });

$("#submit_btn").click(function() {
							
							$("body,html").css({'cursor' : 'wait'});
							
							var name = $("input#name").val();
							var email = $("input#email").val();
							var subject = $("input#subject").val();
							var message = $("textarea#message").val();
							var destination = $("input#destination").val();
							//var captcha = $("input#capt").val();
														
							if ((name == "") || (email == "") || (subject == "") || (message == "")) {
								
								$('#msg_container').html('<div id="error_msg" class="error"></div>');
								$('#error_msg').html('<h1>You have errors that need to be corrected!</h1>');
								
								if (name == "") {
									$('body,html').css({'cursor' : 'default'});
									$('#name').css({'backgroundColor' : '#FBE3E4','border' : '2px solid #ff1200'});
									$('#error_msg').append('You have not entered your name.<br />');
									$('html,body').animate({scrollTop:250}, 'slow');
								} else {
									$('body,html').css({'cursor' : 'default'});
									$('#name').css({'backgroundColor' : '#E6EFC2','border' : '2px solid #136400'});
								}
								if (email == "") {
									$('body,html').css({'cursor' : 'default'});
									$('#email').css({'backgroundColor' : '#FBE3E4','border' : '2px solid #ff1200'});
									$('#error_msg').append('You have not entered your email address.<br />');
									$('html,body').animate({scrollTop:250}, 'slow');
								} else {
									$('body,html').css({'cursor' : 'default'});
									$('#email').css({'backgroundColor' : '#E6EFC2','border' : '2px solid #136400'});
								}
								
								if (subject == "") {
									$('body,html').css({'cursor' : 'default'});
									$('#subject').css({'backgroundColor' : '#FBE3E4','border' : '2px solid #ff1200'});
									$('#error_msg').append('You have not entered a subject.<br />');
									$('html,body').animate({scrollTop:250}, 'slow');
								} else {
									$('body,html').css({'cursor' : 'default'});
									$('#subject').css({'backgroundColor' : '#E6EFC2','border' : '2px solid #136400'});	
								}
								
								if (message == "") {
									$('body,html').css({'cursor' : 'default'});
									$('#message').css({'backgroundColor' : '#FBE3E4','border' : '2px solid #ff1200'});
									$('#error_msg').append('You have not entered a message.<br />');
									$('html,body').animate({scrollTop:250}, 'slow');
								} else {
									$('body,html').css({'cursor' : 'default'});
									$('#message').css({'backgroundColor' : '#E6EFC2','border' : '2px solid #136400'});
								}
								
								if (echeck(email) == false) {
									$('body,html').css({'cursor' : 'default'});
									$('#error_msg').append('You have entered an incorrectly formatted email address.');
									$('#email').css({'backgroundColor' : '#FBE3E4','border' : '2px solid #ff1200'});
									$('html,body').animate({scrollTop:250}, 'slow');
									
								}
								else {
									$('body,html').css({'cursor' : 'default'});
									$('#email').css({'backgroundColor' : '#E6EFC2','border' : '2px solid #136400'});
								}
								
								return false;
							}
										
							if (echeck(email) == false) {
								$('#msg_container').html('<div id="error_msg" class="error"></div>')
								$('#error_msg').html('<h1>You have errors that need to be corrected!</h1>');
								$('#error_msg').append('You have entered an incorrectly formatted email address.');
								$('#error_msg').css({'backgroundColor' : '#FBE3E4'});
								$('#email').css({'backgroundColor' : '#FBE3E4','border' : '2px solid #ff1200'});
								$('html,body').animate({scrollTop:250}, 'slow');
								return false;
							}
								
							
var dataString = 'name=' + name + '&email='+ email + '&subject=' + subject + '&message=' + message + '&destination=' + destination;
//alert (dataString);return false;
$.ajax({
	   type: "POST",
	   url: "http://www.saintjohnschurch.info/scripts/mailer.php",
	   data: dataString,
	   success: function() {
		   $('body,html').css({'cursor' : 'default'});
		   $('.form').css({'background' : 'none', '-moz-box-shadow' : 'none', '-webkit-box-shadow' : 'none', 'box-shadow' : 'none' });
		   $('.form').html("<div id='success_msg' class='success'></div>");
		   $('#success_msg').html("<h1>Thank you! We have received your message.</h1>")
		   .append("<p>If you requested that we contact you we will do so as soon as possible.</p>")
		   .hide()
		   .fadeIn(1500, function() {
				$('#message').append("<img id='checkmart' src='../images/icon_confirmation.gif' />");
								  });
		   
	   },
	   error: function() {
		   	$("#sjcDialog").dialog('open');
			$('#sjcDialog').data('title.dialog', "Error"); 
			$("#sjcDialog p").text("There was an error in processing your request.  Please try again later.");
	   }
	   });
		return false;
	});
  });

function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   //alert("Invalid E-mail ID")
		   //message("Error","You have entered a wrong email address or the formatting is incorrect.  Please try again.");
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   //alert("Invalid E-mail ID")
		  // message("Error","You have entered a wrong email address or the formatting is incorrect.  Please try again.");
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    //alert("Invalid E-mail ID")
			//message("Error","You have entered a wrong email address or the formatting is incorrect.  Please try again.");
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    //alert("Invalid E-mail ID")
			//message("Error","You have entered a wrong email address or the formatting is incorrect.  Please try again.");
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    //alert("Invalid E-mail ID")
			//message("Error","You have entered a wrong email address or the formatting is incorrect.  Please try again.");
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    //alert("Invalid E-mail ID")
			//message("Error","You have entered a wrong email address or the formatting is incorrect.  Please try again.");
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    //alert("Invalid E-mail ID")
			//message("Error","You have entered a wrong email address or the formatting is incorrect.  Please try again.");
		    return false
		 }

 		 return true					
}

function docPrint()
{
	//$("#content").jqprint();
	window.focus();
	window.print();
}
