﻿(function($) {
    $.fn.bindValidators2 = function() {
        return this.each(function() {
            $(this).find('.digits').bind('keydown', function(event) {
                var keyCode = event.keyCode;
                if (keyCode == 46 || keyCode == 8 || keyCode == 9 ||  //allow only delete, backspace and tab
                                (event.shiftKey == false && ((keyCode >= 48 && keyCode <= 57) || (keyCode >= 96 && keyCode <= 105)))
                           ) {
                    //let it happen, don't do anything
                }
                else {
                    //Ensure that it is a number and stop the keypress                       
                    event.preventDefault();
                }
            });
            $(this).find('[class*=max]').bind('keydown', function(event) {
                var keyCode = event.keyCode;
                if (keyCode == 46 || keyCode == 8 || keyCode == 9) //allow delete, backspace and tab
                    return;

                var classNames = $(this).attr("className").split(" ");
                for (i = 0; i < classNames.length; i++) {
                    className = classNames[i];
                    if (/^max/i.test(className)) {
                        value = $(this).val();
                        max = parseInt(className.substr(3));
                        if (value.length == max) {
                            event.preventDefault();
                            return;
                        }
                    }
                }
            });
        });


    }

    $.fn.isValidForm2 = function() {
        $form = $(this);
        var valid = true;
        var message = '';

        var name = $form.find('#tbName2').val();
        var phone = $form.find('#tbPhone2').val();
        var prefix = $form.find('#tbPrefix2').val();
        var email = $form.find('#tbEmail2').val();

        if (name == '') {
            message += nameRequired + '<br />';
            valid = false;
        }
        if (email == '' && prefix == '' && phone == '') {
            message += phoneOrEmailRequired + '<br />';
            valid = false;
        }
        else if (prefix == '' ^ phone == '') {
            message += prefixAndPhoneRequired + '<br />';
            valid = false;
        }
        if (email != '' && !(/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+\s*([-.]\w+\s*)*$/i.test(email))) {
            message += emailInvalid + '<br />';
            valid = false;
        }
        if (phone != '' && prefix != '' && (!(/^[0-9]{4,20}$/i.test(phone)) || !(/^[0-9]{1,4}$/i.test(prefix)))) {
            message += phoneInvalid + '<br />';
            valid = false;
        }

        if (message !== '')
            $form.find('#message2').html(message);
        else
            $form.find('#message2').html('');

        return valid;
    }
})(jQuery);

function CloseContactFormDialog(contactFormID) {
    JQ('#' + contactFormID).dialog('close');
}

function OpenContactFormDialog(contactFormID, dialogTitle) {
    JQ('#' + contactFormID).dialog('option', 'title', dialogTitle.trim())
                        .dialog('option', 'position', ['center', 'center'])
                        .dialog('open');
}

function SaveBizFormInfo(contactFormID, webServiceUrl) {
    $contactForm = JQ(contactFormID);
    if ($contactForm.isValidForm2()) {
        $name = $contactForm.find('#tbName2');
        $phone = $contactForm.find('#tbPhone2');
        $prefix = $contactForm.find('#tbPrefix2');
        $email = $contactForm.find('#tbEmail2');

        var parameters = "{'name':'" + $name.val() + "','phone':'" + '+' + $prefix.val() + ' ' + $phone.val() + "','email':'" + $email.val() + "','buttonText':'" + $contactForm.dialog('option', 'title') + "'}";
        JQ.ajax({
            type: "POST",
            url: webServiceUrl,
            data: parameters,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(msg) {
                if (msg.d.success == true) {
                    $contactForm.find('.fields').hide();
                }
                $contactForm.find('#message2').html(msg.d.message);
            }
        });
    }
}
