$(function() {
	// load the modal window
	$('a[name=modalFormulario]').click(function(){
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();
		var winH = $(window).height();
		var winW = $(window).width();
		
		$('#div_id_contacMoreInfo').css('top',  winH/2-$('#div_id_contacMoreInfo').height()/2);
		$('#div_id_contacMoreInfo').css('left', winW/2-$('#div_id_contacMoreInfo').width()/2);
		
		$('.success, .error').hide();
		$('form#contactFormMoreInfo').show();
		
		// Reset all the default values in the form fields
		$('#name').val('Your name');
		$('#surname').val('Your surname');
		/*$('#street').val('Your Street Name');*/
		/*$('#city').val('Your City Name');*/
		/*$('#state').val('Your State Name');*/
		/*$('#postCode').val('Your Post Code');*/
		$('#country').val('Your Country Name');
		$('#phone').val('Your Phone Number');
		/*$('#mobile').val('Your Mobile Number');
		$('#fax').val('Your Fax Number');*/
		$('#email').val('Your email address');
		$('#comment').val('Enter your comment or query...');

		//show the mask and contact divs
		$('#maskFormMoreInfo').css({'width':maskWidth,'height':maskHeight});
		
		
		$('#maskFormMoreInfo').show().fadeTo('', 0.7);
		$('div#div_id_contacMoreInfo').fadeIn();

		// stop the modalFormulario link from doing its default action
		return false;
	});

	// div_id_contacMoreInfoClose the modalFormulario window is div_id_contacMoreInfoClose div or mask div are clicked.
	$('div#div_id_contacMoreInfoClose, div#maskFormMoreInfo').click(function() {
		$('div#div_id_contacMoreInfo, div#maskFormMoreInfo').stop().fadeOut('slow');
	});

	$('#contactFormMoreInfo input').focus(function() {
		$(this).val('');
	});
	
	$('#contactFormMoreInfo .txtAreaMoreInfoCoursesComment').focus(function() {
        $(this).val('');
    });

	// when the Submit button is clicked...
	$('input#submit').click(function() {
	$('.error').hide().remove();
		//Inputed Strings
		var name = $('#name').val(),
			surname = $('#surname').val(),
			email = $('#email').val(),
			phone = $('#phone').val(),
			comment = $('#comment').val();
			/*country = $('#country').val();*/
			coursesSelected = $('#coursesSelected').val();
	        country = $('#country :selected').text();
		//Error Count
		var error_count=0; 
		/*[a-zA-Z\s]{10,}*/
		//Regex Strings
		var name_regex = /[a-zA-Z\s]{1,30}/,
			surname_regex = /[a-zA-Z\s]{1,30}/,
			phone_regex = /^[a-z0-9_-]{3,16}$/,
			email_regex = /^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/;
		
			//Test name
			if(!name_regex.test(name)) {
				$('#contactFormMoreInfo #name').css("background", "#F66");
				error_count += 1;
			}
			else{
				$('#contactFormMoreInfo #name').css("background", "white");
			}
			//Test surname
			if(!surname_regex.test(surname)) {
				$('#contactFormMoreInfo #surname').css("background", "#F66");
				error_count += 1;
			}
			else{
				$('#contactFormMoreInfo #surname').css("background", "white");
			}
			//Test Email
			if(!email_regex.test(email)) {
				$('#contactFormMoreInfo #email').css("background", "#F66");
				error_count += 1;
			}
			else{
				$('#contactFormMoreInfo #email').css("background", "white");
			}
			//Test phone
			if(!phone_regex.test(phone)) {
				$('#contactFormMoreInfo #phone').css("background", "#F66");
				error_count += 1;
			}
			else{
				$('#contactFormMoreInfo #phone').css("background", "white");
			}
			//Blank Comment?
			if(comment == '  ') {
				$('#contactFormMoreInfo #comment').css("background", "#F66");
				error_count += 1;
			}
			else{
				$('#contactFormMoreInfo #comment').css("background", "white");
			}
			//error message
			if(error_count == 1) {
				$('#div_id_contacMoreInfo_Messages').append('<p class=error>Invalid field entered!</p>');
			}
			if(error_count > 1) {
				$('#div_id_contacMoreInfo_Messages').append('<p class=error>Invalid fields entered!</p>');
			}
			
			//No Errors?
			if(error_count === 0) {
				$.ajax({
					type: "post",
					url: "../../PHP/email/send.php",
					data: "&name=" + name + "&surname=" + surname + "&email=" + email + "&phone=" + phone + "&comment=" + comment + "&country=" + country + "&coursesSelected=" + coursesSelected,
					error: function() {
						$('.error').hide();
						$('#sendError').slideDown('slow');
					},
					success: function () {
						$('.error').hide();
						
						$('#div_id_contacMoreInfo_Success').css("visibility", "visible");
						
						$('.success').fadeIn('slow');
						$('form#contactFormMoreInfo').fadeOut('slow');
						
						$('#maskFormMoreInfo').fadeOut('slow');
						$('#div_id_contacMoreInfo_Success').fadeOut(10000);
					}				
				});	
			}
			
			else {
                $('.error').show();
            }
			
		return false;
	});
	
});
