function validateEmail(elementValue){
    var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
    return emailPattern.test(elementValue); 
}


$(document).ready(function(){
$('#email').focus(function(){
this.tempValue1 = this.value;
this.value = '';
$('#email').addClass('focused');
});
$('#email').blur(function(){
if (this.value == '') {
this.value = this.tempValue1;
}
$('#email').removeClass('focused');
});
$('#name').focus(function(){
this.tempValue1 = this.value;
this.value = '';
$('#name').addClass('focused');
});
$('#name').blur(function(){
if (this.value == '') {
this.value = this.tempValue1;
}
$('#name').removeClass('focused');
});
});

var recaptcha = {
    register: function() {
        $('.recaptchaForm').submit(recaptcha.validate);
        $('#contactCopy').change(recaptcha.checkbox);
    },

    validate: function() {
        var challenge = $('#recaptcha_challenge_field').val();
        var response  = $('#recaptcha_response_field').val();
        var infoName  = new Array();
        $('.recaptchaInfo').each(function(x){
            infoName[x] = $('#'+this.id).attr('rel');
        });
        var infoValue  = new Array();
        $('.recaptchaInfo').each(function(y){
            infoValue[y] = $('#'+this.id).val();
        });
        var strNames  = infoName.join('+++');
        var strValues = infoValue.join('+++');
        var checked   = $('#contactCopy').val();
	if(validateEmail($('#contactEmail').val())){
		$.post('default/contact/validate-recaptcha',
		    { challenge_field: challenge, response_field: response },
		    function(valid) {
			if(valid) {
			    $.post('default/contact/send', { names: strNames, values: strValues, copy: checked },
				function(send) {
				    if(send) {
				        $(document).attr('location','default/contact/success');
				    }
				}, 'json'
			    );
			} else {
			    $.jGrowl("Los datos de validación no son correctos.", { header: 'Alerta', life: 10000 });
			    Recaptcha.reload ();
			}
		    }, 'json'
		);
	} else {
			    $.jGrowl("La dirección de email, no es válida.", { header: 'Alerta', life: 10000 });
			    Recaptcha.reload ();
        }
        return false;
    },

    checkbox : function () {
        if($('#contactCopy').val()==0) {
            $('#contactCopy').val('1');
        } else {
            $('#contactCopy').val('0');
        }
    }
}
