/**
 * ONE TRUE MAILFORM. COPYRIGHT 2010 @ insight marketing
 */
$(document).ready(function(){
    $('#ot-mailform').submit(function(){
        try {
        
            if (otvalidation()) {
                $.post('mailform/sendmail.php', $('#ot-mailform').serializeArray(), function(AObj){
                    if (AObj.success) {
                    	if (AObj.invalidfields){
							$('#ot-mailform .cont-errormessage').text(AObj.msg).fadeIn(function(){
                                $('#ot-mailform [name='+AObj.invalidfields+']').focus();
                            });
						}else if (AObj.redirect_on_success) {
                            document.location.href = AObj.redirect_page;
                        }
                        else {
                            $('#ot-mailform .cont-errormessage').text(AObj.msg).fadeIn(function(){
                                $('#ot-mailform .cont-errormessage').delay(3000).fadeOut();
                            });
                            $('#ot-mailform input[type=text],#ot-mailform textarea').val('');
                        }
                    }
                    else {
                        alert(AObj.msg);
                    }
                }, 'json');
            }
        } 
        catch (e) {
            return false;
        }
        return false;
        
    });
    
    // simple validation on blur
    $('.noempty').blur(function(){
        if ($(this).closest('.ot-rowitem').hasClass('ot-error') &&
        $(this).val() != '') {
            $(this).closest('.ot-rowitem').removeClass('ot-error').find('.ot-icon').fadeOut();
        }
        
        if ($(this).val() == '') {
            $(this).closest('.ot-rowitem').addClass('ot-error').find('.ot-icon').fadeIn();
        }
    });
    
    // just in case clear textarea (after code formatting the
    // line is broken)
    $('#ot-mailform textarea').val('');
});
// validation on submit
function otvalidation(){
    _r = true;
    $('#ot-mailform .ot-error').removeClass('ot-error');
    
    $('#ot-mailform .noempty').each(function(){
        if (!_r) 
            return;
        
        if ($(this).val() == '') {
            _r = false;
            $(this).closest('.ot-rowitem').addClass('ot-error').find('.ot-icon').fadeIn();
            $('.cont-errormessage').text($(this).attr('errormsg')).fadeIn();
            $(this).focus();
        }
        
    });
    try {
        //alert($('#ot-mailform input[name=email]').val());
        if (_r && !isValidEmailAddress($('#ot-mailform input[name=email]').val())) {
            _r = false;
            $('#ot-mailform input[name=email]').closest('#ot-mailform .ot-rowitem').addClass('ot-error').find('#ot-mailform .ot-icon').fadeIn();
            $('.cont-errormessage').text($('#ot-mailform input[name=email]').attr('errormsg')).fadeIn();
            $('#ot-mailform input[name=email]').focus();
        }
    } 
    catch (e) {
        alert('2');
    }
    
    if (_r) {
        $('#ot-mailform .cont-errormessage').fadeOut();
    }
    return _r;
}

function isValidEmailAddress(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);
}

