﻿function showRecaptcha(element, submitButton, themeName) {
    Recaptcha.create("6LcEiwUAAAAAAFvukgZYqtpVhkokytwkKLJ-ulXT", element, {
        theme: themeName,
        tabindex: 0,
        callback: Recaptcha.focus_response_field
    });

}
function destroyRecaptchaWidget() {
    Recaptcha.destroy();
}

function validation() {
    //validation
    jQuery("#contactus_form").validate({
        rules: {
            first_name: "required",
            last_name: "required",
            email_address: {
                required: true,
                email: true
            },
            zip: {
                required: true,
                digits: true,
                minlength: 5
            },
            country: "required",
            Field2180641: "required",
            recaptcha_response_field: "required"
        },
        errorClass: "invalidLabel",
        messages: {
            first_name: "*",
            last_name: "*",
            email_address: {
                required: "*",
                email: "*"
            },
            zip: {
                required: "*",
                digits: "*",
                minlength: "*"
            },
            country: "*",
            Field2180641: "*",
            recaptcha_response_field: ""
        },
        highlight: function(element, errorClass) {
            jQuery(element).addClass('invalidField');

            //add css to inline style too to overrule css specificity on captcha
            if (jQuery(element).attr("id") == "recaptcha_response_field")
                jQuery(element).css("border", "1px solid red");
        },
        unhighlight: function(element, errorClass) {
            jQuery(element).removeClass('invalidField');

            //add css to inline style too to overrule css specificity on captcha
            if (jQuery(element).attr("id") == "recaptcha_response_field")
                jQuery(element).css("border", "1px solid gray");
        }
    });
}

//document ready
jQuery(function() {

    jQuery('.contact_link').click(function(event) {
		event.preventDefault();
        jQuery('body').append('<div id="dialog" title="Contact Us"></div>');
        jQuery('#dialog').dialog({
            autoOpen: false,
            modal: true,
            width: 333,
            height: 652,
            buttons: {
                "Send": function() {
                    if (jQuery("#contactus_form").valid())
                        jQuery("form").submit();
                },
                "Cancel": function() {
                    jQuery(this).dialog("close").dialog('destroy').remove();
                }
            }
        });

        jQuery('#dialog').load("http://www.umc.org/site/c.lwL4KnN1LtH/b.5337373/k.9A86/Ajax_Contact_Form.htm form", function() {
            jQuery('#dialog').dialog('open');
            jQuery.getScript("http://www.umc.org/atf/cf/%7Bdb6a45e4-c446-4248-82c8-e131b6424741%7D/jquery.validation.js", function() { validation(); });
            jQuery.getScript("http://www.umc.org/atf/cf/%7Bdb6a45e4-c446-4248-82c8-e131b6424741%7D/RECAPTCHA_AJAX.JS", function() {
                showRecaptcha('ajax_dynamic_recaptcha_1', 'submit_button_1', 'white');
            });
        });

    });


    //click off script...yesss!
    jQuery(".ui-widget-overlay").live("click", function() {
        jQuery('#dialog').dialog('close').dialog('destroy').remove();
		
    });


});