﻿$(function(){

	$('.post_box').each(function(){
	$(this).find('p:last').css({marginBottom:10});
	});
	
	Cufon.replace('h1');
	Cufon.replace('h2');
	Cufon.replace('h3');
	Cufon.replace('h4');
	Cufon.replace('h5');
	Cufon.replace('h6');
	Cufon.replace('#htmlabmail');
	
	$('ul#menuUl').superfish();

	$("a").filter(function(){
		return this.hostname && this.hostname.replace("www.", "") != location.hostname.replace("www.", "");
    }).click(function(){
		window.open($(this).attr("href"));
		return false;
	});

});

/**
 * Contains the functionality of the send email form. Makes the validation and sends the message.
 */
htmlab_contact_form={
	set:function(){
		this.setSendButtonClickHandler();
		this.setInputClickHandler();
	},
	
	setSendButtonClickHandler:function(){
		$("#send_button").click(function(event){
			event.preventDefault();	
			valid=true;  
			$("#name_text_box").removeClass('invalid');
			$("#email_text_box").removeClass('invalid');
			$("#question_text_area").removeClass('invalid');
			$('#invalid_input').hide();
			$('#sent_successful').hide();
			
			//verify whether the name text box is empty
			var name=$("#name_text_box").val();
			if(name=='' || name==null){
				$("#name_text_box").addClass('invalid');
				valid=false;
			}
			
			//verify whether the inserted email address is valid
			var email = $("#email_text_box").val();
			if(!htmlab_contact_form.isValidEmailAddress(email)) {
				$("#email_text_box").addClass('invalid');
				valid=false;
			}
			
			//verify whether the question text area is empty
			var question=$("#question_text_area").val();
			if(question=='' || question==null){
				$("#question_text_area").addClass('invalid');
				valid=false;
			}
			
			if(!valid){
				$('#invalid_input').show();
			}else{
				//the data is valid, sumbit the form
				urlToPhp="contact.php";
				
				var dataString = 'name='+ name + '&comments=' + question + '&email=' + email;  

				$.ajax({  
					type: "POST",  
					url: urlToPhp,  
					data: dataString,  
					success: function(){
						$('#sent_successful').show();
						$("#submit_form").each(function(){
							this.reset();
						});
					}
				}); 
			}
	    });
	},
	
	setInputClickHandler:function(){
		$('.form_input').click(function(){
			$(this).removeClass('invalid');
		});
	},
	
	/**
	 * Checks if an email address is a valid one.
	 * 
	 * @param emailAddress
	 *            the email address to validate
	 * @return true if the address is a valid one
	 */
	isValidEmailAddress:function(emailAddress) {
		var pattern = new RegExp(
				/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
		return pattern.test(emailAddress);
	}
};
