/**
 * ONE TRUE MAILFORM. COPYRIGHT 2010 @ insight marketing
 */

jQuery(document).ready(function($){
	
    $('.ot-mailform').submit(function(){
    	_form = this;
        try {
        
            if (mfvalidation($,_form)) {
            	$(_form).find('[type=submit]').attr('disabled','disabled');
                $.post('modules/mod_mailform/mailform/sendmail.php', $(_form).serializeArray(), function(AObj){
                    if (AObj.success) {
                    	if (AObj.invalidfields){
							$('.ot-mailform .cont-errormessage').text(AObj.msg).fadeIn(function(){
								$(_form).find('[type=submit]').removeAttr('disabled');
                                $(_form).find('[name='+AObj.invalidfields+']').focus();
                            });
						}else if (AObj.redirect_on_success) {
							document.location.href = AObj.redirect_page+'?ref='+$('input[name=brochureid]').val();
                        }
                        else {
                            $(_form).find$('.cont-errormessage').text(AObj.msg).fadeIn(function(){
                                $(_form).find('.cont-errormessage').delay(3000).fadeOut();
                            });
                            $(_form).find(' input[type=text],textarea').val('');
                            $(_form).find('[type=submit]').removeAttr('disabled');
                        }
                    }
                    else {
                        alert(AObj.msg);
                        $(_form).find('[type=submit]').removeAttr('disabled');
                    }
                }, '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 mfvalidation($,_form){
	
    _r = true;
    $(_form).find('.ot-error').removeClass('ot-error');
    $(_form).find('.noempty').each(function(){
        if (!_r) 
            return;
        
        if ($(this).val() == '') {
            _r = false;
            $(_form).find('.cont-errormessage').text($(this).attr('errormsg')).fadeIn();
            $(this).focus();
        }
        
    });
    try {
        //alert($('.ot-mailform input[name=email]').val());
        if (_r && !isValidEmailAddress($(_form).find('input[name=email]').val())) {
            _r = false;
            $(_form).find('input[name=email]').closest('.ot-mailform .ot-rowitem').addClass('ot-error').find('.ot-mailform .ot-icon').fadeIn();
            $(_form).find('.cont-errormessage').text($(_form).find('input[name=email]').attr('errormsg')).fadeIn();
            $(_form).find('input[name=email]').focus();
        }
    } 
    catch (e) {
        
    }
    
    if (_r) {
    	$(_form).find('.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);
}

