$(function() {
    function randomFromTo(from, to){
       return Math.floor(Math.random() * (to - from + 1) + from);
    }


    var starting_name =  'τo όνομά σας';
    var starting_email =  'το email σας';
    var starting_comment =  'το μήνυμά σας...';
	
	$('#mask').fadeTo( 'fast', 0.7 );
	//
	// load the modal window
	$('a.modal').click(function(){
			
    		// scroll to top
    		$('html, body').animate({scrollTop:0}, 'fast');
			
    		// before showing the modal window, reset the form incase of previous use.
    		$('.success, .error').hide();
			$("#reason option:first").attr('selected','selected');    		

    		// Reset all the default values in the form fields
            praks_a = randomFromTo(1, 14);
            praks_b = randomFromTo(1, 9);
            correct_answer = praks_a + praks_b;
            $('#do_the_maths').html(praks_a +'+' + praks_b);

    		$('#name').val(starting_name);
    		$('#email').val(starting_email);
    		$('#comment').val(starting_comment);
            $('#arithmetic').val('');
    		//show the mask and contact divs
    		$('#contact_container').fadeIn();

    		// stop the modal link from doing its default action
    		return false;
	});
	
	$('#reason').change(function(){
		var reason = $('#reason option:selected').val();
		
		if(reason == 2){
			$("#contactForm p:not(.send_your_cv_selector)").hide(function(){$(".send_your_cv").show();});			
		}else{	
			$("#contactForm p:not(.send_your_cv)").show(function(){$(".send_your_cv").hide();});			
		}
		
	});
	

	// close the modal window is close div or mask div are clicked.
	$('div#close, div#mask').click(function() {
		$('#contact_container').stop().fadeOut('slow',function(){		
				$('#contactForm').show();				
				$("#contactForm p:not(.send_your_cv)").show(function(){$(".send_your_cv").hide();});		
		});
	});

	$('#name').focus(function() {
        if($.trim($(this).val()) == starting_name){
            $(this).val('');
        }
	});

    $('#name').blur(function() {
        if($.trim($(this).val()) == ''){
            $(this).val(starting_name);
        }
	});



    $('#email').focus(function() {
		if($.trim($(this).val()) == starting_email){
            $(this).val('');
        }
	});
    $('#email').blur(function() {
        if($.trim($(this).val()) == ''){
            $(this).val(starting_email);
        }
	});

    $('#comment').focus(function() {
		if($.trim($(this).val()) == starting_comment){
            $(this).val('');
        }
	});
    $('#comment').blur(function() {
        if($.trim($(this).val()) == ''){
            $(this).val(starting_comment);
        }
	});
	

	// when the Submit button is clicked...
	$('input#submit').click(function() {
	    $('.error').hide().remove();
		//Inputed Strings
		var username = $.trim($('#name').val()),
			email = $.trim($('#email').val()),
			comment = $.trim($('#comment').val()),
			arithmetic = $.trim($('#arithmetic').val()),
			reason = $('#reason option:selected').val()
			;
		
	
		//Error Count
		var error_count = 0;
		
		//Regex Strings
		var username_regex = /^[a-z0-9_-]{3,16}$/,
			email_regex = /^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/;
		
            //arithmetic
            if (arithmetic != correct_answer){
                $('#arithmetic').parent().after('<p class="error hovered">Γράψτε το σωστό αποτέλεσμα της πράξης '+$('#do_the_maths').html()+'</p>');
                error_count += 1;
            }

            //Blank Comment?
			if(comment == '' || comment == starting_comment) {
				$('#comment').parent().after('<p class="error hovered">Συμπληρώστε το μήνυμα προς αποστολή</p>');
				error_count += 1;
			}

            //Test Email
			if(!email_regex.test(email)) {
				$('#email').parent().after('<p class="error hovered">Συμπληρώστε το email σας</p>');
				error_count += 1;
			}

            //Test Username
			if(username == starting_name) {
				$('#name').parent().after('<p class="error hovered">Συμπληρώστε το όνομά σας</p>');
				error_count += 1;
			}
			//alert("name=" + username + "&email=" + email + "&comment=" + comment + "&reason=" + reason);
			//No Errors?
			if(error_count === 0) {
                $('form#contactForm').slideUp('fast',function (){$('#loading').fadeIn('fast');});
				$.ajax({
					type: "post",
					url: "send.php",
                    timeout:11000,
					data: "name=" + username + "&email=" + email + "&comment=" + comment + "&reason=" + reason,
					error: function(error) {						
						$('.error').hide();
						$('#sendError').slideDown('slow');
						alert('Λυπούμαστε αλλά η αποστολή δεν έγινε με επιτυχία. Ξαναδοκιμάστε ή επικοινωνήστε απευθείας με το info@princeoliver.com');
                        $('form#contactForm').slideDown('slow',function (){$('#loading').fadeOut();});
					},
					success: function (html) {
						$('.error').hide();
						$('.success').fadeIn('fast',function (){$('#loading').hide();});
					}
				});
			}
	
			else {
                $('.error').show();
            }
			
		return false;
	});
	
});
